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

import com.friday.custody.dto.TenantEscrowAuthorizationInfoDto;
import com.friday.custody.entity.CustShareSeqInfoEntity;
import com.friday.custody.enums.DataDictionaryTypeEnum;
import com.friday.custody.service.basicService.impl.CustShareSeqInfoServiceImpl;
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 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.CUSTOMER_SHARE_CURRENT_DETAIL_LIST_API)
public class ProcessGuosenCustShareSeqInfoServiceImpl implements ProcessGuosenDataService {

    @Autowired
    private CustShareSeqInfoServiceImpl custShareSeqInfoService;//c

    @Autowired
    private FundInfoServiceImpl fundInfoService;
    @Autowired
    private DataDictionaryBasicService dataDictionaryBasicService;
    @Override
    public void processData(InvokeGuosenRecordDto dto) {
        if (!CollectionUtils.isEmpty(dto.getRemoteDatas())) {
            //3、封装成标准表的对象
            List<CustShareSeqInfoEntity> entities = getCustShareSeqInfoData(dto.getTenant(),dto.getRemoteDatas() , dto.getTrustNo(), dto.getFundInnerCodeAndCodeMap());
            //4、调用 basicservice 的保存数据接口
            custShareSeqInfoService.saveData(null, entities);
        }
    }
    private List<CustShareSeqInfoEntity> getCustShareSeqInfoData(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<CustShareSeqInfoEntity> 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("structuredFundCode"),trustNo);
            result.add(CustShareSeqInfoEntity.builder()
                    .confirmDate(remoteDataMap.get("cdate"))
                    .fundAcco(remoteDataMap.get("fundAccount"))
                    .tradeAcco(remoteDataMap.get("tradeAccount"))
                    .occurShares(BigDecimalUtil.obj2BigDecimal(remoteDataMap.get("occurShares")))
                    .occurBalance(BigDecimalUtil.obj2BigDecimal(remoteDataMap.get("occurBalance")))
                    .lastHoldShares(BigDecimalUtil.obj2BigDecimal(remoteDataMap.get("lastShares")))
                    .occurSharesFreeze(BigDecimalUtil.obj2BigDecimal(remoteDataMap.get("occurSharesFreeze")))
                    .lastFreezeShare(BigDecimalUtil.obj2BigDecimal(remoteDataMap.get("lastFreezeShare")))
                    .agencyNo(MatchDataDictionaryKeyUtil.getKey(sellerDic,remoteDataMap.get("agencyNumber")))
                    .agencyName(remoteDataMap.get("agencyName"))
                    .innerFundCode(remoteDataMap.get("structuredFundCode"))
                    .fundName(remoteDataMap.get("structuredFundName"))
                    .seqNo(StrUtil.getSeqNo(null))
                    .fundCode(fundCode)
                    .trustNo(trustNo)
                    .admin(tenant.getTenantName())
                    .createTime(now)
                    .updateTime(now)
                    .build());
        }
        return result;
    }
}
