package com.friday.custody.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.friday.custody.dto.TenantEscrowAuthorizationInfoDto;
import com.friday.custody.entity.TenantEscrowAuthorizationInfoEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 
 * 
 * @author lyb
 * @email k0051582@gmail.com
 * @date 2022-01-05 14:07:22
 */
@Mapper
public interface TenantEscrowAuthorizationInfoDao extends BaseMapper<TenantEscrowAuthorizationInfoEntity> {

    int insertBatch(List<TenantEscrowAuthorizationInfoEntity> entityList);

    TenantEscrowAuthorizationInfoDto getAuthorizationByTrustNo(@Param("trustNo") String trustNo);
    List<TenantEscrowAuthorizationInfoDto> getDtoByTenantNameTrustNo(@Param("tenantName") String tenantName,@Param("trustNo") String trustNo);

    /**
     * 查询基金管理人编码
     * @return
     */
    default Set<String> queryAdminList(){
        LambdaQueryWrapper<TenantEscrowAuthorizationInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(TenantEscrowAuthorizationInfoEntity::getTenantName);
        queryWrapper.groupBy(TenantEscrowAuthorizationInfoEntity::getTenantName);
        List<TenantEscrowAuthorizationInfoEntity> entityList = this.selectList(queryWrapper);
        return entityList.stream().map(TenantEscrowAuthorizationInfoEntity::getTenantName).collect(Collectors.toSet());
    }

    default TenantEscrowAuthorizationInfoEntity queryByTenant(String trustNo, String admin){
        LambdaQueryWrapper<TenantEscrowAuthorizationInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TenantEscrowAuthorizationInfoEntity::getTrustNo, trustNo);
        queryWrapper.eq(TenantEscrowAuthorizationInfoEntity::getTenantName, admin);
        TenantEscrowAuthorizationInfoEntity entity = selectOne(queryWrapper);
        return entity;
    }

    default int updateByTenant(TenantEscrowAuthorizationInfoEntity authorizationInfo){
        LambdaUpdateWrapper<TenantEscrowAuthorizationInfoEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(TenantEscrowAuthorizationInfoEntity::getTenantName, authorizationInfo.getTenantName());
        updateWrapper.eq(TenantEscrowAuthorizationInfoEntity::getTrustNo, authorizationInfo.getTrustNo());
        return update(authorizationInfo, updateWrapper);
    }

    default List<TenantEscrowAuthorizationInfoEntity> queryList(String trustNo, String admin, String version){
        LambdaQueryWrapper<TenantEscrowAuthorizationInfoEntity> queryWrapper = new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(trustNo)){
            queryWrapper.eq(TenantEscrowAuthorizationInfoEntity::getTrustNo, trustNo);
        }
        queryWrapper.eq(TenantEscrowAuthorizationInfoEntity::getTenantName, admin);
        queryWrapper.eq(TenantEscrowAuthorizationInfoEntity::getIsDeleted, 0);
//        queryWrapper.eq(TenantEscrowAuthorizationInfoEntity::getVersion, version);
        List<TenantEscrowAuthorizationInfoEntity> entityList = selectList(queryWrapper);
        return entityList;
    }

    default int deleteByTrustInfo(String admin, String trustNo){
        LambdaQueryWrapper<TenantEscrowAuthorizationInfoEntity> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(TenantEscrowAuthorizationInfoEntity::getTenantName, admin);
        queryWrapper.eq(TenantEscrowAuthorizationInfoEntity::getTrustNo, trustNo);
        int delete = delete(queryWrapper);
        return delete;
    }
}
