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

import com.brilliance.mda.runtime.mda.util.MdaUtils;
import com.brilliance.isc.bo.Eba;
import com.brilliance.isc.bo.Ebb;
import com.brilliance.isc.bo.Ebc;
import com.brilliance.isc.bo.model.Cfagit;
import com.brilliance.isc.common.bop.cfa.cfagit.service.CfaDependService;
import com.brilliance.isc.common.bop.cfa.cfagit.service.GIStoreContractService;
import com.brilliance.isc.common.context.SettleSession;
import com.brilliance.isc.mda.dao.EbaMapper;
import com.brilliance.isc.mda.dao.EbbMapper;
import com.brilliance.isc.common.sysmod.SysmodService;
import com.brilliance.isc.mda.dao.EbcMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author yangxd
 * @version 1.0
 * @description:
 * @data 2023/6/16 19:24
 */
@Service("eba.cfa")
public class GIStoreContractServiceImpl implements GIStoreContractService {

    @Autowired
    EbaMapper ebaMapper;

    @Autowired
    EbbMapper ebbMapper;

    @Autowired
    EbcMapper ebcMapper;

    @Autowired
    private SysmodService sysmodService;

    @Autowired
    private CfaDependService cfaDependService;

//    private Cfagit cfagit = new Cfagit();

    private static ThreadLocal<Cfagit> cfagitVar = new ThreadLocal<>();

    @Override
    public void storeContract(Cfagit cfagitv) {
        if(cfagitv.getRecgrp().getBas().getInr() == null)
        {
            cspsLogger.error("Inr为空数据");
            return;
        }
//        BeanUtils.copyProperties(cfagitv,cfagit);
        cfagitv.reset();
        cfagitVar.set(cfagitv);
        storeBASContract();
        storeDVContract();
    }

    /**
     *存签约信息
     */
    private void storeBASContract() {
        Cfagit cfagit = cfagitVar.get();
        if (cfagit.getBasflg().isEmpty()) {
            cspsLogger.info("无签约信息");
            return;
        }
//        cfaDependService.setAmeAmtAndExpDat(cfagit);
        String inrBas = cfagit.getRecgrp().getBas().getInr();
        //判断Inr对应的BAS对象为空
        if (!inrBas.isEmpty() && ebaMapper.selectByInr(inrBas) != null) {
//        ebaMapper.updateByPrimaryKey(ebaBas);
            sysmodService.sdbUpdateWithLog(cfagit.getRecgrp().getBas());
        }
        else {
            if (cfagit.getRecgrp().getBas().getInr().isEmpty())
                sysmodService.sdbInsertWithLog(cfagit.getRecgrp().getBas());
            else
                sysmodService.sdbUndeleteWithLog(cfagit.getRecgrp().getBas());
            //注释掉清空逻辑，没啥实际意义
//            MdaUtils.clear(cfagit.getCfamap().getCfarep());
            inrBas = cfagit.getRecgrp().getBas().getInr();
            cfagit.getCfamap().getCfarep().getEad().setInr(inrBas);
        }
        if( MdaUtils.compareTo(cfagit.getEblmod().getDectyp(),"M") == 0 )
        {
            //若是error数据,更改cfaerr中sta状态
            cfaDependService.setCFAERRSta( cfagit,"EBA", "BAS",0 );
        }
    }

    /**
     *存责任余额和履约信息
     */
    private void storeDVContract()
    {
        storeDCL();
        storeVCR();
    }

    private void storeDCL() {
        Cfagit cfagit = cfagitVar.get();
        for (int ebbI=0; ebbI < cfagit.getRecgrp().getDcl().size();ebbI++)
        {
            if (cfagit.getRecgrp().getDcl().get(ebbI).getAcp().equals("X"))
            {
                //保函相关都是担保相关的 因此当前是EBA
                cfagit.getRecgrp().getDcl().get(ebbI).setObjtyp("EBA");
                cfagit.getRecgrp().getDcl().get(ebbI).setObjinr(cfagit.getRecgrp().getBas().getInr());
                if (MdaUtils.isEmpty(ebbMapper.selectByInr(cfagit.getRecgrp().getDcl().get(ebbI).getInr()))) {
                    sysmodService.sdbInsertWithLog(cfagit.getRecgrp().getDcl().get(ebbI));
                    //TODO 存快照文件
                    // sysmodService.streamSaveInSubDir(cfarep.getEba());
                } else {
                    //TODO 存快照文件-对EBA读取后存快照
                    // sysmodService.streamSaveInSubDir(ebaMapper.selectByInr(ebb.getInr())...);
                    sysmodService.sdbUpdateWithLog(cfagit.getRecgrp().getDcl().get(ebbI));

                }
                if( MdaUtils.compareTo(cfagit.getEblmod().getDectyp(),"M") == 0 )
                {
                    //若是error数据,更改cfaerr中sta状态
                    cfaDependService.setCFAERRSta( cfagit,"EBA", "DCL",ebbI );
                }
            }

        }
    }

    private void storeVCR(){
        Cfagit cfagit = cfagitVar.get();
        for (int ebcI=0; ebcI < cfagit.getRecgrp().getVrf().size();ebcI++)
        {
            if (cfagit.getRecgrp().getVrf().get(ebcI).getAcp().equals("X"))
            {
                //保函相关都是担保相关的 因此当前是EBA，目标类型
                cfagit.getRecgrp().getVrf().get(ebcI).setObjtyp("EBA");
                cfagit.getRecgrp().getVrf().get(ebcI).setObjinr(cfagit.getRecgrp().getBas().getInr());

                if (MdaUtils.isEmpty(ebcMapper.selectByInr(cfagit.getRecgrp().getVrf().get(ebcI).getInr()))) {
                    sysmodService.sdbInsertWithLog(cfagit.getRecgrp().getVrf().get(ebcI));
                    //TODO 存快照文件
                    // sysmodService.streamSaveInSubDir(cfarep.getEba());
                } else {
                    sysmodService.sdbUpdateWithLog(cfagit.getRecgrp().getVrf().get(ebcI));
                    //TODO 存快照文件-对EBA读取后存快照
                    // sysmodService.streamSaveInSubDir(ebaMapper.selectByInr(ebb.getInr())...);
                }
                if( MdaUtils.compareTo(cfagit.getEblmod().getDectyp(),"M") == 0 )
                {
                    //若是error数据,更改cfaerr中sta状态
                    cfaDependService.setCFAERRSta( cfagit,"EBA", "VRF",ebcI );
                }
            }

        }
    }


}
