package com.brilliance.isc.common.bop.bopdec.service.imp;

import com.brilliance.mda.runtime.mda.util.Dates;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import com.brilliance.mda.runtime.mda.util.Numerics;
import com.brilliance.mda.runtime.mda.util.Strings;
import com.brilliance.isc.bo.*;
import com.brilliance.isc.bo.bopdecbo.Dba;
import com.brilliance.isc.bo.bopdecbo.Dbd;
import com.brilliance.isc.bo.bopdecbo.Dbg;
import com.brilliance.isc.bo.bopdecbo.Dbr;
import com.brilliance.isc.bo.model.BopObjectType;
import com.brilliance.isc.bo.model.Bopgat;
import com.brilliance.isc.bo.model.CheckItems;
import com.brilliance.isc.common.sysmod.SysmodService;
import com.brilliance.isc.common.util.BopUtils;
import com.brilliance.isc.common.bopset.service.BopdecCommonService;
import com.brilliance.isc.common.bopset.service.BopgatService;
import com.brilliance.isc.common.bopset.service.CtlmodService;
import com.brilliance.isc.common.cache.pty.PtyCacheService;
import com.brilliance.isc.common.codetable.service.CodetableService;
import com.brilliance.isc.common.component.PtsptaToolComponent;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.transaction.help.TransactionHelpService;
import com.brilliance.isc.common.trnmod.vo.TrnmodVo;
import com.brilliance.isc.common.vo.PtsptaVo;
import com.brilliance.isc.mda.dao.*;
import com.brilliance.isc.vo.bop.BopActionTypeVo;
import com.brilliance.isc.vo.bop.RptnoRequestVo;
import com.brilliance.isc.vo.collection.BodBizInfoVo;
import com.brilliance.isc.vo.collection.CcdBizInfoVo;
import com.brilliance.isc.vo.finance.BpdBizInfoVo;
import com.brilliance.isc.vo.lc.BedBizInfoVo;
import com.brilliance.isc.vo.lc.BtdBizInfoVo;
import com.brilliance.isc.vo.lg.GcdBizInfoVo;
import com.brilliance.isc.vo.lg.NcdBizInfoVo;
import com.brilliance.isc.vo.remit.CpdBizInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import static com.brilliance.mda.runtime.mda.Constants.NULLSTR;

@Service
public class BopgatServiceImpl implements BopgatService {

    @Autowired
    private PtsptaToolComponent ptsptaToolComponent;

    @Autowired
    private PtyCacheService ptyCacheService;

    @Autowired
    private BopdecCommonService bopdecCommonService;

    @Autowired
    private CodetableService codetableService;

    @Autowired
    private TransactionHelpService transactionHelpService;

    @Autowired
    private CtlmodService ctlmodService;

    @Autowired
    private DblMapper dblMapper;

    @Autowired
    private DbaMapper dbaMapper;

    @Autowired
    private DbgMapper dbgMapper;

    @Autowired
    private DbdMapper dbdMapper;

    @Autowired
    private DbrMapper dbrMapper;

    @Autowired
    SysmodService sysmodService;

    @Autowired
    private AdrMapper adrMapper;


    @Override
    public Bopgat mappingBopgatDataAAAAAA(Bopgat bopgat) {
        //将未初始化的属性初始化
        bopgat.reset();
        // 后端逻辑全是跟大写的transName做比对，这里先转成大写
        bopgat.setTransName(bopgat.getTransName().toUpperCase());
        String transName = bopgat.getTransName().toUpperCase();

        String szflg = bopgat.getSzflg();
        String oldszflg = bopgat.getOldszflg();
        String basflg = bopgat.getBasflg();
        String dclflg = bopgat.getDclflg();
        String vrfflg = bopgat.getVrfflg();
        String aloneflg = bopgat.getAloneflg();
        String acttyp = bopgat.getActtyp();

        // 申报类型选择不报送，需要清空原有数据，如果申报编号已生成，则保留申报编号防止跳号
        if("3".equals(szflg)){
            Bopgat newBopgat = new Bopgat();
            newBopgat.reset();
            String dbarptno = "";
            String dbdrptno = "";
            dbarptno = bopgat.getDbagrp().getBas().getRptno();
            newBopgat.getDbagrp().getBas().setRptno(dbarptno);
            newBopgat.getDbagrp().getDcl().setRptno(dbarptno);

            dbdrptno = bopgat.getDbdgrp().getBas().getRptno();
            newBopgat.getDbdgrp().getBas().setRptno(dbdrptno);
            newBopgat.getDbdgrp().getVrf().setRptno(dbdrptno);
            newBopgat.setSzflg(bopgat.getSzflg());
            return newBopgat;
        }

        //地区机构号取值
        String ownextkey = bopgat.getOwnextkey();
        if (MdaUtils.compareTo(szflg, "1") == 0 || MdaUtils.compareTo(szflg, "2") == 0) {
            if (!bopgat.isModified("ownextkey")) {
                //防止未选择客户时空值针
                if(!MdaUtils.isEmpty(SettleContext.getUserSession().getWrkbch())){
                    ownextkey = SettleContext.getUserSession().getWrkbch().getBranch();
                    bopgat.setOwnextkey(ownextkey);
                }
            }
        }
        // 是否随业务申报
        if(MdaUtils.compareTo(szflg, "1") == 0 || MdaUtils.compareTo(szflg, "2") == 0){
            bopgat.setAloneflg(aloneflg);
        }

        //款项来源
        if(MdaUtils.compareTo(szflg, "1") == 0){
            bopgat.setActtyp(acttyp);
        }

        //ptapta begin
        PtsptaVo ptsptaAPL = ptsptaToolComponent.getPtsFromListByRol(bopgat.getPtsList(), "APL");
        if (MdaUtils.isEmpty(ptsptaAPL)) {
            ptsptaAPL = new PtsptaVo();
            ptsptaAPL.reset();
        }
        PtsptaVo ptsptaDRE = ptsptaToolComponent.getPtsFromListByRol(bopgat.getPtsList(), "DRE");
        if (MdaUtils.isEmpty(ptsptaDRE)) {
            ptsptaDRE = new PtsptaVo();
            ptsptaDRE.reset();
        }
        PtsptaVo ptsptaCOR = ptsptaToolComponent.getPtsFromListByRol(bopgat.getPtsList(), "COR");
        if (MdaUtils.isEmpty(ptsptaCOR)) {
            ptsptaCOR = new PtsptaVo();
            ptsptaCOR.reset();
        }
        PtsptaVo ptsptaORC = ptsptaToolComponent.getPtsFromListByRol(bopgat.getPtsList(), "ORC");
        if (MdaUtils.isEmpty(ptsptaORC)) {
            ptsptaORC = new PtsptaVo();
            ptsptaORC.reset();
        }
        //收款人PYE
        PtsptaVo ptsptaPYE = ptsptaToolComponent.getPtsFromListByRol(bopgat.getPtsList(), "PYE");
        if (MdaUtils.isEmpty(ptsptaPYE)) {
            ptsptaPYE = new PtsptaVo();
            ptsptaPYE.reset();
        }
        //代付行
        PtsptaVo ptsptaDFF = ptsptaToolComponent.getPtsFromListByRol(bopgat.getPtsList(), "DFF");
        if (MdaUtils.isEmpty(ptsptaDFF)) {
            ptsptaDFF = new PtsptaVo();
            ptsptaDFF.reset();
        }
        //融资申请人FIA
        PtsptaVo ptsptaFIA = ptsptaToolComponent.getPtsFromListByRol(bopgat.getPtsList(), "FIA");
        if (MdaUtils.isEmpty(ptsptaFIA)) {
            ptsptaFIA = new PtsptaVo();
            ptsptaFIA.reset();
        }


        /**
         * 需要获取的业务数据
         */
        String boptyp = "BOPGAT";
        String objTyp = NULLSTR;
        Pty pty = null;
        Pty pty1 = null;
        // 业务数据里的paytype
        String payTyp = NULLSTR;
        String payType = NULLSTR;
        String orcact = NULLSTR;
        //收款人账户
        String pyeact = NULLSTR;
        // 银行业务编号
        String ownref = "";
        String hndtyp = NULLSTR;
        //外币款专户
        String ffaacc = NULLSTR;
        //默认对手费用收取角色 setmod里取
        String feecorrol = NULLSTR;
        //setmod 单据币种
        String doccur = NULLSTR;
        //委托行代付
        String dfflag = NULLSTR;
        //代付业务编号
        String dfownref = NULLSTR;
        //代付币种
        String dfcur = NULLSTR;
        //代付金额
        BigDecimal dfamt = BigDecimal.ZERO;
        //cptadv 处理方式
        String accmod = NULLSTR;
        //cptadv 付款人国别
        String draweecountcode = NULLSTR;
        // getset netset payrol
        String payrol = NULLSTR;
        //setmode 自贸区主账号
        String zmqacc = NULLSTR;
        //同名划转
        String tsnflg = NULLSTR;
        // 托收交易对手国别
        String stacty = NULLSTR;
        // ledgrp inr
        String ledinr = NULLSTR;
        //根据交易类型组数
        switch (Strings.mid(transName, 1, 3)) {
            case "CPT":
                CpdBizInfoVo cpdBizInfoVo = (CpdBizInfoVo)bopgat.getRec();
                ownref = cpdBizInfoVo.getOwnref();
                payTyp = cpdBizInfoVo.getPaytyp();
                payType = cpdBizInfoVo.getPaytype();
                objTyp = cpdBizInfoVo.getObjtyp();
                orcact = cpdBizInfoVo.getOrcact();
                ffaacc = cpdBizInfoVo.getFfaacc();
                doccur = cpdBizInfoVo.getDoccur();
                feecorrol = cpdBizInfoVo.getFeecorrol();
                accmod = cpdBizInfoVo.getAccmod();
                draweecountcode = cpdBizInfoVo.getDraweecountcode();
                zmqacc = cpdBizInfoVo.getZmqacc();
                tsnflg = cpdBizInfoVo.getTsnflg();
                pyeact = cpdBizInfoVo.getPyeact();
                break;
            case "BOT":
                BodBizInfoVo bodBizInfoVo = (BodBizInfoVo)bopgat.getRec();
                ownref = bodBizInfoVo.getOwnref();
                objTyp = bodBizInfoVo.getObjtyp();
                doccur = bodBizInfoVo.getDoccur();
                feecorrol = bodBizInfoVo.getFeecorrol();
                break;
            case "CCT":
                CcdBizInfoVo ccdBizInfoVo = (CcdBizInfoVo)bopgat.getRec();
                ownref = ccdBizInfoVo.getOwnref();
                objTyp = ccdBizInfoVo.getObjtyp();
                doccur = ccdBizInfoVo.getDoccur();
                feecorrol = ccdBizInfoVo.getFeecorrol();
                stacty = ccdBizInfoVo.getStacty();
                break;
            case "BPT":
                BpdBizInfoVo bpdBizInfoVo = (BpdBizInfoVo)bopgat.getRec();
                ownref = bpdBizInfoVo.getOwnref();
                objTyp = bpdBizInfoVo.getObjtyp();
                doccur = bpdBizInfoVo.getDoccur();
                feecorrol = bpdBizInfoVo.getFeecorrol();
                dfflag = bpdBizInfoVo.getDfflag();
                dfownref = bpdBizInfoVo.getDfownref();
                dfcur = bpdBizInfoVo.getDfcur();
                dfamt = bpdBizInfoVo.getDfamt();
                ledinr = bpdBizInfoVo.getLedinr();
                break;
            case "BET":
                BedBizInfoVo bedBizInfoVo = (BedBizInfoVo)bopgat.getRec();
                ownref = bedBizInfoVo.getOwnref();
                objTyp = bedBizInfoVo.getObjtyp();
                doccur = bedBizInfoVo.getDoccur();
                feecorrol = bedBizInfoVo.getFeecorrol();
                ledinr = bedBizInfoVo.getLedinr();
                break;
            case "BTT":
                BtdBizInfoVo btdBizInfoVo = (BtdBizInfoVo)bopgat.getRec();
                ownref = btdBizInfoVo.getOwnref();
                objTyp = btdBizInfoVo.getObjtyp();
                doccur = btdBizInfoVo.getDoccur();
                feecorrol = btdBizInfoVo.getFeecorrol();
                ledinr = btdBizInfoVo.getLedinr();
                break;
            case "GIT":
            case "GET":
                GcdBizInfoVo gcdBizInfoVo = (GcdBizInfoVo)bopgat.getRec();
                ownref = gcdBizInfoVo.getOwnref();
                objTyp = gcdBizInfoVo.getObjtyp();
                doccur = gcdBizInfoVo.getDoccur();
                hndtyp = gcdBizInfoVo.getHndtyp();
                feecorrol = gcdBizInfoVo.getFeecorrol();
                payrol = gcdBizInfoVo.getPayrol();
                break;
            case "NET":
                NcdBizInfoVo ncdBizInfoVo = (NcdBizInfoVo)bopgat.getRec();
                ownref = ncdBizInfoVo.getOwnref();
                objTyp = ncdBizInfoVo.getObjtyp();
                doccur = ncdBizInfoVo.getDoccur();
                hndtyp = ncdBizInfoVo.getHndtyp();
                feecorrol = ncdBizInfoVo.getFeecorrol();
                payrol = ncdBizInfoVo.getPayrol();
                break;
        }

        //境内申报最新需求,收付汇款人国别均为CHN，同名划转，账号性质为主账号或者为账号性质为资本项目，则交易编码为929010灰显不可修改
        //境内非居民机构账户减境内划转资金，交易双方国别均不为CHN，账号不为NRA/FTN/EFN/OSA,交易附言不含有“非居民机构账户间划转”“NONRESIDENT”
        //交易编码为929090灰显且不可修改
        if(MdaUtils.isEmpty(bopgat.getBoptxcode())) {
            String boptxcode = NULLSTR;
            String rolCountry = NULLSTR;
            String rolAct = NULLSTR;
            String rivalAct = NULLSTR;
            String rivalCountry = NULLSTR;
            if (MdaUtils.compareTo(szflg, "2") == 0) {
                switch (Strings.mid(transName, 1, 3)) {
                    case "CPT":
                        if (!MdaUtils.isEmpty(ptsptaPYE.getPtainr())) {
                            String ptaInr = ptsptaPYE.getPtainr();
                            Adr adr = adrMapper.selectByPtaInr(ptaInr);
                            if (!MdaUtils.isEmpty(adr)) {
                                String loccty = adr.getLoccty();
                                if (!MdaUtils.isEmpty(loccty)) {
                                    rolCountry = codetableService.getTblLabel(loccty, "CTYCOD");
                                    rivalCountry = draweecountcode;
                                }
                            }
                        }
                        rolAct = pyeact;
                        rivalAct = orcact;
                        break;
                }
                if (MdaUtils.compareTo(tsnflg, "X") == 0) {
                    if (!MdaUtils.isEmpty(rolCountry) && !MdaUtils.isEmpty(rivalCountry)) {
                        if (MdaUtils.compareTo(rolCountry, "CHN") == 0 && MdaUtils.compareTo(rivalCountry, "CHN") == 0 && MdaUtils.compareTo(tsnflg, "X") == 0 && bopdecCommonService.isAccountZbxm(pyeact)) {
                            boptxcode = "929010";
                        }
                    }
                } else {
                    if (!MdaUtils.isEmpty(rolCountry) && MdaUtils.compareTo(rolCountry, "CHN") != 0 && !MdaUtils.isEmpty(rivalCountry) && MdaUtils.compareTo(rivalCountry, "CHN") != 0) {
                        if (bopdecCommonService.isValidAct(rolAct) && (bopdecCommonService.isValidAct(rivalAct) || bopdecCommonService.isValidCommet(bopgat.getCommetItems()))) {
                            boptxcode = "929090";
                        }
                    }
                }
            }
            if (!bopgat.isModified("boptxcode")) {
                bopgat.setBoptxcode(boptxcode);
            }
        }

        //设置国外银行扣费（涉外收入申报专用)币种
        bopgat.setOutchargeccy(doccur);

        if(!(("1".equals(szflg) || "2".equals(szflg)) && "X".equals(basflg) && !MdaUtils.isEmpty(ownextkey))){
            return bopgat;
        }

        //记录上一次确认非居民机构账号间境内划转资金状态时影响要素的值
        String oppold = "";
        String orcactold = "";
        String lcyaccold = "";
        String fcyaccold = "";
        String othaccold = "";
        int posacc = 0;
        if(!bopgat.getDbdgrp().getBas().isModified("oppuser")){
            if(MdaUtils.compareTo(bopgat.getDbdgrp().getBas().getCustype(),"C") == 0){
                if(bopdecCommonService.ischeckacc(bopgat.getCommetItems(),bopgat.getDbdgrp().getBas().getLcyacc(),bopgat.getDbdgrp().getBas().getFcyacc(),bopgat.getDbdgrp().getBas().getOthacc())){
                    posacc = Strings.pos(bopgat.getDbdgrp().getBas().getOppuser(),"[acc:");
//                  orcactold = this.getDbdp().getOldact();
                    orcactold = bopgat.getDbdgrp().getOldact();
                    if( posacc == 0 ) {
                        oppold = bopgat.getDbdgrp().getBas().getOppuser();
                    }else{
                        if( posacc > 0 ) {
                            oppold = Strings.mid(bopgat.getDbdgrp().getBas().getOppuser(),1,posacc - 1);
                        }
                    }
                    lcyaccold = bopgat.getDbdgrp().getBas().getLcyacc();
                    fcyaccold = bopgat.getDbdgrp().getBas().getFcyacc();
                    othaccold = bopgat.getDbdgrp().getBas().getOthacc();
                }
            }
        }
        if("X".equals(dfflag)){
            bopgat.getDbagrp().getBas().setBuscode(dfownref);
            bopgat.getDbdgrp().getBas().setBuscode(dfownref);
        }else {
            bopgat.getDbagrp().getBas().setBuscode(ownref);
            bopgat.getDbdgrp().getBas().setBuscode(ownref);
        }
        //设置申报样式
        String typ = bopdecCommonService.getBopdecTyp(boptyp,bopgat.getSzflg(),bopgat.getBasflg(),bopgat.getDclflg(),bopgat.getVrfflg());
        bopgat.setTyp(typ);
        bopgat.setBoptyp(boptyp);

        //设置申报子类型
        if("1".equals(bopgat.getSzflg())){
            bopgat.setBopobjtyp("DBA");
        }
        if("2".equals(bopgat.getSzflg())){
            bopgat.setBopobjtyp("DBD");
        }
        //设置收付汇日期
        if(MdaUtils.isEmpty(bopgat.getPaydat())){
            String dectyp = ctlmodService.setDectyp(transName);
            if("V".equals(dectyp)){
                bopgat.setPaydat(bopdecCommonService.getCoreSysDay());
            }
        }

        String nam1 = NULLSTR;
        //组织机构代码
        String juscod = NULLSTR;
        //客户ptyinr
        String cusptyinr = NULLSTR;
        // 获取交易币种
        String cur = bopdecCommonService.getTranCur(transName,bopgat.getCbsMap());
        pty = bopdecCommonService.getPtyByTransname(transName,bopgat.getPtsList(),"APL",payTyp,hndtyp);
        if (!MdaUtils.isEmpty(pty)) {
            nam1 = pty.getNam1();
            juscod = pty.getJuscod();
            cusptyinr = pty.getInr();
        }
        if(!bopgat.getDbagrp().getBas().isModified("custnm")) {
            bopgat.getDbagrp().getBas().setCustnm(nam1);
        }
        if(!bopgat.getDbdgrp().getBas().isModified("custnm")) {
            bopgat.getDbdgrp().getBas().setCustnm(nam1);
        }

        String fieldVal = NULLSTR;
        String fieldVal2 = NULLSTR;
        //身份证号
        String idCode = NULLSTR;
        String transRol = bopdecCommonService.getTranRol(transName);
        pty1 = bopdecCommonService.getPtyByTransname(transName,bopgat.getPtsList(),transRol,payTyp,hndtyp);
        if (!MdaUtils.isEmpty(pty1)) {
            fieldVal = pty1.getExtkey();
            fieldVal2 = pty1.getIdtype();
            idCode = pty1.getIdcode();
        }
        if( MdaUtils.compareTo(fieldVal2,"1") == 0 || MdaUtils.compareTo(fieldVal2,"2") == 0 ) {
            bopgat.getDbagrp().getBas().setCustype("C");
            bopgat.getDbdgrp().getBas().setCustype("C");
        }else{
            if(!MdaUtils.isEmpty(fieldVal2)){
                //（境内收入）出口收汇核销专用联，如静态数据为F，带入交易应转为D----刘文青
                String type = bopgat.getSzflg();
                if( MdaUtils.compareTo(type,"2") == 0 && MdaUtils.compareTo(fieldVal2,"F") == 0 ) {
                    bopgat.getDbagrp().getBas().setCustype("D");
                    bopgat.getDbdgrp().getBas().setCustype("D");

                }
            }else{
                bopgat.getDbagrp().getBas().setCustype("C");
                bopgat.getDbdgrp().getBas().setCustype("C");
            }
        }
        //获得组织机构代码
        if( MdaUtils.compareTo(bopgat.getDbagrp().getBas().getCustype(),"C") == 0 ) {
            //对公
            if(!bopgat.getDbagrp().getBas().isModified("custcod")){
                bopgat.getDbagrp().getBas().setCustcod(juscod);
            }
            if(!bopgat.getDbdgrp().getBas().isModified("custcod")){
                bopgat.getDbdgrp().getBas().setCustcod(juscod);
            }
            if(!bopgat.getDbagrp().getBas().isModified("idcode")){
                bopgat.getDbagrp().getBas().setIdcode("");
            }
            if(!bopgat.getDbdgrp().getBas().isModified("idcode")) {
                bopgat.getDbdgrp().getBas().setIdcode("");
            }
        }else{
            //给身份证号赋值
            if(!bopgat.getDbagrp().getBas().isModified("idcode")) {
                bopgat.getDbagrp().getBas().setIdcode(idCode);
            }
            if(!bopgat.getDbdgrp().getBas().isModified("idcode")) {
                bopgat.getDbdgrp().getBas().setIdcode(idCode);
            }
            if(!bopgat.getDbagrp().getBas().isModified("custcod")) {
                bopgat.getDbagrp().getBas().setCustcod("");
            }
            if(!bopgat.getDbdgrp().getBas().isModified("custcod")) {
                bopgat.getDbdgrp().getBas().setCustcod("");
            }
        }

        //文青需求 此处以界面为准
        BigDecimal outchargeamt = bopdecCommonService.getchargeFamt(transName,bopgat.getOutchargeamt());
        bopgat.getDbagrp().getBas().setOutchargeamt(outchargeamt);
        //获得扣费币种OUTCHARGECCY
        if( outchargeamt.compareTo(BigDecimal.ZERO)>0 ) {
            bopgat.getDbagrp().getBas().setOutchargeccy(cur);
        }else {
            bopgat.getDbagrp().getBas().setOutchargeccy("");
        }

        //endif
        //刘文青需求，bptopn交易中不出国内银行费用。
        BigDecimal inchargeamt = null;
        if( MdaUtils.compareTo(transName,"BPTOPN") == 0 ) {
            bopgat.getDbagrp().getBas().setInchargeccy("");
            bopgat.getDbdgrp().getBas().setInchargeccy("");
            bopgat.getDbagrp().getBas().setInchargeamt(BigDecimal.ZERO);
            bopgat.getDbdgrp().getBas().setInchargeamt(BigDecimal.ZERO);
        }else {
            inchargeamt = bopdecCommonService.getInchargeAmt(transName,cur,bopgat.getSetFelList(),bopgat.getPtydet());
            bopgat.getDbagrp().getBas().setInchargeamt(inchargeamt);
            bopgat.getDbdgrp().getBas().setInchargeamt(inchargeamt);
            if( inchargeamt.compareTo(BigDecimal.ZERO)>0 ) {
                bopgat.getDbagrp().getBas().setInchargeccy(cur);
                bopgat.getDbdgrp().getBas().setInchargeccy(cur);
            }else {
                bopgat.getDbagrp().getBas().setInchargeccy("");
                bopgat.getDbdgrp().getBas().setInchargeccy("");
            }
        }

        Map<String, BigDecimal> amtList = new HashMap<>();
        Map<String, String> accList = new HashMap<>();
        bopdecCommonService.getBasic(transName,bopgat.getSetgll(),cur,ffaacc,amtList,accList);
        String lcyacc = accList.get("lcyacc");
        BigDecimal exrate = amtList.get("exrate");
        String fcyacc = accList.get("fcyacc");
        String othacc = accList.get("othacc");
        BigDecimal lcyamt = amtList.get("lcyamt");
        BigDecimal fcyamt = amtList.get("fcyamt");
        BigDecimal othamt = amtList.get("othamt");
        if(!bopgat.getDbagrp().getBas().isModified("lcyacc")) {
            bopgat.getDbagrp().getBas().setLcyacc(lcyacc);
        }
        if(!bopgat.getDbdgrp().getBas().isModified("lcyacc")) {
            bopgat.getDbdgrp().getBas().setLcyacc(lcyacc);
        }
        if(!bopgat.getDbagrp().getBas().isModified("exrate")) {
            bopgat.getDbagrp().getBas().setExrate(exrate);
        }
        if(!bopgat.getDbdgrp().getBas().isModified("exrate")) {
            bopgat.getDbdgrp().getBas().setExrate(exrate);
        }
        if(!bopgat.getDbagrp().getBas().isModified("fcyacc")) {
            bopgat.getDbagrp().getBas().setFcyacc(fcyacc);
        }
        if(!bopgat.getDbdgrp().getBas().isModified("fcyacc")) {
            bopgat.getDbdgrp().getBas().setFcyacc(fcyacc);
        }
        if(!bopgat.getDbagrp().getBas().isModified("othacc")) {
            bopgat.getDbagrp().getBas().setOthacc(othacc);
        }
        if(!bopgat.getDbdgrp().getBas().isModified("othacc")) {
            bopgat.getDbdgrp().getBas().setOthacc(othacc);
        }
        if(!bopgat.getDbagrp().getBas().isModified("lcyamt")) {
            bopgat.getDbagrp().getBas().setLcyamt(Numerics.round(lcyamt, 0));
        }
        if(!bopgat.getDbdgrp().getBas().isModified("lcyamt")) {
            bopgat.getDbdgrp().getBas().setLcyamt(Numerics.round(lcyamt,0));
        }
        if(!bopgat.getDbagrp().getBas().isModified("fcyamt")) {
            bopgat.getDbagrp().getBas().setFcyamt(Numerics.round(fcyamt, 0));
        }
        if(!bopgat.getDbdgrp().getBas().isModified("fcyamt")) {
            bopgat.getDbdgrp().getBas().setFcyamt(Numerics.round(fcyamt, 0));
        }
        if(!bopgat.getDbagrp().getBas().isModified("othamt")) {
            bopgat.getDbagrp().getBas().setOthamt(Numerics.round(othamt, 0));
        }
        if(!bopgat.getDbdgrp().getBas().isModified("othamt")) {
            bopgat.getDbdgrp().getBas().setOthamt(Numerics.round(othamt, 0));
        }

        //汇款交易 需要针对 自贸区机构 业务进行特殊处理，如果是自贸区，申报人账号需要取主账号
        String zmqflg = NULLSTR;
        Bch bch = SettleContext.getUserSession().getBch();
        if(!MdaUtils.isEmpty(bch)){
            zmqflg = bch.getZmqflg();
        }

        if("CPTADV".equals(transName)){
            if(fcyamt.compareTo(BigDecimal.ZERO) > 0) {
                if("X".equals(zmqflg)){
                    if (!bopgat.getDbagrp().getBas().isModified("fcyacc")) {
                        bopgat.getDbagrp().getBas().setFcyacc(zmqacc);
                    }
                    if (!bopgat.getDbdgrp().getBas().isModified("fcyacc")) {
                        bopgat.getDbdgrp().getBas().setFcyacc(zmqacc);
                    }
                }
            }
            if(othamt.compareTo(BigDecimal.ZERO) > 0){
                if("X".equals(zmqflg)){
                    if (!bopgat.getDbagrp().getBas().isModified("othacc")) {
                        bopgat.getDbagrp().getBas().setOthacc(zmqacc);
                    }
                    if (!bopgat.getDbdgrp().getBas().isModified("othacc")) {
                        bopgat.getDbdgrp().getBas().setOthacc(zmqacc);
                    }
                }
            }
        }

        /*
        //todo 代付场景 现汇金额为代付金额  结汇金额 人民币账号 其他金额 其他账号都 默认为空
        if("X".equals(dfflag)){
            if(!bopgat.getDbagrp().getBas().isModified("fcyamt")) {
                bopgat.getDbagrp().getBas().setFcyamt(Numerics.round(dfamt,0));
            }
            if(!bopgat.getDbdgrp().getBas().isModified("fcyamt")) {
                bopgat.getDbdgrp().getBas().setFcyamt(Numerics.round(dfamt,0));
            }
            if(!bopgat.getDbagrp().getBas().isModified("othamt")) {
                bopgat.getDbagrp().getBas().setOthamt(BigDecimal.ZERO);
            }
            if(!bopgat.getDbdgrp().getBas().isModified("othamt")) {
                bopgat.getDbdgrp().getBas().setOthamt(BigDecimal.ZERO);
            }
            if(!bopgat.getDbagrp().getBas().isModified("lcyamt")) {
                bopgat.getDbagrp().getBas().setLcyamt(BigDecimal.ZERO);
            }
            if(!bopgat.getDbdgrp().getBas().isModified("lcyamt")) {
                bopgat.getDbdgrp().getBas().setLcyamt(BigDecimal.ZERO);
            }
            if(!bopgat.getDbagrp().getBas().isModified("lcyacc")) {
                bopgat.getDbagrp().getBas().setLcyacc("");
            }
            if(!bopgat.getDbdgrp().getBas().isModified("lcyacc")) {
                bopgat.getDbdgrp().getBas().setLcyacc("");
            }
            if(!bopgat.getDbagrp().getBas().isModified("othacc")) {
                bopgat.getDbagrp().getBas().setOthacc("");
            }
            if(!bopgat.getDbdgrp().getBas().isModified("othacc")) {
                bopgat.getDbdgrp().getBas().setOthacc("");
            }
        }

         */

        //给收入款金额赋值
        BigDecimal txamt = null;
        switch( Strings.mid(transName,1,2) ) {
            case "BR":
            case "BC":
            case "GI":
            case "NI":
                txamt = Numerics.round(lcyamt.add(fcyamt).add(othamt),0);
                bopgat.getDbagrp().getBas().setTxamt(txamt);
                bopgat.getDbdgrp().getBas().setTxamt(txamt);
                break;
            default:
                txamt = bopdecCommonService.gettxccy(transName,bopgat.getSetgll(),accmod,payrol);
                bopgat.getDbagrp().getBas().setTxamt(txamt);
                bopgat.getDbdgrp().getBas().setTxamt(txamt);
        }
        // 代付场景取代付金额
        if("X".equals(dfflag)){
            txamt = dfamt;
            bopgat.getDbagrp().getBas().setTxamt(dfamt);
            //代付场景下 境内收入的收款金额也去代付金额
            bopgat.getDbdgrp().getBas().setTxamt(dfamt);
        }
        if( MdaUtils.compareTo(objTyp,"BOD") == 0 ) {
            if(!MdaUtils.isEmpty(ptsptaDRE.getPts())){
                if(!bopgat.getDbagrp().getBas().isModified("oppuser")) {
                    bopgat.getDbagrp().getBas().setOppuser(ptsptaDRE.getPts().getNam());
                }
                if(!bopgat.getDbdgrp().getBas().isModified("oppuser")) {
                    bopgat.getDbdgrp().getBas().setOppuser(ptsptaDRE.getPts().getNam());
                }
            }
            bopgat.getDbagrp().getBas().setMethod("C");
            bopgat.getDbdgrp().getBas().setMethod("C");
        }
        if( MdaUtils.compareTo(objTyp,"BED") == 0 ) {
            Map<String,PtsptaVo> ptsptaVoMap = ptsptaToolComponent.getPtsptaList(ledinr,"LED");
            ptsptaAPL = ptsptaVoMap.get("APL");
            if(!MdaUtils.isEmpty(ptsptaAPL.getPts())){
                if(!bopgat.getDbagrp().getBas().isModified("oppuser")) {
                    bopgat.getDbagrp().getBas().setOppuser(ptsptaAPL.getPts().getNam());
                }
                if(!bopgat.getDbdgrp().getBas().isModified("oppuser")) {
                    bopgat.getDbdgrp().getBas().setOppuser(ptsptaAPL.getPts().getNam());
                }
            }
            bopgat.getDbagrp().getBas().setMethod("L");
            bopgat.getDbdgrp().getBas().setMethod("L");
        }
        if( MdaUtils.compareTo(objTyp,"BTD") == 0 ) {
            Map<String,PtsptaVo> ptsptaVoMap = ptsptaToolComponent.getPtsptaList(ledinr,"LED");
            ptsptaAPL = ptsptaVoMap.get("APL");
            if(!MdaUtils.isEmpty(ptsptaAPL.getPts())){
                if(!bopgat.getDbagrp().getBas().isModified("oppuser")) {
                    bopgat.getDbagrp().getBas().setOppuser(ptsptaAPL.getPts().getNam());
                }
                if(!bopgat.getDbdgrp().getBas().isModified("oppuser")) {
                    bopgat.getDbdgrp().getBas().setOppuser(ptsptaAPL.getPts().getNam());
                }
            }
            bopgat.getDbagrp().getBas().setMethod("L");
            bopgat.getDbdgrp().getBas().setMethod("L");
            txamt = Numerics.round(lcyamt.add(fcyamt).add(othamt),0);
            bopgat.getDbagrp().getBas().setTxamt(txamt);
            bopgat.getDbdgrp().getBas().setTxamt(txamt);
        }
        if( MdaUtils.compareTo(objTyp,"BPD") == 0 ) {
            //代付融资放款交易，汇款人取消关联境外代付行，经办手动输入汇款人----刘奕洋
            if("X".equals(dfflag)){
//                if(!bopgat.getDbagrp().getBas().isModified("oppuser")) {
//                    bopgat.getDbagrp().getBas().setOppuser(ptsptaDFF.getPts().getNam());
//                }
//                if(!bopgat.getDbdgrp().getBas().isModified("oppuser")) {
//                    bopgat.getDbdgrp().getBas().setOppuser(ptsptaDFF.getPts().getNam());
//                }
                if(!bopgat.getDbagrp().getBas().isModified("oppuser")) {
                    bopgat.getDbagrp().getBas().setOppuser("");
                }
                if(!bopgat.getDbdgrp().getBas().isModified("oppuser")) {
                    bopgat.getDbdgrp().getBas().setOppuser("");
                }
                bopgat.getDbagrp().getBas().setMethod("T");
                bopgat.getDbdgrp().getBas().setMethod("T");
            }else {
                Map<String,PtsptaVo> ptsptaVoMap = ptsptaToolComponent.getPtsptaList(ledinr,"LED");
                ptsptaAPL = ptsptaVoMap.get("APL");
                if (!MdaUtils.isEmpty(ptsptaAPL.getPts())) {
                    if(!bopgat.getDbagrp().getBas().isModified("oppuser")) {
                        bopgat.getDbagrp().getBas().setOppuser(ptsptaAPL.getPts().getNam());
                    }
                    if(!bopgat.getDbdgrp().getBas().isModified("oppuser")) {
                        bopgat.getDbdgrp().getBas().setOppuser(ptsptaAPL.getPts().getNam());
                    }
                }
                bopgat.getDbagrp().getBas().setMethod("L");
                bopgat.getDbdgrp().getBas().setMethod("L");
            }
        }
        if( MdaUtils.compareTo(objTyp,"CCD") == 0 ) {
            if(!MdaUtils.isEmpty(ptsptaCOR.getPts())){
                if(!bopgat.getDbagrp().getBas().isModified("oppuser")) {
                    bopgat.getDbagrp().getBas().setOppuser(ptsptaCOR.getPts().getNam());
                }
                if(!bopgat.getDbdgrp().getBas().isModified("oppuser")) {
                    bopgat.getDbdgrp().getBas().setOppuser(ptsptaCOR.getPts().getNam());
                }
            }
            bopgat.getDbagrp().getBas().setMethod("C");
            bopgat.getDbdgrp().getBas().setMethod("C");
        }
        //光大汇入汇款同出口收汇
        String methods = NULLSTR;
        if( MdaUtils.compareTo(objTyp,"CPD") == 0 ) {
            //对手客户名称，刘文青要求统一修改为取pts的nam
            if(!MdaUtils.isEmpty(ptsptaORC.getPts())){
                if(!bopgat.getDbagrp().getBas().isModified("oppuser")) {
                    bopgat.getDbagrp().getBas().setOppuser(ptsptaORC.getPts().getNam());
                }
                if(!bopgat.getDbdgrp().getBas().isModified("oppuser")) {
                    bopgat.getDbdgrp().getBas().setOppuser(ptsptaORC.getPts().getNam());
                }
            }
            switch( payType ) {
                case "0":
                    methods = "T";
                    break;
                case "1":
                    methods = "D";
                    break;
                case "2":
                    methods = "M";
                    break;
                default:
                    methods = "O";
            }
            bopgat.getDbagrp().getBas().setMethod(methods);
            bopgat.getDbdgrp().getBas().setMethod(methods);
        }
        if( MdaUtils.compareTo(objTyp,"GID") == 0 || MdaUtils.compareTo(objTyp,"GCD") == 0 || MdaUtils.compareTo(objTyp,"NID") == 0 || MdaUtils.compareTo(objTyp,"NCD") == 0) {
            bopgat.getDbagrp().getBas().setMethod("G");
            bopgat.getDbdgrp().getBas().setMethod("G");
            bopgat.getDbagrp().getBas().setOppuser(bopdecCommonService.getCorNM(bopgat.getPtsList(), feecorrol));
            bopgat.getDbdgrp().getBas().setOppuser(bopdecCommonService.getCorNM(bopgat.getPtsList(), feecorrol));
        }
        //cgl for szsb2012
        // 境外申报付款人名称前面增加境内境外下拉框，联动款项去向。(JW)(JN)与付款人名称分开-----边仲超
        bopgat.getDbagrp().getBas().setJwjn(bopgat.getActtyp());
        if(!bopgat.getDbagrp().getBas().isModified("oppuser")){
//            bopgat.getDbagrp().getBas().setOppuser( bopgat.getActtyp() + bopgat.getDbagrp().getBas().getOppuser());
            // 如果是对私业务，付款人名称后增加付款人账号信息，以名称后加“[acc:账号]”表示。
            //对于速汇业务和托收等没有对应账号的业务，账号部分填写“000000”（六个零）。
            if( MdaUtils.compareTo(bopgat.getDbagrp().getBas().getCustype(),"C") != 0 ) {
                if( ! MdaUtils.isEmpty(orcact) ) {
                    bopgat.getDbagrp().getBas().setOppuser( bopgat.getDbagrp().getBas().getOppuser() + "[acc:" + orcact + "]");
                }else {
                    bopgat.getDbagrp().getBas().setOppuser( bopgat.getDbagrp().getBas().getOppuser() + "[acc:000000]");
                }
            }
        }
        //当人民币账号或外汇账号或其他账号以“NRA”或“FTN”或“FTF”或“FTU”开头，
        //当附言中包含“NRA PAYMENT”或“境内非居民付款”
        //则在“付款人名称”后添加付款人账号信息，以名称后加“[acc:账号]”表示。
        //汇款交易自动获取付款人账号，托收交易账号位置补充六个零[acc:000000]，其他交易仅补充[acc:]。
        if(!bopgat.getDbdgrp().getBas().isModified("oppuser") ) {
            if( MdaUtils.compareTo(bopgat.getDbdgrp().getBas().getCustype(),"C") == 0 ) {
                if( bopdecCommonService.ischeckacc(bopgat.getCommetItems(),bopgat.getDbdgrp().getBas().getLcyacc(),bopgat.getDbdgrp().getBas().getFcyacc(),bopgat.getDbdgrp().getBas().getOthacc()) ) {
                    orcact = bopdecCommonService.getact( transName,orcact );
                    bopgat.getDbdgrp().setOldact(orcact);
//                    this.getDbdp().setOldact( orcact);
                    //若付款人名称、账号、人民币账号、外汇账号、其他账号有变化，则重置确认非居民机构账号间境内划转资金状态
                    if( MdaUtils.compareTo(oppold,bopgat.getDbdgrp().getBas().getOppuser()) != 0 || MdaUtils.compareTo(orcactold,orcact) != 0 || MdaUtils.compareTo(lcyaccold,bopgat.getDbdgrp().getBas().getLcyacc()) != 0 || MdaUtils.compareTo(fcyaccold,bopgat.getDbdgrp().getBas().getFcyacc()) != 0 || MdaUtils.compareTo(othaccold,bopgat.getDbdgrp().getBas().getOthacc()) != 0 ) {

                        bopgat.getDbdgrp().setIsyesc("X");
                    }
                }
            }
        }else {
            //若手动修改过付款人名称，需重置确认非居民机构账户境内划转资金状态
            bopgat.getDbdgrp().setIsyesc("M");
        }
        //给收入款币别赋值
        if( txamt.compareTo(BigDecimal.ZERO)>0 ) {
            if("X".equals(dfflag)){
                bopgat.getDbagrp().getBas().setTxccy(dfcur);
                bopgat.getDbdgrp().getBas().setTxccy(dfcur);
            }else {
                bopgat.getDbagrp().getBas().setTxccy(doccur);
                bopgat.getDbdgrp().getBas().setTxccy(doccur);
            }
        }else {
            bopgat.getDbagrp().getBas().setTxccy("");
            bopgat.getDbdgrp().getBas().setTxccy("");
        }
        //给地区机构号赋值
        bopgat.getDbagrp().getBas().setOwnextkey(ownextkey);
        bopgat.getDbdgrp().getBas().setOwnextkey(ownextkey);

        //给ctlmod中的dectyp赋值
        bopgat.getCtlmod().setDectyp(ctlmodService.setDectyp(transName));

        //生成rptno
        RptnoRequestVo rptnoRequestVo = new RptnoRequestVo();
        rptnoRequestVo.setOwnextkey(ownextkey);
        rptnoRequestVo.setBopobjtyp(bopgat.getBopobjtyp());
        rptnoRequestVo.setTransname(transName);
        rptnoRequestVo.setPaydat(bopgat.getPaydat());
        rptnoRequestVo.setWrkDblReldat(bopgat.getCtlmod().getWrkdbl().getReldat());
        if("1".equals(szflg) && "X".equals(basflg)){
            rptnoRequestVo.setCustype(bopgat.getDbagrp().getBas().getCustype());
            rptnoRequestVo.setRptno(bopgat.getDbagrp().getBas().getRptno());
        }
        if("2".equals(szflg) && "X".equals(basflg)){
            rptnoRequestVo.setCustype(bopgat.getDbdgrp().getBas().getCustype());
            rptnoRequestVo.setRptno(bopgat.getDbdgrp().getBas().getRptno());
        }
        String ref = bopdecCommonService.getRef(rptnoRequestVo);
        if(!MdaUtils.isEmpty(ref)){
            if("1".equals(szflg)){
                bopgat.getDbagrp().getBas().setRptno(ref);
            }
            if("2".equals(szflg)){
                bopgat.getDbdgrp().getBas().setRptno(ref);
            }
        }

        if("X".equals(dclflg)){
            if("1".equals(szflg)){
                bopgat.getDbagrp().getDcl().setRptno(bopgat.getDbagrp().getBas().getRptno());
                if("CPTADV".equals(transName)) {
                    if(!bopgat.getDbagrp().getDcl().isModified("country")) {
                        bopgat.getDbagrp().getDcl().setCountry(draweecountcode);
                    }
                }
//                String ptyInr = NULLSTR;
//                switch (transName){
//                    case "BPTOPN":
//                        ptyInr = ptsptaFIA.getPtyinr();
//                        break;
//                    case "CPTADV":
//                        ptyInr = ptsptaPYE.getPtyinr();
//                        break;
//                }
                //前面试算已经取过了客户pty 通过客户查询ptc获取申报联系人
                if(!MdaUtils.isEmpty(cusptyinr)){
                    Ptc ptc = bopdecCommonService.getPtcInfo(cusptyinr,"BOP");
                    if(!MdaUtils.isEmpty(ptc)){
                        if(!bopgat.getDbagrp().getDcl().isModified("crtuser")){
                            bopgat.getDbagrp().getDcl().setCrtuser(ptc.getNam());
                        }
                        if(!bopgat.getDbagrp().getDcl().isModified("inptelc")){
                            bopgat.getDbagrp().getDcl().setInptelc(ptc.getTelmob());
                        }
                    }
                }
                //申报日期默认取当天
                if (!bopgat.getDbagrp().getDcl().isModified("rptdate")) {
                    bopgat.getDbagrp().getDcl().setRptdate(Dates.today());
                }
            }
        }
        if("X".equals(vrfflg)){
            if("2".equals(szflg)){
                bopgat.getDbdgrp().getVrf().setRptno(bopgat.getDbagrp().getBas().getRptno());
//                String ptyInr = NULLSTR;
//                switch (transName){
//                    case "BPTOPN":
//                        ptyInr = ptsptaFIA.getPtyinr();
//                        break;
//                    case "CPTADV":
//                        ptyInr = ptsptaPYE.getPtyinr();
//                        break;
//                }
                //前面试算已经取过客户
                if(!MdaUtils.isEmpty(cusptyinr)){
                    Ptc ptc = bopdecCommonService.getPtcInfo(cusptyinr,"BOP");
                    if(!MdaUtils.isEmpty(ptc)){
                        if(!bopgat.getDbdgrp().getVrf().isModified("crtuser")){
                            bopgat.getDbdgrp().getVrf().setCrtuser(ptc.getNam());
                        }
                        if(!bopgat.getDbdgrp().getVrf().isModified("inptelc")){
                            bopgat.getDbdgrp().getVrf().setInptelc(ptc.getTelmob());
                        }
                    }
                }
                //申报日期默认取当天
                if (!bopgat.getDbdgrp().getVrf().isModified("rptdate")) {
                    bopgat.getDbdgrp().getVrf().setRptdate(Dates.today());
                }
            }
        }
        this.setCheckItems(bopgat);


        return bopgat;
    }



    private void setCheckItems(Bopgat bopgat){
        CheckItems checkItems = new CheckItems();
        String dectyp = ctlmodService.setDectyp(bopgat.getTransName());
        checkItems.setDectyp(dectyp);
        checkItems.setTransName(bopgat.getTransName());
        checkItems.setSzflg(bopgat.getSzflg());
        checkItems.setBasflg(bopgat.getBasflg());
        checkItems.setDclflg(bopgat.getDclflg());
        checkItems.setVrfflg(bopgat.getVrfflg());
        checkItems.setActtyp(bopgat.getActtyp());
        checkItems.setBopobjtyp(bopgat.getBopobjtyp());
        checkItems.setZbxmszh(bopgat.getZbxmszh());
        if("1".equals(bopgat.getSzflg())){
            checkItems.setBuscode(bopgat.getDbagrp().getBas().getBuscode());
            checkItems.setBasactiontype(bopgat.getDbagrp().getBas().getActiontype());
            checkItems.setCustcod(bopgat.getDbagrp().getBas().getCustcod());
            checkItems.setTxccy(bopgat.getDbagrp().getBas().getTxccy());
            checkItems.setExrate(bopgat.getDbagrp().getBas().getExrate());
            checkItems.setFcyacc(bopgat.getDbagrp().getBas().getFcyacc());
            checkItems.setFcyamt(bopgat.getDbagrp().getBas().getFcyamt());
            checkItems.setIdcode(bopgat.getDbagrp().getBas().getIdcode());
            checkItems.setLcyacc(bopgat.getDbagrp().getBas().getLcyacc());
            checkItems.setLcyamt(bopgat.getDbagrp().getBas().getLcyamt());
            checkItems.setOppuser(bopgat.getActtyp() + bopgat.getDbagrp().getBas().getOppuser());
            checkItems.setOthacc(bopgat.getDbagrp().getBas().getOthacc());
            checkItems.setOthamt(bopgat.getDbagrp().getBas().getOthamt());
            checkItems.setTxamt(bopgat.getDbagrp().getBas().getTxamt());
            checkItems.setOwnextkey(bopgat.getDbagrp().getBas().getOwnextkey());
            checkItems.setBasactiondesc(bopgat.getDbagrp().getBas().getActiondesc());
            checkItems.setRptno(bopgat.getDbagrp().getBas().getRptno());
            checkItems.setMethod(bopgat.getDbagrp().getBas().getMethod());
            checkItems.setCustnm(bopgat.getDbagrp().getBas().getCustnm());
            checkItems.setCustype(bopgat.getDbagrp().getBas().getCustype());
            checkItems.setInchargeccy(bopgat.getDbagrp().getBas().getInchargeccy());
            checkItems.setInchargeamt(bopgat.getDbagrp().getBas().getInchargeamt());
            checkItems.setOutchargeccy(bopgat.getDbagrp().getBas().getOutchargeccy());
            checkItems.setOutchargeamt(bopgat.getDbagrp().getBas().getOutchargeamt());
            if("X".equals(bopgat.getDclflg())){
                checkItems.setDclactiontype(bopgat.getDbagrp().getDcl().getActiontype());
                checkItems.setCountry(bopgat.getDbagrp().getDcl().getCountry());
                checkItems.setDclcrtuser(bopgat.getDbagrp().getDcl().getCrtuser());
                checkItems.setDclinptelc(bopgat.getDbagrp().getDcl().getInptelc());
                checkItems.setDclisref(bopgat.getDbagrp().getDcl().getIsref());
                checkItems.setPaytype(bopgat.getDbagrp().getDcl().getPaytype());
                checkItems.setDclrptdate(bopgat.getDbagrp().getDcl().getRptdate());
                checkItems.setTc1amt(bopgat.getDbagrp().getDcl().getTc1amt());
                checkItems.setTc2amt(bopgat.getDbagrp().getDcl().getTc2amt());
                checkItems.setTxrem(bopgat.getDbagrp().getDcl().getTxrem());
                checkItems.setTx2rem(bopgat.getDbagrp().getDcl().getTx2rem());
                checkItems.setTxcode(bopgat.getDbagrp().getDcl().getTxcode());
                checkItems.setTxcode2(bopgat.getDbagrp().getDcl().getTxcode2());
                checkItems.setDclactiondesc(bopgat.getDbagrp().getDcl().getActiondesc());
                checkItems.setPayattr(bopgat.getDbagrp().getDcl().getPayattr());
                checkItems.setBillno(bopgat.getDbagrp().getDcl().getBillno());
            }
        }
        if("2".equals(bopgat.getSzflg())){
            checkItems.setBuscode(bopgat.getDbdgrp().getBas().getBuscode());
            checkItems.setBasactiontype(bopgat.getDbdgrp().getBas().getActiontype());
            checkItems.setCustcod(bopgat.getDbdgrp().getBas().getCustcod());
            checkItems.setTxccy(bopgat.getDbdgrp().getBas().getTxccy());
            checkItems.setExrate(bopgat.getDbdgrp().getBas().getExrate());
            checkItems.setFcyacc(bopgat.getDbdgrp().getBas().getFcyacc());
            checkItems.setFcyamt(bopgat.getDbdgrp().getBas().getFcyamt());
            checkItems.setIdcode(bopgat.getDbdgrp().getBas().getIdcode());
            checkItems.setLcyacc(bopgat.getDbdgrp().getBas().getLcyacc());
            checkItems.setLcyamt(bopgat.getDbdgrp().getBas().getLcyamt());
            checkItems.setOppuser(bopgat.getDbdgrp().getBas().getOppuser());
            checkItems.setOthacc(bopgat.getDbdgrp().getBas().getOthacc());
            checkItems.setOthamt(bopgat.getDbdgrp().getBas().getOthamt());
            checkItems.setTxamt(bopgat.getDbdgrp().getBas().getTxamt());
            checkItems.setOwnextkey(bopgat.getDbdgrp().getBas().getOwnextkey());
            checkItems.setBasactiondesc(bopgat.getDbdgrp().getBas().getActiondesc());
            checkItems.setRptno(bopgat.getDbdgrp().getBas().getRptno());
            checkItems.setMethod(bopgat.getDbdgrp().getBas().getMethod());
            checkItems.setCustnm(bopgat.getDbdgrp().getBas().getCustnm());
            checkItems.setCustype(bopgat.getDbdgrp().getBas().getCustype());
            checkItems.setInchargeccy(bopgat.getDbdgrp().getBas().getInchargeccy());
            checkItems.setInchargeamt(bopgat.getDbdgrp().getBas().getInchargeamt());
            if("X".equals(bopgat.getVrfflg())){
                checkItems.setVrfactiontype(bopgat.getDbdgrp().getVrf().getActiontype());
                checkItems.setVrfcrtuser(bopgat.getDbdgrp().getVrf().getCrtuser());
                checkItems.setVrfinptelc(bopgat.getDbdgrp().getVrf().getInptelc());
                checkItems.setVrfisref(bopgat.getDbdgrp().getVrf().getIsref());
                checkItems.setPaytype(bopgat.getDbdgrp().getVrf().getPaytype());
                checkItems.setVrfrptdate(bopgat.getDbdgrp().getVrf().getRptdate());
                checkItems.setTc1amt(bopgat.getDbdgrp().getVrf().getTc1amt());
                checkItems.setTc2amt(bopgat.getDbdgrp().getVrf().getTc2amt());
                checkItems.setTxrem(bopgat.getDbdgrp().getVrf().getTxrem());
                checkItems.setTx2rem(bopgat.getDbdgrp().getVrf().getTx2rem());
                checkItems.setTxcode(bopgat.getDbdgrp().getVrf().getTxcode());
                checkItems.setTxcode2(bopgat.getDbdgrp().getVrf().getTxcode2());
                checkItems.setPayattr(bopgat.getDbdgrp().getVrf().getPayattr());
                checkItems.setRegno(bopgat.getDbdgrp().getVrf().getRegno());
            }
        }
        bopgat.setCheckItems(checkItems);
    }

    @Override
    public void saveBopgatData(Bopgat bopgat) {
        //需要从业务里去数据
        String objTyp = "";
        String hndtyp = "";
        String transName = bopgat.getTransName().toUpperCase();
        BopActionTypeVo bopActionTypeVo = new BopActionTypeVo();
        if("1".equals(bopgat.getSzflg())){
            bopActionTypeVo.setBasActionType(bopgat.getDbagrp().getBas().getActiontype());
            bopActionTypeVo.setDclActionType(bopgat.getDbagrp().getDcl().getActiontype());
        }
        if("2".equals(bopgat.getSzflg())){
            bopActionTypeVo.setBasActionType(bopgat.getDbdgrp().getBas().getActiontype());
            bopActionTypeVo.setVrfActionType(bopgat.getDbdgrp().getVrf().getActiontype());
        }
        switch (Strings.mid(transName, 1, 3)) {
            case "CPT":
                CpdBizInfoVo cpdBizInfoVo = (CpdBizInfoVo)bopgat.getRec();
                objTyp = cpdBizInfoVo.getObjtyp();
                break;
            case "BOT":
                BodBizInfoVo bodBizInfoVo = (BodBizInfoVo)bopgat.getRec();
                objTyp = bodBizInfoVo.getObjtyp();
                break;
            case "CCT":
                CcdBizInfoVo ccdBizInfoVo = (CcdBizInfoVo)bopgat.getRec();
                objTyp = ccdBizInfoVo.getObjtyp();
                break;
            case "BPT":
                BpdBizInfoVo bpdBizInfoVo = (BpdBizInfoVo)bopgat.getRec();
                objTyp = bpdBizInfoVo.getObjtyp();
                break;
            case "BET":
                BedBizInfoVo bedBizInfoVo = (BedBizInfoVo)bopgat.getRec();
                objTyp = bedBizInfoVo.getObjtyp();
                break;
            case "BTT":
                BtdBizInfoVo btdBizInfoVo = (BtdBizInfoVo)bopgat.getRec();
                objTyp = btdBizInfoVo.getObjtyp();
                break;
        }

        BopObjectType bopObjectType = bopdecCommonService.getBOPInfobyType(transName,hndtyp,bopgat.getTyp());

        String ownref = "";
        String busCode = "";
        TrnmodVo trnmodVo = transactionHelpService.getTransactionStoreSet().getTrnmodVo();
        ownref = trnmodVo.getTrn().getOwnref();
        if(MdaUtils.compareTo(bopgat.getSzflg(),"1") == 0){
            busCode = bopgat.getDbagrp().getBas().getBuscode();
            if( !MdaUtils.isEmpty(ownref) && MdaUtils.compareTo(ownref,busCode) != 0 ) {
                bopgat.getDbagrp().getBas().setBuscode(ownref);
            }

        }
        if(MdaUtils.compareTo(bopgat.getSzflg(),"2") == 0){
            busCode = bopgat.getDbdgrp().getBas().getBuscode();
            if( !MdaUtils.isEmpty(ownref) && MdaUtils.compareTo(ownref,busCode) != 0 ) {
                bopgat.getDbdgrp().getBas().setBuscode(ownref);
            }
        }

        this.saveTmpRef(bopgat);

        Dbl dbl = ctlmodService.setDblSta(transName,bopActionTypeVo,bopObjectType,bopgat.getCtlmod().getDbl(),bopgat.getCtlmod().getWrkdbl());
        bopgat.getCtlmod().setDbl(dbl);
        //拼接付款人名称
        bopgat.getDbagrp().getBas().setOppuser( bopgat.getActtyp() + bopgat.getDbagrp().getBas().getOppuser());
        ctlmodService.bopGatStoreContract(bopgat);
        String tratyp = ctlmodService.getTraTyp("GAT","","");
        bopgat.getCtlmod().getDbl().setTrdtyp(tratyp);
        bopgat.getCtlmod().getDbl().setActtyp(bopgat.getActtyp());
        ctlmodService.setBopgatDblDat(bopgat,bopObjectType.getSubtyp());
        ctlmodService.bopStoreDBL(transName,bopgat.getCtlmod(),"");
    }

    @Override
    public Bopgat bopGatSetAcct(Bopgat bopgat) {
        return this.setacc(bopgat);
    }

    @Override
    public void repairBopgatData(String trninr) {
        this.repairDbaData(trninr);
        this.repairDbdData(trninr);
    }

    private void repairDbaData(String trninr){
        if (MdaUtils.isEmpty(trninr)) {
            return;
        }
        Dbl dblObj = dblMapper.selectByOBjtypAndTrninr("DBA",trninr);
        if (!MdaUtils.isEmpty(dblObj)) {
            String objInr = dblObj.getObjinr();
            if(!MdaUtils.isEmpty(objInr)) {
                Dba dbaCondition = new Dba();
                dbaCondition.setInr(objInr);
                Dba dbaObj = dbaMapper.selectByPrimaryKey(dbaCondition);
                if(!MdaUtils.isEmpty(dbaObj)){
                    sysmodService.sdbDeleteWithLog(dbaObj);
                }
                Dbg dbgObj = dbgMapper.selectByTmpref(objInr);
                if(!MdaUtils.isEmpty(dbgObj)){
                    sysmodService.sdbDeleteWithLog(dbgObj);
                }
            }
            sysmodService.sdbDeleteWithLog(dblObj);
        }
    }

    private void repairDbdData(String trninr){
        if (MdaUtils.isEmpty(trninr)) {
            return;
        }
        Dbl dblObj = dblMapper.selectByOBjtypAndTrninr("DBD",trninr);
        if (!MdaUtils.isEmpty(dblObj)) {
            String objInr = dblObj.getObjinr();
            if(!MdaUtils.isEmpty(objInr)) {
                Dbd dbdCondition = new Dbd();
                dbdCondition.setInr(objInr);
                Dbd dbdObj = dbdMapper.selectByPrimaryKey(dbdCondition);
                if(!MdaUtils.isEmpty(dbdObj)){
                    sysmodService.sdbDeleteWithLog(dbdObj);
                }
                Dbr dbrObj = dbrMapper.selectByTmpref(objInr);
                if(!MdaUtils.isEmpty(dbrObj)){
                    sysmodService.sdbDeleteWithLog(dbrObj);
                }
            }
            sysmodService.sdbDeleteWithLog(dblObj);
        }
    }

    @Override
    public void saveSpt(Bopgat bopgat) {
        //通过境内境外判断交易码
        if("1".equals(bopgat.getSzflg())){
            BopUtils.saveSpt("DBAAME");
        }else{
            BopUtils.saveSpt("DBDAME");
        }
    }


    public Bopgat setacc(Bopgat bopgat){
        String orcact = NULLSTR;
        String transName = bopgat.getTransName().toUpperCase();
        switch (Strings.mid(transName, 1, 3)) {
            case "CPT":
                CpdBizInfoVo cpdBizInfoVo = (CpdBizInfoVo) bopgat.getRec();
                orcact = cpdBizInfoVo.getOrcact();
                break;
        }
        if( MdaUtils.compareTo(bopgat.getDbdgrp().getIsyesc(),"Y") == 0 ) {
            orcact = bopdecCommonService.getact(transName, orcact);
            int num = Strings.pos(bopgat.getDbdgrp().getBas().getOppuser(), "[acc:");
            int num1 = Strings.pos(bopgat.getDbdgrp().getBas().getOppuser(), "]");
            if (num == 0 || num1 == 0) {
                bopgat.getDbdgrp().getBas().setOppuser(bopgat.getDbdgrp().getBas().getOppuser() + "[acc:" + orcact + "]");
            }
        }
        return bopgat;
    }

    /**
     * 数据提取交易存盘时自动生成临时临时参考号
     * @param bopgat
     * @return
     */
    public void saveTmpRef(Bopgat bopgat){
        String temRef = NULLSTR;
        String rptno = NULLSTR;
        String szflg = bopgat.getSzflg();
        String transName = bopgat.getTransName().toUpperCase();
        if(MdaUtils.compareTo(bopgat.getSzflg(),"1") == 0){
            temRef = bopgat.getDbagrp().getBas().getTmpref();
        }
        if(MdaUtils.compareTo(bopgat.getSzflg(),"2") == 0){
            temRef = bopgat.getDbdgrp().getBas().getTmpref();
        }
        if(MdaUtils.isEmpty(temRef)){
            temRef = bopdecCommonService.getBopTmpRef();
            if(MdaUtils.compareTo(bopgat.getSzflg(),"1") == 0) {
                bopgat.getDbagrp().getBas().setTmpref(temRef);
                //此处不进行赋值，存盘时管理和申报信息中 tmpref 赋值 基础信息inr
                //bopgat.getDbagrp().getDcl().setTmpref(temRef);
            }
            if(MdaUtils.compareTo(bopgat.getSzflg(),"2") == 0){
                bopgat.getDbdgrp().getBas().setTmpref(temRef);
                //此处不进行赋值，存盘时管理和申报信息中 tmpref 赋值 基础信息inr
                //bopgat.getDbdgrp().getVrf().setTmpref(temRef);
            }
        }
        if(MdaUtils.compareTo(bopgat.getSzflg(),"3") != 0 && MdaUtils.compareTo(bopgat.getSzflg(),"") != 0 && (MdaUtils.compareTo(bopgat.getCtlmod().getDectyp(),"V") == 0 || MdaUtils.compareTo(bopgat.getCtlmod().getDectyp(),"A") == 0)){
            if("1".equals(bopgat.getSzflg())){
                rptno = bopgat.getDbagrp().getBas().getRptno();
            }
            if("2".equals(bopgat.getSzflg())){
                rptno = bopgat.getDbdgrp().getBas().getRptno();
            }
            if(MdaUtils.isEmpty(rptno)) {
                RptnoRequestVo rptnoRequestVo = new RptnoRequestVo();
                rptnoRequestVo.setTransname(transName);
                rptnoRequestVo.setSzflg(bopgat.getSzflg());
                rptnoRequestVo.setBasflg(bopgat.getBasflg());
                rptnoRequestVo.setDclflg(bopgat.getDclflg());
                rptnoRequestVo.setVrfflg(bopgat.getVrfflg());
                rptnoRequestVo.setBopobjtyp(bopgat.getBopobjtyp());
                rptnoRequestVo.setBoptyp(bopgat.getBoptyp());
                rptnoRequestVo.setTyp(bopgat.getTyp());
                rptnoRequestVo.setOwnextkey(bopgat.getOwnextkey());
                String dectyp = ctlmodService.setDectyp(transName);
                if (MdaUtils.isEmpty(bopgat.getPaydat())) {
                    if ("V".equals(dectyp)) {
                        rptnoRequestVo.setPaydat(bopdecCommonService.getCoreSysDay());
                    }
                } else {
                    rptnoRequestVo.setPaydat(bopgat.getPaydat());
                }
                if("1".equals(szflg)) {
                    rptnoRequestVo.setCustype(bopgat.getDbagrp().getBas().getCustype());
                }
                if("2".equals(szflg)) {
                    rptnoRequestVo.setCustype(bopgat.getDbdgrp().getBas().getCustype());
                }
                String ref = bopdecCommonService.getRef(rptnoRequestVo);
                bopgat.getDbagrp().getBas().setRptno(ref);
                bopgat.getDbdgrp().getBas().setRptno(ref);
                if("X".equals(bopgat.getDclflg())){
                    bopgat.getDbagrp().getDcl().setRptno(ref);
                }
                if("X".equals(bopgat.getVrfflg())){
                    bopgat.getDbdgrp().getVrf().setRptno(ref);
                }
            }
        }
    }
}
