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

import com.friday.custody.dto.TenantEscrowAuthorizationInfoDto;
import com.friday.custody.entity.FundProfitInfoEntity;
import com.friday.custody.enums.DataDictionaryTypeEnum;
import com.friday.custody.service.basicService.impl.FundInfoServiceImpl;
import com.friday.custody.service.basicService.impl.FundProfitInfoServiceImpl;
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.PERSONAL_REWARD_LIST_API)
public class ProcessGuosenFundProfitInfoServiceImpl implements ProcessGuosenDataService {

    @Autowired
    private FundProfitInfoServiceImpl fundProfitInfoService;//c

    @Autowired
    private FundInfoServiceImpl fundInfoService;
    @Autowired
    private DataDictionaryBasicService dataDictionaryBasicService;
    @Override
    public void processData(InvokeGuosenRecordDto dto) {
        if (!CollectionUtils.isEmpty(dto.getRemoteDatas())) {
            //3、封装成标准表的对象
            List<FundProfitInfoEntity> entities = getProfitInfoData(dto.getTenant(),dto.getRemoteDatas() , dto.getTrustNo(), dto.getFundInnerCodeAndCodeMap());
            //4、调用 basicservice 的保存数据接口
            fundProfitInfoService.saveData(null, entities);
        }
    }
    private List<FundProfitInfoEntity> getProfitInfoData(TenantEscrowAuthorizationInfoDto tenant, List<Object> remoteDatas, String trustNo, Map<String, String> fundInnerCodeAndCodeMap) {
        //1、获取字典表数据
        Map<String, String> businessNameDic = dataDictionaryBasicService.queryWithDetail(DataDictionaryTypeEnum.BUSINESS_NAME_SERVICE, trustNo);
        Map<String, String> sellerDic = dataDictionaryBasicService.queryWithDetail(DataDictionaryTypeEnum.SELLER_SERVICE, "国信证券");
        //2、封装数据
        List<FundProfitInfoEntity> result = new ArrayList<>();
        HashMap<String, String> remoteDataMap;
        Date now = new Date();
        for (Object remoteData : remoteDatas) {
            remoteDataMap = (HashMap<String, String>) remoteData;
            String fundCode = fundInfoService.getFundCodeByInnerFundCode(remoteDataMap.get("fundCode"),trustNo);
            result.add(FundProfitInfoEntity.builder()
                    .businessName(MatchDataDictionaryKeyUtil.getKey(businessNameDic,remoteDataMap.get("bizType")))
                    .confirmDate(remoteDataMap.get("confirmDate"))
                    .fundAcco(remoteDataMap.get("fundAccount"))
                    .innerFundCode(remoteDataMap.get("fundCode"))
                    .fundName(remoteDataMap.get("fundName"))
                    .agencyName(remoteDataMap.get("agencyName"))
                    .agencyNo(MatchDataDictionaryKeyUtil.getKey(sellerDic,remoteDataMap.get("agencyNo")))
                    .branchCode(remoteDataMap.get("netNo"))
                    .tradeAcco(remoteDataMap.get("tradeAccount"))
                    .shares(BigDecimalUtil.obj2BigDecimal(remoteDataMap.get("shares")))
                    .oriDate(remoteDataMap.get("beginDate"))
                    .nav(BigDecimalUtil.obj2BigDecimal(remoteDataMap.get("endingValue"),4))
                    .totalNav(BigDecimalUtil.obj2BigDecimal(remoteDataMap.get("totalEndValue"),4))
                    .factBalance(BigDecimalUtil.obj2BigDecimal(remoteDataMap.get("factBalance")))
                    .cserialNo(StrUtil.getSeqNo(remoteDataMap.get("confirmNo")))
                    .fundCode(fundCode)
                    .trustNo(trustNo)
                    .admin(tenant.getTenantName())
                    .createTime(now)
                    .updateTime(now)
                    .build());
        }
        return result;
    }
}
