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

import com.brilliance.mda.runtime.mda.util.*;
import com.brilliance.isc.bo.Cbb;
import com.brilliance.isc.bo.Pbl;
import com.brilliance.isc.common.bop.service.PblmodService;
import com.brilliance.isc.common.bop.service.Rmb2123Service;
import com.brilliance.isc.common.util.BopUtils;
import com.brilliance.isc.common.bopset.converter.PblInfoBuilder;
import com.brilliance.isc.common.bopset.counter.service.CounterRmbService;
import com.brilliance.isc.common.component.PtsptaToolComponent;
import com.brilliance.isc.common.context.BopContext;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.rmbbop.vo.Rmb2123;
import com.brilliance.isc.common.transaction.help.TransactionHelpService;
import com.brilliance.isc.common.vo.BizInfoVo;
import com.brilliance.isc.common.vo.PtsptaVo;
import com.brilliance.isc.common.wfmmod.service.WfmmodService;
import com.brilliance.isc.mda.dao.GidMapper;
import com.brilliance.isc.mda.dao.NidMapper;
import com.brilliance.isc.mda.dao.PblMapper;
import com.brilliance.isc.vo.lg.GcdBizInfoVo;
import com.brilliance.isc.vo.lg.NcdBizInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.brilliance.mda.runtime.mda.Constants.NULLSTR;


@Service("tdq")
public class Rmb2123ServiceImpl implements Rmb2123Service {

    @Autowired
    private PblmodService pblmodService;

    @Autowired
    private CounterRmbService counterRmbService;

    @Autowired
    private PblMapper pblMapper;

    @Autowired
    private WfmmodService wfmmodService;

    @Autowired
    private PtsptaToolComponent ptsptaToolComponent;

    @Autowired
    private GidMapper gidMapper;

    @Autowired
    private NidMapper nidMapper;

    @Autowired
    private TransactionHelpService transactionHelpService;

    @Override
    public void save(Rmb2123 rmb2123) {
        //保存人民币跨境支出信息

        if (MdaUtils.compareTo(rmb2123.getRmbflg(), "1") == 0) {

            String objinr = rmb2123.getRecgrp().getAds().getInr();
            if (!MdaUtils.isEmpty(objinr)) {
                Pbl pbl = pblMapper.selectByObjtypAndObjinr("TDQ", objinr);
                rmb2123.getPblmod().setWrkpbl(pbl);
            }
            pblmodService.storeAllinBusTx(rmb2123);
//            wfmmodService.wfeAdd("BCK", "BCK", new Date(), BigDecimal.valueOf(24));
//            wfmmodService.wfeAdd("RSD", "RSD", new Date(), BigDecimal.valueOf(24));
        }
    }


    @Override
    public void updrmb(Rmb2123 rmb2123) {
        pblmodService.mappingRMBDataCommon(rmb2123);
        mappingRMBDataAAAAAA(rmb2123);
    }

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

    private void mappingRMBDataAAAAAA(Rmb2123 rmb2123) {
        List<PtsptaVo> ptsList = BopContext.getPtsList();
        BizInfoVo bizInfo = BopContext.getBizInfo();
        if (ptsList != null && ptsList.size() > 0) {
            for (PtsptaVo ptspta : ptsList) {
                ptsptaToolComponent.fillPtsptaAllProperties(ptspta);
            }
        }
        String ownref = bizInfo.getOwnref();
        rmb2123.setOwnref(ownref);
        String rmbownref = rmb2123.getRecgrp().getAds().getBanktrano();
        String levyno = "";
        if (!MdaUtils.isEmpty(ownref) && MdaUtils.compareTo(ownref, rmbownref) != 0) {
            rmb2123.getRecgrp().getAds().setBanktrano(ownref);
            String msgtyp = "2123";
            levyno = getRmbRef(msgtyp, ownref);
        } else {
            levyno = rmb2123.getRecgrp().getAds().getLevyno();
        }

        rmb2123.getRecgrp().getAds().setLevyno(levyno);

        // 银行机构代码
        String lhanam = NULLSTR;
        if (!MdaUtils.isEmpty(SettleContext.getUserSession().getWrkbch())) {
            lhanam = SettleContext.getUserSession().getWrkbch().getLhanum();
        }
        // String bchname = SettleContext.getUserSession().getWrkbch().getBchname();
        rmb2123.getRecgrp().getAds().setBankorgcode(lhanam);

        //业务所属机构
        if (!MdaUtils.isEmpty(SettleContext.getUserSession().getWrkbch())) {
            rmb2123.getRecgrp().getAds().setOwnextkey(SettleContext.getUserSession().getWrkbch().getBranch());
        } else {
            rmb2123.getRecgrp().getAds().setOwnextkey(SettleContext.getUserSession().getBch().getBranch());
        }

        if (MdaUtils.compareTo(rmb2123.getRmbflg(), "1") == 0) {
            String swiftbic = "";
            PtsptaVo adv = ptsptaToolComponent.getPtsFromListByRol(ptsList, "ADV");
            if (MdaUtils.isEmpty(adv)) {
                adv = new PtsptaVo();
                adv.reset();
            }
            PtsptaVo atb = ptsptaToolComponent.getPtsFromListByRol(ptsList, "ATB");
            if (MdaUtils.isEmpty(atb)) {
                atb = new PtsptaVo();
                atb.reset();
            }
            PtsptaVo iss = ptsptaToolComponent.getPtsFromListByRol(ptsList, "ISS");
            if (MdaUtils.isEmpty(iss)) {
                iss = new PtsptaVo();
                iss.reset();
            }
            //境外银行SWIFT BIC
            if (MdaUtils.compareTo(Strings.mid(SettleContext.getTransName().toUpperCase(), 1, 2), "GI") == 0 || MdaUtils.compareTo(Strings.mid(SettleContext.getTransName().toUpperCase(), 1, 2), "NI") == 0) {

                if (!MdaUtils.isEmpty(adv.getAdr())) {
                    swiftbic = adv.getAdr().getBic();
                }
                if (MdaUtils.isEmpty(swiftbic)) {
                    if (!MdaUtils.isEmpty(atb.getAdr())) {
                        swiftbic = atb.getAdr().getBic();
                    }
                }
            } else {
                if (!MdaUtils.isEmpty(iss.getAdr())) {
                    swiftbic = iss.getAdr().getBic();
                }
            }

            rmb2123.getRecgrp().getAds().setSwiftbic(swiftbic);
            //原担保登记申报号码
            String orilevyno = "";
            if (MdaUtils.compareTo(Strings.mid(SettleContext.getTransName().toUpperCase(), 1, 2), "GI") == 0 || MdaUtils.compareTo(Strings.mid(SettleContext.getTransName().toUpperCase(), 1, 2), "GE") == 0) {
                GcdBizInfoVo gcdBizInfoVo = (GcdBizInfoVo) bizInfo;
                String gidownref = gidMapper.selectByGcdOwnref(gcdBizInfoVo.getOwnref());
                orilevyno = pblMapper.selectByGidLevyno(gidownref);
            } else if (MdaUtils.compareTo(Strings.mid(SettleContext.getTransName().toUpperCase(), 1, 2), "NI") == 0 || MdaUtils.compareTo(Strings.mid(SettleContext.getTransName().toUpperCase(), 1, 2), "NE") == 0) {
                NcdBizInfoVo ncdBizInfoVo = (NcdBizInfoVo) bizInfo;
                String nidownref = nidMapper.selectByNcdOwnref(ncdBizInfoVo.getOwnref());
                orilevyno = pblMapper.selectByNidLevyno(nidownref);
            }
            rmb2123.getRecgrp().getAds().setOrilevyno(orilevyno);

            //履约币种
            Map<String, Cbb> cbsMap = BopContext.getCbsMap();

            String cur = NULLSTR;
            BigDecimal vouchamt = null;
            BigDecimal vouchamtall = null;
            BigDecimal vouchamtall2 = null;


            if (MdaUtils.compareTo(Strings.mid(SettleContext.getTransName().toUpperCase(), 1, 2), "GI") == 0 || MdaUtils.compareTo(Strings.mid(SettleContext.getTransName().toUpperCase(), 1, 2), "GE") == 0) {
                //取实际赔付金额
                Cbb gcdopn1 = cbsMap.get("OPN1");
                cur = gcdopn1.getCur();
                vouchamt = gcdopn1.getAmt();
                Cbb gcdopn2 = cbsMap.get("OPN2");
                if (!MdaUtils.isEmpty(gcdopn2)) {
                    vouchamt = gcdopn1.getAmt().add(gcdopn2.getAmt());
                }
                Cbb gidmax = cbsMap.get("GIDMAX");
                vouchamtall = gidmax.getAmt();
                Cbb gidmax2 = cbsMap.get("GIDMAX2");
                if (!MdaUtils.isEmpty(gidmax2)) {
                    vouchamtall2 = gidmax2.getAmt();
                }
            }

            if (MdaUtils.compareTo(Strings.mid(SettleContext.getTransName().toUpperCase(), 1, 2), "NI") == 0 || MdaUtils.compareTo(Strings.mid(SettleContext.getTransName().toUpperCase(), 1, 2), "NE") == 0) {
                //取实际赔付金额
                Cbb ncdopn1 = cbsMap.get("OPN1");
                cur = ncdopn1.getCur();
                vouchamt = ncdopn1.getAmt();
                Cbb nidmax = cbsMap.get("NIDMAX");
                vouchamtall = nidmax.getAmt();
            }

            rmb2123.getRecgrp().getAds().setCurrencycode(cur);

            //担保履约金额

            rmb2123.getRecgrp().getAds().setVouchamt(vouchamt);
            //担保履约比例
            //取gidgrp下的cbb中max的amt


            if (!MdaUtils.isEmpty(vouchamtall)) {
                BigDecimal vouchscale = new BigDecimal(1);
                if (!MdaUtils.isEmpty(vouchamtall2)) {
                    vouchscale = Decimals.div(vouchamt, vouchamtall.add(vouchamtall2));
                } else {
                    vouchscale = Decimals.div(vouchamt, vouchamtall);
                }
                rmb2123.getRecgrp().getAds().setVouchscale(vouchscale.setScale(2, BigDecimal.ROUND_HALF_UP));
            }
            // TODO: 2024/3/14 此逻辑需要在mappingRMBDataCommon中补充
            //履约跨境收/支申报号码
//            String paymentlevyno = "";
//            if (MdaUtils.compareTo(Strings.mid(SettleContext.getTransName().toUpperCase(), 1, 2), "GI") == 0 || MdaUtils.compareTo(Strings.mid(SettleContext.getTransName().toUpperCase(), 1, 2), "NI") == 0) {
//                paymentlevyno = rmbbopRequest.getRmbbop().getRmb2111().getRecgrp().getAds().getLevyno();
//            } else {
//                paymentlevyno = rmbbopRequest.getRmbbop().getRmb2101().getRecgrp().getAds().getLevyno();
//            }
//            if (!MdaUtils.isEmpty(paymentlevyno)) {
//                rmb2123.getRecgrp().getAds().setPaymentlevyno(paymentlevyno);
//            }
        }

    }

    @Override
    public void buildData(Rmb2123 rmb2123) {
        rmb2123.reset();
        rmb2123.getPblmod().setPblInfo(PblInfoBuilder.buildPblInfo(rmb2123));

    }

    @Override
    public void saveChildData(Rmb2123 rmb2123) {
        /**
         * TDQ无须处理
         */

        rmb2123.getPblmod().setPblInfo(PblInfoBuilder.buildPblInfo(rmb2123));
    }

    @Override
    public void rmbLoadContract(Rmb2123 rmb2123) {
        /**
         * TDQ无须处理
         */
    }

    @Override
    public List<String> setRMBPresaveoldValue(Rmb2123 rmb2123) {
        /**
         * TDQ无须处理
         */
        return null;
    }


    public String getRmbRef(String msgtyp, String ownref) {
        //!光大改为生成业务主键
        String bopref = "";
        Date dat = Dates.today();
        String couNam = msgtyp + ownref;
        int counum = counterRmbService.dbRmbCounter(couNam, dat);
        while (MdaUtils.compareTo(Decimals.mul(Numerics.round(Decimals.div(counum, 100), 0), 100), counum) == 0) {
            counum = counterRmbService.dbRmbCounter(couNam, dat);
        }
        String couVal = MdaUtils.toString(counum, -4);
        bopref = msgtyp + ownref + couVal;
        return bopref;
    }


}
