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

import com.friday.custody.dto.TenantEscrowAuthorizationInfoDto;
import com.friday.custody.entity.FundOpenInfoEntity;
import com.friday.custody.enums.DataDictionaryTypeEnum;
import com.friday.custody.service.basicService.impl.DataDictionaryOpenStateServiceImpl;
import com.friday.custody.service.basicService.impl.FundOpenInfoServiceImpl;
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.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.FUND_OPEN_INFO)
@Slf4j
public class PrcoessGuosenFundOpenInfoServiceImpl implements ProcessGuosenDataService {

    @Autowired
    private FundOpenInfoServiceImpl fundOpenInfoService;
    @Autowired
    private DataDictionaryBasicService dataDictionaryBasicService;
    @Autowired
    private DataDictionaryOpenStateServiceImpl openStateService;

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

    private List<FundOpenInfoEntity> getFundOpenInfo(TenantEscrowAuthorizationInfoDto tenant, List<Object> remoteDatas, String trustNo, Map<String, String> fundInnerCodeAndCodeMap) {
        //获取数据字典
        //开放日类型
        Map<String, String> typeOfOpenDayDic = dataDictionaryBasicService.queryWithDetail(DataDictionaryTypeEnum.TYPE_OF_OPEN_DAY_SERVICE, trustNo);
        //开放状态
        Map<String, Map<String, String>> openStateDic = openStateService.queryOpenStateWithDetail(trustNo);
        //封装数据
        List<FundOpenInfoEntity> entityList = new ArrayList<>();
        Map<String, String> dataMap;
        for (Object data : remoteDatas) {
            dataMap = (HashMap<String, String>) data;
            String openType = MatchDataDictionaryKeyUtil.getKey(typeOfOpenDayDic, dataMap.get("openMode"));
            log.info("国信获取上游openType = {}, 转义之后openType = {}", dataMap.get("openMode"), openType);
            String openStatus = MatchDataDictionaryKeyUtil.getKey(openType, openStateDic, dataMap.get("openStatus"));
            log.info("国信获取上游openStatus = {}, 转义之后openStatus = {}", dataMap.get("openStatus"), openStatus);
            entityList.add(FundOpenInfoEntity.builder()
                    .fundCode(dataMap.get("fundCode"))
                    .innerFundCode(dataMap.get("structuredFundCode"))
                    .fundName(dataMap.get("structuredFundName"))
                    .fundOpenDate(dataMap.get("openDay"))
                    .openType(openType)
                    .openStatus(openStatus)
                    .trustNo(trustNo)
                    .admin(tenant.getTenantName())
                    .createTime(new Date())
                    .updateTime(new Date())
                    .build());
        }
        return entityList;
    }


}
