package com.simuwang.manage.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.simuwang.base.common.util.DateUtils;
import com.simuwang.base.common.util.StringUtil;
import com.simuwang.base.mapper.AssetMapper;
import com.simuwang.base.mapper.EmailFundAssetMapper;
import com.simuwang.base.pojo.dos.AssetDO;
import com.simuwang.base.pojo.dos.EmailFundAssetDO;
import com.simuwang.base.pojo.dos.EmailFundNavDO;
import com.simuwang.base.pojo.dos.NavDO;
import com.simuwang.daq.service.EmailParseService;
import com.simuwang.manage.service.EmailFundAssetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * FileName: EmailFundAssetServiceImpl
 * Author:   chenjianhua
 * Date:     2024/9/15 12:43
 * Description: ${DESCRIPTION}
 */
@Service
public class EmailFundAssetServiceImpl implements EmailFundAssetService {

    @Autowired
    private EmailFundAssetMapper emailFundAssetMapper;

    @Autowired
    private EmailParseService emailParseService;

    @Autowired
    private AssetMapper assetMapper;

    @Override
    public void reparseFileAsset(String sourceFundName,String registerNumber, String targetFundId,String sourceFundId) {
        List<EmailFundAssetDO> fundAssetDOList = emailFundAssetMapper.selectNotMappingAsset(sourceFundName,registerNumber);
        Map<String,List<EmailFundAssetDO>> fundAssetDOGroup = fundAssetDOList.stream().collect(Collectors.groupingBy(e -> DateUtils.format(e.getPriceDate(),DateUtils.YYYY_MM_DD)));
        List<EmailFundAssetDO> dataList = new ArrayList<>();
        //去重
        for(String priceDate : fundAssetDOGroup.keySet()){
            List<EmailFundAssetDO> doList = fundAssetDOGroup.get(priceDate);
            dataList.add(doList.get(0));
        }
        if(fundAssetDOList.size() > 0){
            //修改采集库的数据
            for(EmailFundAssetDO fundNavDO : fundAssetDOList){
                fundNavDO.setFundId(targetFundId);
                fundNavDO.setExceptionStatus(1);
                fundNavDO.setIsvalid(1);
                fundNavDO.setIsStored(1);
                fundNavDO.setUpdateTime(new Date());
                fundNavDO.setCreateTime(new Date());
            }
            emailFundAssetMapper.batchUpdate(fundAssetDOList);
            //净值入库
            List<AssetDO> assetDOList = dataList.stream().filter(e -> StrUtil.isNotBlank(e.getFundId()))
                    .map(e -> BeanUtil.copyProperties(e, AssetDO.class)).collect(Collectors.toList());
            emailParseService.saveAssetDo(assetDOList);

            if(StringUtil.isNotEmpty(sourceFundId)){
                List<String> priceDateList = new ArrayList<>();
                for(EmailFundAssetDO fundAssetDO : fundAssetDOList){
                    priceDateList.add(DateUtils.format(fundAssetDO.getPriceDate(),DateUtils.YYYY_MM_DD));
                }
                //将匹配错的数据置为无效
                assetMapper.batchDeleteAsset(sourceFundId,priceDateList);
                //从解析库把原来的规模数据重新写入到sourceFundId对应的基金上
                List<EmailFundAssetDO> sourceAssetList = emailFundAssetMapper.selectAssetByFundId(sourceFundId);
                List<AssetDO> sourceAssetDOList = sourceAssetList.stream().filter(e -> StrUtil.isNotBlank(e.getFundId()))
                        .map(e -> BeanUtil.copyProperties(e, AssetDO.class)).collect(Collectors.toList());
                sourceAssetDOList.forEach(e -> e.setUpdateTime(DateUtils.getNowDate()));
                emailParseService.saveAssetDo(sourceAssetDOList);
            }

        }
    }
}
