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

import com.friday.custody.dto.TenantEscrowAuthorizationInfoDto;
import com.friday.custody.entity.FundTradeInfoEntity;
import com.friday.custody.enums.DataDictionaryTypeEnum;
import com.friday.custody.service.basicService.impl.FundInfoServiceImpl;
import com.friday.custody.service.basicService.impl.FundTradeInfoServiceImpl;
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 com.friday.custody.utils.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

@Service(GuosenConstants.TA_CONFIRM_INFO_LIST_API)
public class ProcessGuosenFundTradeInfoServiceImpl implements ProcessGuosenDataService {
    @Autowired
    private FundTradeInfoServiceImpl fundTradeInfoService;//f
    @Autowired
    private FundInfoServiceImpl fundInfoService;
    @Autowired
    private DataDictionaryBasicService dataDictionaryBasicService;
    @Override
    public void processData(InvokeGuosenRecordDto dto) {
        if (!CollectionUtils.isEmpty(dto.getRemoteDatas())) {
            //3、封装成标准表的对象
            List<FundTradeInfoEntity> entities = getFundTradeInfoData(dto.getTenant(),dto.getRemoteDatas() , dto.getTrustNo(),dto.getFundInnerCodeAndCodeMap());
            //4、调用 basicservice 的保存数据接口
            fundTradeInfoService.saveData(null, entities);
            //todo  入库到基金净值信息表（fund_net_value_info）时，需要此响应结果的产品代码、产品名称和估值日期与表中基金内部代码、基金名称和净值日期一致且响应结果中科目代码为“证券投资合计:”的数据，更新表中的资产合计total_asset字段为该值
        }
    }


    private List<FundTradeInfoEntity> getFundTradeInfoData(TenantEscrowAuthorizationInfoDto tenant, List<Object> remoteDatas, String trustNo, Map<String, String> fundInnerCodeAndCodeMap) {
        //1、获取字典表数据
        Map<String, String> businessTypeDic = dataDictionaryBasicService.queryWithDetail(DataDictionaryTypeEnum.BUSINESS_NAME_SERVICE, trustNo);
        Map<String, String> sellerDic = dataDictionaryBasicService.queryWithDetail(DataDictionaryTypeEnum.SELLER_SERVICE, "国信证券");
        //2、封装数据
        List<FundTradeInfoEntity> 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 = fundInnerCodeAndCodeMap.get(remoteDataMap.get("structuredFundCode"));
//            }
            String fundCode = fundInfoService.getFundCodeByInnerFundCode(remoteDataMap.get("structuredFundCode"),trustNo);
            //1 todo fundAcco 缺失数据   agencyNo适配
            result.add(FundTradeInfoEntity.builder()
                    .innerFundCode(remoteDataMap.get("structuredFundCode"))
                    .fundName(remoteDataMap.get("structuredFundName"))
                    .requestDate(remoteDataMap.get("applyDate"))
                    .confirmDate(remoteDataMap.get("confirmDate"))
                    .applyBalance(BigDecimalUtil.obj2BigDecimal(remoteDataMap.get("requestBalance")))
                    .applyShares(BigDecimalUtil.obj2BigDecimal(remoteDataMap.get("requestShare")))
                    .confirmBalance(BigDecimalUtil.obj2BigDecimal(remoteDataMap.get("confirmMoney")))
                    .confirmShares(BigDecimalUtil.obj2BigDecimal(remoteDataMap.get("confirmShare")))
                    .agencyNo(MatchDataDictionaryKeyUtil.getKey(sellerDic,remoteDataMap.get("agencyNumber")))
                    .agencyName(remoteDataMap.get("agency"))
                    .fundAcco(remoteDataMap.get("fundAccount"))
                    .tradeAcco(remoteDataMap.get("tradeAccount"))
                    .tradeFare(BigDecimalUtil.obj2BigDecimal(remoteDataMap.get("tradeFare")))
                    .totalFare(BigDecimalUtil.obj2BigDecimal(remoteDataMap.get("totalFare")))
                    .interest(BigDecimalUtil.obj2BigDecimal(remoteDataMap.get("interest")))
                    .interestShare(BigDecimalUtil.obj2BigDecimal(remoteDataMap.get("interestShare")))
                    .profitBalance(BigDecimalUtil.obj2BigDecimal(remoteDataMap.get("profitBalance")))
                    .netValue(BigDecimalUtil.obj2BigDecimal(remoteDataMap.get("netValue"), 4))
                    .businessName(MatchDataDictionaryKeyUtil.getKey(businessTypeDic, remoteDataMap.get("businessFlag")))
                    .requestNo(StrUtil.subString(remoteDataMap.get("requestNnumber")))
                    .cserialNo(StrUtil.subString(remoteDataMap.get("confirmNumber")))
                    .fundCode(fundCode)
                    .trustNo(trustNo)
                    .admin(tenant.getTenantName())
                    .createTime(now)
                    .updateTime(now)
                    .build());
        }
        return result;

    }
}
