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.FundNetValueInfoEntity;
import com.friday.operation.api.custody.dto.MaximumDrawdownDTO;
import com.friday.operation.api.custody.dto.SharpRatioDTO;
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 FundNetValueInfoDao extends BaseMapper<FundNetValueInfoEntity> {

    boolean insertOrUpdate(FundNetValueInfoEntity entity);

    boolean insertOrUpdateSome(FundNetValueInfoEntity entity);

    FundNetValueInfoEntity selectByUniqueKey(FundNetValueInfoEntity req);

    FundNetValueInfoEntity getSharesByFundInfo(FundNetValueInfoEntity req);

    String getFundCodeByInnerFundCode(String fundCode);

    int updateTotalAssetData(FundNetValueInfoEntity entity);

    default Set<String> queryUniqueKeyList(List<String> uniqueKeyList){
        LambdaQueryWrapper<FundNetValueInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(FundNetValueInfoEntity::getUniqueKey).in(FundNetValueInfoEntity::getUniqueKey, uniqueKeyList);
        List<FundNetValueInfoEntity> 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<FundNetValueInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(FundNetValueInfoEntity::getUniqueKey, uniqueKeySet);
        return delete(queryWrapper);
    }

    default List<FundNetValueInfoEntity> getFundNetInfoByTradingDayList(String admin, List<String> tradingDays){
        LambdaQueryWrapper<FundNetValueInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FundNetValueInfoEntity::getAdmin, admin);
        queryWrapper.in(FundNetValueInfoEntity::getNetValueDate, tradingDays);
        return selectList(queryWrapper);
    }

    /**
     * 计算最大回撤
     * @return
     */
    List<MaximumDrawdownDTO> getMaximumDrawdown(@Param("admin") String admin,
                                                @Param("fundCodeList") List<String> fundCodeList,
                                                @Param("startDate") String startDate,
                                                @Param("endDate") String endDate);

    List<SharpRatioDTO> getSharpRatio(@Param("admin") String admin,
                                      @Param("startDate") String startDate,
                                      @Param("endDate") String endDate,
                                      @Param("fundCodeList") List<String> fundCodeList,
                                      @Param("tradingDayList") List<String> tradingDayList,
                                      @Param("riskFreeRate") Double riskFreeRate);


}
