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

import com.alibaba.fastjson.JSONObject;
import com.brilliance.mda.runtime.mda.util.Decimals;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import com.brilliance.mda.runtime.mda.util.Strings;
import com.brilliance.isc.bo.Cbb;
import com.brilliance.isc.bo.Finmod;
import com.brilliance.isc.bo.Pty;
import com.brilliance.isc.bo.Tax;
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.esb.finance.IFinanceFeign;
import com.brilliance.isc.common.exception.CommonServiceException;
import com.brilliance.isc.common.finmod.service.FinmodService;
import com.brilliance.isc.common.finmod.vo.*;
import com.brilliance.isc.common.sysmod.SysmodService;
import com.brilliance.isc.common.util.StringUtils;
import com.brilliance.isc.common.util.Utils;
import com.brilliance.isc.common.vo.PtsptaVo;
import com.brilliance.isc.esb.model.inner.*;
import com.brilliance.isc.mda.dao.FinmodMapper;
import com.brilliance.isc.mda.dao.PublicMapper;
import com.brilliance.isc.mda.dao.TaxMapper;
import com.brilliance.isc.vo.model.DinmodVo;
import com.brilliance.isc.vo.model.FinmodVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

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

@Service
public class FinmodServiceImpl implements FinmodService {

    @Autowired
    private TaxMapper taxMapper;

    @Autowired
    private PtsptaToolComponent ptsptaToolComponent;

    @Autowired
    private PtyCacheService ptyCacheService;

    @Autowired
    private CodetableService codetableService;

    @Autowired
    private FinmodMapper finmodMapper;

    @Autowired
    private PublicMapper publicMapper;

    @Autowired
    private SysmodService sysmodService;

    @Autowired(required = false)
    private IFinanceFeign financeFeign;

    private static final String TAX_CODE = "T000000001";

    @Override
    public void initFinmod(FinmodVo finmodVo) {
        boolean flag = sysmodService.isBusinessOpen("DINCHK");
        if (flag) {
            finmodVo.setOpnflg("Y");
        } else {
            finmodVo.setOpnflg("N");
        }
        //finmodVo.setOpnflg(Inis.getIniBlock("intace.ini", "INFCPC3", "open"));
    }

    @Override
    public void defaultFinmod(FinmodVo finmodVo) {
        if (!finmodVo.isModified("trntyp")) {
            finmodVo.setTrntyp("0");
        }
        if (!finmodVo.isModified("intflg")) {
            finmodVo.setIntflg("1");
        }
        if (!finmodVo.isModified("cmpflg")) {
            finmodVo.setCmpflg("1");
        }
        if (!finmodVo.isModified("intprd")) {
            finmodVo.setIntprd("6");
        }
        /**
         #按核心名称
         $allowedvalues = "1" + CR + "A" + CR + "4" + CR + "5" + CR + "D" + CR + "E" + CR + "F" + CR + "2" + CR + "7" + CR + "8" + CR + "9"
         SetValues( RATCHGTYP, $allowedvalues )
         **/
        if (!finmodVo.isModified("ratchgtyp")) {
            finmodVo.setRatchgtyp("1");
        }
        if (!finmodVo.isModified("ovdinttyp")) {
            finmodVo.setOvdinttyp("1");
        }
        //---BRTSET交易宽限期有为0的情况---
        /**
         if not IsModified( GRADAY ) then
         **/
        if (MdaUtils.compareTo(Strings.mid(SettleContext.getTransName(), 1, 2), "BP") == 0) {
            finmodVo.setGraday(10);
        } else {
            finmodVo.setGraday(5);
        }
        Tax tax = taxMapper.getTaxByCode(TAX_CODE);
        if (tax != null) {
            finmodVo.setShuilv(tax.getTaxrat());
        }
        //文青要求在开立和修改交易放开此域
        //Systems.resetEnabled(this,"graday");
        finmodVo.resetEnabled("graday");

        //执行default rule
        this.defaultFixprdN1000(finmodVo);
        this.defaultCbsCbeCurN1000(finmodVo);
        this.defaultWjdkflN1000(finmodVo);
        this.defaultDingjia2N1400(finmodVo);
        this.defaultDinmodJzllgzN900(finmodVo);
    }

    @Override
    public FinmodVo loadDataFromBlock(String objinr, String objtyp, String finblk) {
        Finmod finmod = finmodMapper.getFinmodByObjinrAndObjtyp(objinr, objtyp);
        if (finmod != null) {
            FinmodVo finmodVo = new FinmodVo();
            finmodVo.reset();
            finmodVo.setTrntyp(finmod.getTrntyp());
            finmodVo.setFincod(finmod.getFincod());
            finmodVo.setConref(finmod.getConref());
            finmodVo.setIntprd(finmod.getIntprd());
            finmodVo.setFixprd(finmod.getFixprd());
            finmodVo.setIntdat(finmod.getIntdat());
            finmodVo.setRatchgtyp(finmod.getRatchgtyp());
            finmodVo.setRatchgprd(finmod.getRatchgprd());
            finmodVo.setRatchgdat(finmod.getRatchgdat());
            finmodVo.setGraday(finmod.getGraday());
            finmodVo.setFlttyp(finmod.getFlttyp());
            finmodVo.setFltval(finmod.getFltval());
            finmodVo.setOvdinttyp(finmod.getOvdinttyp());
            finmodVo.setOvdintrat(finmod.getOvdintrat());
            finmodVo.setIntflg(finmod.getIntflg());
            finmodVo.setCmpflg(finmod.getCmpflg());
            finmodVo.setAct1(finmod.getAct1());
            finmodVo.setAct2(finmod.getAct2());
            finmodVo.setAct3(finmod.getAct3());
            finmodVo.setActtyp(finmod.getActtyp());
            finmodVo.setUsr(finmod.getUsr());
            finmodVo.setBranch(finmod.getBranch());
            finmodVo.setAccbch(finmod.getAccbch());
            finmodVo.setIntamt(finmod.getIntamt());
            finmodVo.setIntact(finmod.getIntact());
            finmodVo.setWjdkfl(finmod.getWjdkfl());
            finmodVo.setInttyp(finmod.getInttyp());
            finmodVo.setDinjdm(finmod.getDinjdm());
            finmodVo.getDinmod().setDinjdm(finmod.getDinjdm());
            finmodVo.getDinmod().setRmbsbs(finmod.getRmbsbs());
            String fin6153Data = finmod.getFin6153();
            if(!MdaUtils.isEmpty(fin6153Data)){
                InnerReq6153 innerReq6153 = Utils.toObject(InnerReq6153.class, fin6153Data);
                if(innerReq6153 != null){
                    finmodVo.getDinmod().setJzllgz(innerReq6153.fentgz);
                }
            }
            return finmodVo;
        } else if (StringUtils.isNotEmpty(finblk)) {
            FinmodVo finmodVo = new FinmodVo();
            finmodVo.reset();
            String block = finblk;
            int pos = Strings.pos(block, TAB);
            finmodVo.setTrntyp(Strings.mid(block, 1, pos - 1));
            block = Strings.mid(block, pos + 1);
            pos = Strings.pos(block, TAB);
            finmodVo.setFincod(Strings.mid(block, 1, pos - 1));
            block = Strings.mid(block, pos + 1);
            pos = Strings.pos(block, TAB);
            finmodVo.setConref(Strings.mid(block, 1, pos - 1));
            block = Strings.mid(block, pos + 1);
            pos = Strings.pos(block, TAB);
            finmodVo.setIntprd(Strings.mid(block, 1, pos - 1));
            block = Strings.mid(block, pos + 1);
            pos = Strings.pos(block, TAB);
            finmodVo.setFixprd(Strings.parseDecimal(Strings.mid(block, 1, pos - 1)).intValue());
            block = Strings.mid(block, pos + 1);
            pos = Strings.pos(block, TAB);
            finmodVo.setIntdat(Strings.mid(block, 1, pos - 1));
            block = Strings.mid(block, pos + 1);
            pos = Strings.pos(block, TAB);
            finmodVo.setRatchgtyp(Strings.mid(block, 1, pos - 1));
            block = Strings.mid(block, pos + 1);
            pos = Strings.pos(block, TAB);
            finmodVo.setRatchgprd(Strings.mid(block, 1, pos - 1));
            block = Strings.mid(block, pos + 1);
            pos = Strings.pos(block, TAB);
            finmodVo.setRatchgdat(Strings.mid(block, 1, pos - 1));
            block = Strings.mid(block, pos + 1);
            pos = Strings.pos(block, TAB);
            finmodVo.setGraday(Strings.parseDecimal(Strings.mid(block, 1, pos - 1)).intValue());
            block = Strings.mid(block, pos + 1);
            pos = Strings.pos(block, TAB);
            finmodVo.setFlttyp(Strings.mid(block, 1, pos - 1));
            block = Strings.mid(block, pos + 1);
            pos = Strings.pos(block, TAB);
            finmodVo.setFltval(Strings.parseDecimal(Strings.mid(block, 1, pos - 1)));
            block = Strings.mid(block, pos + 1);
            pos = Strings.pos(block, TAB);
            finmodVo.setOvdinttyp(Strings.mid(block, 1, pos - 1));
            block = Strings.mid(block, pos + 1);
            pos = Strings.pos(block, TAB);
            finmodVo.setOvdintrat(Strings.parseDecimal(Strings.mid(block, 1, pos - 1)));
            block = Strings.mid(block, pos + 1);
            pos = Strings.pos(block, TAB);
            finmodVo.setIntflg(Strings.mid(block, 1, pos - 1));
            block = Strings.mid(block, pos + 1);
            pos = Strings.pos(block, TAB);
            finmodVo.setCmpflg(Strings.mid(block, 1, pos - 1));
            block = Strings.mid(block, pos + 1);
            pos = Strings.pos(block, TAB);
            finmodVo.setAct1(Strings.mid(block, 1, pos - 1));
            block = Strings.mid(block, pos + 1);
            pos = Strings.pos(block, TAB);
            finmodVo.setAct2(Strings.mid(block, 1, pos - 1));
            block = Strings.mid(block, pos + 1);
            pos = Strings.pos(block, TAB);
            finmodVo.setAct3(Strings.mid(block, 1, pos - 1));
            block = Strings.mid(block, pos + 1);
            pos = Strings.pos(block, TAB);
            finmodVo.setActtyp(Strings.mid(block, 1, pos - 1));
            block = Strings.mid(block, pos + 1);
            pos = Strings.pos(block, TAB);
            finmodVo.setUsr(Strings.mid(block, 1, pos - 1));
            block = Strings.mid(block, pos + 1);
            pos = Strings.pos(block, TAB);
            finmodVo.setBranch(Strings.mid(block, 1, pos - 1));
            block = Strings.mid(block, pos + 1);
            pos = Strings.pos(block, TAB);
            finmodVo.setAccbch(Strings.mid(block, 1, pos - 1));
            block = Strings.mid(block, pos + 1) + TAB;
            pos = Strings.pos(block, TAB);
            finmodVo.setIntamt(Strings.parseDecimal(Strings.mid(block, 1, pos - 1)));
            block = Strings.mid(block, pos + 1);
            pos = Strings.pos(block, TAB);
            finmodVo.setIntact(Strings.mid(block, 1, pos - 1));
            block = Strings.mid(block, pos + 1);
            pos = Strings.pos(block, TAB);
            /**
             WJDKFL = Trim( Mid( $Block, $Pos + 1 ) )
             SetModified( WJDKFL )
             **/
            String wjdkfl = Strings.mid(block, pos - 1);
            finmodVo.setWjdkfl(wjdkfl);
            block = Strings.mid(block, pos + 1);
            pos = Strings.pos(block, TAB);
            finmodVo.setInttyp(Strings.mid(block, 1, pos - 1));
            block = Strings.mid(block, pos + 1);
            pos = Strings.pos(block, TAB);
            finmodVo.setDinjdm(Strings.mid(block, 1, pos - 1));
            finmodVo.getDinmod().setDinjdm(finmodVo.getDinjdm());
            block = Strings.mid(block, pos + 1);
            pos = Strings.pos(block, TAB);
            finmodVo.getDinmod().setRmbsbs(Strings.mid(block, 1, pos - 1));
            return finmodVo;
        }
        return null;
    }

    @Override
    public Finmod saveDataToBlock(FinmodVo finmodVo) {
        Finmod finmod = new Finmod();
        finmod.setTrntyp(finmodVo.getTrntyp());
        finmod.setFincod(finmodVo.getFincod());
        finmod.setConref(finmodVo.getConref());
        finmod.setIntprd(finmodVo.getIntprd());
        finmod.setFixprd(finmodVo.getFixprd());
        finmod.setIntdat(finmodVo.getIntdat());
        finmod.setRatchgtyp(finmodVo.getRatchgtyp());
        finmod.setRatchgprd(finmodVo.getRatchgprd());
        finmod.setRatchgdat(finmodVo.getRatchgdat());
        finmod.setGraday(finmodVo.getGraday());
        finmod.setFlttyp(finmodVo.getFlttyp());
        finmod.setFltval(finmodVo.getFltval());
        finmod.setOvdinttyp(finmodVo.getOvdinttyp());
        finmod.setOvdintrat(finmodVo.getOvdintrat());
        finmod.setIntflg(finmodVo.getIntflg());
        finmod.setCmpflg(finmodVo.getCmpflg());
        finmod.setAct1(finmodVo.getAct1());
        finmod.setAct2(finmodVo.getAct2());
        finmod.setAct3(finmodVo.getAct3());
        finmod.setActtyp(finmodVo.getActtyp());
        finmod.setUsr(finmodVo.getUsr());
        finmod.setBranch(finmodVo.getBranch());
        finmod.setAccbch(finmodVo.getAccbch());
        finmod.setIntamt(finmodVo.getIntamt());
        finmod.setIntact(finmodVo.getIntact());
        finmod.setWjdkfl(finmodVo.getWjdkfl());
        finmod.setInttyp(finmodVo.getInttyp());
        finmod.setDinjdm(finmodVo.getDinjdm());
        finmod.setRmbsbs(finmodVo.getDinmod().getRmbsbs());
        return finmod;
    }

    @Override
    public String save6153Data(FinmodVo finmodVo) {
        Map<String, String> maps = new LinkedHashMap<>();
        maps.put("bustyp", finmodVo.getTrntyp());
        maps.put("jiejuh", finmodVo.getFincod());
        maps.put("hetobh", Strings.trim(Strings.mid(finmodVo.getFincod(), 1, 16)));
        String objtyp = finmodVo.getRec().getObjtyp();
        String role = "";
        if ("BPD".equals(objtyp)) {
            role = "FIA";
        } else {
            role = "FIP";
        }
        PtsptaVo ptsptaVo = ptsptaToolComponent.getPtsFromListByRol(finmodVo.getPtsptaList(), role);
        if (ptsptaVo != null) {
            Pty pty = ptyCacheService.getPtyById(ptsptaVo.getPtyinr());
            if (pty != null) {
                maps.put("kehuha", pty.getExtkey());
                maps.put("kehucn", pty.getNam1());
            }
        }
        Map<String, Cbb> cbsMap = finmodVo.getCbsMap();
        Cbb maxCbb = cbsMap.getOrDefault("MAX", new Cbb());
        String cur = maxCbb.getCur();
        String curid = codetableService.getTblLabel(cur, "CURKEY");
        maps.put("huobdh", curid);
        String type = "";
        String name = "";
        String fintyp = finmodVo.getRec().getFintyp();
        if ("BPD".equals(objtyp)) {
            if ("M".equals(fintyp)) {
                type = "12520";
                name = "卖方押汇";
            } else if ("T".equals(fintyp)) {
                type = "02550";
                name = "议付信用证款项";
            } else if ("P".equals(fintyp) || "I".equals(fintyp)) {
                type = "01350";
                name = "出口押汇";
            } else if ("F".equals(fintyp)) {
                type = "00550";
                name = "买入外币票据";
            } else if ("D".equals(fintyp)) {
                type = "21400";
                name = "外币票据贴现";
            } else if ("U".equals(fintyp)) {
                type = "10510";
                name = "风险分担票据包买";
            } else if ("G".equals(fintyp)) {
                type = "26840";
                name = "买入国内信用证下债权";
            } else if ("V".equals(fintyp)) {
                type = "27160";
                name = "国内证风险分担债权包买";
            }
        } else {
            if ("DRF".equals(fintyp)) {
                type = "12510";
                name = "买方押汇";
            } else {
                type = "01360";
                name = "进口押汇";
            }
        }
        maps.put("debtyp", type);
        maps.put("dbtyna", name);
        if (maxCbb.getAmt() != null) {
            maps.put("zdjine", maxCbb.getAmt().toPlainString());
        }
        maps.put("blrzzl", "");
        maps.put("jxzouq", finmodVo.getIntprd());
        maps.put("gudizq", String.valueOf(finmodVo.getFixprd()));
        maps.put("jixirq", finmodVo.getIntdat());
        maps.put("lltzfs", finmodVo.getRatchgtyp());
        maps.put("lltzzq", finmodVo.getRatchgprd());
        maps.put("lltzyr", finmodVo.getRatchgdat());
        Date date = null;
        if ("BPD".equals(objtyp)) {
            date = finmodVo.getRec().getOpndat();
        } else {
            date = finmodVo.getRec().getStttendat();
        }
        if (date != null) {
            maps.put("kihurq", Utils.fmtDate(date));
        }
        date = finmodVo.getRec().getMatdat();
        if (date != null) {
            maps.put("daoqir", Utils.fmtDate(date));
        }
        maps.put("kuxiqi", String.valueOf(finmodVo.getGraday()));
        if (finmodVo.getRec().getIntrat() != null) {
            maps.put("jzunll", finmodVo.getRec().getIntrat().toPlainString());
        }
        if ("BPD".equals(objtyp)) {
            if (finmodVo.getRec().getTolrat() != null) {
                maps.put("zixill", finmodVo.getRec().getTolrat().toPlainString());
            }
        } else {
            if (finmodVo.getRec().getActrat() != null) {
                maps.put("zixill", finmodVo.getRec().getActrat().toPlainString());
            }
        }
        maps.put("fudofs", finmodVo.getFlttyp());
        if (finmodVo.getFltval() != null) {
            maps.put("fudonz", finmodVo.getFltval().toPlainString());
        }
        maps.put("yqjxfs", finmodVo.getOvdinttyp());
        if (finmodVo.getOvdintrat() != null) {
            maps.put("faxibl", finmodVo.getOvdintrat().toPlainString());
        }
        maps.put("soxilx", finmodVo.getIntflg());
        maps.put("jfxibz", finmodVo.getCmpflg());
        maps.put("dyhkzh", finmodVo.getAct1());
        maps.put("dehkzh", finmodVo.getAct2());
        maps.put("dshkzh", finmodVo.getAct3());
        maps.put("dshkfs", finmodVo.getActtyp());
        maps.put("credgy", finmodVo.getUsr());
        maps.put("fadibm", finmodVo.getBranch());
        maps.put("llzxdc", "");
        maps.put("ruzajg", finmodVo.getAccbch());
        maps.put("caozbz", finmodVo.getItftyp());
        if (maxCbb.getAmt() != null) {
            maps.put("hetoje", maxCbb.getAmt().toPlainString());
        }
        maps.put("lxfkzh", finmodVo.getIntact());
        maps.put("wjdkfl", finmodVo.getWjdkfl());
        if (finmodVo.getShuilv() != null) {
            maps.put("shuilv", finmodVo.getShuilv().toPlainString());
        }
        maps.put("fentgz", finmodVo.getDinmod().getJzllgz());
        maps.put("shijcq", finmodVo.getRec().getLprtyp());
        maps.put("zhidrq", "");
        maps.put("tiozrq", "");
        maps.put("zuicqs", "");
        return JSONObject.toJSONString(maps);
    }

    @Override
    public String save6205Data(FinmodVo finmodVo) {
        Map<String, String> maps = new LinkedHashMap<>();
        String objtyp = finmodVo.getRec().getObjtyp();
        maps.put("JIEJUH", finmodVo.getFincod());
        maps.put("JXZOUQ", finmodVo.getIntprd());
        maps.put("GUDIZQ", String.valueOf(finmodVo.getFixprd()));
        maps.put("JIXIRQ", finmodVo.getIntdat());
        maps.put("LLTZFS", finmodVo.getRatchgtyp());
        maps.put("LLTZYR", finmodVo.getRatchgdat());
        maps.put("LLTZZQ", finmodVo.getRatchgprd());
        Date date = finmodVo.getRec().getMatdat();
        if (date != null) {
            maps.put("DAOQIR", Utils.fmtDate(date));
        }
        maps.put("KUXIQI", String.valueOf(finmodVo.getGraday()));
        maps.put("SFNZDK", "0");
        maps.put("FUDOFS", finmodVo.getFlttyp());
        if (finmodVo.getFltval() != null) {
            maps.put("FUDONZ", finmodVo.getFltval().toPlainString());
        }
        if ("BPD".equals(objtyp)) {
            if (finmodVo.getRec().getTolrat() != null) {
                maps.put("ZIXILL", finmodVo.getRec().getTolrat().toPlainString());
            }
        } else {
            if (finmodVo.getRec().getActrat() != null) {
                maps.put("ZIXILL", finmodVo.getRec().getActrat().toPlainString());
            }
        }
        maps.put("YQJXFS", finmodVo.getOvdinttyp());
        if (finmodVo.getOvdintrat() != null) {
            maps.put("FAXIBL", finmodVo.getOvdintrat().toPlainString());
        }
        maps.put("WYJXFS", "");
        if (finmodVo.getViorat() != null) {
            maps.put("WYLILU", finmodVo.getViorat().toPlainString());
        }
        maps.put("DYHKZH", finmodVo.getAct1());
        maps.put("DEHKZH", finmodVo.getAct2());
        maps.put("DSHKZH", finmodVo.getAct3());
        maps.put("DSHKFS", finmodVo.getActtyp());
        maps.put("YTDKZH", "");
        maps.put("WTDWZH", "");
        maps.put("WTKSBZ", "");
        maps.put("WTKSSL", "0");
        maps.put("SOXILX", finmodVo.getIntflg());
        maps.put("JFXIBZ", finmodVo.getCmpflg());
        maps.put("YAHUZL", finmodVo.getPurtyp());
        maps.put("YAHUBH", finmodVo.getPurcod());
        maps.put("KOKUBH", "");
        maps.put("BLRZZL", "");
        Cbb maxcbb = finmodVo.getCbsMap().getOrDefault("MAX", new Cbb());
        if (maxcbb.getAmt() != null) {
            maps.put("ZDJINE", maxcbb.getAmt().toPlainString());
        }
        maps.put("LLZXDC", "");
        maps.put("ACCBCH", finmodVo.getAccbch());
        maps.put("WJDKFL", finmodVo.getWjdkfl());
        maps.put("SHFBZ1", "1");
        maps.put("FENTGZ", finmodVo.getDinmod().getJzllgz());
        maps.put("SHIJCQ", finmodVo.getRec().getLprtyp());
        return JSONObject.toJSONString(maps);
    }

    @Override
    public String save6107Data(FinmodVo finmodVo) {
        String ovdflg = finmodVo.getRec().getOvdflg();
        if (StringUtils.isEmpty(ovdflg)) {
            throw new CommonServiceException("移植有误数据，暂时无法还款");
        }
        Map<String, String> maps = new LinkedHashMap<>();
        maps.put("jiejuh", finmodVo.getFincod());
        maps.put("dakuzh", finmodVo.getRec().getFinact());
        maps.put("hukufs", finmodVo.getReptyp());
        BigDecimal amt = Decimals.add(finmodVo.getCbs().getCbe().getAmt(), finmodVo.getCbs().getTotint());
        maps.put("hukuze", amt.toPlainString());
        maps.put("joyije", "0");
        String act = finmodVo.getRec().getSetact();
        maps.put("hukuzh", act);
        if (finmodVo.getCbs().getCbb().getIntamt1() != null) {
            maps.put("bwqxlx", finmodVo.getCbs().getCbb().getIntamt1().toPlainString());
        }
        if (finmodVo.getCbs().getCbb().getIntamty1() != null) {
            maps.put("bowiqx", finmodVo.getCbs().getCbb().getIntamty1().toPlainString());
        }
        if (finmodVo.getCbs().getCbb().getIntamt2() != null) {
            maps.put("bnqxlx", finmodVo.getCbs().getCbb().getIntamt2().toPlainString());
        }
        if (finmodVo.getCbs().getCbb().getIntamty2() != null) {
            maps.put("boneqx", finmodVo.getCbs().getCbb().getIntamty2().toPlainString());
        }
        if (finmodVo.getCbs().getCbb().getIntamt() != null) {
            maps.put("bejilx", finmodVo.getCbs().getCbb().getIntamt().toPlainString());
        }
        if (finmodVo.getCbs().getCbb().getAmt() != null) {
            maps.put("benjin", finmodVo.getCbs().getCbb().getAmt().toPlainString());
        }
        if (finmodVo.getCbs().getCbe().getIntamt1() != null) {
            maps.put("chbwql", finmodVo.getCbs().getCbe().getIntamt1().toPlainString());
        }
        if (finmodVo.getCbs().getCbe().getIntamty1() != null) {
            maps.put("chbwqx", finmodVo.getCbs().getCbe().getIntamty1().toPlainString());
        }
        if (finmodVo.getCbs().getCbe().getIntamt2() != null) {
            maps.put("chbnql", finmodVo.getCbs().getCbe().getIntamt2().toPlainString());
        }
        if (finmodVo.getCbs().getCbe().getIntamty2() != null) {
            maps.put("chbnqx", finmodVo.getCbs().getCbe().getIntamty2().toPlainString());
        }
        if (finmodVo.getCbs().getCbe().getIntamt() != null) {
            maps.put("chbjlx", finmodVo.getCbs().getCbe().getIntamt().toPlainString());
        }
        if (finmodVo.getCbs().getCbe().getAmt() != null) {
            maps.put("chdkbj", finmodVo.getCbs().getCbe().getAmt().toPlainString());
        }
        maps.put("fhdima", "");
        maps.put("pnzezl", "850");
        maps.put("pnzeph", "");
        maps.put("pnzexh", "");
        maps.put("qifarq", "");
        maps.put("zifumm", "");
        maps.put("pnzeho", "");
        maps.put("accbch", finmodVo.getAccbch());
        return JSONObject.toJSONString(maps);
    }

    /**
     * source:finmod.0000.script
     */
    public void defaultFixprdN1000(FinmodVo finmodVo) {
        if (MdaUtils.compareTo(finmodVo.getIntprd(), "5") == 0) {
            finmodVo.setEnabled("fixprd");
        } else {
            finmodVo.setFixprd(0);
            finmodVo.resetEnabled("fixprd");
        }
    }

    /**
     * source:finmod.0014.script
     */
    public void defaultCbsCbeCurN1000(FinmodVo finmodVo) {
        finmodVo.getCbs().getCbe().setCur(finmodVo.getCbs().getCbb().getCur());
    }

    public void defaultTotintN1000(FinmodVo finmodVo) {
        finmodVo.getCbs().setTotint(Decimals.add(Decimals.add(Decimals.add(Decimals.add(finmodVo.getCbs().getCbe().getIntamt(), finmodVo.getCbs().getCbe().getIntamt1()), finmodVo.getCbs().getCbe().getIntamty1()), finmodVo.getCbs().getCbe().getIntamt2()), finmodVo.getCbs().getCbe().getIntamty2()));
    }

    /**
     * source:finmod.0044.script
     */
    public void defaultWjdkflN1000(FinmodVo finmodVo) {
        finmodVo.resetEnabled("wjdkfl");
        if (!finmodVo.isModified("wjdkfl") || MdaUtils.isEmpty(finmodVo.getWjdkfl())) {
            finmodVo.setWjdkfl("1");
        }
    }

    /**
     * source:finmod.0056.script
     */
    public void defaultDingjia2N1400(FinmodVo finmodVo) {
        String frm = SettleContext.getDirectTransName();
        String role = "";
        switch (frm) {
            case "TRTOPN":
                role = "FIP";
                break;
            case "BPTOPN":
                role = "FIA";
                break;
            case "BRTSET":
            case "BCTSET":
            case "BDTSET":
            case "CPTOPN":
            case "CPTATO":
                role = "FIP";
                break;
        }
        PtsptaVo ptsptaVo = ptsptaToolComponent.getPtsFromListByRol(finmodVo.getPtsptaList(), role);
        String idtype = "";
        if (ptsptaVo != null) {
            Pty pty = ptyCacheService.getPtyById(ptsptaVo.getPtyinr());
            if (pty != null) {
                idtype = pty.getIdtype();
            }
        }
        if (MdaUtils.compareTo(idtype, "2") == 0) {
            finmodVo.resetEnabled("dinjdm");
            finmodVo.resetEnabled("dget");
            finmodVo.setDinjdm("");
            finmodVo.getDinmod().setVisflg("");
            finmodVo.setDinjdm(NULLSTR);
        } else {
            finmodVo.setEnabled("dinjdm");
            finmodVo.setEnabled("dget");
            if (MdaUtils.compareTo(finmodVo.getOpnflg(), "Y") == 0) {
                String fortyp = finmodVo.getRec().getFortyp();
                String ffttyp = finmodVo.getRec().getFfttyp();
                if (MdaUtils.compareTo(fortyp, "2") == 0 || MdaUtils.compareTo(ffttyp, "0") == 0 || MdaUtils.compareTo(ffttyp, "1") == 0) {
                    finmodVo.resetEnabled("dinjdm");
                    finmodVo.resetEnabled("dget");
                    finmodVo.setDinjdm("");
                }
            }
        }
    }

    /**
     * source:finmod.0057.script
     */
    public void defaultDinmodJzllgzN900(FinmodVo finmodVo) {
        String frame = SettleContext.getDirectTransName();
        String jzllgz = NULLSTR;
        String idtype = NULLSTR;
        String ffttyp = NULLSTR;
        String fortyp = NULLSTR;
        PtsptaVo ptsptaVo = null;
        switch (frame) {
            case "TRTOPN":
            case "TRTAME":
            case "BRTSET":
            case "BCTSET":
            case "BDTSET":
            case "CPTOPN":
            case "CPTATO":
                //Systems.setVisible(this.getDinmod(),"jzllgz");
                //Systems.setValues(this.getDinmod(),"jzllgz","0" + CR + "1");
                jzllgz = finmodVo.getRec().getJzllgz();
                if (MdaUtils.isEmpty(finmodVo.getDinmod().getJzllgz()) && !MdaUtils.isEmpty(jzllgz)) {
                    finmodVo.getDinmod().setJzllgz(jzllgz);
                }
                idtype = "";
                ptsptaVo = ptsptaToolComponent.getPtsFromListByRol(finmodVo.getPtsptaList(), "FIP");
                if (ptsptaVo != null) {
                    Pty pty = ptyCacheService.getPtyById(ptsptaVo.getPtyinr());
                    if (pty != null) {
                        idtype = pty.getIdtype();
                    }
                }
                if (MdaUtils.compareTo(finmodVo.getOpnflg(), "Y") == 0) {
                    if (MdaUtils.compareTo(idtype, "2") == 0) {
                        finmodVo.setEnabled("jzllgz");
                        if (MdaUtils.isEmpty(finmodVo.getDinmod().getJzllgz())) {
                            finmodVo.getDinmod().setJzllgz("1");
                        }
                    } else {
                        finmodVo.resetEnabled("jzllgz");
                        if (MdaUtils.isEmpty(finmodVo.getDinmod().getRmbsbs()) && MdaUtils.isEmpty(jzllgz)) {
                            finmodVo.getDinmod().setJzllgz(NULLSTR);
                        }
                    }
                } else {
                    finmodVo.setEnabled("jzllgz");
                    if (MdaUtils.isEmpty(finmodVo.getDinmod().getJzllgz())) {
                        finmodVo.getDinmod().setJzllgz("1");
                    }
                }
                break;
            case "BPTOPN":
            case "BPTAME":
                //Systems.setVisible(this.getDinmod(),"jzllgz");
                //Systems.setValues(this.getDinmod(),"jzllgz","0" + CR + "1");
                jzllgz = finmodVo.getRec().getJzllgz();
                if (MdaUtils.isEmpty(finmodVo.getDinmod().getJzllgz()) && !MdaUtils.isEmpty(jzllgz)) {
                    finmodVo.getDinmod().setJzllgz(jzllgz);
                }
                //客户类型
                idtype = "";
                ptsptaVo = ptsptaToolComponent.getPtsFromListByRol(finmodVo.getPtsptaList(), "FIA");
                if (ptsptaVo != null) {
                    Pty pty = ptyCacheService.getPtyById(ptsptaVo.getPtyinr());
                    if (pty != null) {
                        idtype = pty.getIdtype();
                    }
                }
                //福费廷转卖类型
                ffttyp = finmodVo.getRec().getFfttyp();
                //二级市场买入
                fortyp = finmodVo.getRec().getFortyp();
                if (MdaUtils.compareTo(finmodVo.getOpnflg(), "Y") == 0) {
                    if (MdaUtils.compareTo(idtype, "2") == 0 || !MdaUtils.isEmpty(ffttyp) || MdaUtils.compareTo(fortyp, "2") == 0) {
                        finmodVo.setEnabled("jzllgz");
                        if (MdaUtils.isEmpty(finmodVo.getDinmod().getJzllgz())) {
                            finmodVo.getDinmod().setJzllgz("1");
                        }
                    } else {
                        finmodVo.resetEnabled("jzllgz");
                        if (MdaUtils.isEmpty(finmodVo.getDinmod().getRmbsbs()) && MdaUtils.isEmpty(jzllgz)) {
                            finmodVo.getDinmod().setJzllgz(NULLSTR);
                        }
                    }
                } else {
                    finmodVo.setEnabled("jzllgz");
                    if (MdaUtils.isEmpty(finmodVo.getDinmod().getJzllgz())) {
                        finmodVo.getDinmod().setJzllgz("1");
                    }
                }
                break;
            case "BPTFFT":
            case "BPTFCT":
                //Systems.setVisible(this.getDinmod(),"jzllgz");
                //Systems.setValues(this.getDinmod(),"jzllgz","0" + CR + "1");
                jzllgz = finmodVo.getRec().getJzllgz();
                if (MdaUtils.isEmpty(finmodVo.getDinmod().getJzllgz()) && !MdaUtils.isEmpty(jzllgz)) {
                    finmodVo.getDinmod().setJzllgz(jzllgz);
                }
                finmodVo.resetEnabled("jzllgz");
                break;
            default:
                //Systems.resetVisible(this.getDinmod(),"jzllgz");
        }
    }

    @Override
    public Finmod2571Response call2571(Finmod2571Request request) {
        String type = "";
        if ("BPD".equalsIgnoreCase(request.getObjtyp())) {
            switch (request.getFintyp()) {
                case "P":
                case "I":
                case "M":
                    type = "01350";
                    break;
                case "U":
                    type = "10510";
                    break;
                case "F":
                    type = "00550";
                    break;
                case "D":
                case "T":
                    type = "21400";
                    break;
                case "G":
                    type = "26840";
                    break;
                case "V":
                    type = "27160";
                    break;
            }
        } else {
            type = "01360";
        }
        String curKey = codetableService.getTblLabel(request.getCur(), "CURKEY");
        String opnDateStr = Utils.fmtDate(request.getOpndat(), "yyyyMMdd");
        String matDateStr = Utils.fmtDate(request.getMatdat(), "yyyyMMdd");
        InnerReq2571 req2571 = new InnerReq2571(type, curKey, opnDateStr, matDateStr,
                request.getRatchgtyp(), request.getRatchgprd(), "0", request.getLpctyp(), request.getJzllgz());
        InnerReq<InnerReq2571> innerReq2571 = new InnerReq<>(req2571);
        Finmod2571Response response = new Finmod2571Response();
        //调用接口
        try {
            InnerRes<InnerRes2571> innerRes2571 = financeFeign.call2571(innerReq2571);
            if (innerRes2571 != null) {
                if ("AAAAAAA".equals(innerRes2571.header.code) && innerRes2571.DATA != null) {
                    response.setIntrat(new BigDecimal(innerRes2571.DATA.jizlil));
                } else {
                    response.setErrorMsg(innerRes2571.header.description);
                }
                response.setCode(innerRes2571.header.code);
            }
        } catch (Exception e) {
            response.setErrorMsg("实时查询基准利率2571接口调用失败");
        }
        return response;
    }

    @Override
    public Finmod6153Response call6153(FinmodVo finmodVo) {
        finmodVo.setItftyp("1");
        InnerReq6153 req6153 = Utils.toObject(InnerReq6153.class, this.save6153Data(finmodVo));
        Header header = new Header();
        InnerReq<InnerReq6153> innerReq6153 = new InnerReq<>(req6153);
        innerReq6153.header = header;
        Finmod6153Response response = new Finmod6153Response();
        try {
            InnerRes<InnerRes6153> innerRes6153 = financeFeign.call6153(innerReq6153);
            if (innerRes6153 != null) {
                if ("AAAAAAA".equals(innerRes6153.header.code) && innerRes6153.DATA != null) {
                    response.setIntamt(new BigDecimal(innerRes6153.DATA.ysxije));
                } else {
                    response.setErrorMsg(innerRes6153.header.description);
                }
                response.setCode(innerRes6153.header.code);
            }
        } catch (Exception e) {
            response.setErrorMsg("融资开户6153接口调用失败");
        }
        return response;
    }

    @Override
    public FinmodCPC3Response callCPC3(FinmodCPC3Request finmodCPC3Request) {
        InnerReqCPC3 reqCPC3 = new InnerReqCPC3(finmodCPC3Request.getDinjdm());
        InnerReq<InnerReqCPC3> innerReqCPC3 = new InnerReq<>(reqCPC3);
        FinmodCPC3Response response = new FinmodCPC3Response();
        response.reset();
        //调用接口
        try {
            InnerRes<InnerResCPC3> innerResCPC3 = financeFeign.callCPC3(innerReqCPC3);
            if (innerResCPC3 != null) {
                if ("AAAAAAA".equals(innerResCPC3.header.code) && innerResCPC3.DATA != null) {
                    response.getDinmod().setDinjdm(innerResCPC3.DATA.dinjdm);
                    response.getDinmod().setDinjls(innerResCPC3.DATA.dinjls);
                    response.getDinmod().setSxpfdm(innerResCPC3.DATA.sxpfdm);
                    response.getDinmod().setDjhtno(innerResCPC3.DATA.hetnho);
                    response.getDinmod().setSxlexi(innerResCPC3.DATA.sxlexg);
                    response.getDinmod().setSxlexi(innerResCPC3.DATA.sxlexg);
                    String snpizt = innerResCPC3.DATA.snpizt;
                    response.setDinSta(snpizt);
                    if ("0".equals(snpizt)) {
                        response.getDinmod().setSpzhta("审批未通过");
                    } else if ("1".equals(snpizt)) {
                        response.getDinmod().setSpzhta("审批通过");
                    } else if ("2".equals(snpizt)) {
                        response.getDinmod().setSpzhta("已撤销");
                    } else {
                        response.getDinmod().setSpzhta("待审批");
                    }
                    response.getDinmod().setOckhdm(innerResCPC3.DATA.ockhdm);
                    response.getDinmod().setHxkhdm(innerResCPC3.DATA.hxkhdm);
                    response.getDinmod().setChapdm(innerResCPC3.DATA.chpidm);
                    response.getDinmod().setChapmc(getCecmCpmc(innerResCPC3.DATA.chpidm));
                    response.getDinmod().setShpijg("".equals(innerResCPC3.DATA.shpijg) ? Decimals.ZERO_SCALE8 : new BigDecimal(innerResCPC3.DATA.shpijg));
                    response.getDinmod().setDakaje("".equals(innerResCPC3.DATA.dakaje) ? Decimals.ZERO_SCALE2 : new BigDecimal(innerResCPC3.DATA.dakaje));
                    if (!MdaUtils.isEmpty(innerResCPC3.DATA.bizhog)) {
                        response.getDinmod().setBizhog(codetableService.getTblLabel(innerResCPC3.DATA.bizhog, "CURVAL"));
                    }
                    response.getDinmod().setDakaqx("".equals(innerResCPC3.DATA.dakaqx) ? null : Long.parseLong(innerResCPC3.DATA.dakaqx));
                    String qixndw = innerResCPC3.DATA.qixndw;
                    if ("D".equals(qixndw)) {
                        response.getDinmod().setQixndw("天");
                    } else if ("M".equals(qixndw)) {
                        response.getDinmod().setQixndw("月");
                    } else if ("Y".equals(qixndw)) {
                        response.getDinmod().setQixndw("年");
                    } else {
                        response.getDinmod().setQixndw("");
                    }

                    //20贷款结束日
                    if (MdaUtils.compareTo(response.getDinmod().getDakaje(), 0) != 0) {
                        response.getDinmod().setMaxdje(Decimals.mul(response.getDinmod().getDakaje(), 1.05));
                        response.getDinmod().setMindje(Decimals.mul(response.getDinmod().getDakaje(), 0.95));
                    }
                    if (response.getDinmod().getDakaqx() != 0) {
                        response.getDinmod().setMaxqix(response.getDinmod().getDakaqx() + 5);
                        response.getDinmod().setMinqix(response.getDinmod().getDakaqx() - 5);
                    }
                    //21拟定价格标识
                    String ndjgbs = innerResCPC3.DATA.ndjgbs;
                    if (MdaUtils.isEmpty(ndjgbs)) {
                        ndjgbs = "0";
                    }
                    response.getDinmod().setNdjgbs(ndjgbs);

                    response.getDinmod().setDinjlb("".equals(innerResCPC3.DATA.dinjlb) ? Decimals.ZERO_SCALE8 : new BigDecimal(innerResCPC3.DATA.dinjlb));
                    response.getDinmod().setSheplb("".equals(innerResCPC3.DATA.sheplb) ? Decimals.ZERO_SCALE8 : new BigDecimal(innerResCPC3.DATA.sheplb));
                    response.getDinmod().setLibor("".equals(innerResCPC3.DATA.cpc3lb) ? Decimals.ZERO_SCALE8 : new BigDecimal(innerResCPC3.DATA.cpc3lb));
                    response.getDinmod().setSpjgdc("".equals(innerResCPC3.DATA.shepdc) ? Decimals.ZERO_SCALE8 : new BigDecimal(innerResCPC3.DATA.shepdc));

                    String sql = "SELECT TAXRAT FROM TAX WHERE TAXCOD='T000000001'";
                    Map<String, Object> dyncMap = new HashMap<>();
                    dyncMap.put("sql", sql);
                    List<BigDecimal> taxratList = publicMapper.dyncQueryNumList(dyncMap);
                    if (taxratList != null && taxratList.size() > 0) {
                        response.getDinmod().setShuilv(taxratList.get(0));
                    } else {
                        response.getDinmod().setShuilv(new BigDecimal("6"));
                    }
                    BigDecimal shuilv = new BigDecimal(innerResCPC3.DATA.shuilv);
                    if (MdaUtils.compareTo(shuilv, 0) == 0 || !MdaUtils.isEmpty(shuilv)) {
                        response.getDinmod().setShuilv(shuilv);
                    }
                    response.getDinmod().setShpjgh("".equals(innerResCPC3.DATA.shpjgh) ? Decimals.ZERO_SCALE8 : new BigDecimal(innerResCPC3.DATA.shpjgh));
                    response.getDinmod().setShpdch("".equals(innerResCPC3.DATA.shpdch) ? Decimals.ZERO_SCALE8 : new BigDecimal(innerResCPC3.DATA.shpdch));
                    String rmbsbs = innerResCPC3.DATA.rmbsbs;
                    response.getDinmod().setRmbsbs(rmbsbs);
                    if (MdaUtils.compareTo(rmbsbs, "0") == 0 || MdaUtils.compareTo(rmbsbs, "1") == 0) {
                        response.getDinmod().setJzllgz(rmbsbs);
                    }
                    response.getDinmod().setLprtyp(innerResCPC3.DATA.lprtyp);
                } else {
                    response.setErrorMsg(innerResCPC3.header.description);
                }
                response.setCode(innerResCPC3.header.code);
            }
        } catch (Exception e) {
            response.setErrorMsg("实时查询定价系统CPC3接口调用失败");
        }
        if (StringUtils.isNotEmpty(response.getErrorMsg())) {
            response.setDinmod(new DinmodVo());
        }
        return response;
    }

    /**
     * source:finmod.0025.script
     */
    public boolean isCreFinTrn(FinmodVo finmodVo) {
        boolean boolVar = false;
        if (!MdaUtils.isEmpty(finmodVo.getIntprd())) {
            switch (SettleContext.getDirectTransName()) {
                case "BPTOPN":
                case "TRTOPN":
                    boolVar = true;
                    /**
                     case "BRTSET", "BCTSET", "BDTSET"
                     **/
                    break;
                case "BRTSET":
                case "BCTSET":
                case "BDTSET":
                case "CPTOPN":
                case "CPTATO":
                    if ("X".equalsIgnoreCase(finmodVo.getRec().getCrefinflg())) {
                        boolVar = true;
                    }
                    break;
                case "BPTAME":
                case "TRTAME":
                    if ("A".equalsIgnoreCase(finmodVo.getRec().getAMETYP())) {
                        boolVar = true;
                    }
                    break;
            }
        }
        return boolVar;

    }

    @Override
    public String getCecmCpmc(String argCode) {
        //!获取CECM产品代码对应的名称
        //!==================================================
        //!返回值：无
        //!变量: cecm产品代码名称
        //!==================================================
        String cpdmmc = "";
        switch (argCode) {
            case "0502":
                cpdmmc = "进口信用证押汇";
                break;
            case "0503":
                cpdmmc = "进口代收押汇";
                break;
            case "0504":
                cpdmmc = "汇出汇款押汇";
                break;
            case "0505":
                cpdmmc = "合格出口信用证押汇";
                break;
            case "0506":
                cpdmmc = "不合格出口信用证押汇";
                break;
            case "0507":
                cpdmmc = "出口托收押汇";
                break;
            case "0508":
                cpdmmc = "汇入汇款押汇";
                break;
            case "0509":
                cpdmmc = "合格出口信用证贴现";
                break;
            case "0510":
                cpdmmc = "不合格出口信用证贴现";
                break;
            case "051101":
                cpdmmc = "我行买断出口信用证项下票据";
                break;
            case "051102":
                cpdmmc = "我行转卖出口信用证项下债权";
                break;
            case "051103":
                cpdmmc = "第三方风险参与的信用证项下票据包买";
                break;
            case "051104":
                cpdmmc = "我行买断进口信用证项下票据";
                break;
            case "051602":
                cpdmmc = "买方押汇";
                break;
            case "05160301":
                cpdmmc = "合格卖方押汇";
                break;
            case "05160302":
                cpdmmc = "不合格卖方押汇";
                break;
            case "05160401":
                cpdmmc = "合格议付";
                break;
            case "05160402":
                cpdmmc = "不合格议付";
                break;
            case "05160501":
                cpdmmc = "我行买断国内信用证卖方项下债权";
                break;
            case "05160503":
                cpdmmc = "第三方风险参与的国内信用证项下债权包买";
                break;
            default:
                cpdmmc = "";
        }

        return cpdmmc;
    }

}
