package com.brilliance.isc.common.register.document.common;

import com.brilliance.isc.common.component.DyncDaoService;
import com.brilliance.isc.common.component.PtsptaToolComponent;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.register.document.IDocumentRegister;
import com.brilliance.isc.common.trndoc.service.TrndocService;
import com.brilliance.isc.common.trndoc.vo.TrndocVo;
import com.brilliance.isc.common.util.Utils;
import com.brilliance.isc.common.vo.SimpleModuleVo;
import com.brilliance.isc.vo.component.bptbck.BptbckVo;
import com.brilliance.isc.vo.component.trtcre.TrtcreVo;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

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

@Component
public class FinancDocumentRegister implements IDocumentRegister {

    @Autowired
    private TrndocService trndocService;

    @Autowired
    private DyncDaoService dyncDaoService;

    @Autowired
    private PtsptaToolComponent ptsptaToolComponent;

    @Override
    public void registerDocument(TrndocVo trndoc) {
        String frame = SettleContext.getDirectTransName();
        String finance = "";
        String rol = "";
        SimpleModuleVo simpleModule = SimpleModuleVo.buildModule(trndoc.getRec());
        if( MdaUtils.compareTo(frame,"CPTOPN") == 0 || MdaUtils.compareTo(frame,"CPTATO") == 0 ) {
            TrtcreVo trtcreVo = trndoc.getRec().getTrtcreVo();
            if (trtcreVo != null) {
                finance = trtcreVo.getCrefinflg();
            }
            rol = "ORC";
        }
        if( MdaUtils.compareTo(frame,"BRTSET") == 0 ) {
            TrtcreVo trtcreVo = trndoc.getRec().getTrtcreVo();
            if (trtcreVo != null) {
                finance = trtcreVo.getCrefinflg();
            }
            rol = "APL";
        }
        //国内证
        /**
         if $frame == "BDTSET" then
         $finance = GetContent( "\\\\TRTCRE\\\\CREFINFLG" )
         $rolflg = GetContent( "\\\\BRDGRP\\\\APL\\\\ROLSETFLG" )
         $module = GetModule( "\\\\BRDGRP" )
         $rol = "APL"
         endif

         **/
        if( MdaUtils.compareTo(frame,"BDTSET") == 0 ) {
            TrtcreVo trtcreVo = trndoc.getRec().getTrtcreVo();
            if (trtcreVo != null) {
                finance = trtcreVo.getCrefinflg();
            }
            rol = "APL";
        }
        if( MdaUtils.compareTo(frame,"BCTSET") == 0 ) {
            TrtcreVo trtcreVo = trndoc.getRec().getTrtcreVo();
            if (trtcreVo != null) {
                finance = trtcreVo.getCrefinflg();
            }
            rol = "DRE";
        }
        BigDecimal amt = BigDecimal.ZERO;
        if(MdaUtils.compareTo(frame,"BETSET") == 0 || MdaUtils.compareTo(frame,"BFTSET") == 0) {
            BptbckVo bptbckVo = trndoc.getRec().getBptbckVo();
            if (bptbckVo != null && bptbckVo.getBptbckg() != null && bptbckVo.getBptbckg().size() > 0) {
                amt = bptbckVo.getBptbckg().get(0).getBckamt();
            }
            if(MdaUtils.compareTo(amt,0) > 0) {
                finance = "P";
            }
            rol = "APL";
        }
        if(MdaUtils.compareTo(frame,"BOTSET") == 0){
            BptbckVo bptbckVo = trndoc.getRec().getBptbckVo();
            if (bptbckVo != null && bptbckVo.getBptbckg() != null && bptbckVo.getBptbckg().size() > 0) {
                amt = bptbckVo.getBptbckg().get(0).getBckamt();
            }
            if(MdaUtils.compareTo(amt,0) > 0) {
                finance = "P";
            }
            rol = "DRE";
        }
        if( MdaUtils.compareTo(frame,"BPTOPN") == 0 || MdaUtils.compareTo(frame,"BPTAME") == 0 || MdaUtils.compareTo(frame,"BPTSET") == 0 ) {
            /**
             if GetContent( "\\\\BPDGRP\\\\REC\\\\FINTYP" ) <> "F" and GetContent( "\\\\BPDGRP\\\\REC\\\\FINTYP" ) <> "U" then

             **/
            finance = "X";
            rol = "FIA";
        }
        if( MdaUtils.compareTo(frame,"TRTOPN") == 0 || MdaUtils.compareTo(frame,"TRTAME") == 0 || MdaUtils.compareTo(frame,"TRTSET") == 0 ) {
            finance = "X";
            rol = "FIP";
        }
        String stitle = NULLSTR;
        String txt = NULLSTR;
        switch(frame) {
            case "CPTOPN":
            case  "CPTATO":
                stitle = "融资业务放款通知书";
                break;
            case "BCTSET":
            case  "BRTSET":
            case  "TRTOPN":
            case  "BPTOPN":
                stitle = "融资业务放款通知书";
                break;
            case "BPTAME":
                stitle = "融资业务修改通知书";
                break;
            case "BPTSET":
            case  "BETSET":
            case  "BOTSET":
            case  "BFTSET":
                stitle = "融资业务还款通知书";
                break;
            case "BDTSET":
                stitle = "国内信用证融资业务放款通知书";
                break;
            case "TRTSET":
                txt = dyncDaoService.dyncReadFieldByInr(trndoc.getRec().getObjtyp(), trndoc.getRec().getObjinr(), "pntref");
                if( MdaUtils.compareTo(txt, "DD") == 0 ) {
                    stitle = "国内信用证融资业务还款通知书";
                } else {
                    stitle = "融资业务还款通知书";
                }
                break;
            case "TRTAME":
                txt = dyncDaoService.dyncReadFieldByInr(trndoc.getRec().getObjtyp(), trndoc.getRec().getObjinr(), "pntref");
                if( MdaUtils.compareTo(txt, "DD") == 0 ) {
                    stitle = "国内信用证融资修改通知书";
                } else {
                    stitle = "融资业务修改通知书";
                }
                break;
            default:
        }
        if( MdaUtils.compareTo(finance,"X") == 0 ) {
            if (ptsptaToolComponent.ptsIsRolSet(trndoc.getPtsptas(), rol)) {
                trndocService.defDocEot(trndoc,"TRN", "APL1", "\\TRTAPLL1", "LET", 0, "", Utils.toJson(simpleModule), stitle, rol, "" );
            }
        } else if( MdaUtils.compareTo(finance,"P") == 0 ) {
            if (ptsptaToolComponent.ptsIsRolSet(trndoc.getPtsptas(), rol)) {
                trndocService.defDocEot(trndoc,"TRN", "APL1", "\\PAYAPLL1", "LET", 0, "", Utils.toJson(simpleModule), stitle, rol, "" );
            }
        }
    }
}
