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

import com.brilliance.mda.runtime.mda.CodetableItem;
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 com.brilliance.isc.bo.Bch;
import com.brilliance.isc.bo.Cbb;
import com.brilliance.isc.bo.Pty;
import com.brilliance.isc.bo.model.Trdgrp;
import com.brilliance.isc.bo.model.Trtp;
import com.brilliance.isc.bo.model.Wbdp;
import com.brilliance.isc.common.cache.pty.PtyCacheService;
import com.brilliance.isc.common.cbsmod.service.XrtmodService;
import com.brilliance.isc.common.codetable.service.CodetableService;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.glemod.service.GlemodService;
import com.brilliance.isc.common.sysmod.SysmodService;
import com.brilliance.isc.common.trtcre.service.TrtpService;
import com.brilliance.isc.common.util.StringUtils;
import com.brilliance.isc.vo.model.FinmodVo;
import com.brilliance.isc.vo.model.TrtcreBizInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

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

@Service
public class TrtpServiceImpl implements TrtpService {

    @Autowired
    private GlemodService glemodService;

    @Autowired
    private XrtmodService xrtmodService;

    @Autowired
    private CodetableService codetableService;

    @Autowired
    private PtyCacheService ptyCacheService;

    @Autowired
    private SysmodService sysmodService;

    @Override
    public void initWbdcre(Wbdp wbdp) {
        //外汇贷款专户开关
        //为空表示不关闭，为X表示关闭
        boolean flag = sysmodService.isBusinessOpen("FFACTL");
        if (flag) {
            wbdp.setFfactl("");
        } else {
            wbdp.setFfactl("X");
        }
        //String ffactl = Inis.getIniStr("do5para.ini","Transaction","FFACTL");
        //wbdp.setFfactl(ffactl);
    }

    @Override
    public void assertWbdcre(Wbdp wbdp, String cur, String ptyinr) {
        String zmqflg = "";
        Bch bch = SettleContext.getUserSession().getBch();
        if (bch != null) {
            zmqflg = bch.getZmqflg();
        }
        Set<String> actproSet = new HashSet<>();
        if ("X".equalsIgnoreCase(zmqflg)) {
            actproSet.add("3603");
            actproSet.add("3604");
        } else {
            actproSet.add("2303");
            actproSet.add("3400");
        }
        wbdp.setActproSet(actproSet);
        //获取账号下拉列表
        /*Set<String> actSet = new HashSet<>();
        String actval = glemodService.getAvailAcc("LFC", cur, ptyinr, BigDecimal.ZERO);
        if (StringUtils.isNotEmpty(actval)) {
            Arrays.stream(actval.split("[\\r\\n]{1,2}")).forEach(account -> {
                if ("3400".equals(wbdp.getActpro())) {
                    //只添加 NRA 账号
                    if (account.contains("NRA")) {
                        actSet.add(account);
                    }
                } else {
                    actSet.add(account);
                }
            });
        }
        wbdp.setActSet(actSet);*/
    }

    /**
     *   source:trtmod.0001.script
     */
    public void defaultTrdgrpRecOpndatN1000(Trdgrp trdgrp){
        trdgrp.getRec().setOpndat(trdgrp.getRec().getIssdat());
    }

    /**
     *   source:trtmod.0002.script
     */
    public void defaultTrdgrpRecActfindayN1000(Trdgrp trdgrp){
        if(!MdaUtils.isEmpty(trdgrp.getRec().getMatdat()) && ! MdaUtils.isEmpty(trdgrp.getRec().getIssdat()) ) {
            trdgrp.getRec().setActfinday(Dates.diff(trdgrp.getRec().getMatdat(), trdgrp.getRec().getIssdat()));
        } else {
            trdgrp.getRec().setActfinday(0);
        }
    }

    /**
     *   source:trtmod.0015.script
     */
    public void defaultTrdgrpRecLprtypN1000(Trtp trtp, Trdgrp trdgrp, FinmodVo finmodVo){
        String frame = SettleContext.getDirectTransName();
        switch(frame) {
            case "TRTOPN":
            case "BRTSET":
            case  "BCTSET":
            case  "BDTSET":
            case  "CPTOPN":
            case  "CPTATO":
                trtp.setLprtypVisiable("X");
                //客户类型
                String idtype = NULLSTR;
                Pty pty = ptyCacheService.getPtyById(trdgrp.getFip().getPtyinr());
                if (pty != null) {
                    idtype = pty.getIdtype();
                }
                if ("Y".equals(finmodVo.getOpnflg())) {
                    if ("2".equals(idtype)) {
                        trtp.setLprtypDisable("");
                    } else {
                        trtp.setLprtypDisable("X");
                        if( MdaUtils.isEmpty(finmodVo.getDinmod().getRmbsbs()) ) {
                            trdgrp.getRec().setLprtyp(NULLSTR);
                        }
                    }
                } else {
                    trtp.setLprtypDisable("");
                }
                break;
            case "TRTAME":
                trtp.setLprtypVisiable("X");
                trtp.setLprtypDisable("");
                break;
            default:
                trtp.setLprtypVisiable("");
        }
    }

    /**
     *   source:trtp.0002.script
     */
    public void defaultLabmaxamtN1000(Trtp trtp, Trdgrp trdgrp){
        trtp.setLabmaxamt(Strings.trim(Strings.getText("$1 Amount", Strings.mid(trdgrp.getRec().getFintyp(),1,2))));
    }

    /**
     *   source:trtp.0010.script
     */
    public void defaultTrdgrpRecStttendatN1000(Trtp trtp, Trdgrp trdgrp){
        String frme = Strings.mid(SettleContext.getTransName(),4,3);
        if( MdaUtils.compareTo(frme,"ADD") != 0 ) {
            if(!trdgrp.getRec().isModified("stttendat")) {
                trdgrp.getRec().setStttendat(Dates.today());
            }
        }
    }

    /**
     *   source:trtp.0013.script
     */
    public void defaultTrdgrpRecOwnusrN1000(Trdgrp trdgrp){
        //! Set OWNUSR if not already set
        if(MdaUtils.isEmpty(trdgrp.getRec().getOwnusr()) ) {
            trdgrp.getRec().setOwnusr(SettleContext.getUserSession().getUsr().getExtkey());
        }
    }

    /**
     *   source:trtp.0021.script
     */
    public void defaultTrdgrpCbsMaxCurN12000(Trtp trtp, Cbb maxcbb){
        String pntcur = "";
        if (maxcbb != null) {
            pntcur = maxcbb.getCur();
        }
        List<CodetableItem> curSet = new ArrayList<>();
        if (StringUtils.isEmpty(pntcur) || "CNY".equals(pntcur)) {
            curSet = xrtmodService.validCurrencies();
        } else {
            curSet.add(new CodetableItem(codetableService.getTblLabel("CNY", "CURTXT"), "CNY"));
            curSet.add(new CodetableItem(codetableService.getTblLabel(pntcur, "CURTXT"), pntcur));
        }
        trtp.setCurSet(curSet);
    }

    /**
     *   source:trtp.0031.script
     *   TODO 自贸区业务
     */
    /*@Default(target="codsel.codp",order=1000)
    public boolean defaultCodselCodpN1000(){
        IContext ctx = MdaEnv.getContext();

        //--zmqzmq-----
        if( MdaUtils.compareTo(Platform.getzmqflg(),"X") == 0 || MdaUtils.compareTo(ctx.absGet(Sysmod.class).getBch().getZmqflg(),"X") == 0 )
        {
            Systems.setVisible(this.getCodsel(),"codp");
        }
        else
        {
            Systems.resetVisible(this.getCodsel(),"codp");
        }


        return true;
    }*/

    /**
     *   source:trtp.0032.script
     *   TODO 自贸区业务
     */
    /*@Default(target="dxzfp",order=1000)
    public boolean defaultDxzfpN1000(){
        IContext ctx = MdaEnv.getContext();

        String finflg = Datas.getContent(this,"\\TRTCRE\\CREFINFLG");
        int err = ctx.getErrorCode();
        if( MdaUtils.compareTo(Platform.getzmqflg(),"X") == 0 || MdaUtils.compareTo(ctx.absGet(Sysmod.class).getBch().getZmqflg(),"X") == 0 )
        {
            if( MdaUtils.compareTo(ctx.getTransName(),"TRTOPN") == 0 || ( ( MdaUtils.compareTo(ctx.getTransName(),"BRTSET") == 0 || MdaUtils.compareTo(ctx.getTransName(),"BCTSET") == 0 || MdaUtils.compareTo(ctx.getTransName(),"BDTSET") == 0 || MdaUtils.compareTo(ctx.getTransName(),"CPTOPN") == 0 || MdaUtils.compareTo(ctx.getTransName(),"CPTATO") == 0 ) && ! MdaUtils.isEmpty(finflg) ) )
            {
                Systems.setVisible(this,"dxzfp");
            }
            else
            {
                Systems.resetVisible(this,"dxzfp");
            }
        }
        else
        {
            Systems.resetVisible(this,"dxzfp");
        }
        *//**
         if TRDGRP\\REC\\FLOWFLAG == "1" then
         Enable( DXZFP )
         Visible( DXZFP )
         else
         Disable( DXZFP )
         Invisible( DXZFP )
         endif
         **//*

        return true;
    }*/

    /**
     *   source:trtp.0038.script
     *   TODO 自贸区业务
     */
    /*@Default(target="flowflag",order=1000)
    public boolean defaultFlowflagN1000(){
        IContext ctx = MdaEnv.getContext();

        String a = this.getFlowflag();
        if( MdaUtils.compareTo(Platform.getzmqflg(),"X") == 0 || MdaUtils.compareTo(ctx.absGet(Sysmod.class).getBch().getZmqflg(),"X") == 0 )
        {
            Systems.setVisible(this,"flowflag");
            this.setFlowflag(  "是否定向收付");
        }
        else
        {
            Systems.resetVisible(this,"flowflag");
        }


        return true;
    }*/

    /**
     *   source:trtp.0039.script
     *   TODO 自贸区业务
     */
    /*@Default(target="trdgrp.rec.flowflag",order=1000)
    public boolean defaultTrdgrpRecFlowflagN1000(){
        IContext ctx = MdaEnv.getContext();

        String a = this.getTrdgrp().getRec().getFlowflag();
        if( MdaUtils.compareTo(Platform.getzmqflg(),"X") == 0 || MdaUtils.compareTo(ctx.absGet(Sysmod.class).getBch().getZmqflg(),"X") == 0 )
        {
            Systems.setVisible(this.getTrdgrp().getRec(),"flowflag");
        }
        else
        {
            Systems.resetVisible(this.getTrdgrp().getRec(),"flowflag");
        }


        return true;
    }*/

    /**
     *   source:trtp.0042.script
     */
    public void defaultTrdgrpRecFfaflgN1000(Trtp trtp, Trdgrp trdgrp){
        //!新增需求【20211011】:
        //!增加参数控制交易：增加开关控制交易，在制度没下发或其他特殊情况下，可单独关闭“外币贷款专户过账”功能
        //!新增配置项,读取配置项,为空表示不控制，=X表示关闭功能
        if( MdaUtils.isEmpty(trtp.getWbdp().getFfactl()) ) {
            Cbb maxcbb = trdgrp.getCbs().getMax();
            if(MdaUtils.compareTo(maxcbb.getCur(),"CNY") != 0 && !MdaUtils.isEmpty(maxcbb.getCur()) && ! MdaUtils.isEmpty(maxcbb.getAmt())) {
                switch(trdgrp.getRec().getFintyp()) {
                    case "BRF":
                    case "BCF":
                    case "ECF":
                        //Systems.resetEnabled(this.getTrdgrp().getRec(),"ffaflg");
                        trtp.getWbdp().setVisiableFlag("X");
                        trdgrp.getRec().setFfaflg("Y");
                        break;
                    default:
                        //Systems.resetEnabled(this.getTrdgrp().getRec(),"ffaflg");
                        trtp.getWbdp().setVisiableFlag("");
                        trdgrp.getRec().setFfaflg(NULLSTR);
                }
            } else {
                trdgrp.getRec().setFfaflg(NULLSTR);
                trtp.getWbdp().setVisiableFlag("");
                //Systems.resetEnabled(this.getTrdgrp().getRec(),"ffaflg");
            }
        } else {
            trdgrp.getRec().setFfaflg(NULLSTR);
            trtp.getWbdp().setVisiableFlag("");
            //Systems.resetEnabled(this.getTrdgrp().getRec(),"ffaflg");
        }

    }

    /**
     *   source:trtp.0045.script
     */
    public void defaultTrdgrpRecGoddspcnN1000(String crefinflg, TrtcreBizInfoVo rec, Trdgrp trdgrp){
        if ("TRTOPN".equalsIgnoreCase(SettleContext.getTransName()) && "X".equals(crefinflg)) {
            if (StringUtils.isEmpty(trdgrp.getRec().getGoddspcn()) && !trdgrp.getRec().isModified("goddspcn")) {
                trdgrp.getRec().setGoddspcn(rec.getGoddspcn());
            }
        } else {
            if ("BRD".equals(trdgrp.getRec().getPnttyp()) || "BCD".equals(trdgrp.getRec().getPnttyp())) {
                if (StringUtils.isEmpty(trdgrp.getRec().getGoddspcn()) && !trdgrp.getRec().isModified("goddspcn")) {
                    trdgrp.getRec().setGoddspcn(rec.getGoddspcn());
                }
            }
        }
    }
}
