package com.friday.custody.service.trustService.gf.utils;

import com.friday.custody.dto.GuangfaFundNetValueDTO;
import com.friday.custody.dto.TenantEscrowAuthorizationInfoDto;
import com.friday.custody.entity.FundInfoEntity;
import com.friday.custody.entity.FundNetValueInfoEntity;
import com.friday.custody.enums.DataDictionaryTypeEnum;
import com.friday.custody.service.basicService.impl.FundInfoServiceImpl;
import com.friday.custody.service.logicService.impl.DataDictionaryBasicServiceImpl;
import com.friday.custody.utils.BigDecimalUtil;
import com.friday.custody.utils.MatchDataDictionaryKeyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class GFConverter  {

    @Autowired
    private FundInfoServiceImpl fundInfoService;
    @Autowired
    private DataDictionaryBasicServiceImpl dataDictionaryBasicService;

    public List<FundNetValueInfoEntity> fundNetConvert(List<GuangfaFundNetValueDTO> dtoList, TenantEscrowAuthorizationInfoDto tenant) {
        List<FundNetValueInfoEntity> result = new ArrayList<>();
        //获取数据字典
        Map<String, String> currencyDir = dataDictionaryBasicService.queryWithDetail(DataDictionaryTypeEnum.CURRENCY_SERVICE, tenant.getTrustNo());
        Set<String> fundNameSet = dtoList.stream().filter(x -> org.apache.commons.lang.StringUtils.isNotBlank(x.getProductCname()))
                .map(GuangfaFundNetValueDTO::getProductCname).collect(Collectors.toSet());
        if (CollectionUtils.isEmpty(fundNameSet)){
            log.info("广发封装fundNetValue数据为空!");
            return null;
        }
        //根据fundName分组
        Map <String,List <GuangfaFundNetValueDTO>> dtoMap = dtoList.stream().collect(Collectors.groupingBy(GuangfaFundNetValueDTO::getProductCname));
       for(String fundName : fundNameSet){
            List<GuangfaFundNetValueDTO> fundNetValueDTOList = dtoMap.get(fundName);
            FundInfoEntity fundInfoEntity = fundInfoService.queryByFundName(tenant.getTrustNo(), tenant.getTenantName(), fundNetValueDTOList.get(0).getProductCname());
            String fundCode = null;
            String innerFundCode = null;
            if (!ObjectUtils.isEmpty(fundInfoEntity)){
                fundCode = fundInfoEntity.getFundCode();
                innerFundCode = fundInfoEntity.getInnerFundCode();
            }
            FundNetValueInfoEntity fundNetValueInfoEntity = FundNetValueInfoEntity.builder()
                    .fundName(fundNetValueDTOList.get(0).getProductCname())
                    .fundCode(fundCode)
                    .innerFundCode(innerFundCode)
                    .trustNo(tenant.getTrustNo())
                    .admin(tenant.getTenantName())
                    .createTime(new Date())
                    .updateTime(new Date())
                    .build();
            for(GuangfaFundNetValueDTO dto : fundNetValueDTOList){
                if (StringUtils.isEmpty(dto.getLSfqr())){
                    continue;
                }
                if ("1".equals(dto.getLSfqr())) {
                    String vcKmdm = dto.getVcKmdm();
                    BigDecimal  kmmc = BigDecimalUtil.str2BigDecimal(dto.getVcKmmc(), 4);
                    BigDecimal  ensz = BigDecimalUtil.str2BigDecimalDefaultZero(dto.getEnSZ());
                    if ("实收资本".equals(vcKmdm)){
                        fundNetValueInfoEntity.setShares(ensz);
                    }
                    if ("资产类合计:".equals(vcKmdm)){
                        fundNetValueInfoEntity.setTotalAsset(ensz);
                    }
                    if ("基金资产净值:".equals(vcKmdm)){
                        fundNetValueInfoEntity.setAssetValue(ensz);
                    }
                    if ("基金单位净值：".equals(vcKmdm)){
                        fundNetValueInfoEntity.setNetValue(ensz);
                    }
                    if ("累计单位净值:".equals(vcKmdm)){
                        fundNetValueInfoEntity.setTotalNetValue(kmmc);
                    }
                    fundNetValueInfoEntity.setNetValueDate(dto.getDYwrq());
                    fundNetValueInfoEntity.setCurrency(StringUtils.isEmpty(dto.getVcJsbz()) ? "156" : MatchDataDictionaryKeyUtil.getKey(currencyDir, dto.getVcJsbz()));
                }
            }
            result.add(fundNetValueInfoEntity);
        }
        return result;
    }
}
