package com.friday.custody.api.util;


import com.alibaba.fastjson.JSONObject;
import com.friday.custody.entity.*;
import com.friday.custody.service.basicService.DataDictionaryTypeCertificateCsdcService;
import com.friday.custody.service.basicService.impl.CustShareSeqInfoServiceImpl;
import com.friday.custody.service.basicService.impl.DataDictionaryOpenStateServiceImpl;
import com.friday.custody.service.basicService.impl.DataDictionarySellerServiceImpl;
import com.friday.custody.service.logicService.DataDictionaryBasicService;
import com.friday.custody.service.trustService.guojindaofu.dto.*;
import com.friday.custody.service.trustService.guojindaofu.service.impl.GuoJinDaoFuServiceImpl;
import com.friday.custody.enums.DataDictionaryTypeEnum;
import com.friday.custody.utils.MatchDataDictionaryKeyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
@Slf4j
public class GJDFInfoToEntityConvertor {
    @Autowired
    private DataDictionaryBasicService dataDictionaryBasicService;
    @Autowired
    private DataDictionaryTypeCertificateCsdcService certificateCsdcService;
    @Autowired
    private DataDictionaryOpenStateServiceImpl openStateService;

    /**
     * 基金信息
     * @param gjdfFundInfo
     * @param admin
     */
    public static FundInfoEntity getFundInfo(GJDFFundInfo gjdfFundInfo  , String admin){
        return FundInfoEntity.builder()
                .innerFundCode(gjdfFundInfo.getFundcode())
                .fundName(gjdfFundInfo.getFundname())
                .fundStatus(gjdfFundInfo.getFundstatus())
                .durationPeriod(gjdfFundInfo.getTimelimit())
                .fundCode(gjdfFundInfo.getFormalfundcode())
                .createDate(gjdfFundInfo.getSetupdate())
                .trustNo("98")
                .admin(admin)
                .createTime(new Date())
                .updateTime(new Date())
                .build();
    }
    public List<FundInfoEntity> fundInfoDtoToEntity(List<GJDFFundInfo> gjdfFundInfoList,String admin) {
        List<FundInfoEntity> collect = gjdfFundInfoList.stream()
                .map((GJDFFundInfo gjdfFundInfo) -> getFundInfo(gjdfFundInfo, admin))
                .collect(Collectors.toList());
        //获取产品状态数据字典
        Map<String, String> productStatusDictionary = dataDictionaryBasicService.queryWithDetail(DataDictionaryTypeEnum.PRODUCT_STATUS_SERVICE, "98");
        //获取币种字典
        //Map<String, String> currencyDir = dataDictionaryBasicService.queryWithDetail(DataDictionaryTypeEnum.CURRENCY_SERVICE, "98");
        return collect.stream().map(item -> {
            String productStatus = MatchDataDictionaryKeyUtil.getKey(productStatusDictionary, item.getFundStatus());
            item.setFundStatus(productStatus);
            return item;
        }).collect(Collectors.toList());
    }

    /**
     * 开放周期
     * @param gjdfFundopendayDto
     * @param trustNo
     * @param admin
     */
    public static FundOpenInfoEntity getFundOpenInfoEntity(GJDFFundopendayDto gjdfFundopendayDto, String trustNo, String admin){
        return FundOpenInfoEntity.builder()
                .openType(gjdfFundopendayDto.getTempopenflag())
                .innerFundCode(gjdfFundopendayDto.getFundcode())
                .fundOpenDate(gjdfFundopendayDto.getBegindate())
                //需转成字典表
                .openStatus(gjdfFundopendayDto.getOpenstatus())
                .trustNo(trustNo)
                .admin(admin)
                .createTime(new Date())
                .updateTime(new Date())
                .build();
    }
    public  List<FundOpenInfoEntity> fundOpenInfoDtoToEntity(List<GJDFFundopendayDto> gjdfFundopendayDtoList,String admin) {
        List<FundOpenInfoEntity> collect = gjdfFundopendayDtoList.stream()
                .map((GJDFFundopendayDto gjdfFundopendayDto) -> getFundOpenInfoEntity(gjdfFundopendayDto, "98", admin))
                .collect(Collectors.toList());
        //开放日类型
        Map<String, String> typeOfOpenDayDic = dataDictionaryBasicService.queryWithDetail(DataDictionaryTypeEnum.TYPE_OF_OPEN_DAY_SERVICE, "98");
        //开放状态
        Map<String, Map<String, String>> openStateDic = openStateService.queryOpenStateWithDetail("98");

        return collect.stream().map(item -> {
            String openType = MatchDataDictionaryKeyUtil.getKey(typeOfOpenDayDic, item.getOpenType());
            String openStatus = MatchDataDictionaryKeyUtil.getKey(openType, openStateDic, item.getOpenStatus());
            item.setOpenType(openType);
            item.setOpenStatus(openStatus);
            return item;
        }).collect(Collectors.toList());
    }

    /**
     * 基金行情信息
     * @param gjdfFundvaluationsDto
     * @param admin
     */
    public static FundNetValueInfoEntity getFundvaluationsEntity(GJDFFundvaluationsDto gjdfFundvaluationsDto, String admin){
        return FundNetValueInfoEntity.builder()
                .innerFundCode(gjdfFundvaluationsDto.getFundcode())
                .netValueDate(gjdfFundvaluationsDto.getDate())
                .netValue(new BigDecimal(gjdfFundvaluationsDto.getNetvalue()))
                .totalNetValue(new BigDecimal(gjdfFundvaluationsDto.getTotalnetvalue()))
                .shares(new BigDecimal(gjdfFundvaluationsDto.getLastshares()))
                .assetValue(new BigDecimal(gjdfFundvaluationsDto.getLastasset()))
                .totalAsset(new BigDecimal(gjdfFundvaluationsDto.getLastasset()))
                .trustNo("98")
                .admin(admin)
                .createTime(new Date())
                .updateTime(new Date())
                .build();
    }
    public static List<FundNetValueInfoEntity> fundNetValueInfoDtoToEntity(List<GJDFFundvaluationsDto> gjdfFundvaluationsDtoList , String admin) {
        return gjdfFundvaluationsDtoList.stream()
                .map((GJDFFundvaluationsDto gjdfFundvaluationsDto) -> getFundvaluationsEntity(gjdfFundvaluationsDto,admin))
                .collect(Collectors.toList());
    }
    /**
     * 交易确认
     * @param gjdfConfirmDto
     * @param trustNo
     * @param admin
     */
    public static FundTradeInfoEntity getFundTradeInfoEntity(GJDFConfirmDto gjdfConfirmDto, String trustNo, String admin){
        return FundTradeInfoEntity.builder()
                .confirmDate(gjdfConfirmDto.getCdate())
                .requestNo(gjdfConfirmDto.getRequestno())
                //需转换成数据字典：销售商
                .agencyNo(gjdfConfirmDto.getAgencyno())
                //两位小小数
                .confirmShares(stringToBigDecimal(gjdfConfirmDto.getConfirmshares(),2))
                .confirmBalance(stringToBigDecimal(gjdfConfirmDto.getConfirmbalance(), 2))
                .cserialNo(gjdfConfirmDto.getCserialno())
                .tradeAcco(gjdfConfirmDto.getTradeacco())
                .netValue(stringToBigDecimal(gjdfConfirmDto.getNetvalue(),8))
                //另需要新增机构，数据字典：业务类型-业务类型
                .businessName(gjdfConfirmDto.getTxtbusinflag())
                .interest(stringToBigDecimal(gjdfConfirmDto.getInterest(), 2))
                .interestShare(stringToBigDecimal(gjdfConfirmDto.getInterestshare(), 2))
                .fundAcco(gjdfConfirmDto.getFundacco())
                .innerFundCode(gjdfConfirmDto.getFundcode())
                //格式YYYYMMDD
                .requestDate(gjdfConfirmDto.getRequestdate())
                .trustNo(trustNo)
                .admin(admin)
                .createTime(new Date())
                .updateTime(new Date())
                .build();
    }
    public  List<FundTradeInfoEntity> fundTradeInfoDtoToEntity(List<GJDFConfirmDto> gjdfConfirmDtoDtoList, String trustNo, String admin) {
        List<FundTradeInfoEntity> collect = gjdfConfirmDtoDtoList.stream()
                .map((GJDFConfirmDto GJDFConfirmDto) -> getFundTradeInfoEntity(GJDFConfirmDto, trustNo, admin))
                .collect(Collectors.toList());
        //获取销售商字典表
        Map<String, String> sellerServiceDir = dataDictionaryBasicService.queryWithDetail(DataDictionaryTypeEnum.SELLER_SERVICE, "国金道富");
        List<FundTradeInfoEntity> fundTradeInfoEntities = collect.stream().map(item -> {
            String agencyNo = item.getAgencyNo();
            agencyNo = MatchDataDictionaryKeyUtil.getKey(sellerServiceDir, agencyNo);
            if (null==agencyNo){
                //为空调用销售商接口更新销售商字典表
                guoJinDaoFuService.saveSellerInformation(admin);
                Map<String, String> gjdfSellerServiceDir = dataDictionaryBasicService.queryWithDetail(DataDictionaryTypeEnum.SELLER_SERVICE, "国金道富");
                agencyNo = MatchDataDictionaryKeyUtil.getKey(gjdfSellerServiceDir, agencyNo);
            }
            String agencyName = dataDictionarySellerService.queryValue("国金道富",agencyNo);
            item.setAgencyName(agencyName);
            item.setAgencyNo(agencyNo);
            return item;
        }).collect(Collectors.toList());
        return fundTradeInfoEntities;
    }
    /**
     * 客户账户信息数据
     * @param gjdfAccoDto
     * @param admin
     * @return
     */
    public static InvestorInfoEntity getAccoEntity(GJDFAccoDto gjdfAccoDto, String admin){
        InvestorInfoEntity investorInfoEntity = InvestorInfoEntity.builder()
                .custName(gjdfAccoDto.getCustname())
                .address(gjdfAccoDto.getAddress())
                .zipCode(gjdfAccoDto.getZipcode())
                .email(gjdfAccoDto.getEmail())
                .mobileNo(gjdfAccoDto.getMobileno())
                //需字典翻译为对应的key值。数据字典：客户类型证件类型-中登报送的客户类型
                .custType(gjdfAccoDto.getCusttype())
                //需字典翻译为对应的key值，并且字典有扩充。数据字典：客户类型证件类型-中登报送的个人/机构/产品的证件类型
                .identityType(gjdfAccoDto.getIdentitytype())
                .identityNo(gjdfAccoDto.getIdentityno())
                .fundAcco(gjdfAccoDto.getFundacco())
                .trustNo("98")
                .admin(admin)
                .createTime(new Date())
                .updateTime(new Date())
                .build();
        return investorInfoEntity;
    }
    public List<InvestorInfoEntity> accoDtoToEntity(List<GJDFAccoDto> gjdfAccoDtoList, String admin) {
        List<InvestorInfoEntity> collect = gjdfAccoDtoList.stream()
                .map((GJDFAccoDto gjdfAccoDto) -> getAccoEntity(gjdfAccoDto, admin))
                .collect(Collectors.toList());
        //获取客户类型字典表数据
        Map<String, String> custTypeCsdcDic = dataDictionaryBasicService.queryWithDetail(DataDictionaryTypeEnum.CUST_TYPE_CSDC_SERVICE, "98");
        //获取证件类型字典表数据
        Map<String, Map<String, String>> identityTypeCsdcDic = certificateCsdcService.queryCertificateWithDetail("98");
        List<InvestorInfoEntity> investorInfoEntities = collect.stream().map(item -> {
            String key = MatchDataDictionaryKeyUtil.getKey(custTypeCsdcDic, item.getCustType());
            String identityType = MatchDataDictionaryKeyUtil.getKey(key, identityTypeCsdcDic, item.getIdentityType());
            item.setCustType(key);
            item.setIdentityType(identityType);
            return item;
        }).collect(Collectors.toList());
        return investorInfoEntities;
    }
    /**
     * 客户账户信息数据-资金账户基本信息表
     * @param gjdfAccoDto
     * @param trustNo
     * @param admin
     * @return
     */
    public static AccountInfoEntity getAccountInfoEntityByAccoDto(GJDFAccoDto gjdfAccoDto, String trustNo, String admin){
        AccountInfoEntity build = AccountInfoEntity.builder()
                .nameInBank(gjdfAccoDto.getNameinbank())
                .bankCode(gjdfAccoDto.getBankacco())
                .trustNo(trustNo)
                .admin(admin)
                .build();
        return build;
    }
    public static List<AccountInfoEntity> accoDtoToAccountInfoEntity(List<GJDFAccoDto> gjdfAccoDtoList, String trustNo, String admin) {
        return gjdfAccoDtoList.stream()
                .map((GJDFAccoDto gjdfAccoDto) -> getAccountInfoEntityByAccoDto(gjdfAccoDto,trustNo,admin))
                .collect(Collectors.toList());
    }
    /**
     * 客户账户信息数据-基金出资人银行账户信息表
     * @param gjdfAccoDto
     * @param trustNo
     * @param admin
     * @return
     */
    public static InvestorBankInfoEntity getInvestorBankInfoEntityByAccoDto(GJDFAccoDto gjdfAccoDto, String trustNo, String admin){
        InvestorBankInfoEntity build = InvestorBankInfoEntity.builder()
                .nameInBank(gjdfAccoDto.getNameinbank())
                .bankNo(gjdfAccoDto.getBankno())
                .fundAcco(gjdfAccoDto.getFundacco())
                .trustNo(trustNo)
                .admin(admin)
                .build();
        return build;
    }
    public static List<InvestorBankInfoEntity> accoDtoToInvestorBankInfoEntity(List<GJDFAccoDto> gjdfAccoDtoList, String trustNo, String admin) {
        return gjdfAccoDtoList.stream()
                .map((GJDFAccoDto gjdfAccoDto) -> getInvestorBankInfoEntityByAccoDto(gjdfAccoDto,trustNo,admin))
                .collect(Collectors.toList());
    }
    /**
     * 基金静态份额
     * @param gjdfShareDto
     * @param admin
     * @return
     */
    public static CustShareInfoEntity getCustShareInfoEntityByShareDto(GJDFShareDto gjdfShareDto, String admin){
        CustShareInfoEntity build = CustShareInfoEntity.builder()
                .tradeAcco(gjdfShareDto.getTradeacco())
                .income(stringToBigDecimal(gjdfShareDto.getIncome(), 2))
                .fundAcco(gjdfShareDto.getFundacco())
                .innerFundCode(gjdfShareDto.getFundcode())
                .agencyNo(gjdfShareDto.getAgencyno())
                .frozenShares(stringToBigDecimal(gjdfShareDto.getFrozenshares(), 2))
                .trustNo("98")
                .admin(admin)
                .createTime(new Date())
                .updateTime(new Date())
                .build();
        return build;
    }
    @Autowired
    GuoJinDaoFuServiceImpl guoJinDaoFuService;
    @Autowired
    DataDictionarySellerServiceImpl dataDictionarySellerService;
    public  List<CustShareInfoEntity> shareDtoToCustShareInfoEntity(List<GJDFShareDto> gjdfShareDtoList, String admin) {
        List<CustShareInfoEntity> collect = gjdfShareDtoList.stream()
                .map((GJDFShareDto gjdfShareDto) -> getCustShareInfoEntityByShareDto(gjdfShareDto, admin))
                .collect(Collectors.toList());
        //获取销售商字典表
        Map<String, String> sellerServiceDir = dataDictionaryBasicService.queryWithDetail(DataDictionaryTypeEnum.SELLER_SERVICE, "国金道富");
        List<CustShareInfoEntity> custShareInfoEntities = collect.stream().map(item -> {
            String agencyNo = item.getAgencyNo();
            agencyNo = MatchDataDictionaryKeyUtil.getKey(sellerServiceDir, agencyNo);
            if (null==agencyNo){
                //为空调用销售商接口更新销售商字典表
                guoJinDaoFuService.saveSellerInformation(admin);
                Map<String, String> gjdfSellerServiceDir = dataDictionaryBasicService.queryWithDetail(DataDictionaryTypeEnum.SELLER_SERVICE, "国金道富");
                agencyNo = MatchDataDictionaryKeyUtil.getKey(gjdfSellerServiceDir, agencyNo);
            }
            String agencyName = dataDictionarySellerService.queryValue("国金道富",agencyNo);
            item.setAgencyName(agencyName);
            item.setAgencyNo(agencyNo);
            return item;
        }).collect(Collectors.toList());
        return custShareInfoEntities;
    }
    /**
     * 基金份额明细
     * @param gjdfSharedetailDto
     * @param admin
     * @return
     */

    public static CustShareDetailInfoEntity getCustShareDetailInfoEntityBySharedetailDto(GJDFSharedetailDto gjdfSharedetailDto, String admin){
        //从custody_cust_share_seq_info获取流水号？
        CustShareDetailInfoEntity build = CustShareDetailInfoEntity.builder()
                .remainShares(new BigDecimal(gjdfSharedetailDto.getRemainshares()))
                .oriNetValue(stringToBigDecimal(gjdfSharedetailDto.getOrinetvalue(), 8))
                .tradeAcco(gjdfSharedetailDto.getTradeacco())
                .registDate(gjdfSharedetailDto.getRegistdate())
                .oriBalance(stringToBigDecimal(gjdfSharedetailDto.getOribalance(), 2))
                .oriShares(stringToBigDecimal(gjdfSharedetailDto.getOrishares(), 2))
                .fundAcco(gjdfSharedetailDto.getFundacco())
                .innerFundCode(gjdfSharedetailDto.getFundcode())
                .agencyNo(gjdfSharedetailDto.getAgencyno())
                .confirmDate(gjdfSharedetailDto.getCdate())
                .cserialNo(gjdfSharedetailDto.getCserialno())
                //为空时填A
                .sourceType(gjdfSharedetailDto.getSourcetype()==null?"A":gjdfSharedetailDto.getSourcetype())
                .seqNo(gjdfSharedetailDto.getSerialno())
                .trustNo("98")
                .admin((admin))
                .createTime(new Date())
                .updateTime(new Date())
                .build();
        return build;
    }
    @Autowired
    CustShareSeqInfoServiceImpl custShareSeqInfoService;
    public  List<CustShareDetailInfoEntity> sharedetailDtoToCustShareDetailInfoEntity(List<GJDFSharedetailDto> gjdfSharedetailDtoList, String admin) {
        List<CustShareDetailInfoEntity> collect = gjdfSharedetailDtoList.stream()
                .map((GJDFSharedetailDto gjdfSharedetailDto) -> getCustShareDetailInfoEntityBySharedetailDto(gjdfSharedetailDto, admin))
                .collect(Collectors.toList());
        //份额来源
        Map<String, String> shareSourceDic = dataDictionaryBasicService.queryWithDetail(DataDictionaryTypeEnum.SHARE_SOURCE_SERVICE, "98");
        //获取销售商字典表
        Map<String, String> sellerServiceDir = dataDictionaryBasicService.queryWithDetail(DataDictionaryTypeEnum.SELLER_SERVICE, "国金道富");
        List<CustShareDetailInfoEntity> entities = collect.stream().map(item -> {
            //通过cserial_no，admin，trust_no，inner_fund_code从custody_cust_share_seq_info
            String seqNo = custShareSeqInfoService.querySeqNo("98", item.getAdmin(), item.getInnerFundCode(), item.getCserialNo());
            item.setSeqNo(seqNo);
            String key = MatchDataDictionaryKeyUtil.getKey(shareSourceDic, item.getSourceType());
            item.setSourceType(key);
            String agencyNo = item.getAgencyNo();
             agencyNo = MatchDataDictionaryKeyUtil.getKey(sellerServiceDir, agencyNo);
            if (null==agencyNo){
                //为空调用销售商接口更新销售商字典表
                guoJinDaoFuService.saveSellerInformation(admin);
                Map<String, String> gjdfSellerServiceDir = dataDictionaryBasicService.queryWithDetail(DataDictionaryTypeEnum.SELLER_SERVICE, "国金道富");
                agencyNo = MatchDataDictionaryKeyUtil.getKey(gjdfSellerServiceDir, agencyNo);
            }
            item.setAgencyNo(agencyNo);
            String agencyName = dataDictionarySellerService.queryValue("国金道富",agencyNo);
            item.setAgencyName(agencyName);
             return item;
        }).collect(Collectors.toList());
        return entities;
    }

    public  List<FundProfitInfoEntity> sharedetailDtoToFundProfitInfoEntity(List<GJDFSharedetailDto> gjdfSharedetailDtoList, String admin) {
        List<FundProfitInfoEntity> collect = gjdfSharedetailDtoList.stream()
                .map((GJDFSharedetailDto gjdfSharedetailDto) -> getFundProfitInfoEntityBySharedetailDto(gjdfSharedetailDto, admin))
                .collect(Collectors.toList());
        //获取收费方式字典表
        Map<String, String> pay = dataDictionaryBasicService.queryWithDetail(DataDictionaryTypeEnum.PAYMENT_WAY_SERVICE, "98");
        List<FundProfitInfoEntity> fundProfitInfoEntities = collect.stream().map(item -> {
            String shareType = item.getShareType();
            String key = MatchDataDictionaryKeyUtil.getKey(pay, shareType);
            item.setShareType(key);
            return item;
        }).collect(Collectors.toList());
        return fundProfitInfoEntities;
    }
    public static FundProfitInfoEntity getFundProfitInfoEntityBySharedetailDto(GJDFSharedetailDto gjdfSharedetailDto, String admin){
        FundProfitInfoEntity build = FundProfitInfoEntity.builder()
                .admin(admin)
                .shareType(gjdfSharedetailDto.getSharetype())
               // .cserialNo(gjdfSharedetailDto.getCserialno())
                .innerFundCode(gjdfSharedetailDto.getFundcode())
                .trustNo("98")
                .build();
        return build;
    }
    /**
     * 业绩提成流水
     * @param gjdfProfitcurrentDto
     * @param admin
     * @return
     */

    public static FundProfitInfoEntity getFundProfitInfoEntityBygjdfProfitcurrentDto(GJDFProfitcurrentDto gjdfProfitcurrentDto, String admin){
        FundProfitInfoEntity build = FundProfitInfoEntity.builder()
                .confirmDate(gjdfProfitcurrentDto.getCdate())
                .registDate(gjdfProfitcurrentDto.getRegistdate())
                .nav(stringToBigDecimal(gjdfProfitcurrentDto.getNetvalue(), 8))
                .totalNav(stringToBigDecimal(gjdfProfitcurrentDto.getTotalnetvalue(), 8))
                .currRatio(stringToBigDecimal(gjdfProfitcurrentDto.getCurratio(), 8))
                .yearRatio(stringToBigDecimal(gjdfProfitcurrentDto.getYearratio(), 8))
                .returnFare(stringToBigDecimal(gjdfProfitcurrentDto.getReturnfare(), 2))
                .oriBalance(stringToBigDecimal(gjdfProfitcurrentDto.getDeductbalance(), 2))
                .factBalance(stringToBigDecimal(gjdfProfitcurrentDto.getFactbalance(), 2))
                .factShares(stringToBigDecimal(gjdfProfitcurrentDto.getFactshares(), 2))
                .bonusBalance(stringToBigDecimal(gjdfProfitcurrentDto.getBonusbalance(), 2))
                .sortFlag(gjdfProfitcurrentDto.getSortflag())
                .oriDate(gjdfProfitcurrentDto.getBegindate())
                .cserialNo(gjdfProfitcurrentDto.getCserialno())
                .holdDay(gjdfProfitcurrentDto.getHold())
                .oriNav(stringToBigDecimal(gjdfProfitcurrentDto.getOrinav(), 8))
                .oriTotalNav(stringToBigDecimal(gjdfProfitcurrentDto.getOritotalnav(), 8))
                .fundAcco(gjdfProfitcurrentDto.getFundacco())
                .innerFundCode(gjdfProfitcurrentDto.getFundcode())
                .agencyNo(gjdfProfitcurrentDto.getAgencyno())
                .tradeAcco(gjdfProfitcurrentDto.getTradeacco())
                .requestDate(gjdfProfitcurrentDto.getDate())
                .trustNo("98")
                .admin(admin)
                .createTime(new Date())
                .updateTime(new Date())
                .build();
        return build;
    }
    public  List<FundProfitInfoEntity> profitcurrentDtoToFundProfitInfoEntity(List<GJDFProfitcurrentDto> gjdfProfitcurrentDtoList, String admin) {
        List<FundProfitInfoEntity> collect = gjdfProfitcurrentDtoList.stream()
                .map((GJDFProfitcurrentDto gjdfProfitcurrentDto) -> getFundProfitInfoEntityBygjdfProfitcurrentDto(gjdfProfitcurrentDto, admin))
                .collect(Collectors.toList());
        Map<String, String> handleDir = dataDictionaryBasicService.queryWithDetail(DataDictionaryTypeEnum.HANDLE_TYPE_SERVICE, "98");
        //获取销售商字典表
        Map<String, String> sellerServiceDir = dataDictionaryBasicService.queryWithDetail(DataDictionaryTypeEnum.SELLER_SERVICE, "国金道富");
        return collect.stream().map(item -> {
            String sortFlag = MatchDataDictionaryKeyUtil.getKey(handleDir, item.getSortFlag());
            item.setSortFlag(sortFlag);
            String agencyNo = item.getAgencyNo();
            agencyNo = MatchDataDictionaryKeyUtil.getKey(sellerServiceDir, agencyNo);
            if (null==agencyNo){
                //为空调用销售商接口更新销售商字典表
                guoJinDaoFuService.saveSellerInformation(admin);
                Map<String, String> gjdfSellerServiceDir = dataDictionaryBasicService.queryWithDetail(DataDictionaryTypeEnum.SELLER_SERVICE, "国金道富");
                agencyNo = MatchDataDictionaryKeyUtil.getKey(gjdfSellerServiceDir, agencyNo);
            }
            item.setAgencyNo(agencyNo);
            String agencyName = dataDictionarySellerService.queryValue("国金道富",agencyNo);
            item.setAgencyName(agencyName);
            return item;
        }).collect(Collectors.toList());
    }
    /**
     * 份额变化流水
     * @param dto
     * @param admin
     * @return
     */

    public static CustShareSeqInfoEntity getShareSeqInfoEntityBysharecurrentsDto(GJDFSharecurrentsDto dto, String admin){
        return CustShareSeqInfoEntity.builder()
                .confirmDate(dto.getCdate())
                .occurShares(stringToBigDecimal(dto.getOccurshares(),2))
                .occurBalance(stringToBigDecimal(dto.getOccurbalance(),2))
                .lastHoldShares(stringToBigDecimal(dto.getLastshares(),2))
                .cserialNo(dto.getCserialno())
                .requestDate(dto.getRequestdate())
                .requestNo(dto.getRequestno())
                .fundAcco(dto.getFundacco())
                .tradeAcco(dto.getTradeacco())
                .innerFundCode(dto.getFundcode())
                .agencyNo(dto.getAgencyno())
                .seqNo(dto.getSerialno())
                .trustNo("98")
                .admin(admin)
                .createTime(new Date())
                .updateTime(new Date())
                .build();
    }
    public  List<CustShareSeqInfoEntity> sharecurrentsDtoToShareSeqInfoEntity(List<GJDFSharecurrentsDto> gjdfSharecurrentsDtoList, String admin) {
        List<CustShareSeqInfoEntity> collect = gjdfSharecurrentsDtoList.stream()
                .map((GJDFSharecurrentsDto gjdfSharecurrentsDto) -> getShareSeqInfoEntityBysharecurrentsDto(gjdfSharecurrentsDto, admin))
                .collect(Collectors.toList());
        //获取销售商字典表
        Map<String, String> sellerServiceDir = dataDictionaryBasicService.queryWithDetail(DataDictionaryTypeEnum.SELLER_SERVICE, "国金道富");
        List<CustShareSeqInfoEntity> custShareSeqInfoEntities = collect.stream().map(item -> {
            String agencyNo = item.getAgencyNo();
            agencyNo = MatchDataDictionaryKeyUtil.getKey(sellerServiceDir, agencyNo);
            if (null==agencyNo){
                //为空调用销售商接口更新销售商字典表
                guoJinDaoFuService.saveSellerInformation(admin);
                //再次调用
                Map<String, String> stringStringMap = dataDictionaryBasicService.queryWithDetail(DataDictionaryTypeEnum.SELLER_SERVICE, "国金道富");
                agencyNo = MatchDataDictionaryKeyUtil.getKey(stringStringMap, agencyNo);
            }
            item.setAgencyNo(agencyNo);
            String agencyName = dataDictionarySellerService.queryValue("国金道富",agencyNo);
            item.setAgencyName(agencyName);
            return item;
        }).collect(Collectors.toList());
        return custShareSeqInfoEntities;
    }

    /**
     * 将String转BigDecimal并保留l位小数
     * @param converted
     * @param l
     */
    public static BigDecimal stringToBigDecimal(String converted, int l){
        boolean empty = ObjectUtils.isEmpty(converted);
        if (empty){
            return null;
        }else {
            String f="0.";
            for (int i = 0; i < l; i++) {
                f=f+"0";
            }
            DecimalFormat decimalFormat = new DecimalFormat(f);
            //不足l位补0
            BigDecimal bigDecimal = new BigDecimal(converted);
            //格式化对象
            String format = decimalFormat.format(bigDecimal);
            return new BigDecimal(format).setScale(l, RoundingMode.HALF_DOWN);
        }
    }

    /**
     * 基金账号登记信息转entity
     * @param gjdfBankInfoDtoList
     * @param admin
     * @return
     */
    public List<InvestorBankInfoEntity> bankInfoDtoToEntity(List<GJDFBankInfoDto> gjdfBankInfoDtoList, String admin) {
        //1、获取字典表数据
        Map<String, String> bankCodeDir = dataDictionaryBasicService.queryWithDetail(DataDictionaryTypeEnum.BANK_CODE_SERVICE, "国金道富TA");
        //2、封装数据
        List<InvestorBankInfoEntity> result = new ArrayList<>();
        Date now = new Date();
        for (GJDFBankInfoDto bankInfoDto : gjdfBankInfoDtoList) {
            String bankCode = MatchDataDictionaryKeyUtil.getKey(bankCodeDir, bankInfoDto.getBankno());
            log.info("getInvestorBankInfoData_bankCode原值为{},映射值为{}", bankInfoDto.getBankno(), bankCode);
            result.add(InvestorBankInfoEntity.builder()
                    .fundAcco(bankInfoDto.getFundacco())
                    .tradeAcco(bankInfoDto.getTradeacco())
                    .bankCode(bankCode)
                    .bankNo(bankInfoDto.getBankno())
                    .branchBankName(bankInfoDto.getBankname())
                    .nameInBank(bankInfoDto.getNameinbank())
                    .bankNoType("银行账号")
                    .trustNo("98")
                    .admin(admin)
                    .createTime(now)
                    .updateTime(now)
                    .build());
        }
        return result;
    }
}
