package com.brilliance.isc.common.bop.service.impl;

import cn.hutool.core.date.DateUtil;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import com.brilliance.isc.bo.Pbl;
import com.brilliance.isc.bo.Pty;
import com.brilliance.isc.common.bop.service.PblmodService;
import com.brilliance.isc.common.bop.service.Rmb2101Service;
import com.brilliance.isc.common.bopset.converter.PblInfoBuilder;
import com.brilliance.isc.common.context.BopContext;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.rmbbop.model.Tdagrp;
import com.brilliance.isc.common.rmbbop.vo.Rmb2101;
import com.brilliance.isc.common.util.BopUtils;
import com.brilliance.isc.common.util.RmbbopUtils;
import com.brilliance.isc.common.util.Utils;
import com.brilliance.isc.common.vo.BizInfoVo;
import com.brilliance.isc.mda.dao.CndMapper;
import com.brilliance.isc.mda.dao.PblMapper;
import com.brilliance.isc.vo.remit.CpdBizInfoVo;
import com.brilliance.isc.vo.remit.CpdCipsBizInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service("tda")
public class Rmb2101ServiceImpl implements Rmb2101Service {
    @Autowired
    private CndMapper cndMapper;

    @Autowired
    private PblmodService pblmodService;

    @Autowired
    private PblMapper pblMapper;

    @Override
    public void save(Rmb2101 rmb2101) {
        //保存人民币跨境支出信息
        if (MdaUtils.compareTo(rmb2101.getRmbflg(), "1") == 0) {
            String objinr = rmb2101.getRecgrp().getAds().getInr();
            if (!MdaUtils.isEmpty(objinr)) {
                Pbl pbl = pblMapper.selectByObjtypAndObjinr("TDA", objinr);
                rmb2101.getPblmod().setWrkpbl(pbl);
            }
            pblmodService.storeAllinBusTx(rmb2101);
//            wfmmodService.wfeAdd("BCK", "BCK", new Date(), BigDecimal.valueOf(24));
//            wfmmodService.wfeAdd("RSD", "RSD", new Date(), BigDecimal.valueOf(24));
        }
    }

    @Override
    public void updrmb(Rmb2101 rmb2101) {
        pblmodService.mappingRMBDataCommon(rmb2101);
        mappingRMBDataAAAAAA(rmb2101);
    }

    @Override
    public void saveSpt(Rmb2101 rmb2101) {
        BopUtils.saveSpt(rmb2101.getObjtyp() + "ADD");
    }

    @Override
    public void buildData(Rmb2101 rmb2101) {
        rmb2101.reset();
        rmb2101.getPblmod().setPblInfo(PblInfoBuilder.buildPblInfo(rmb2101));

    }

    private void mappingRMBDataAAAAAA(Rmb2101 rmb2101) {
        Tdagrp recgrp = rmb2101.getRecgrp();
        //收款人名称
        recgrp.getAds().setPayeename(RmbbopUtils.getPty("APL").getNam1());
        //收款人属性
        Pty pty = RmbbopUtils.getPty(RmbbopUtils.getTranRol());
        switch (pty.getIdtype()) {
            case "1":
                recgrp.getAds().setPayeeattr("2003");
                break;
            case "2":
            case "F":
            case "D":
            default:
                recgrp.getAds().setPayeeattr("");
        }
        //收款人机构代码或身份证件号码
        String transName = SettleContext.getTransName();
        if (StringUtils.equals(transName, "BTTSET")) {
            pty = RmbbopUtils.getPty("BE1");
        }
        recgrp.getAds().setPayeeorgcode(pty.getIdcode());
        //收款币种
        recgrp.getAds().setPayeecny(RmbbopUtils.getCur());
        //收款总金额  allamt
        recgrp.getAds().setAllamt(RmbbopUtils.getTxccy());
        //收款日期
        recgrp.getAds().setPayeedate(DateUtil.date());
        BizInfoVo bizInfo = BopContext.getBizInfo();
        switch (bizInfo.getObjtyp()) {
            case "BOD":
                //付款人名称
                recgrp.getAds().setPayername(BopUtils.getPtsByRol("DRE").getNam());
                //结算方式
                switch (bizInfo.getDoctypcod()) {
                    case "A":
                        recgrp.getAds().setBalancemode("DA");
                        break;
                    case "D":
                    case "P":
                        recgrp.getAds().setBalancemode("DP");
                        break;
                    default:
                        recgrp.getAds().setBalancemode("OT");
                }
                break;
            case "BED":
                //付款人名称
                recgrp.getAds().setPayername(BopUtils.getPtsByRol("APL").getNam());
                //结算方式
                recgrp.getAds().setBalancemode("LC");
                break;
            case "BTD":
                //付款人名称
                recgrp.getAds().setPayername(BopUtils.getPtsByRol("APL").getNam());
                //收款人名称
                recgrp.getAds().setPayeename(BopUtils.getPtsByRol("BE1").getNam());
                //结算方式
                recgrp.getAds().setBalancemode("LC");
                break;
            case "CCD":
                //付款人名称
                recgrp.getAds().setPayername(BopUtils.getPtsByRol("COR").getNam());
                //结算方式
                recgrp.getAds().setBalancemode("DP");
                break;
            //光大汇入汇款同出口收汇
            case "CPD":
                //对手客户名称，刘文青要求统一修改为取pts的nam
                recgrp.getAds().setPayername(BopUtils.getPtsByRol("ORC").getNam());
                //结算方式
                CpdBizInfoVo cpdBizInfoVo = (CpdBizInfoVo) bizInfo;
                switch (cpdBizInfoVo.getPaytype()) {
                    case "0":
                        recgrp.getAds().setBalancemode("TT");
                        break;
                    case "1":
                        recgrp.getAds().setBalancemode("DD");
                        break;
                    case "2":
                        recgrp.getAds().setBalancemode("MT");
                        break;
                    default:
                        recgrp.getAds().setBalancemode("OT");
                }
                break;
            case "GID":
            case "GCD":
            case "NID":
            case "NCD":
                //结算方式
                recgrp.getAds().setBalancemode("GT");
                //付款人名称
                if (transName.startsWith("GE") || transName.startsWith("NE")) {
                    recgrp.getAds().setPayername(BopUtils.getPtsByRol("APL").getNam());
                }
        }
        //付款行行号
        String sndbic = "";
        //CIPS页面付款人开户行行号
        String fkrkhh = "";
        CpdCipsBizInfo cpdCipsBizInfo = Utils.getFieldValue(bizInfo, "cpdCipsBizInfo");
        if (cpdCipsBizInfo != null) {
            fkrkhh = cpdCipsBizInfo.getFkrkhh();
        }
        if (StringUtils.isEmpty(fkrkhh)) {
            switch (transName) {
                case "BOTSET":
                    sndbic = BopUtils.getPtaByRol("COL").getPtyextkey();
                    break;
                case "BETSET":
                case "BTTSET":
                case "GETSET":
                case "NETSET":
                    sndbic = BopUtils.getPtaByRol("ISS").getPtyextkey();
                    break;
                case "CPTADV":
                case "CPTREP":
                    sndbic = BopUtils.getPtaByRol("ORI").getPtyextkey();
                    if (StringUtils.isEmpty(sndbic)) {
                        sndbic = BopUtils.getPtaByRol("SND").getPtyextkey();
                    }
                    break;
                case "CCTSET":
                    sndbic = BopUtils.getPtaByRol("DRO").getPtyextkey();
                    break;
            }
        } else {
            sndbic = fkrkhh;
        }
        recgrp.getAds().setPaybankcode(sndbic);
    }


}

