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

import com.brilliance.isc.bo.*;
import com.brilliance.isc.bo.model.Zxbgrp;
import com.brilliance.isc.common.contants.CommonContants;
import com.brilliance.isc.common.counter.service.CounterService;
import com.brilliance.isc.common.util.Utils;
import com.brilliance.isc.common.whj.service.WhjService;
import com.brilliance.isc.common.whj.service.WhjZxbInterService;
import com.brilliance.isc.common.whj.vo.WhjDetailsResponseVo;
import com.brilliance.isc.esb.api.inner.InnerWhjServer;
import com.brilliance.isc.esb.model.inner.InnerReq;
import com.brilliance.isc.esb.model.inner.InnerReqWhj;
import com.brilliance.isc.esb.model.inner.InnerRes;
import com.brilliance.isc.esb.model.inner.InnerResWhj;
import com.brilliance.isc.esb.model.whj.ReqMsgData;
import com.brilliance.isc.esb.model.whj.content.sinosure.*;
import com.brilliance.isc.esb.model.whj.res.sinosure.*;
import com.brilliance.isc.mda.dao.CusMapper;
import com.brilliance.isc.mda.dao.FinMapper;
import com.brilliance.isc.mda.dao.FsaMapper;
import com.brilliance.isc.mda.dao.IneMapper;
import com.brilliance.isc.vo.finance.ZxbStoreVo;
import com.brilliance.mda.runtime.mda.util.Decimals;
import com.brilliance.mda.runtime.mda.util.Formats;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import com.brilliance.mda.runtime.mda.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

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

@Component
public class WhjZxbToolComponent {

    private static final String WHJ_SUCCESS = "0";
    private static final String TAB = "\\t";
    public static final String BUTTON_FINAPL = "finapl";
    public static final String BUTTON_FINAME = "finame";
    public static final String BUTTON_FINCHK = "finchk";
    public static final String BUTTON_KJCBTZ = "kjcbtz";
    public static final String BUTTON_CBTZXZ = "cbtzxz";
    public static final String BUTTON_LOAREG = "loareg";
    public static final String BUTTON_RCVREG = "rcvreg";
    public static final String BUTTON_LOAUND = "loaund";
    public static final String BUTTON_DETSRC = "detsrc";
    public static final String BUTTON_STAMDF = "stamdf";
    public static final String BUTTON_RETRY = "retry";
    public static final String BUTTON_FINCHG = "finchg";
    public static final String BUTTON_APLDEL = "apldel";
    public static final String BUTTON_BUND = "bund";
    public static final String BUTTON_UNBUND = "unbund";
    public static final String BUTTON_MOD = "mod";
    public static final String BUTTON_DEL = "del";
    public static final String BUTTON_SAV = "sav";
    public static final String BUTTON_EXIT = "exit";

    @Autowired
    private WhjService whjService;

    @Autowired
    private WhjZxbInterService whjZxbInterService;

    @Autowired(required = false)
    private InnerWhjServer innerWhjServer;

    @Autowired
    CounterService counterService;

    @Autowired
    private FinMapper finMapper;

    @Autowired
    private FsaMapper fsaMapper;

    @Autowired
    private IneMapper ineMapper;

    @Autowired
    private CusMapper cusMapper;

    public InnerRes<InnerResWhj> sinoBankApply(ZxbStoreVo zxbStoreVo) {
        SinoBankApplyContent sinoBankApplyContent = new SinoBankApplyContent();
        sinoBankApplyContent.setAmount(Utils.fmtAmtToWgLong(zxbStoreVo.getRecgrp().getRec().getRcvtotamt()));
        sinoBankApplyContent.setApplyAmount(Utils.fmtAmtToWgLong(zxbStoreVo.getRecgrp().getRec().getAmt()));
        sinoBankApplyContent.setApplyEntCode(zxbStoreVo.getRecgrp().getRec().getCustcode());
        sinoBankApplyContent.setPolicyNo(zxbStoreVo.getRecgrp().getRec().getPolicyno());
        sinoBankApplyContent.setAgreementNo(zxbStoreVo.getRecgrp().getRec().getAgreementno());
        sinoBankApplyContent.setOverseasPayEndDate(Formats.format(zxbStoreVo.getRecgrp().getRec().getConenddat(), "yyyy-MM-dd"));
        sinoBankApplyContent.setCurrency(zxbStoreVo.getRecgrp().getRec().getRcvtotcur());

        sinoBankApplyContent.setInvoices(new ArrayList<Invoice>());
        List<Ine> inelst = zxbStoreVo.getRecgrp().getInelst();
        if (inelst == null || inelst.size() == 0) {
            Invoice invoice = new Invoice();
            invoice.setImportEntName("");
            invoice.setInvoiceAmount(0L);
            invoice.setInvoiceCode("");

            sinoBankApplyContent.getInvoices().add(invoice);

            Manifest manifest = new Manifest();
            manifest.setManifestAmount(0L);
            manifest.setManifestCode("");

            invoice.setManifests(new ArrayList<Manifest>());
            invoice.getManifests().add(manifest);
        } else {
            for (Ine ine : inelst) {
                Invoice invoice = new Invoice();
                invoice.setImportEntName(ine.getClinam());
                invoice.setInvoiceAmount(Utils.fmtAmtToWgLong(ine.getAmt()));
                invoice.setInvoiceCode(ine.getIneref());

                sinoBankApplyContent.getInvoices().add(invoice);

                invoice.setManifests(new ArrayList<Manifest>());
                List<Cus> cuslst = zxbStoreVo.getRecgrp().getCuslst();
                if (cuslst == null || cuslst.size() == 0) {
                    Manifest manifest = new Manifest();
                    manifest.setManifestAmount(0L);
                    manifest.setManifestCode("");
                    invoice.getManifests().add(manifest);
                } else {
                    for (Cus cus : cuslst) {
                        if (MdaUtils.compareTo(cus.getIneref(), ine.getIneref()) == 0) {
                            Manifest manifest = new Manifest();
                            manifest.setManifestAmount(Utils.fmtAmtToWgLong(cus.getAmt()));
                            manifest.setManifestCode(cus.getCusref());
                            invoice.getManifests().add(manifest);
                        }
                    }
                }
            }
        }
        return whjZxbInterService.sinoBankApply(sinoBankApplyContent);
    }

    public InnerRes<InnerResWhj> bankApplySupplement(ZxbStoreVo zxbStoreVo) {
        BankApplySupplementContent bankApplySupplementContent = new BankApplySupplementContent();
        bankApplySupplementContent.setAmount(Utils.fmtAmtToWgLong(zxbStoreVo.getRecgrp().getRec().getRcvtotamt()));
        bankApplySupplementContent.setApplyAmount(Utils.fmtAmtToWgLong(zxbStoreVo.getRecgrp().getRec().getAmt()));
        bankApplySupplementContent.setApplyEntCode(zxbStoreVo.getRecgrp().getRec().getCustcode());
        bankApplySupplementContent.setPolicyNo(zxbStoreVo.getRecgrp().getRec().getPolicyno());
        bankApplySupplementContent.setAgreementNo(zxbStoreVo.getRecgrp().getRec().getAgreementno());
        bankApplySupplementContent.setNoticeId(zxbStoreVo.getRecgrp().getRec().getNoticeid());
        bankApplySupplementContent.setOverseasPayEndDate(Formats.format(zxbStoreVo.getRecgrp().getRec().getConenddat(), "yyyy-MM-dd"));
        bankApplySupplementContent.setCurrency(zxbStoreVo.getRecgrp().getRec().getRcvtotcur());

        bankApplySupplementContent.setInvoices(new ArrayList<SupInvoice>());
        List<Ine> inelst = zxbStoreVo.getRecgrp().getInelst();
        if (inelst == null || inelst.size() == 0) {
            SupInvoice supInvoice = new SupInvoice();
            supInvoice.setImportEntName("");
            supInvoice.setInvoiceAmount(0L);
            supInvoice.setInvoiceCode("");
            supInvoice.setInvoiceCurrency("");
            supInvoice.setShipmentId("");

            bankApplySupplementContent.getInvoices().add(supInvoice);

            Manifest manifest = new Manifest();
            manifest.setManifestAmount(0L);
            manifest.setManifestCode("");
            supInvoice.setManifests(new ArrayList<Manifest>());
            supInvoice.getManifests().add(manifest);
        } else {
            for (Ine ine : inelst) {
                SupInvoice supInvoice = new SupInvoice();
                supInvoice.setImportEntName(ine.getClinam());
                supInvoice.setInvoiceAmount(Utils.fmtAmtToWgLong(ine.getAmt()));
                supInvoice.setInvoiceCode(ine.getIneref());
                supInvoice.setInvoiceCurrency(ine.getCur());
                supInvoice.setShipmentId(ine.getShipid());

                bankApplySupplementContent.getInvoices().add(supInvoice);

                supInvoice.setManifests(new ArrayList<Manifest>());
                List<Cus> cuslst = zxbStoreVo.getRecgrp().getCuslst();
                if (cuslst == null || cuslst.size() == 0) {
                    Manifest manifest = new Manifest();
                    manifest.setManifestAmount(0L);
                    manifest.setManifestCode("");
                    supInvoice.getManifests().add(manifest);
                } else {
                    for (Cus cus : cuslst) {
                        if (MdaUtils.compareTo(cus.getIneref(), ine.getIneref()) == 0) {
                            Manifest manifest = new Manifest();
                            manifest.setManifestAmount(Utils.fmtAmtToWgLong(cus.getAmt()));
                            manifest.setManifestCode(cus.getCusref());
                            supInvoice.getManifests().add(manifest);
                        }
                    }
                }
            }
        }
        return whjZxbInterService.bankApplySupplement(bankApplySupplementContent);
    }

    public InnerRes<InnerResWhj> sinoCheck(ZxbStoreVo zxbStoreVo) {
        SinoCheckContent sinoCheckContent = new SinoCheckContent();
        sinoCheckContent.setSafeAssetId(zxbStoreVo.getRecgrp().getRec().getWgrref());
        return whjZxbInterService.sinoCheck(sinoCheckContent);
    }

    public InnerRes<InnerResWhj> applyNotice(ZxbStoreVo zxbStoreVo, int opt) {
        ApplyNoticeContent applyNoticeContent = new ApplyNoticeContent();
        applyNoticeContent.setSafeAssetId(zxbStoreVo.getRecgrp().getRec().getWgrref());
        applyNoticeContent.setOperate(opt);
        return whjZxbInterService.applyNotice(applyNoticeContent);
    }

    public InnerRes<InnerResWhj> sinoPay(ZxbStoreVo zxbStoreVo) {
        SinoPayContent sinoPayContent = new SinoPayContent();
        sinoPayContent.setNote(zxbStoreVo.getRecgrp().getRec().getRemark());
        sinoPayContent.setPayDate(Formats.format(zxbStoreVo.getRecgrp().getRec().getLoadat(), "yyyy-MM-dd"));
        sinoPayContent.setRecDate(Formats.format(zxbStoreVo.getRecgrp().getRec().getPrcrepdat(), "yyyy-MM-dd"));
        sinoPayContent.setSafeAssetId(zxbStoreVo.getRecgrp().getRec().getWgrref());
        sinoPayContent.setPayCurrency(zxbStoreVo.getRecgrp().getRec().getFinloacur());
        List<Ine> inelst = zxbStoreVo.getRecgrp().getInelst();
        if (inelst != null) {
            sinoPayContent.setPayList(new ArrayList<PayList>());
            for (int i = 0; i < inelst.size(); i++) {
                Ine ine = inelst.get(i);
                BigDecimal loaamt = Decimals.ZERO_SCALE2;
                if (MdaUtils.compareTo(ine.getAmt(), Decimals.ZERO_SCALE0) > 0 && !MdaUtils.isEmpty(ine.getIneref())) {
                    if (i == 0) {
                        loaamt = zxbStoreVo.getRecgrp().getRec().getFinloaamt();
                    } else {
                        loaamt = new BigDecimal("1");
                    }
                }
                PayList payList = new PayList();
                payList.setPayAmount(Utils.fmtAmtToWgLong(loaamt));
                payList.setInvoiceNo(ine.getIneref());
                sinoPayContent.getPayList().add(payList);
            }
        }
        return whjZxbInterService.sinoPay(sinoPayContent);
    }

    public InnerRes<InnerResWhj> sinoRecOnce(ZxbStoreVo zxbStoreVo) {
        SinoRecOnceContent sinoRecOnceContent = new SinoRecOnceContent();
        sinoRecOnceContent.setSafeAssetId(zxbStoreVo.getRecgrp().getRec().getWgrref());
        List<Ine> inelst = zxbStoreVo.getRecgrp().getInelst();
        if (inelst != null) {
            sinoRecOnceContent.setRecs(new ArrayList<RecList>());
            for (int i = 0; i < inelst.size(); i++) {
                BigDecimal rcvamt = Decimals.ZERO_SCALE2;
                if (i == 0) {
                    rcvamt = zxbStoreVo.getRecgrp().getRec().getFinloaamt();
                }
                Ine ine = inelst.get(i);
                if (MdaUtils.compareTo(ine.getAmt(), Decimals.ZERO_SCALE0) > 0 && !MdaUtils.isEmpty(ine.getIneref())) {
                    RecList recsList = new RecList();
                    recsList.setRecAmount(Utils.fmtAmtToWgLong(rcvamt));
                    recsList.setInvoiceNo(ine.getIneref());
                    recsList.setRecDate(Formats.format(zxbStoreVo.getRecgrp().getRec().getFacrcvdat(), "yyyy-MM-dd"));
                    sinoRecOnceContent.getRecs().add(recsList);
                }
            }
        }
        return whjZxbInterService.sinoRecOnce(sinoRecOnceContent);
    }

    public InnerRes<InnerResWhj> sinoCorrect(ZxbStoreVo zxbStoreVo) {
        SinoCorrectContent sinoCorrectContent = new SinoCorrectContent();
        sinoCorrectContent.setSafeAssetId(zxbStoreVo.getRecgrp().getRec().getWgrref());
        sinoCorrectContent.setReason(zxbStoreVo.getRecgrp().getRec().getRfurea());
        return whjZxbInterService.sinoCorrect(sinoCorrectContent);
    }

    public InnerRes<InnerResWhj> sinoDetail(ZxbStoreVo zxbStoreVo) {
        SinoCheckContent sinoCheckContent = new SinoCheckContent();
        sinoCheckContent.setSafeAssetId(zxbStoreVo.getRecgrp().getRec().getWgrref());
        return whjZxbInterService.sinoDetail(sinoCheckContent);
    }

    public InnerRes<InnerResWhj> sinoRetry(ZxbStoreVo zxbStoreVo) {
        SinoCheckContent sinoCheckContent = new SinoCheckContent();
        sinoCheckContent.setSafeAssetId(zxbStoreVo.getRecgrp().getRec().getWgrref());
        return whjZxbInterService.sinoRetry(sinoCheckContent);
    }

    public InnerRes<InnerResWhj> changeToExport(ZxbStoreVo zxbStoreVo) {
        SinoCheckContent sinoCheckContent = new SinoCheckContent();
        sinoCheckContent.setSafeAssetId(zxbStoreVo.getRecgrp().getRec().getWgrref());
        return whjZxbInterService.changeToExport(sinoCheckContent);
    }

    public InnerRes<InnerResWhj> bankApplyUpdate(ZxbStoreVo zxbStoreVo) {
        BankApplyUpdateContent bankApplyUpdateContent = new BankApplyUpdateContent();
        bankApplyUpdateContent.setSafeAssetId(zxbStoreVo.getRecgrp().getRec().getWgrref());
        bankApplyUpdateContent.setAmount(Utils.fmtAmtToWgLong(zxbStoreVo.getRecgrp().getRec().getRcvtotamt()));
        bankApplyUpdateContent.setApplyAmount(Utils.fmtAmtToWgLong(zxbStoreVo.getRecgrp().getRec().getAmt()));
        bankApplyUpdateContent.setApplyEntCode(zxbStoreVo.getRecgrp().getRec().getCustcode());
        bankApplyUpdateContent.setPolicyNo(zxbStoreVo.getRecgrp().getRec().getPolicyno());
        bankApplyUpdateContent.setAgreementNo(zxbStoreVo.getRecgrp().getRec().getAgreementno());
        bankApplyUpdateContent.setOverseasPayEndDate(Formats.format(zxbStoreVo.getRecgrp().getRec().getConenddat(), "yyyy-MM-dd"));
        bankApplyUpdateContent.setCurrency(zxbStoreVo.getRecgrp().getRec().getRcvtotcur());

        bankApplyUpdateContent.setInvoices(new ArrayList<UpdateInvoice>());
        List<Ine> inelst = zxbStoreVo.getRecgrp().getInelst();
        if (inelst == null || inelst.size() == 0) {
            UpdateInvoice updateInvoice = new UpdateInvoice();
            updateInvoice.setImportEntName("");
            updateInvoice.setInvoiceAmount(0L);
            updateInvoice.setInvoiceCode("");
            updateInvoice.setShipmentId("");

            bankApplyUpdateContent.getInvoices().add(updateInvoice);

            Manifest manifest = new Manifest();
            manifest.setManifestAmount(0L);
            manifest.setManifestCode("");
            updateInvoice.getManifests().add(manifest);
        } else {
            for (Ine ine : inelst) {
                UpdateInvoice updateInvoice = new UpdateInvoice();
                updateInvoice.setImportEntName(ine.getClinam());
                updateInvoice.setInvoiceAmount(Utils.fmtAmtToWgLong(ine.getAmt()));
                updateInvoice.setInvoiceCode(ine.getIneref());
                updateInvoice.setShipmentId(ine.getShipid());

                bankApplyUpdateContent.getInvoices().add(updateInvoice);

                updateInvoice.setManifests(new ArrayList<Manifest>());
                List<Cus> cuslst = zxbStoreVo.getRecgrp().getCuslst();
                if (cuslst == null || cuslst.size() == 0) {
                    Manifest manifest = new Manifest();
                    manifest.setManifestAmount(0L);
                    manifest.setManifestCode("");
                    updateInvoice.getManifests().add(manifest);
                } else {
                    for (Cus cus : cuslst) {
                        if (MdaUtils.compareTo(cus.getIneref(), ine.getIneref()) == 0) {
                            Manifest manifest = new Manifest();
                            manifest.setManifestAmount(Utils.fmtAmtToWgLong(cus.getAmt()));
                            manifest.setManifestCode(cus.getCusref());
                            updateInvoice.getManifests().add(manifest);
                        }
                    }
                }
            }
        }
        return whjZxbInterService.bankApplyUpdate(bankApplyUpdateContent);
    }

    public InnerRes<InnerResWhj> sinoAbandon(ZxbStoreVo zxbStoreVo) {
        SinoAbandonContent sinoAbandonContent = new SinoAbandonContent();
        sinoAbandonContent.setSafeAssetId(zxbStoreVo.getRecgrp().getRec().getWgrref());
        sinoAbandonContent.setReason(zxbStoreVo.getRecgrp().getRec().getRfurea());
        return whjZxbInterService.sinoAbandon(sinoAbandonContent);
    }

    public InnerRes<InnerResWhj> rejectAccept(ZxbStoreVo zxbStoreVo) {
        SinoAcceptContent sinoAcceptContent = new SinoAcceptContent();
        sinoAcceptContent.setSafeAssetId(zxbStoreVo.getRecgrp().getRec().getWgrref());
        sinoAcceptContent.setReason(zxbStoreVo.getRecgrp().getRec().getRfurea());
        return whjZxbInterService.rejectAccept(sinoAcceptContent);
    }

    public boolean buttonHandle(String typ, ZxbStoreVo zxbStoreVo) {
//        //锁定交易
//        Fin fin = zxbStoreVo.getRecgrp().getRec();
//        whjService.lockFin(fin);
//        try {
        zxbStoreVo.getRecgrp().getFsa().setBnkaplsta(typ);
        if (!"RTY".equals(typ)) {
            zxbStoreVo.getRecgrp().getFsa().setAplret("W");
        }
        InnerRes<InnerResWhj> innerRes = setFsaAplret(typ, zxbStoreVo);
        boolean result = whjService.handleResult(zxbStoreVo.getRecgrp().getRec(), zxbStoreVo.getRecgrp().getFsa(), typ, innerRes);
        if ("CXZ".equals(typ) && result) {
            ApplyNotice applyNotice = (ApplyNotice) Utils.getObjectByMap(ApplyNotice.class.getName(), (LinkedHashMap) innerRes.DATA.detail);
            zxbStoreVo.setPdfData(applyNotice.getPdfData());
        }
        whjService.setFsaHandType(zxbStoreVo.getRecgrp().getFsa(), typ);
        finMapper.updateByPrimaryKey(zxbStoreVo.getRecgrp().getRec());
//            //释放锁
//            whjService.unLockFin(fin);
        return result;
//        } catch (Exception e) {
//            //发生异常需要释放锁定
//            whjService.unLockFin(fin);
//            throw e;
//        }
    }

    public InnerRes<InnerResWhj> setFsaAplret(String typ, ZxbStoreVo zxbStoreVo) {
        InnerRes<InnerResWhj> innerRes = null;
        switch (typ) {
            case "APL":
                if (MdaUtils.isEmpty(zxbStoreVo.getRecgrp().getRec().getNoticeid())) {
                    innerRes = sinoBankApply(zxbStoreVo);
                } else {
                    innerRes = bankApplySupplement(zxbStoreVo);
                }
                break;
            case "CHK":
                innerRes = sinoCheck(zxbStoreVo);
                break;
            case "KJC":
                innerRes = applyNotice(zxbStoreVo, 1);
                break;
            case "CXZ":
                innerRes = applyNotice(zxbStoreVo, 2);
                break;
            case "REG":
                innerRes = sinoPay(zxbStoreVo);
                break;
            case "RCV":
                if (MdaUtils.compareTo(zxbStoreVo.getRecgrp().getRec().getRcvamt(), zxbStoreVo.getRecgrp().getRec().getFinloaamt()) != 0) {
                    zxbStoreVo.getRecgrp().getRec().setRcvamt(zxbStoreVo.getRecgrp().getRec().getFinloaamt());
                }
                innerRes = sinoRecOnce(zxbStoreVo);
                break;
            case "UND":
                innerRes = sinoCorrect(zxbStoreVo);
                break;
            case "DET":
                innerRes = sinoDetail(zxbStoreVo);
                break;
            case "RTY":
                innerRes = sinoRetry(zxbStoreVo);
                break;
            case "CHG":
                innerRes = changeToExport(zxbStoreVo);
                break;
            case "AME":
                innerRes = bankApplyUpdate(zxbStoreVo);
                break;
            case "ABA":
                innerRes = sinoAbandon(zxbStoreVo);
                break;
            case "RFU":
                innerRes = rejectAccept(zxbStoreVo);
                break;
            default:
        }
        return innerRes;
    }

    public boolean buttonHandle1(String typ, ZxbStoreVo zxbStoreVo) {
        //锁定交易
        Fin fin = zxbStoreVo.getRecgrp().getRec();
        whjService.lockFin(fin);
        try {
            zxbStoreVo.getRecgrp().getFsa().setBnkaplsta(typ);
            if (!"RTY".equals(typ)) {
                zxbStoreVo.getRecgrp().getFsa().setAplret("W");
            }
            InnerRes<InnerResWhj> innerRes = setFsaAplret1(typ, zxbStoreVo);
            boolean result = whjService.handleResult(zxbStoreVo.getRecgrp().getRec(), zxbStoreVo.getRecgrp().getFsa(), typ, innerRes);
            whjService.setFsaHandType(zxbStoreVo.getRecgrp().getFsa(), typ);
            finMapper.updateByPrimaryKey(zxbStoreVo.getRecgrp().getRec());

            return result;
        } finally {
            whjService.unLockFin(fin);
        }
    }

    public InnerRes<InnerResWhj> setFsaAplret1(String typ, ZxbStoreVo zxbStoreVo) {
        InnerRes<InnerResWhj> innerRes = null;
        switch (typ) {
            case "APL":
                if (MdaUtils.isEmpty(zxbStoreVo.getRecgrp().getRec().getNoticeid())) {
                    innerRes = sinoBankApply(zxbStoreVo);
                } else {
                    innerRes = bankApplySupplement(zxbStoreVo);
                }
                break;
            case "CHK":
                innerRes = sinoCheck(zxbStoreVo);
                break;
            case "KJC":
                innerRes = applyNotice(zxbStoreVo, 1);
                break;
            case "CXZ":
                innerRes = applyNotice(zxbStoreVo, 2);
                break;
            case "REG":
                SinoPayContent sinoPayContent = new SinoPayContent();
                sinoPayContent.setNote(zxbStoreVo.getRecgrp().getRec().getRemark());
                sinoPayContent.setPayDate(Formats.format(zxbStoreVo.getRecgrp().getRec().getLoadat(), "yyyy-MM-dd"));
                sinoPayContent.setRecDate(Formats.format(zxbStoreVo.getRecgrp().getRec().getPrcrepdat(), "yyyy-MM-dd"));
                sinoPayContent.setSafeAssetId(zxbStoreVo.getRecgrp().getRec().getWgrref());
                sinoPayContent.setPayCurrency(zxbStoreVo.getRecgrp().getRec().getFinloacur());
                List<Ine> inelst = zxbStoreVo.getRecgrp().getInelst();
                if (inelst != null) {
                    sinoPayContent.setPayList(new ArrayList<PayList>());
                    for (int i = 0; i < inelst.size(); i++) {
                        Ine ine = inelst.get(i);
                        BigDecimal loaamt = Decimals.ZERO_SCALE2;
                        if (MdaUtils.compareTo(ine.getAmt(), Decimals.ZERO_SCALE0) > 0 && !MdaUtils.isEmpty(ine.getIneref())) {
                            if (i == 0) {
                                loaamt = zxbStoreVo.getRecgrp().getRec().getFinloaamt();
                            } else {
                                loaamt = new BigDecimal("1");
                            }
                        }
                        PayList payList = new PayList();
                        payList.setPayAmount(Utils.fmtAmtToWgLong(loaamt));
                        payList.setInvoiceNo(ine.getIneref());
                        sinoPayContent.getPayList().add(payList);
                    }
                }
                ReqMsgData<SinoPayContent> reqMsgData = new ReqMsgData<>(sinoPayContent);
                reqMsgData.method = "/v1/asset/directLink/pay";
                reqMsgData.service = "SINOSURE";
                reqMsgData.bankSafeCode = "";

                InnerReqWhj innerReqWhj = new InnerReqWhj();
                innerReqWhj.serviceType = "SINOSUREREQ";
                innerReqWhj.reqMsgData = reqMsgData;

                InnerReq<InnerReqWhj> innerReqWhjInnerReq = new InnerReq<>(innerReqWhj);

                innerRes = innerWhjServer.callWhj(innerReqWhjInnerReq);
                break;
            case "RCV":
                if (MdaUtils.compareTo(zxbStoreVo.getRecgrp().getRec().getRcvamt(), zxbStoreVo.getRecgrp().getRec().getFinloaamt()) != 0) {
                    zxbStoreVo.getRecgrp().getRec().setRcvamt(zxbStoreVo.getRecgrp().getRec().getFinloaamt());
                }
                SinoRecOnceContent sinoRecOnceContent = new SinoRecOnceContent();
                sinoRecOnceContent.setSafeAssetId(zxbStoreVo.getRecgrp().getRec().getWgrref());
                List<Ine> inelst1 = zxbStoreVo.getRecgrp().getInelst();
                if (inelst1 != null) {
                    sinoRecOnceContent.setRecs(new ArrayList<RecList>());
                    for (int i = 0; i < inelst1.size(); i++) {
                        BigDecimal rcvamt = Decimals.ZERO_SCALE2;
                        if (i == 0) {
                            rcvamt = zxbStoreVo.getRecgrp().getRec().getFinloaamt();
                        }
                        Ine ine = inelst1.get(i);
                        if (MdaUtils.compareTo(ine.getAmt(), Decimals.ZERO_SCALE0) > 0 && !MdaUtils.isEmpty(ine.getIneref())) {
                            RecList recsList = new RecList();
                            recsList.setRecAmount(Utils.fmtAmtToWgLong(rcvamt));
                            recsList.setInvoiceNo(ine.getIneref());
                            recsList.setRecDate(Formats.format(zxbStoreVo.getRecgrp().getRec().getFacrcvdat(), "yyyy-MM-dd"));
                            sinoRecOnceContent.getRecs().add(recsList);
                        }
                    }
                }
                ReqMsgData<SinoRecOnceContent> reqMsgData1 = new ReqMsgData<>(sinoRecOnceContent);
                reqMsgData1.method = "/v1/asset/directLink/recOnce";
                reqMsgData1.service = "SINOSURE";
                reqMsgData1.bankSafeCode = "";

                InnerReqWhj innerReqWhj1 = new InnerReqWhj();
                innerReqWhj1.serviceType = "SINOSUREREQ";
                innerReqWhj1.reqMsgData = reqMsgData1;

                InnerReq<InnerReqWhj> innerReqWhjInnerReq1 = new InnerReq<>(innerReqWhj1);

                innerRes = innerWhjServer.callWhj(innerReqWhjInnerReq1);
                break;
            case "UND":
                innerRes = sinoCorrect(zxbStoreVo);
                break;
            case "DET":
                innerRes = sinoDetail(zxbStoreVo);
                break;
            case "RTY":
                innerRes = sinoRetry(zxbStoreVo);
                break;
            case "CHG":
                innerRes = changeToExport(zxbStoreVo);
                break;
            case "AME":
                innerRes = bankApplyUpdate(zxbStoreVo);
                break;
            case "ABA":
                innerRes = sinoAbandon(zxbStoreVo);
                break;
            case "RFU":
                innerRes = rejectAccept(zxbStoreVo);
                break;
            default:
        }
        return innerRes;
    }

    public void disableAllZxbButton(ZxbStoreVo zxbStoreVo) {
        zxbStoreVo.getDisableSet().add(BUTTON_FINAPL);
        zxbStoreVo.getDisableSet().add(BUTTON_FINAME);
        zxbStoreVo.getDisableSet().add(BUTTON_FINCHK);
        zxbStoreVo.getDisableSet().add(BUTTON_KJCBTZ);
        zxbStoreVo.getDisableSet().add(BUTTON_CBTZXZ);
        zxbStoreVo.getDisableSet().add(BUTTON_LOAREG);
        zxbStoreVo.getDisableSet().add(BUTTON_RCVREG);
        zxbStoreVo.getDisableSet().add(BUTTON_LOAUND);
        zxbStoreVo.getDisableSet().add(BUTTON_DETSRC);
        zxbStoreVo.getDisableSet().add(BUTTON_STAMDF);
        zxbStoreVo.getDisableSet().add(BUTTON_RETRY);
        zxbStoreVo.getDisableSet().add(BUTTON_FINCHG);
        zxbStoreVo.getDisableSet().add(BUTTON_APLDEL);
        zxbStoreVo.getDisableSet().add(BUTTON_BUND);
        zxbStoreVo.getDisableSet().add(BUTTON_UNBUND);
        zxbStoreVo.getDisableSet().add(BUTTON_MOD);
        zxbStoreVo.getDisableSet().add(BUTTON_DEL);
        zxbStoreVo.getDisableSet().add(BUTTON_SAV);
//        zxbStoreVo.getDisableSet().add(BUTTON_EXIT);
    }

    public void initZxbpButton(ZxbStoreVo zxbStoreVo) {
        if (!MdaUtils.isEmpty(zxbStoreVo.getRecgrp().getFsa().getInr())) {
            if (!MdaUtils.isEmpty(zxbStoreVo.getRecgrp().getFsa().getDelflg())) {
                return;
            }
        }
        String bnkaplsta = zxbStoreVo.getRecgrp().getFsa().getBnkaplsta();
        String aplret = zxbStoreVo.getRecgrp().getFsa().getAplret();
        String blcsta = zxbStoreVo.getRecgrp().getFsa().getBlcsta();
        String sta = zxbStoreVo.getRecgrp().getFsa().getSta();
        Map<String, String> res = setCheckResult(zxbStoreVo);
        String chksta = res.get("chksta");
        String chkret = res.get("chkret");

/*没有拒绝受理按钮        if (("SUCCESS".equals(sta) && ("APPLY".equals(blcsta) || "CHECK".equals(blcsta))) && ("APL".equals(bnkaplsta) || "CHK".equals(bnkaplsta))) {
            zxbStoreVo.getRecpan().setIsenableRfu(true);
        }
        if ("RFU".equals(bnkaplsta) && "O".equals(aplret)) {
            zxbStoreVo.getRecpan().setIsenableRfu(true);
        }*/
        if ("REG".equals(bnkaplsta) && "O".equals(aplret)) {
            zxbStoreVo.getDisableSet().remove(BUTTON_LOAREG);
        }
        if (("PAY".equals(blcsta) || "REC".equals(blcsta) || "CLEAR".equals(blcsta)) && "D".equals(aplret)) {
            zxbStoreVo.getDisableSet().remove(BUTTON_LOAUND);
        }
        if ("UND".equals(bnkaplsta) && "O".equals(aplret)) {
            zxbStoreVo.getDisableSet().remove(BUTTON_LOAUND);
        }
        if ("FAIL".equals(sta) && !"APPLY".equals(blcsta)) {
            zxbStoreVo.getDisableSet().remove(BUTTON_RETRY);
        }
        if (!MdaUtils.isEmpty(zxbStoreVo.getRecgrp().getRec().getWgrref())) {
            zxbStoreVo.getDisableSet().remove(BUTTON_DETSRC);
        }
        if ("WAI".equals(bnkaplsta) || "BRK".equals(bnkaplsta) || MdaUtils.isEmpty(zxbStoreVo.getRecgrp().getRec().getWgrref())) {
            zxbStoreVo.getDisableSet().remove(BUTTON_MOD);
            zxbStoreVo.getDisableSet().remove(BUTTON_DEL);
        }
        if (("APL".equals(bnkaplsta) && "O".equals(aplret)) || ("APL".equals(bnkaplsta) && "D".equals(aplret) && "APPLY".equals(blcsta) && !"SUCCESS".equals(sta))) {
            zxbStoreVo.getDisableSet().remove(BUTTON_DEL);
        }

        Date now = new Date();
        Date updtim = zxbStoreVo.getRecgrp().getFsa().getUpdtim();
        long diff = (now.getTime() - updtim.getTime()) / 1000 / 60;
        if ("W".equals(zxbStoreVo.getRecgrp().getFsa().getAplret()) && diff > 3) {
            disableAllZxbButton(zxbStoreVo);
            zxbStoreVo.getDisableSet().remove(BUTTON_STAMDF);
            zxbStoreVo.getDisableSet().remove(BUTTON_EXIT);
            zxbStoreVo.getDisableSet().remove(BUTTON_SAV);
        }

        if (isOptAvail(zxbStoreVo, "APL", bnkaplsta, aplret, blcsta, sta, chksta, chkret)) {
            zxbStoreVo.getDisableSet().remove(BUTTON_FINAPL);
        }
        if (isOptAvail(zxbStoreVo, "AME", bnkaplsta, aplret, blcsta, sta, chksta, chkret)) {
            zxbStoreVo.getDisableSet().remove(BUTTON_FINAME);
        }
        if (isOptAvail(zxbStoreVo, "CHK", bnkaplsta, aplret, blcsta, sta, chksta, chkret)) {
            zxbStoreVo.getDisableSet().remove(BUTTON_FINCHK);
        }
        if (isOptAvail(zxbStoreVo, "KJC", bnkaplsta, aplret, blcsta, sta, chksta, chkret)) {
            zxbStoreVo.getDisableSet().remove(BUTTON_KJCBTZ);
        }
        if (isOptAvail(zxbStoreVo, "CXZ", bnkaplsta, aplret, blcsta, sta, chksta, chkret)) {
            zxbStoreVo.getDisableSet().remove(BUTTON_CBTZXZ);
        }
        if (isOptAvail(zxbStoreVo, "BUN", bnkaplsta, aplret, blcsta, sta, chksta, chkret)) {
            zxbStoreVo.getDisableSet().remove(BUTTON_BUND);
        }
        if (isOptAvail(zxbStoreVo, "UNB", bnkaplsta, aplret, blcsta, sta, chksta, chkret)) {
            zxbStoreVo.getDisableSet().remove(BUTTON_UNBUND);
        }
        if (isOptAvail(zxbStoreVo, "ABA", bnkaplsta, aplret, blcsta, sta, chksta, chkret)) {
            zxbStoreVo.getDisableSet().remove(BUTTON_APLDEL);
        }
        if (isOptAvail(zxbStoreVo, "CHG", bnkaplsta, aplret, blcsta, sta, chksta, chkret)) {
            zxbStoreVo.getDisableSet().remove(BUTTON_FINCHG);
        }
        if (isOptAvail(zxbStoreVo, "REG", bnkaplsta, aplret, blcsta, sta, chksta, chkret)) {
            zxbStoreVo.getDisableSet().remove(BUTTON_LOAREG);
        }
        if (isOptAvail(zxbStoreVo, "RCV", bnkaplsta, aplret, blcsta, sta, chksta, chkret)) {
            zxbStoreVo.getDisableSet().remove(BUTTON_RCVREG);
        }

//        if(SettleContext.getUserSession().getUsr().get){
//
//        }
    }

    public Map<String, String> setCheckResult(ZxbStoreVo zxbStoreVo) {
        Map<String, String> res = new HashMap<>();
        String vchksta = "";    //核验情况
        String vchkret = "Y";   //总的核验结果
        for (Cus cus : zxbStoreVo.getRecgrp().getCuslst()) {
            if (MdaUtils.compareTo(cus.getChksta(), " ") == 0) {
                vchksta = "A";
                break;
            }
        }
        if (MdaUtils.isEmpty(vchksta)) {
            for (Cus cus : zxbStoreVo.getRecgrp().getCuslst()) {
                if (MdaUtils.compareTo(cus.getChksta(), "CODE_ERROR") == 0 || MdaUtils.compareTo(cus.getChksta(), "CURRENCY_ERROR") == 0) {
                    vchksta = "B";
                    break;
                }
            }
        }
        if (MdaUtils.isEmpty(vchksta)) {
            for (Cus cus : zxbStoreVo.getRecgrp().getCuslst()) {
                if (MdaUtils.compareTo(cus.getChksta(), "OVERFLOW") == 0) {
                    vchksta = "C";
                    break;
                }
            }
        }
        if (MdaUtils.isEmpty(vchksta)) {
            vchksta = "D";
        }
        for (Cus cus : zxbStoreVo.getRecgrp().getCuslst()) {
            if (MdaUtils.compareTo(cus.getChkret(), "SUCCESS") != 0) {
                vchkret = "N";
                break;
            }
        }
        res.put("chksta", vchksta);
        res.put("chkret", vchkret);
        return res;
    }

    public boolean isOptAvail(ZxbStoreVo zxbStoreVo, String opt, String bnkaplsta, String aplret, String blcsta, String sta, String chksta, String chkret) {
        boolean available = false;
        String fsainr = NULLSTR;
        String sql = NULLSTR;
        int err = 0;
        switch (opt) {
            //融资申请
            case "APL":
                if (MdaUtils.isEmpty(zxbStoreVo.getRecgrp().getRec().getWgrref())) {
                    return true;
                }
                //关单核验
                break;
            case "CHK":
                if (MdaUtils.compareTo(blcsta, "APPLY") == 0 && MdaUtils.compareTo(sta, "SUCCESS") == 0) {
                    return true;
                }
                //开具承保通知书
                break;
            case "KJC":
                if ((MdaUtils.compareTo(blcsta, "CHECK") == 0 || (MdaUtils.compareTo(blcsta, "APPLY") == 0 && MdaUtils.compareTo(sta, "SUCCESS") == 0)) && MdaUtils.isEmpty(zxbStoreVo.getRecgrp().getRec().getNoticeid())) {
                    return true;
                }
                //承保通知书下载
                break;
            case "CXZ":
                if (isOptAvail(zxbStoreVo, "KJC", bnkaplsta, aplret, blcsta, sta, chksta, chkret)) {
                    return true;
                }
                if (!MdaUtils.isEmpty(zxbStoreVo.getRecgrp().getRec().getNoticeid())) {
                    return true;
                }
                //绑定
                break;
            case "BUN":
                if (MdaUtils.isEmpty(zxbStoreVo.getRecgrp().getRec().getRelref())) {
                    if (MdaUtils.compareTo(blcsta, "CHECK") == 0 && MdaUtils.compareTo(sta, "SUCCESS") == 0 && MdaUtils.compareTo(chkret, "Y") == 0 && (MdaUtils.compareTo(chksta, "C") == 0 || MdaUtils.compareTo(chksta, "D") == 0)) {
                        return true;
                    }
                }
                //解绑定
                break;
            case "UNB":
                if (!MdaUtils.isEmpty(zxbStoreVo.getRecgrp().getRec().getRelref())) {
                    Fsa fsa = new Fsa();
                    fsa.setFininr(zxbStoreVo.getRecgrp().getFsa().getFininr());
                    fsa.setBlcsta("PAY");
                    fsa.setSta("SUCCESS");
//                    fsa.setIsvalid("Y");
                    List<Fsa> fsalst = fsaMapper.selectByConditions(fsa);

                    if (fsalst == null || fsalst.size() == 0) {
                        return true;
                    }
                }
                //修改申请
                break;
            case "AME":
                if (!MdaUtils.isEmpty(zxbStoreVo.getRecgrp().getRec().getWgrref())) {
                    if (Strings.pos("-APPLY-CHECK-", blcsta) > 0 || (MdaUtils.compareTo(blcsta, "PAY") == 0 && Strings.pos("-FAIL-ERROR-", sta) > 0) || (Strings.pos("-APL-AME-", bnkaplsta) > 0 && MdaUtils.compareTo(aplret, "O") == 0)) {
                        return true;
                    }
                }
                //转普通
                break;
            case "CHG":
                if (!MdaUtils.isEmpty(zxbStoreVo.getRecgrp().getRec().getWgrref())) {
                    if (Strings.pos("-APPLY-CHECK-", blcsta) > 0 || (MdaUtils.compareTo(blcsta, "PAY") == 0 && Strings.pos("-FAIL-ERROR-", sta) > 0)) {
                        return true;
                    }
                }
                //申请废弃
                break;
            case "ABA":
                if (!MdaUtils.isEmpty(zxbStoreVo.getRecgrp().getRec().getWgrref())) {
                    if (Strings.pos("-APPLY-CHECK-", blcsta) > 0 || (MdaUtils.compareTo(blcsta, "PAY") == 0 && Strings.pos("-FAIL-ERROR-", sta) > 0) || (Strings.pos("-APL-ABA-AME-", bnkaplsta) > 0 && MdaUtils.compareTo(aplret, "O") == 0)) {
                        return true;
                    }
                }
                //放款登记
                break;
            case "REG":
                if (!MdaUtils.isEmpty(zxbStoreVo.getRecgrp().getRec().getRelref())) {
                    if (MdaUtils.compareTo(blcsta, "CHECK") == 0 && MdaUtils.compareTo(sta, "SUCCESS") == 0 && !MdaUtils.isEmpty(zxbStoreVo.getRecgrp().getRec().getNoticeid()) && (MdaUtils.compareTo(chksta, "C") == 0 || MdaUtils.compareTo(chksta, "D") == 0)) {
                        return true;
                    }
                }
                //还款登记
                break;
            case "RCV":
                if (MdaUtils.compareTo(blcsta, "PAY") == 0 && MdaUtils.compareTo(sta, "SUCCESS") == 0) {
                    return true;
                }
                break;
            default:
        }
        return available;
    }

    public WhjDetailsResponseVo queryDetails(ZxbStoreVo zxbStoreVo) {
//        //锁定交易
//        Fin fin = zxbStoreVo.getRecgrp().getRec();
//        whjService.lockFin(fin);
//        try {
        WhjDetailsResponseVo whjDetailsResponseVo = new WhjDetailsResponseVo();
        Zxbgrp zxbgrp = whjService.getZxbgrpByInr(zxbStoreVo.getRecgrp().getRec().getInr());
        zxbStoreVo.setRecgrp(zxbgrp);

        boolean mdfFlg = false;
        String ineRef = NULLSTR;
        String cusRef = NULLSTR;
        HashMap detailMap = new HashMap();
        if (!MdaUtils.isEmpty(zxbStoreVo.getRecgrp().getRec().getWgrref())) {
            zxbStoreVo.getRecpan().setButtyp("DET");
            whjService.setFsaHandType(zxbStoreVo.getRecgrp().getFsa(), zxbStoreVo.getRecpan().getButtyp());
            //**************调用接口部分***********#
            InnerRes<InnerResWhj> innerRes = setFsaAplret(zxbStoreVo.getRecpan().getButtyp(), zxbStoreVo);
            String code = innerRes.header.code;
            String description = innerRes.header.description;
            Detail detail = null;
            if (!WHJ_SUCCESS.equals(code)) {
                zxbStoreVo.getRecgrp().getFsa().setHinmsg("操作失败，失败原因：" + description);
                whjDetailsResponseVo.setErrcod("999999");
                whjDetailsResponseVo.setErrmsg(zxbStoreVo.getRecgrp().getFsa().getHinmsg());
                return whjDetailsResponseVo;
            } else {
//                LinkedHashMap map = (LinkedHashMap) innerRes.DATA.detail;
//                detail = Utils.toObject(Detail.class, JSONObject.toJSONString(map));
                if (innerRes.DATA.detail != null) {
                    detail = (Detail) Utils.getObjectByMap(Detail.class.getName(), (LinkedHashMap) innerRes.DATA.detail);
                }
                zxbStoreVo.getRecgrp().getFsa().setHinmsg("操作成功");
            }
            //*************END****************#
            disableAllZxbButton(zxbStoreVo);
//不知老国结为何要亮显            zxbStoreVo.getDisableSet().remove("sav");
            if (MdaUtils.compareTo(zxbStoreVo.getRecgrp().getRec().getWgrref(), detail.getSafeAssetId()) != 0) {
                zxbStoreVo.getRecgrp().getFsa().setHinmsg("获取到的外管返回信息不正确，请检查！");
                whjDetailsResponseVo.setErrcod("999999");
                whjDetailsResponseVo.setErrmsg(zxbStoreVo.getRecgrp().getFsa().getHinmsg());
                return whjDetailsResponseVo;
            }
            zxbStoreVo.getRecgrp().getFsa().setBlcsta(detail.getAssetState());
            zxbStoreVo.getRecgrp().getFsa().setSta(detail.getBizState());
            if (!MdaUtils.isEmpty(detail.getNoticeId()) && !MdaUtils.isEmpty(detail.getInvoiceCheck())) {
                zxbStoreVo.getRecgrp().getRec().setNoticeid(detail.getNoticeId());
                if (MdaUtils.compareTo(detail.getInvoiceCheck(), "0") == 0) {
                    zxbStoreVo.getRecgrp().getRec().setNoticeid("");
                }
            }

            if (detail != null && detail.getApply() != null && detail.getApply().getInvoices() != null) {
                Map<String, DetailInvoices> ineMap = new HashMap<>();
                Map<String, Manifests> cusMap = new HashMap<>();
                List<DetailInvoices> detailInvoicesList = detail.getApply().getInvoices();
                for (DetailInvoices detailInvoices : detailInvoicesList) {
                    String invref = detailInvoices.getInvoiceCode();
                    ineMap.put(invref, detailInvoices);
                    if (detailInvoices.getManifests() != null) {
                        List<Manifests> manifestsList = detailInvoices.getManifests();
                        for (Manifests manifests : manifestsList) {
                            String cusref = manifests.getManifestCode();
                            cusMap.put(invref + TAB + cusref, manifests);
                        }
                    }
                }

                List<Cus> cusList = zxbStoreVo.getRecgrp().getCuslst();
                for (Cus cus : cusList) {
                    String key = cus.getIneref() + TAB + cus.getCusref();
                    Manifests manifests = cusMap.get(key);
                    if (manifests != null) {
                        cus.setUsesta(manifests.getUseResult());
                        cus.setChksta(manifests.getCheckResult());
                        cus.setChkret(manifests.getChainResult());
                    }
                    if (!MdaUtils.isEmpty(cus.getUsesta()) || !MdaUtils.isEmpty(cus.getChksta()) || !MdaUtils.isEmpty(cus.getChkret())) {
                        mdfFlg = true;
                    }
                }

                List<Ine> ineList = zxbStoreVo.getRecgrp().getInelst();
                for (Ine ine : ineList) {
                    String key = ine.getIneref();
                    DetailInvoices detailInvoices = ineMap.get(key);
                    if (detailInvoices != null) {
                        ine.setShipid(detailInvoices.getShipmentId());
//                            ine.setAmt(detailInvoices.getInvoiceAmount());
                    }
                }
            }
            detailMap.putAll(this.printDetailInfo(detail, zxbStoreVo.getRecgrp().getRec().getFinloacur()));
        }
        if (mdfFlg) {
            for (Danlst danlst : zxbStoreVo.getRecpan().getDanlst()) {
                ineRef = danlst.getIneref();
                for (Cus cus : zxbStoreVo.getRecgrp().getCuslst()) {
                    if (MdaUtils.compareTo(ineRef, cus.getIneref()) == 0) {
                        danlst.setUsesta(cus.getUsesta());
                        danlst.setChksta(cus.getChksta());
                        danlst.setChkret(cus.getChkret());
                    }
                }
            }
        }
        //!详情查询输出两次日志
        // #######TODO 改写 LABEL与GOTO   ####    label detAnalyEnd
        whjService.setFsaHandType(zxbStoreVo.getRecgrp().getFsa(), zxbStoreVo.getRecpan().getButtyp());
        List<Cus> cusList = zxbStoreVo.getRecgrp().getCuslst();
        for (Cus cus : cusList) {
            cusMapper.updateByPrimaryKey(cus);
        }
        List<Ine> ineList = zxbStoreVo.getRecgrp().getInelst();
        for (Ine ine : ineList) {
            ineMapper.updateByPrimaryKey(ine);
        }
        finMapper.updateByPrimaryKey(zxbStoreVo.getRecgrp().getRec());
        initZxbpButton(zxbStoreVo);
        whjDetailsResponseVo.setErrcod(CommonContants.SUCCESS);
        whjDetailsResponseVo.setRecgrp(zxbStoreVo.getRecgrp());
        whjDetailsResponseVo.setRecpan(zxbStoreVo.getRecpan());
        whjDetailsResponseVo.setDisableSet(zxbStoreVo.getDisableSet());
        whjDetailsResponseVo.setDetailInfo(detailMap);
//            //释放锁
//            whjService.unLockFin(fin);
        return whjDetailsResponseVo;
//        } catch (Exception e) {
//            //发生异常需要释放锁定
//            whjService.unLockFin(fin);
//            throw e;
//        }
    }

    public void saveInelstAndCuslst(ZxbStoreVo zxbStoreVo, NoticeInvoices noticeInvoices) {
        List<Ine> inelst = zxbStoreVo.getRecgrp().getInelst();
        if (inelst != null) {
            inelst.forEach(ine -> {
                ine.setInr(counterService.dbCounterFormat("INE"));
                String ineref = ine.getIneref();
                if (!MdaUtils.isEmpty(ineref)) {
                    ine.setFininr(zxbStoreVo.getRecgrp().getRec().getInr());
                    ine.setCur(zxbStoreVo.getRecgrp().getRec().getRcvtotcur());
                    ine.setTyp("A");
                    ine.setSta("Y");

                    if (noticeInvoices != null && MdaUtils.compareTo(zxbStoreVo.getRecgrp().getRec().getNoticeid(), noticeInvoices.getNoticeId()) == 0 && noticeInvoices.getInvoices() != null) {
                        for (InvoicesForNi invoicesForNi : noticeInvoices.getInvoices()) {
                            if (MdaUtils.compareTo(ineref, invoicesForNi.getInvoiceCode()) == 0) {
                                ine.setLbteffdat(Utils.parseDate(invoicesForNi.getEffectiveDateOfLiability(), "yyyy-MM-dd"));
                                ine.setShipid(invoicesForNi.getShipmentId());
                            }
                        }
                    }
                }

                ineMapper.insert(ine);
            });
        }

        List<Cus> cuslst = zxbStoreVo.getRecgrp().getCuslst();
        if (cuslst != null) {
            cuslst.forEach(cus -> {
                cus.setInr(counterService.dbCounterFormat("CUS"));
                cus.setFininr(zxbStoreVo.getRecgrp().getRec().getInr());
                cus.setCur(zxbStoreVo.getRecgrp().getRec().getRcvtotcur());

                cusMapper.insert(cus);
            });
        }
    }

    private Map<String, Object> printDetailInfo(Detail detail, String payCur) {
        Map<String, Object> map = new HashMap();

        //融资类型
        String assettype = detail.getAssetType();
        String assettypetxt = "";
        switch (assettype) {
            case "COMMON":
                assettypetxt = "一般关单融资";
                break;
            case "SOUTH_BOUND":
                assettypetxt = "南向融资";
                break;
            case "SOLE_WINDOW":
                assettypetxt = "单一窗口融资";
                break;
            case "SINOSURE":
                assettypetxt = "中信保融资";
                break;
        }
        //资产状态
        String blcsta = detail.getAssetState();
        String blcstatxt = "";
        switch (blcsta) {
            case "APPLY":
                blcstatxt = "融资申请";
                break;
            case "CHECK":
                blcstatxt = "融资核验";
                break;
            case "PAY":
                blcstatxt = "放款登记";
                break;
            case "REC":
                blcstatxt = "还款登记";
                break;
            case "ACCEPT_AGREE":
                blcstatxt = "融资受理通过";
                break;
            case "ACCEPT_REFUSE":
                blcstatxt = "融资受理拒绝";
                break;
            case "AUDIT_AGREE":
                blcstatxt = "融资审核通过";
                break;
            case "AUDIT_REFUSE":
                blcstatxt = "融资审核拒绝";
                break;
            case "CANCEL_PAY":
                blcstatxt = "放款拒绝";
                break;
            case "CLEAR":
                blcstatxt = "还款结清";
                break;
            case "GIVE_UP_BY_BANK":
                blcstatxt = "银行废弃";
                break;
            case "CORRECT":
                blcstatxt = "放款冲正";
                break;
            case "GIVE_UP":
                blcstatxt = "超时废弃";
                break;
            case "STAGING":
                blcstatxt = "暂存";
                break;
        }
        //业务处理状态
        String bizstate = detail.getBizState();
        String bizstatetxt = "";
        switch (bizstate) {
            case "PENDING":
                bizstatetxt = "等待中";
                break;
            case "SUCCESS":
                bizstatetxt = "成功";
                break;
            case "FAIL":
                bizstatetxt = "失败";
                break;
            case "ERROR":
                bizstatetxt = "异常";
                break;
        }
        //本次核验金额占用情况
        String lockstate = detail.getLockState();
        String lockstatetxt = "";
        switch (lockstate) {
            case "LOCK":
                lockstatetxt = "占用";
                break;
            case "UNLOCK":
                lockstatetxt = "未占用";
                break;
        }
        //是否存在超额
        String overflg = "";
        if (!detail.getManifestQuotaOver()) {
            overflg = "否";
        } else {
            overflg = "是";
        }
        //是否存在核验异常的关单
        String exceptionflg = "";
        if (!detail.getManifestIsException()) {
            exceptionflg = "否";
        } else {
            exceptionflg = "是";
        }
        //是否开具承保通知书
        String invoiceCheck = detail.getInvoiceCheck();
        if (invoiceCheck == null) {
            invoiceCheck = "尚未开具";
        } else {
            switch (invoiceCheck) {
                case "1":
                    invoiceCheck = "已开具";
                    break;
                case "0":
                    invoiceCheck = "开具后删除";
                    break;
            }
        }
        //融资申请发起方
        String applytype = detail.getApplyType();
        String applytypetxt = "";
        switch (applytype) {
            case "ENT":
                applytypetxt = "企业发起";
                break;
            case "BANK":
                applytypetxt = "托管银行端发起的";
                break;
            case "BANK_DIRECT_LINK":
                applytypetxt = "直联融资";
                break;
        }
        //登记业务类型
        String registryType = detail.getApply().getRegistryType();
        String registryTypeTxt = "";
        switch (registryType) {
            case "SUPPLEMENT":
                registryTypeTxt = "补登记";
                break;
            case "NORMAL":
                registryTypeTxt = "新增";
                break;
        }
        //还款交易状态
        List<Recs> recs = detail.getRec().getRecs();
        Long recamount = 0L;
        String recdate = "";
        String txstate = "";
        String txstatetxt = "";
        if (recs != null) {
            for (Recs rec : recs) {
                if (rec.getRecAmount() > 0 && !MdaUtils.isEmpty(rec.getRecDate())) {
                    recamount = rec.getRecAmount();
                    recdate = rec.getRecDate();
                    txstate = rec.getTxState();
                    break;
                }
            }
        }
        switch (txstate) {
            case "SUCCESS":
                txstatetxt = "成功";
                break;
            case "FAIL":
                txstatetxt = "失败";
                break;
            case "ERROR":
                txstatetxt = "异常";
                break;
        }

        String cur = detail.getApply().getCurrency();
        if (MdaUtils.isEmpty(payCur)) {
            payCur = "CNY";
        }

        map.put("wgjrzh", detail.getSafeAssetId());
        map.put("rzityp", assettypetxt);
        map.put("zicsta", blcstatxt);
        map.put("ywcsta", bizstatetxt);
        map.put("heydiz", detail.getContractAddress());
        map.put("jezyqk", lockstatetxt);
        map.put("sfczce", overflg);
        map.put("sfycgd", exceptionflg);
        map.put("qiynam", detail.getApply().getApplyEntName());
        map.put("zzjgdm", detail.getApply().getApplyEntCode());
        map.put("gdhybz", cur);
        map.put("yszkje", Utils.changeWgAmt(detail.getApply().getAmount(), cur));
        map.put("jwfkqx", detail.getApply().getOverseasPaymentDeadline());
        map.put("bdh", detail.getApply().getPolicyNo());
        map.put("xybh", detail.getApply().getAgreementNo());
        map.put("sfkj", invoiceCheck);
        map.put("cbtzsbh", detail.getApply().getNoticeId());
        map.put("aplfqf", applytypetxt);
        map.put("slyhdm", detail.getApply().getAcceptBankCode());
        map.put("slyhmc", detail.getApply().getAcceptBankName());
        map.put("rzsqje", Utils.changeWgAmt(detail.getApply().getApplyAmount(), cur));
        map.put("djywlx", registryTypeTxt);
        map.put("fkjine", Utils.changeWgAmt(detail.getPay().getPayAmount(), payCur));
        map.put("fkleix", detail.getPay().getPayType());
        map.put("fkjilu", detail.getPay().getPayNote());
        map.put("fkriqi", detail.getPay().getPayDate());
        map.put("hkqixn", detail.getPay().getRecDate());
        map.put("hkjine", Utils.changeWgAmt(recamount, payCur));
        map.put("hkriqi", recdate);
        map.put("hkjyzt", txstatetxt);
        map.put("recs", recs);

        return map;
    }
}