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

import com.brilliance.mda.runtime.mda.util.Dates;
import com.brilliance.isc.bo.Bch;
import com.brilliance.isc.bo.Pty;
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 com.brilliance.isc.vo.component.bptbck.BptbckVo;
import com.brilliance.isc.vo.model.BpdgrpVo;
import com.brilliance.isc.vo.model.CbbVo;
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 BuildBptbckLetter {

    @Autowired
    private PtyCacheService ptyCacheService;

    @Autowired
    private CodetableService codetableService;

    public Map<String, Object> build(BptbckVo bptbckVo, DoceotVo doceot) {
        Map<String, Object> map = new HashMap<>();
        map.put("title", "融资业务还款通知书");
        BpdgrpVo bpdgrp = bptbckVo.getWrkbpdgrp();
        PtsptaVo ptsptaVo = bpdgrp.getFia();
        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", bpdgrp.getRec().getOwnref());
        map.put("finact", bpdgrp.getRec().getFinact());
        String fintyp = codetableService.getTblLabel(bpdgrp.getRec().getFintyp(), "BPDTYP");
        map.put("fintyp", fintyp);
        map.put("paydat", Utils.fmtChineseDate(Dates.today()));
        String samt = "";
        CbbVo maxcbb = bpdgrp.getCbs().getMax();
        if (maxcbb != null) {
            samt = maxcbb.getCur() + " " + Utils.fmtAmount(maxcbb.getAmt(), maxcbb.getCur());
        }
        map.put("samt", samt);
        String paycur = bptbckVo.getWrkfinmod().getCbs().getCbe().getCur();
        BigDecimal payamt = bptbckVo.getWrkfinmod().getCbs().getCbe().getAmt();
        map.put("pamt", Utils.fmtAmount(payamt, paycur));
        String actrat = "";
        if (bpdgrp.getRec().getTolrat() != null) {
            actrat = bpdgrp.getRec().getTolrat().toPlainString() + "%";
        }
        map.put("actrat", actrat);
        BigDecimal intamt = bptbckVo.getWrkfinmod().getCbs().getTotint();
        map.put("pint", Utils.fmtAmount(intamt, paycur));
        map.put("opndat", Utils.fmtChineseDate(bpdgrp.getRec().getOpndat()));
        map.put("matdat", Utils.fmtChineseDate(bpdgrp.getRec().getMatdat()));
        String status = codetableService.getTblLabel(bpdgrp.getRec().getOvdflg(), "OVDFLG");
        map.put("status", status);
        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;
    }
}
