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

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.Dbl;
import com.brilliance.isc.bo.bop.jsh.Dbjgrp;
import com.brilliance.isc.bo.bop.jsh.Dbwgrp;
import com.brilliance.isc.bo.bop.jsh.IJshBasData;
import com.brilliance.isc.bo.bopdecbo.*;
import com.brilliance.isc.bo.model.*;
import com.brilliance.isc.common.bopset.service.BopdecCommonService;
import com.brilliance.isc.common.bopset.service.CtlmodService;
import com.brilliance.isc.common.component.DyncDaoService;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.jshmod.common.Ctlset;
import com.brilliance.isc.common.jshmod.common.JshObjType;
import com.brilliance.isc.common.jshmod.vo.Jshmod;
import com.brilliance.isc.common.sysmod.SysmodService;
import com.brilliance.isc.common.transaction.help.TransactionHelpService;
import com.brilliance.isc.mda.dao.*;
import com.brilliance.isc.vo.bop.BopActionTypeVo;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Optional;

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

@Service
public class CtlmodServiceImpl implements CtlmodService {

    @Autowired
    private DbaMapper dbaMapper;

    @Autowired
    private DbdMapper dbdMapper;

    @Autowired
    private DbgMapper dbgMapper;

    @Autowired
    private DbrMapper dbrMapper;

    @Autowired
    private DblMapper dblMapper;

    @Autowired
    private DbbMapper dbbMapper;

    @Autowired
    private DbhMapper dbhMapper;

    @Autowired
    private DbnMapper dbnMapper;

    @Autowired
    private DbeMapper dbeMapper;

    @Autowired
    private DbqMapper dbqMapper;

    @Autowired
    private DbcMapper dbcMapper;

    @Autowired
    private DbfMapper dbfMapper;

    @Autowired
    private DbkMapper dbkMapper;

    @Autowired
    private DbpMapper dbpMapper;

    @Autowired
    private DbsMapper dbsMapper;

    @Autowired
    private DyncDaoService dyncDaoService;

    @Autowired
    private SysmodService sysmodService;

    @Autowired
    private BopdecCommonService bopdecCommonService;
    @Autowired
    private TransactionHelpService transactionHelpService;
    @Autowired
    private RedissonClient redissonClient;

    @Override
    public void bopGatStoreContract(Bopgat bopgat) {
        String objInr = "";
        String objFlg = "";
        boolean basFlg = false;

        if (MdaUtils.compareTo(bopgat.getSzflg(), "1") == 0) {
            objInr = bopgat.getDbagrp().getBas().getInr();
            if (!MdaUtils.isEmpty(objInr)) {
                Dba dbaInfo = new Dba();
                dbaInfo.setInr(objInr);
                dbaInfo = dbaMapper.selectByPrimaryKey(dbaInfo);
                if (!MdaUtils.isEmpty(dbaInfo)) {
                    objFlg = "F";
                }
            }
        }
        if (MdaUtils.compareTo(bopgat.getSzflg(), "2") == 0) {
            objInr = bopgat.getDbdgrp().getBas().getInr();
            if (!MdaUtils.isEmpty(objInr)) {
                Dbd dbdInfo = new Dbd();
                dbdInfo.setInr(objInr);
                dbdInfo = dbdMapper.selectByPrimaryKey(dbdInfo);
                if (!MdaUtils.isEmpty(dbdInfo)) {
                    objFlg = "F";
                }
            }
        }

        boolean dclsta = false;
        boolean vrfsta = false;
        if (!this.isNo(bopgat.getCtlmod().getDbl().getDclsta())) {
            dclsta = true;
        }
        if (!this.isNo(bopgat.getCtlmod().getDbl().getVrfsta())) {
            vrfsta = true;
        }

        //!3. 根据DBL中的STA存储DCL/VRF数据
        Dbg objDCLMod = new Dbg();
        Dbr objVRFMod = new Dbr();

        if (MdaUtils.isEmpty(objFlg)) {
            if (MdaUtils.isEmpty(objInr)) {
                if (MdaUtils.compareTo(bopgat.getSzflg(), "1") == 0) {
                    sysmodService.sdbInsertWithLog(bopgat.getDbagrp().getBas());
                }
                if (MdaUtils.compareTo(bopgat.getSzflg(), "2") == 0) {
                    sysmodService.sdbInsertWithLog(bopgat.getDbdgrp().getBas());
                }

            } else {
                if (MdaUtils.compareTo(bopgat.getSzflg(), "1") == 0) {
                    sysmodService.sdbUndeleteWithLog(bopgat.getDbagrp().getBas());
                }
                if (MdaUtils.compareTo(bopgat.getSzflg(), "2") == 0) {
                    sysmodService.sdbUndeleteWithLog(bopgat.getDbdgrp().getBas());
                }
            }

        } else {
            if (MdaUtils.compareTo(bopgat.getSzflg(), "1") == 0) {
                sysmodService.sdbUpdateWithLog(bopgat.getDbagrp().getBas());
            }
            if (MdaUtils.compareTo(bopgat.getSzflg(), "2") == 0) {
                sysmodService.sdbUpdateWithLog(bopgat.getDbdgrp().getBas());
            }
        }
        if (dclsta) {
            //增加 tmpref 关联 ，适配子系统维护交易
            bopgat.getDbagrp().getDcl().setTmpref(bopgat.getDbagrp().getBas().getInr());
            //原逻辑不太合理，用基础信息inr 查询 申报信息，改为直接判断申报信息inr
            if(!MdaUtils.isEmpty(bopgat.getDbagrp().getDcl().getInr())){
                objDCLMod.setInr(bopgat.getDbagrp().getDcl().getInr());
                objDCLMod = dbgMapper.selectByPrimaryKey(objDCLMod);
                if (MdaUtils.isEmpty(objDCLMod)) {
                    sysmodService.sdbInsertWithLog(bopgat.getDbagrp().getDcl());
                } else {
                    sysmodService.sdbUpdateWithLog(bopgat.getDbagrp().getDcl());
                }
            }else{
                sysmodService.sdbInsertWithLog(bopgat.getDbagrp().getDcl());
            }

        }
        if (vrfsta) {
            //增加 tmpref 关联 ，适配子系统维护交易
            bopgat.getDbdgrp().getVrf().setTmpref(bopgat.getDbdgrp().getBas().getInr());
            //原逻辑不太合理，用基础信息inr 查询 申报信息，改为直接判断申报信息inr
            if(!MdaUtils.isEmpty(bopgat.getDbdgrp().getVrf().getInr())){
                objVRFMod.setInr(bopgat.getDbdgrp().getVrf().getInr());
                objVRFMod = dbrMapper.selectByPrimaryKey(objVRFMod);
                if (MdaUtils.isEmpty(objVRFMod)) {
                    sysmodService.sdbInsertWithLog(bopgat.getDbdgrp().getVrf());
                } else {
                    sysmodService.sdbUpdateWithLog(bopgat.getDbdgrp().getVrf());
                }
            }else{
                sysmodService.sdbInsertWithLog(bopgat.getDbdgrp().getVrf());
            }

        }

    }

    @Override
    public void bopRemStoreContract(Boprem boprem) {
        String objInr = "";
        String objFlg = "";
        boolean basFlg = false;

        if (MdaUtils.compareTo(boprem.getSzflg(), "1") == 0) {
            objInr = boprem.getDbbgrp().getBas().getInr();
            if (!MdaUtils.isEmpty(objInr)) {
                Dbb dbbInfo = new Dbb();
                dbbInfo.setInr(objInr);
                dbbInfo = dbbMapper.selectByPrimaryKey(dbbInfo);
                if (!MdaUtils.isEmpty(dbbInfo)) {
                    objFlg = "F";
                }
            }
        }
        if (MdaUtils.compareTo(boprem.getSzflg(), "2") == 0) {
            objInr = boprem.getDbegrp().getBas().getInr();
            if (!MdaUtils.isEmpty(objInr)) {
                Dbe dbeInfo = new Dbe();
                dbeInfo.setInr(objInr);
                dbeInfo = dbeMapper.selectByPrimaryKey(dbeInfo);
                if (!MdaUtils.isEmpty(dbeInfo)) {
                    objFlg = "F";
                }
            }
        }

        boolean dclsta = false;
        boolean vrfsta = false;
        if (!this.isNo(boprem.getCtlmod().getDbl().getDclsta())) {
            dclsta = true;
        }
        if (!this.isNo(boprem.getCtlmod().getDbl().getVrfsta())) {
            vrfsta = true;
        }

        //!3. 根据DBL中的STA存储DCL/VRF数据
        Dbh objDCLMod = new Dbh();
        Dbn objVRFMod = new Dbn();
        Dbq objVRFMod1 = new Dbq();

        if (MdaUtils.isEmpty(objFlg)) {
            if (MdaUtils.isEmpty(objInr)) {
                if (MdaUtils.compareTo(boprem.getSzflg(), "1") == 0) {
                    sysmodService.sdbInsertWithLog(boprem.getDbbgrp().getBas());
                }
                if (MdaUtils.compareTo(boprem.getSzflg(), "2") == 0) {
                    sysmodService.sdbInsertWithLog(boprem.getDbegrp().getBas());
                }

            } else {
                if (MdaUtils.compareTo(boprem.getSzflg(), "1") == 0) {
                    sysmodService.sdbUndeleteWithLog(boprem.getDbbgrp().getBas());
                }
                if (MdaUtils.compareTo(boprem.getSzflg(), "2") == 0) {
                    sysmodService.sdbUndeleteWithLog(boprem.getDbegrp().getBas());
                }
            }

        } else {
            if (MdaUtils.compareTo(boprem.getSzflg(), "1") == 0) {
                sysmodService.sdbUpdateWithLog(boprem.getDbbgrp().getBas());
            }
            if (MdaUtils.compareTo(boprem.getSzflg(), "2") == 0) {
                sysmodService.sdbUpdateWithLog(boprem.getDbegrp().getBas());
            }
        }
        if (dclsta) {
            //增加 tmpref 关联 ，适配子系统维护交易
            boprem.getDbbgrp().getDcl().setTmpref(boprem.getDbbgrp().getBas().getInr());
            //改为判断 申报信息inr
            if(!MdaUtils.isEmpty(boprem.getDbbgrp().getDcl().getInr())){
                objDCLMod.setInr(boprem.getDbbgrp().getDcl().getInr());
                objDCLMod = dbhMapper.selectByPrimaryKey(objDCLMod);
                if (MdaUtils.isEmpty(objDCLMod)) {
                    sysmodService.sdbInsertWithLog(boprem.getDbbgrp().getDcl());
                } else {
                    sysmodService.sdbUpdateWithLog(boprem.getDbbgrp().getDcl());
                }
            }else{
                sysmodService.sdbInsertWithLog(boprem.getDbbgrp().getDcl());
            }

        }
        if (vrfsta) {
            if (MdaUtils.compareTo(boprem.getSzflg(), "1") == 0) {
                //增加 tmpref 关联 ，适配子系统维护交易
                boprem.getDbbgrp().getVrf().setTmpref(boprem.getDbbgrp().getBas().getInr());
                //改为判断 管理信息inr
                if(!MdaUtils.isEmpty(boprem.getDbbgrp().getVrf().getInr())){
                    objVRFMod.setInr(boprem.getDbbgrp().getVrf().getInr());
                    objVRFMod = dbnMapper.selectByPrimaryKey(objVRFMod);
                    if (MdaUtils.isEmpty(objVRFMod)) {
                        sysmodService.sdbInsertWithLog(boprem.getDbbgrp().getVrf());
                    } else {
                        sysmodService.sdbUpdateWithLog(boprem.getDbbgrp().getVrf());
                    }
                }else {
                    sysmodService.sdbInsertWithLog(boprem.getDbbgrp().getVrf());
                }
            }
            if (MdaUtils.compareTo(boprem.getSzflg(), "2") == 0) {
                //增加 tmpref 关联 ，适配子系统维护交易
                boprem.getDbegrp().getVrf().setTmpref(boprem.getDbegrp().getBas().getInr());
                //改为判断 管理信息inr
                if(!MdaUtils.isEmpty(boprem.getDbegrp().getVrf().getInr())){
                    objVRFMod1.setInr(boprem.getDbegrp().getVrf().getInr());
                    objVRFMod1 = dbqMapper.selectByPrimaryKey(objVRFMod1);
                    if (MdaUtils.isEmpty(objVRFMod1)) {
                        sysmodService.sdbInsertWithLog(boprem.getDbegrp().getVrf());
                    } else {
                        sysmodService.sdbUpdateWithLog(boprem.getDbegrp().getVrf());
                    }
                }else{
                    sysmodService.sdbInsertWithLog(boprem.getDbegrp().getVrf());
                }

            }
        }
    }

    @Override
    public void bopPayStoreContract(Boppay boppay) {
        String objInr = "";
        String objFlg = "";
        boolean basFlg = false;

        if (MdaUtils.compareTo(boppay.getSzflg(), "1") == 0) {
            objInr = boppay.getDbcgrp().getBas().getInr();
            if (!MdaUtils.isEmpty(objInr)) {
                Dbc dbcInfo = new Dbc();
                dbcInfo.setInr(objInr);
                dbcInfo = dbcMapper.selectByPrimaryKey(dbcInfo);
                if (!MdaUtils.isEmpty(dbcInfo)) {
                    objFlg = "F";
                }
            }
        }
        if (MdaUtils.compareTo(boppay.getSzflg(), "2") == 0) {
            objInr = boppay.getDbfgrp().getBas().getInr();
            if (!MdaUtils.isEmpty(objInr)) {
                Dbf dbfInfo = new Dbf();
                dbfInfo.setInr(objInr);
                dbfInfo = dbfMapper.selectByPrimaryKey(dbfInfo);
                if (!MdaUtils.isEmpty(dbfInfo)) {
                    objFlg = "F";
                }
            }
        }

        boolean dclsta = false;
        boolean vrfsta = false;
        if (!this.isNo(boppay.getCtlmod().getDbl().getDclsta())) {
            dclsta = true;
        }
        if (!this.isNo(boppay.getCtlmod().getDbl().getVrfsta())) {
            vrfsta = true;
        }

        //!3. 根据DBL中的STA存储DCL/VRF数据
        Dbk objDCLMod = new Dbk();
        Dbp objVRFMod = new Dbp();
        Dbs objVRFMod1 = new Dbs();

        if (MdaUtils.isEmpty(objFlg)) {
            if (MdaUtils.isEmpty(objInr)) {
                if (MdaUtils.compareTo(boppay.getSzflg(), "1") == 0) {
                    sysmodService.sdbInsertWithLog(boppay.getDbcgrp().getBas());
                }
                if (MdaUtils.compareTo(boppay.getSzflg(), "2") == 0) {
                    sysmodService.sdbInsertWithLog(boppay.getDbfgrp().getBas());
                }

            } else {
                if (MdaUtils.compareTo(boppay.getSzflg(), "1") == 0) {
                    sysmodService.sdbUndeleteWithLog(boppay.getDbcgrp().getBas());
                }
                if (MdaUtils.compareTo(boppay.getSzflg(), "2") == 0) {
                    sysmodService.sdbUndeleteWithLog(boppay.getDbfgrp().getBas());
                }
            }
        } else {
            if (MdaUtils.compareTo(boppay.getSzflg(), "1") == 0) {
                sysmodService.sdbUpdateWithLog(boppay.getDbcgrp().getBas());
            }
            if (MdaUtils.compareTo(boppay.getSzflg(), "2") == 0) {
                sysmodService.sdbUpdateWithLog(boppay.getDbfgrp().getBas());
            }
        }
        if (dclsta) {
            //增加 tmpref 关联 ，适配子系统维护交易
            boppay.getDbcgrp().getDcl().setTmpref(boppay.getDbcgrp().getBas().getInr());
            //原逻辑不太合理，用基础信息inr 查询 申报信息，改为直接判断申报信息inr
            if(!MdaUtils.isEmpty(boppay.getDbcgrp().getDcl().getInr())){
                objDCLMod.setInr(boppay.getDbcgrp().getDcl().getInr());
                objDCLMod = dbkMapper.selectByPrimaryKey(objDCLMod);
                if (MdaUtils.isEmpty(objDCLMod)) {
                    sysmodService.sdbInsertWithLog(boppay.getDbcgrp().getDcl());
                } else {
                    sysmodService.sdbUpdateWithLog(boppay.getDbcgrp().getDcl());
                }
            }else{
                sysmodService.sdbInsertWithLog(boppay.getDbcgrp().getDcl());
            }
        }
        if (vrfsta) {
            if (MdaUtils.compareTo(boppay.getSzflg(), "1") == 0) {
                //增加 tmpref 关联 ，适配子系统维护交易
                boppay.getDbcgrp().getVrf().setTmpref(boppay.getDbcgrp().getBas().getInr());
                //判断管理信息inr
                if(!MdaUtils.isEmpty(boppay.getDbcgrp().getVrf().getInr())){
                    objVRFMod.setInr(boppay.getDbcgrp().getVrf().getInr());
                    objVRFMod = dbpMapper.selectByPrimaryKey(objVRFMod);
                    if (MdaUtils.isEmpty(objVRFMod)) {
                        sysmodService.sdbInsertWithLog(boppay.getDbcgrp().getVrf());
                    } else {
                        sysmodService.sdbUpdateWithLog(boppay.getDbcgrp().getVrf());
                    }
                }else{
                    sysmodService.sdbInsertWithLog(boppay.getDbcgrp().getVrf());
                }

            }
            if (MdaUtils.compareTo(boppay.getSzflg(), "2") == 0) {
                //增加 tmpref 关联 ，适配子系统维护交易
                boppay.getDbfgrp().getVrf().setTmpref(boppay.getDbfgrp().getBas().getInr());
                //判断管理信息inr
                if(!MdaUtils.isEmpty(boppay.getDbfgrp().getVrf().getInr())){
                    objVRFMod1.setInr(boppay.getDbfgrp().getVrf().getInr());
                    objVRFMod1 = dbsMapper.selectByPrimaryKey(objVRFMod1);
                    if (MdaUtils.isEmpty(objVRFMod1)) {
                        sysmodService.sdbInsertWithLog(boppay.getDbfgrp().getVrf());
                    } else {
                        sysmodService.sdbUpdateWithLog(boppay.getDbfgrp().getVrf());
                    }
                }else{
                    sysmodService.sdbInsertWithLog(boppay.getDbfgrp().getVrf());
                }
            }
        }
    }

    //!得到贸易类型
    //!===================================
    //!返回值: 无
    //!变量:   typ     申报类型，包括:"PAY"/"GAT"/"REM"/"GUA"
    //!        trntyp  交易类型，LI:信用证 BO:托收 BC:代收 CQ:支票代收 GC:保函 CP:汇款
    //!        para    贸易类型,
    //!        tratyp  贸易类型，1:贸易 2:非贸易 3:资本 4:其他
    //!===================================
    @Override
    public String getTraTyp(String typ, String trntyp, String para) {
        String tratyp = "";
        if (MdaUtils.compareTo(typ, "PAY") == 0) {
            if (MdaUtils.compareTo(trntyp, "LI") == 0 || MdaUtils.compareTo(trntyp, "BC") == 0) {
                tratyp = "1";
            } else {
                if (MdaUtils.compareTo(trntyp, "CQ") == 0) {
                    if (MdaUtils.compareTo(para, "T") == 0) {
                        tratyp = "1";
                    }
                    if (MdaUtils.compareTo(para, "N") == 0) {
                        tratyp = "2";
                    }
                    if (MdaUtils.compareTo(para, "C") == 0) {
                        tratyp = "3";
                    }
                }
            }
        }
        if (MdaUtils.compareTo(typ, "GAT") == 0) {
            tratyp = "1";
        }
        if (MdaUtils.compareTo(typ, "GUA") == 0) {
            if (MdaUtils.compareTo(para, "M") == 0) {
                tratyp = "1";
            }
            if (MdaUtils.compareTo(para, "F") == 0) {
                tratyp = "2";
            }
            if (MdaUtils.compareTo(para, "Z") == 0) {
                tratyp = "3";
            }
        }
        if (MdaUtils.compareTo(typ, "REM") == 0) {
            if (MdaUtils.compareTo(para, "T") == 0) {
                tratyp = "1";
            }
            if (MdaUtils.compareTo(para, "NT") == 0) {
                tratyp = "2";
            }
            if (MdaUtils.compareTo(para, "C") == 0) {
                tratyp = "3";
            }
        }
        return tratyp;
    }

    @Override
    public void setBopgatDblDat(Bopgat bopgat, String objtyp) {
        if (!MdaUtils.isEmpty(transactionHelpService.getTrninr())) {
            bopgat.getCtlmod().getDbl().setTrninr(transactionHelpService.getTrninr());
        }
//        String trnInr = bopgat.getCtlmod().getWrktrn().getInr();
//        bopgat.getCtlmod().getDbl().setTrninr(trnInr);
        bopgat.getCtlmod().getDbl().setObjtyp(objtyp);
        bopgat.getCtlmod().getDbl().setCurflg("*");
        bopgat.getCtlmod().getDbl().setSndflg("C");
        bopgat.getCtlmod().getDbl().setDatsrc("G");
        //补录状态赋值  只有不随业务申报才需要补录
        if("2".equals(bopgat.getAloneflg()) && "Y".equals(bopgat.getEnableflg())){
            bopgat.getCtlmod().getDbl().setAddsta("1");
        }
        if (MdaUtils.compareTo(bopgat.getSzflg(), "1") == 0) {
            bopgat.getCtlmod().getDbl().setObjinr(bopgat.getDbagrp().getBas().getInr());
            bopgat.getCtlmod().getDbl().setRptno(bopgat.getDbagrp().getBas().getRptno());
            bopgat.getCtlmod().getDbl().setTmpref(bopgat.getDbagrp().getBas().getTmpref());
            bopgat.getCtlmod().getDbl().setOwnextkey(bopgat.getDbagrp().getBas().getOwnextkey());
        }
        if (MdaUtils.compareTo(bopgat.getSzflg(), "2") == 0) {
            bopgat.getCtlmod().getDbl().setCodext(bopgat.getBoptxcode());
            bopgat.getCtlmod().getDbl().setObjinr(bopgat.getDbdgrp().getBas().getInr());
            bopgat.getCtlmod().getDbl().setRptno(bopgat.getDbdgrp().getBas().getRptno());
            bopgat.getCtlmod().getDbl().setTmpref(bopgat.getDbdgrp().getBas().getTmpref());
            bopgat.getCtlmod().getDbl().setOwnextkey(bopgat.getDbdgrp().getBas().getOwnextkey());
        }
        bopgat.getCtlmod().getDbl().setOwnusr(SettleContext.getUserSession().getUserVo().getEtyextkey());
        bopgat.getCtlmod().getDbl().setCredat(bopdecCommonService.getCoreSysDay());
        bopgat.getCtlmod().getDbl().setSfhdat(bopdecCommonService.getCoreSysDay());

        //在提取交易中生成申报号码时，要同时设日期定入帐
        Date relDAT = bopdecCommonService.getCoreSysDay();
        if (MdaUtils.compareTo(bopgat.getCtlmod().getDectyp(), "V") == 0) {
            bopgat.getCtlmod().getDbl().setReldat(relDAT);
        } else {
            //  if \\CTLMOD\\DECTYP = "A" and DBL\\RELDAT = NULLDATE and PAYDAT <> NULLDATE then
            if (MdaUtils.compareTo(bopgat.getCtlmod().getDectyp(), "A") == 0 && bopgat.getCtlmod().getPaydat() != NULLDATE) {
                bopgat.getCtlmod().getDbl().setReldat(bopgat.getCtlmod().getPaydat());
            }
        }
    }

    @Override
    public void setBopremDblDat(Boprem boprem, String objtyp) {
        if (!MdaUtils.isEmpty(transactionHelpService.getTrninr())) {
            boprem.getCtlmod().getDbl().setTrninr(transactionHelpService.getTrninr());
        }
//        String trnInr = boprem.getCtlmod().getWrktrn().getInr();
//        boprem.getCtlmod().getDbl().setTrninr(trnInr);
        boprem.getCtlmod().getDbl().setObjtyp(objtyp);
        boprem.getCtlmod().getDbl().setCurflg("*");
        boprem.getCtlmod().getDbl().setSndflg("C");
        boprem.getCtlmod().getDbl().setDatsrc("G");
        //补录状态赋值  只有不随业务申报才需要补录
        if("2".equals(boprem.getAloneflg()) && "Y".equals(boprem.getEnableflg())){
            boprem.getCtlmod().getDbl().setAddsta("1");
        }
        if (MdaUtils.compareTo(boprem.getSzflg(), "1") == 0) {
            boprem.getCtlmod().getDbl().setObjinr(boprem.getDbbgrp().getBas().getInr());
            boprem.getCtlmod().getDbl().setRptno(boprem.getDbbgrp().getBas().getRptno());
            boprem.getCtlmod().getDbl().setTmpref(boprem.getDbbgrp().getBas().getTmpref());
            boprem.getCtlmod().getDbl().setOwnextkey(boprem.getDbbgrp().getBas().getOwnextkey());
        }
        if (MdaUtils.compareTo(boprem.getSzflg(), "2") == 0) {
            boprem.getCtlmod().getDbl().setCodext(boprem.getBoptxcode());
            boprem.getCtlmod().getDbl().setObjinr(boprem.getDbegrp().getBas().getInr());
            boprem.getCtlmod().getDbl().setRptno(boprem.getDbegrp().getBas().getRptno());
            boprem.getCtlmod().getDbl().setTmpref(boprem.getDbegrp().getBas().getTmpref());
            boprem.getCtlmod().getDbl().setOwnextkey(boprem.getDbegrp().getBas().getOwnextkey());
        }
        boprem.getCtlmod().getDbl().setOwnusr(SettleContext.getUserSession().getUserVo().getEtyextkey());
        boprem.getCtlmod().getDbl().setCredat(bopdecCommonService.getCoreSysDay());
        boprem.getCtlmod().getDbl().setSfhdat(bopdecCommonService.getCoreSysDay());


        //在提取交易中生成申报号码时，要同时设日期定入帐
        Date relDAT = bopdecCommonService.getCoreSysDay();
        if (MdaUtils.compareTo(boprem.getCtlmod().getDectyp(), "V") == 0) {
            boprem.getCtlmod().getDbl().setReldat(relDAT);
        } else {
            //  if \\CTLMOD\\DECTYP = "A" and DBL\\RELDAT = NULLDATE and PAYDAT <> NULLDATE then
            if (MdaUtils.compareTo(boprem.getCtlmod().getDectyp(), "A") == 0 && boprem.getCtlmod().getPaydat() != NULLDATE) {
                boprem.getCtlmod().getDbl().setReldat(boprem.getCtlmod().getPaydat());
            }
        }

    }

    @Override
    public void setBopPayDblDat(Boppay boppay, String objtyp) {
        if (!MdaUtils.isEmpty(transactionHelpService.getTrninr())) {
            boppay.getCtlmod().getDbl().setTrninr(transactionHelpService.getTrninr());
        }
//        String trnInr = boppay.getCtlmod().getWrktrn().getInr();
//        boppay.getCtlmod().getDbl().setTrninr(trnInr);
        boppay.getCtlmod().getDbl().setObjtyp(objtyp);
        boppay.getCtlmod().getDbl().setCurflg("*");
        boppay.getCtlmod().getDbl().setSndflg("C");
        boppay.getCtlmod().getDbl().setDatsrc("G");
        //补录状态赋值  只有不随业务申报且试点行才需要补录
        if("2".equals(boppay.getAloneflg()) && "Y".equals(boppay.getEnableflg())){
            boppay.getCtlmod().getDbl().setAddsta("1");
        }
        if (MdaUtils.compareTo(boppay.getSzflg(), "1") == 0) {
            boppay.getCtlmod().getDbl().setObjinr(boppay.getDbcgrp().getBas().getInr());
            boppay.getCtlmod().getDbl().setRptno(boppay.getDbcgrp().getBas().getRptno());
            boppay.getCtlmod().getDbl().setTmpref(boppay.getDbcgrp().getBas().getTmpref());
            boppay.getCtlmod().getDbl().setOwnextkey(boppay.getDbcgrp().getBas().getOwnextkey());
        }
        if (MdaUtils.compareTo(boppay.getSzflg(), "2") == 0) {
            boppay.getCtlmod().getDbl().setCodext(boppay.getBoptxcode());
            boppay.getCtlmod().getDbl().setObjinr(boppay.getDbfgrp().getBas().getInr());
            boppay.getCtlmod().getDbl().setRptno(boppay.getDbfgrp().getBas().getRptno());
            boppay.getCtlmod().getDbl().setTmpref(boppay.getDbfgrp().getBas().getTmpref());
            boppay.getCtlmod().getDbl().setOwnextkey(boppay.getDbfgrp().getBas().getOwnextkey());
        }
        boppay.getCtlmod().getDbl().setOwnusr(SettleContext.getUserSession().getUserVo().getEtyextkey());
        boppay.getCtlmod().getDbl().setCredat(bopdecCommonService.getCoreSysDay());
        boppay.getCtlmod().getDbl().setSfhdat(bopdecCommonService.getCoreSysDay());


        //在提取交易中生成申报号码时，要同时设日期定入帐
        Date relDAT = bopdecCommonService.getCoreSysDay();
        if (MdaUtils.compareTo(boppay.getCtlmod().getDectyp(), "V") == 0) {
            boppay.getCtlmod().getDbl().setReldat(relDAT);
        } else {
            //  if \\CTLMOD\\DECTYP = "A" and DBL\\RELDAT = NULLDATE and PAYDAT <> NULLDATE then
            if (MdaUtils.compareTo(boppay.getCtlmod().getDectyp(), "A") == 0 && boppay.getCtlmod().getPaydat() != NULLDATE) {
                boppay.getCtlmod().getDbl().setReldat(boppay.getCtlmod().getPaydat());
            }
        }
    }

    @Override
    public void bopStoreDBL(String transName, Ctlmod ctlmod, String reason) {
        String objFlg = "";
        Dbl dbl = ctlmod.getDbl();
        if (!MdaUtils.isEmpty(dbl.getInr())) {
            Dbl dblInf = dblMapper.selectByPrimaryKey(dbl);
            if (!MdaUtils.isEmpty(dblInf)) {
                objFlg = "F";
            }
        }
        if (MdaUtils.isEmpty(objFlg)) {
            sysmodService.sdbInsert(dbl);
        } else {
            sysmodService.sdbUpdate(dbl);
        }
        setRecordSig(transName, ctlmod, reason);
        sysmodService.sdbInsert(ctlmod.getBopsig());

    }

    @Override
    public String setDectyp(String transName) {
        String decTyp = "I";
        String namTyp = Strings.mid(transName.toUpperCase(), 4, 3);
        switch (namTyp) {
            case "ADD":
                decTyp = "A";
                break;
            case "AME":
                decTyp = "M";
                break;
            case "DEL":
                decTyp = "D";
                break;
            case "SND":
                decTyp = "S";
                break;
            case "INF":
                decTyp = "I";
                break;
            case "COR":
                decTyp = "U";
                break;
            case "PNT":
                decTyp = "P";
                break;
            default:
                decTyp = "V";
        }
        return decTyp;
    }

    //!设置控制表DBL的状态
    //!===================================
    //!变量：       typ     申报单格式
    //!===================================
    @Override
    public Dbl setDblSta(String transName, BopActionTypeVo bopActionTypeVo, BopObjectType bopObjectType, Dbl dbl, Dbl wrkdbl) {
        String dectyp = this.setDectyp(transName);
        if (MdaUtils.compareTo(dectyp, "A") != 0) {
            Dbl dblInfo = new Dbl();
            dblInfo.setInr(wrkdbl.getInr());
            dblInfo = dblMapper.selectByPrimaryKey(dblInfo);
            if (!MdaUtils.isEmpty(dblInfo)) {
                dbl = MdaUtils.deepClone(wrkdbl);
            }
        }
        if (MdaUtils.isEmpty(dbl.getTrdtyp())) {
            //ADD 交易的时候TradeType需要附值
            dbl.setTrdtyp(wrkdbl.getTrdtyp());
        }
        String bassta = this.bopSetDBLStatus(transName, "BAS", bopActionTypeVo.getBasActionType(), bopObjectType, dbl, wrkdbl.getBassta());
        dbl.setBassta(bassta);
        String dclsta = this.bopSetDBLStatus(transName, "DCL", bopActionTypeVo.getDclActionType(), bopObjectType, dbl, wrkdbl.getDclsta());
        dbl.setDclsta(dclsta);
        String vrfsta = this.bopSetDBLStatus(transName, "VRF", bopActionTypeVo.getVrfActionType(), bopObjectType, dbl, wrkdbl.getVrfsta());
        dbl.setVrfsta(vrfsta);
        return dbl;
    }

    //!设置dbl表中基础数据、申报数据、核销数据的状态。
    //!===================================
    //!返回值: 无
    //!变量：  typ    申报数据类型
    //!        fldtyp 数据类型，包括：bas/dcl/vrf
    //!        oldfld wrkdbl中对应的状态
    //!        fld    最终状态
    //!===================================
    @Override
    public String bopSetDBLStatus(String transName, String typ, String actionType, BopObjectType bopObjectType, Dbl dbl, String oldfld) {
        String fld = NULLSTR;
        String acpsta = "X";
        String sta = NULLSTR;
        switch (typ.toUpperCase()) {
            case "BAS":
                sta = bopObjectType.getBastyp();
                break;
            case "DCL":
                sta = bopObjectType.getDcltyp();
                break;
            case "VRF":
                sta = bopObjectType.getVrftyp();
                break;
            default:
                //error
        }
        String basfld = NULLSTR;
        String dectyp = this.setDectyp(transName);
        switch (dectyp) {
            case "A":  //新增
            case "V":  //提取
                if (MdaUtils.compareTo(sta, "N") == 0) {
                    fld = "N";
                } else {
                    if (!MdaUtils.isEmpty(acpsta)) {
                        fld = "W";
                    } else {
                        fld = "P";
                    }
                }
                break;
            case "M":  //修改
                if (MdaUtils.compareTo(sta, "N") == 0) {
                    fld = oldfld;
                } else {
                    if (MdaUtils.compareTo(sta, "D") == 0) {
                        if (MdaUtils.compareTo(typ, "BAS") != 0) {
                            basfld = dbl.getBassta();
                            switch (actionType) {
                                case "A":
                                    if (MdaUtils.compareTo(basfld, "P") == 0) {
                                        fld = "P";
                                    } else {
                                        fld = oldfld;
                                    }
                                    break;
                                case "C":
                                    fld = oldfld;
                                    break;
                                case "D":
                                    fld = oldfld;
                                    break;
                            }
                        } else {
                            switch (actionType) {
                                case "R":
                                    if (MdaUtils.compareTo(oldfld, "P") == 0 || MdaUtils.compareTo(oldfld, "G") == 0) {
                                        fld = oldfld;
                                    } else {
                                        fld = "W";
                                    }
                                    break;
                                default:
                                    fld = oldfld;
                            }
                        }
                    } else {
                        if (!MdaUtils.isEmpty(acpsta)) {
                            fld = "W";
                        } else {
                            fld = "P";
                        }
                    }
                }
                break;
            case "D":   //删除
                if (MdaUtils.compareTo(sta, "N") == 0 || MdaUtils.compareTo(sta, "D") == 0) {
                    fld = oldfld;
                } else {
                    switch (actionType) {
                        case "A":
                            fld = "D";
                            break;
                        case "C":
                            fld = "G";
                            break;
                        case "D":
                            if (MdaUtils.compareTo(typ, "BAS") == 0) {
                                fld = "W";
                            } else {
                                fld = "G";
                            }
                            break;
                    }
                }
                break;
            case "U":   //Undo中的操作
                if (MdaUtils.compareTo(oldfld, "N") == 0) {
                    fld = "N";
                } else {
                    if (MdaUtils.compareTo(oldfld, "G") == 0 || MdaUtils.compareTo(oldfld, "S") == 0) {
                        fld = "G";
                    } else {
                        fld = "D";
                    }
                }
                break;
            case "I":
            case "P":   //查询交易
                fld = oldfld;
                break;
            default:
                fld = oldfld;
        }
        return fld;
    }

    //!记录当前DBL所关联的BOPSIG信息
    //!===================================
    //!返回值：无
    //!变量：  当前交易描述
    //!===================================
    public void setRecordSig(String transName, Ctlmod ctlmod, String txt) {
        ctlmod.getBopsig().setDblinr(ctlmod.getDbl().getInr());
        ctlmod.getBopsig().setUsr(SettleContext.getUserSession().getUserVo().getEtyextkey());
        ctlmod.getBopsig().setDat(bopdecCommonService.getCoreSysDay());
        ctlmod.getBopsig().setTim(Numerics.time());
        ctlmod.getBopsig().setSta(ctlmod.getDbl().getBassta() + ctlmod.getDbl().getDclsta() + ctlmod.getDbl().getVrfsta());
        ctlmod.getBopsig().setTxt(" FRAME: " + transName + " " + txt);
    }

    public boolean isNo(String flag) {
        //!输出当前DBL STA是否代表为空
        //!===================================
        //!返回值：ture   为空
        //!        false  不为空
        //!变量：  flag  当前的状态
        //!===================================
        if (MdaUtils.isEmpty(flag) || MdaUtils.compareTo(flag, "N") == 0) {
            return true;
        } else {
            return false;
        }
    }


    public void jshStoreContract(Jshmod jshmod) {
        //存储BAS数据
        String szflg = jshmod.getSzflg();
        Dbjgrp dbjgrp = jshmod.getDbjgrp();
        Dbwgrp dbwgrp = jshmod.getDbwgrp();
        if ("X".equals(jshmod.getBasflg())) {
            Object bas = null;
            if ("1".equals(szflg)) {
                bas = dbjgrp.getBas();
            } else if ("2".equals(szflg)) {
                bas = dbwgrp.getBas();
            }
            if (dyncDaoService.dyncReadNoDataByInr(bas)) {
                sysmodService.sdbUpdateWithLog(bas);
            } else {
                sysmodService.sdbInsertWithLog(bas);
            }
        }
        //根据DBL中的STA存储VRF数据
        String vrfflg = jshmod.getVrfflg();
        if ("X".equals(vrfflg)) {
            Object vrf = null;
            if ("1".equals(szflg)) {
                dbjgrp.getVrf().setTmpref(dbjgrp.getBas().getInr());
                vrf = dbjgrp.getVrf();
            } else if ("2".equals(szflg)) {
                dbwgrp.getVrf().setTmpref(dbwgrp.getBas().getInr());
                vrf = dbwgrp.getVrf();
            }
            if (dyncDaoService.dyncReadNoDataByInr(vrf)) {
                sysmodService.sdbUpdateWithLog(vrf);
            } else {
                sysmodService.sdbInsertWithLog(vrf);
            }
        }
    }

    /**
     * //!设置dbl表中基础数据、管理数据的状态。
     * //!===================================
     * //!返回值: 无
     * //!变量：  typ    申报数据类型
     * //!        fldtyp 数据类型，包括：bas/vrf
     * //!        oldfld wrkdbl中对应的状态
     * //!        fld    最终状态
     * //!===================================
     */
    private String jshSetDBLStatus(Jshmod jshmod, String typ, String fldtyp, String oldfld) {
        Ctlset ctlmod = jshmod.getCtlmod();
        JshObjType jshObjType = ctlmod.getJshObjectMap().get(typ);
        String objtyp = jshObjType.getSubtyp();
        String bastyp = jshObjType.getBastyp();
        String dcltyp = jshObjType.getDcltyp();
        String vrftyp = jshObjType.getVrftyp();

        String sta = "";
        switch (Strings.toUpper(fldtyp)) {
            case "BAS":
                sta = bastyp;
                break;
            case "DCL":
                sta = dcltyp;
                break;
            case "VRF":
                sta = vrftyp;
                break;
            default:
        }
        String acpsta = "X";
        String actiontype = "";
        if ("DBJ".equals(objtyp)) {
            if ("BAS".equals(fldtyp)) {
                actiontype = jshmod.getDbjgrp().getBas().getActiontype();
//                acpsta = jshmod.getDbjgrp().getBasacp();
            } else if ("VRF".equals(typ)) {
                actiontype = jshmod.getDbjgrp().getVrf().getActiontype();
//                acpsta = jshmod.getDbjgrp().getVrfacp();
            }
        } else if ("DBW".equals(objtyp)) {
            if ("BAS".equals(fldtyp)) {
                actiontype = jshmod.getDbwgrp().getBas().getActiontype();
//                acpsta = jshmod.getDbjgrp().getBasacp();
            } else if ("VRF".equals(typ)) {
                actiontype = jshmod.getDbwgrp().getVrf().getActiontype();
//                acpsta = jshmod.getDbjgrp().getVrfacp();
            }
        }

        String basfld = "";
        switch (ctlmod.getDectyp()) {
            case "A":  //新增
            case "V":   //提取
                if (MdaUtils.compareTo(sta, "N") == 0) {
                    return "N";
                } else {
                    if (!this.isEmptyss(acpsta)) {
                        return "W";
                    } else {
                        return "P";
                    }
                }
            case "M":  //修改
                if (MdaUtils.compareTo(sta, "N") == 0) {
                    return oldfld;
                } else {
                    if (MdaUtils.compareTo(sta, "D") == 0) {
                        if (MdaUtils.compareTo(fldtyp, "BAS") != 0) {
                            basfld = ctlmod.getDbl().getBassta();
                            switch (actiontype) {
                                case "A":
                                    if (MdaUtils.compareTo(basfld, "P") == 0) {
                                        return "P";
                                    } else {
                                        return oldfld;
                                    }
                                case "C":
                                case "D":
                                    return oldfld;
                            }
                        } else {
                            if ("R".equals(actiontype)) {
                                if (MdaUtils.compareTo(oldfld, "P") == 0 || MdaUtils.compareTo(oldfld, "G") == 0) {
                                    return oldfld;
                                } else {
                                    return "W";
                                }
                            } else {
                                return oldfld;
                            }
                        }
                    } else {
                        if (!this.isEmptyss(acpsta)) {
                            return "W";
                        } else {
                            return "P";
                        }
                    }
                }
                break;
            case "D":   //删除
                if (MdaUtils.compareTo(sta, "N") == 0 || MdaUtils.compareTo(sta, "D") == 0) {
                    return oldfld;
                } else {
                    switch (actiontype) {
                        case "A":
                            return "D";
                        case "C":
                            return "G";
                        case "D":
                            if (MdaUtils.compareTo(fldtyp, "BAS") == 0) {
                                return "W";
                            } else {
                                return "G";
                            }
                    }
                }
                break;
            case "U":   //Undo中的操作
                if (MdaUtils.compareTo(oldfld, "N") == 0) {
                    return "N";
                } else {
                    if (MdaUtils.compareTo(oldfld, "G") == 0 || MdaUtils.compareTo(oldfld, "S") == 0) {
                        return "G";
                    } else {
                        return "D";
                    }
                }
            case "I":
            case "P":   //查询交易
            default:
                return oldfld;
        }
        return oldfld;
    }


    public boolean isEmptyss(String txt) {
        //取得上下文
        //!判断当前的PANSTA是否为空状态
        //!===================================
        //!返回值：True  为空
        //!        False 非空
        //!变量：  txt PANSTA 状态字 (X  非空，其他字均为空)
        //!===================================
        if (MdaUtils.compareTo(txt, "X") != 0) {
            return true;
        } else {
            return false;
        }

    }

    /**
     * //!设置控制表DBL的状态
     * //!===================================
     * //!变量：       typ     申报单格式
     * //!===================================
     */
    public void setDblStatus(Jshmod jshmod, String typ) {
        Dbl wrkdbl = Optional.ofNullable(jshmod.getCtlmod().getWrkdbl()).orElseGet(Dbl::new);
        String bassta = jshSetDBLStatus(jshmod, typ, "BAS", wrkdbl.getBassta());
        jshmod.getCtlmod().getDbl().setBassta(bassta);
        String dclsta = jshSetDBLStatus(jshmod, typ, "DCL", wrkdbl.getDclsta());
        jshmod.getCtlmod().getDbl().setDclsta(dclsta);
        String vrfsta = jshSetDBLStatus(jshmod, typ, "VRF", wrkdbl.getVrfsta());
        jshmod.getCtlmod().getDbl().setVrfsta(vrfsta);

    }

    public void setDblData(String objtyp, Ctlset ctlmod, Object dbjwgrp) {
        //!设置控制表DBL的数据
        //!===================================
        //!变量：  path    pancel存取路径
        //!        objtyp  处理的对象名
        //!===================================
        Date relDAT = ctlmod.getPaydat();
        String trninr = transactionHelpService.getTrninr();
        ctlmod.getDbl().setTrninr(trninr);
        ctlmod.getDbl().setObjtyp(objtyp);
        IJshBasData jshBasData = null;
        if (dbjwgrp instanceof Dbwgrp) {
            jshBasData = ((Dbwgrp) dbjwgrp).getBas();
        } else {
            jshBasData = ((Dbjgrp) dbjwgrp).getBas();
        }
        ctlmod.getDbl().setCurflg("*");
        ctlmod.getDbl().setObjinr(jshBasData.getInr());
        ctlmod.getDbl().setRptno(jshBasData.getRptno());
        ctlmod.getDbl().setTmpref(jshBasData.getTmpref());
        ctlmod.getDbl().setOwnextkey(jshBasData.getOwnextkey());
        ctlmod.getDbl().setOwnusr(SettleContext.getUserSession().getUsr().getExtkey());
        Date coreSysDay = bopdecCommonService.getCoreSysDay();
        ctlmod.getDbl().setCredat(coreSysDay);
        ctlmod.getDbl().setSfhdat(coreSysDay);
        relDAT = coreSysDay;   //在提取交易中生成申报号码时，要同时设日期定入帐
        if (MdaUtils.compareTo(ctlmod.getDectyp(), "V") == 0) {
            ctlmod.getDbl().setReldat(relDAT);
        } else {
            if (MdaUtils.compareTo(ctlmod.getDectyp(), "A") == 0 && ctlmod.getPaydat() != NULLDATE) {
                ctlmod.getDbl().setReldat(ctlmod.getPaydat());
            }
        }
        ctlmod.getDbl().setSndflg("C");
        ctlmod.getDbl().setDatsrc("G");
    }

    public void jshStoreDBL(Ctlset ctlmod, String reason) {
        //!保存当前DBL信息
        //!===================================
        //!返回值：无
        //!变量：  reason  交易处理描述
        //!===================================
        String objFlg = "";
        if (!MdaUtils.isEmpty(ctlmod.getDbl().getInr())) {
            Dbl dbl = dblMapper.selectByPrimaryKey(ctlmod.getDbl());
            if (dbl != null) {
                objFlg = "F";
            }
        }
        if (MdaUtils.isEmpty(objFlg)) {
            sysmodService.sdbInsert(ctlmod.getDbl());
        } else {
            sysmodService.sdbUpdate(ctlmod.getDbl());
        }
        Bopsig bopsig = new Bopsig();
        setRecordSig(bopsig, ctlmod.getDbl(), reason);
        sysmodService.sdbInsert(bopsig);
    }

    public void setRecordSig(Bopsig bopsig, Dbl dbl, String txt) {
        //!记录当前DBL所关联的JSHSIG信息
        //!===================================
        //!返回值：无
        //!变量：  当前交易描述
        //!===================================
        bopsig.setDblinr(dbl.getInr());
        bopsig.setUsr(SettleContext.getUserSession().getUsr().getExtkey());
        bopsig.setDat(new Date());
        bopsig.setTim(Numerics.time());
        bopsig.setSta(dbl.getBassta() + dbl.getVrfsta());
        bopsig.setTxt(" FRAME: " + SettleContext.getTransName() + TAB + txt);

    }

}
