package com.brilliance.isc.doc.handle.impl.trtcre;

import com.brilliance.mda.runtime.mda.util.Dates;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import com.brilliance.isc.bo.Bch;
import com.brilliance.isc.bo.Cbb;
import com.brilliance.isc.bo.Pty;
import com.brilliance.isc.bo.model.Trdgrp;
import com.brilliance.isc.bo.model.Trtcre;
import com.brilliance.isc.common.cache.pty.PtyCacheService;
import com.brilliance.isc.common.codetable.service.CodetableService;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.trndoc.vo.DoceotVo;
import com.brilliance.isc.common.util.StringUtils;
import com.brilliance.isc.common.util.Utils;
import com.brilliance.isc.common.vo.PtsptaVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Component
public class BuildTrtcreLetter {

    @Autowired
    private PtyCacheService ptyCacheService;

    @Autowired
    private CodetableService codetableService;

    public Map<String, Object> build(Trtcre trtcre, DoceotVo doceot) {
        Map<String, Object> map = new HashMap<>();
        //String transName = SettleContext.getTransName();
        String transName = SettleContext.getDirectTransName();
        String title = "";
        if ("BDTSET".equals(transName)) {
            title = "国内信用证融资业务放款通知书";
        } else if ("BCTSET-BRTSET-CPTOPN".indexOf(transName) > -1) {
            title = "融资业务放款通知书";
        }
        map.put("title", title);
        Trdgrp trdgrp = trtcre.getTrdgrp();
        PtsptaVo ptsptaVo = trdgrp.getFip();
        String nam = "";
        Pty pty = ptyCacheService.getPtyById(ptsptaVo.getPtyinr());
        if (pty != null) {
            nam = pty.getNam1();
        }
        map.put("nam", nam);
        String txt1 = "应贵公司的申请,根据贵公司与我行签订的相关融资协议,我行已办理了贵司所申请的融资业务,具体细节如下:";
        map.put("txt1", txt1);
        map.put("ownref", trdgrp.getRec().getOwnref());
        map.put("opndat", Utils.fmtChineseDate(trdgrp.getRec().getStttendat()));
        String fintyp = codetableService.getTblLabel(trdgrp.getRec().getFintyp(), "TRDTYP");
        map.put("fintyp", fintyp);
        map.put("matdat", Utils.fmtChineseDate(trdgrp.getRec().getMatdat()));
        String samt = "";
        Cbb maxcbb = trdgrp.getCbs().getMax();
        if (maxcbb != null) {
            samt = maxcbb.getCur() + " " + Utils.fmtAmount(maxcbb.getAmt(), maxcbb.getCur());
        }
        map.put("samt", samt);
        long tenday = trdgrp.getRec().getTenday();
        map.put("tenday", tenday + "天");
        String actrat = "";
        if (trdgrp.getRec().getActrat() != null) {
            actrat = trdgrp.getRec().getActrat().toPlainString() + "%";
        }
        map.put("actrat", actrat);
        String finact = trdgrp.getRec().getFinact();
        map.put("finact", finact);
        String txt2 = "我行已将上述融资款项";
        map.put("txt2", txt2);
        String txt3 = "";
        if( MdaUtils.compareTo(trtcre.getFinanc().getRzkx1(),"X") == 0 ) {
            txt3 = "■ 贷记贵司指定账户(帐号: " + trtcre.getFinanc().getAcc() + ")";
        } else {
            txt3 = "□ 贷记贵司指定账户(帐号:                           )";
        }
        map.put("txt3", txt3.replaceAll(" ", "&nbsp;"));
        String txt4 = "";
        if( MdaUtils.compareTo(trtcre.getFinanc().getRzkx2(),"X") == 0 ) {
            if(!MdaUtils.isEmpty(trtcre.getFinanc().getRef2())) {
                txt4 = "■ 支付进口信用证项下款项(业务编号: " + trtcre.getFinanc().getRef2() + ")";
            } else {
                txt4 = "■ 支付进口信用证项下款项(业务编号:                    )";
            }
        } else {
            txt4 = "□ 支付进口信用证项下款项(业务编号:                    )";
        }
        map.put("txt4", txt4.replaceAll(" ", "&nbsp;"));
        String txt5 = "";
        if(MdaUtils.compareTo(trtcre.getFinanc().getRzkx3(),"X") == 0) {
            if(!MdaUtils.isEmpty(trtcre.getFinanc().getRef3())) {
                txt5 = "■ 支付进口代收项下款项(业务编号: " + trtcre.getFinanc().getRef3() + ")";
            } else {
                txt5 = "■ 支付进口代收项下款项(业务编号:                    )";
            }
        } else {
            txt5 = "□ 支付进口代收项下款项(业务编号:                    )";
        }
        map.put("txt5", txt5.replaceAll(" ", "&nbsp;"));
        String txt6 = "";
        if(MdaUtils.compareTo(trtcre.getFinanc().getRzkx4(),"X") == 0) {
            if(!MdaUtils.isEmpty(trtcre.getFinanc().getRef4())) {
                txt6 = "■ 支付汇出汇款项下款项(业务编号: " + trtcre.getFinanc().getRef4() + ")";
            } else {
                txt6 = "■ 支付汇出汇款项下款项(业务编号:                    )";
            }
        } else {
            txt6 = "□ 支付汇出汇款项下款项(业务编号:                    )";
        }
        map.put("txt6", txt6.replaceAll(" ", "&nbsp;"));
        String txt14 = "";
        if( MdaUtils.compareTo(trtcre.getFinanc().getRzkx5(),"X") == 0 ) {
            if(!MdaUtils.isEmpty(trtcre.getFinanc().getRef5()) ) {
                txt14 = "■ 支付国内信用证项下款项(业务编号: " + trtcre.getFinanc().getRef5() + ")";
            } else {
                txt14 = "■ 支付国内信用证项下款项(业务编号:                    )";
            }
        } else {
            txt14 = "□ 支付国内信用证项下款项(业务编号:                    )";
        }
        map.put("txt14", txt14.replaceAll(" ", "&nbsp;"));
        if ("BDTSET".equals(transName)) {
            map.put("dflg", "D");
        } else {
            map.put("dflg", "");
        }
        String txt12 = "";
        BigDecimal interest = trtcre.getFinmod().getIntamt();
        if (interest != null) {
            txt12 = interest.toPlainString();
            String inttyp = trtcre.getFinmod().getInttyp();
            if(MdaUtils.compareTo(inttyp,"0") == 0) {
                txt12 = "预扣融资利息: " + interest.toPlainString();
            }
        }
        map.put("txt12", txt12);
        String addinf = "";
        if (StringUtils.isNotEmpty(doceot.getAddtxt())) {
            addinf = doceot.getAddtxt().replaceAll("\n", "<br/>").replaceAll(" ", "&nbsp;");
        }
        map.put("addinf", addinf);
        String bchname = "";
        Bch bch = SettleContext.getUserSession().getBch();
        if (bch != null) {
            bchname = bch.getBchname();
        }
        map.put("footname", bchname);
        map.put("footdate", Utils.fmtChineseDate(Dates.today()));
        return map;
    }
}
