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

import com.friday.custody.dto.TenantEscrowAuthorizationInfoDto;
import com.friday.custody.entity.FundFeeDataInfoEntity;
import com.friday.custody.enums.DataDictionaryTypeEnum;
import com.friday.custody.service.basicService.impl.FundFeeDataInfoServiceImpl;
import com.friday.custody.service.basicService.impl.FundInfoServiceImpl;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

@Service(GuosenConstants.EXPENSE_QUERYLIST)
@Slf4j
public class PrcoessGuosenFundFeeDataInfoServiceImpl implements ProcessGuosenDataService {

    @Autowired
    private FundInfoServiceImpl fundInfoService;

    @Autowired
    private FundFeeDataInfoServiceImpl fundFeeDataInfoService;

    @Autowired
    private DataDictionaryBasicService dataDictionaryBasicService;

    @Override
    public void processData(InvokeGuosenRecordDto dto) {
        if (!CollectionUtils.isEmpty(dto.getRemoteDatas())) {
            log.info("国信获取fundFeeDataInfo数据{}条" , dto.getRemoteDatas().size());
            //3、封装成标准表的对象
            List<FundFeeDataInfoEntity> entities = getFundFeeDataInfo(dto.getTenant(),dto.getRemoteDatas() , dto.getTrustNo(), dto.getFundInnerCodeAndCodeMap());
            log.info("国信封装fundFeeDataInfo数据{}条" ,entities.size());
            //4、调用 basicservice 的保存数据接口
            fundFeeDataInfoService.saveData(null, entities);
        }
    }

    private List<FundFeeDataInfoEntity> getFundFeeDataInfo(TenantEscrowAuthorizationInfoDto tenant, List<Object> remoteDatas, String trustNo, Map<String, String> fundInnerCodeAndCodeMap) {
        //1.获取字典
        Map<String, String> typesOfFeeDic = dataDictionaryBasicService.queryWithDetail(DataDictionaryTypeEnum.TYPES_OF_FEE_SERVICE, trustNo);
        //2.封装数据
        List<FundFeeDataInfoEntity> entityList = 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);
            entityList.add(FundFeeDataInfoEntity.builder()
                    .fundName(remoteDataMap.get("fundName"))
                    .innerFundCode(remoteDataMap.get("fundCode"))
                    .fareDebit(BigDecimalUtil.obj2BigDecimalTrunToZero(remoteDataMap.get("debit")))
                    .fareCredit(BigDecimalUtil.obj2BigDecimalTrunToZero(remoteDataMap.get("credit")))
                    .confirmDate(remoteDataMap.get("valuationDate"))
                    .subjectCode(MatchDataDictionaryKeyUtil.getKey(typesOfFeeDic,remoteDataMap.get("subjectName")))
                    .subjectName(remoteDataMap.get("subjectName"))
                    .fundCode(fundCode)
                    .trustNo(trustNo)
                    .admin(tenant.getTenantName())
                    .createTime(now)
                    .updateTime(now)
                    .build());

        }
        return entityList;
    }
}
