package com.brilliance.isc.common.bop.bopdec.service.imp;

import com.brilliance.mda.runtime.mda.util.MdaUtils;
import com.brilliance.isc.bo.bop.bopprint.BopTrnBo;
import com.brilliance.isc.bo.model.Dbagrp;
import com.brilliance.isc.common.bopset.service.IBopPrintService;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.util.Utils;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.misc.BASE64Encoder;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service("dba.print")
public class DbaPrintServiceImpl implements IBopPrintService<Dbagrp> {

    private static Logger log = LoggerFactory.getLogger(DbaPrintServiceImpl.class);

    private String bgimg;

    @Autowired
    Pntmod pntmod;

    @PostConstruct
    public  void loadBgImg() throws IOException {
        //获取背景图片
        String base64 = new BASE64Encoder().encode(IOUtils.resourceToByteArray("images/BOPCOMDBA.jpg", DbaPrintServiceImpl.class.getClassLoader()));
        bgimg = "data:image/jpg;base64,"+base64;
    }

    @Override
    public Map<String, Object> getInputMapData(Dbagrp bopGroup, Date credat,String alonflg) {
        //新申请一个空的Map
        Map<String, Object> data = new HashMap<String, Object>();
        //赋值背景图片
        data.put("bgImg",bgimg);


        String checkedSign = pntmod.getCheckedChar();//勾选符
        //下面将要向Map中输入数据
        //! 申报号码
        pntmod.setSplitCodeNumber(data,bopGroup.getBas().getRptno(), "rptno");
        //! 收款人名称
        data.put("custnm", bopGroup.getBas().getCustnm());
        //对公/[对私/个人/非个人]
        String custypeCode = bopGroup.getBas().getCustype();
        //对公
        if ("C".equals(custypeCode)) {
            //! 勾选对公
            data.put("custype_c", checkedSign);
            //! 组织机构代码
            pntmod.setSplitCodeNumber(data,bopGroup.getBas().getCustcod(), "custcod");
        }
        //对私中国居民
        else if ("D".equals(custypeCode)) {
            //! 对私
            data.put("custype_df", checkedSign);
            //! 中国居民个人
            data.put("custype_d", checkedSign);
            //! 对私将加入个人身份证号码
            data.put("idcode", bopGroup.getBas().getIdcode());
        }
        //对私非中国居民
        else if ("F".equals(custypeCode)) {
            //! 对私
            data.put("custype_df", checkedSign);
            //! 非中国居民个人
            data.put("custype_f", checkedSign);
            //! 对私将加入个人身份证号码
            data.put("idcode", bopGroup.getBas().getIdcode());
        }
        //直接忽视掉其他情况（属于异常）
        else {
            log.error("data incorrect in 'dbapt',custype not recognized:" + custypeCode);
        }

        //结算方式
        String methodsCode = bopGroup.getBas().getMethod();
        //! 结算方式
        pntmod.setCodetableData(data, methodsCode,"methods", "L:A:C:G:D:T:M:O");

        BigDecimal zero = new BigDecimal(0);
        BigDecimal tx_amt = bopGroup.getBas().getTxamt();
        data.put("txccy_amt",pntmod.getFormatedMoney(bopGroup.getBas().getTxccy(), tx_amt));
        //! 收入款币种及金额
        log.debug("_____________________END");

        //结汇汇率
        if (bopGroup.getBas().getExrate().compareTo(zero) <= 0) {
            data.put("exrate", "");
        } else {
            BigDecimal exrateStr = bopGroup.getBas().getExrate();
            data.put("exrate", exrateStr.toPlainString());
        }

        if (bopGroup.getBas().getLcyamt().compareTo(zero) == 0) {
            data.put("lcyamt", "");
        } else {
            BigDecimal lcyamt = bopGroup.getBas().getLcyamt();
            //! 结汇金额
            data.put("lcyamt", pntmod.getFormatedMoney(bopGroup.getBas().getTxccy(), lcyamt));
        }
        //! 人民币账户
        data.put("lcyacc", bopGroup.getBas().getLcyacc());
        if (bopGroup.getBas().getFcyamt().compareTo(zero) == 0) {
            data.put("fcyamt", "");
        } else {
            BigDecimal fcyamt = bopGroup.getBas().getFcyamt();
            //! 现汇金额
            data.put("fcyamt",pntmod.getFormatedMoney(bopGroup.getBas().getTxccy(),fcyamt));
        }
        //! 外汇账户
        data.put("fcyacc", bopGroup.getBas().getFcyacc());
        if (bopGroup.getBas().getOthamt().compareTo(zero) == 0) {
            data.put("othamt", "");
        } else {
            BigDecimal othamt = bopGroup.getBas().getOthamt();
            //! 其他金额
            data.put("othamt",pntmod.getFormatedMoney(bopGroup.getBas().getTxccy(),othamt));
        }
        //! 其他账户
        data.put("othacc", bopGroup.getBas().getOthacc());
        //<12928C>

        if (bopGroup.getBas().getInchargeamt().compareTo(zero) == 0) {
            data.put("inchargeccyamt", "");
        } else {
            //! 国内银行扣费款币种及金额
            data.put("inchargeccyamt",
                    pntmod.getFormatedMoney(bopGroup.getBas().getInchargeccy(),bopGroup.getBas().getInchargeamt()));
        }
        //! 国外银行扣费款币种及金额
        if (bopGroup.getBas().getOutchargeamt().compareTo(zero) == 0){
            data.put("outchargeamt", "");
        }
        else {
            data.put("outchargeccyamt",
                    pntmod.getFormatedMoney(bopGroup.getBas().getOutchargeccy(),
                            bopGroup.getBas().getOutchargeamt()));
        }
        //</12928C>
        log.debug("-----------------------D");
        if (!MdaUtils.isEmpty(bopGroup.getBas().getOppuser())) {
            String s = bopGroup.getBas().getOppuser();
            data.put("oppuser", s);
        }
        //判断是否需要打印全部信息
        if("1".equals(alonflg)){
            //! 付款人常驻国家地区编码
            pntmod.setSplitCodeNumber(data,bopGroup.getDcl().getCountry(), "country");
            //! 申报日期
            data.put("rptdate", Utils.fmtDate(bopGroup.getDcl().getRptdate(), "yyyy/MM/dd"));
            String paytypeCode = bopGroup.getDcl().getPaytype();
            pntmod.setCodetableData(data, paytypeCode, "paytype", "A:R");
            //! 结算方式
            pntmod.setCodetableData(data, bopGroup.getDcl().getIsref(),"isref", "Y:N");
            //! 外汇局批件号**
            data.put("billno", bopGroup.getDcl().getBillno());
            //! 收入类型
            pntmod.setCodetableData(data, bopGroup.getDcl().getPayattr(),
                    "payattr", "F:T:N:D:O");
            //! 交易编码1
            pntmod.setSplitCodeNumber(data,
                    bopGroup.getDcl().getTxcode(), "txcode");
            if (!MdaUtils.isEmpty(bopGroup.getDcl().getTxcode())) {
                BigDecimal txamt = new BigDecimal(0);;
                if(!MdaUtils.isEmpty(bopGroup.getDcl().getTc1amt())){
                    txamt = bopGroup.getDcl().getTc1amt();
                }
                log.debug("-----------------------E");
                //! 币种及金额
                data.put("tc1ccyamt",
                        pntmod.getFormatedMoney(bopGroup.getBas().getTxccy(), txamt));
            }
            //! 交易附言
            data.put("txrem", bopGroup.getDcl().getTxrem());
            //! 交易编码2
            pntmod.setSplitCodeNumber(data,
                    bopGroup.getDcl().getTxcode2(), "txcode2");
            if (!MdaUtils.isEmpty(bopGroup.getDcl().getTxcode2())) {
                BigDecimal tx2amt = new BigDecimal(0);;
                if(!MdaUtils.isEmpty(bopGroup.getDcl().getTc2amt())){
                    tx2amt = bopGroup.getDcl().getTc2amt();
                }
                //! 币种及金额2
                data.put("tc2ccyamt",
                        pntmod.getFormatedMoney(bopGroup.getBas().getTxccy(), tx2amt));
            } else {
                data.put("tc2ccyamt", "");
            }
            //! 交易附言2
            data.put("tx2rem", bopGroup.getDcl().getTx2rem());
            //! 填报人
            data.put("crtuser", bopGroup.getDcl().getCrtuser());
            //! 交易附言2
            data.put("inptelc", bopGroup.getDcl().getInptelc());
        }

        //银行业务编号
        data.put("buscode", bopGroup.getBas().getBuscode());
        //增加银行经办人签章
        data.put("accusr", SettleContext.getSessionUserVo().getName());
        log.debug("-----------------------F");
        return data;
    }

    @Override
    public Map<String, Object> getInputMapDataForTrn(Dbagrp bopGroup, Date credate, String alonflg, BopTrnBo bopTrnBo) {
        //先打印申报数据
        Map<String, Object> data = this.getInputMapData(bopGroup,credate,alonflg);
        //补充打印业务信息 若需要

        return data;
    }
}
