package com.friday.custody.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.friday.custody.entity.FundInfoEntity;
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 FundInfoDao extends BaseMapper<FundInfoEntity> {

    String getFundCodeByInnerFundCode(@Param("innerFundCode") String innerFundCode,@Param("trustNo") String trustNo);

    List<String> getFundCodeList(@Param("trustNo") String trustNo, @Param("tenantName") String tenantName);

    String getFundCodeByFundCode(String fundCode);

    List<FundInfoEntity>  getDataByFundCode(@Param("fundCode") String fundCode,
                                            @Param("trustNo") String trustNo,
                                            @Param("admin") String admin);

    boolean updateStatus(FundInfoEntity entity);
    String selectFundNameByInnerFundCode(String innerFundCode, String trustNo);
    String selectFundNameByFundCode(@Param("fundCode") String fundCode, @Param("trustNo") String trustNo, @Param("admin")String admin);

    String selectInnerFundCodeByFundName(String fundName,String trustNo,String admin);

    List<FundInfoEntity>  getDataByTrustNo(String trustNo);

    List<String> getInnerFundCode(@Param("trustNo") String trustNo, @Param("tenantName") String tenantName);

    String getInnerFundCodeByInnerFundCode(String innerFundCode);

    String getFundCodeByName(String fundName, String trustNo);

    String getFundCodeByFundName(@Param("fundName") String fundName,
                                 @Param("trustNo") String trustNo,
                                 @Param("admin") String admin);

    default FundInfoEntity selectByFundName(String trustNo, String admin, String fundName){
        LambdaQueryWrapper<FundInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FundInfoEntity::getAdmin, admin)
                .eq(FundInfoEntity::getTrustNo, trustNo)
                .eq(FundInfoEntity::getFundName, fundName);
        FundInfoEntity fundInfoEntity = selectOne(queryWrapper);
        return fundInfoEntity;
    }

    default Set<String> queryUniqueKeyList(List<String> rawUniqueKeyList){
        LambdaQueryWrapper<FundInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(FundInfoEntity::getUniqueKey).in(FundInfoEntity::getUniqueKey, rawUniqueKeyList);
        List<FundInfoEntity> entityList = this.selectList(queryWrapper);
        Set<String> result = entityList.stream().map(t -> t.getUniqueKey()).collect(Collectors.toSet());
        return result;
    }

    default int deleteByUniqueKeys(Set<String> uniqueKeySet){
        LambdaQueryWrapper<FundInfoEntity> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.in(FundInfoEntity::getUniqueKey, uniqueKeySet);
        return delete(queryWrapper);
    }
}
