package com.brilliance.isc.common.transaction.newcheck.setmod;

import com.brilliance.isc.bo.*;
import com.brilliance.isc.common.api.settle.service.SettleDetailApiService;
import com.brilliance.isc.common.api.settle.vo.AccountRequestVo;
import com.brilliance.isc.common.api.settle.vo.AccountResponseVo;
import com.brilliance.isc.common.cache.pty.PtyCacheService;
import com.brilliance.isc.common.cbsmod.service.XrtmodService;
import com.brilliance.isc.common.component.ActmodComponent;
import com.brilliance.isc.common.component.PtsptaToolComponent;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.glemod.service.GlemodService;
import com.brilliance.isc.common.oit.service.OitService;
import com.brilliance.isc.common.setglg.vo.ActProVo;
import com.brilliance.isc.common.setgll.service.SetgllService;
import com.brilliance.isc.common.setgll.vo.SetgllVo;
import com.brilliance.isc.common.setmod.service.SetmodService;
import com.brilliance.isc.common.setmod.vo.SetmodVo;
import com.brilliance.isc.common.transaction.newcheck.*;
import com.brilliance.isc.common.util.StringUtils;
import com.brilliance.isc.common.util.Utils;
import com.brilliance.isc.common.vo.ActVo;
import com.brilliance.isc.common.vo.PtsptaVo;
import com.brilliance.isc.mda.dao.BopcodMapper;
import com.brilliance.isc.mda.dao.CnyctyMapper;
import com.brilliance.isc.mda.dao.PublicMapper;
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.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

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

@Component
public class SetgllValidator extends AbstractModuleValidator<SetgllVo> {

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

    @Autowired
    private XrtmodService xrtmodService;

    @Autowired
    private SetmodService setmodService;

    @Autowired
    private SetgllService setgllService;

    @Autowired
    private ActmodComponent actmodComponent;

    @Autowired
    private GlemodService glemodService;

    @Autowired
    private PublicMapper publicMapper;

    @Autowired
    private CnyctyMapper cnyctyMapper;

    @Autowired
    private BopcodMapper bopcodMapper;

    @Autowired(required = false)
    private OitService oitService;

    @Autowired
    private SespayValidator sespayValidator;

    @Autowired
    private SettleDetailApiService settleDetailApiService;

    @Autowired
    private PtsptaToolComponent ptsptaToolComponent;

    @Autowired
    private PtyCacheService ptyCacheService;

    @Autowired
    private CipmodValidator cipmodValidator;

    @Autowired
    private FxpmodValidator fxpmodValidator;

    @Override
    public Map<String, String> validate(SetgllVo vo, int index) {
        Map<String, String> maps = new HashMap<>();
        maps.putAll(super.validate(vo, index));
        sespayValidator.registerParent(vo);
        maps.putAll(sespayValidator.validate(vo.getSespay(), index));
        sespayValidator.removeTl();
        if ("CIV".equals(vo.getSftmt()) || "CIU".equals(vo.getSftmt())) {
            cipmodValidator.registerParent(vo);
            maps.putAll(cipmodValidator.validate(vo.getSespay().getCipmod(), index));
            cipmodValidator.removeTl();
        }
        if ("111".equals(vo.getSftmt()) || "112".equals(vo.getSftmt())) {
            maps.putAll(fxpmodValidator.validate(vo.getSespay().getFxpmod(), index));
        }
        return maps;
    }

    /**
     * source:setgll.0000.script
     */
    @Validate(path = "setmod.setglg.setgll.%s.acccur")
    public ValidResult checkAcccur(SetgllVo setgllVo) {
        if (MdaUtils.compareTo(setgllVo.getAccamt(), 0) != 0) {
            if (MdaUtils.isEmpty(setgllVo.getAcccur())) {
                return ValidResult.build(false, "This field is mandatory");
            }
        }
        return null;
    }

    /**
     * source:setgll.0001.script
     */
    @Validate(path = "setmod.setglg.setgll.%s.act")
    public ValidResult checkAct(SetgllVo setgllVo) {
        if (MdaUtils.compareTo(setgllVo.getAccamt(), 0) != 0) {
            if (MdaUtils.isEmpty(setgllVo.getAct())) {
                return ValidResult.build(false, "This field is mandatory");
            }
        }
        return null;
    }

    /**
     * source:setgll.0002.script
     */
    @Validate(path = "setmod.setglg.setgll.%s.valdat")
    public ValidResult checkValdat(SetgllVo setgllVo) {
        //! value date is mandatory in final settlements (if Settlement is visible only)
        if (MdaUtils.isEmpty(setgllVo.getValdat()) && (!MdaUtils.isEmpty(setgllVo.getAmt()))) {
            return ValidResult.build(false, "This field is mandatory");
        }
        return null;
    }

    /**
     * source:setgll.0005.script
     */
    @Validate(path = "setmod.setglg.setgll.%s.setgrp.int.pts.extkey")
    public ValidResult checkSetgrpIntPtsExtkey(SetgllVo setgllVo) {
        //! the addresses in the payment chain have to be different
        if (!MdaUtils.isEmpty(setgllVo.getSetgrp().getTmpint().getPtyextkey()) && MdaUtils.compareTo(setgllVo.getSetgrp().getTmpint().getPtyextkey(), setgllVo.getSetgrp().getRol().getPtyextkey()) == 0) {
            return ValidResult.build(false, "Payee and intermediary bank may not be the same");
        }
        if (!MdaUtils.isEmpty(setgllVo.getSetgrp().getTmpint().getPtyextkey()) && MdaUtils.compareTo(setgllVo.getSetgrp().getTmpint().getPtyextkey(), setgllVo.getSetgrp().getAwi().getPtyextkey()) == 0) {
            return ValidResult.build(false, "Account with institution and intermediary bank may not be the same");
        }
        if (!MdaUtils.isEmpty(setgllVo.getSetgrp().getTmpint().getPtyextkey()) && MdaUtils.compareTo(setgllVo.getSetgrp().getTmpint().getPtyextkey(), setgllVo.getSetgrp().getApy().getPtyextkey()) == 0) {
            return ValidResult.build(false, "Account party and intermediary bank may not be the same");
        }
        return null;
    }

    /**
     * source:setgll.0015.script
     */
    @Validate(path = "setmod.setglg.setgll.%s.dcbextrat")
    public ValidResult checkDcbextrat(SetgllVo setgllVo) {
        if (MdaUtils.compareTo(setgllVo.getDcbrattyp(), "F") == 0) {
            try {
                xrtmodService.xrtCheckRate(setgllVo.getDcbextrat(), setgllVo.getCur(), setgllVo.getValdat());
            } catch (Exception e) {
                return ValidResult.build(false, e.getMessage());
            }
            if (MdaUtils.compareTo(setgllVo.getRattyp(), "R") == 0 && !setgllVo.isModified("dcbextrat")) {
                return ValidResult.build(false, "Please type in the reserved rate");
            }
        }
        return null;
    }

    /**
     * source:setgll.0016.script
     */
    @Validate(path = "setmod.setglg.setgll.%s.scbextrat")
    public ValidResult checkScbextrat(SetgllVo setgllVo) {
        if (MdaUtils.compareTo(setgllVo.getScbrattyp(), "F") == 0) {
            try {
                xrtmodService.xrtCheckRate(setgllVo.getScbextrat(), setgllVo.getAcccur(), setgllVo.getValdat());
            } catch (Exception e) {
                return ValidResult.build(false, e.getMessage());
            }
            if (MdaUtils.compareTo(setgllVo.getRattyp(), "R") == 0 && !setgllVo.isModified("scbextrat")) {
                return ValidResult.build(false, "Please type in the reserved rate");
            }
        }
        return null;
    }

    /**
     * source:setgll.0019.script
     */
    @Validate(path = "setmod.setglg.setgll.%s.rat")
    public ValidResult checkRat(SetgllVo setgllVo) {
        if (MdaUtils.compareTo(Strings.mid(setgllVo.getRattyp(), 2, 1), "X") == 0) {
            String cur1 = Strings.mid(setgllVo.getRatcurlab(), 1, 3);
            String cur2 = Strings.mid(setgllVo.getRatcurlab(), 5, 3);
            if (MdaUtils.compareTo(cur1, xrtmodService.basRatCur()) != 0) {
                try {
                    xrtmodService.xrtCheckRate(setgllVo.getDcbextrat(), setgllVo.getCur(), setgllVo.getValdat());
                } catch (Exception e) {
                    return ValidResult.build(false, e.getMessage());
                }
            }
            if (MdaUtils.compareTo(cur2, xrtmodService.basRatCur()) != 0) {
                try {
                    xrtmodService.xrtCheckRate(setgllVo.getScbextrat(), setgllVo.getAcccur(), setgllVo.getValdat());
                } catch (Exception e) {
                    return ValidResult.build(false, e.getMessage());
                }
            }
            if (MdaUtils.compareTo(setgllVo.getRattyp(), "RX") == 0 && !setgllVo.isModified("rat")) {
                return ValidResult.build(false, "Please type in the reserved rate");
            }
        }
        return null;
    }

    /**
     * source:setgll.0027.script
     */
    @Warning(path = "setmod.setglg.setgll.%s.valdat")
    public WarningResult checkValdatN1500(SetgllVo setgllVo) {
        if (!MdaUtils.isEmpty(setgllVo.getAcccur()) && !MdaUtils.isEmpty(setgllVo.getValdat())) {
            if (setmodService.isHoliday(setgllVo.getAcccur(), setgllVo.getValdat())) {
                return WarningResult.build("SubCheckVALDAT", Strings.getText("结算有效日期$1是节假日", Utils.fmtDate(setgllVo.getValdat(), "yyyy-MM-dd")));
            }
        }
        return WarningResult.buildEmpty("SubCheckVALDAT");
    }

    /**
     * source:setgll.0027.script
     */
    @Warning(path = "setmod.setglg.setgll.%s.valdat")
    public WarningResult checkValdatN2000(SetgllVo setgllVo) {
        if (!MdaUtils.isEmpty(setgllVo.getAcccur()) && !MdaUtils.isEmpty(setgllVo.getValdat())) {
            if (Dates.diff(setgllVo.getValdat(), Dates.today()) < 0) {
                return WarningResult.build("VALDATPASWRN", getMessage("0141", Utils.fmtDate(setgllVo.getValdat())));
            }
        }
        return WarningResult.buildEmpty("VALDATPASWRN");
    }

    /**
     * source:setgll.0043.script
     */
    @Validate(path = "setmod.setglg.setgll.%s.setgrp.act.pts.extkey")
    public ValidResult checkSetgrpActPtsExtkey(SetgllVo setgllVo) {
        if (MdaUtils.compareTo(Strings.mid(setgllVo.getDsp(), 1, 2), "PA") == 0
                || MdaUtils.compareTo(setgllVo.getDsp(), "RTC") == 0
                || MdaUtils.compareTo(setgllVo.getDsp(), "TGC") == 0) {
            if (setgllVo.getSetgrp().getAct().getAdr() != null && !MdaUtils.isEmpty(Strings.mid(setgllVo.getSetgrp().getAct().getAdr().getBic(), 1, 4))) {
                if (MdaUtils.compareTo(Strings.mid(setgllVo.getSetgrp().getAct().getAdr().getBic(), 1, 4), Strings.mid(SettleContext.getOwnPts().getAdr().getBic(), 1, 4)) == 0) {
                    return ValidResult.build(false, "Please do not use our branches here");
                }
            }
            if (setgllVo.getSetgrp().getAct().getPta() != null && MdaUtils.compareTo(setgllVo.getSetgrp().getAct().getPta().getPtyinr(), setgllVo.getSetgrp().getRol().getPta().getPtyinr()) == 0 && (!MdaUtils.isEmpty(setgllVo.getSetgrp().getRol().getPta().getPtyinr()))) {
                if (MdaUtils.compareTo(setgllVo.getAmt(), 0) > 0) {
                    return ValidResult.build(false, "Please use direct account relationship for Payer (if any)");
                } else {
                    return ValidResult.build(false, "Please use direct account relationship for Payee (if any)");
                }
            }
        }
        return null;
    }

    /**
     * source:setgll.0048.script
     */
    @Validate(path = "setmod.setglg.setgll.%s.dsp")
    public ValidResult checkDsp(SetgllVo setgllVo) {
        /** Currencies may be restricted to currencies for special disposition codes
         if ( ACCAMT <> 0 ) and ( DSP == "BRF" ) then
         if ACCCUR <> SYSISO then
         ErrorOnField( DSP, 'LF000081', DSP, SYSISO )
         endif
         endif
         **/
        if (MdaUtils.compareTo(setgllVo.getAccamt(), 0) != 0 && MdaUtils.isEmpty(setgllVo.getDsp())) {
            return ValidResult.build(false, "This field is mandatory");
        }
        if (MdaUtils.compareTo(setgllVo.getDsp(), "RTC") == 0 || MdaUtils.compareTo(setgllVo.getDsp(), "TGC") == 0) {
            String rtgrol = "";
            Argument<String> rtgrolBox = new Argument<String>("", rtgrol);
            setgllService.getRelevantRtgRol(setgllVo, setgllVo.getDsp(), rtgrolBox);
            rtgrol = rtgrolBox.value;
            if (MdaUtils.isEmpty(rtgrol)) {
                return ValidResult.build(false, Strings.formatText("Disposition $1 not allowed for actual addresses", setgllVo.getDsp()));
            }
        }
        return null;
    }

    /**
     * source:setgll.0054.script
     */
    @Validate(path = "setmod.setglg.setgll.%s.act")
    public ValidResult checkActN980(SetgllVo setgllVo) {
        //! *E-Accounts are not permitted as settlement accounts
        if (MdaUtils.compareTo(Strings.mid(setgllVo.getAct(), 1, 2), "*E") == 0) {
            return ValidResult.build(false, "No account found in database.\nPlease add account to database first.");
        }
        return null;
    }

    /**
     * source:setgll.0058.script
     */
    @Validate(path = "setmod.setglg.setgll.%s.acccur")
    public ValidResult checkAcccurN1200(SetgllVo setgllVo) {
        //! Check if rate is available
        if (!MdaUtils.isEmpty(setgllVo.getAcccur())) {
            if (MdaUtils.compareTo(setgllVo.getAcccur(), setgllVo.getBascur()) != 0) {
                Cur cur = new Cur();
                Xrt xrt = new Xrt();
                xrtmodService.getXrt(setgllVo.getAcccur(), Dates.today(), cur, xrt);
                if (MdaUtils.isEmpty(xrt.getMidrat())) {
                    return ValidResult.build(false, Strings.formatText("No exchange rate for $1 stored, please update the exchange rate first", setgllVo.getAcccur()));
                }
            }
        }
        return null;
    }

    /**
     * source:setgll.0059.script
     */
    @Validate(path = "setmod.setglg.setgll.%s.cur")
    public ValidResult checkCurN1200(SetgllVo setgllVo) {
        //! Check if rate is available
        if (!MdaUtils.isEmpty(setgllVo.getCur())) {
            if (MdaUtils.compareTo(setgllVo.getCur(), setgllVo.getBascur()) != 0) {
                Cur cur = new Cur();
                Xrt xrt = new Xrt();
                xrtmodService.getXrt(setgllVo.getCur(), Dates.today(), cur, xrt);
                if (MdaUtils.isEmpty(xrt.getMidrat())) {
                    return ValidResult.build(false, Strings.formatText("No exchange rate for $1 stored, please update the exchange rate first", setgllVo.getCur()));
                }
            }
        }
        return null;
    }

    /**
     * source:setgll.0061.script
     */
    @Validate(path = "setmod.setglg.setgll.%s.rol")
    public ValidResult checkRolN950(SetgllVo setgllVo) {
        //---起头交易时候客户没有STOP，做后续交易时候客户STOP了仍然可以继续交易---
        /**
         //! Check OIT of paying party
         // For outgoing payments (credits) level threshhold is 2 (stop outgoing payments) else 3
         if AMT < 0 then
         $StpLev = "2"
         else
         $StpLev = "3"
         endif
         $WrnPfx = GetAttributeText( SETGRP\\ROL\\PTS\\PTYINR, tdAttrFullName )
         $ErrTxt = OitSysWrnForObj( "PTY", SETGRP\\ROL\\PTS\\PTYINR, GetText( "$1 $2", GetTblText( ROL, "ROLALL", GetUIL ), SETGRP\\ROL\\PTS\\EXTKEY ), $StpLev, $WrnPfx )
         // block transaction process by setting a error message
         if not IsEmpty( $ErrTxt ) then
         Error( "$1", $ErrTxt )
         endi**/
        return null;
    }

    /**
     * source:setgll.0062.script
     */
    //@Validate(path = "setmod.setglg.setgll.%s.trdint")
    @Validate(path = "setmod.setglg.setgll.%s.modflg")
    public ValidResult checkTrdint(SetgllVo setgllVo) {
        // Add by ZJ on Feb 2008
        // 从5.01移植
        String rmb = xrtmodService.sysiso();
        if (((MdaUtils.compareTo(setgllVo.getAcccur(), rmb) == 0 && MdaUtils.compareTo(setgllVo.getCur(), rmb) != 0 && MdaUtils.compareTo(setgllVo.getAmt(), 0) < 0)
                || (MdaUtils.compareTo(setgllVo.getAcccur(), rmb) != 0 && MdaUtils.compareTo(setgllVo.getCur(), rmb) == 0 && MdaUtils.compareTo(setgllVo.getAmt(), 0) > 0))) {
            if (MdaUtils.isEmpty(setgllVo.getTrdint())) {
                return ValidResult.build(false, "请选择结汇方式代码!");
            }
        }
        return null;
    }

    /**
     * source:setgll.0063.script
     */
    //@Validate(path = "setmod.setglg.setgll.%s.trdout")
    @Validate(path = "setmod.setglg.setgll.%s.modflg")
    public ValidResult checkTrdout(SetgllVo setgllVo) {
        // Add by ZJ on Feb 2008
        // 从5.01移植
        String rmb = xrtmodService.sysiso();
        if (((MdaUtils.compareTo(setgllVo.getAcccur(), rmb) == 0 && MdaUtils.compareTo(setgllVo.getCur(), rmb) != 0 && MdaUtils.compareTo(setgllVo.getAmt(), 0) > 0) || (MdaUtils.compareTo(setgllVo.getAcccur(), rmb) != 0 && MdaUtils.compareTo(setgllVo.getCur(), rmb) == 0 && MdaUtils.compareTo(setgllVo.getAmt(), 0) < 0))) {
            if (MdaUtils.isEmpty(setgllVo.getTrdout())) {
                return ValidResult.build(false, "请选择售汇方式代码!");
            }
        }
        return null;
    }

    /**
     * source:setgll.0064.script
     */
    //@Validate(path = "setmod.setglg.setgll.%s.trnman")
    @Validate(path = "setmod.setglg.setgll.%s.modflg")
    public ValidResult checkTrnman(SetgllVo setgllVo) {
        // Add by ZJ on Feb 2008
        // 从5.01移植
        String rmb = xrtmodService.sysiso();
        if (((MdaUtils.compareTo(setgllVo.getAcccur(), rmb) == 0 && MdaUtils.compareTo(setgllVo.getCur(), rmb) != 0 && MdaUtils.compareTo(setgllVo.getAmt(), 0) < 0)
                || (MdaUtils.compareTo(setgllVo.getAcccur(), rmb) != 0 && MdaUtils.compareTo(setgllVo.getCur(), rmb) == 0 && MdaUtils.compareTo(setgllVo.getAmt(), 0) > 0))
                || ((MdaUtils.compareTo(setgllVo.getAcccur(), rmb) == 0 && MdaUtils.compareTo(setgllVo.getCur(), rmb) != 0 && MdaUtils.compareTo(setgllVo.getAmt(), 0) > 0)
                || (MdaUtils.compareTo(setgllVo.getAcccur(), rmb) != 0 && MdaUtils.compareTo(setgllVo.getCur(), rmb) == 0 && MdaUtils.compareTo(setgllVo.getAmt(), 0) < 0))) {
            if (MdaUtils.isEmpty(setgllVo.getTrnman())) {
                return ValidResult.build(false, "请选择交易主体!");
            }
        }
        return null;
    }

    /**
     * source:setgll.0070.script
     */
    @Validate(path = "setmod.setglg.setgll.%s.acttrmtyp", enable = "N")
    public ValidResult checkActtrmtyp(SetgllVo setgllVo) {
        if (!MdaUtils.isEmpty(setgllVo.getAct())) {
            if (MdaUtils.isEmpty(setgllVo.getActtrmtyp())) {
                return ValidResult.build(false, "This field is mandatory");
            }
        }
        return null;
    }

    /**
     * source:setgll.0072.script
     */
    @Validate(path = "setmod.setglg.setgll.%s.act")
    public ValidResult checkActN2000(SetgllVo setgllVo) {
        String trmCod = NULLSTR;
        String err = NULLSTR;
        String trmtyp = NULLSTR;
        if ((MdaUtils.isEmpty(setgllVo.getAct())) && (MdaUtils.compareTo(setgllVo.getAccamt(), 0) != 0)) {
            return ValidResult.build(false, "此字段为必填字段");
        } else {
            if (!MdaUtils.isEmpty(setgllVo.getActtrmtyp())) {
                List<Trm> trmList = actmodComponent.getActTermCode(setgllVo.getDsp());
                if (trmList != null) {
                    Optional<Trm> trm = trmList.stream().filter(vo -> setgllVo.getActtrmtyp().equals(vo.getTrmcod())).findFirst();
                    if (trm.isPresent()) {
                        trmCod = trm.get().getTrmtyp();
                    }
                }
            }
            Argument<String> errBox = new Argument<String>();
            if ((MdaUtils.compareTo(setgllVo.getAccamt(), 0) != 0) && !actmodComponent.checkAccount(setgllVo.getAct(), trmCod, setgllVo.getBranch(), setgllVo.getAcccur(), setgllVo.getActflg(), errBox)) {
                err = errBox.value;
                return ValidResult.build(false, err);
            }
            //---1115刘文清，内部帐都是17位，大于17的都是客户帐，88代表对公账户---
            if (MdaUtils.compareTo(setgllVo.getDsp(), "CCD") != 0) {
                /**
                 if IsEmpty( AccCode ) and ( ( Len( ACT ) == 17 and Mid( ACT, 7, 2 ) == "88" ) or Len( ACT ) > 17 or Mid( ACT, 1, 2 ) == "FT" ) then
                 **/
                if (MdaUtils.isEmpty(setgllVo.getAcccode()) && ((Strings.len(setgllVo.getAct()) == 17 && (MdaUtils.compareTo(Strings.mid(setgllVo.getAct(), 7, 2), "88") == 0 || MdaUtils.compareTo(Strings.mid(setgllVo.getAct(), 7, 2), "80") == 0)) || Strings.len(setgllVo.getAct()) > 17 || MdaUtils.compareTo(Strings.mid(setgllVo.getAct(), 1, 2), "FT") == 0 || MdaUtils.compareTo(Strings.mid(setgllVo.getAct(), 1, 2), "EF") == 0)) {
                    trmtyp = glemodService.actGetTRMCOD(setgllVo.getAct());
                    if (MdaUtils.isEmpty(trmtyp)) {
                        return ValidResult.build(false, "请点击获取按钮获取核心业务代号!");
                    }
                }
            }
        }
        return null;
    }

    /**
     * source:setgll.0073.script
     */
    @Validate(path = "setmod.setglg.setgll.%s.act")
    public ValidResult checkActN10000(SetgllVo setgllVo) {
        String info = SettleContext.getDirectTransName();
        /**
         $m = Mid( ACT, 7, 2 )
         **/
        String m = "";
        if (Strings.len(setgllVo.getAct()) == 17) {
            m = Strings.mid(setgllVo.getAct(), 7, 2);
        }
        if (Strings.len(setgllVo.getAct()) == 21) {
            m = Strings.mid(setgllVo.getAct(), 11, 2);
        }
        if (MdaUtils.compareTo(Strings.mid(setgllVo.getAct(), 1, 3), "ZMQ") == 0 || MdaUtils.compareTo(Strings.mid(setgllVo.getAct(), 1, 3), "NRA") == 0) {
            m = Strings.mid(setgllVo.getAct(), 10, 2);
        }
        /**
         if Mid( ACT, 1, 2 ) == "FT" then
         **/
        //陆婷(010949) 2018-03-21 16:09
        //NRA就是普通17位前面加NRA
        //然后就是自贸 自贸是
        //FT*+开户机构+3693（核算机构）+序号
        //自贸子帐户是ZMQ+17位
        //FTE FTN FTU FTI
        //没有两个字母的
        /**
         if Mid( ACT, 1, 2 ) == "FT" and Pos( "0123456789", Mid( ACT, 3, 1 ) ) - 1 >= 0 then
         $m = Mid( ACT, 9, 2 )
         endif
         **/
        String num = "";
        if (MdaUtils.compareTo(m, "83") == 0) {
            /**
             if ( $info == "BPTOPN" ) and ( $dspn <> "NFR" and $dspn <> "BEF" and $dspn <> "BDF" ) then
             ErrorOnField( ACT, 'LG000181' )
             endif
             //  if $info = "BPTOPN" and $dspn <> "ITF" then
             //    ErrorOnField( ACT, 'RG000184
             //  endif
             //add by zt 20080128
             //BDF类型也可以为83帐号
             // if ( $info = "BRTUTL" or $info = "BRTSUS" or $info = "BCTSET" or $info = "BCTSUS" or $info = "TRTOPN" ) and $dspn <> "TRF" then
             if ( $info == "BRTSET" or $info == "BCTSET" or $info == "TRTOPN" ) and ( $dspn <> "TRF" and $dspn <> "BDF" ) then
             ErrorOnField( ACT, 'LG000183' )
             endif
             **/
            return ValidResult.build(false, "融资账号不能直接在国结系统中记账!");
        }
        //校验借记账号为88的业务代号当为LITENG,BRTSET,GITENG,GITSET,DITENG,BDTSET #交易时账号为AAAABB88XXXXXXXXX，借方，disp不为CCD,则必须调用核心8091交易
        String yewudh = NULLSTR;
        if ((MdaUtils.compareTo(info, "LITENG") == 0
                || MdaUtils.compareTo(info, "BRTSET") == 0
                || MdaUtils.compareTo(info, "GITENG") == 0
                || MdaUtils.compareTo(info, "NITENG") == 0
                || MdaUtils.compareTo(info, "GITSET") == 0
                || MdaUtils.compareTo(info, "NITSET") == 0
                || MdaUtils.compareTo(info, "GITPSE") == 0
                || MdaUtils.compareTo(info, "NITPSE") == 0
                || MdaUtils.compareTo(info, "DITENG") == 0
                || MdaUtils.compareTo(info, "BDTSET") == 0)
                && MdaUtils.compareTo(setgllVo.getDebcdtflg(), "D") == 0) {
            if (MdaUtils.compareTo(setgllVo.getDsp(), "CCD") != 0) {
                if (Strings.len(setgllVo.getAct()) == 17) {
                    num = Strings.mid(setgllVo.getAct(), 7, 2);
                    /**
                     if $num == "88" and AccCode == "" then
                     **/
                    if ((MdaUtils.compareTo(num, "88") == 0 || MdaUtils.compareTo(num, "80") == 0) && MdaUtils.compareTo(setgllVo.getAcccode(), "") == 0) {
                        return ValidResult.build(false, "请点击获取按钮获取核心业务代号");
                    }
                }
                if (Strings.len(setgllVo.getAct()) == 21) {
                    yewudh = Strings.mid(setgllVo.getAct(), 12, 4);
                    if (MdaUtils.compareTo(yewudh, "0431") == 0 || MdaUtils.compareTo(yewudh, "0432") == 0 || MdaUtils.compareTo(yewudh, "0435") == 0 || MdaUtils.compareTo(yewudh, "0436") == 0) {
                        return ValidResult.build(false, "如需扣减保证金，请在Cash Cover页面操作");
                    }
                }
                if (!MdaUtils.isEmpty(setgllVo.getAcccode())) {
                    if (MdaUtils.compareTo(setgllVo.getAcccode(), "0431") == 0 || MdaUtils.compareTo(setgllVo.getAcccode(), "0432") == 0 || MdaUtils.compareTo(setgllVo.getAcccode(), "0435") == 0 || MdaUtils.compareTo(setgllVo.getAcccode(), "0436") == 0) {
                        return ValidResult.build(false, "如需扣减保证金，请在Cash Cover页面操作");
                    }
                }
            }
        }
        //! 国结3693自贸区机构下，MCTFEE交易，在操作人员发起JH、SH、ZZ（结汇、售汇、转账）交易时，
        //! 需对SETTLEMENT界面中进行帐号选择的控制。借方或贷方账户如果选择客户类帐户时，
        //! 控制只能选择FT或ZMQ开头的自贸区账号。
        String error = NULLSTR;
        if (!MdaUtils.isEmpty(setgllVo.getAct())) {
            SetmodVo setmodVo = (SetmodVo) getParent();
            if (MdaUtils.compareTo(setgllVo.getDsp(), "LOD") == 0 || MdaUtils.compareTo(setgllVo.getDsp(), "LOC") == 0) {
                if (MdaUtils.compareTo(SettleContext.getUserSession().getBch().getZmqflg(), "X") == 0) {
                    if (MdaUtils.compareTo(Strings.mid(setmodVo.getRef(), 11, 2), "JH") == 0 || MdaUtils.compareTo(Strings.mid(setmodVo.getRef(), 11, 2), "SH") == 0 || MdaUtils.compareTo(Strings.mid(setmodVo.getRef(), 11, 2), "ZZ") == 0) {
                        if (MdaUtils.compareTo(Strings.mid(setgllVo.getAct(), 1, 2), "FT") != 0 && MdaUtils.compareTo(Strings.mid(setgllVo.getAct(), 1, 2), "EF") != 0 && MdaUtils.compareTo(Strings.mid(setgllVo.getAct(), 1, 3), "ZMQ") != 0) {
                            error = "客户类帐户请选择EF、FT、ZMQ开头的自贸区帐号。";
                            return ValidResult.build(false, error);
                        }
                    }
                }
            }
        }
        return null;
    }

    /**
     * source:setgll.0073.script
     */
    @Validate(path = "setmod.setglg.setgll.%s.act")
    public ValidResult check81Act(SetgllVo setgllVo) {
        if ("CCD".equals(setgllVo.getDsp()) || "CCC".equals(setgllVo.getDsp())) {
            String m = "";
            if (Strings.len(setgllVo.getAct()) == 17) {
                m = Strings.mid(setgllVo.getAct(), 7, 2);
            }
            if (Strings.len(setgllVo.getAct()) == 21) {
                m = Strings.mid(setgllVo.getAct(), 11, 2);
            }
            if (MdaUtils.compareTo(Strings.mid(setgllVo.getAct(), 1, 3), "ZMQ") == 0 || MdaUtils.compareTo(Strings.mid(setgllVo.getAct(), 1, 3), "NRA") == 0) {
                m = Strings.mid(setgllVo.getAct(), 10, 2);
            }
            if (MdaUtils.compareTo(m, "81") == 0) {
                return ValidResult.build(false, "定期保证金账号不能直接在国结系统中记账，请到保证金调整交易做定转活操作！");
            }
        }
        return null;
    }

    /**
     * source:setgll.0077.script
     */
    @Validate(path = "setmod.setglg.setgll.%s.act")
    public ValidResult checkActN1500(SetgllVo setgllVo) {
        if ((MdaUtils.compareTo(setgllVo.getActtrmtyp(), "0433") == 0
                || MdaUtils.compareTo(setgllVo.getActtrmtyp(), "0440") == 0)
                && MdaUtils.compareTo(setgllVo.getDebcdtflg(), "D") == 0) {
            return ValidResult.build(false, Strings.formatText("$1科目只能贷记不能借记", setgllVo.getActtrmtyp()));
        }
        return null;
    }

    @Validate(path = "setmod.setglg.setgll.%s.act")
    public ValidResult checkAct8091(SetgllVo setgllVo) {
        if (!MdaUtils.isEmpty(setgllVo.getAct())) {
            if (MdaUtils.compareTo(setgllVo.getDsp(), "LOD") == 0 || MdaUtils.compareTo(setgllVo.getDsp(), "LOC") == 0) {
                if (!setgllVo.getActSet().contains(setgllVo.getAct())) {
                    SetmodVo setmodVo = (SetmodVo)getParent();
                    String rolExtkey = "";
                    PtsptaVo ptsptaVo = ptsptaToolComponent.getPtsFromListByRol(setmodVo.getPtsList(), setgllVo.getRol());
                    if (ptsptaToolComponent.ptsIsRolSet(ptsptaVo)) {
                        Pty pty = ptyCacheService.getPtyById(ptsptaVo.getPtyinr());
                        if (pty != null) {
                            rolExtkey = pty.getExtkey();
                        }
                    }
                    AccountRequestVo requestVo = new AccountRequestVo();
                    requestVo.setAccount(setgllVo.getAct());
                    AccountResponseVo response = settleDetailApiService.getCoreAccountCode(requestVo);
                    if (response != null) {
                        if (!rolExtkey.equals(response.getKehhao())) {
                            return ValidResult.build(false, Strings.getText("此账号不属于角色$1，请重新选择", setgllVo.getRol()));
                        }
                    }
                }
            }
        }
        return null;
    }

    @Validate(path = "setmod.setglg.setgll.%s.act", enable = "N")
    public ValidResult checkActForEf(SetgllVo setgllVo) {
        if (!MdaUtils.isEmpty(setgllVo.getAct())) {
            if (MdaUtils.compareTo(Strings.mid(setgllVo.getAct(), 1, 2), "EF") == 0) {
                if (!setgllVo.getActSet().contains(setgllVo.getAct())) {
                    return ValidResult.build(false, "请点击获取按钮获取核心业务代号");
                }
            }
        }
        return null;
    }

    /**
     * source:setgll.0078.script
     */
    @Validate(path = "setmod.setglg.setgll.%s.setgrp.rcvcor.pts.extkey")
    public ValidResult checkSetgrpRcvcorPtsExtkey(SetgllVo setgllVo) {
        //cover202特殊处理
        //56a的check
        if (!MdaUtils.isEmpty(setgllVo.getSetgrp().getRcvcor().getPtyextkey()) && MdaUtils.compareTo(setgllVo.getSetgrp().getRcvcor().getPtyextkey(), setgllVo.getSetgrp().getRol().getPtyextkey()) == 0) {
            return ValidResult.build(false, "Payee and intermediary bank may not be the same");
        }
        if (!MdaUtils.isEmpty(setgllVo.getSetgrp().getRcvcor().getPtyextkey()) && MdaUtils.compareTo(setgllVo.getSetgrp().getRcvcor().getPtyextkey(), setgllVo.getSetgrp().getAwi().getPtyextkey()) == 0) {
            return ValidResult.build(false, "Account with institution and intermediary bank may not be the same");
        }
        if (!MdaUtils.isEmpty(setgllVo.getSetgrp().getRcvcor().getPtyextkey()) && MdaUtils.compareTo(setgllVo.getSetgrp().getRcvcor().getPtyextkey(), setgllVo.getSetgrp().getApy().getPtyextkey()) == 0) {
            return ValidResult.build(false, "Account party and intermediary bank may not be the same");
        }
        return null;
    }

    /**
     * source:setgll.0102.script
     */
    @Validate(path = "setmod.setglg.setgll.%s.setgrp.tri.pts.extkey")
    public ValidResult checkSetgrpTriPtsExtkey(SetgllVo setgllVo) {
        //!===============================================
        //!跨境人民币代理行模式下，为了确保款项能够准确、顺利地解付，系统增加了“清算代理行”与其“参与行”
        //!关联关系的校验规则,将原先规则调整为：
        //!报文类型是MT103或MT202时，如果如56A有值，检核56A的银行在"清算代理行"的"参与行"清单中存在；
        //!如56A无值，57A有值，检核57A的银行在"清算代理行"的"参与行"清单中存在；
        //!如56A、57A都没有值，检核58A的银行在"清算代理行"的"参与行"清单中存在。
        //!===============================================
        /**
         //单证业务，如“清算代理行”选择BKCHHKHHXXX，且57A有值，检核57A的银行在“中银香港参加行清单”中存在；
         **/
        String transName = SettleContext.getDirectTransName();
        if (MdaUtils.compareTo(transName, "BCTSET") == 0
                || MdaUtils.compareTo(transName, "GITSET") == 0
                || MdaUtils.compareTo(transName, "GITPSE") == 0
                || MdaUtils.compareTo(transName, "NITSET") == 0
                || MdaUtils.compareTo(transName, "NITPSE") == 0
                || MdaUtils.compareTo(transName, "BRTSET") == 0) {
            SetmodVo setmodVo = (SetmodVo)getParent();
            String liqtyp = Utils.getFieldValue(setmodVo.getRec(), "cmtflg");
            String cleage = setgllVo.getSespay().getCorbnk();
            //if $LIQTYP = "C" and not IsEmpty and not IsEmpty( SESPAY\\CORBNK ) then
            if (MdaUtils.compareTo(liqtyp, "C") == 0 && !MdaUtils.isEmpty(setgllVo.getSetgrp().getTri().getPts().getExtkey()) && MdaUtils.isEmpty(setgllVo.getSetgrp().getRcvcor().getPts().getExtkey()) && !MdaUtils.isEmpty(setgllVo.getSespay().getCorbnk())) {
                if (checklink(cleage, setgllVo.getSetgrp().getTri().getPta().getPtyextkey())) {
                    return ValidResult.build(false, "请在dbibtb交易中维护,收款人开户行不在代理行"+cleage+"的参加行清单");
                }
            }
        }
        return null;
    }

    /**
     * source:setgll.0104.script
     */
    @Validate(path = "setmod.setglg.setgll.%s.setgrp.bei.pts.extkey")
    public ValidResult checkSetgrpBeiPtsExtkey(SetgllVo setgllVo) {
        //!===============================================
        //!跨境人民币代理行模式下，为了确保款项能够准确、顺利地解付，系统增加了“清算代理行”与其“参与行”
        //!关联关系的校验规则,将原先规则调整为：
        //!报文类型是MT103或MT202时，如果如56A有值，检核56A的银行在"清算代理行"的"参与行"清单中存在；
        //!如56A无值，57A有值，检核57A的银行在"清算代理行"的"参与行"清单中存在；
        //!如56A、57A都没有值，检核58A的银行在"清算代理行"的"参与行"清单中存在。
        //!===============================================
        /**
         //单证业务，如“清算代理行”选择BKCHHKHHXXX，且58A有值，检核58A的银行在“中银香港参加行清单”中存在；
         **/
        String liqtyp = NULLSTR;
        String cleage = NULLSTR;
        String transName = SettleContext.getDirectTransName();
        if (MdaUtils.compareTo(transName, "BCTSET") == 0
                || MdaUtils.compareTo(transName, "GITSET") == 0
                || MdaUtils.compareTo(transName, "GITPSE") == 0
                || MdaUtils.compareTo(transName, "NITSET") == 0
                || MdaUtils.compareTo(transName, "NITPSE") == 0
                || MdaUtils.compareTo(transName, "BRTSET") == 0) {
            SetmodVo setmodVo = (SetmodVo)getParent();
            liqtyp = Utils.getFieldValue(setmodVo.getRec(), "cmtflg");
            cleage = setgllVo.getSespay().getCorbnk();
            //if $LIQTYP = "C" and not IsEmpty and IsEmpty( SESPAY\\SETGRP\\TRI\\PTS\\EXTKEY ) and not IsEmpty( SESPAY\\CORBNK ) then
            if (MdaUtils.compareTo(liqtyp, "C") == 0
                    && !MdaUtils.isEmpty(setgllVo.getSetgrp().getBei().getPts().getExtkey())
                    && MdaUtils.isEmpty(setgllVo.getSetgrp().getTri().getPts().getExtkey())
                    && MdaUtils.isEmpty(setgllVo.getSetgrp().getRcvcor().getPts().getExtkey())
                    && !MdaUtils.isEmpty(setgllVo.getSespay().getCorbnk())) {
                if (checklink(cleage, setgllVo.getSetgrp().getBei().getPta().getPtyextkey())) {
                    return ValidResult.build(false, "请在dbibtb交易中维护,收款人开户行不在代理行"+cleage+"的参加行清单");
                }
            }
            if (MdaUtils.isEmpty(setgllVo.getSetgrp().getBei().getPts().getExtkey())
                    && !MdaUtils.isEmpty(setgllVo.getSespay().getCorbnk())) {
                return ValidResult.build(false, "This field is mandatory");
            }
        }
        if (MdaUtils.compareTo(SettleContext.getTransName(), "CPTOPN") == 0
                && MdaUtils.compareTo(setgllVo.getSftmt(), "202") == 0) {
            SetmodVo setmodVo = (SetmodVo) getParent();
            liqtyp = Utils.getFieldValue(setmodVo.getRec(), "liqtyp");
            cleage = Utils.getFieldValue(setmodVo.getRec(), "cleage");
            //if $LIQTYP = "C" and not IsEmpty( SESPAY\\SETGRP\\BEI\\PTS\\EXTKEY ) and IsEmpty( SESPAY\\SETGRP\\AWI\\PTS\\EXTKEY ) then
            if (MdaUtils.compareTo(liqtyp, "C") == 0
                    && !MdaUtils.isEmpty(setgllVo.getSetgrp().getBei().getPts().getExtkey())
                    && MdaUtils.isEmpty(setgllVo.getSetgrp().getAwi().getPts().getExtkey())
                    && MdaUtils.isEmpty(setgllVo.getSetgrp().getTmpint().getPts().getExtkey())) {
                if (checklink(cleage, setgllVo.getSetgrp().getBei().getPta().getPtyextkey())) {
                    return ValidResult.build(false, "请在dbibtb交易中维护,收款人开户行不在代理行"+cleage+"的参加行清单");
                }
            }
        }
        return null;
    }

    /**
     * source:setgll.0105.script
     */
    @Validate(path = "setmod.setglg.setgll.%s.setgrp.awi.pts.extkey")
    public ValidResult checkSetgrpAwiPtsExtkey(SetgllVo setgllVo) {
        //!===============================================
        //!跨境人民币代理行模式下，为了确保款项能够准确、顺利地解付，系统增加了“清算代理行”与其“参与行”
        //!关联关系的校验规则,将原先规则调整为：
        //!报文类型是MT103或MT202时，如果如56A有值，检核56A的银行在"清算代理行"的"参与行"清单中存在；
        //!如56A无值，57A有值，检核57A的银行在"清算代理行"的"参与行"清单中存在；
        //!如56A、57A都没有值，检核58A的银行在"清算代理行"的"参与行"清单中存在。
        //!===============================================
        /**
         //汇出汇款业务，如"清算代理行"选择BKCHHKHHXXX，且57A有值，检核57A的银行在"中银香港参加行清单"中存在；
         **/
        if (MdaUtils.compareTo(SettleContext.getTransName(), "CPTOPN") == 0) {
            SetmodVo setmodVo = (SetmodVo) getParent();
            String liqtyp = Utils.getFieldValue(setmodVo.getRec(), "liqtyp");
            String cleage = Utils.getFieldValue(setmodVo.getRec(), "cleage");
            //if $LIQTYP = "C" and not IsEmpty then
            if (MdaUtils.compareTo(liqtyp, "C") == 0
                    && !MdaUtils.isEmpty(setgllVo.getSetgrp().getAwi().getPts().getExtkey())
                    && MdaUtils.isEmpty(setgllVo.getSetgrp().getTmpint().getPts().getExtkey())) {
                if (checklink(cleage, setgllVo.getSetgrp().getAwi().getPta().getPtyextkey())) {
                    return ValidResult.build(false, "请在dbibtb交易中维护,收款人开户行不在代理行"+cleage+"的参加行清单");
                }
            }
        }
        return null;
    }

    /**
     * source:setgll.0111.script
     */
    @Validate(path = "setmod.setglg.setgll.%s.setgrp.int.pts.extkey")
    public ValidResult checkSetgrpIntPtsExtkeyN1001(SetgllVo setgllVo) {
        //!===============================================
        //!跨境人民币代理行模式下，为了确保款项能够准确、顺利地解付，系统增加了“清算代理行”与其“参与行”
        //!关联关系的校验规则,将原先规则调整为：
        //!报文类型是MT103或MT202时，如果如56A有值，检核56A的银行在"清算代理行"的"参与行"清单中存在；
        //!如56A无值，57A有值，检核57A的银行在"清算代理行"的"参与行"清单中存在；
        //!如56A、57A都没有值，检核58A的银行在"清算代理行"的"参与行"清单中存在。
        //!===============================================
        if (MdaUtils.compareTo(SettleContext.getTransName(), "CPTOPN") == 0) {
            SetmodVo setmodVo = (SetmodVo) getParent();
            String liqtyp = Utils.getFieldValue(setmodVo.getRec(), "liqtyp");
            String cleage = Utils.getFieldValue(setmodVo.getRec(), "cleage");
            if (MdaUtils.compareTo(liqtyp, "C") == 0
                    && !MdaUtils.isEmpty(setgllVo.getSetgrp().getTmpint().getPts().getExtkey())) {
                if (checklink(cleage, setgllVo.getSetgrp().getTmpint().getPta().getPtyextkey())) {
                    return ValidResult.build(false, "请在dbibtb交易中维护,收款人开户行不在代理行"+cleage+"的参加行清单");
                }
            }
        }
        return null;
    }

    /**
     * source:setgll.0112.script
     */
    @Validate(path = "setmod.setglg.setgll.%s.setgrp.rcvcor.pts.extkey")
    public ValidResult checkSetgrpRcvcorPtsExtkeyN1001(SetgllVo setgllVo) {
        //!===============================================
        //!跨境人民币代理行模式下，为了确保款项能够准确、顺利地解付，系统增加了“清算代理行”与其“参与行”
        //!关联关系的校验规则,将原先规则调整为：
        //!报文类型是MT103或MT202时，如果如56A有值，检核56A的银行在"清算代理行"的"参与行"清单中存在；
        //!如56A无值，57A有值，检核57A的银行在"清算代理行"的"参与行"清单中存在；
        //!如56A、57A都没有值，检核58A的银行在"清算代理行"的"参与行"清单中存在。
        //!===============================================
        String transName = SettleContext.getDirectTransName();
        if (MdaUtils.compareTo(transName, "BCTSET") == 0
                || MdaUtils.compareTo(transName, "GITSET") == 0
                || MdaUtils.compareTo(transName, "GITPSE") == 0
                || MdaUtils.compareTo(transName, "NITSET") == 0
                || MdaUtils.compareTo(transName, "NITPSE") == 0
                || MdaUtils.compareTo(transName, "BRTSET") == 0) {
            SetmodVo setmodVo = (SetmodVo) getParent();
            String liqtyp = Utils.getFieldValue(setmodVo.getRec(), "cmtflg");
            String cleage = setgllVo.getSespay().getCorbnk();
            if (MdaUtils.compareTo(liqtyp, "C") == 0
                    && !MdaUtils.isEmpty(setgllVo.getSetgrp().getRcvcor().getPts().getExtkey())
                    && !MdaUtils.isEmpty(setgllVo.getSespay().getCorbnk())) {
                if (checklink(cleage, setgllVo.getSetgrp().getRcvcor().getPta().getPtyextkey())) {
                    return ValidResult.build(false, "请在dbibtb交易中维护,收款人开户行不在代理行"+cleage+"的参加行清单");
                }
            }
        }
        return null;
    }

    /**
     * source:setgll.0123.script
     */
    //@Validate(path = "setmod.setglg.setgll.%s.discod")
    @Validate(path = "setmod.setglg.setgll.%s.modflg")
    public ValidResult checkDiscod(SetgllVo setgllVo) {
        //--ZMQZMQ-------------
        if (MdaUtils.compareTo(Strings.mid(setgllVo.getAct(), 1, 3), "ZMQ") == 0
                || MdaUtils.compareTo(Strings.mid(setgllVo.getAct(), 1, 2), "EF") == 0
                || MdaUtils.compareTo(Strings.mid(setgllVo.getAct(), 1, 2), "FT") == 0) {
            if (MdaUtils.compareTo(setgllVo.getCtycod(), "CHN") == 0) {
                if (MdaUtils.isEmpty(setgllVo.getDiscod())) {
                    return ValidResult.build(false, "国内地区代码不能为空");
                }
            }
        }
        return null;
    }

    /**
     * source:setgll.0125.script
     */
//    @Validate(path = "setmod.setglg.setgll.%s.trntyp")
    @Validate(path = "setmod.setglg.setgll.%s.modflg")
    public ValidResult checkTrntyp(SetgllVo setgllVo) {
        //--ZMQZMQ-------------
        if (MdaUtils.compareTo(Strings.mid(setgllVo.getAct(), 1, 3), "ZMQ") == 0
                || MdaUtils.compareTo(Strings.mid(setgllVo.getAct(), 1, 2), "EF") == 0
                || MdaUtils.compareTo(Strings.mid(setgllVo.getAct(), 1, 2), "FT") == 0) {
            if (MdaUtils.isEmpty(setgllVo.getTrntyp())) {
                //  if Mid( AMTIDSSTM, 1, 5 ) = "*SETFEG" then
                return ValidResult.build(false, "国际收支交易编码不能为空");
            } else {
                List<Bopcod> bopcodList = bopcodMapper.getBopcodByCondition(setgllVo.getTyp(), "A", null, setgllVo.getTrntyp());
                if (bopcodList == null || bopcodList.size() == 0) {
                    return ValidResult.build(false, "无效的国际收支交易编码");
                }
                if (MdaUtils.compareTo(SettleContext.getUserSession().getBch().getZmqflg(), "") == 0 && MdaUtils.compareTo(Strings.mid(setgllVo.getTrntyp(), 1, 1), "D") == 0) {
                    return ValidResult.build(false, "非自贸区机构不允许选择D开头的国际收支交易编码");
                }
                if (MdaUtils.compareTo(SettleContext.getUserSession().getBch().getZmqflg(), "X") == 0 && MdaUtils.compareTo(Strings.mid(setgllVo.getTrntyp(), 1, 1), "9") == 0) {
                    return ValidResult.build(false, "自贸区机构不允许选择9开头的国际收支交易编码");
                }
            }
        }
        return null;
    }

    /**
     * source:setgll.0128.script
     */
    //@Validate(path = "setmod.setglg.setgll.%s.ctycod")
    @Validate(path = "setmod.setglg.setgll.%s.modflg")
    public ValidResult checkCtycod(SetgllVo setgllVo) {
        //--ZMQZMQ-------------
        if (MdaUtils.compareTo(Strings.mid(setgllVo.getAct(), 1, 3), "ZMQ") == 0
                || MdaUtils.compareTo(Strings.mid(setgllVo.getAct(), 1, 2), "EF") == 0
                || MdaUtils.compareTo(Strings.mid(setgllVo.getAct(), 1, 2), "FT") == 0) {
            if (MdaUtils.isEmpty(setgllVo.getCtycod())) {
                return ValidResult.build(false, "国别代码不能为空");
            } else {
                Cnycty cnycty = cnyctyMapper.getCnyctyByCode(setgllVo.getCtycod());
                if (cnycty == null) {
                    return ValidResult.build(false, Strings.formatText("$1 查询不到CNYCTY表数据", setgllVo.getCtycod()));
                }
            }
        }
        return null;
    }

    /**
     * source:setgll.0135.script
     */
    @Validate(path = "setmod.setglg.setgll.%s.act")
    public ValidResult checkActN2005(SetgllVo setgllVo) {
        //---zmqzmq---
        String error = NULLSTR;
        if (!MdaUtils.isEmpty(setgllVo.getAct())) {
            if (MdaUtils.compareTo(SettleContext.getUserSession().getBch().getZmqflg(), "") == 0) {
                if (MdaUtils.compareTo(Strings.mid(SettleContext.getTransName(), 4, 3), "ACT") != 0) {
                    if (MdaUtils.compareTo(Strings.mid(setgllVo.getAct(), 1, 2), "FT") == 0
                            || MdaUtils.compareTo(Strings.mid(setgllVo.getAct(), 1, 2), "EF") == 0
                            || MdaUtils.compareTo(Strings.mid(setgllVo.getAct(), 1, 3), "ZMQ") == 0) {
                        error = "非自贸区机构不允许出现EF、FT账户（含ZMQ账户）";
                        return ValidResult.build(false, error);
                    }
                }
            }
        }
        return null;
    }

    /**
     * source:setgll.0147.script
     */
    //@Validate(path = "setmod.setglg.setgll.%s.xzhacc")
    @Validate(path = "setmod.setglg.setgll.%s.modflg")
    public ValidResult checkXzhacc(SetgllVo setgllVo) {
        //2019-7-资金池需求
        //当虚账户名称或虚账户摘要不为空时，虚账户账号必输
        String fromtxt = ";+;";
        if (!MdaUtils.isEmpty(setgllVo.getXzhnam())) {
            if (MdaUtils.isEmpty(setgllVo.getXzhacc())) {
                return ValidResult.build(false, "虚拟户账号不能为空");
            }
        }
        if (!MdaUtils.isEmpty(setgllVo.getXzhacc())) {
            int pos = Strings.pos(setgllVo.getXzhacc(), fromtxt);
            if (pos != 0) {
                return ValidResult.build(false, "虚拟户账号不能连续输入;+;");
            }
        }
        return null;
    }

    /**
     * source:setgll.0148.script
     */
    //@Validate(path = "setmod.setglg.setgll.%s.xzhnam")
    @Validate(path = "setmod.setglg.setgll.%s.modflg")
    public ValidResult checkXzhnam(SetgllVo setgllVo) {
        String fromtxt = ";+;";
        if (!MdaUtils.isEmpty(setgllVo.getXzhnam())) {
            int pos = Strings.pos(setgllVo.getXzhnam(), fromtxt);
            if (pos != 0) {
                return ValidResult.build(false, "虚拟户名称不能连续输入;+;");
            }
        }
        return null;
    }

    @Validate(path = "setmod.setglg.setgll.%s.act")
    public ValidResult checkAccForOit(SetgllVo setgllVo) {
        ActVo actVo = setgllVo.getActrec();
        if (actVo != null && setgllVo.getAct().equals(actVo.getExtkey())) {
            Map<String, List<Oit>> maps = oitService.filterOit(actVo.getInr(), "ACT");
            if (maps != null && maps.containsKey("ERROR")) {
                List<Oit> errorOits = maps.get("ERROR");
                String message = getMessage("0217", null);
                for (int i = 0; i < errorOits.size(); i++) {
                    if (i == 0) {
                        message += errorOits.get(i).getInftxt();
                    } else {
                        message += "\r\n" + errorOits.get(i).getInftxt();
                    }
                }
                return ValidResult.build(false, message);
            }
        }
        return null;
    }

    @Warning(path = "setmod.setglg.setgll.%s.act")
    public WarningResult warningAccForOit(SetgllVo setgllVo) {
        ActVo actVo = setgllVo.getActrec();
        if (actVo != null && setgllVo.getAct().equals(actVo.getExtkey())) {
            Map<String, List<Oit>> maps = oitService.filterOit(actVo.getInr(), "ACT");
            if (maps != null && !maps.containsKey("ERROR") && maps.containsKey("WARN")) {
                List<Oit> warnOits = maps.get("WARN");
                String title = getMessage("0220", actVo.getExtkey());
                String message = getMessage("0218", title);
                for (int i = 0; i < warnOits.size(); i++) {
                    if (i == 0) {
                        message += warnOits.get(i).getInftxt();
                    } else {
                        message += "\r\n" + warnOits.get(i).getInftxt();
                    }
                }
                return WarningResult.build("SetgllAccountWarning", message);
            }
        }
        return WarningResult.buildEmpty("SetgllAccountWarning");
    }

    @Warning(path = "setmod.setglg.setgll.%s.act", level = "3")
    public WarningResult infoAccForOit(SetgllVo setgllVo) {
        ActVo actVo = setgllVo.getActrec();
        if (actVo != null && setgllVo.getAct().equals(actVo.getExtkey())) {
            Map<String, List<Oit>> maps = oitService.filterOit(actVo.getInr(), "ACT");
            if (maps != null && !maps.containsKey("ERROR") && maps.containsKey("INFO")) {
                List<Oit> warnOits = maps.get("INFO");
                String title = getMessage("0220", actVo.getExtkey());
                String message = getMessage("0219", title);
                for (int i = 0; i < warnOits.size(); i++) {
                    if (i == 0) {
                        message += warnOits.get(i).getInftxt();
                    } else {
                        message += "\r\n" + warnOits.get(i).getInftxt();
                    }
                }
                return WarningResult.build("SetgllAccountInfo", message);
            }
        }
        return WarningResult.buildEmpty("SetgllAccountInfo");
    }

    @Warning(path = "setmod.setglg.setgll.%s.act")
    public WarningResult warningAccForVerify(SetgllVo setgllVo) {
        SetmodVo setmodVo = (SetmodVo) getParent();
        List<ActProVo> actPro = setmodVo.getSetglg().getActPro();
        Optional<ActProVo> optionalActProVo = actPro.stream().filter(actProVo -> setgllVo.getAct().equals(actProVo.getAct())).findFirst();
        if (optionalActProVo.isPresent() && "Y".equals(optionalActProVo.get().getSta())) {
            String warntxt_DR = setgllVo.getAct() + "账户为待核查账户，请确认是否为划入企业经常项目外汇结算账户、退汇、冲账或经外汇局登记的其他外汇支出";
            String warntxt_CR = setgllVo.getAct() + "账户性质为待核查账户，请确认是否为货物贸易外汇收入";
            String warnmsg = "D".equals(setgllVo.getDebcdtflg()) ? warntxt_DR : warntxt_CR;
            return WarningResult.build("SubVerifyAccount", warnmsg);
        }
        return WarningResult.buildEmpty("SubVerifyAccount");
    }

    @Validate(path = "setmod.setglg.setgll.%s.act")
    public ValidResult checkAccForVerify(SetgllVo setgllVo) {
        SetmodVo setmodVo = (SetmodVo) getParent();
        List<ActProVo> actPro = setmodVo.getSetglg().getActPro();
        Optional<ActProVo> optionalActProVo = actPro.stream().filter(actProVo -> setgllVo.getAct().equals(actProVo.getAct())).findFirst();
        if (optionalActProVo.isPresent()) {
            if ("N".equals(optionalActProVo.get().getSta())) {
                return ValidResult.build(false, "该账号为待核查且已确认不使用，请重新点击获取确认");
            }
        } else {
            if(!MdaUtils.isEmpty(setgllVo.getAct())
                    && MdaUtils.compareTo(Strings.mid(setgllVo.getDsp(),1,2),"LO") == 0
                    && MdaUtils.compareTo(setgllVo.getAcccur(),"CNY") != 0 ) {
                AccountRequestVo requestVo = new AccountRequestVo();
                requestVo.setAccount(setgllVo.getAct());
                AccountResponseVo response = settleDetailApiService.getAccountNature(requestVo);
                if (response != null) {
                    if (StringUtils.isNotEmpty(response.getErrorMsg())) {
                        return ValidResult.build(false, "请点击获取按钮获取账号性质");
                    } else if ("1101".equals(response.getWjzhxz())) {
                        return ValidResult.build(false, "该账号为待核查账户，请点击获取确认");
                    }
                }
            }
        }
        return null;
    }

    /**
     * source:setgll.0103.script
     */
    public boolean checklink(String corbch, String pyebch) {
        //根据BTB表查寻检核收款人开户银行在代理行参加行清单中存在
        //存在-----true
        //不存在---flase
        if (MdaUtils.compareTo(corbch, pyebch) == 0) {
            return false;
        } else {
            String sql = "SELECT count(*) FROM BTB WHERE CORBNK='" + corbch + "'AND PYEBCH='" + pyebch + "' and useflg='X'";
            Map<String, Object> dyncMap = new HashMap<>();
            dyncMap.put("sql", sql);
            int count = publicMapper.dyncCount(dyncMap);
            if (count == 0) {
                return false;
            } else {
                return true;
            }
        }
    }

}
