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

import com.brilliance.mda.runtime.mda.util.MdaUtils;
import com.brilliance.isc.bo.Pbl;
import com.brilliance.isc.common.bop.service.PblmodService;
import com.brilliance.isc.common.bop.service.Rmb2112Service;
import com.brilliance.isc.common.codetable.service.CodetableService;
import com.brilliance.isc.common.component.DyncDaoService;
import com.brilliance.isc.common.context.BopContext;
import com.brilliance.isc.common.rmbbop.entity.Tbz;
import com.brilliance.isc.common.rmbbop.model.Tcrgrp;
import com.brilliance.isc.common.rmbbop.vo.Rmb2112;
import com.brilliance.isc.common.setgll.vo.SetgllVo;
import com.brilliance.isc.common.sysmod.SysmodService;
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.PblMapper;
import com.brilliance.isc.mda.dao.TbzMapper;
import com.brilliance.isc.vo.collection.BodBizInfoVo;
import com.brilliance.isc.vo.collection.CcdBizInfoVo;
import com.brilliance.isc.vo.lc.BedBizInfoVo;
import com.brilliance.isc.vo.lc.BtdBizInfoVo;
import com.brilliance.isc.vo.remit.CpdBizInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service("tcr")
public class Rmb2112ServiceImpl implements Rmb2112Service {
    @Autowired
    private DyncDaoService dyncDaoService;
    @Autowired
    private CodetableService codetableService;
    @Autowired
    private PblmodService pblmodService;
    @Autowired
    private SysmodService sysmodService;
    @Autowired
    private TbzMapper tbzMapper;
    @Autowired
    private PblMapper pblMapper;

    @Override
    public void save(Rmb2112 rmb2112) {
        //保存人民币跨境支出信息
        String objinr = rmb2112.getRecgrp().getAds().getInr();
        if (!MdaUtils.isEmpty(objinr)) {
            Pbl pbl = pblMapper.selectByObjtypAndObjinr("TCR", objinr);
            rmb2112.getPblmod().setWrkpbl(pbl);
        }
        pblmodService.storeAllinBusTx(rmb2112);
    }

    @Override
    public void updrmb(Rmb2112 rmb2112) {
        pblmodService.mappingRMBDataCommon(rmb2112);
        mappingRMBDataAAAAAA(rmb2112);
        setTbz(rmb2112);
    }

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

    private void setTbz(Rmb2112 rmb2112) {
        Tcrgrp recgrp = rmb2112.getRecgrp();
        for (int i = 0; i < recgrp.getTbz().size(); i++) {
            Tbz tbz = recgrp.getTbz().get(i);
            tbz.setSeqno(i + 1);
            //明细操作类型
            tbz.setListopertype("1");
            //融资合同备案号
//            $CONTRACTNO = GetContentN( "\\RMBBOP\\RMB2108\\RECGRP\\ADS\\LEVYNO" )
//            if GetContentN( "\\RMBBOP\\RMB2108\\RMBFLG" ) = "1" then
//            MappingField_RMB( GetField( "RECP\TBZPP(" + tejRow + ")\CONTRACTNO" ), $CONTRACTNO )
//            else
//            MappingField_RMB( GetField( "RECP\TBZPP(" + tejRow + ")\CONTRACTNO" ), "" )
//            endif
            tbz.setCuststate("0001");
//            ExecuteDefault( RECP\RECGRP\TBZ )
        }
    }

    private void mappingRMBDataAAAAAA(Rmb2112 rmb2112) {
        Tcrgrp recgrp = rmb2112.getRecgrp();

        //收款人账号
        recgrp.getAds().setPayeeacct(RmbbopUtils.getBasic().getLeft());

        String cty = null;
        String payeracct = recgrp.getAds().getPayeracct();
        BizInfoVo bizInfo = BopContext.getBizInfo();
        switch (bizInfo.getObjtyp()) {
            case "BOD":
                BodBizInfoVo bodBizInfoVo = (BodBizInfoVo) bizInfo;
                cty = bodBizInfoVo.getStacty();
                if (StringUtils.isEmpty(payeracct)) {
                    List<SetgllVo> setgll = BopContext.getSetglg().getSetgll();
                    payeracct = setgll.stream().filter(i -> "COL".equals(i.getRol()) && "D".equals(i.getDebcdtflg()))
                            .findAny().orElseGet(SetgllVo::new).getAct();
                }
                break;
            case "BED":
                BedBizInfoVo bedBizInfoVo = (BedBizInfoVo) bizInfo;
                cty = bedBizInfoVo.getStacty();
                break;
            case "BTD":
                BtdBizInfoVo btdBizInfoVo = (BtdBizInfoVo) bizInfo;
                cty = btdBizInfoVo.getStacty();
                break;
            case "CCD":
                CcdBizInfoVo ccdBizInfoVo = (CcdBizInfoVo) bizInfo;
                cty = ccdBizInfoVo.getStacty();
                break;
            case "CPD":
                CpdBizInfoVo cpdBizInfoVo = (CpdBizInfoVo) bizInfo;
                cty = cpdBizInfoVo.getOrcctycod();
                payeracct = cpdBizInfoVo.getOrcact();
                break;
            case "GID":
            case "GCD":
            case "NID":
            case "NCD":
                cty = (String) Utils.getField(bizInfo, "stacty").orElseGet(String::new);
                break;
        }
        //付款人常驻国家（地区）代码
        if (StringUtils.isNotEmpty(cty)) {
            String payercountrycode = cty.length() == 2 ? codetableService.getTblLabel(cty, "CTYCOD") : cty;
            recgrp.getAds().setPayercountrycode(payercountrycode);
        }
        //付款人账号
        String payeraccttype = recgrp.getAds().getPayeraccttype();
        if (StringUtils.equals(payeraccttype, "3")) {
            recgrp.getAds().setPayeracct("N/A");
        } else if (StringUtils.isNotEmpty(payeracct)) {
            recgrp.getAds().setPayeracct(payeracct);
        }
        //是否退款
        recgrp.getAds().setIfrefund("N");
    }

    @Override
    public void saveChildData(Rmb2112 rmb2112) {
        Tcrgrp recgrp = rmb2112.getRecgrp();
        //savdatafrompanel
        pblmodService.setListData(rmb2112.getPblmod(), recgrp.getTbz());
        recgrp.getAds().setIncomelist(recgrp.getTbz().size());
        //存储TBZ数据
        for (Tbz tbz : recgrp.getTbz()) {
            tbz.setObjinr(recgrp.getAds().getInr());
            tbz.setObjtyp(rmb2112.getObjtyp());
            if (dyncDaoService.dyncReadNoDataByInr(tbz))
                sysmodService.sdbUpdateWithLog(tbz);
            else
                sysmodService.sdbInsertWithLog(tbz);
        }
    }

    @Override
    public void rmbLoadContract(Rmb2112 rmb2112) {
        //!读入TBZ数据
        rmb2112.getRecgrp().getAds().setIncomelist(rmb2112.getNewnum());
        int tbk = rmb2112.getRecgrp().getAds().getIncomelist();
        if (tbk > 0) {
            List<Tbz> tbkList = rmb2112.getRecgrp().getTbz();
            for (int i = 0; i < tbk; i++) {
                Tbz newtbz = new Tbz();
                newtbz.setObjtyp("TCR");
                newtbz.setObjinr(rmb2112.getRecgrp().getAds().getInr());
                newtbz.setSeqno(i + 1);
                newtbz = tbzMapper.selectByObjInfo(newtbz);
                if (newtbz != null) {
                    tbkList.add(newtbz);
                }
            }
        }
        //读TDA
//        String levyno = rmb2112.getRecgrp().getAds().getLevyno();
//        Tda newtda = new Tda();
//        newtda = tdaMapper.selectByLevyno(levyno);
    }

    @Override
    public List<String> setRMBPresaveoldValue(Rmb2112 rmb2112) {
        rmb2112.getRecgrp().getAds().setIncomelist(rmb2112.getNewnum());
        int tbz = rmb2112.getRecgrp().getAds().getIncomelist();
        List<String> list = new ArrayList<>();
        if (tbz > 0) {
            rmb2112.getRecgrp().getTbz().forEach(tbzObj -> {
                if (!MdaUtils.isEmpty(tbzObj.getSeqno()) && !MdaUtils.isEmpty(tbzObj.getListopertype())) {
                    list.add(tbzObj.getSeqno() + "TBZ" + "\t" + tbzObj.getListopertype());
                }
            });
        }
        return list;
    }


}
