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

import com.brilliance.mda.runtime.mda.util.Dates;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import com.brilliance.mda.runtime.mda.util.Strings;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import com.brilliance.isc.bo.*;
import com.brilliance.isc.bo.model.Cfagit;
import com.brilliance.isc.common.bop.cfa.cfagit.service.CfaDependService;
import com.brilliance.isc.common.bopset.service.CfagitService;
import com.brilliance.isc.common.bop.cfa.common.CfaStoreContractService;
import com.brilliance.isc.common.bop.cfa.common.TraMappingCfaDataService;
import com.brilliance.isc.common.bopset.service.EblmodService;
import com.brilliance.isc.common.context.SpringEnvContext;
import com.brilliance.isc.common.sysmod.SysmodService;
import com.brilliance.isc.common.transaction.help.TransactionHelpService;
import com.brilliance.isc.common.transaction.help.TransactionServiceSet;
import com.brilliance.isc.common.transaction.vo.BaseTransactionVo;
import com.brilliance.isc.common.vo.BizInfoVo;
import com.brilliance.isc.mda.dao.EbaMapper;
import com.brilliance.isc.mda.dao.EbbMapper;
import com.brilliance.isc.mda.dao.EbcMapper;
import com.brilliance.isc.mda.dao.EblMapper;
import com.brilliance.isc.vo.lg.GcdBizInfoVo;
import com.brilliance.isc.vo.lg.GidBizInfoVo;
import com.brilliance.isc.vo.lg.NcdBizInfoVo;
import com.brilliance.isc.vo.lg.NidBizInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

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

/**
 * @version 1.0
 * @description:
 * @data 2023/6/8 14:06
 */
@Service
public class CfagitServiceImpl implements CfagitService {
    private final Logger log = LoggerFactory.getLogger(CfagitServiceImpl.class);
    /**
     * eba-对外担保签约
     * ebb-对外担保责任余额
     * ebc-对外担保履约
     * ead-外债海外代付签约
     * eaf-外债远期信用证签约
     * ear-外债变动
     * eca-国内外汇贷款签约
     * ecb-国内外汇贷款变动
     */
    private String typeOfCfa = "eba";


    @Autowired
    EblmodService eblmodService;

    @Autowired
    SysmodService sysmodService;

    @Autowired
    TraMappingCfaDataService traMappingCfaDataService;

    @Autowired
    CfaDependService cfaDependService;

    @Autowired
    TransactionHelpService transactionHelpService;

    @Autowired
    EblMapper eblMapper;

    @Autowired
    EbaMapper ebaMapper;

    @Autowired
    EbbMapper ebbMapper;

    @Autowired
    EbcMapper ebcMapper;


    public void cfaBopSaveData(Cfagit cfagit){
        cfagit.reset();
        if( MdaUtils.compareTo(cfagit.getBasflg(),"X") != 0 && MdaUtils.compareTo(cfagit.getDclflg(),"X") != 0 && MdaUtils.compareTo(cfagit.getVrfflg(),"X") != 0 )
        return ;
        //生成外债编号
        if( MdaUtils.compareTo(cfagit.getBasflg(),"X") == 0 ) {
            //签约信息新增
            if( MdaUtils.compareTo(cfagit.getCfatyp(),"A") == 0 )
            {
                if(MdaUtils.isEmpty(cfagit.getRecgrp().getBas().getExguarancode())) {
                    cfaDependService.setCfagitPro(cfagit);
                    cfaDependService.getCfaReference("EBA", cfagit.getRecgrp().getBas().getContractdate());
                }
            }
            cfagit.getEblmod().getEbl().setBasflg( "X");
//            if( MdaUtils.compareTo(cfagit.getRecgrp().getBas().getSta(),"S") != 0 )
//            {
//                cfagit.getRecgrp().getBas().setInr(NULLSTR);
//            }
            cfagit.getRecgrp().getBas().setInr(NULLSTR);
            cfagit.getRecgrp().getBas().setSta( "W");
        }
//    EBL存表//资本类型当前保函都从bas中获取，后续其他对象需要从对应类型中获取
        cfagit.getEblmod().getEbl().setOwnextkey(cfagit.getOwnextkey());
        cfagit.getEblmod().getEbl().setOwnref(cfagit.getRec().getOwnref());
        cfagit.getEblmod().getEbl().setRptno(cfagit.getRecgrp().getBas().getExguarancode());
        eblmodService.setEblData(cfagit,typeOfCfa.toUpperCase(), Dates.today());
        sysmodService.sdbInsert(cfagit.getEblmod().getEbl());

        //责任余额、履约信息新增
        String complianceno = NULLSTR;
        if( MdaUtils.compareTo(cfagit.getDclflg(),"X") == 0 )
        {
            //责任余额信息新增
            int sizeDcl = cfagit.getRecgrp().getDcl().size();
            for(int  i = 0;i < sizeDcl;i++)
            {
                cfagit.getRecgrp().getDcl().get(i).setExguarancode( cfagit.getRecgrp().getBas().getExguarancode());
                //去掉了dclp去保存前端数据
//                cfagit.getRecp().getDclp().getElement(i).setExguarancode( this.getRecgrp().getBas().getExguarancode());
                cfagit.getRecgrp().getDcl().get(i).setSta( "W");
                cfagit.getRecgrp().getDcl().get(i).setEblinr( cfagit.getEblmod().getEbl().getInr());
                cfagit.getRecgrp().getDcl().get(i).setCredat( Dates.today());
            }
        }
        if( MdaUtils.compareTo(cfagit.getVrfflg(),"X") == 0 )
        {
            //履约信息新增
            int sizeVrf = cfagit.getRecgrp().getVrf().size();
            for(int  i = 0;i < sizeVrf;i++)
            {
                cfagit.getRecgrp().getVrf().get(i).setExguarancode( cfagit.getRecgrp().getBas().getExguarancode());
                //后续是否需要反馈页面显示
//                cfagit.getRecp().getVrfp().getElement(i).setExguarancode( this.getRecgrp().getBas().getExguarancode());
                cfagit.getRecgrp().getVrf().get(i).setSta( "W");
                cfagit.getRecgrp().getVrf().get(i).setEblinr( cfagit.getEblmod().getEbl().getInr());
                cfagit.getRecgrp().getVrf().get(i).setCredat( Dates.today());
                if( MdaUtils.isEmpty(cfagit.getRecgrp().getVrf().get(i).getComplianceno()) )
                {
                    cfaDependService.setCfagitPro(cfagit);
                    complianceno = cfaDependService.getChangeNo ( cfagit.getRecgrp().getBas().getExguarancode() );
                    cfagit.getRecgrp().getVrf().get(i).setComplianceno( complianceno);
                    //后续是否需要反馈页面显示履约编号
//                    cfagit.getRecp().getVrfp().get(i).setComplianceno( complianceno);
                }
            }
        }
        //存申报数据表
        // 设置eba到ebl关联ebltrn
        cfagit.getRecgrp().getBas().setEblinr(cfagit.getEblmod().getEbl().getInr());
        //修改eba credat日期
        cfagit.getRecgrp().getBas().setCredat(Dates.today());
        CfaStoreContractService cfaStoreConSer = SpringEnvContext.getBean(typeOfCfa.toLowerCase()+".cfa");
        cfaStoreConSer.storeContract(cfagit);
        //赋值ebl关联表inr更新
        String eblInr = cfagit.getEblmod().getEbl().getObjinr();
        String basInr = cfagit.getRecgrp().getBas().getInr();
        if (!eblInr.equals(basInr))
        {
            cfagit.getEblmod().getEbl().setObjinr(basInr);
            sysmodService.sdbUpdate(cfagit.getEblmod().getEbl());
        }
        //       记录报送审计操作
        eblmodService.setRecordSig(cfagit,"数据提取");
        sysmodService.sdbInsert(cfagit.getEblmod().getCfasig());
    }

    public void setCfaType(String cfaType){
        this.typeOfCfa = cfaType;
    }

    @Override
    public Cfagit cfaMappingData(Cfagit cfagit) {
        return traMappingCfaDataService.mappingCFADataCFAGIT(cfagit);
    }

    @Override
    public boolean isCheckField(String accept, String typ) {
        return eblmodService.isCheckField(accept,typ);
    }

    @Override
    public Cfagit cfaGetDataFromGID(Cfagit cfagit) {
        return traMappingCfaDataService.cfaGetDataFromGID(cfagit);
    }

    @Override
    public Map<String, Object> cfaGetGIDataByTrans(String transName, BizInfoVo bizInfoVo) {
        transName = transName.toUpperCase();
        Map<String,Object> datasByTrans = new HashMap<>();
        //是否对外担保
        String recFingua = NULLSTR;

        String matchStr = Strings.mid(transName.toUpperCase(), 1, 2);

        switch (matchStr)
        {
            case "GI":
            case "GE":
            case "GC":
                if(transName.equals("GITPSE") || transName.equals("GITSET")){
                    GcdBizInfoVo rec = (GcdBizInfoVo) bizInfoVo;
                    if (!MdaUtils.isEmpty(rec)) {
                        recFingua = rec.getFingua();
                    }
                }else{
                    GidBizInfoVo rec = (GidBizInfoVo) bizInfoVo;
                    if (!MdaUtils.isEmpty(rec)) {
                        recFingua = rec.getFingua();
                    }
                }


                break;
            case "NI":
                if(transName.equals("NITPSE") || transName.equals("NITSET")){
                    NcdBizInfoVo rec = (NcdBizInfoVo) bizInfoVo;
                    if (!MdaUtils.isEmpty(rec)) {
                        recFingua = rec.getFingua();
                    }
                }else{
                    NidBizInfoVo rec = (NidBizInfoVo) bizInfoVo;
                    if (!MdaUtils.isEmpty(rec)) {
                        recFingua = rec.getFingua();
                    }
                }
                break;
        }
        datasByTrans.put("recFingua",recFingua);

        return datasByTrans;
    }

    //复核回退删除已入库对外担保数据
    @Override
    public void cfagitRePair(String trninr){
        if(MdaUtils.isEmpty(trninr)){
            return;
        }
        //20240415 二期调整，增加报送类型判断
        Ebl eblObj = eblMapper.selectByTrninrAndObjtyp(trninr,"EBA");
        if(!MdaUtils.isEmpty(eblObj)){
            String objInr = eblObj.getObjinr();
            String objType = eblObj.getObjtyp();
            String eblInr = eblObj.getInr();

            switch (objType){
                case "EBA":
                    Eba eba = ebaMapper.selectByEblInr(eblInr);
                    Ebc ebc = ebcMapper.selectByEblInr(eblInr);
                    Ebb ebb = ebbMapper.selectByEblInr(eblInr);
                    if(!MdaUtils.isEmpty(eba)){
                        ebaMapper.deleteByPrimaryKey(eba);
                    }
                    if(!MdaUtils.isEmpty(ebc)){
                        ebcMapper.deleteByPrimaryKey(ebc);
                    }
                    if(!MdaUtils.isEmpty(ebb)){
                        ebbMapper.deleteByPrimaryKey(ebb);
                    }
                    eblMapper.deleteByPrimaryKey(eblObj);
                    break;
            }
        }
    }

}
