package com.brilliance.isc.common.trnmod.service.impl;

import com.brilliance.mda.runtime.mda.util.Dates;
import com.brilliance.isc.bo.*;
import com.brilliance.isc.bo.model.*;
import com.brilliance.isc.common.cache.adr.AdrCacheService;
import com.brilliance.isc.common.cache.pta.PtaCacheService;
import com.brilliance.isc.common.cbsmod.service.CbsmodService;
import com.brilliance.isc.common.cbsmod.service.XrtmodService;
import com.brilliance.isc.common.component.PtsptaToolComponent;
import com.brilliance.isc.common.exception.CommonServiceException;
import com.brilliance.isc.common.liaall.service.LiaallService;
import com.brilliance.isc.common.liaall.vo.LiaallVo;
import com.brilliance.isc.common.liaget.service.LiagetService;
import com.brilliance.isc.common.transaction.help.TransactionServiceSet;
import com.brilliance.isc.common.util.StringUtils;
import com.brilliance.isc.common.util.Utils;
import com.brilliance.isc.common.vo.PtsptaVo;
import com.brilliance.isc.common.vo.SimpleModuleVo;
import com.brilliance.isc.mda.dao.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Component
public class LcToolComponents {

    @Autowired
    private PtaCacheService ptaCacheService;

    @Autowired
    private AdrCacheService adrCacheService;
    private static final String DATA_TYPE = "LID";

    private static final String BRD_DATA_TYPE = "BRD";

    private static final String BED_DATA_TYPE = "BED";

    private static final String LED_DATA_TYPE = "LED";

    private static final String LTD_DATA_TYPE = "LTD";

    private static final String BTD_DATA_TYPE = "BTD";

    private static final String IS_ERROR = "N";

    @Autowired
    private LidMapper lidMapper;

    @Autowired
    private LedMapper ledMapper;

    @Autowired
    private LitMapper litMapper;

    @Autowired
    private LetMapper letMapper;

    @Autowired
    private LtdMapper ltdMapper;

    @Autowired
    private LttMapper lttMapper;

    @Autowired
    private BrdMapper brdMapper;

    @Autowired
    private BrtMapper brtMapper;

    @Autowired
    private BedMapper bedMapper;

    @Autowired
    private BetMapper betMapper;

    @Autowired
    private BpdMapper bpdMapper;

    @Autowired
    private TrdMapper trdMapper;

    @Autowired
    private UclMapper uclMapper;

    @Autowired
    private TrtMapper trtMapper;

    @Autowired
    private BchMapper bchMapper;

    @Autowired
    private BtdMapper btdMapper;

    @Autowired
    private BttMapper bttMapper;
    @Autowired
    private SrmMapper srmMapper;


    private static Logger log = LoggerFactory.getLogger(LcToolComponents.class);

    @Autowired
    private CbsmodService cbsmodService;

    @Autowired
    private PtsptaToolComponent ptsptaToolComponent;

    @Autowired
    private LiagetService liagetService;

    @Autowired
    private LiaallService liaallService;

    @Autowired
    private XrtmodService xrtmodService;

    /**
     * 根据前端传过来的pts信息，补充完整PtsptaVo
     *
     * @param set 具体某个交易
     * @param xxxgrp 传过来的xxxgrp或者其它含有PtsptaVo的对象
     * @param ptsNames 如果不清楚具体传的pts信息，就直接填null，将全部搜集
     */
    public void collectPtsList(TransactionServiceSet set, Object xxxgrp, String... ptsNames) {
        if (ptsNames == null) {
            //找所有的ptspta对象
            Field[] fields = xxxgrp.getClass().getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                if (PtsptaVo.class.equals(field.getType())) {
                    PtsptaVo ptspta = Utils.getFieldValue(xxxgrp, field.getName());
                    if (ptspta != null) {
                        set.addPtspta(transferFrom(ptspta));
                    }
                }
            }
        } else {
            for (String name : ptsNames) {
                PtsptaVo ptspta = Utils.getFieldValue(xxxgrp, name);
                if (ptspta != null && ptspta.getPts() != null && StringUtils.isNotEmpty(ptspta.getPts().getRol())) {
                    set.addPtspta(transferFrom(ptspta));
                }
            }
        }
    }



    public void collectCbsMap(TransactionServiceSet set, String prefix, Object cbs, String... cbsNames) {
        Map<String, Cbb> cbsMap = set.getCbsMap();
        if (cbsMap == null) {
            cbsMap = new HashMap<>();
        }
        for (String name : cbsNames) {
            Cbb cbb = Utils.getFieldValue(cbs, name);
            if (cbb != null) {
                cbsMap.put(prefix.toUpperCase() + name.toUpperCase(), cbb);
            }
        }
        set.setCbsMap(cbsMap);
    }

    /**
     * 根据STL结构的ptspta转化成抽象组件的ptspta，结构不一样
     *
     * @param ptsptas
     * @return List<com.brilliance.common.vo.PtsptaVo>
     */
    public PtsptaVo transferFrom(PtsptaVo ptsptas) {
        PtsptaVo ptspta = ptsptas;
        ptspta.setRol(ptsptas.getPts().getRol());
        ptspta.setPtsinr(ptsptas.getPts().getInr());
        ptspta.setNam(ptsptas.getPts().getNam());
        ptspta.setPtyinr(ptsptas.getPts().getPtyinr());
        ptspta.setPtainr(ptsptas.getPts().getPtainr());
        ptspta.setExtkey(ptsptas.getPts().getExtkey());
        ptspta.setDftdsp(ptsptas.getPts().getDftdsp());
        ptspta.setDftcur(ptsptas.getPts().getDftcur());
        ptspta.setDftact(ptsptas.getPts().getDftact());
        ptspta.setDftfeecur(ptsptas.getPts().getDftfeecur());
        ptspta.setDftactptainr(ptsptas.getPts().getDftactptainr());
        ptspta.setDftselrol(ptsptas.getPts().getDftsetrol());
        ptspta.setGlggrpflg(ptsptas.getPts().getGlggrpflg());
        ptspta.setRef(ptsptas.getPts().getRef());
        ptspta.setPtytyp(ptsptas.getPtytyp());
        ptspta.setPtyextkey(ptsptas.getPtyextkey());
        ptspta.setAdrblk(ptsptas.getPts().getAdrblk());

        // 填充pta信息，如果pta为null或者pta为空,但从前端传过来的pta不为空，则从缓存或者从数据库中读出pta的信息，填充
        if ((null==ptspta.getPta() || StringUtils.isEmpty(ptspta.getPta().getInr()) )
                && StringUtils.isNotEmpty(ptspta.getPtainr())) {
            ptspta.setPta(ptaCacheService.getPtaByInr(ptspta.getPtainr()));
        }

        // 填充adr信息，如果adr为null或者adr为空,但从前端传过来的pta不为空，则从缓存或者从数据库中读出adr的信息，填充
        if ((null == ptspta.getAdr()|| StringUtils.isEmpty(ptspta.getAdr().getInr()))
                && StringUtils.isNotEmpty(ptspta.getPtainr())) {
            ptspta.setAdr(adrCacheService.selectByPtaInr(ptspta.getPtainr()));
        }
        return ptspta;
    }

    public Brdgrp getBrpgrpByBrdInr(String inr) {
        Brdgrp brdgrpVo = new Brdgrp();
        brdgrpVo.reset();
        //找BRD的数据
        Brd brd = brdgrpVo.getRec();
        brd.setInr(inr);
        brd = brdMapper.selectByPrimaryKey(brd);
        if (brd == null) {
            throw new CommonServiceException("找不到Brd{" + inr + "}数据!");
        }
        brdgrpVo.setRec(brd);
        //找BRT的数据
        Brt brt = brdgrpVo.getBlk();
        brt.setInr(inr);
        brt = brtMapper.selectByPrimaryKey(brt);
        if (brt == null) {
            throw new CommonServiceException("找不到Brt{" + inr + "}数据!");
        }
        brdgrpVo.setBlk(brt);
        //找liacbs数据
        Date date = Dates.add(Utils.getMaxDate(), -1);
        cbsmodService.cbsGetBalance(brdgrpVo.getCbs().getMax(), BRD_DATA_TYPE, inr, "MAXSUM", "AMT1", date);
        cbsmodService.cbsGetBalance(brdgrpVo.getCbs().getOpn1(), BRD_DATA_TYPE, inr, "OPN", "AMT1", date);
//        cbsmodService.cbsGetBalance(brdgrpVo.getCbs().getOpnbpdisc(), BRD_DATA_TYPE, inr, "OPN", "BPDISC", date);
//        cbsmodService.cbsGetBalance(brdgrpVo.getCbs().getRed(), BRD_DATA_TYPE, inr, "RED", "AMT1", date);
//        cbsmodService.cbsGetBalance(brdgrpVo.getCbs().getMax2(), BRD_DATA_TYPE, inr, "MAXSUM", "CCVAMT", date);
//        cbsmodService.cbsGetBalance(brdgrpVo.getCbs().getOpn2(), BRD_DATA_TYPE, inr, "OPN", "AMT2", date);
        cbsmodService.cbsGetBalance(brdgrpVo.getCbs().getMax2(), BRD_DATA_TYPE, inr, "MAXSUM", "AMT2", date);
        cbsmodService.cbsGetBalance(brdgrpVo.getCbs().getOpn2(), BRD_DATA_TYPE, inr, "OPN", "AMT2", date);
        //找ptspta数据
        Map<String, PtsptaVo> maps = ptsptaToolComponent.getPtsptaList(inr, BRD_DATA_TYPE);
        if (maps.size() > 0) {
            maps.keySet().forEach(key -> {
                Utils.setFieldValue(brdgrpVo, key.toLowerCase(), maps.get(key));
            });
        }
        return brdgrpVo;
    }


    public Lidgrp getLidgrpByInr(String inr) {
        Lidgrp lidgrpVo = new Lidgrp();
        lidgrpVo.reset();
        //找LID的数据
        Lid lid = lidgrpVo.getRec();
        lid.setInr(inr);
        lid = lidMapper.selectByPrimaryKey(lid);
        if (lid != null) {
            lidgrpVo.setRec(lid);
        }
        //找LIT的数据
        Lit lit = lidgrpVo.getBlk();
        lit.setInr(inr);
        lit = litMapper.selectByPrimaryKey(lit);
        if (lit != null) {
            lidgrpVo.setBlk(lit);
        }
        //找liacbs数据
        Date date = Dates.add(Utils.getMaxDate(), -1);
        cbsmodService.cbsGetBalance(lidgrpVo.getCbs().getMax(), DATA_TYPE, inr, "MAXSUM", "AMT1", date);
        cbsmodService.cbsGetBalance(lidgrpVo.getCbs().getNom1(), DATA_TYPE, inr, "NOMSUM", "AMT1", date);
        cbsmodService.cbsGetBalance(lidgrpVo.getCbs().getOpn1(), DATA_TYPE, inr, "OPN", "AMT1", date);
        cbsmodService.cbsGetBalance(lidgrpVo.getCbs().getMax2(), DATA_TYPE, inr, "MAXSUM", "AMT2", date);
        cbsmodService.cbsGetBalance(lidgrpVo.getCbs().getOpn2(), DATA_TYPE, inr, "OPN", "AMT2", date);
        //找ptspta数据
        Map<String, PtsptaVo> maps = ptsptaToolComponent.getPtsptaList(inr, DATA_TYPE);
        if (maps.size() > 0) {
            maps.keySet().forEach(key -> {
                Utils.setFieldValue(lidgrpVo, key.toLowerCase(), maps.get(key));
            });
        }
        return lidgrpVo;
    }


    public Bedgrp getBedgrpByInr(String inr) {
        Bedgrp bedgrpVo = new Bedgrp();
        bedgrpVo.reset();
        //找BRD的数据
        Bed bed = bedgrpVo.getRec();
        bed.setInr(inr);
        bed = bedMapper.selectByPrimaryKey(bed);
        if (bed == null) {
            throw new CommonServiceException("找不到Bed{" + inr + "}数据!");
        }
        bedgrpVo.setRec(bed);
        //找BRT的数据
        Bet bet = bedgrpVo.getBlk();
        bet.setInr(inr);
        bet = betMapper.selectByPrimaryKey(bet);
        if (bet == null) {
            throw new CommonServiceException("找不到Bet{" + inr + "}数据!");
        }
        bedgrpVo.setBlk(bet);
        //找liacbs数据
        Date date = Dates.add(Utils.getMaxDate(), -1);
        cbsmodService.cbsGetBalance(bedgrpVo.getCbs().getMax(), BED_DATA_TYPE, inr, "MAXSUM", "AMT1", date);
        cbsmodService.cbsGetBalance(bedgrpVo.getCbs().getOpn1(), BED_DATA_TYPE, inr, "OPN", "AMT1", date);
        cbsmodService.cbsGetBalance(bedgrpVo.getCbs().getOpnbpdisc(), BED_DATA_TYPE, inr, "OPN", "BPDISC", date);
        cbsmodService.cbsGetBalance(bedgrpVo.getCbs().getRed(), BED_DATA_TYPE, inr, "RED", "AMT1", date);
        cbsmodService.cbsGetBalance(bedgrpVo.getCbs().getMax2(), BED_DATA_TYPE, inr, "MAXSUM", "AMT2", date);
        cbsmodService.cbsGetBalance(bedgrpVo.getCbs().getOpn2(), BED_DATA_TYPE, inr, "OPN", "AMT2", date);
        //找ptspta数据
        Map<String, PtsptaVo> maps = ptsptaToolComponent.getPtsptaList(inr, BED_DATA_TYPE);
        if (maps.size() > 0) {
            maps.keySet().forEach(key -> {
                Utils.setFieldValue(bedgrpVo, key.toLowerCase(), maps.get(key));
            });
        }
        return bedgrpVo;
    }

    public Brdgrp getBrdgrpByInr(String inr) {
        Brdgrp brdgrpVo = new Brdgrp();
        brdgrpVo.reset();
        //找BRD的数据
        Brd brd = brdgrpVo.getRec();
        brd.setInr(inr);
        brd = brdMapper.selectByPrimaryKey(brd);
        if (brd != null) {
            brdgrpVo.setRec(brd);
        }
        //找BRT的数据
        Brt brt = brdgrpVo.getBlk();
        brt.setInr(inr);
        brt = brtMapper.selectByPrimaryKey(brt);
        if (brt != null) {
            brdgrpVo.setBlk(brt);
        }
        //找SRM的数据
        Srm srm = brdgrpVo.getSrm();
        srm = srmMapper.selectByObjinrAndObjtyp(inr,"BRD");
        if (srm != null) {
            brdgrpVo.setSrm(srm);
        }
        //找liacbs数据
        Date date = Dates.add(Utils.getMaxDate(), -1);
        cbsmodService.cbsGetBalance(brdgrpVo.getCbs().getMax(), BRD_DATA_TYPE, inr, "MAXSUM", "AMT1", date);
        cbsmodService.cbsGetBalance(brdgrpVo.getCbs().getOpn1(), BRD_DATA_TYPE, inr, "OPN", "AMT1", date);
        cbsmodService.cbsGetBalance(brdgrpVo.getCbs().getMax2(), BRD_DATA_TYPE, inr, "MAXSUM", "AMT2", date);
        cbsmodService.cbsGetBalance(brdgrpVo.getCbs().getOpn2(), BRD_DATA_TYPE, inr, "OPN", "AMT2", date);
        //找ptspta数据
        Map<String, PtsptaVo> maps = ptsptaToolComponent.getPtsptaList(inr, BRD_DATA_TYPE);
        if (maps.size() > 0) {
            maps.keySet().forEach(key -> {
                Utils.setFieldValue(brdgrpVo, key.toLowerCase(), maps.get(key));
            });
        }
//        brdgrpVo.getRec().setNam( brdgrpVo.getCbs().getMax().getCur() + " " + Formats.fmtAmount(brdgrpVo.getCbs().getMax().getAmt(),brdgrpVo.getCbs().getMax().getCur()) + " " + brdgrpVo.getApl().getPts().getNam());
        return brdgrpVo;
    }

    public Ledgrp getLedgrpByInr(String inr) {
        Ledgrp ledgrpVo = new Ledgrp();
        ledgrpVo.reset();
        //找LED的数据
        Led led = ledgrpVo.getRec();
        led.setInr(inr);
        led = ledMapper.selectByPrimaryKey(led);
        if (led == null) {
            throw new CommonServiceException("找不到Led{" + inr + "}数据!");
        }
        ledgrpVo.setRec(led);
        //找LET的数据
        Let let = ledgrpVo.getBlk();
        let.setInr(inr);
        let = letMapper.selectByPrimaryKey(let);
        if (let == null) {
            throw new CommonServiceException("找不到Let{" + inr + "}数据!");
        }
        ledgrpVo.setBlk(let);
        //找liacbs数据
        Date date = Dates.add(Utils.getMaxDate(), -1);
        cbsmodService.cbsGetBalance(ledgrpVo.getCbs().getMax(), LED_DATA_TYPE, inr, "MAXSUM", "AMT1", date);
        cbsmodService.cbsGetBalance(ledgrpVo.getCbs().getOpn1(), LED_DATA_TYPE, inr, "OPN", "AMT1", date);
        cbsmodService.cbsGetBalance(ledgrpVo.getCbs().getNom1(), LED_DATA_TYPE, inr, "NOMSUM", "AMT1", date);
        cbsmodService.cbsGetBalance(ledgrpVo.getCbs().getCnf(), LED_DATA_TYPE, inr, "CNFSUM", "AMT1", date);
        cbsmodService.cbsGetBalance(ledgrpVo.getCbs().getMax2(), LED_DATA_TYPE, inr, "MAXSUM", "AMT2", date);
        cbsmodService.cbsGetBalance(ledgrpVo.getCbs().getOpn2(), LED_DATA_TYPE, inr, "OPN", "AMT2", date);
        //找ptspta数据
        Map<String, PtsptaVo> maps = ptsptaToolComponent.getPtsptaList(inr, LED_DATA_TYPE);
        if (maps.size() > 0) {
            maps.keySet().forEach(key -> {
                Utils.setFieldValue(ledgrpVo, key.toLowerCase(), maps.get(key));
            });
        }
//        brdgrpVo.getRec().setNam( brdgrpVo.getCbs().getMax().getCur() + " " + Formats.fmtAmount(brdgrpVo.getCbs().getMax().getAmt(),brdgrpVo.getCbs().getMax().getCur()) + " " + brdgrpVo.getApl().getPts().getNam());
        return ledgrpVo;
    }

    public Ltdgrp getLtdgrpByInr(String inr) {
        Ltdgrp ltdgrpVo = new Ltdgrp();
        ltdgrpVo.reset();
        //找LTD的数据
        Ltd ltd = ltdgrpVo.getRec();
        ltd.setInr(inr);
        ltd = ltdMapper.selectByPrimaryKey(ltd);
        if (ltd == null) {
            throw new CommonServiceException("找不到Ltd{" + inr + "}数据!");
        }
        ltdgrpVo.setRec(ltd);
        //找LTT的数据
        Ltt ltt = ltdgrpVo.getBlk();
        ltt.setInr(inr);
        ltt = lttMapper.selectByPrimaryKey(ltt);
        if (ltt == null) {
            throw new CommonServiceException("找不到Ltt{" + inr + "}数据!");
        }
        ltdgrpVo.setBlk(ltt);
        //找liacbs数据
        Date date = Dates.add(Utils.getMaxDate(), -1);
        cbsmodService.cbsGetBalance(ltdgrpVo.getCbs().getMax(), LTD_DATA_TYPE, inr, "MAXSUM", "AMT1", date);
        cbsmodService.cbsGetBalance(ltdgrpVo.getCbs().getOpn1(), LTD_DATA_TYPE, inr, "OPN", "AMT1", date);
        cbsmodService.cbsGetBalance(ltdgrpVo.getCbs().getNom1(), LTD_DATA_TYPE, inr, "NOMSUM", "AMT1", date);
        cbsmodService.cbsGetBalance(ltdgrpVo.getCbs().getMax2(), LTD_DATA_TYPE, inr, "MAXSUM", "AMT2", date);
        cbsmodService.cbsGetBalance(ltdgrpVo.getCbs().getOpn2(), LTD_DATA_TYPE, inr, "OPN", "AMT2", date);
        //找ptspta数据
        Map<String, PtsptaVo> maps = ptsptaToolComponent.getPtsptaList(inr, LTD_DATA_TYPE);
        if (maps.size() > 0) {
            maps.keySet().forEach(key -> {
                Utils.setFieldValue(ltdgrpVo, key.toLowerCase(), maps.get(key));
            });
        }

        return ltdgrpVo;
    }


    public Btdgrp getBtdgrpByInr(String inr) {
        Btdgrp btdgrp = new Btdgrp();
        btdgrp.reset();
        //找BTD的数据
        Btd btd = btdgrp.getRec();
        btd.setInr(inr);
        btd = btdMapper.selectByPrimaryKey(btd);
        if (btd == null) {
            throw new CommonServiceException("找不到Btd{" + inr + "}数据!");
        }
        btdgrp.setRec(btd);
        //找BTT的数据
        Btt btt = btdgrp.getBlk();
        btt.setInr(inr);
        btt = bttMapper.selectByPrimaryKey(btt);
        if (btt == null) {
            throw new CommonServiceException("找不到Btt{" + inr + "}数据!");
        }
        btdgrp.setBlk(btt);
        //找liacbs数据
        Date date = Dates.add(Utils.getMaxDate(), -1);
        cbsmodService.cbsGetBalance(btdgrp.getCbs().getMax(), BTD_DATA_TYPE, inr, "MAXSUM", "AMT1", date);
        cbsmodService.cbsGetBalance(btdgrp.getCbs().getOpn1(), BTD_DATA_TYPE, inr, "OPN", "AMT1", date);
        cbsmodService.cbsGetBalance(btdgrp.getCbs().getMax2(), BTD_DATA_TYPE, inr, "MAXSUM", "AMT2", date);
        cbsmodService.cbsGetBalance(btdgrp.getCbs().getOpn2(), BTD_DATA_TYPE, inr, "OPN", "AMT2", date);
        //找ptspta数据
        Map<String, PtsptaVo> maps = ptsptaToolComponent.getPtsptaList(inr, BTD_DATA_TYPE);
        if (maps.size() > 0) {
            maps.keySet().forEach(key -> {
                Utils.setFieldValue(btdgrp, key.toLowerCase(), maps.get(key));
            });
        }

        return btdgrp;
    }

    public String getTrdInrByOwnref(String ownref) {
        String inr = trdMapper.selectByownref(ownref);
        if (StringUtils.isNotEmpty(inr)) {
            return inr;
        } else {
            return null;
        }
    }

    public Trdgrp getTrdgrpByInr(String inr) {
        Trdgrp trdgrp = new Trdgrp();
        trdgrp.reset();
        //找LID的数据
        Trd trd = trdgrp.getRec();
        trd.setInr(inr);
        trd = trdMapper.selectByPrimaryKey(trd);
        if (trd == null) {
            throw new CommonServiceException("找不到TRD{" + inr + "}数据!");
        }
        trdgrp.setRec(trd);
        //找TRT的数据
        Trt trt = trdgrp.getBlk();
        trt.setInr(inr);
        trt = trtMapper.selectByPrimaryKey(trt);
        if (trt == null) {
            throw new CommonServiceException("找不到TRT{" + inr + "}数据!");
        }
        trdgrp.setBlk(trt);
        //找liacbs数据
        Date date = Dates.add(Utils.getMaxDate(), -1);
        cbsmodService.cbsGetBalance(trdgrp.getCbs().getMax(), "TRD", inr, "MAXSUM", "AMT1", date);
        cbsmodService.cbsGetBalance(trdgrp.getCbs().getIntacr(), "TRD", inr, "INTSUM", "ACR", date);
        cbsmodService.cbsGetBalance(trdgrp.getCbs().getIntclc(), "TRD", inr, "INTSUM", "CLC", date);
        cbsmodService.cbsGetBalance(trdgrp.getCbs().getIntcpl(), "TRD", inr, "INTSUM", "CPL", date);
        cbsmodService.cbsGetBalance(trdgrp.getCbs().getIntset(), "TRD", inr, "INTSUM", "SET", date);
        cbsmodService.cbsGetBalance(trdgrp.getCbs().getOpn1(), "TRD", inr, "OPN", "AMT1", date);

//        Platform.cbsGetBalance(argTRD.getCbs().getMax(),argTRD.getRec(),"MAXSUM","AMT1",argDat);
//        Platform.cbsGetBalance(argTRD.getCbs().getIntacr(),argTRD.getRec(),"INTSUM","ACR",argDat);
//        Platform.cbsGetBalance(argTRD.getCbs().getIntclc(),argTRD.getRec(),"INTSUM","CLC",argDat);
//        Platform.cbsGetBalance(argTRD.getCbs().getIntcpl(),argTRD.getRec(),"INTSUM","CPL",argDat);
//        Platform.cbsGetBalance(argTRD.getCbs().getIntset(),argTRD.getRec(),"INTSUM","SET",argDat);
//        Platform.cbsGetBalance(argTRD.getCbs().getOpn1(),argTRD.getRec(),"OPN","AMT1",argDat);
        //找ptspta数据
        Map<String, PtsptaVo> maps = ptsptaToolComponent.getPtsptaList(inr, "TRD");
        if (maps.size() > 0) {
            maps.keySet().forEach(key -> {
                Utils.setFieldValue(trdgrp, key.toLowerCase(), maps.get(key));
            });
        }
        return trdgrp;
    }

    public String isTRTrnAllowed(Trdgrp trdgrp, String trnNam) {

        String errSel = "No set of documents selected";
        String errClsY = "Selected set of documents is closed";
        String errTenY = "There are open defered payment maturities under selected set of documents";
        String errTenN = "No deferred payment maturities under selected set of documents";
        String errRejY = "Selected set of documents is already rejected or accepted";
        String errRefY = "Selected set of documents has already been refused and returned";
        String errIntN = "Documents under selected set of documents have been fully settled/accepted";
        String errMT750 = "No advice of discrepancies received for the selected set of documents";
        String errRcvN = "Documents under selected document contract have not been received yet";
        String errRcvY = "Documents under selected document contract have been received already";
        String errAdvY = "Documents under selected document contract have been advised already";
        String errOpnN = "Selected document contract is not yet opened";
        String fincodN = "核心未开户";

        if ("TRTSET".equals(trnNam)) {

            if (StringUtils.isEmpty(trdgrp.getRec().getInr())) {
                log.warn(errRcvY);
                return IS_ERROR;
            } else {
                // not allowed if contract is closed
                if (Objects.nonNull(trdgrp.getRec().getClsdat())) {
                    log.warn(errClsY);
                    return IS_ERROR;
                }
                // not allowed if loan is not open
                if (Objects.isNull(trdgrp.getRec().getOpndat())) {
                    log.warn(errOpnN);
                    return IS_ERROR;
                }
                // not allowed if loan is fully repaid
//                compareTo(BigDecimal.valueOf(0)) <= 0
                if (trdgrp.getCbs().getOpn1().getAmt().compareTo(BigDecimal.valueOf(0)) < 0) {
                    log.warn(errIntN);
                    return IS_ERROR;
                }
            }
            if (StringUtils.isEmpty(trdgrp.getRec().getFincod())) {
                log.warn(fincodN);
                return IS_ERROR;
            }
//            if( ! Platform.checkFinStatus("TRD",trdgrp.getRec().getInr()) )
//            {
//                msgTxt = msgTxt + cr + MdaUtils.getI18NString("trdlod","CT000030");
//                cr = CR;
//                res = false;
//            }


//            if (StringUtils.isNotEmpty(trdgrp.getRec().getInr()) && Objects.nonNull(trdgrp.getRec().getRcvdat())) {
//                log.warn(errRcvY);
//                return IS_ERROR;
//            }
        }
        return errSel;
    }

    //lialed.bookCnfAmt
    public void bookCnfAmt(LiaallVo liaallVo, SimpleModuleVo linkModule, BigDecimal argMaxAmt) {
        Cbb maxCbb = liaallVo.getCbsMap().getOrDefault("MAX", new Cbb());
        liaallService.liaallSetGrpKey( "LE", "INT", "INT" );
        liaallService.liaallSetGrpKey( "LE", "AVL", "AVL" );
        liaallService.liaallSetGrpKey( "LE", "AVS", "AVS" );
        liaallService.liaallSetCbeAmount( "ISS", 3, maxCbb.getCur(), xrtmodService.amtRnd(argMaxAmt, maxCbb.getCur()), "INT", linkModule);
        liaallService.liaallSetDisable( "INT" );
    }

    public void collectCbsMap(TransactionServiceSet set, String prefix, Btacbs cbs, String... cbsNames) {
        Map<String, Cbb> cbsMap = set.getCbsMap();
        if (cbsMap == null) {
            cbsMap = new HashMap<>();
        }
        for (String name : cbsNames) {
            Cbb cbb = Utils.getFieldValue(cbs, name);
            if (cbb != null) {
                cbsMap.put(prefix.toUpperCase() + name.toUpperCase(), cbb);
            }
        }
        set.setCbsMap(cbsMap);
    }

    public void collectPtsList(TransactionServiceSet set, Btdgrp btdgrp,String... ptsNames) {
        if (ptsNames == null) {
            //找所有的ptspta对象
            Field[] fields = btdgrp.getClass().getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                if (PtsptaVo.class.equals(field.getType())) {
                    PtsptaVo ptspta = Utils.getFieldValue(btdgrp, field.getName());
                    if (ptspta != null) {
                        set.addPtspta(transferFrom(ptspta));
                    }
                }
            }
        } else {
            for (String name : ptsNames) {
                PtsptaVo ptspta = Utils.getFieldValue(btdgrp, name);
                if (ptspta != null && ptspta.getPts() != null && StringUtils.isNotEmpty(ptspta.getPts().getRol())) {
                    set.addPtspta(transferFrom(ptspta));
                }
            }
        }
    }
}
