package com.friday.custody.service.trustService.guosen.service.impl;

import com.friday.custody.dto.TenantEscrowAuthorizationInfoDto;
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.basicService.impl.FundNetValueInfoServiceImpl;
import com.friday.custody.service.logicService.DataDictionaryBasicService;
import com.friday.custody.service.trustService.guosen.common.GuosenConstants;
import com.friday.custody.service.trustService.guosen.dto.InvokeGuosenRecordDto;
import com.friday.custody.service.trustService.guosen.service.ProcessGuosenDataService;
import com.friday.custody.utils.BigDecimalUtil;
import com.friday.custody.utils.MatchDataDictionaryKeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

@Service(GuosenConstants.NET_VALUE_INFO_LIST_API)
public class ProcessGuosenFundNetValueInfoServiceImpl implements ProcessGuosenDataService {

    @Autowired
    private FundNetValueInfoServiceImpl fundNetValueInfoService;//c
    @Autowired
    private FundInfoServiceImpl fundInfoService;//c
    @Autowired
    private DataDictionaryBasicService dataDictionaryBasicService;
    @Override
    public void processData(InvokeGuosenRecordDto dto) {
        if (!CollectionUtils.isEmpty(dto.getRemoteDatas())) {
            //3、封装成标准表的对象
            List<FundNetValueInfoEntity> entities = getFundNetValueInfoData(dto.getTenant(),dto.getRemoteDatas() , dto.getTrustNo(), dto.getFundInnerCodeAndCodeMap());
            //4、调用 basicservice 的保存数据接口
            fundNetValueInfoService.saveData(null, entities);
            //todo  入库到基金净值信息表（fund_net_value_info）时，需要此响应结果的产品代码、产品名称和估值日期与表中基金内部代码、基金名称和净值日期一致且响应结果中科目代码为“证券投资合计:”的数据，更新表中的资产合计total_asset字段为该值
        }
    }
    private List<FundNetValueInfoEntity> getFundNetValueInfoData(TenantEscrowAuthorizationInfoDto tenant, List<Object> remoteDatas, String trustNo, Map<String, String> fundInnerCodeAndCodeMap) {
        //1、获取字典表数据
        //todo 数据字典 复核状态
        Map<String, String> reviewStatusDic = dataDictionaryBasicService.queryWithDetail(DataDictionaryTypeEnum.REVIEW_STATUS_SERVICE, trustNo);
        //2、封装数据
        List<FundNetValueInfoEntity> result = new ArrayList<>();
        HashMap<String, String> remoteDataMap;
        Date now = new Date();
        for (Object remoteData : remoteDatas) {
            remoteDataMap = (HashMap<String, String>) remoteData;
//            String fundCode = null;
//            if (fundInnerCodeAndCodeMap != null && fundInnerCodeAndCodeMap.size() > 0) {
//                fundCode = StrUtil.getFundCode(fundInnerCodeAndCodeMap,remoteDataMap.get("fundCode"));
//            }
            String fundCode = fundInfoService.getFundCodeByInnerFundCode(remoteDataMap.get("fundCode"),trustNo);
            result.add(FundNetValueInfoEntity.builder()
                    .fundName(remoteDataMap.get("fundName"))
                    .innerFundCode(remoteDataMap.get("fundCode"))
                    .netValueDate(remoteDataMap.get("netValueDate"))
                    .checkStatus(MatchDataDictionaryKeyUtil.getKey(reviewStatusDic,remoteDataMap.get("checkStatus")))
                    .netValue(BigDecimalUtil.obj2BigDecimal(remoteDataMap.get("netValue"),4))
                    .totalNetValue(BigDecimalUtil.obj2BigDecimal(remoteDataMap.get("totalNetValue"),4))
                    .assetValue(BigDecimalUtil.obj2BigDecimal(remoteDataMap.get("fundAssets")))
                    .shares(BigDecimalUtil.obj2BigDecimal(remoteDataMap.get("fundShares")))
                    .uniqueKey(tenant.getTenantName() + remoteDataMap.get("fundCode") + remoteDataMap.get("fundName") + remoteDataMap.get("netValueDate"))
                    .fundCode(fundCode)
                    .trustNo(trustNo)
                    .admin(tenant.getTenantName())
                    .createTime(now)
                    .updateTime(now)
                    .build());
        }
        return result;
    }
}
