package com.simuwang.manage.service.impl;

import com.simuwang.base.common.support.MybatisPage;
import com.simuwang.base.mapper.CompanyInformationMapper;
import com.simuwang.base.mapper.EmailFundAssetMapper;
import com.simuwang.base.mapper.EmailFundNavMapper;
import com.simuwang.base.mapper.FundAliasMapper;
import com.simuwang.base.pojo.dos.FundAliasDO;
import com.simuwang.base.pojo.dto.query.FundAliasPageQuery;
import com.simuwang.base.pojo.vo.FundAliasVO;
import com.simuwang.manage.service.EmailFundAssetService;
import com.simuwang.manage.service.EmailFundNavService;
import com.simuwang.manage.service.EmailFundInfoService;
import com.simuwang.manage.service.FundAliasService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * FileName: FunAliasServiceImpl
 * Author:   chenjianhua
 * Date:     2024/9/14 16:18
 * Description: ${DESCRIPTION}
 */
@Service
public class FundAliasServiceImpl implements FundAliasService {
    @Autowired
    private FundAliasMapper fundAliasMapper;

    @Autowired
    private EmailFundNavMapper emailFundNavMapper;

    @Autowired
    private EmailFundAssetMapper emailFundAssetMapper;

    @Autowired
    private CompanyInformationMapper companyInformationMapper;

    @Autowired
    private EmailFundInfoService emailFundInfoService;

    @Autowired
    private EmailFundNavService emailFileNavService;

    @Autowired
    private EmailFundAssetService emailFundAssetService;

    @Override
    public MybatisPage<FundAliasVO> searchFundAlias(FundAliasPageQuery fundAliasPageQuery) {
        List<FundAliasDO> fundAliasDOList = fundAliasMapper.searchFundAlias(fundAliasPageQuery);
        List<FundAliasVO> fundAliasVOList =  fundAliasDOList.stream().map(FundAliasDO::toVo).collect(Collectors.toList());
        for(FundAliasVO vo : fundAliasVOList){
            //异常净值数量
            Integer navTotal = emailFundNavMapper.countNoStoreNav(vo.getSourceFundName());
            vo.setNavTotal(navTotal);
            //异常净值数量
            Integer assetTotal = emailFundAssetMapper.countNoStoreAsset(vo.getSourceFundName());
            vo.setAssetTotal(assetTotal);
            //管理人
            if(vo.getTargetFundId() !=null){
                vo.setCompanyName(companyInformationMapper.getCompanyNameByFundId(vo.getTargetFundId()));
            }
        }
        long total = fundAliasMapper.countFundAlias(fundAliasPageQuery);
        return MybatisPage.of(total,fundAliasVOList);
    }

    @Override
    public FundAliasVO searchFundAliasById(Integer id) {
        FundAliasDO fundAliasDO = fundAliasMapper.searchFundAliasById(id);
        return fundAliasDO.toVo();
    }

    @Override
    public void saveFundAlias(FundAliasVO fundAliasVO) {
        FundAliasDO fundAliasDO = new FundAliasDO();
        fundAliasDO.setSourceFundName(fundAliasVO.getSourceFundName());
        fundAliasDO.setTargetFundId(fundAliasVO.getTargetFundId());
        fundAliasDO.setSourceRegisterNumber(fundAliasVO.getSourceRegisterNumber());
        fundAliasDO.setTargetFundName(fundAliasVO.getTargetFundName());
        fundAliasDO.setTargetRegisterNumber(fundAliasVO.getTargetRegisterNumber());
        fundAliasDO.setId(fundAliasVO.getId());
        fundAliasDO.setIsvalid(1);
        fundAliasDO.setUpdateTime(new Date());
        fundAliasMapper.saveFundAlias(fundAliasDO);
        //将采集的净值数据匹配之后重新入库
        emailFileNavService.reparseFileNav(fundAliasVO.getSourceFundName(),fundAliasVO.getTargetFundId());
        //将采集的规模数据匹配之后重新入库
        emailFundAssetService.reparseFileAsset(fundAliasVO.getSourceFundName(),fundAliasVO.getTargetFundId());
        //处理采集数据，将采集的数据入库
        emailFundInfoService.reparseValuationFile(fundAliasVO.getSourceFundName());
    }
}
