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

import com.brilliance.isc.bo.Cbb;
import com.brilliance.isc.bo.Pty;
import com.brilliance.isc.common.cache.pty.PtyCacheService;
import com.brilliance.isc.common.codetable.service.CodetableService;
import com.brilliance.isc.common.component.PtsptaToolComponent;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.finmod.service.FinmodService;
import com.brilliance.isc.common.transaction.newcheck.AbstractModuleValidator;
import com.brilliance.isc.common.transaction.newcheck.ValidResult;
import com.brilliance.isc.common.transaction.newcheck.Validate;
import com.brilliance.isc.common.util.StringUtils;
import com.brilliance.isc.common.vo.PtsptaVo;
import com.brilliance.isc.mda.dao.PublicMapper;
import com.brilliance.isc.vo.model.FinmodVo;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import com.brilliance.mda.runtime.mda.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

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

@Component
public class FinmodValidator extends AbstractModuleValidator<FinmodVo> {

    @Autowired
    private FinmodService finmodService;

    @Autowired
    private CodetableService codetableService;

    @Autowired
    private PtsptaToolComponent ptsptaToolComponent;

    @Autowired
    private PtyCacheService ptyCacheService;

    @Autowired
    private PublicMapper publicMapper;

    @Autowired
    private DinmodValidator dinmodValidator;

    @Override
    @PostConstruct
    public void register() {
        super.register();
    }

    @Override
    public Map<String, String> validate(FinmodVo vo) {
        Map<String, String> maps = new HashMap<>();
        maps.putAll(super.validate(vo));
        //验证Dinmod模型
        dinmodValidator.registerParent(vo);
        maps.putAll(dinmodValidator.validate(vo.getDinmod()));
        dinmodValidator.removeTl();
        return maps;
    }

    /**
     * source:finmod.0024.script
     */
    @Validate(path = "finmod.intdat")
    public ValidResult checkIntdatN1000(FinmodVo finmodVo) {
        if (finmodService.isCreFinTrn(finmodVo)) {
            if (Strings.pos("12345", finmodVo.getIntprd()) > 0) {
                try {
                    int val = Integer.parseInt(finmodVo.getIntdat());
                    if (val <= 0 || val > 31) {
                        return ValidResult.build(false, "输入日期错误！");
                    }
                } catch (Exception e) {
                    return ValidResult.build(false, "输入日期不合法");
                }
            }
        }
        return null;
    }

    /**
     * source:finmod.0026.script
     */
    @Validate(path = "finmod.flttyp")
    public ValidResult checkFlttypN1000(FinmodVo finmodVo) {
        if (finmodService.isCreFinTrn(finmodVo)) {
            if (MdaUtils.isEmpty(finmodVo.getFlttyp())) {
                return errorMandatory();
            }
        }
        return null;
    }

    /**
     * source:finmod.0027.script
     */
    @Validate(path = "finmod.fltval")
    public ValidResult checkFltvalN1000(FinmodVo finmodVo) {
        if (finmodService.isCreFinTrn(finmodVo)) {
            if ((MdaUtils.compareTo(finmodVo.getFlttyp(), "2") == 0 || MdaUtils.compareTo(finmodVo.getFlttyp(), "1") == 0)
                    && MdaUtils.isEmpty(finmodVo.getFltval())) {
                return errorMandatory();
            }
        }
        return null;
    }

    /**
     * source:finmod.0028.script
     */
    @Validate(path = "finmod.ovdinttyp")
    public ValidResult checkOvdinttypN1000(FinmodVo finmodVo) {
        if (finmodService.isCreFinTrn(finmodVo)) {
            if (MdaUtils.isEmpty(finmodVo.getOvdinttyp())) {
                return errorMandatory();
            }
        }
        return null;
    }

    /**
     * source:finmod.0029.script
     */
    @Validate(path = "finmod.ratchgdat")
    public ValidResult checkRatchgdatN1000(FinmodVo finmodVo) {
        if (finmodService.isCreFinTrn(finmodVo)) {
            if (Strings.pos("3456", finmodVo.getRatchgtyp()) > 0) {
                if (MdaUtils.isEmpty(finmodVo.getRatchgdat())) {
                    return errorMandatory();
                } else {
                    int month = 0;
                    try {
                        month = Integer.parseInt(Strings.mid(finmodVo.getRatchgdat(), 1, 2));
                    } catch (Exception e) {
                        return ValidResult.build(false, "输入的月份不合法！");
                    }
                    int day = 0;
                    try {
                        day = Integer.parseInt(Strings.mid(finmodVo.getRatchgdat(), 3));
                    } catch (Exception e) {
                        return ValidResult.build(false, "输入月日数中日期不合法！");
                    }
                    if (MdaUtils.compareTo(finmodVo.getRatchgtyp(), "6") == 0) {
                        int dayAllow = 0;
                        switch (month) {
                            case 1:
                            case 3:
                            case 5:
                            case 7:
                            case 8:
                            case 10:
                            case 12:
                                dayAllow = 31;
                                break;
                            case 2:
                                dayAllow = 28;
                                break;
                            case 4:
                            case 6:
                            case 9:
                            case 11:
                                dayAllow = 30;
                                break;
                            default:
                                return ValidResult.build(false, "输入的月份不合法！");
                        }
                        if (dayAllow > 0) {
                            if (day <= 0 || day > dayAllow) {
                                return ValidResult.build(false, "输入月日数中日期不合法！");
                            }
                        }
                    } else {
                        if (month != 0) {
                            return ValidResult.build(false, "非按年调整，不能输入月份");
                        }
                        if (day <= 0 || day > 31) {
                            return ValidResult.build(false, "输入月日数中日期不合法！");
                        }
                    }
                }
            }
        }
        return null;
    }

    /**
     * source:finmod.0030.script
     */
    @Validate(path = "finmod.ratchgprd")
    public ValidResult checkRatchgprdN1000(FinmodVo finmodVo) {
        if (finmodService.isCreFinTrn(finmodVo)) {
            if ((Strings.pos("-7-8-9-", finmodVo.getRatchgtyp()) > 0 || (Strings.pos("-I-J-G-H-", finmodVo.getRatchgtyp()) > 0 && Strings.pos("-BPTOPN-TRTOPN-BRTSET-BCTSET-BDTSET-CPTOPN-CPTATO-", SettleContext.getDirectTransName()) > 0))
                    && MdaUtils.isEmpty(finmodVo.getRatchgprd())) {
                return errorMandatory();
            }
            switch (SettleContext.getDirectTransName()) {
                case "BPTOPN":
                case "TRTOPN":
                case "BRTSET":
                case "BCTSET":
                case "BDTSET":
                case "CPTOPN":
                case "CPTATO":
                    //新增放款
                    if (!MdaUtils.isEmpty(finmodVo.getRatchgprd()) && MdaUtils.compareTo(finmodVo.getRatchgtyp(), "J") == 0 && MdaUtils.compareTo(finmodVo.getRatchgprd(), "D") != 0) {
                        return ValidResult.build(false, "利率调整方式为国际无风险后顾时，利率调整周期必须为一天");
                    }
                    break;
            }
        }
        return null;
    }

    /**
     * source:finmod.0031.script
     */
    @Validate(path = "finmod.act1")
    public ValidResult checkAct1N1000(FinmodVo finmodVo) {
        if (finmodService.isCreFinTrn(finmodVo)) {
            if (MdaUtils.compareTo(finmodVo.getActtyp(), "2") != 0 && MdaUtils.isEmpty(finmodVo.getAct1()) && MdaUtils.compareTo(finmodVo.getIntprd(), "6") != 0 && MdaUtils.compareTo(finmodVo.getIntprd(), "7") != 0) {
                return errorMandatory();
            }
            Cbb maxCbb = finmodVo.getCbsMap().getOrDefault("MAX", new Cbb());
            String cur = maxCbb.getCur();
            String errTxt = checkFinAct(cur, finmodVo.getAct1());
            if (StringUtils.isNotEmpty(errTxt)) {
                return ValidResult.build(false, errTxt);
            }
        }
        //钩选是否到期主动扣收贷款时，此项必输
        if (MdaUtils.compareTo(finmodVo.getWjdkfl(), "0") == 0 && MdaUtils.isEmpty(finmodVo.getAct1())) {
            return errorMandatory();
        }
        return null;
    }

    /**
     * source:finmod.0032.script
     */
    @Validate(path = "finmod.act3")
    public ValidResult checkAct3N1000(FinmodVo finmodVo) {
        if (finmodService.isCreFinTrn(finmodVo)) {
            if (!MdaUtils.isEmpty(finmodVo.getActtyp()) && MdaUtils.isEmpty(finmodVo.getAct3())) {
                return errorMandatory();
            }
            Cbb maxcbb = finmodVo.getCbsMap().getOrDefault("MAX", new Cbb());
            String cur = maxcbb.getCur();
            String errTxt = checkFinAct(cur, finmodVo.getAct3());
            if (StringUtils.isNotEmpty(errTxt)) {
                return ValidResult.build(false, errTxt);
            }
        }
        return null;
    }

    /**
     * source:finmod.0034.script
     */
    @Validate(path = "finmod.inttyp")
    public ValidResult checkInttypN1000(FinmodVo finmodVo) {
        if (finmodService.isCreFinTrn(finmodVo)) {
            String frame = SettleContext.getTransName();
            if (MdaUtils.isEmpty(finmodVo.getInttyp()) && MdaUtils.compareTo(finmodVo.getIntamt(), 0) > 0 && MdaUtils.compareTo(frame, "BPTAME") != 0 && MdaUtils.compareTo(frame, "TRTAME") != 0) {
                return errorMandatory();
            }
        }
        return null;
    }

    /**
     * source:finmod.0035.script
     */
    @Validate(path = "finmod.intact")
    public ValidResult checkIntactN1000(FinmodVo finmodVo) {
        if (finmodService.isCreFinTrn(finmodVo)) {
            if (MdaUtils.isEmpty(finmodVo.getIntact()) && MdaUtils.compareTo(finmodVo.getInttyp(), "1") == 0) {
                return errorMandatory();
            }
            Cbb maxcbb = finmodVo.getCbsMap().getOrDefault("MAX", new Cbb());
            String cur = maxcbb.getCur();
            String errTxt = checkFinAct(cur, finmodVo.getAct1());
            if (StringUtils.isNotEmpty(errTxt)) {
                return ValidResult.build(false, errTxt);
            }
        }
        return null;
    }

    /**
     * source:finmod.0037.script
     */
    @Validate(path = "finmod.ratchgtyp")
    public ValidResult checkRatchgtypN1000(FinmodVo finmodVo) {
        if (finmodService.isCreFinTrn(finmodVo)) {
            if (MdaUtils.isEmpty(finmodVo.getRatchgtyp())) {
                return errorMandatory();
            } else {
                switch (SettleContext.getDirectTransName()) {
                    case "BPTOPN":
                    case "TRTOPN":
                    case "BRTSET":
                    case "BCTSET":
                    case "BDTSET":
                    case "CPTOPN":
                    case "CPTATO":
                        //新增放款
                        if (MdaUtils.compareTo(finmodVo.getRatchgtyp(), "G") == 0
                                || MdaUtils.compareTo(finmodVo.getRatchgtyp(), "H") == 0) {
                            return ValidResult.build(false, "新放款不允许选择该利率调整方式");
                        }
                        break;
                }
            }
        }
        return null;
    }

    /**
     * source:finmod.0038.script
     */
    @Validate(path = "finmod.intamt")
    public ValidResult checkIntamtN1000(FinmodVo finmodVo) {
        if (finmodService.isCreFinTrn(finmodVo)) {
            String frame = SettleContext.getTransName();
            if (MdaUtils.compareTo(finmodVo.getIntprd(), "7") == 0
                    && MdaUtils.isEmpty(finmodVo.getIntamt()) && MdaUtils.compareTo(frame, "BPTAME") != 0 && MdaUtils.compareTo(frame, "TRTAME") != 0) {
                return ValidResult.build(false, "预扣息金额计算有误");
            }
        }
        return null;
    }

    /**
     * source:finmod.0040.script
     */
    @Validate(path = "finmod.ovdintrat")
    public ValidResult checkOvdintratN1000(FinmodVo finmodVo) {
        if (finmodService.isCreFinTrn(finmodVo)) {
            if (MdaUtils.compareTo(finmodVo.getOvdinttyp(), "1") == 0
                    && MdaUtils.isEmpty(finmodVo.getOvdintrat())) {
                return errorMandatory();
            }
        }
        return null;
    }

    /**
     * source:finmod.0045.script
     */
    @Validate(path = "finmod.dinjdm")
    public ValidResult checkDinjdmN1000(FinmodVo finmodVo) {
        //!进口融资放款和出口融资放款增加定价审批代码必输
        String ownref = "";
        String dinjdm = NULLSTR;
        String idtype = NULLSTR;
        String ffttyp = NULLSTR;
        PtsptaVo ptsptaVo = null;
        switch (SettleContext.getDirectTransName()) {
            case "TRTOPN":
            case "BRTSET":
            case "BCTSET":
            case "BDTSET":
            case "CPTOPN":
            case "CPTATO":
                dinjdm = finmodVo.getDinjdm();
                ptsptaVo = ptsptaToolComponent.getPtsFromListByRol(finmodVo.getPtsptaList(), "FIP");
                if (ptsptaVo != null) {
                    Pty pty = ptyCacheService.getPtyById(ptsptaVo.getPtyinr());
                    if (pty != null) {
                        idtype = pty.getIdtype();
                    }
                }
                if (!"2".equals(idtype)) {
                    if (StringUtils.isEmpty(dinjdm)) {
                        return errorMandatory();
                    } else {
                        Map<String, Object> dyncMap = new HashMap<>();
                        String sql = "SELECT ownref FROM TRD WHERE DINJDM = '" + dinjdm + "' limit 1";
                        dyncMap.put("sql", sql);
                        Map<String, Object> maps = publicMapper.dyncQueryMap(dyncMap);
                        if (maps != null) {
                            ownref = (String) maps.get("ownref");
                        }
                        if (StringUtils.isEmpty(ownref)) {
                            sql = "SELECT ownref FROM BPD WHERE DINJDM = '" + dinjdm + "'  limit 1";
                            dyncMap.put("sql", sql);
                            maps = publicMapper.dyncQueryMap(dyncMap);
                            if (maps != null) {
                                ownref = (String) maps.get("ownref");
                            }
                        }
                    }
                }
                break;
            case "BPTOPN":
                ffttyp = finmodVo.getRec().getFfttyp();
                if (!MdaUtils.isEmpty(ffttyp)) {

                } else {
                    if (finmodService.isCreFinTrn(finmodVo)) {
                        dinjdm = finmodVo.getDinjdm();
                        ptsptaVo = ptsptaToolComponent.getPtsFromListByRol(finmodVo.getPtsptaList(), "FIA");
                        if (ptsptaVo != null) {
                            Pty pty = ptyCacheService.getPtyById(ptsptaVo.getPtyinr());
                            if (pty != null) {
                                idtype = pty.getIdtype();
                            }
                        }
                        String fortyp = finmodVo.getRec().getFortyp();
                        //--FFT(SXL)--------------
                        if (MdaUtils.compareTo(fortyp, "2") == 0) {

                        } else {
                            if (!"2".equals(idtype)) {
                                if (StringUtils.isEmpty(dinjdm)) {
                                    return errorMandatory();
                                } else {
                                    Map<String, Object> dyncMap = new HashMap<>();
                                    String sql = "SELECT ownref FROM BPD WHERE DINJDM = '" + dinjdm + "'  limit 1";
                                    dyncMap.put("sql", sql);
                                    Map<String, Object> maps = publicMapper.dyncQueryMap(dyncMap);
                                    if (maps != null) {
                                        ownref = (String) maps.get("ownref");
                                    }
                                    if (StringUtils.isEmpty(ownref)) {
                                        sql = "SELECT ownref FROM TRD WHERE DINJDM = '" + dinjdm + "'  limit 1";
                                        dyncMap.put("sql", sql);
                                        maps = publicMapper.dyncQueryMap(dyncMap);
                                        if (maps != null) {
                                            ownref = (String) maps.get("ownref");
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                break;
        }
        if (!MdaUtils.isEmpty(ownref)) {
            return ValidResult.build(false, Strings.getText("定价审批代码已在$1此笔融资中使用，请更换审批代码", ownref));
        }
        return null;
    }

    /**
     * source:finmod.0051.script
     */
    @Validate(path = "finmod.dinmod.ockhdm")
    public ValidResult checkDinmodOckhdmN1000(FinmodVo finmodVo) {
        //!校验国结的OC客户码
        String ockhdm = NULLSTR;
        String idtype = NULLSTR;
        PtsptaVo ptsptaVo = null;
        if (MdaUtils.compareTo(finmodVo.getOpnflg(), "Y") == 0 && !MdaUtils.isEmpty(finmodVo.getDinjdm())) {
            switch (SettleContext.getDirectTransName()) {
                case "TRTOPN":
                case "BRTSET":
                case "BCTSET":
                case "BDTSET":
                case "CPTOPN":
                case "CPTATO":
                    ockhdm = finmodVo.getDinmod().getOckhdm();
                    ptsptaVo = ptsptaToolComponent.getPtsFromListByRol(finmodVo.getPtsptaList(), "FIP");
                    if (ptsptaVo != null) {
                        Pty pty = ptyCacheService.getPtyById(ptsptaVo.getPtyinr());
                        if (pty != null) {
                            idtype = pty.getIdtype();
                        }
                    }
                    if (MdaUtils.compareTo(idtype, "2") == 0) {
                        //
                    } else {
                        if (MdaUtils.isEmpty(ockhdm)) {
                            return ValidResult.build(false, "请点击定价审批Get按钮获取定价系统放款信息");
                        }
                    }
                    break;
                case "BPTOPN":
                    if (finmodService.isCreFinTrn(finmodVo)) {
                        ockhdm = finmodVo.getDinmod().getOckhdm();
                        ptsptaVo = ptsptaToolComponent.getPtsFromListByRol(finmodVo.getPtsptaList(), "FIA");
                        if (ptsptaVo != null) {
                            Pty pty = ptyCacheService.getPtyById(ptsptaVo.getPtyinr());
                            if (pty != null) {
                                idtype = pty.getIdtype();
                            }
                        }
                        if (MdaUtils.compareTo(idtype, "2") == 0) {
                            //
                        } else {
                            if (MdaUtils.isEmpty(ockhdm)) {
                                return ValidResult.build(false, "请点击定价审批Get按钮获取定价系统放款信息");
                            }
                        }
                        break;
                    }
                    break;
            }
        }
        return null;
    }

    /**
     * source:finmod.0052.script
     */
    @Validate(path = "finmod.dinmod.chapdm")
    public ValidResult checkDinmodChapdmN1000(FinmodVo finmodVo) {
        //!校验国结系统的业务类型与定价系统的产品代码是否一致
        /**
         #--------------国结给cecm接口的逻辑--------------------------#
         CASE
         WHEN T.FINTYP='L0000145' AND T.RSKTYP='L0000146' then 'L0000147'
         WHEN T.FINTYP='L0000148' AND T.RSKTYP='L0000149' AND T.PNTTYP='L0000150' then 'L0000151'
         WHEN T.FINTYP='L0000152' AND T.RSKTYP='L0000153' then 'L0000154'
         WHEN T.FINTYP='L0000155' then 'L0000156'
         WHEN T.FINTYP='L0000157' AND (T.RSKTYP ='L0000158' OR T.RSKTYP='L0000159') then 'L0000160'
         WHEN T.FINTYP='L0000161' then 'L0000162'
         WHEN T.FINTYP='L0000163' AND T.FORTYP='L0000164' then 'L0000165'
         WHEN T.FINTYP='L0000166' then 'L0000167'
         WHEN T.FINTYP='L0000168' then 'L0000169'
         WHEN T.FINTYP='L0000170' AND T.FORTYP='L0000171' then 'L0000172'
         WHEN T.FINTYP='L0000173' then 'L0000174'
         WHEN T.FINTYP='L0000175' then 'L0000176'
         WHEN T.FINTYP='L0000177' then 'L0000178'
         WHEN T.FINTYP='L0000179' AND T.RSKTYP='L0000180' then 'L0000181'
         WHEN T.FINTYP='L0000182' AND T.RSKTYP='L0000183' then 'L0000184'
         WHEN T.FINTYP='L0000185' AND (T.RSKTYP ='L0000186' OR T.RSKTYP='L0000187') then 'L0000188'
         WHEN T.FINTYP='L0000189' then 'L0000190'
         ELSE
         'L0000191'
         END 产品代码 ,
         CASE
         WHEN T.FINTYP='L0000192' then 'L0000193'
         WHEN T.FINTYP='L0000194' then 'L0000195'
         WHEN T.FINTYP='L0000196' AND T.RSKTYP='L0000197' then 'L0000198'
         WHEN T.FINTYP='L0000199' AND T.RSKTYP='L0000200' then 'L0000201'
         WHEN T.FINTYP='L0000202' AND T.RSKTYP='L0000203' then 'L0000204'
         WHEN T.FINTYP='L0000205' AND T.RSKTYP='L0000206' then 'L0000207'
         WHEN T.FINTYP='L0000208' then 'L0000209'
         WHEN T.FINTYP='L0000210' then 'L0000211'
         WHEN T.FINTYP='L0000212' then 'L0000213'
         WHEN T.FINTYP='L0000214' AND T.RSKTYP='L0000215' AND T.PNTTYP='L0000216' then 'L0000217'
         WHEN T.FINTYP='L0000218' AND T.RSKTYP='L0000219' AND T.PNTTYP='L0000220' then 'L0000221'
         WHEN T.FINTYP='L0000222' AND T.RSKTYP='L0000223' AND T.PNTTYP='L0000224' then 'L0000225'
         WHEN T.FINTYP='L0000226' AND T.RSKTYP='L0000227' AND T.PNTTYP='L0000228' then 'L0000229'
         WHEN T.FINTYP='L0000230' AND T.RSKTYP='L0000231' then 'L0000232'
         WHEN T.FINTYP='L0000233' AND T.RSKTYP='L0000234' then 'L0000235'
         ELSE
         'L0000236'
         END  产品名称
         **/
        String gjcpdm = "";
        String frmnam = SettleContext.getDirectTransName();
        String fintyp = NULLSTR;
        String rsktyp = NULLSTR;
        String pnttyp = NULLSTR;
        String fortyp = NULLSTR;
        if (!MdaUtils.isEmpty(finmodVo.getDinjdm())) {
            if (MdaUtils.compareTo(finmodVo.getOpnflg(), "Y") == 0 && finmodService.isCreFinTrn(finmodVo)) {
                switch (frmnam) {
                    case "TRTOPN":
                    case "BRTSET":
                    case "BDTSET":
                    case "BCTSET":
                    case "CPTOPN":
                    case "CPTATO":
                        fintyp = finmodVo.getRec().getFintyp();
                        if (StringUtils.isNotEmpty(fintyp)) {
                            switch (fintyp) {
                                case "BRF":
                                    gjcpdm = "0502";
                                    break;
                                case "LOA":
                                    gjcpdm = "0504";
                                    break;
                                case "PCL":
                                    gjcpdm = "";
                                    break;
                                case "BEL":
                                    gjcpdm = "";
                                    break;
                                case "BCF":
                                    gjcpdm = "0503";
                                    break;
                                case "ICF":
                                    gjcpdm = "0504";
                                    break;
                                case "ECF":
                                    gjcpdm = "0504";
                                    break;
                                case "DRF":
                                    gjcpdm = "051602";
                                    break;
                                default:
                                    gjcpdm = "";
                            }
                        }
                        if (MdaUtils.compareTo(gjcpdm, finmodVo.getDinmod().getChapdm()) != 0) {
                            return ValidResult.build(false, Strings.getText("国结业务品种$1与定价系统的产品代码不匹配", gjcpdm));
                        }
                        break;
                    case "BPTOPN":
                        if (finmodService.isCreFinTrn(finmodVo)) {
                            fintyp = finmodVo.getRec().getFintyp();
                            rsktyp = finmodVo.getRec().getRsktyp();
                            pnttyp = finmodVo.getRec().getPnttyp();
                            fortyp = finmodVo.getRec().getFortyp();
                            if (StringUtils.isNotEmpty(fintyp)) {
                                switch (fintyp) {
                                    case "P":
                                        gjcpdm = "0506";
                                        if (MdaUtils.compareTo(rsktyp, "NB0") == 0) {
                                            gjcpdm = "0505";
                                        }
                                        if (MdaUtils.compareTo(rsktyp, "DSB") == 0 && MdaUtils.compareTo(pnttyp, "BED") == 0) {
                                            gjcpdm = "0506";
                                        }
                                        if (MdaUtils.compareTo(rsktyp, "DSB") == 0 && MdaUtils.compareTo(pnttyp, "BOD") == 0) {
                                            gjcpdm = "0507";
                                        }
                                        if (MdaUtils.compareTo(rsktyp, "BO") == 0) {
                                            gjcpdm = "0507";
                                        }
                                        break;
                                    case "D":
                                        gjcpdm = "0510";
                                        if (MdaUtils.compareTo(rsktyp, "DIS") == 0 || MdaUtils.compareTo(rsktyp, "AAA") == 0) {
                                            gjcpdm = "0509";
                                        }
                                        break;
                                    case "F":
                                        gjcpdm = "051101";
                                        if (MdaUtils.compareTo(fortyp, "4") == 0) {
                                            gjcpdm = "051104";
                                        }
                                        break;
                                    case "U":
                                        gjcpdm = "051103";
                                        break;
                                    case "G":
                                        gjcpdm = "05160501";
                                        if (MdaUtils.compareTo(fortyp, "4") == 0) {
                                            gjcpdm = "05160504";
                                        }
                                        break;
                                    case "V":
                                        gjcpdm = "05160503";
                                        break;
                                    case "I":
                                        gjcpdm = "0508";
                                        break;
                                    case "M":
                                        /**
                                         if $RSKTYP == "NBO" then
                                         **/
                                        if (MdaUtils.compareTo(rsktyp, "NBO") == 0 || MdaUtils.compareTo(rsktyp, "NB0") == 0) {
                                            gjcpdm = "05160301";
                                        }
                                        if (MdaUtils.compareTo(rsktyp, "DSB") == 0) {
                                            gjcpdm = "05160302";
                                        }
                                        break;
                                    case "T":
                                        gjcpdm = "05160402";
                                        if (MdaUtils.compareTo(rsktyp, "DIS") == 0 || MdaUtils.compareTo(rsktyp, "AAA") == 0) {
                                            gjcpdm = "05160401";
                                        }
                                        break;
                                    default:
                                        gjcpdm = "";
                                }
                            }
                            if (MdaUtils.compareTo(gjcpdm, finmodVo.getDinmod().getChapdm()) != 0) {
                                return ValidResult.build(false, Strings.getText("国结业务品种$1与定价系统的产品代码不匹配", gjcpdm));
                            }
                        }
                        break;
                    default:
                }
            }
        }
        return null;
    }

    /**
     * source:finmod.0053.script
     */
    @Validate(path = "finmod.dinmod.hxkhdm")
    public ValidResult checkDinmodHxkhdmN1000(FinmodVo finmodVo) {
        //核心客户代码
        if (!MdaUtils.isEmpty(finmodVo.getDinmod().getHxkhdm())) {
            String role = "";
            switch (SettleContext.getDirectTransName()) {
                case "TRTOPN":
                case "BRTSET":
                case "BCTSET":
                case "BDTSET":
                case "CPTOPN":
                case "CPTATO":
                    role = "FIP";
                    break;
                case "BPTOPN":
                    role = "FIA";
                    break;
                default:
            }
            String extkey = "";
            PtsptaVo ptsptaVo = ptsptaToolComponent.getPtsFromListByRol(finmodVo.getPtsptaList(), role);
            if (ptsptaVo != null) {
                Pty pty = ptyCacheService.getPtyById(ptsptaVo.getPtyinr());
                if (pty != null) {
                    extkey = pty.getExtkey();
                }
            }
            if (MdaUtils.compareTo(extkey, finmodVo.getDinmod().getHxkhdm()) != 0) {
                return ValidResult.build(false, "国结核心客户代码与定价系统不一致，不允许放款请重新定价");
            }
        }
        return null;
    }

    /**
     * source:finmod.0039.script
     */
    public String checkFinAct(String cur, String act) {
        int len = Strings.len(act);
        String curId = "";
        String errorText = "";
        switch (len) {
            case 17:
                curId = Strings.mid(act, 5, 2);
                break;
            case 21:
                curId = Strings.mid(act, 9, 2);
                //--zmqzmq-----
                break;
            case 20:
                curId = Strings.mid(act, 8, 2);
                break;
            case 0:
                return "";
            default:
                errorText = "账号长度不正确";
                return errorText;
        }
        /**
         if $CurId <> Mid( GetTblText( Cur, "CURKEY", "EN" ), 1, 2 ) then
         **/
        if (MdaUtils.compareTo(curId, Strings.mid(codetableService.getTblLabel(cur, "CURKEY"), 1, 2)) != 0
                && MdaUtils.compareTo(Strings.mid(act, 1, 2), "FT") != 0
                && MdaUtils.compareTo(Strings.mid(act, 1, 2), "EF") != 0) {
            errorText = "还款币种与融资币种不一致";
        }
        return errorText;
    }
}
