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

import com.brilliance.isc.bo.Cus;
import com.brilliance.isc.bo.Danlst;
import com.brilliance.isc.bo.Fin;
import com.brilliance.isc.bo.Fsa;
import com.brilliance.isc.bo.model.Blcgrp;
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.WhjBlcInterService;
import com.brilliance.isc.common.whj.service.WhjService;
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.export.DirectPayContent;
import com.brilliance.isc.esb.model.whj.content.export.RecContent;
import com.brilliance.isc.esb.model.whj.content.export.RecItem;
import com.brilliance.isc.esb.model.whj.content.export.RecOnceContent;
import com.brilliance.isc.esb.model.whj.res.export.Detail;
import com.brilliance.isc.esb.model.whj.res.export.Invoices;
import com.brilliance.isc.esb.model.whj.res.export.Manifests;
import com.brilliance.isc.esb.model.whj.res.export.Recs;
import com.brilliance.isc.esb.model.whj.res.sinosure.BankApply;
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.vo.finance.BlcStoreVo;
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.util.*;

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

@Component
public class WhjBlcToolComponent {

    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_FINCHK = "finchk";  //关单核验按钮
    public static final String BUTTON_RFU = "rfuacp";        //拒绝受理按钮
    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_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 WhjBlcInterService whjBlcInterService;

    @Autowired(required = false)
    private InnerWhjServer innerWhjServer;

    @Autowired
    CounterService counterService;

    @Autowired
    private FinMapper finMapper;

    @Autowired
    private FsaMapper fsaMapper;

    @Autowired
    private CusMapper cusMapper;

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

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

    public InnerRes<InnerResWhj> setFsaAplret1(String typ, BlcStoreVo blcStoreVo) {
        InnerRes<InnerResWhj> innerRes = null;
        switch (typ) {
            case "APL":
                innerRes = whjBlcInterService.bankApply(blcStoreVo);
                break;
            case "CHK":
                innerRes = whjBlcInterService.check(blcStoreVo);
                break;
            case "RFU":
                innerRes = whjBlcInterService.accept(blcStoreVo);
                break;
            case "REG":
                DirectPayContent directPayContent = new DirectPayContent();
                directPayContent.setAcceptAmount(Utils.fmtAmtToWgLong(blcStoreVo.getRecgrp().getRec().getLoaamt()));
                directPayContent.setAcceptCurrency(blcStoreVo.getRecgrp().getRec().getFinloacur());
                directPayContent.setDayLimit(Integer.parseInt(blcStoreVo.getRecgrp().getRec().getLoalimday()));
                directPayContent.setNote(Strings.converttoXMLStringPublic(blcStoreVo.getRecgrp().getRec().getRemark()));
                directPayContent.setPayAmount(Utils.fmtAmtToWgLong(blcStoreVo.getRecgrp().getRec().getFinloaamt()));
                directPayContent.setPayDate(Utils.fmtDate(blcStoreVo.getRecgrp().getRec().getLoadat(), "yyyy-MM-dd"));
                directPayContent.setRecDate(Utils.fmtDate(blcStoreVo.getRecgrp().getRec().getPrcrepdat(), "yyyy-MM-dd"));
                directPayContent.setSafeAssetId(blcStoreVo.getRecgrp().getRec().getWgrref());
                directPayContent.setReason(Strings.converttoXMLStringPublic(blcStoreVo.getRecgrp().getRec().getAbarea()));

                ReqMsgData<DirectPayContent> reqMsgData = new ReqMsgData<>(directPayContent);
                reqMsgData.method = "/v1/asset/direct-pay";
                reqMsgData.service = "EXPORT";
                reqMsgData.bankSafeCode = "";

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

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

                innerRes = innerWhjServer.callWhj(innerReqWhjInnerReq);
                break;
            case "RCV":
                if (MdaUtils.compareTo(blcStoreVo.getRecgrp().getRec().getRcvamt(), blcStoreVo.getRecgrp().getRec().getFinloaamt()) < 0) {
                    RecContent recContent = new RecContent();
                    recContent.setSafeAssetId(blcStoreVo.getRecgrp().getRec().getWgrref());
                    recContent.setRecs(new ArrayList<RecItem>());
                    RecItem rec = new RecItem();
                    rec.setRecAmount(Utils.fmtAmtToWgLong(blcStoreVo.getRecgrp().getRec().getRcvamt()));
                    rec.setRecDate(Utils.fmtDate(blcStoreVo.getRecgrp().getRec().getFacrcvdat(), "yyyy-MM-dd"));
                    recContent.getRecs().add(rec);

                    ReqMsgData<RecContent> reqMsgData1 = new ReqMsgData<>(recContent);
                    reqMsgData1.method = "/v1/asset/rec";
                    reqMsgData1.service = "EXPORT";
                    reqMsgData1.bankSafeCode = "";

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

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

                    innerRes = innerWhjServer.callWhj(innerReqWhjInnerReq1);
                } else {
                    RecOnceContent recOnceContent = new RecOnceContent();
                    recOnceContent.setSafeAssetId(blcStoreVo.getRecgrp().getRec().getWgrref());
                    recOnceContent.setRecDate(Utils.fmtDate(blcStoreVo.getRecgrp().getRec().getFacrcvdat(), "yyyy-MM-dd"));

                    ReqMsgData<RecOnceContent> reqMsgData2 = new ReqMsgData<>(recOnceContent);
                    reqMsgData2.method = "/v1/asset/recOnce";
                    reqMsgData2.service = "EXPORT";
                    reqMsgData2.bankSafeCode = "";

                    InnerReqWhj innerReqWhj2 = new InnerReqWhj();
                    innerReqWhj2.serviceType = "EXPORT";
                    innerReqWhj2.reqMsgData = reqMsgData2;

                    InnerReq<InnerReqWhj> innerReqWhjInnerReq2 = new InnerReq<>(innerReqWhj2);

                    innerRes = innerWhjServer.callWhj(innerReqWhjInnerReq2);
                }
                break;
            case "RTY":
                innerRes = whjBlcInterService.retry(blcStoreVo);
                break;
            case "UND":
                innerRes = whjBlcInterService.correct(blcStoreVo);
                break;
            case "ABA":
                innerRes = whjBlcInterService.abandon(blcStoreVo);
                break;
            case "DET":
                innerRes = whjBlcInterService.detail(blcStoreVo);
                break;
            default:
        }
        return innerRes;
    }

    public boolean buttonHandle(String typ, BlcStoreVo blcStoreVo) {
//        //锁定交易
//        Fin fin = blcStoreVo.getRecgrp().getRec();
//        whjService.lockFin(fin);
//        try {
        if (MdaUtils.compareTo(typ, "RTY") != 0) {
            blcStoreVo.getRecgrp().getFsa().setBnkaplsta(typ);
            blcStoreVo.getRecgrp().getFsa().setAplret("W");
        }
        InnerRes<InnerResWhj> innerRes = setFsaAplret(typ, blcStoreVo);
        boolean result = handleResult(blcStoreVo.getRecgrp().getRec(), blcStoreVo.getRecgrp().getFsa(), typ, innerRes);
        whjService.setFsaHandType(blcStoreVo.getRecgrp().getFsa(), typ);
        finMapper.updateByPrimaryKey(blcStoreVo.getRecgrp().getRec());
//            //释放锁
//            whjService.unLockFin(fin);
        return result;
//        } catch (Exception e) {
//            //发生异常需要释放锁定
//            whjService.unLockFin(fin);
//            throw e;
//        }
    }

    public InnerRes<InnerResWhj> setFsaAplret(String typ, BlcStoreVo blcStoreVo) {
        InnerRes<InnerResWhj> innerRes = null;
        switch (typ) {
            case "APL":
                innerRes = whjBlcInterService.bankApply(blcStoreVo);
                break;
            case "CHK":
                innerRes = whjBlcInterService.check(blcStoreVo);
                break;
            case "RFU":
                innerRes = whjBlcInterService.accept(blcStoreVo);
                break;
            case "REG":
                innerRes = whjBlcInterService.directPay(blcStoreVo);
                break;
            case "RCV":
                if (MdaUtils.compareTo(blcStoreVo.getRecgrp().getRec().getRcvamt(), blcStoreVo.getRecgrp().getRec().getFinloaamt()) < 0) {
                    innerRes = whjBlcInterService.rec(blcStoreVo);
                } else {
                    innerRes = whjBlcInterService.recOnce(blcStoreVo);
                }
                break;
            case "RTY":
                innerRes = whjBlcInterService.retry(blcStoreVo);
                break;
            case "UND":
                innerRes = whjBlcInterService.correct(blcStoreVo);
                break;
            case "ABA":
                innerRes = whjBlcInterService.abandon(blcStoreVo);
                break;
            case "DET":
                innerRes = whjBlcInterService.detail(blcStoreVo);
                break;
            default:
        }
        return innerRes;
    }

    public boolean handleResult(Fin fin, Fsa fsa, String typ, InnerRes<InnerResWhj> innerRes) {
        String code = innerRes.header.code;
        String description = innerRes.header.description;
        if (WHJ_SUCCESS.equals(code)) {
            if (innerRes.DATA.detail != null) {
                BankApply bankApply = (BankApply) Utils.getObjectByMap(BankApply.class.getName(), (LinkedHashMap) innerRes.DATA.detail);
                if (!MdaUtils.isEmpty(bankApply.getSafeAssetId())) {
                    fin.setWgrref(bankApply.getSafeAssetId());
                }
            }
            if ("RTY".equals(typ)) {
                fsa.setHinmsg("操作成功");
            } else {
                if ("RCV".equals(typ)) {
                    fsa.setAplret("D");
                    fsa.setHinmsg("操作成功，还款金额：" + fin.getRcvcur() + "：" + MdaUtils.toString(fin.getRcvamt()));
                } else {
                    fsa.setAplret("D");
                    fsa.setHinmsg("操作成功");
                }
            }
            return true;
        } else {
            if ("RTY".equals(typ)) {
                fsa.setHinmsg("操作失败，失败原因：" + description);
            }
            fsa.setAplret("O");
            fsa.setHinmsg("操作失败，失败原因：" + description);

            return false;
        }
    }


    public void initBlcpButton(BlcStoreVo blcStoreVo) {
        if (!MdaUtils.isEmpty(blcStoreVo.getRecgrp().getFsa().getInr())) {
            if (!MdaUtils.isEmpty(blcStoreVo.getRecgrp().getFsa().getDelflg())) {
                return;
            }
        }
        String blcsta = blcStoreVo.getRecgrp().getFsa().getBlcsta();
        String bnkaplsta = blcStoreVo.getRecgrp().getFsa().getBnkaplsta();
        String sta = blcStoreVo.getRecgrp().getFsa().getSta();
        String aplret = blcStoreVo.getRecgrp().getFsa().getAplret();

        Map<String, String> res = setCheckResult(blcStoreVo);
        String chksta = res.get("chksta");
        String chkret = res.get("chkret");

        //绑定按钮
        if ("CHK".equals(bnkaplsta) && "CHECK".equals(blcsta) && ("SUCCESS".equals(sta) && "D".equals(aplret) && MdaUtils.isEmpty(blcStoreVo.getRecgrp().getRec().getRelref()) && "Y".equals(chkret) && ("C".equals(chksta) || "D".equals(chksta)))) {
            blcStoreVo.getDisableSet().remove(BUTTON_BUND);
        }

        //解绑定
        if (!MdaUtils.isEmpty(blcStoreVo.getRecgrp().getRec().getRelref())) {
            Fsa fsa = new Fsa();
            fsa.setFininr(blcStoreVo.getRecgrp().getFsa().getFininr());
            fsa.setBlcsta("PAY");
            fsa.setSta("SUCCESS");
//            fsa.setIsvalid("Y");
            List<Fsa> fsalst = fsaMapper.selectByConditions(fsa);

            if (fsalst == null || fsalst.size() == 0) {
                blcStoreVo.getDisableSet().remove(BUTTON_UNBUND);
            }
        }

        //融资申请
        if ("WAI".equals(bnkaplsta) || ("APL".equals(bnkaplsta) && "O".equals(aplret))) {
            blcStoreVo.getDisableSet().remove(BUTTON_FINAPL);
        }

        //关单核验按钮
        if ("APPLY".equals(blcsta) && "SUCCESS".equals(sta) && ("APL".equals(bnkaplsta) || "CHG".equals(bnkaplsta)) && "D".equals(aplret)) {
            blcStoreVo.getDisableSet().remove(BUTTON_FINCHK);
        }
        if ("CHK".equals(bnkaplsta) && "O".equals(aplret)) {
            blcStoreVo.getDisableSet().remove(BUTTON_FINCHK);
        }

        //拒绝受理按钮
        if (("SUCCESS".equals(sta) && ("APPLY".equals(blcsta) || "CHECK".equals(blcsta))) && Strings.pos("-APL-CHK-", bnkaplsta) > 0) {
            blcStoreVo.getDisableSet().remove(BUTTON_RFU);
        }
        if ("RFU".equals(bnkaplsta) && "O".equals(aplret)) {
            blcStoreVo.getDisableSet().remove(BUTTON_RFU);
        }

        //放款登记按钮
        if ("CHECK".equals(blcsta) && "SUCCESS".equals(sta) && "CHK".equals(bnkaplsta) && "D".equals(aplret) && ("C".equals(chksta) || "D".equals(chksta)) && MdaUtils.compareTo(blcStoreVo.getRecgrp().getRec().getRelref(), " ") != 0) {
            blcStoreVo.getDisableSet().remove(BUTTON_LOAREG);
        }
        if ("REG".equals(bnkaplsta) && "O".equals(aplret)) {
            blcStoreVo.getDisableSet().remove(BUTTON_LOAREG);
        }

        //还款登记
        if ("SUCCESS".equals(sta) && (("PAY".equals(blcsta) || "REC".equals(blcsta)) && ("RCV".equals(bnkaplsta) || "REG".equals(bnkaplsta))) && "D".equals(aplret)) {
            blcStoreVo.getDisableSet().remove(BUTTON_RCVREG);
        }
        if ("RCV".equals(bnkaplsta) && "O".equals(aplret)) {
            blcStoreVo.getDisableSet().remove(BUTTON_RCVREG);
        }

        //申请废弃按钮
        if ("SUCCESS".equals(sta) && ("APPLY".equals(blcsta) || "CHECK".equals(blcsta)) && Strings.pos("-APL-CHK-CHG-", bnkaplsta) > 0) {
            blcStoreVo.getDisableSet().remove(BUTTON_APLDEL);
        }
        if ("ABA".equals(bnkaplsta) && "O".equals(aplret)) {
            blcStoreVo.getDisableSet().remove(BUTTON_APLDEL);
        }

        //放款冲正按钮
        if (("PAY".equals(blcsta) || "REC".equals(blcsta) || "CLEAR".equals(blcsta)) && "D".equals(aplret)) {
            blcStoreVo.getDisableSet().remove(BUTTON_LOAUND);
        }
        if ("UND".equals(bnkaplsta) && "O".equals(aplret)) {
            blcStoreVo.getDisableSet().remove(BUTTON_LOAUND);
        }

        //上链重试按钮
        if ("FAIL".equals(sta) && MdaUtils.compareTo(blcsta, "APPLY") != 0) {
            blcStoreVo.getDisableSet().remove(BUTTON_RETRY);
        }

        //详情查询
        if (!MdaUtils.isEmpty(blcStoreVo.getRecgrp().getRec().getWgrref())) {
            blcStoreVo.getDisableSet().remove(BUTTON_DETSRC);
        }

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

        //修改、废弃
        if ("WAI".equals(bnkaplsta) || "BRK".equals(bnkaplsta) || MdaUtils.isEmpty(blcStoreVo.getRecgrp().getRec().getWgrref())) {
            blcStoreVo.getDisableSet().remove(BUTTON_MOD);
            blcStoreVo.getDisableSet().remove(BUTTON_DEL);
        }
        if (("APL".equals(bnkaplsta) && "O".equals(aplret)) || ("APL".equals(bnkaplsta) && "D".equals(aplret) && "APPLY".equals(blcsta) && MdaUtils.compareTo(sta, "SUCCESS") != 0)) {
            blcStoreVo.getDisableSet().remove(BUTTON_DEL);
        }
    }


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

    public void disableAllButton(BlcStoreVo blcStoreVo) {
        blcStoreVo.getDisableSet().add(BUTTON_SAV);
        blcStoreVo.getDisableSet().add(BUTTON_FINAPL);
        blcStoreVo.getDisableSet().add(BUTTON_FINCHK);
        blcStoreVo.getDisableSet().add(BUTTON_RFU);
        blcStoreVo.getDisableSet().add(BUTTON_LOAREG);
        blcStoreVo.getDisableSet().add(BUTTON_RCVREG);
        blcStoreVo.getDisableSet().add(BUTTON_APLDEL);
        blcStoreVo.getDisableSet().add(BUTTON_LOAUND);
        blcStoreVo.getDisableSet().add(BUTTON_BUND);
        blcStoreVo.getDisableSet().add(BUTTON_UNBUND);
        blcStoreVo.getDisableSet().add(BUTTON_STAMDF);
        blcStoreVo.getDisableSet().add(BUTTON_MOD);
        blcStoreVo.getDisableSet().add(BUTTON_DEL);

    }

    public WhjDetailsResponseVo queryDetails(BlcStoreVo blcStoreVo) {
//        //锁定交易
//        Fin fin = blcStoreVo.getRecgrp().getRec();
//        whjService.lockFin(fin);
//        try {
        WhjDetailsResponseVo whjDetailsResponseVo = new WhjDetailsResponseVo();
        Blcgrp blcgrp = whjService.getBlcgrpByInr(blcStoreVo.getRecgrp().getRec().getInr());
        blcStoreVo.setRecgrp(blcgrp);
        boolean mdfFlg = false;
        String ineRef = NULLSTR;
        String cusRef = NULLSTR;
        HashMap detailMap = new HashMap();
        if (!MdaUtils.isEmpty(blcStoreVo.getRecgrp().getRec().getWgrref())) {
            blcStoreVo.getRecpan().setButtyp("DET");
            whjService.setFsaHandType(blcStoreVo.getRecgrp().getFsa(), blcStoreVo.getRecpan().getButtyp());
            //**************调用接口部分***********#
            InnerRes<InnerResWhj> innerRes = setFsaAplret(blcStoreVo.getRecpan().getButtyp(), blcStoreVo);
            String code = innerRes.header.code;
            String description = innerRes.header.description;
            LinkedHashMap map = (LinkedHashMap) innerRes.DATA.detail;
            Detail detail = null;
            if (!WHJ_SUCCESS.equals(code)) {
                blcStoreVo.getRecgrp().getFsa().setHinmsg("操作失败，失败原因：" + description);
                whjDetailsResponseVo.setErrcod("999999");
                whjDetailsResponseVo.setErrmsg(blcStoreVo.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);
                }
                blcStoreVo.getRecgrp().getFsa().setHinmsg("操作成功");
            }
            //*************END****************#
            disableAllButton(blcStoreVo);
            blcStoreVo.getDisableSet().remove("sav");

            if (MdaUtils.compareTo(blcStoreVo.getRecgrp().getRec().getWgrref(), detail.getSafeAssetId()) != 0) {
                blcStoreVo.getRecgrp().getFsa().setHinmsg("获取到的外管返回信息不正确，请检查！");
                whjDetailsResponseVo.setErrcod("999999");
                whjDetailsResponseVo.setErrmsg(blcStoreVo.getRecgrp().getFsa().getHinmsg());
                return whjDetailsResponseVo;
            }
            blcStoreVo.getRecgrp().getFsa().setBlcsta(detail.getAssetState());
            blcStoreVo.getRecgrp().getFsa().setSta(detail.getBizState());

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

                List<Cus> cusList = blcStoreVo.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;
                    }
                }

            }

            detailMap.putAll(this.printDetailInfo(detail));
        }
        if (mdfFlg) {
            for (Danlst danlst : blcStoreVo.getRecpan().getDanlst()) {
                ineRef = danlst.getIneref();
                for (Cus cus : blcStoreVo.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(blcStoreVo.getRecgrp().getFsa(), blcStoreVo.getRecpan().getButtyp());
        List<Cus> cusList = blcStoreVo.getRecgrp().getCuslst();
        for (Cus cus : cusList) {
            cusMapper.updateByPrimaryKey(cus);
        }
        finMapper.updateByPrimaryKey(blcStoreVo.getRecgrp().getRec());

        whjDetailsResponseVo.setErrcod(CommonContants.SUCCESS);
        whjDetailsResponseVo.setBlcgrp(blcStoreVo.getRecgrp());
        whjDetailsResponseVo.setBlcp(blcStoreVo.getRecpan());
        whjDetailsResponseVo.setDetailInfo(detailMap);

//            //释放锁
//            whjService.unLockFin(fin);
        return whjDetailsResponseVo;
//        } catch (Exception e) {
//            //发生异常需要释放锁定
//            whjService.unLockFin(fin);
//            throw e;
//        }
    }


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

        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;
        }

        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 cur = detail.getApply().getCurrency();
        String acceptcurrency = detail.getAudit().getAcceptCurrency();
        List<String> ydlb = detail.getApply().getExportWayBills();

        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 lockstate = detail.getLockState();
        String lockstatetxt = "";
        switch (lockstate) {
            case "LOCK":
                lockstatetxt = "占用";
                break;
            case "UNLOCK":
                lockstatetxt = "未占用";
                break;
        }

        String applytype = detail.getApplyType();
        String applytypetxt = "";
        switch (applytype) {
            case "ENT":
                applytypetxt = "企业发起";
                break;
            case "BANK":
                applytypetxt = "托管银行端发起的";
                break;
            case "BANK_DIRECT_LINK":
                applytypetxt = "直联融资";
                break;
        }

        String overflg = "";
        if (!detail.getManifestQuotaOver()) {
            overflg = "否";
        } else {
            overflg = "是";
        }

        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 exceptionflg = "";
        if (!detail.getManifestIsException()) {
            exceptionflg = "否";
        } else {
            exceptionflg = "是";
        }

        map.put("wgjrzh", detail.getSafeAssetId());
        map.put("zicsta", blcstatxt);
        map.put("ywcsta", bizstatetxt);
        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("hthkrq", detail.getApply().getContractEndDate());
        map.put("rzsqje", Utils.changeWgAmt(detail.getApply().getApplyAmount(), cur));
        map.put("slyhdm", detail.getApply().getAcceptBankCode());
        map.put("slyhmc", detail.getApply().getAcceptBankName());
        map.put("ydlist", ydlb);
        map.put("sheqix", detail.getAudit().getDayLimit());
        map.put("rzicur", acceptcurrency);
        map.put("rziamt", Utils.changeWgAmt(detail.getAudit().getAcceptAmount(), acceptcurrency));
        map.put("shjjyy", detail.getAudit().getAuditReason());
        map.put("heydiz", detail.getContractAddress());
        map.put("fkjine", Utils.changeWgAmt(detail.getPay().getPayAmount(), acceptcurrency));
        map.put("fkleix", detail.getPay().getPayType());
        map.put("fkjilu", detail.getPay().getPayNote());
        map.put("fkjjyy", detail.getPay().getPayReason());
        map.put("hkqixn", detail.getPay().getRecDate());
        map.put("fkriqi", detail.getPay().getPayDate());
        map.put("sltgyy", detail.getAccept().getAcceptReason());
        map.put("recamount", recamount);
        map.put("hkjine", Utils.changeWgAmt(recamount, acceptcurrency));
        map.put("hkriqi", recdate);
        map.put("hkjyzt", txstatetxt);
        map.put("jezyqk", lockstatetxt);
        map.put("aplfqf", applytypetxt);
        map.put("sfczce", overflg);
        map.put("rzityp", assettypetxt);
        map.put("sfycgd", exceptionflg);
        map.put("file", detail.getAttachment());
        map.put("xitdat", detail.getSystemDate());

        return map;

    }

}


