package com.brilliance.isc.common.transaction.cecm.service.impl;

import com.brilliance.isc.bo.*;
import com.brilliance.isc.bo.batch.Batopn;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.counter.service.CounterService;
import com.brilliance.isc.common.transaction.cecm.service.AbstractBatchStoreService;
import com.brilliance.isc.common.transaction.help.TransactionHelpService;
import com.brilliance.isc.common.transaction.help.TransactionServiceSet;
import com.brilliance.isc.common.trnmod.vo.TrnmodVo;
import com.brilliance.isc.common.util.StringUtils;
import com.brilliance.isc.common.util.Utils;
import com.brilliance.isc.mda.dao.*;
import com.brilliance.isc.utils.DateUtils;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("git_opn.cecmbatch")
public class GuaranteeOpnBatchStoreServiceImpl extends AbstractBatchStoreService {
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    @Resource
    private BatopnMapper batchCecmGidOpnMapper;

    @Resource
    private CciMapper cciMapper;

    @Resource
    private PtaMapper ptaMapper;

    @Resource
    private CbeMapper cbeMapper;

    @Resource
    private LimMapper limMapper;

    @Resource
    private CounterService counterService;

    @Resource
    private TransactionHelpService transactionHelpService;

    @Override
    public void txnSave(TrnmodVo trnmod){
        if("-GITOPN-NITOPN-HITOPN-GITAME-NITAME-HITAME-".indexOf(trnmod.getTrn().getInifrm())<=0){
            return;
        }
        Trn trn=trnmod.getTrn();
        TransactionServiceSet transactionStoreSet =transactionHelpService.getTransactionStoreSet();
        Lim lim=transactionStoreSet.getLimmodVo().getLim();
        if(lim==null || (lim!=null && StringUtils.isEmpty(lim.getInr()))){
            lim=limMapper.selectCecmOpnLim(trn.getOwnref(),trn.getObjtyp());
        }
        Object rec=transactionStoreSet.getRec();
        Batopn gitopnUnl=batchCecmGidOpnMapper.selectByOwnref(trn.getOwnref());
        if(gitopnUnl==null){
            gitopnUnl=new Batopn();
        }
        String wrkbch= SettleContext.getUserSession().getWrkbch().getBranch();
        String accbch= SettleContext.getUserSession().getBch().getBranch();
        String gartyp=Utils.getFieldValue(rec, "gartyp");
        Date opndat=Utils.getFieldValue(rec, "opndat");
        Date expdat=Utils.getFieldValue(rec, "expdat");
        BigDecimal cshpct=Utils.getFieldValue(rec, "cshpct");
        String fenlishi=Utils.getFieldValue(rec, "fenlishi");
        String guaflg = Utils.getFieldValue(rec, "guaflg");
        if(MdaUtils.isEmpty(gitopnUnl.getInr())){
            gitopnUnl.setTrninr(trn.getInr());
            gitopnUnl.setOwnref(trn.getOwnref());
            gitopnUnl.setBranchinr(wrkbch);
            gitopnUnl.setAccbchinr(accbch);
            gitopnUnl.setPtyextkey(getPtyExtkey(trn));
            gitopnUnl.setCtmnam(trn.getObjnam());
            Cbe cbe=getCecmCbe(trn);
            gitopnUnl.setCur(cbe.getCur());
            gitopnUnl.setAmt(cbe.getAmt());
            gitopnUnl.setCcvact(getCshAct(trn));
            gitopnUnl.setCshpct(cshpct);
            if(lim!=null) {
                gitopnUnl.setLimtype(lim.getLimtype());
                gitopnUnl.setLimref(lim.getLimref());
            }
            gitopnUnl.setGartyp(getGidTrnCodeOwnref(gartyp));
            gitopnUnl.setOpndat(DateUtils.parseDateToStr("yyyyMMdd", opndat));
            gitopnUnl.setExpdat(DateUtils.parseDateToStr("yyyyMMdd", expdat));
            gitopnUnl.setGuafig(guaflg);
            gitopnUnl.setCode(getCode(gartyp,rec));
            gitopnUnl.setSeparationflag(getFenlishi(fenlishi));
            gitopnUnl.setInr(counterService.dbCounterFormat("BATOPN"));
            batchCecmGidOpnMapper.insert(gitopnUnl);
        }else{
            gitopnUnl.setOpndat(DateUtils.parseDateToStr("yyyyMMdd", opndat));
            gitopnUnl.setExpdat(DateUtils.parseDateToStr("yyyyMMdd", expdat));
            gitopnUnl.setGuafig(guaflg);
            batchCecmGidOpnMapper.updateByPrimaryKey(gitopnUnl);
        }
    }

    @Override
    public void repair(Trn trn) {
        if("-NITOPN-GITOPN-HITOPN-".indexOf(trn.getInifrm())<=0) {
           return;
        }
        batchCecmGidOpnMapper.deleteByTrninr(trn.getInr());
    }

    public String  getCode(String gartyp, Object rec){
        String gidtyp = "0158";
        if (gartyp.startsWith("U")) {
            gidtyp = "2747";
        }
        // 加贸保函业务本金代号定义为"2743"
        if (gartyp.startsWith("X")) {
            gidtyp = "2743";
        }
        // 20160626,应青姐业务要求，当保函类型为银行信贷证明时，业务代号传2239
        if (gartyp.startsWith("V")) {
            gidtyp = "2239";
        }
        if (gartyp.startsWith("F")) {
            String segtyp=Utils.getFieldValue(rec, "segtyp");
            // 201809汇总征税保函科目同银关保取2747
            if (segtyp!=null && segtyp.startsWith("02")) {
                gidtyp = "2747";
            }
        }
        return gidtyp;
    }

   public String getFenlishi(String fenlishi){
        if(StringUtils.isEmpty(fenlishi) || "0".equals(fenlishi)){
            return "0";
        }else{
            return "1";
        }
   }

    public String getPtyExtkey(Trn argTRN){
        Pta pta =new Pta();
        Cci cci =cciMapper.getCciByObjinrAndObjTyp(argTRN.getObjinr(),argTRN.getObjtyp());
        if(cci!=null) {
            pta.setInr(cci.getCcicusptainr());
            pta = ptaMapper.selectByPrimaryKey(pta);
            return pta.getPtyextkey();
        }
        return "";
    }
    public String getGidTrnCodeOwnref(String gartyp) {
        String trncode; // 业务品种
        if (gartyp.startsWith("A")) {
            trncode = "05140201";
        } else if (gartyp.startsWith("B")) {
            trncode = "05140202";
        } else if (gartyp.startsWith("C")) {
            trncode = "05140203";
        } else if (gartyp.startsWith("D")) {
            trncode = "05140204";
        } else if (gartyp.startsWith("F")) {
            trncode = "05140206";
        } else if (gartyp.startsWith("2")) {
            trncode = "05140210";
        } else if (gartyp.startsWith("G")) {
            trncode = "05140101";
        } else if (gartyp.startsWith("H")) {
            trncode = "05140102";
        } else if (gartyp.startsWith("J")) {
            trncode = "05140205";
        } else if (gartyp.startsWith("S")) {
            trncode = "05140104";
        } else if (gartyp.startsWith("T")) {
            trncode = "05140103";
        } else if (gartyp.startsWith("U")) {
            trncode = "05140207";
        } else if (gartyp.startsWith("V")) {
            trncode = "051419";
        } else if (gartyp.startsWith("X")) {
            trncode = "05140208";
        } else if (gartyp.startsWith("W")) {
            trncode = "05140105";
        } else {
            trncode = "05140299";
        }
        return trncode;
    }

    public String  getCshAct(Trn argTrn){
        List<Cbe> cbeList =cbeMapper.selectByObjtypAndObjinrAndCbtAndTrninr(argTrn.getObjtyp(),argTrn.getObjinr(),"CCVIN",argTrn.getInr());
        if(cbeList!=null && cbeList.size()>0){
            return cbeList.get(0).getAcc();
        }
        return "";
    }

    public Cbe getCecmCbe(Trn argTrn){
        List<String> extidList=new ArrayList<>();
        extidList.add("AMT1");
        extidList.add("AMT2");
        Cbe cbe =cbeMapper.selectCecmCbe(argTrn.getObjtyp(),argTrn.getObjinr(),argTrn.getInr(),"MAXAMT",extidList);
        if(cbe==null){
            return new Cbe();
        }
        return cbe;
    }
}
