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.Dbdgrp;
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("dbd.print")
public class DbdPrintServiceImpl implements IBopPrintService<Dbdgrp> {

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

    private String bgimg;

    @Autowired
    Pntmod pntmod;

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

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

        String checkedSign = pntmod.getCheckedChar();//勾选符
        BigDecimal zero = new BigDecimal(0);
        //下面将要向Map中输入数据
        pntmod.setSplitCodeNumber(data,
               bopGroup.getBas().getRptno(), "rptno");			//! 申报号码  rptno_1~22
        data.put("custnm",bopGroup.getBas().getCustnm());  //收款人名称   custnm
        String custypeCode =bopGroup.getBas().getCustype();//对公：组织机构代码   custype_c(对公)  custcod_1~9
        if("C".equals(custypeCode))
        {
            data.put("custype_c",checkedSign);
            pntmod.setSplitCodeNumber(data,
                   bopGroup.getBas().getCustcod(),"custcod");
        }
        if("D".equals(custypeCode))
        {
            data.put("custype_df",checkedSign);//对私中国居民
            data.put("custype_d",checkedSign);
            pntmod.setSplitCodeNumber(data,
                   bopGroup.getBas().getIdcode(),"idcode");
        }
        if("F".equals(custypeCode))
        {
            data.put("custype_df",checkedSign);
            data.put("custype_f",checkedSign);//对私 中国非居民
            //data.put("idcode",bopGroup.getBas().idcode);
            pntmod.setSplitCodeNumber(data,
                   bopGroup.getBas().getIdcode(),"idcode");
        }//对私：个人身份证件号码    custype_df(对私)  idcode 1~18
        String methodsCode =bopGroup.getBas().getMethod();
        pntmod.setCodetableData(data,methodsCode,"methods","L:A:C:G:D:T:M:O");//结算方式：信用证、托收、保函、电汇、票汇、信汇、其它  methods_1~7
        if(bopGroup.getBas().getTxamt().compareTo(zero) == 0){
            data.put("txccy_amt","");
        }
        else
        {
            BigDecimal tx_amt =bopGroup.getBas().getTxamt();
            data.put("txccy_amt",pntmod.getFormatedMoney(bopGroup.getBas().getTxccy(),tx_amt));//收入款币种及金额  txccy_amt
        }
        BigDecimal exrate =bopGroup.getBas().getExrate();
        exrate = exrate.setScale(8);


            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));//结汇金额  lcyamt
        }
        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));//现汇金额  fcyamt
        }
        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));  //其它金额  othamt
        }
        data.put("lcyacc",bopGroup.getBas().getLcyacc());//账号1银行卡号1  lcyacc
        data.put("fcyacc",bopGroup.getBas().getFcyacc());//账号/银行卡号2  fcyacc
        data.put("othacc",bopGroup.getBas().getOthacc());//账号/银行卡号3  othacc
        data.put("inchargeccy_amt",pntmod.getFormatedMoney(bopGroup.getBas().getInchargeccy(),bopGroup.getBas().getInchargeamt()));//国内银行扣费币种及金额  inchargeccy_amt
        data.put("oppuser",bopGroup.getBas().getOppuser());//付款人名称  oppuser
        data.put("paydate",Utils.fmtDate(credat,"yyyy/MM/dd"));//收账/结汇日期   rcvdate
        //判断是否需要打印全部信息
        if("1".equals(alonflg)){
            String payattrCode =bopGroup.getVrf().getPayattr();
            pntmod.setCodetableData(data,payattrCode,"payttr","X:E:D:A:M:O:R:C:I:N:L:B");
            //  境内收入类型：
            //生加工结转  payattr1//保税区//出口加工区//钻石交易所//其它特殊经济区域//汇路引起出口项下的跨境收入//出口信用保险理赔//出口货物保险理赔//买方信贷//转让信用证//背对背信用证 payattr11、它   payattr12
            pntmod.setCodetableData(data, bopGroup.getVrf().getIsref(),
                    "isref", "Y:N");														//本笔款项是否为保税货物款项下收入：isref_y
            pntmod.setCodetableData(data, bopGroup.getVrf().getPaytype(),
                    "paytype", "A:O");		//本笔款型是否为预收货款 ：advpay_y
            data.put("regno",bopGroup.getVrf().getRegno());//外汇局批件号/备案表号/业务编号  regno
            pntmod.setSplitCodeNumber(data,bopGroup.getVrf().getTxcode(),"txcode");//交易编码   txcode_1—6
            pntmod.setSplitCodeNumber(data,bopGroup.getVrf().getTxcode2(),"txcode2");//交易编码   txcode_21——26
            if(!MdaUtils.isEmpty(bopGroup.getVrf().getTxcode()))
            {
                BigDecimal txamt = new BigDecimal(0);;
                if(!MdaUtils.isEmpty(bopGroup.getVrf().getTc1amt())){
                    txamt = bopGroup.getVrf().getTc1amt();
                }
                data.put("txccy_amt1",pntmod.getFormatedMoney(bopGroup.getBas().getTxccy(),txamt));//相应币种及金额  txccy_amt1
            }
            else
            {
                data.put("txccy_amt1","");
            }
            if(!MdaUtils.isEmpty(bopGroup.getVrf().getTxcode2()))
            {
                BigDecimal tx2amt = new BigDecimal(0);;
                if(!MdaUtils.isEmpty(bopGroup.getVrf().getTc2amt())){
                    tx2amt = bopGroup.getVrf().getTc2amt();
                }
                data.put("txccy_amt2",pntmod.getFormatedMoney(bopGroup.getBas().getTxccy(),tx2amt));//相应币种及金额  txccy_amt2
            }
            else
            {
                data.put("txccy_amt2","");
            }
            data.put("txrem1",bopGroup.getVrf().getTxrem());//交易附言txrem1
            data.put("txrem2",bopGroup.getVrf().getTx2rem());//交易附言txrem2
            data.put("crtuser",bopGroup.getVrf().getCrtuser());//填报人签章  crtuser
            data.put("inptelc",bopGroup.getVrf().getInptelc());//  填报人号码  inptelc
            data.put("rptdate",Utils.fmtDate(bopGroup.getVrf().getRptdate(),"yyyy-MM-dd"));//申报日期

        }


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

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

        return data;
    }
}
