package com.brilliance.isc.common.component;

import com.brilliance.isc.bo.Bch;
import com.brilliance.isc.bo.Pty;
import com.brilliance.isc.bo.model.Trdgrp;
import com.brilliance.isc.common.cache.pty.PtyCacheService;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.counter.service.CounterService;
import com.brilliance.isc.common.trnmod.service.TrnmodService;
import com.brilliance.isc.common.util.StringUtils;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class TrtmodComponent {

    @Autowired
    private PtyCacheService ptyCacheService;

    @Autowired
    private CounterService counterService;

    @Autowired
    private TrnmodService trnmodService;

    /**
     *   source:trtmod.0007.script
     */
    public int initrdten(String ptyinr) {
        int allow = 90;
        Pty pty = ptyCacheService.getPtyById(ptyinr);
        if (pty != null && StringUtils.isNotEmpty(pty.getTrdcndflg())) {
            if (pty.getTrdtenini() != 0) {
                allow = pty.getTrdtenini();
            }
        }
        return allow;
    }

    /**
     *   source:trtmod.0006.script
     */
    public int maxextten(String ptyinr) {
        int allow = 365;
        Pty pty = ptyCacheService.getPtyById(ptyinr);
        if (pty != null && StringUtils.isNotEmpty(pty.getTrdcndflg())) {
            if (pty.getTrdtenext() != 0) {
                allow = pty.getTrdtenext();
            }
        }
        return allow;
    }
    /**
     *   source:trtmod.0003.script
     *   警告信息
     */
    public String chktotten(int totaldays, String ptyinr) {
        // 分行需求汇总，进口押汇大于365天不出错，只会出warning
        int allow = 365;
        Pty pty = ptyCacheService.getPtyById(ptyinr);
        if (pty != null) {
            if (StringUtils.isNotEmpty(pty.getTrdcndflg())) {
                allow = pty.getTrdtentot();
            }
        }
        if (totaldays > allow) {
            return Strings.getText("融资天数$2天，已经超过$1天", allow, totaldays);
        }
        return "";
    }
    /**
     *   source:trtmod.0000.script
     */
    public void sav(Trdgrp trdgrp) {
        // saves contract including texts. CBS resp. of transaction
        if(MdaUtils.isEmpty(trdgrp.getRec().getCredat())) {
            trdgrp.getRec().setCredat(Dates.today());
        }
        trnmodService.trnStoreContract(trdgrp);
    }

    /**
     *   source:trtmod.0008.script
     *   获取参考编号
     */
    public String getref(String ownref, String fintyp, String dflg) {
        //>>>ZL:FRA000052-相关交易增加按钮GETREF--------
        //<<<ZL: END
        //! Set reference number, if not already set (usually called in transaction`s MTABUT\\SAV Event)
        Bch wrkbch = SettleContext.getUserSession().getWrkbch();
        if(StringUtils.isEmpty(ownref)) {
            ownref = generateDefaultOwnref("TR");
        }
        String finFlg = "";
        if(ownref.length() != 13) {
            //  进口信用证加L, 进口代收加C，TT押汇的加T
            switch(fintyp) {
                case "BRF":
                    finFlg = "L";
                    break;
                case "BCF":
                    finFlg = "C";
                    break;
                case "ECF":
                    finFlg = "T";
                    break;
            }
            /**
             if LIDGRP\\REC\\DFLG <> "D" then

             **/
            if( MdaUtils.compareTo(dflg, "D") != 0) {
                ownref = Strings.mid(ownref,1,12) + wrkbch.getBchflg() + finFlg;
            } else {
                ownref = Strings.mid(ownref,1,12) + wrkbch.getBchflg() + finFlg + "D";
            }
        }
        return ownref;
    }

    private String generateDefaultOwnref(String trn) {
        StringBuilder ownRefBufferBuilder = new StringBuilder();
        String branch = getBranchRef();
        // 交易代码 比如 TR，
        ownRefBufferBuilder.append(trn);

        // 4位分行编码
        ownRefBufferBuilder.append(branch);

        // 2位年份
        ownRefBufferBuilder.append(Strings.mid(MdaUtils.toString(Dates.getYear(Dates.today()), 4), 3, 2));

        // 4位流水号
        ownRefBufferBuilder.append(MdaUtils.toString(counterService.dbCounter(trn + branch), -4));

        return ownRefBufferBuilder.toString();
    }

    /**
     * 获得机构所属的分行编号
     * 若分行编号不为空，则直接返回，否则返回机构编号.
     *
     * @param
     * @return
     */
    private String getBranchRef() {
        Bch bch = SettleContext.getUserSession().getBch();
        if (bch != null) {
            return bch.getBranch();
        }
        return "";
    }
}
