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

import com.brilliance.mda.runtime.mda.util.Dates;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import com.brilliance.mda.runtime.mda.util.Strings;
import com.brilliance.isc.bo.Bch;
import com.brilliance.isc.bo.model.Dftcre;
import com.brilliance.isc.common.cbsmod.service.XrtmodService;
import com.brilliance.isc.common.codetable.service.CodetableService;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.util.Utils;
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 BuildDftcreLetter {

    @Autowired
    private CodetableService codetableService;

    @Autowired
    private XrtmodService xrtmodService;

    public Map<String, Object> build(Dftcre dftcre) {
        Map<String, Object> map = new HashMap<>();
        Bch bch = SettleContext.getUserSession().getBch();
        String bchname = "";
        String branch = "";
        if (bch != null) {
            bchname = bch.getBchname();
            branch = bch.getBranch();
        }
        map.put("bchnam", bchname);
        map.put("bchref", branch);
        map.put("ownref", dftcre.getDfdgrp().getRec().getOwnref());
        map.put("bankcn", dftcre.getDfdgrp().getRec().getBankcn());
        map.put("bankno", dftcre.getDfdgrp().getRec().getBankno());
        String acctyp = "";
        String trncod = "";
        if( MdaUtils.compareTo(dftcre.getDfdgrp().getRec().getDfuflg(),"1") == 0 ) {
            acctyp = "境内银行代付（他代本）";
            trncod = "1431";
        } else {
            acctyp = "境外银行代付（他代本）";
            trncod = "2203";
            String extkey = dftcre.getDfdgrp().getDff().getExtkey();
            if(!MdaUtils.isEmpty(extkey)) {
                /**
                 $bank = Mid( $bankno, 1, 11 )
                 #判断香港光大（EVERHKHHXXX）判断首尔光大（EVERKRSEXXX）
                 if $bank == "EVERHKHHXXX" or $bank == "EVERKRSEXXX" then

                 **/
                String bank = Strings.mid(extkey,1,4);
                String bank1 = Strings.mid(extkey,5,2);
                //光银欧洲（swift code:EVERLULX)开展的海外代付业务记账从现在的业务代号"2719境内外系统内其他往来"科目调整为"2203境外同业代付（他代本）"
                String bank2 = Strings.mid(extkey,1,8);
                //增加判断卢森堡分行（EVERLULLXXX）
                if( MdaUtils.compareTo(bank,"EVER") == 0 && MdaUtils.compareTo(bank1,"CN") != 0 ) {
                    if( MdaUtils.compareTo(bank2,"EVERLULX") == 0 ) {
//                        acctyp = "科目代码：352807";
                        trncod = "2203";
                    } else {
//                        acctyp = "科目代码：352807";
                        trncod = "2719";
                    }
                }
            }
        }
        map.put("acctyp", acctyp);
        map.put("trncod", trncod);
        String cur = dftcre.getDfdgrp().getCbs().getMax().getCur();
        map.put("cur", cur);
        String intprd = codetableService.getTblLabel(dftcre.getDfdgrp().getRec().getIntprd(),"INTPRD");
        map.put("intprd", intprd);
        if (dftcre.getDfdgrp().getRec().getActrat() != null) {
            map.put("actrat", dftcre.getDfdgrp().getRec().getActrat().toPlainString() + "%");
        }
        String flttyp = codetableService.getTblLabel(dftcre.getDfdgrp().getRec().getFlttyp(),"FLTTYP");
        map.put("flttyp", flttyp);
        map.put("rattyp", "同业活期(154)");
        map.put("valdat", Utils.fmtChineseDate(dftcre.getDfdgrp().getRec().getStttendat()));
        map.put("matdat", Utils.fmtChineseDate(dftcre.getDfdgrp().getRec().getMatdat()));
        BigDecimal amt = dftcre.getDfdgrp().getCbs().getMax().getAmt();
        if (amt != null) {
            map.put("amt", xrtmodService.amtRnd(amt, cur).toPlainString());
        }
        String addtxt = "";
        if( MdaUtils.compareTo(dftcre.getDfdgrp().getRec().getDfuflg(),"1") == 0 ) {
            addtxt = "境内";
        } else {
            addtxt = "境外";
        }
        String text19 = "请将以上金额从国结系统汇入汇出款账户 " + branch + codetableService.getTblLabel(cur, "CURKEY") + "21490000003"
                + " 中划转至已开立的<br/>" + addtxt + "银行代付（他代本）账户中。";
        map.put("text19", text19);
        map.put("footname", bchname + "交易银行部");
        map.put("footdate", Utils.fmtChineseDate(Dates.today()));
        return map;
    }
}
