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

import com.brilliance.mda.runtime.mda.impl.Argument;
import com.brilliance.mda.runtime.mda.util.Dates;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import com.brilliance.mda.runtime.mda.util.Strings;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import com.brilliance.isc.bo.*;
import com.brilliance.isc.common.cache.atpbch.AtpBchCacheService;
import com.brilliance.isc.common.cbsmod.service.XrtmodService;
import com.brilliance.isc.common.codetable.service.CodetableService;
import com.brilliance.isc.common.component.SptDataComponent;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.context.SettleSession;
import com.brilliance.isc.common.context.SpringEnvContext;
import com.brilliance.isc.common.gedmod.vo.GedmodVo;
import com.brilliance.isc.common.ordmod.service.OrdmodService;
import com.brilliance.isc.common.sysmod.SysmodService;
import com.brilliance.isc.common.transaction.help.TransactionHelpService;
import com.brilliance.isc.common.transaction.vo.BaseTransactionVo;
import com.brilliance.isc.common.trndoc.vo.TrndocVo;
import com.brilliance.isc.common.trnism.service.IWfeRegister;
import com.brilliance.isc.common.trnism.service.TrnismService;
import com.brilliance.isc.common.trnism.vo.TrnismVo;
import com.brilliance.isc.common.trnmod.service.TrnmodService;
import com.brilliance.isc.common.trnmod.vo.TrnmodVo;
import com.brilliance.isc.common.trnsig.service.TrnsigService;
import com.brilliance.isc.common.util.Utils;
import com.brilliance.isc.common.vo.BizInfoVo;
import com.brilliance.isc.common.wfmmod.service.WfmmodService;
import com.brilliance.isc.common.wfmmod.vo.WfmmodVo;
import com.brilliance.isc.mda.dao.*;
import com.brilliance.isc.utils.StringUtils;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;

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

@Service
public class TrnismServiceImpl implements TrnismService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WfmmodService wfmmodService;

    @Autowired
    private TrnmodService trnmodService;

    @Autowired
    private TrlMapper trlMapper;

    @Autowired
    private TrnsigService trnsigService;

    @Autowired
    private GleMapper gleMapper;

    @Autowired
    public PublicMapper publicMapper;

    @Autowired
    private XrtmodService xrtmodService;

    @Autowired
    private OrdmodService ordmodService;

    @Autowired
    private SysmodService sysmodService;

    @Autowired
    private TransactionHelpService transactionHelpService;

    @Autowired
    private SptDataComponent sptDataComponent;

    @Autowired
    private CodetableService codetableService;

    @Autowired
    private GtxMapper gtxMapper;

    @Autowired
    private FujianMapper fujianMapper;

    @Autowired
    private SmhMapper smhMapper;

    @Autowired
    private AtpBchCacheService atpBchCacheService;

    public boolean existGLE(TrnmodVo trnmodVo){
       /*
        String sql = "SELECT count(*) CNT from gle where trninr='" + trnmodVo.getTrn().getInr() + "'";
        int cnt = publicMapper.dyncCount(ImmutableBiMap.of("sql",sql));
        */
        int cnt = gleMapper.selectCountByTrninr(trnmodVo.getTrn().getInr());
        return cnt > 0;
//        int count = -1;
//        Argument<Integer> _countBox = Argument.box(count);
//        ctx.getDaoSession().dbExecuteSQL(sql);
//        if( ctx.getErrorCode() <= 0 )
//        {
//            ctx.getDaoSession().dbFetchFields("CNT",_countBox);
//            count = _countBox.value;
//            ctx.getDaoSession().dbCloseCursor();
//            if( count == 0 )
//            {
//                return false;
//            }
//            else
//            {
//                return true;
//            }
//        }
//        return false;

    }

    /**
     * source:trnism.0006.script
     */
    public void wfmAdd_Release(TrnmodVo trnmodVo) {
        WfmmodVo wfmmodVo = trnmodVo.getTrnism().getWfmmod();
        //取得上下文
//        IContext ctx = MdaEnv.getContext();
//        ctx.setErrorCode(NO_ERROR);
        //! Create WorkflowEntries for Control & Release and other core services
        String allint = NULLSTR;
//        String fftcce = NULLSTR;
        String scainr = NULLSTR;
        String imgnum = NULLSTR;
        String maimpz = NULLSTR;
        String diaoqi = NULLSTR;
        String chlibz = NULLSTR;
        List<Wfe> wfeg = trnmodVo.getTrnism().getWfmmod().getWfe();
        if (!sysmodService.sptIsExeLoaded(trnmodVo.getSptaut().getSta())) {
            // Checking for complete signatures is done by SRVPDS
            // WFMMOD.WfeAdd ( "PDS", "PDS", DateAdd( Today, 1 ), BigDecimal.valueOf(21) )
            // when ORD is activated, add SRVFIN
            // ORDMOD\\ACTFLG  固定给“X”
            //if (!MdaUtils.isEmpty(ordmodService.getActflg())) {
            //}
            // Service REL is to be used only, if there are prerequisites for being allowed to release the transaction.
            // Thus if release of transactions in TRNREL should be allowed only after execution
            // of other services (i.e. limit check), please include the service REL into the workflow.
            // This is done by activating the following line.
            // WFMMOD.WfeAdd ( "REL", "REL", DateAdd( Today, 1 ), 21 )
            // Add WFEs that are required for every transaction
            // WFMMOD.WfeAdd ( "COM", "COM", DateAdd( Today, 1 ), 21 )
            // WFMMOD.WfeAdd ( "CLN", "CLN", DateAdd( Today, 1 ), 21 )
            GedmodVo gedmod = transactionHelpService.getTransactionStoreSet().getGedmod();
            if (gedmod != null) {
                // 记账前增加E结算额度加锁和电子购汇额度解冻
                wfmmodService.wfeAdd("EDB", "EDB", Dates.add(Dates.today(),1), BigDecimal.valueOf(21));
            }
            if (existGLE(trnmodVo) && !wfmmodService.existCreFin(wfmmodVo.getWfe())) {
                wfmmodService.wfeAdd("ACT", "ACT", Dates.add(Dates.today(), 1), BigDecimal.valueOf(21));
            }
            if (gedmod != null) {
                // 记账后增加E结算额度解锁
                wfmmodService.wfeAdd("EDA", "EDA", Dates.add(Dates.today(),1), BigDecimal.valueOf(21));
            }

            String transName = SettleContext.getTransName();
            try {
                IWfeRegister wfeRegister = SpringEnvContext.getBean(atpBchCacheService.getSdTransNameForRegister().toLowerCase() + "." + IWfeRegister.REGISTER_NAME);
                logger.info("执行{}交易下注册的WFE添加类",transName);
                wfeRegister.wfeRegister(trnmodVo);
            } catch (NoSuchBeanDefinitionException e) {
                logger.info("没有找到交易{} WFE注册类", transName);
            }
            wfmmodService.wfeAdd("PDP", "PDP", Dates.add(Dates.today(), 1), BigDecimal.valueOf(21));
            wfmmodService.wfeAdd("FIN", "FIN", Dates.add(Dates.today(), 1), BigDecimal.valueOf(21));
            /**if not IsEmpty( \\TRNMOD\\TRN\\XREFLG ) then
             WFMMOD.WfeAdd ( "PDX", "PDX", DateAdd( Today, 1 ), 21 )
             endif**/
            //增加新服务，调接口4338
//            allint = Datas.getContent(this,"BPTFFT\\ALLINT");
//            err = ctx.getErrorCode();
//            fftcce = Datas.getContent(this,"BPTFFT\\FFTCCE");
//            err = ctx.getErrorCode();
//            allint = trnmodVo.getAllint();
////            fftcce = trnmodVo.getTrnism().getFftcce();
//            if (MdaUtils.compareTo(SettleContext.getTransName(), "BPTFFT") == 0) {
//                if   (MdaUtils.compareTo(allint, "0") != 0) {
//                    wfmmodService.wfeAdd("FFT", "FFT", Dates.add(Dates.today(), 1), BigDecimal.valueOf(21));
//                }
//                /**
//                 if $fftcce <> "0" then
//                 WFMMOD.WfeAdd ( "ACT", "ACT", DateAdd( Today, 1 ), 21 )
//                 endif
//                 **/
//            } else {
//                // Modify by cz 增加记帐流程
//                if (existGLE(trnmodVo) && !wfmmodService.existCreFin(wfeg)) {
//                    wfmmodService.wfeAdd("ACT", "ACT", Dates.add(Dates.today(), 1), BigDecimal.valueOf(21));
//                }
//            }
//            // CPTECO交易增加一个服务，RTO用于受到ack报文后给中台系统返回成功状态
//            if (MdaUtils.compareTo(SettleContext.getTransName(), "CPTECO") == 0) {
//                wfmmodService.wfeAdd("RTO", "RTO", Dates.add(Dates.today(), 1), BigDecimal.valueOf(21));
//            }
//            if (MdaUtils.compareTo(SettleContext.getTransName(), "CPTECI") == 0) {
//                wfmmodService.wfeAdd("ECI", "ECI", Dates.add(Dates.today(), 1), BigDecimal.valueOf(21));
//            }
//            if (MdaUtils.compareTo(SettleContext.getTransName(), "DITOPN") == 0 || MdaUtils.compareTo(SettleContext.getTransName(), "DITAME") == 0) {
////                scainr = Datas.getContent(this,"\\DITP\\ELCSCA\\INR");
////                err = ctx.getErrorCode();
//                scainr = trnmodVo.getElcsca().getInr();
////                imgnum = Datas.getContent(this,"\\DITP\\ELCSCA\\IMGNUM");
////                err = ctx.getErrorCode();
//                imgnum = trnmodVo.getElcsca().getImgnum();
//                if (!MdaUtils.isEmpty(scainr) && !MdaUtils.isEmpty(imgnum)) {
//                    wfmmodService.wfeAdd("IMGELC", "IMGELC", Dates.today(), BigDecimal.valueOf(24));
//                }
//            }
//            if (MdaUtils.compareTo(SettleContext.getTransName(), "BFTSND") == 0 || MdaUtils.compareTo(SettleContext.getTransName(), "BFTDRV") == 0) {
////                scainr = Datas.getContent(this,"\\BFTP\\ELCSCA\\INR");
////                err = ctx.getErrorCode();
//                scainr = trnmodVo.getElcsca().getInr();
////                imgnum = Datas.getContent(this,"\\BFTP\\ELCSCA\\IMGNUM ");
////                err = ctx.getErrorCode();
//                imgnum = trnmodVo.getElcsca().getImgnum();
//                if (!MdaUtils.isEmpty(scainr) && !MdaUtils.isEmpty(imgnum)) {
//                    wfmmodService.wfeAdd("IMGELC", "IMGELC", Dates.today(), BigDecimal.valueOf(24));
//                }
//            }
//            //---YSP---TEST---
//            if (MdaUtils.compareTo(Strings.mid(SettleContext.getTransName(), 1, 2), "JS") == 0) {
////                maimpz = Datas.getContent(this,"\\JSDGRP\\REC\\MAIMPZ");
////                err = ctx.getErrorCode();
//                maimpz = Utils.getFieldValue(getRec(),"MAIMPZ").toString();
////                diaoqi = Datas.getContent(this,"\\JSDGRP\\REC\\DIAOQI");
////                err = ctx.getErrorCode();
//                diaoqi = Utils.getFieldValue(getRec(),"DIAOQI").toString();
////                chlibz = Datas.getContent(this,"\\JSDGRP\\REC\\CHLIBZ");
////                err = ctx.getErrorCode();
//                chlibz = Utils.getFieldValue(getRec(),"CHLIBZ").toString();
//                if (MdaUtils.compareTo(Strings.mid(SettleContext.getTransName(), 4, 3), "OPN") == 0) {
//                    if (MdaUtils.compareTo(maimpz, "3") == 0 && MdaUtils.compareTo(chlibz, "2") == 0) {
//                        wfmmodService.wfeAdd("JSJ", "JSJ", Dates.add(Dates.today(), 1), BigDecimal.valueOf(21));
//                        wfmmodService.wfeAdd("JSK", "JSK", Dates.add(Dates.today(), 1), BigDecimal.valueOf(21));
//                    }
//                    wfmmodService.wfeAdd("JSA", "JSA", Dates.add(Dates.today(), 1), BigDecimal.valueOf(21));
//                    if (MdaUtils.compareTo(chlibz, "2") == 0) {
//                        wfmmodService.wfeAdd("JSB", "JSB", Dates.add(Dates.today(), 1), BigDecimal.valueOf(21));
//                    }
//                }
//                if (MdaUtils.compareTo(Strings.mid(SettleContext.getTransName(), 4, 3), "AME") == 0) {
//                    if (MdaUtils.compareTo(diaoqi, "1") == 0) {
//                        wfmmodService.wfeAdd("JSN", "JSN", Dates.add(Dates.today(), 1), BigDecimal.valueOf(21));
//                    } else {
//                        wfmmodService.wfeAdd("JSG", "JSG", Dates.add(Dates.today(), 1), BigDecimal.valueOf(21));
//                    }
//                }
//                /**
//                 if Mid( GetFramename, 4, 3 ) == "DEL" then
//                 WFMMOD.WfeAdd ( "JSA", "JSA", DateAdd( Today, 1 ), 21 )
//                 endif
//                 **/
//                if (MdaUtils.compareTo(Strings.mid(SettleContext.getTransName(), 4, 3), "PAY") == 0) {
//                    if (MdaUtils.compareTo(diaoqi, "1") == 0) {
//                        wfmmodService.wfeAdd("JSH", "JSH", Dates.add(Dates.today(), 1), BigDecimal.valueOf(21));
//                        wfmmodService.wfeAdd("JSI", "JSI", Dates.add(Dates.today(), 1), BigDecimal.valueOf(21));
//                    } else {
//                        wfmmodService.wfeAdd("JSE", "JSE", Dates.add(Dates.today(), 1), BigDecimal.valueOf(21));
//                        wfmmodService.wfeAdd("JSF", "JSF", Dates.add(Dates.today(), 1), BigDecimal.valueOf(21));
//                    }
//                }
//                if (MdaUtils.compareTo(Strings.mid(SettleContext.getTransName(), 4, 3), "SET") == 0) {
//                    if (MdaUtils.compareTo(diaoqi, "1") == 0) {
//                        wfmmodService.wfeAdd("JSL", "JSL", Dates.add(Dates.today(), 1), BigDecimal.valueOf(21));
//                        wfmmodService.wfeAdd("JSM", "JSM", Dates.add(Dates.today(), 1), BigDecimal.valueOf(21));
//                    } else {
//                        wfmmodService.wfeAdd("JSC", "JSC", Dates.add(Dates.today(), 1), BigDecimal.valueOf(21));
//                        wfmmodService.wfeAdd("JSD", "JSD", Dates.add(Dates.today(), 1), BigDecimal.valueOf(21));
//                    }
//                }
//            }
//            if (MdaUtils.compareTo(Strings.mid(SettleContext.getTransName(), 1, 2), "WI") == 0) {
//                if (MdaUtils.compareTo(Strings.mid(SettleContext.getTransName(), 4, 3), "OPN") == 0) {
//                    wfmmodService.wfeAdd("WIA", "WIA", Dates.add(Dates.today(), 1), BigDecimal.valueOf(21));
//                }
//                if (MdaUtils.compareTo(Strings.mid(SettleContext.getTransName(), 4, 3), "OPE") == 0) {
//                    wfmmodService.wfeAdd("WIB", "WIB", Dates.add(Dates.today(), 1), BigDecimal.valueOf(21));
//                }
//                if (MdaUtils.compareTo(Strings.mid(SettleContext.getTransName(), 4, 3), "SET") == 0) {
//                    wfmmodService.wfeAdd("WIC", "WIC", Dates.add(Dates.today(), 1), BigDecimal.valueOf(21));
//                }
//                /**
//                 $MAIMPZ = GetContent( "\\\\WIDGRP\\\\REC\\\\QIQULX" )
//                 $err = ErrorCode
//                 if Mid( GetFramename, 4, 3 ) == "OPN" or Mid( GetFramename, 4, 3 ) == "DEL" then
//                 if $MAIMPZ == "0" then
//                 WFMMOD.WfeAdd ( "WIA", "WIA", DateAdd( Today, 1 ), 21 )
//                 endif
//                 if $MAIMPZ == "1" then
//                 WFMMOD.WfeAdd ( "WIB", "WIB", DateAdd( Today, 1 ), 21 )
//                 endif
//                 else
//                 WFMMOD.WfeAdd ( "WIC", "WIC", DateAdd( Today, 1 ), 21 )
//                 endif
//                 **/
//            }
        }

    }

    @Override
    public void wfmAdd_Qin(String cortypsub) {
        wfmmodService.wfeAdd("QIN", "QIN" + cortypsub, Dates.today(), BigDecimal.valueOf(24));
        //跨境人名币结算代理行模式的7708转账交易和清算报文绑定发送
        if (checkAgency()) {
            wfmmodService.wfeAdd("AGE", "AGE", Dates.add(Dates.today(), 1), BigDecimal.valueOf(21));
        }
        wfmmodService.wfeAdd("PDA", "PDA" + cortypsub, Dates.today(), BigDecimal.valueOf(24));
    }

    @Override
    public void wfmAdd_Swift(String cortypsub) {
        wfmmodService.wfeAdd("SWT", "SWT" + cortypsub, Dates.today(), BigDecimal.valueOf(24));
        wfmmodService.wfeAdd("PDA", "PDA" + cortypsub, Dates.today(), BigDecimal.valueOf(24));
    }

    @Override
    public void wfmAdd_ELC(String cortypsub) {
        // Create WorkflowEntry for HVPS-Message
        wfmmodService.wfeAdd("ELC", "ELC" + cortypsub, Dates.today(), BigDecimal.valueOf(24));
        wfmmodService.wfeAdd("PDE", "PDE" + cortypsub, Dates.today(), BigDecimal.valueOf(24));
    }

    @Override
    public void wfmAdd_RTGS_Plus(String cortypsub) {
        //空实现
    }

    @Override
    public void wfmAdd_Target(String cortypsub) {
        //空实现
    }

    @Override
    public void wfmAdd_FAX(String cortypsub) {
        //空实现
    }

    @Override
    public void wfmAdd_Telex(String cortypsub) {
        //空实现
    }

    @Override
    public void wfmAdd_email(String cortypsub) {
        wfmmodService.wfeAdd("EML", "EML" + cortypsub, Dates.today(), BigDecimal.valueOf(24));
    }

    @Override
    public void wfmAdd_CMT100(String cortypsub) {
        // public
        // Create WorkflowEntry for SWIFT-Message
        TrnmodVo trnmodVo = transactionHelpService.getTransactionStoreSet().getTrnmodVo();
        String secmod = "";
        int ignore = 0;
        String transName = SettleContext.getDirectTransName();
        switch (transName) {
            //新增电商支付汇出汇款CPTECO,PANEL为CPTP1,与原汇款逻辑保持一致
            case "CPTECO":
                //secmod = Datas.getContent(this,"\\CPTP1\\SECMOD");
                secmod = trnmodVo.getSecmod();
                break;
            case "CPTOPN":
                //secmod = Datas.getContent(this,"\\CPTP\\SECMOD");
                secmod = trnmodVo.getSecmod();
                break;
            case "BRTSET":
            case "BCTSET":
            case "GITSET":
            case "DFTSET":
            case "DFTMSG":
                secmod = trnmodVo.getSecmod();
                break;
            default:
                secmod = "";
        }
        if ("1".equals(secmod)) {
            wfmmodService.wfeAdd("HVP", "HVP" + cortypsub, Dates.today(), BigDecimal.valueOf(24));
        } else {
            if ("0".equals(secmod)) {
                wfmmodService.wfeAdd("CMT", "CMT" + cortypsub, Dates.today(), BigDecimal.valueOf(24));
                wfmmodService.wfeAdd("PDA", "PDA" + cortypsub, Dates.today(), BigDecimal.valueOf(24));
            }
        }
    }

    @Override
    public void wfmAdd_HVPS(String cortypsub) {
        wfmmodService.wfeAdd("HVP", "HVP" + cortypsub, Dates.today(), BigDecimal.valueOf(24));
    }

    @Override
    public void wfmAdd_CIPS(String cortypsub) {
        // public
        // Create WorkflowEntry for SWIFT-Message
        wfmmodService.wfeAdd("CIP", "CIP" + cortypsub, Dates.today(), BigDecimal.valueOf(24));
        //--------------------------
        if( MdaUtils.compareTo(SettleContext.getDirectTransName(),"CPTOPN") == 0 ) {
            BaseTransactionVo transactionVo = transactionHelpService.getTransactionStoreSet().getTransactionVo();
            String msgtyp = Utils.getFieldValue(transactionVo, "cpdgrp//msgtyp", "");
            if ("6801".equalsIgnoreCase(msgtyp)) {
                wfmmodService.wfeAdd("PDB", "PDB" + cortypsub, Dates.today(), BigDecimal.valueOf(24));
            }
        }
    }

    @Override
    public void wfmAdd_Tradeconnect(String cortypsub) {
        wfmmodService.wfeAdd("TCO", "TCO" + cortypsub, Dates.today(), BigDecimal.valueOf(24));
    }

    /**
     * source:trnism.0047.script
     */
    public void wfmAdd_YPTGNZ(String cortypsub) {
        wfmmodService.wfeAdd("YPT", "YPT", Dates.add(Dates.today(), 1), BigDecimal.valueOf(24));
    }

    /**
     *   source:trnism.0041.script
     */
    public void wfmAdd_FMT(String cortypsub) {
        //! Create WorkflowEntry for text messages
        if (MdaUtils.compareTo(SettleContext.getDirectTransName(),"CPTOPN") == 0) {
            Cpd rec = (Cpd)transactionHelpService.getTransactionStoreSet().getRec();
            if ("HNHZ".equalsIgnoreCase(rec.getCptrou())) {
                // 行内划转跳过FMT工作流
                return;
            }
        }
        wfmmodService.wfeAdd("FMT", "FMT" + cortypsub, Dates.add(Dates.today(),1), BigDecimal.valueOf(24));
        //WFMMOD.WfeAdd ( "PDA", "PDA" + cortypsub, DateAdd( Today, 1 ), 24 )
        if( MdaUtils.compareTo(SettleContext.getDirectTransName(),"CPTOPN") == 0 ) {
            BaseTransactionVo transactionVo = transactionHelpService.getTransactionStoreSet().getTransactionVo();
            String msgtyp = Utils.getFieldValue(transactionVo, "cpdgrp//msgtyp", "");
            if ("6901".equalsIgnoreCase(msgtyp)) {
                wfmmodService.wfeAdd("PDB", "PDB" + cortypsub, Dates.today(), BigDecimal.valueOf(24));
            }
        }

    }
    /**
     * source:trnism.0044.script
     */
    public void wfmAdd_ERX(String cortypsub) {
        //public
        //Create WorkflowEntry for FAX-Message
        //---TEST---
        /**
         if \\SYSMOD\\ATP\\COD == "GITAME" or \\SYSMOD\\ATP\\COD == "GITOPN" or \\SYSMOD\\ATP\\COD == "GITCRJ" or \\SYSMOD\\ATP\\COD == "GITCAN" or \\SYSMOD\\ATP\\COD == "GITSET" then
         **/
        String transName = SettleContext.getTransName();
        TrndocVo trndocVo = transactionHelpService.getTransactionStoreSet().getTrndocVo();
        BizInfoVo rec = trndocVo.getRec();
        String bey = Utils.getFieldValue(rec, "beyzd3", "");
        String beyFlg = Utils.getFieldValue(rec, "beyzd6", "");
        if("Y".equalsIgnoreCase(bey)){
            // 委托行绑定影像
            imgygeRegister(cortypsub);
            return;
        }
        if ("GITAME|NITAME|HITAME|GITOPN|NITOPN|HITOPN|GITCRJ|NITCRJ|GITCAN|NITCAN|HITCAN|GITADD|NITADD|GITSET|NITSET|NITMJM".indexOf(transName) > -1) {
            String fromflg = Utils.getFieldValue(rec, "fromflg", "");
            if ("Y".equalsIgnoreCase(fromflg)) {
                String othersno = Utils.getFieldValue(rec, "othersno", "");
                wfmmodService.wfeAddYTZ("YTZ", "YTZ" + cortypsub, Dates.today(), BigDecimal.valueOf(24));
                String msgtyp = "";
                //Datas.getContent(this,"\\TRNMOD\\TRNDOC\\ERSMOD\\ERSMAP\\WRKSMH\\MSGTYP");
                if("NITMJM".indexOf(transName) > -1 && MdaUtils.compareTo(msgtyp,"BHJMSQ") == 0 ) {
                    if (MdaUtils.compareTo(othersno,"E0000015") == 0 )
                        wfmmodService.wfeAddYTZ ( "AXQ", "AXQ", Dates.add(Dates.today(),1), BigDecimal.valueOf(24) );
                    else if (MdaUtils.compareTo(othersno,"E0000018") == 0 ) {
                        //  宁夏保函解密 需要先明文签章合并密文生成新的OFD，再次盖骑缝章
                        wfmmodService.wfeAddYTZ ( "OFDNXS", "OFDNXS", Dates.add(Dates.today(),1), BigDecimal.valueOf(24) );//宁夏保函生成明文OFD
                        wfmmodService.wfeAdd ( "OFF", "OFF", Dates.add(Dates.today(),1), BigDecimal.valueOf(24) );//明文盖章
                        wfmmodService.wfeAdd ( "OFDM", "OFDM", Dates.add(Dates.today(),1), BigDecimal.valueOf(24) );//合并再盖章
                    }
                }
                if ("GITAME|NITAME|HITAME|GITOPN|NITOPN|HITOPN".indexOf(transName) > -1) {
                    /**
                     $OTHERSNO = GetContent( "\\\\GIDGRP\\\\REC\\\\OTHERSNO" )
                     $ERR = ErrorCode
                     **/
                    String rtnfilestyle = Utils.getFieldValue(rec, "rtnfilestyle", "");
                    //  $M22AMECNT = GetContent( "\\\\TRNMOD\\\\SWIADD\\\\M22AMECNT" )
                    //  $ERR = ErrorCode
                    //  if $M22AMECNT = "01" then
                    if ("GITOPN|NITOPN|HITOPN".indexOf(transName) > -1) {
                        //---由于暂时不支持修改交易OFD---此处代码修改---
                        if( MdaUtils.compareTo(othersno,"E0000007") != 0 && MdaUtils.compareTo(othersno,"E0000012") != 0 && MdaUtils.compareTo(othersno,"E0000003") != 0 && MdaUtils.compareTo(othersno,"E0000023") != 0) {
                            if (!isByIFTTransfer2YPT(othersno)){
                                wfmmodService.wfeAddYTZ ( "AXQ", "AXQ", Dates.add(Dates.today(),1), BigDecimal.valueOf(24) );
                            }
                        }
                        if (MdaUtils.compareTo(othersno, "E0000007") == 0 || MdaUtils.compareTo(othersno, "E0000012") == 0) {
                            if (MdaUtils.compareTo(othersno, "E0000007") == 0) {
                                wfmmodService.wfeAddYTZ("OFD", "OFD", Dates.add(Dates.today(), 1), BigDecimal.valueOf(24));
                            }
                            wfmmodService.wfeAdd("OFF", "OFF", Dates.add(Dates.today(), 1), BigDecimal.valueOf(24));
                            if (MdaUtils.compareTo(othersno, "E0000012") == 0) {
                                wfmmodService.wfeAddYTZ("OFE", "OFE", Dates.add(Dates.today(), 1), BigDecimal.valueOf(24));
                                wfmmodService.wfeAddYTZ("OFG", "OFG", Dates.add(Dates.today(), 1), BigDecimal.valueOf(24));
                                wfmmodService.wfeAdd("OFH", "OFH", Dates.add(Dates.today(), 1), BigDecimal.valueOf(24));
                            }
                        }

                        // OFDNXM（宁夏生成密文OFD） OFH（密文安心签OFD）
                        if (MdaUtils.compareTo(othersno,"E0000018") == 0) {
                            wfmmodService.wfeAddYTZ ( "OFDNXM", "OFDNXM", Dates.add(Dates.today(),1), BigDecimal.valueOf(24) );
                            wfmmodService.wfeAdd ( "OFH", "OFH", Dates.add(Dates.today(),1), BigDecimal.valueOf(24) );
                        }
                        //---深担保安心签---
                        if (MdaUtils.compareTo(othersno, "E0000003") == 0) {
                            //如果是信开不走安心签上传
                            if(!isXK()) {
                                wfmmodService.wfeAddYTZ("SDBDQZ", "SDBDQZ", Dates.add(Dates.today(), 1), BigDecimal.valueOf(24));
                            }
                        }
                        //---SZBH---
                        if (MdaUtils.compareTo(othersno, "E0000009") == 0) {
                            wfmmodService.wfeAddYTZ("AXC", "AXC", Dates.add(Dates.today(), 1), BigDecimal.valueOf(24));
                        }
                        //呼和浩特增加密文电子章
                        if (MdaUtils.compareTo(othersno, "E0000017") == 0) {
                            wfmmodService.wfeAddYTZ("AXR", "AXR", Dates.add(Dates.today(), 1), BigDecimal.valueOf(24));
                        }
                        if (MdaUtils.compareTo(rtnfilestyle, "02") == 0) {
                            wfmmodService.wfeAddYTZ("CJL", "CJL", Dates.add(Dates.today(), 1), BigDecimal.valueOf(24));
                        }
                    } else {
                        if (!isByIFTTransfer2YPT(othersno)) {
                            wfmmodService.wfeAddYTZ("AXQ", "AXQ", Dates.add(Dates.today(), 1), BigDecimal.valueOf(24));
                        }
                    }
                    //---南京保函02改为03，03改为文本后边加一个面函---
                    //增加深圳
                    /**
                     if $OTHERSNO == "E0000010" or $OTHERSNO == "E0000002" or ( $Rtnfilestyle == "02" and \\SYSMOD\\ATP\\COD == "GITOPN" ) then
                     **/
                    if (MdaUtils.compareTo(othersno, "E0000010") == 0 || MdaUtils.compareTo(othersno, "E0000002") == 0 || MdaUtils.compareTo(othersno, "E0000003") == 0 || (MdaUtils.compareTo(rtnfilestyle, "03") == 0 && "GITOPN|NITOPN|HITOPN".indexOf(transName) > -1)) {
                        //深担保主动修改不注册下面两个工作流， spt-smh
                        if (StringUtils.equals("E0000003", othersno) && StringUtils.equals("NITAME", transName)) {
                            Spt spt = transactionHelpService.getTransactionStoreSet().getTrnmodVo().getSpt();
                            if (spt != null) {
                                logger.info("NITAME交易sptinr为{}", spt.getInr());
                                Smh smh = smhMapper.getSmhInfoBySptSmhinr(spt.getSmhinr());
                                if (smh != null) {
                                    //如果是信开并且是深担保，不走安心签下载
                                    if(!isXKAndOthersno(othersno) ) {
                                        wfmmodService.wfeAddYTZ("AXD", "AXD", Dates.add(Dates.today(), 1), BigDecimal.valueOf(24));
                                    }
                                    wfmmodService.wfeAdd("MFT", "MFT", Dates.add(Dates.today(), 1), BigDecimal.valueOf(24));
                                }
                            }
                        } else {
                            //如果是信开并且是深担保，不走安心签下载
                            if(!isXKAndOthersno(othersno) ) {
                                wfmmodService.wfeAddYTZ("AXD", "AXD", Dates.add(Dates.today(), 1), BigDecimal.valueOf(24));
                            }
                            wfmmodService.wfeAdd("MFT", "MFT", Dates.add(Dates.today(), 1), BigDecimal.valueOf(24));
                        }
                        if ("GITOPN|NITOPN|HITOPN".indexOf(transName) > -1) {
                            /**
                             WFMMOD.WfeAdd ( "MFV", "MFV", DateAdd( Today, 1 ), BigDecimal.valueOf(24) )
                             **/
                            String trninr = transactionHelpService.getTrninr();
                            String interTxt = "SRVWBC|bhwb1_snd|" + trninr;
                            wfmmodService.wfeAddWithVarYPT("MFV", "MFV", Dates.add(Dates.today(), 1), BigDecimal.valueOf(24), interTxt);
                        }
                    }
                    // 信开或者主动修改通过ift传输给云平台 现在只有内蒙古财政厅和云平台
                    if (isByIFTTransfer2YPT(othersno)) {
                        TrnmodVo trnmodVo = transactionHelpService.getTransactionStoreSet().getTrnmodVo();
                        WfmmodVo wfmmodVo = trnmodVo.getTrnism().getWfmmod();
                        int mft = wfmmodService.getIdx(wfmmodVo, "MFT");
                        if (mft == -1)
                            wfmmodService.wfeAdd("MFT", "MFT", Dates.add(Dates.today(), 1), BigDecimal.valueOf(24));
                    }

                }
            } else {
                wfmmodService.wfeAdd("ERX", "ERX" + cortypsub, Dates.today(), BigDecimal.valueOf(24));
            }
        } else {
            wfmmodService.wfeAdd("ERX", "ERX" + cortypsub, Dates.today(), BigDecimal.valueOf(24));
        }
        // 委托行已绑定影像，递转行不需要在进行绑定，非递转业务逻辑beyzd6无值正常走此逻辑
        if(!"Y".equalsIgnoreCase(beyFlg)){
            imgygeRegister(cortypsub);
        }
        //String imginr = bo.getTrnInfo().getImginr();
        /*String imgnum = Datas.getContent(this,"\\TRNMOD\\TRNDOC\\ERSMOD\\IMGNUM");
        err = ctx.getErrorCode();
        String scainr = Datas.getContent(this,"\\MTABUT\\IMGMOD\\SCAWRK\\INR");
        err = ctx.getErrorCode();
        if( ! MdaUtils.isEmpty(imgnum) && ! MdaUtils.isEmpty(scainr) )
        {
            this.getWfmmod().wfeAdd ( "IMGYGE", "IMGYGE" + cortypsub, Dates.today(), BigDecimal.valueOf(24) );
        }*/
    }

    private void imgygeRegister(String cortypsub){
        BaseTransactionVo bo = transactionHelpService.getTransactionStoreSet().getTransactionVo();
        Ers ers = sptDataComponent.loadErs(bo);
        // 委托行已绑定影像，递转行不需要绑定影像
        if (ers != null) {
            if (StringUtils.isNotEmpty(ers.getImgnum())) {
                wfmmodService.wfeAdd("IMGYGE", "IMGYGE" + cortypsub, Dates.today(), BigDecimal.valueOf(24));
            } else {
                Fujian fujian = fujianMapper.getFujianByFtynum(ers.getFtynum());
                if (fujian != null && StringUtils.isNotEmpty(fujian.getDocid())) {
                    wfmmodService.wfeAdd("IMGYGE", "IMGYGE" + cortypsub, Dates.today(), BigDecimal.valueOf(24));
                }
            }
        }
    }


    /**
     * 信开是否通过ift传输到云平台
     *
     *
     * @return
     */
    private boolean isByIFTTransfer2YPT(String othersno){
        // 开立方式如果是信开，渠道是内蒙古，深圳，注册MFT
        if (StringUtils.isEmpty(othersno)||StringUtils.equals(othersno,"E0000022") || StringUtils.equals(othersno,"E0000023")){
            BaseTransactionVo bo = transactionHelpService.getTransactionStoreSet().getTransactionVo();
            Ers ers = sptDataComponent.loadErs(bo);
            //ers为空说明不是来报交易操作（比如主动修改），lx为02是信开
            if (ers == null || "02".equals(ers.getLx()))
                return true;
        }
        return false;
    }

    /**
     *是否信开
     * @return
     */
    private boolean isXK() {
        BaseTransactionVo bo = transactionHelpService.getTransactionStoreSet().getTransactionVo();
        Ers ers = sptDataComponent.loadErs(bo);
        //ers为空说明不是来报交易操作（比如主动修改），lx为02是信开
        if (ers != null && "02".equals(ers.getLx())) {
            return true;
        }
        return false;
    }

    /**
     *是否信开并且渠道是深担保
     * @return
     */
    private boolean isXKAndOthersno(String othersno) {
        if(isXK() && MdaUtils.compareTo(othersno, "E0000003") == 0) {
            return true;
        }
        return false;
    }


    public void wfmInitNewEntry(WfmmodVo wfmmodVo) {
        // public
        // Force creating of a new WFS-subject via clearing the INR
        wfmmodVo.getWfs().setInr(NULLSTR);

    }

    public void wfmAdd_EXEDAT(TrnmodVo trnmodVo) {
        wfmmodService.wfeAdd("PDD", "PDD", trnmodVo.getTrn().getExedat(), BigDecimal.valueOf(24));
    }

    public void wfmAdd_Compliance(WfmmodVo wfmmodVo, TrnmodVo trnmodVo) {
        wfmmodService.wfeAdd("CCS", "CCS", Dates.today(), BigDecimal.valueOf(24));
        wfmmodService.wfeAdd("PDC", "PDC", Dates.today(), BigDecimal.valueOf(24));
    }

    /**
     * @param trnmodVo
     */
    public void setWorkFlow(TrnmodVo trnmodVo) {
        Trn trn = trnmodVo.getTrn();
        SettleSession session = SettleContext.getUserSession();
        // public
//        if( MdaUtils.isEmpty(SettleContext.getUserSession().getAtp().getCcstyp()) )
//        {
//        }
//        else
//        {
//            // trigger service entries for compliance check
//            wfmAdd_Compliance();
//        }
        if (!MdaUtils.isEmpty(SettleContext.getUserSession().getAtp().getCcstyp())) {
            wfmAdd_Compliance(trnmodVo.getTrnism().getWfmmod(), trnmodVo);
        }
        wfmmodService.create(trnmodVo.getTrnism().getWfmmod(), "TRN", trn.getInr(), trn.getObjnam());
        // for PAC: immediately try to apply first signature
        String res = trn.getRelres();
//        String relflg = trnsigService.sigUsrRelFlg(trn, session.getUsr().getExtkey(),session.getUsr().getExtkey(),
//                trn.getRelamt(), trn.getRelcur(),trnmodVo.getTrnism().getTrnsig() );
        int destnum = 1;
        // if the current signature is qualified search for an unsatisfied, qualified signature first
        // To forbid signing from creation, if only one signature needed, activate following if (and corresponding endif)
        // if Len( \\TRNMOD\\TRN\\RELREQ ) > 1 then
//        if( MdaUtils.compareTo(relflg,"A") == 0 )
//        {
//            for(int  i = 1;i <= 3;i++)
//            {
//                if( MdaUtils.compareTo(Strings.mid(trn.getRelreq(),i,1),"A") == 0 )
//                {
//                    destnum = i;
//                }
//            }
//        }
//        if( destnum == 0 )
//        {
//            for(int  i = 1;i <= 3;i++)
//            {
//                if( MdaUtils.compareTo(Strings.mid(trn.getRelreq(),i,1),"B") == 0 )
//                {
//                    destnum = i;
//                }
//            }
//        }
//        if(MdaUtils.compareTo(relflg,"A") == 0){
//            String ch = "A";
//            if(!trn.getRelreq().contains(ch)){
//                ch = "B";
//            }
//            destnum = trn.getRelreq().indexOf(ch)+1;
//        }
        // the following is the corresponding endif:
        // endif
//        destnum = 1;
        String setflg = NULLSTR;
        if (destnum > 0) {
            // type of signature to be set
            setflg = Strings.mid(trn.getRelreq(), destnum, 1);
            if (destnum == 1) {
//                Platform.sigWrite("S",trn,"SG1"); // Signed at creation
                trnmodService.sigWrite("S", trn, "SG1"); // Signed at creation
                res = setflg + Strings.mid(res, 2, 2);
            } else if (destnum == 2) {
                trnmodService.sigWrite("S", trn, "SG2");
                res = Strings.mid(res, 1, 1) + setflg + Strings.mid(res, 3, 1);
            } else if (destnum == 3) {
                trnmodService.sigWrite("S", trn, "SG3");
                res = Strings.mid(res, 1, 2) + setflg;
            }
            trn.setRelres(res);
        } else {
            trn.setRelres(Strings.mid(trn.getRelreq(), 1, 1));
        }

    }

    /**
     * 根据机构 级别 币种 金额 ，设置需要的授权级别
     **/
    public void setRelRequest(Trn trn, TrnmodVo trnmodVo, Map<String, Cbb> cbsMap) {
        TrnismVo trnismVo = trnmodVo.getTrnism();
        String relcur = trnmodVo.getTrn().getReloricur();
        BigDecimal relamt = trnmodVo.getTrn().getReloriamt();
        Bch usrbch = SettleContext.getUserSession().getBch();
        Bch orgbch = SettleContext.getUserSession().getOrgBch();
        Atp usratp = SettleContext.getUserSession().getAtp();
        trn.setRelres0("NN");
        trn.setRelres1("NN");
        trn.setRelres2("NN");
        trn.setRelreq2("NN");
        trn.setRelreq1("NN");
        trn.setRelreq0("NN");
        String framename = usratp.getCod();
        String statxt = "";
        Trl trl = trlMapper.trlSelectOne(framename, SettleContext.getUserSession().getBch().getBranch());
        // 取金额
        BigDecimal amtcontent = null;
        if (MdaUtils.isEmpty(relamt) || MdaUtils.compareTo(relamt, 0) == 0) {
            Cbb cbb = cbsMap.getOrDefault("NOM1", cbsMap.get("DOC1"));
            //增加保函开立的取值
            if (MdaUtils.compareTo(framename, "GITODZ") == 0) {
                cbb = cbsMap.get("MAX");//是否标准文本
                statxt = trnmodVo.getStatxt();
            }
            if (cbb != null) {//gitopn获取的cbb对象为空
                trnismVo.setMaxamt(cbb.getAmt());
            }
        }
        BigDecimal amtbase = null;
        BigDecimal rat = null;
        BigDecimal sqrrat = null;
        BigDecimal amt = null;

        if (trl == null) {
            trn.setRelreq("OA");
            if (MdaUtils.compareTo(usrbch.getLev(), "1") == 0) {//分行权限控制
                trn.setRelreq2("NN");
                trn.setRelreq1("NA");
                trn.setRelreq0("NN");
            } else {
                if (MdaUtils.compareTo(usrbch.getLev(), "2") == 0) {        //     支行权限控制
                    trn.setRelreq2("NA");
                    trn.setRelreq1("NN");
                    trn.setRelreq0("NN");
                }
            }
        } else {
            if (MdaUtils.compareTo(usrbch.getLev(), "0") == 0) {   //总行经办的业务只需要总行授权
                trn.setRelreq("OU");
                trn.setRelreq2("NN");
                trn.setRelreq1("NN");
                trn.setRelreq0("BA");
            } else {
                String relflg = trl.getRelflg() == null ? "" : trl.getRelflg();
                switch (relflg) {
                    case "1":                             //     FLAG为1，总是需要总行授权
                        trn.setRelreq("OU");
                        if (MdaUtils.compareTo(usrbch.getLev(), "1") == 0) {//     分行权限控制
                            trn.setRelreq2("NN");
                            trn.setRelreq1("BA");
                            trn.setRelreq0("BA");
                        } else {
                            if (MdaUtils.compareTo(usrbch.getLev(), "2") == 0) {//     支行权限控制
                                Argument<BigDecimal> ratBox = new Argument<BigDecimal>();
                                Argument<BigDecimal> sqrratBox = new Argument<BigDecimal>();
                                xrtmodService.getrat(relcur, trl.getRelcur(), "M", Dates.today(), ratBox, sqrratBox);
                                rat = ratBox.value;
                                amt = xrtmodService.amtcnv(relamt, rat, relcur, trl.getRelcur());
                                amtbase = trl.getRelamt();
                                if (MdaUtils.compareTo(amt, amtbase) < 0) {        //     支行权限内，分行不需要授权
                                    trn.setRelreq2("BA");
                                    trn.setRelreq1("NN");
                                    trn.setRelreq0("BA");
                                } else {                           //     超过支行权限，需要分行授权
                                    trn.setRelreq2("BA");
                                    trn.setRelreq1("BA");
                                    trn.setRelreq0("BA");
                                }
                            }
                        }
                        break;
                    case "2":                             //      FLAG为2，超过一定金额一定须总行看，不超过一定金额一定不须总行看。但是,需要总经理授权
                        //      对于支行的情况，超过一定金额，分行不需要授权，直接由总行授权。
                        amtbase = trl.getRelamt();
                        Argument<BigDecimal> ratBox1 = new Argument<BigDecimal>("", rat);
                        Argument<BigDecimal> sqrratBox1 = new Argument<BigDecimal>("", sqrrat);
                        xrtmodService.getrat(relcur, trl.getRelcur(), "M", Dates.today(), ratBox1, sqrratBox1);
                        rat = ratBox1.value;
                        amt = xrtmodService.amtcnv(relamt, rat, relcur, trl.getRelcur());
                        amtbase = trl.getRelamt();
                        trn.setRelreq("OU");
                        if (MdaUtils.compareTo(amtbase, amt) > 0) {
                            if (MdaUtils.compareTo(usrbch.getLev(), "1") == 0) { //      不超过分行权限，分行总经理授权
                                trn.setRelreq2("NN");
                                trn.setRelreq1("BA");
                                trn.setRelreq0("NN");
                            } else {                                //      超过支行权限，支行总经理授权，总行授权
                                trn.setRelreq2("BA");
                                trn.setRelreq1("NN");
                                trn.setRelreq0("NN");
                            }
                        } else {
                            if (MdaUtils.compareTo(usrbch.getLev(), "1") == 0) {   //      超过分行权限，分行总经理授权，总行授权
                                trn.setRelreq2("NN");
                                trn.setRelreq1("BA");
                                trn.setRelreq0("BA");
                            } else {                               //      超过支行权限，支行总经理授权，总行授权
                                trn.setRelreq2("BA");
                                trn.setRelreq1("NN");
                                trn.setRelreq0("BA");
                            }
                        }
                        break;
                    case "3":                                     //     FLAG为3，不需要总行授权，超过一定金额，经办行总经理授权
                        Argument<BigDecimal> ratBox2 = new Argument<BigDecimal>("", rat);
                        Argument<BigDecimal> sqrratBox2 = new Argument<BigDecimal>("", sqrrat);
                        xrtmodService.getrat(relcur, trl.getRelcur(), "M", Dates.today(), ratBox2, sqrratBox2);
                        rat = ratBox2.value;
                        amt = xrtmodService.amtcnv(relamt, rat, relcur, trl.getRelcur());
                        amtbase = trl.getRelamt();
                        if (MdaUtils.compareTo(amtbase, amt) > 0) {
                            trn.setRelreq("OA");
                            if (MdaUtils.compareTo(usrbch.getLev(), "1") == 0) {//分行权限控制
                                trn.setRelreq2("NN");
                                trn.setRelreq1("NA");
                                trn.setRelreq0("NN");
                            } else {
                                if (MdaUtils.compareTo(usrbch.getLev(), "2") == 0) { //     支行权限控制
                                    trn.setRelreq2("NA");
                                    trn.setRelreq1("NN");
                                    trn.setRelreq0("NN");
                                }
                            }
                        } else {
                            trn.setRelreq("OU");
                            if (MdaUtils.compareTo(usrbch.getLev(), "1") == 0) {   //      分行总经理授权
                                trn.setRelreq2("NN");
                                trn.setRelreq1("BA");
                                trn.setRelreq0("NN");
                            } else {
                                if (MdaUtils.compareTo(usrbch.getLev(), "2") == 0) {                      //      支行总经理授权
                                    trn.setRelreq2("BA");
                                    trn.setRelreq1("NN");
                                    trn.setRelreq0("NN");
                                }
                            }
                        }
                        break;
                    case "B":                                           //      FLAG为B，支行满足条件需要总经理以及分行授权
                        Argument<BigDecimal> ratBox3 = new Argument<BigDecimal>("", rat);
                        Argument<BigDecimal> sqrratBox3 = new Argument<BigDecimal>("", sqrrat);
                        xrtmodService.getrat(relcur, trl.getRelcur(), "M", Dates.today(), ratBox3, sqrratBox3);
                        rat = ratBox3.value;
                        amt = xrtmodService.amtcnv(relamt, rat, relcur, trl.getRelcur());
                        amtbase = trl.getRelamt();
                        trn.setRelreq("OU");
                        trn.setRelreq0("NN");
                        if (MdaUtils.compareTo(amtbase, amt) > 0) {
                            if (MdaUtils.compareTo(usrbch.getLev(), "1") == 0) {
                                trn.setRelreq1("BA");
                                trn.setRelreq2("NN");
                            }
                            if (MdaUtils.compareTo(usrbch.getLev(), "2") == 0) {
                                trn.setRelreq2("BA");
                                trn.setRelreq1("NN");
                            }
                        } else {
                            if (MdaUtils.compareTo(usrbch.getLev(), "1") == 0) {
                                trn.setRelreq1("BA");
                                trn.setRelreq2("NN");
                            }
                            if (MdaUtils.compareTo(usrbch.getLev(), "2") == 0) {
                                trn.setRelreq2("BA");
                                trn.setRelreq1("BA");
                            }
                        }
                        break;
                    case "4":                          //     FLAG为4，只需要OB俩人即可授权业务
                        trn.setRelreq("OB");
                        if (MdaUtils.compareTo(usrbch.getLev(), "1") == 0) {      //     分行权限控制
                            trn.setRelreq2("NN");
                            trn.setRelreq1("NB");
                            trn.setRelreq0("NN");
                        } else {
                            if (MdaUtils.compareTo(usrbch.getLev(), "2") == 0) {  //     支行权限控制
                                trn.setRelreq2("NB");
                                trn.setRelreq1("NN");
                                trn.setRelreq0("NN");
                            }
                        }
                        break;
                    case "5":                       //     FLAG为5，总是需要总行授权
                        trn.setRelreq("OU");
                        if (MdaUtils.compareTo(usrbch.getLev(), "1") == 0) {      //     分行权限控制
                            trn.setRelreq2("NN");
                            trn.setRelreq1("NA");
                            trn.setRelreq0("BA");
                        } else {
                            if (MdaUtils.compareTo(usrbch.getLev(), "2") == 0) {    //     支行权限控制
                                Argument<BigDecimal> ratBox4 = new Argument<BigDecimal>("", rat);
                                Argument<BigDecimal> sqrratBox4 = new Argument<BigDecimal>("", sqrrat);
                                xrtmodService.getrat(relcur, trl.getRelcur(), "M", Dates.today(), ratBox4, sqrratBox4);
                                rat = ratBox4.value;
                                amt = xrtmodService.amtcnv(relamt, rat, relcur, trl.getRelcur());
                                amtbase = trl.getRelamt();
//                                if (MdaUtils.compareTo(amt, amtbase) <= 0) {        //     支行权限内，分行不需要授权
                                trn.setRelreq2("NA");
                                trn.setRelreq1("NN");
                                trn.setRelreq0("BA");
//                                } else {                           //     超过支行权限，需要分行授权
//                                    trn.setRelreq2("NA");
//                                    trn.setRelreq1("BA");
//                                    trn.setRelreq0("BA");
//                                }
                            }
                        }
                        break;
                    case "6":
                        if (MdaUtils.compareTo(usrbch.getLev(), "1") == 0) {
                            if (MdaUtils.compareTo(framename, "GITODZ") == 0) {
                                amt = cbsMap.get("MAX").getAmt();
                            } else {
                                Argument<BigDecimal> ratBox5 = new Argument<BigDecimal>("", rat);
                                Argument<BigDecimal> sqrratBox5 = new Argument<BigDecimal>("", sqrrat);
                                xrtmodService.getrat(relcur, trl.getRelcur(), "M", Dates.today(), ratBox5, sqrratBox5);
                                rat = ratBox5.value;
                                amt = xrtmodService.amtcnv(relamt, rat, relcur, trl.getRelcur());
                            }
                            amtbase = trl.getRelamt();         //分行授权
                            if (MdaUtils.compareTo(amtbase, amt) <= 0 || MdaUtils.compareTo(statxt, "N") == 0) {            //超过授权金额 总行复核
                                trn.setRelreq("OU");           //未超过授权金额 分行经办复核
                                trn.setRelreq2("NN");
                                trn.setRelreq1("NA");
                                trn.setRelreq0("BA");
                            } else {
                                trn.setRelreq("OB");
                                trn.setRelreq2("NN");
                                trn.setRelreq1("NB");
                                trn.setRelreq0("NN");
                            }
                        }
                        break;
                    default:
                        trn.setRelreq("OA");
                        if (MdaUtils.compareTo(usrbch.getLev(), "1") == 0) {      //     分行权限控制
                            trn.setRelreq2("NN");
                            trn.setRelreq1("NA");
                            trn.setRelreq0("NN");
                        } else {
                            if (MdaUtils.compareTo(usrbch.getLev(), "2") == 0) {  //     支行权限控制
                                trn.setRelreq2("NA");
                                trn.setRelreq1("NN");
                                trn.setRelreq0("NN");
                            }
                        }
                }
            }
        }

        //单证中心上收之后 总行经办处理
        if (MdaUtils.compareTo(orgbch.getLev(), "0") == 0) {//总行权限
            trn.setRelreq2("NN");
            trn.setRelreq1("NN");
            trn.setRelreq0("NA");
        }
        //高新投免审库保函文本权限下放
        if (trnmodVo.getSpt() != null && "-S61-S62-S63-".indexOf(trnmodVo.getSpt().getMsgtyp()) > 0
                && SettleContext.getTransName().startsWith("NITOPN")) {
            String gtxref = Utils.getFieldValue(trnmodVo.getSwiadd(), "gtxref");
            Object rec = transactionHelpService.getTransactionStoreSet().getRec();
            String gtxinr = Utils.getFieldValue(rec, "gtxinr");
            String gtxextkey = "";
            Gtx gtx = new Gtx();
            gtx.setInr(gtxinr);
            gtx = gtxMapper.selectByPrimaryKey(gtx);
            if (gtx != null) {
                gtxextkey = gtx.getExtkey();
            }
            String gidtxtmodflg = Utils.getFieldValue(rec, "gidtxtmodflg");
            List<String> gtxreflst = codetableService.getTblKeyList("SZGXT");
            // String gtxreflst="-GXT202206001-GXT202206002-GXT202206003-GXT202206004-GXT202206005-GXT202206006-GXT202206007-GXT202206008-GXT202206009-GXT202206010-GXT202206011-GXT202206012-GXT202206013-GXT202206014-GXT202206015-GXT202206016-GXT202206017-";
            if (gtxreflst.contains(gtxextkey)) {
                if (Objects.equals(gidtxtmodflg, "X")) {
                } else {
                    if (MdaUtils.compareTo(relamt, 5000000) <= 0) {
                        trn.setRelreq("OA");
                        if (MdaUtils.compareTo(usrbch.getLev(), "1") == 0) {// 分行权限控制
                            trn.setRelreq2("NN");
                            trn.setRelreq1("NA");
                            trn.setRelreq0("NN");
                        } else {
                            if (MdaUtils.compareTo(usrbch.getLev(), "2") == 0) { //支行权限控制
                                trn.setRelreq2("NA");
                                trn.setRelreq1("NN");
                                trn.setRelreq0("NN");
                            }
                        }
                    }
                }
            }
        }
    }


    /**
     * source:trnism.0010.script
     */
    public void wfmAdd_LimitCheck(WfmmodVo wfmmod, TrnmodVo trnmod) {
        // public
        // Create WorkflowEntries for Limit Check
        wfmmodService.wfeAdd("PDL", "PDL", Dates.add(Dates.today(), 1), BigDecimal.valueOf(21));
    }

    /**
     * source:trnism.0040.script
     */
    public boolean checkAgency() {
        //判断是否为跨境人名币代理行模式
        BizInfoVo bizInfoVo = transactionHelpService.getTransactionStoreSet().getSetmodVo().getRec();
        TrnmodVo trnmodVo = transactionHelpService.getTransactionStoreSet().getTrnmodVo();
        String transName = SettleContext.getDirectTransName();
        String liqtyp = "";
        if ("BCTSET".equals(transName) || "GITSET".equals(transName) || "BRTSET".equals(transName)) {
            liqtyp = trnmodVo.getCmtflg();
        }
        if ("CPTOPN".equals(transName) || "CPTECO".equals(transName)) {
            liqtyp = Utils.getFieldValue(bizInfoVo, "liqtyp");
        }
        if ("NITSET".equals(transName) || "NITPSE".equals(transName)) {
            liqtyp = Utils.getFieldValue(bizInfoVo, "cmtflg");
        }
        if ("C".equals(liqtyp)) {
            return true;
        }
        return false;
    }

    public Object getRec() {
        return transactionHelpService.getTransactionStoreSet().getRec();
    }


}
