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

import com.brilliance.mda.runtime.mda.util.*;
import com.brilliance.isc.bo.Cbb;
import com.brilliance.isc.bo.Pbl;
import com.brilliance.isc.bo.Pty;
import com.brilliance.isc.common.bop.service.PblmodService;
import com.brilliance.isc.common.bop.service.Rmb2122Service;
import com.brilliance.isc.common.bopset.converter.PblInfoBuilder;
import com.brilliance.isc.common.bopset.counter.service.CounterRmbService;
import com.brilliance.isc.common.cache.pty.PtyCacheService;
import com.brilliance.isc.common.component.PtsptaToolComponent;
import com.brilliance.isc.common.context.BopContext;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.liaallg.vo.LiaallgVo;
import com.brilliance.isc.common.rmbbop.entity.Tdo;
import com.brilliance.isc.common.rmbbop.vo.Rmb2122;
import com.brilliance.isc.common.util.BopUtils;
import com.brilliance.isc.common.util.StringUtils;
import com.brilliance.isc.common.vo.BizInfoVo;
import com.brilliance.isc.common.vo.PtsptaVo;
import com.brilliance.isc.mda.dao.PblMapper;
import com.brilliance.isc.mda.dao.TdoMapper;
import com.brilliance.isc.vo.lg.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

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


@Service("tdo")
public class Rmb2122ServiceImpl implements Rmb2122Service {

    @Autowired
    private PblmodService pblmodService;

    @Autowired
    private CounterRmbService counterRmbService;

    @Autowired
    private PtyCacheService ptyCacheService;

    @Autowired
    private PblMapper pblMapper;

    @Autowired
    private TdoMapper tdoMapper;

    @Autowired
    private PtsptaToolComponent ptsptaToolComponent;

    @Override
    public void save(Rmb2122 rmb2122) {
        //保存人民币跨境支出信息
        if (MdaUtils.compareTo(rmb2122.getRmbflg(), "1") == 0) {
            String objinr = rmb2122.getRecgrp().getAds().getInr();
            if (!MdaUtils.isEmpty(objinr)) {
                Pbl pbl = pblMapper.selectByObjtypAndObjinr("TDO", objinr);
                rmb2122.getPblmod().setWrkpbl(pbl);
            }
            pblmodService.storeAllinBusTx(rmb2122);

        }
    }

    @Override
    public void updrmb(Rmb2122 rmb2122) {
        if (StringUtils.isNotEmpty(rmb2122.getOwnref())) {
            if (StringUtils.isEmpty(rmb2122.getAcp())) {
                Tdo tdo = tdoMapper.selectTdoByOwnref(rmb2122.getOwnref());
                if (tdo != null) {
                    rmb2122.getRecgrp().setAds(tdo);
                    Pbl pbl = pblMapper.selectByObjtypAndObjinr("TDO", tdo.getInr());
                    rmb2122.getPblmod().setPbl(pbl);
                    rmb2122.setAcp("X");
                }
            }
        } else {
            pblmodService.mappingRMBDataCommon(rmb2122);
        }
        mappingRMBDataAAAAAA(rmb2122);
    }

    @Override
    public void saveSpt(Rmb2122 rmb2122) {
        BopUtils.saveSpt(rmb2122.getObjtyp() + "ADD");
    }

    private void mappingRMBDataAAAAAA(Rmb2122 rmb2122) {
        List<PtsptaVo> ptsList = BopContext.getPtsList();
        BizInfoVo bizInfo = BopContext.getBizInfo();
        if (ptsList != null && ptsList.size() > 0) {
            for (PtsptaVo ptspta : ptsList) {
                ptsptaToolComponent.fillPtsptaAllProperties(ptspta);
            }
        }
        String ownref = bizInfo.getOwnref();
        rmb2122.setOwnref(ownref);
        String rmbownref = rmb2122.getRecgrp().getAds().getBanktrano();
        String levyno = "";
        if (!MdaUtils.isEmpty(ownref) && MdaUtils.compareTo(ownref, rmbownref) != 0) {
            rmb2122.getRecgrp().getAds().setBanktrano(ownref);
            String msgtyp = "2122";
            levyno = getRmbRef(msgtyp, ownref);
        } else {
            levyno = rmb2122.getRecgrp().getAds().getLevyno();
        }

        rmb2122.getRecgrp().getAds().setLevyno(levyno);
//        rmb2122.getRecgrp().getAds().setVouchno(levyno);

        // 银行机构代码
        String lhanam = NULLSTR;
        if (!MdaUtils.isEmpty(SettleContext.getUserSession().getWrkbch())) {
            lhanam = SettleContext.getUserSession().getWrkbch().getLhanum();
        }

        rmb2122.getRecgrp().getAds().setLeorgcode(lhanam);

        //业务所属机构
        if (!MdaUtils.isEmpty(SettleContext.getUserSession().getWrkbch())) {
            rmb2122.getRecgrp().getAds().setOwnextkey(SettleContext.getUserSession().getWrkbch().getBranch());
        } else {
            rmb2122.getRecgrp().getAds().setOwnextkey(SettleContext.getUserSession().getBch().getBranch());
        }


        if (MdaUtils.compareTo(rmb2122.getRmbflg(), "1") == 0) {
            String bchname = NULLSTR;
            if (!MdaUtils.isEmpty(SettleContext.getUserSession().getWrkbch())) {
                lhanam = SettleContext.getUserSession().getWrkbch().getLhanum();
                bchname = SettleContext.getUserSession().getWrkbch().getLhanam();
            }
            String cfatyp = "";
            String cty = "";
            Date vouchefficientdate = NULLDATE;
            String expflg = "";
            String exptyp = "";
            //#VouchEndDate	担保到期日期
            Date expdat = NULLDATE;
            String gartyp = "";
            String orcref = "";
            Date reldat = NULLDATE;
            BigDecimal ameamt = new BigDecimal(0);
            Date newexpdat = NULLDATE;
            String grtjuscod = "";
            String giduil = "";
            String fenlishi = "";
            String purpos = "";
            String hndtyp = "";
            Date liadat = NULLDATE;
            Date newliadat = NULLDATE;
            String newliaflg = NULLSTR;
            String newliatyp = NULLSTR;
            String newExpTyp = NULLSTR;
            String newExpflg = NULLSTR;
            BigDecimal addameamt = new BigDecimal(0);
//            BigDecimal  amt = new BigDecimal(0);


            String[] giArr = new String[]{"GITOPN", "GITPOP", "GITAME", "GITPAM", "GITPDL", "GITDLA", "GETOPN", "GETAME"};
            String[] gcArr = new String[]{"GITPSE", "GITSET", "GETSET"};
            String[] niArr = new String[]{"NITPOP", "NITOPN", "NITPAM", "NITAME", "NITPDL", "NITDLA", "NETOPN", "NETAME"};
            String[] ncArr = new String[]{"NITPSE", "NITSET", "NETSET"};
            if (Arrays.asList(giArr).contains(SettleContext.getTransName().toUpperCase())) {
                GidBizInfoVo gidBizInfoVo = (GidBizInfoVo) bizInfo;
                if (MdaUtils.compareTo(SettleContext.getTransName().toUpperCase(), "GITAME") == 0 || MdaUtils.compareTo(SettleContext.getTransName().toUpperCase(), "GITPAM") == 0 || MdaUtils.compareTo(SettleContext.getTransName().toUpperCase(), "GITDLA") == 0 || MdaUtils.compareTo(SettleContext.getTransName().toUpperCase(), "GITPDL") == 0 || MdaUtils.compareTo(SettleContext.getTransName().toUpperCase(), "GETAME") == 0) {
                    GidBizAmdInfoVo gidBizAmdInfoVo = (GidBizAmdInfoVo) BopContext.getBizAmdInfo();
                    if (!MdaUtils.isEmpty(gidBizAmdInfoVo)) {
                        if (!MdaUtils.isEmpty(gidBizAmdInfoVo.getNewexpdat())) {
                            newexpdat = gidBizAmdInfoVo.getNewexpdat();
                        }
                        if (!MdaUtils.isEmpty(gidBizAmdInfoVo.getAmeamt())) {
                            ameamt = gidBizAmdInfoVo.getAmeamt();
                        }
                        if (!MdaUtils.isEmpty(gidBizAmdInfoVo.getAddameamt())) {
                            addameamt = gidBizAmdInfoVo.getAddameamt();
                        }
                        newliadat = gidBizAmdInfoVo.getNewliadat();
                        newliatyp = gidBizAmdInfoVo.getNewliatyp();
                        newExpflg = gidBizAmdInfoVo.getNewexpflg();
                        newExpTyp = gidBizAmdInfoVo.getNewexptyp();
                        newliaflg = gidBizAmdInfoVo.getNewliaflg();
                    }
                }

                cfatyp = gidBizInfoVo.getCfaguatyp();
                cty = gidBizInfoVo.getStacty();
                vouchefficientdate = gidBizInfoVo.getOrddat();
                expflg = gidBizInfoVo.getExpflg();
                exptyp = gidBizInfoVo.getExptyp();
                //#VouchEndDate	担保到期日期
                expdat = gidBizInfoVo.getExpdat();
                gartyp = gidBizInfoVo.getGartyp();
                orcref = gidBizInfoVo.getOrcref();
                purpos = gidBizInfoVo.getPurpos();
                hndtyp = gidBizInfoVo.getHndtyp();
                liadat = gidBizInfoVo.getLiadat();

//                ameamt = gidBizInfoVo.getAmeamt();

            } else if (Arrays.asList(gcArr).contains(SettleContext.getTransName().toUpperCase())) {
                GcdBizInfoVo gcdBizInfoVo = (GcdBizInfoVo) bizInfo;
                cfatyp = gcdBizInfoVo.getCfaguatyp();
                cty = gcdBizInfoVo.getStacty();
                vouchefficientdate = gcdBizInfoVo.getOrddat();
                expflg = gcdBizInfoVo.getExpflg();
                exptyp = gcdBizInfoVo.getExptyp();
                //#VouchEndDate	担保到期日期
                expdat = gcdBizInfoVo.getExpdat();
                gartyp = gcdBizInfoVo.getGartyp();
                orcref = gcdBizInfoVo.getOrcref();
            } else if (Arrays.asList(niArr).contains(SettleContext.getTransName().toUpperCase())) {
                NidBizInfoVo nidBizInfoVo = (NidBizInfoVo) bizInfo;
                if (MdaUtils.compareTo(SettleContext.getTransName().toUpperCase(), "NITAME") == 0 || MdaUtils.compareTo(SettleContext.getTransName().toUpperCase(), "NITPAM") == 0 || MdaUtils.compareTo(SettleContext.getTransName().toUpperCase(), "NITDLA") == 0 || MdaUtils.compareTo(SettleContext.getTransName().toUpperCase(), "NITPDL") == 0 || MdaUtils.compareTo(SettleContext.getTransName().toUpperCase(), "NETAME") == 0) {
                    NidBizAmdInfoVo nidBizAmdInfoVo = (NidBizAmdInfoVo) BopContext.getBizAmdInfo();
                    if (!MdaUtils.isEmpty(nidBizAmdInfoVo)) {
                        if (!MdaUtils.isEmpty(nidBizAmdInfoVo.getNewexpdat())) {
                            newexpdat = nidBizAmdInfoVo.getNewexpdat();
                        }
                        ameamt = nidBizAmdInfoVo.getAmeamt();
                        newliadat = nidBizAmdInfoVo.getNewliadat();
                        newliaflg = nidBizAmdInfoVo.getNewliaflg();
                        newExpflg = nidBizAmdInfoVo.getNewexpflg();
                    }
                }
                cfatyp = nidBizInfoVo.getCfaguatyp();
                cty = nidBizInfoVo.getStacty();
                vouchefficientdate = nidBizInfoVo.getOrddat();
                expflg = nidBizInfoVo.getExpflg();
                exptyp = nidBizInfoVo.getExptyp();
                //#VouchEndDate	担保到期日期
                expdat = nidBizInfoVo.getExpdat();
                gartyp = nidBizInfoVo.getGartyp();
                orcref = nidBizInfoVo.getOrcref();
                fenlishi = nidBizInfoVo.getFenlishi();
                grtjuscod = nidBizInfoVo.getGrtjuscod();
                giduil = nidBizInfoVo.getGiduil() != null ? nidBizInfoVo.getGiduil() : "";
                hndtyp = nidBizInfoVo.getHndtyp();
                liadat = nidBizInfoVo.getLiadat();
//                ameamt = nidBizInfoVo.getAmeamt();

            } else if (Arrays.asList(ncArr).contains(SettleContext.getTransName().toUpperCase())) {
                NcdBizInfoVo ncdBizInfoVo = (NcdBizInfoVo) bizInfo;
                cfatyp = ncdBizInfoVo.getCfaguatyp();
                cty = ncdBizInfoVo.getStacty();
                vouchefficientdate = ncdBizInfoVo.getOrddat();
                expflg = ncdBizInfoVo.getExpflg();
                exptyp = ncdBizInfoVo.getExptyp();
                //#VouchEndDate	担保到期日期
                expdat = ncdBizInfoVo.getExpdat();
                gartyp = ncdBizInfoVo.getGartyp();
                orcref = ncdBizInfoVo.getOrcref();
            }
            ameamt = ameamt != null ? ameamt : new BigDecimal("0");

            PtsptaVo apl = ptsptaToolComponent.getPtsFromListByRol(ptsList, "APL");
            if (MdaUtils.isEmpty(apl)) {
                apl = new PtsptaVo();
                apl.reset();
            }
            PtsptaVo ben = ptsptaToolComponent.getPtsFromListByRol(ptsList, "BEN");
            if (MdaUtils.isEmpty(ben)) {
                ben = new PtsptaVo();
                ben.reset();
            }

            //二月常规-通知修改交易要出跨境人民币保送，试算只更新金额和效期 ，为不影响现有逻辑，因此增加交易判断
            if (MdaUtils.compareTo(SettleContext.getTransName().toUpperCase(), "GETAME") == 0 || MdaUtils.compareTo(SettleContext.getTransName().toUpperCase(), "NETAME") == 0) {
                Map<String, Cbb> cbsMap = BopContext.getCbsMap();
                //试算金额
                Cbb max = cbsMap.get("MAX");

                //附加金额
                Cbb max2 = cbsMap.get("MAX2");
                BigDecimal addmat;

                //VouchAmt	担保金额
                BigDecimal vouchamt = max.getAmt();
                if (!rmb2122.getRecgrp().getAds().isModified("vouchamt")) {
                    if (max2 != null) {
                        addmat = max2.getAmt();
                        rmb2122.getRecgrp().getAds().setVouchamt(vouchamt.add(addmat));
                    } else {
                        rmb2122.getRecgrp().getAds().setVouchamt(vouchamt);
                    }
                }

                //试算效期
                if (!rmb2122.getRecgrp().getAds().isModified("vouchenddate")) {
                    if (Strings.mid(SettleContext.getTransName().toUpperCase(), 1, 2).equals("GE")) {
                        if (!MdaUtils.isEmpty(newExpTyp)) {
                            if (!MdaUtils.isEmpty(newexpdat)) {
                                rmb2122.getRecgrp().getAds().setVouchenddate(newexpdat);
                            } else {
                                rmb2122.getRecgrp().getAds().setVouchenddate(Dates.add(rmb2122.getRecgrp().getAds().getVouchefficientdate(), 7305));
                            }
                        } else {
                            if (MdaUtils.isEmpty(expdat)) {
                                rmb2122.getRecgrp().getAds().setVouchenddate(Dates.add(rmb2122.getRecgrp().getAds().getVouchefficientdate(), 7305));
                            } else {
                                rmb2122.getRecgrp().getAds().setVouchenddate(expdat);
                            }
                        }
                    }
                    if (Strings.mid(SettleContext.getTransName().toUpperCase(), 1, 2).equals("NE")) {
                        if (MdaUtils.compareTo(newExpflg, "X") == 0) {
                            rmb2122.getRecgrp().getAds().setVouchenddate(Dates.add(rmb2122.getRecgrp().getAds().getVouchefficientdate(), 7305));
                        } else {
                            if (!MdaUtils.isEmpty(newexpdat)) {
                                rmb2122.getRecgrp().getAds().setVouchenddate(newexpdat);
                            } else {
                                if (!MdaUtils.isEmpty(expdat)) {
                                    rmb2122.getRecgrp().getAds().setVouchenddate(expdat);
                                } else {
                                    rmb2122.getRecgrp().getAds().setVouchenddate(Dates.add(rmb2122.getRecgrp().getAds().getVouchefficientdate(), 7305));
                                }
                            }
                        }
                    }
                }
                //试算完直接返回，其他信息不进行更新
                return;
            }

            if (MdaUtils.compareTo(Strings.mid(SettleContext.getTransName().toUpperCase(), 1, 2), "GI") == 0 || MdaUtils.compareTo(Strings.mid(SettleContext.getTransName().toUpperCase(), 1, 2), "NI") == 0) {
                //#--------担保申请人start-------
                //GuaAppName	担保申请人名称

                String debtorOrgCode = NULLSTR;
                String debtorName = NULLSTR;

                if (apl != null) {
                    String name1 = NULLSTR;
                    String guaAppCode = NULLSTR;
                    if (!MdaUtils.isEmpty(apl.getPtyinr())) {
                        Pty pty = ptyCacheService.getPtyById(apl.getPtyinr());
                        guaAppCode = pty.getJuscod();
                        debtorOrgCode = pty.getJuscod();
                        if (!MdaUtils.isEmpty(apl.getPts())) {
                            if ("CN".equalsIgnoreCase(giduil)) {
                                name1 = apl.getPts().getCnnam();
                                debtorName = apl.getPts().getCnnam();
                            }
                            if ("EN".equalsIgnoreCase(giduil)) {
                                name1 = apl.getPts().getEnnam();
                                debtorName = apl.getPts().getEnnam();
                            }
                        }
                        if (MdaUtils.isEmpty(name1)) {
                            name1 = pty.getNam1();
                            debtorName = pty.getNam1();
                        }
                    }
                    if (MdaUtils.isEmpty(rmb2122.getRecgrp().getAds().getGuaappname())) {
                        rmb2122.getRecgrp().getAds().setGuaappname(name1);
                    }


                    //GuaAppCode	担保申请人机构代码
                    rmb2122.getRecgrp().getAds().setGuaappcode(guaAppCode);
                }
                //#--------担保申请人end-------
                //#--------担保人start-------
                //VoucherSWIFTBIC担保人机构代码
                rmb2122.getRecgrp().getAds().setVoucherswiftbic(lhanam);
                //VoucherName担保人名称
                rmb2122.getRecgrp().getAds().setVouchername(bchname);
                //VoucherCountryCode担保人国别
                if (MdaUtils.isEmpty(rmb2122.getRecgrp().getAds().getVoucheecountrycode())) {
                    rmb2122.getRecgrp().getAds().setVouchercountrycode("CHN");
                }
                //VoucherType担保人类型
                rmb2122.getRecgrp().getAds().setVouchertype("IB03");
                //#----------担保人end----------
                //#----------担保权人start-------
                //取受益人信息

                String voucheeName = NULLSTR;
                if (MdaUtils.compareTo(Strings.mid(SettleContext.getTransName().toUpperCase(), 1, 2), "NI") == 0) {
                    if (ben != null) {
                        if (!MdaUtils.isEmpty(ben.getPts())) {
                            if ("CN".equalsIgnoreCase(giduil)) {
                                voucheeName = ben.getPts().getCnnam();
                            }
                            if ("EN".equalsIgnoreCase(giduil)) {
                                voucheeName = ben.getPts().getEnnam();
                            }

                        }
                    }
                }

                //当开立类型为反担保时，担保权人名称的取值逻辑
                if (MdaUtils.compareTo(Strings.mid(SettleContext.getTransName().toUpperCase(), 1, 2), "GI") == 0) {
                    if (MdaUtils.compareTo(purpos, "ISCO") == 0 || MdaUtils.compareTo(purpos, "ICCO") == 0) {
                        PtsptaVo bec = ptsptaToolComponent.getPtsFromListByRol(ptsList, "BEC");
                        if (MdaUtils.isEmpty(bec)) {
                            bec = new PtsptaVo();
                            bec.reset();
                        }
                        if (bec != null) {
                            if (!MdaUtils.isEmpty(bec.getPts())) {
                                voucheeName = bec.getPts().getEnnam();
                            }
                        }
                    } else {
                        if (ben != null) {
                            if (!MdaUtils.isEmpty(ben.getPts())) {
                                voucheeName = ben.getPts().getEnnam();
                            }
                        }
                    }
                }

                //转开时，担保权人的取值逻辑
                if (MdaUtils.compareTo(hndtyp, "OT") == 0) {
                    String voucheeidcode = NULLSTR;
                    String voucheejuscod = NULLSTR;
                    if (ben != null) {
                        if (!MdaUtils.isEmpty(ben.getPtyinr())) {
                            Pty pty = ptyCacheService.getPtyById(ben.getPtyinr());
                            voucheeidcode = pty.getIdcode();
                            voucheejuscod = pty.getJuscod();
                        }

                    }
                    if (!MdaUtils.isEmpty(voucheeidcode)) {
                        rmb2122.getRecgrp().getAds().setVoucheeorgcode(voucheeidcode);
                    } else {
                        rmb2122.getRecgrp().getAds().setVoucheeorgcode(voucheejuscod);
                    }
                }

                //VoucheeCountryCode担保权人国别
                //VoucheeName担保权人名称
                if (!MdaUtils.isEmpty(cty)) {
                    if (Strings.len(cty) == 2) {
                        if (StringUtils.isEmpty(rmb2122.getRecgrp().getAds().getVoucheecountrycode())) {
                            rmb2122.getRecgrp().getAds().setVoucheecountrycode(Codetables.getCodetableLabelOfTd(cty, "CTYCOD", Systems.getLang()));
                        }
                    } else {
                        if (StringUtils.isEmpty(rmb2122.getRecgrp().getAds().getVoucheecountrycode())) {
                            rmb2122.getRecgrp().getAds().setVoucheecountrycode(cty);
                        }
                    }
                }
                rmb2122.getRecgrp().getAds().setVoucheename(voucheeName);
                //#----------担保权人end-------
                //#----------债务人start-------
                //DebtorOrgCode债务人机构代码
                //DebtorName债务人名称
                //DebtorCountryCode债务人国别


                if (MdaUtils.compareTo(Strings.mid(cfatyp, 1, 2), "12") == 0) {

                    //分离式保函勾选时，非融资时债务人取被保证人，融资时债务人不试算
                    if (MdaUtils.compareTo(fenlishi, "2") == 0) {
                        PtsptaVo bdb = ptsptaToolComponent.getPtsFromListByRol(ptsList, "BDB");
                        if (MdaUtils.isEmpty(bdb)) {
                            bdb = new PtsptaVo();
                            bdb.reset();
                        }
                        if (!MdaUtils.isEmpty(bdb.getPts())) {
                            debtorOrgCode = grtjuscod;
                            if ("CN".equalsIgnoreCase(giduil)) {
                                debtorName = bdb.getPts().getCnnam();
                            }
                            if ("EN".equalsIgnoreCase(giduil)) {
                                debtorName = bdb.getPts().getEnnam();
                            }

                        }
                    }
                    //--非融资---
                    rmb2122.getRecgrp().getAds().setDebtororgcode(debtorOrgCode);

                    rmb2122.getRecgrp().getAds().setDebtorname(debtorName);


                    //转开时，债务人国别的取值逻辑
                    if (MdaUtils.compareTo(SettleContext.getTransName().toUpperCase(), "GITOPN") == 0 || MdaUtils.compareTo(SettleContext.getTransName().toUpperCase(), "NITOPN") == 0 || MdaUtils.compareTo(SettleContext.getTransName().toUpperCase(), "GITPOP") == 0 || MdaUtils.compareTo(SettleContext.getTransName().toUpperCase(), "NITPOP") == 0) {
                        if (MdaUtils.compareTo(hndtyp, "OT") == 0) {
                            rmb2122.getRecgrp().getAds().setDebtorcountrycode("");
                        } else {
                            if (!MdaUtils.isEmpty(debtorName)) {
                                rmb2122.getRecgrp().getAds().setDebtorcountrycode("CHN");
                            }
                        }
                    }

                }
//                else
//                {
//                    if (!rmb2122.getRecgrp().getAds().isModified("debtororgcode")) {
//                        rmb2122.getRecgrp().getAds().setDebtororgcode("");
//                    }
//                    if (!rmb2122.getRecgrp().getAds().isModified("debtorname")) {
//                        rmb2122.getRecgrp().getAds().setDebtorname("");
//                    }
//                    if (!rmb2122.getRecgrp().getAds().isModified("debtorcountrycode")) {
//                        rmb2122.getRecgrp().getAds().setDebtorcountrycode("");
//                    }
//
//                }
                //#----------债务人end-------
                //#----------反担保人start------
                //ReverseOrgCode反担保人机构代码
                //ReverseName反担保人名称
                //ReverseCountryCode反担保人国别
                //#----------反担保人end--------NN
            } else {
                PtsptaVo iss = ptsptaToolComponent.getPtsFromListByRol(ptsList, "ISS");
                String voucherswiftbic = NULLSTR;
                String voucherName = NULLSTR;
                if (iss != null) {
                    if (!MdaUtils.isEmpty(iss.getAdr())) {
                        voucherswiftbic = iss.getAdr().getBic();
                    }
                    if (!MdaUtils.isEmpty(iss.getPts())) {
                        if ("CN".equalsIgnoreCase(giduil)) {
                            voucherName = iss.getPts().getCnnam();
                        }
                        if ("EN".equalsIgnoreCase(giduil)) {
                            voucherName = iss.getPts().getEnnam();
                        }
                    }
                }
                String vouchercountrycode = Codetables.getCodetableLabelOfTd(Strings.mid(voucherswiftbic, 5, 2), "CTYCOD", Systems.getLang());
                //VoucherName担保人名称
                rmb2122.getRecgrp().getAds().setVouchername(voucherName);
                //VoucherCountryCode担保人国别
                if (!MdaUtils.isEmpty(vouchercountrycode)) {
                    rmb2122.getRecgrp().getAds().setVouchercountrycode(vouchercountrycode);
                    // Modules.postDefaultRule(this,"recgrp.ads.vouchertype");
                }

                rmb2122.getRecgrp().getAds().setVoucherswiftbic(voucherswiftbic);
                //#----------担保人end----------
                if (MdaUtils.compareTo(Strings.mid(cfatyp, 1, 2), "11") == 0) {
                    //#----------担保权人start-------
                    //#VoucheeName	担保权人机构代码
                    rmb2122.getRecgrp().getAds().setVoucheeorgcode(lhanam);
                    //#VoucheeName	担保权人名称
                    rmb2122.getRecgrp().getAds().setVoucheename(bchname);
                    //VoucheeCountryCode担保权人国别
                    rmb2122.getRecgrp().getAds().setVoucheecountrycode("CHN");
                    //Modules.postDefaultRule(this,"recgrp.ads.voucheetype");
                    rmb2122.getRecgrp().getAds().setVoucheetype("IB03");
                    //#----------担保权人end-------
                    //#----------债务人start-------
                    //DebtorOrgCode债务人机构代码
                    //DebtorName债务人名称
                    //DebtorCountryCode债务人国别

                    String debtorOrgidCode = NULLSTR;
                    String debtorOrgjuscod = NULLSTR;
                    String debtorName = NULLSTR;
                    String debtorCountryCode = NULLSTR;
                    if (ben != null) {
                        if (!MdaUtils.isEmpty(ben.getPtyinr())) {
                            Pty pty = ptyCacheService.getPtyById(ben.getPtyinr());
                            debtorOrgidCode = pty.getIdcode();
                            debtorOrgjuscod = pty.getJuscod();
                            if (!MdaUtils.isEmpty(ben.getPts())) {
                                if ("CN".equalsIgnoreCase(giduil)) {
                                    debtorName = ben.getPts().getCnnam();
                                }
                                if ("EN".equalsIgnoreCase(giduil)) {
                                    debtorName = ben.getPts().getEnnam();
                                }
                            }
                            debtorCountryCode = ben.getAdr().getLoccty();
                        }
                    }

                    if (!MdaUtils.isEmpty(debtorOrgidCode)) {
                        rmb2122.getRecgrp().getAds().setDebtororgcode(debtorOrgidCode);
                    } else {
                        rmb2122.getRecgrp().getAds().setDebtororgcode(debtorOrgjuscod);
                    }
                    rmb2122.getRecgrp().getAds().setDebtorname(debtorName);


                    if (!MdaUtils.isEmpty(debtorCountryCode)) {
                        if (Strings.len(debtorCountryCode) == 2) {
                            if (MdaUtils.isEmpty(rmb2122.getRecgrp().getAds().getDebtorcountrycode())) {
                                rmb2122.getRecgrp().getAds().setDebtorcountrycode(Codetables.getCodetableLabelOfTd(debtorCountryCode, "CTYCOD", Systems.getLang()));
                            }
                        } else {
                            rmb2122.getRecgrp().getAds().setDebtorcountrycode(debtorCountryCode);
                        }
                    } else {
                        rmb2122.getRecgrp().getAds().setDebtorcountrycode("CHN");
                    }
                    //#----------债务人end-------
                } else {
                    //#----------担保权人start-------
                    //VoucheeCountryCode担保权人国别

                    String voucheecountrycode = NULLSTR;
                    if (ben != null) {
                        if (!MdaUtils.isEmpty(ben.getAdr())) {
                            voucheecountrycode = ben.getAdr().getLoccty();
                        }
                    }

                    if (!MdaUtils.isEmpty(voucheecountrycode)) {
                        if (Strings.len(voucheecountrycode) == 2) {
                            if (MdaUtils.isEmpty(rmb2122.getRecgrp().getAds().getVoucheecountrycode())) {
                                rmb2122.getRecgrp().getAds().setVoucheecountrycode(Codetables.getCodetableLabelOfTd(voucheecountrycode, "CTYCOD", Systems.getLang()));
                            }
                        } else {
                            if (MdaUtils.isEmpty(rmb2122.getRecgrp().getAds().getVoucheecountrycode())) {
                                rmb2122.getRecgrp().getAds().setVoucheecountrycode(voucheecountrycode);
                            }
                        }
                    } else {
                        if (MdaUtils.isEmpty(rmb2122.getRecgrp().getAds().getVoucheecountrycode())) {
                            rmb2122.getRecgrp().getAds().setVoucheecountrycode("CHN");
                        }
                    }
                    // Modules.postDefaultRule(this,"recgrp.ads.voucheetype");
                    //#VoucheeName	担保权人机构代码

                    String voucheeidcode = NULLSTR;
                    String voucheejuscod = NULLSTR;
                    String voucheeName = NULLSTR;
                    if (ben != null) {
                        if (!MdaUtils.isEmpty(ben.getPtyinr())) {
                            Pty pty = ptyCacheService.getPtyById(ben.getPtyinr());
                            voucheeidcode = pty.getIdcode();
                            voucheejuscod = pty.getJuscod();
//                            voucheeName = pty.getNam1();

                            if (!MdaUtils.isEmpty(ben.getPts())) {
                                if ("CN".equalsIgnoreCase(giduil)) {
                                    voucheeName = ben.getPts().getCnnam();
                                }
                                if ("EN".equalsIgnoreCase(giduil)) {
                                    voucheeName = ben.getPts().getEnnam();
                                }
                            }
                        }

                    }
                    if (!MdaUtils.isEmpty(voucheeidcode)) {
                        rmb2122.getRecgrp().getAds().setVoucheeorgcode(voucheeidcode);
                    } else {
                        rmb2122.getRecgrp().getAds().setVoucheeorgcode(voucheejuscod);
                    }
                    //#VoucheeName	担保权人名称

                    rmb2122.getRecgrp().getAds().setVoucheename(voucheeName);
                    //#----------担保权人end-------
                    //#----------债务人start-------
                    //DebtorOrgCode债务人机构代码
                    //DebtorName债务人名称
                    //DebtorCountryCode债务人国别
                    //String cty =gidBizInfoVo.getStacty();
                    String debtorName = NULLSTR;
                    if (apl != null) {
                        if (!MdaUtils.isEmpty(apl.getPts())) {
//                            debtorName = apl.getPts().getNam();
                            if ("CN".equalsIgnoreCase(giduil)) {
                                debtorName = apl.getPts().getCnnam();
                            }
                            if ("EN".equalsIgnoreCase(giduil)) {
                                debtorName = apl.getPts().getEnnam();
                            }
                        }
                    }

                    //DebtorCountryCode债务人国别
                    if (!MdaUtils.isEmpty(cty)) {
                        if (Strings.len(cty) == 2) {
                            if (MdaUtils.isEmpty(rmb2122.getRecgrp().getAds().getDebtorcountrycode())) {
                                rmb2122.getRecgrp().getAds().setDebtorcountrycode(Codetables.getCodetableLabelOfTd(cty, "CTYCOD", Systems.getLang()));
                            }
                        } else {
                            rmb2122.getRecgrp().getAds().setDebtorcountrycode(cty);
                        }
                    }
                    //DebtorName债务人名称
                    rmb2122.getRecgrp().getAds().setDebtorname(debtorName);
                    //#----------债务人end-------
                }
            }
            //#PropertyCountryCode担保物权登记国别#
            //VouchMode反担保方式#
            //VouchNo担保登记编号
            /**
             Post( "exeDefault(\\"RMB2122\\RECGRP\\ADS\\VouchNo\\" ) ", 1 )
             $err = ErrorCode
             **/
            Map<String, Cbb> cbsMap = BopContext.getCbsMap();
            Cbb max = cbsMap.get("MAX");
            String cur = max.getCur();

            //附加金额
            Cbb max2 = cbsMap.get("MAX2");
            BigDecimal addmat = new BigDecimal(0);


            //CurrencyCode	担保币种
            rmb2122.getRecgrp().getAds().setCurrencycode(cur);
            //VouchAmt	担保金额
            BigDecimal vouchamt = max.getAmt();
//            Cbb oldmax = cbsMap.get("OLDMAX");
//            BigDecimal oldvouchamt = new BigDecimal(0);
//            if (!MdaUtils.isEmpty(oldmax)){
//                oldvouchamt = oldmax.getAmt();
//            }
            if (MdaUtils.compareTo(SettleContext.getTransName().toUpperCase(), "GITAME") == 0 || MdaUtils.compareTo(SettleContext.getTransName().toUpperCase(), "NITAME") == 0 || MdaUtils.compareTo(SettleContext.getTransName().toUpperCase(), "GITPAM") == 0 || MdaUtils.compareTo(SettleContext.getTransName().toUpperCase(), "NITPAM") == 0) {
         /*       if ( ameamt !=null && ameamt.compareTo(BigDecimal.ZERO) > 0){
                    if (max2 !=null){
                        addmat = max2.getAmt();
                        if (addameamt != null){
                            if( ! rmb2122.getRecgrp().getAds().isModified("vouchamt")) {
                                rmb2122.getRecgrp().getAds().setVouchamt(oldvouchamt.add(addmat).add(addameamt).add(ameamt));
                            }
                        }else {
                            if( ! rmb2122.getRecgrp().getAds().isModified("vouchamt")) {
                                rmb2122.getRecgrp().getAds().setVouchamt(oldvouchamt.add(addmat).add(ameamt));
                            }
                        }
                    }else {
                        if( ! rmb2122.getRecgrp().getAds().isModified("vouchamt")) {
                            rmb2122.getRecgrp().getAds().setVouchamt(oldvouchamt.add(ameamt));
                        }
                    }

                }else if (ameamt == null || ameamt.compareTo(BigDecimal.ZERO) == 0){
                    if (max2 !=null) {
                        addmat = max2.getAmt();
                        if (addameamt != null){
                            if( ! rmb2122.getRecgrp().getAds().isModified("vouchamt")) {
                                rmb2122.getRecgrp().getAds().setVouchamt(oldvouchamt.add(addmat).add(addameamt));
                            }
                        }else {
                            if( ! rmb2122.getRecgrp().getAds().isModified("vouchamt")) {
                                rmb2122.getRecgrp().getAds().setVouchamt(oldvouchamt.add(addmat));
                            }
                        }
                    }
                }*/

                List<LiaallgVo> liaallg = BopContext.getLiaallg();
                LiaallgVo liaallgVo = new LiaallgVo();
                liaallgVo.reset();
                if (!MdaUtils.isEmpty(liaallg)) {
                    liaallgVo = liaallg.get(0);
                }
                rmb2122.getRecgrp().getAds().setVouchamt(liaallgVo.getOldamt().add(liaallgVo.getAmt()));

            } else if (MdaUtils.compareTo(SettleContext.getTransName().toUpperCase(), "GITDLA") == 0 || MdaUtils.compareTo(SettleContext.getTransName().toUpperCase(), "NITDLA") == 0 || MdaUtils.compareTo(SettleContext.getTransName().toUpperCase(), "GITPDL") == 0 || MdaUtils.compareTo(SettleContext.getTransName().toUpperCase(), "NITPDL") == 0) {
                /*if ( ameamt !=null && ameamt.compareTo(BigDecimal.ZERO) < 0){
                    if (max2 !=null){
                        addmat = max2.getAmt();
                        if (addameamt != null){
                            if( ! rmb2122.getRecgrp().getAds().isModified("vouchamt")) {
                                rmb2122.getRecgrp().getAds().setVouchamt(oldvouchamt.add(addmat).add(addameamt).add(ameamt));
                            }
                        }else {
                            if( ! rmb2122.getRecgrp().getAds().isModified("vouchamt")) {
                                rmb2122.getRecgrp().getAds().setVouchamt(oldvouchamt.add(addmat).add(ameamt));
                            }
                        }
                    }else {
                        if( ! rmb2122.getRecgrp().getAds().isModified("vouchamt")) {
                            rmb2122.getRecgrp().getAds().setVouchamt(oldvouchamt.add(ameamt));
                        }
                    }
                }else if (ameamt == null || ameamt.compareTo(BigDecimal.ZERO) == 0){
                    if (max2 !=null){
                        addmat = max2.getAmt();
                        if (addameamt != null){
                            if( ! rmb2122.getRecgrp().getAds().isModified("vouchamt")) {
                                rmb2122.getRecgrp().getAds().setVouchamt(oldvouchamt.add(addmat).add(addameamt));
                            }
                        }else {
                            if( ! rmb2122.getRecgrp().getAds().isModified("vouchamt")) {
                                rmb2122.getRecgrp().getAds().setVouchamt(oldvouchamt.add(addmat));
                            }
                        }
                    }
                }*/

                List<LiaallgVo> liaallg = BopContext.getLiaallg();
                LiaallgVo liaallgVo = new LiaallgVo();
                liaallgVo.reset();
                if (!MdaUtils.isEmpty(liaallg)) {
                    liaallgVo = liaallg.get(0);
                }
                rmb2122.getRecgrp().getAds().setVouchamt(liaallgVo.getOldamt().add(liaallgVo.getAmt()));
            } else {
                if (max2 != null) {
                    addmat = max2.getAmt();
                    rmb2122.getRecgrp().getAds().setVouchamt(vouchamt.add(addmat));
                } else {
                    rmb2122.getRecgrp().getAds().setVouchamt(vouchamt);
                }
            }
            //VouchEfficientDate担保生效日期
            //Date vouchefficientdate = gidBizInfoVo.getOrddat();
            rmb2122.getRecgrp().getAds().setVouchefficientdate(vouchefficientdate);

            if (MdaUtils.compareTo(SettleContext.getTransName().toUpperCase(), "GITAME") == 0 || MdaUtils.compareTo(SettleContext.getTransName().toUpperCase(), "NITAME") == 0 || MdaUtils.compareTo(SettleContext.getTransName().toUpperCase(), "GITPAM") == 0 || MdaUtils.compareTo(SettleContext.getTransName().toUpperCase(), "NITPAM") == 0) {
                if (Strings.mid(SettleContext.getTransName().toUpperCase(), 1, 2).equals("GI")) {
                    if (!MdaUtils.isEmpty(newExpTyp)) {
                        if (!MdaUtils.isEmpty(newexpdat)) {
                            rmb2122.getRecgrp().getAds().setVouchenddate(newexpdat);
                        } else {
                            rmb2122.getRecgrp().getAds().setVouchenddate(Dates.add(rmb2122.getRecgrp().getAds().getVouchefficientdate(), 7305));
                        }
                    } else {
                        if (MdaUtils.isEmpty(expdat)) {
                            rmb2122.getRecgrp().getAds().setVouchenddate(Dates.add(rmb2122.getRecgrp().getAds().getVouchefficientdate(), 7305));
                        } else {
                            rmb2122.getRecgrp().getAds().setVouchenddate(expdat);
                        }
                    }
                }
                if (Strings.mid(SettleContext.getTransName().toUpperCase(), 1, 2).equals("NI")) {
                    if (MdaUtils.compareTo(newExpflg, "X") == 0) {
                        rmb2122.getRecgrp().getAds().setVouchenddate(Dates.add(rmb2122.getRecgrp().getAds().getVouchefficientdate(), 7305));
                    } else {
                        if (!MdaUtils.isEmpty(newexpdat)) {
                            rmb2122.getRecgrp().getAds().setVouchenddate(newexpdat);
                        } else {
                            if (!MdaUtils.isEmpty(expdat)) {
                                rmb2122.getRecgrp().getAds().setVouchenddate(expdat);
                            } else {
                                rmb2122.getRecgrp().getAds().setVouchenddate(Dates.add(rmb2122.getRecgrp().getAds().getVouchefficientdate(), 7305));
                            }
                        }
                    }
                }
                if (MdaUtils.compareTo(purpos, "ISCO") == 0 || MdaUtils.compareTo(purpos, "ICCO") == 0 || MdaUtils.compareTo(hndtyp, "OC") == 0) {
                    if (Strings.mid(SettleContext.getTransName().toUpperCase(), 1, 2).equals("GI")) {
                        if (!MdaUtils.isEmpty(newliatyp)) {
                            if (!MdaUtils.isEmpty(newliadat)) {
                                rmb2122.getRecgrp().getAds().setVouchenddate(newliadat);
                            } else {
                                rmb2122.getRecgrp().getAds().setVouchenddate(Dates.add(rmb2122.getRecgrp().getAds().getVouchefficientdate(), 7305));
                            }
                        } else {
                            if (MdaUtils.isEmpty(liadat)) {
                                rmb2122.getRecgrp().getAds().setVouchenddate(Dates.add(rmb2122.getRecgrp().getAds().getVouchefficientdate(), 7305));
                            } else {
                                rmb2122.getRecgrp().getAds().setVouchenddate(liadat);
                            }
                        }
                    }
                    if (Strings.mid(SettleContext.getTransName().toUpperCase(), 1, 2).equals("NI")) {
                        if (MdaUtils.compareTo(newliaflg, "X") == 0) {
                            rmb2122.getRecgrp().getAds().setVouchenddate(Dates.add(rmb2122.getRecgrp().getAds().getVouchefficientdate(), 7305));
                        } else {
                            if (!MdaUtils.isEmpty(newliadat)) {
                                rmb2122.getRecgrp().getAds().setVouchenddate(newliadat);
                            } else {
                                if (MdaUtils.isEmpty(liadat)) {
                                    rmb2122.getRecgrp().getAds().setVouchenddate(Dates.add(rmb2122.getRecgrp().getAds().getVouchefficientdate(), 7305));
                                } else {
                                    rmb2122.getRecgrp().getAds().setVouchenddate(liadat);
                                }
                            }
                        }
                    }
                }

            } else if (MdaUtils.compareTo(SettleContext.getTransName().toUpperCase(), "GITDLA") == 0 || MdaUtils.compareTo(SettleContext.getTransName().toUpperCase(), "NITDLA") == 0 || MdaUtils.compareTo(SettleContext.getTransName().toUpperCase(), "GITPDL") == 0 || MdaUtils.compareTo(SettleContext.getTransName().toUpperCase(), "NITPDL") == 0) {
                if (Strings.mid(SettleContext.getTransName().toUpperCase(), 1, 2).equals("GI")) {
                    if (!MdaUtils.isEmpty(newExpTyp)) {
                        if (!MdaUtils.isEmpty(newexpdat)) {
                            rmb2122.getRecgrp().getAds().setVouchenddate(newexpdat);
                        } else {
                            rmb2122.getRecgrp().getAds().setVouchenddate(Dates.add(rmb2122.getRecgrp().getAds().getVouchefficientdate(), 7305));
                        }
                    } else {
                        if (MdaUtils.isEmpty(expdat)) {
                            rmb2122.getRecgrp().getAds().setVouchenddate(Dates.add(rmb2122.getRecgrp().getAds().getVouchefficientdate(), 7305));
                        } else {
                            rmb2122.getRecgrp().getAds().setVouchenddate(expdat);
                        }
                    }
                }
                if (Strings.mid(SettleContext.getTransName().toUpperCase(), 1, 2).equals("NI")) {
                    if (MdaUtils.compareTo(newExpflg, "X") == 0) {
                        rmb2122.getRecgrp().getAds().setVouchenddate(Dates.add(rmb2122.getRecgrp().getAds().getVouchefficientdate(), 7305));
                    } else {
                        if (!MdaUtils.isEmpty(newexpdat)) {
                            rmb2122.getRecgrp().getAds().setVouchenddate(newexpdat);
                        } else {
                            if (!MdaUtils.isEmpty(expdat)) {
                                rmb2122.getRecgrp().getAds().setVouchenddate(expdat);
                            } else {
                                rmb2122.getRecgrp().getAds().setVouchenddate(Dates.add(rmb2122.getRecgrp().getAds().getVouchefficientdate(), 7305));
                            }
                        }
                    }
                }
                if (MdaUtils.compareTo(purpos, "ISCO") == 0 || MdaUtils.compareTo(purpos, "ICCO") == 0 || MdaUtils.compareTo(hndtyp, "OC") == 0) {
                    if (Strings.mid(SettleContext.getTransName().toUpperCase(), 1, 2).equals("GI")) {
                        if (!MdaUtils.isEmpty(newliatyp)) {
                            if (!MdaUtils.isEmpty(newliadat)) {
                                rmb2122.getRecgrp().getAds().setVouchenddate(newliadat);
                            } else {
                                rmb2122.getRecgrp().getAds().setVouchenddate(Dates.add(rmb2122.getRecgrp().getAds().getVouchefficientdate(), 7305));
                            }
                        } else {
                            if (MdaUtils.isEmpty(liadat)) {
                                rmb2122.getRecgrp().getAds().setVouchenddate(Dates.add(rmb2122.getRecgrp().getAds().getVouchefficientdate(), 7305));
                            } else {
                                rmb2122.getRecgrp().getAds().setVouchenddate(liadat);
                            }
                        }
                    }
                    if (Strings.mid(SettleContext.getTransName().toUpperCase(), 1, 2).equals("NI")) {
                        if (MdaUtils.compareTo(newliaflg, "X") == 0) {
                            rmb2122.getRecgrp().getAds().setVouchenddate(Dates.add(rmb2122.getRecgrp().getAds().getVouchefficientdate(), 7305));
                        } else {
                            if (!MdaUtils.isEmpty(newliadat)) {
                                rmb2122.getRecgrp().getAds().setVouchenddate(newliadat);
                            } else {
                                if (MdaUtils.isEmpty(liadat)) {
                                    rmb2122.getRecgrp().getAds().setVouchenddate(Dates.add(rmb2122.getRecgrp().getAds().getVouchefficientdate(), 7305));
                                } else {
                                    rmb2122.getRecgrp().getAds().setVouchenddate(liadat);
                                }
                            }
                        }
                    }
                }
            } else {

                //String expflg = gidBizInfoVo.getExpflg();
                //String exptyp = gidBizInfoVo.getExptyp();
                //#VouchEndDate	担保到期日期
                //Date vouchenddate = gidBizInfoVo.getExpdat();
                if (MdaUtils.compareTo(purpos, "ISCO") == 0 || MdaUtils.compareTo(purpos, "ICCO") == 0 || MdaUtils.compareTo(hndtyp, "OC") == 0) {
                    if (MdaUtils.isEmpty(liadat)) {
                        rmb2122.getRecgrp().getAds().setVouchenddate(Dates.add(vouchefficientdate, 7305));
                    } else {
                        rmb2122.getRecgrp().getAds().setVouchenddate(liadat);
                    }
                } else {
                    if (MdaUtils.isEmpty(expdat)) {
                        rmb2122.getRecgrp().getAds().setVouchenddate(Dates.add(vouchefficientdate, 7305));
                    } else {
                        rmb2122.getRecgrp().getAds().setVouchenddate(expdat);
                    }
                }
            }

            //ContractNo担保合同编号
            if (StringUtils.isNotEmpty(orcref)) {
                rmb2122.getRecgrp().getAds().setContractno(orcref);
            }


            //VouchType	担保类型
            //11开头的对应01融资担保，1201对应03投标担保，1202对应05质量担保，1203对应履04约担保，1204对应02付款担保，1205对应06其他担保
            // undertaking_type对应GARTYP若为付款保函“J“则对应02付款担保1204变更为对应06其他担保
            //String gartyp = gidBizInfoVo.getGartyp();
            String vouchtype = "";
            switch (gartyp) {
                case "J":
                    vouchtype = "0002";
                    break;
                default:
                    String cfatyp1 = Strings.mid(cfatyp, 1, 2);
                    switch (cfatyp1) {
                        case "11":
                            vouchtype = "0001";
                            break;
                        case "12":
                            switch (cfatyp) {
                                case "1201":
                                    vouchtype = "0003";
                                    break;
                                case "1202":
                                    vouchtype = "0005";
                                    break;
                                case "1203":
                                    vouchtype = "0004";
                                    break;
                                case "1204":
                                    vouchtype = "0000";
                                    break;
                                case "1205":
                                    vouchtype = "0000";
                                    break;
                            }
                            break;
                        default:
                    }
            }
            if (!MdaUtils.isEmpty(vouchtype)) {
                rmb2122.getRecgrp().getAds().setVouchtype(vouchtype);
            } else {
                if (MdaUtils.isEmpty(rmb2122.getRecgrp().getAds().getVouchtype())) {
                    rmb2122.getRecgrp().getAds().setVouchtype("");
                }
            }
        }
    }


    @Override
    public void buildData(Rmb2122 rmb2122) {
        rmb2122.reset();
        rmb2122.getPblmod().setPblInfo(PblInfoBuilder.buildPblInfo(rmb2122));

    }

    @Override
    public void saveChildData(Rmb2122 rmb2122) {
        /**
         * TDO无须处理
         */
        rmb2122.getPblmod().setPblInfo(PblInfoBuilder.buildPblInfo(rmb2122));
    }

    @Override
    public void rmbLoadContract(Rmb2122 rmb2122) {
        /**
         * TDO无须处理
         */
    }

    @Override
    public List<String> setRMBPresaveoldValue(Rmb2122 rmb2122) {
        /**
         * TDO无须处理
         */
        return null;
    }


    public String getRmbRef(String msgtyp, String ownref) {
        //!光大改为生成业务主键
        String bopref = "";
        Date dat = Dates.today();
        String couNam = msgtyp + ownref;
        int counum = counterRmbService.dbRmbCounter(couNam, dat);
        while (MdaUtils.compareTo(Decimals.mul(Numerics.round(Decimals.div(counum, 100), 0), 100), counum) == 0) {
            counum = counterRmbService.dbRmbCounter(couNam, dat);
        }
        String couVal = MdaUtils.toString(counum, -4);
        bopref = msgtyp + ownref + couVal;
        return bopref;
    }
}
