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


import com.brilliance.isc.bo.Adr;
import com.brilliance.isc.bo.Pta;
import com.brilliance.isc.common.cache.adr.AdrCacheService;
import com.brilliance.isc.common.cache.pta.PtaCacheService;
import com.brilliance.isc.common.codetable.service.CodetableService;
import com.brilliance.isc.common.component.PtsptaToolComponent;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.register.fee.AbstractFeeRegister;
import com.brilliance.isc.common.setmod.vo.SetmodVo;
import com.brilliance.isc.common.trndoc.vo.DoceotVo;
import com.brilliance.isc.common.util.Utils;
import com.brilliance.isc.common.vo.PtsptaVo;
import com.brilliance.isc.mda.dao.CtyMapper;
import com.brilliance.isc.utils.StringUtils;
import com.brilliance.mda.runtime.mda.CodetableItem;
import com.brilliance.mda.runtime.mda.IStream;
import com.brilliance.mda.runtime.mda.impl.StreamImpl;
import com.brilliance.mda.runtime.mda.util.Dates;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import com.brilliance.mda.runtime.mda.util.Streams;
import com.brilliance.mda.runtime.mda.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

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

@Component("setfeg.RegisterSettlementCorrCha")
public class DocumentFeeRegister extends AbstractFeeRegister {

    @Autowired
    private CodetableService codetableService;

    @Autowired
    private PtsptaToolComponent ptsptaToolComponent;

    @Autowired
    private CtyMapper ctyMapper;

    @Autowired
    private AdrCacheService adrCacheService;

    @Autowired
    private PtaCacheService ptaCacheService;

    /**
     * source:setfeg.0035.script
     */
    public void registerSettlement(SetmodVo setmodVo) {
        int couAnz = 0;
        String couSw = "";
        int letAnz = 0;
        String letSw = "";
        int swtAnz = 0;
        String swtSw = "";
        String fintyp = "";
        int line = 0;
        //########国内信用证电报费、快邮费及平邮费####################
        //!电报费DTELEX
        //!开邮费DDHL11
        //!平邮费DPOST
        //!国内信用证的融资交易与国际信用证交易名称一样，需要单独处理。
        //############################################################
        String transName = SettleContext.getDirectTransName();
        IStream dhlfee = new StreamImpl();
        Streams.streamClear(dhlfee);
        String trnNam = Strings.mid(transName, 1, 2);
        String feeCod = NULLSTR;
        if (MdaUtils.compareTo(trnNam, "DI") == 0
                || MdaUtils.compareTo(trnNam, "DE") == 0
                || MdaUtils.compareTo(trnNam, "BD") == 0
                || MdaUtils.compareTo(trnNam, "BF") == 0) {  //国内信用证
            feeCod = "DTELEX";
        }
        if ("LITOPN".equals(transName)
                || "LTTOPN".equals(transName)
                || "GITOPN".equals(transName)
                || "GITPOP".equals(transName)
                || "GITODZ".equals(transName)) {
            /**
             if ( GetModuleInfo( \\, 1 ) == "LITOPN" ) or ( GetModuleInfo( \\, 1 ) == "LTTOPN" ) or ( GetModuleInfo( \\, 1 ) == "GITOPN" ) then
             **/
            feeCod = "TELEXH";
        } else {
            //融资交易单独处理
            if ("TRTOPN".equals(transName)) {
                fintyp = Utils.getFieldValue(setmodVo.getRec(), "fintyp");
                if ("DRF".equals(fintyp)) {   //国内证买方融资
                    feeCod = "DTELEX";
                } else {
                    feeCod = "TELEX";       //国际证进口融资
                }
            }
            if ("BPTOPN".equals(transName)) {
                fintyp = Utils.getFieldValue(setmodVo.getRec(), "fintyp");
                if ("T".equals(fintyp)
                        || "M".equals(fintyp)
                        || "G".equals(fintyp)
                        || "V".equals(fintyp)) {
                    //国内证卖方融资
                    feeCod = "DTELEX";
                } else {
                    //国际证出口融资
                    feeCod = "TELEX";
                }
            }
            //非融资交易，非国内信用证交易
            if (!"TRTOPN".equals(transName) && !"BPTOPN".equals(transName)
                    && (!"DI".equals(trnNam) && !"DE".equals(trnNam)
                    && !"BD".equals(trnNam) && !"BF".equals(trnNam))) {
                feeCod = "TELEX";
            }
        }
        //清理所有与面函相关的收费
        addFeeData( "POST", "", couAnz, Dates.today(), Dates.today() );
        addFeeData( "DHL01", "", letAnz, Dates.today(), Dates.today() );
        addFeeData( "DHL02", "", letAnz, Dates.today(), Dates.today() );
        addFeeData( "DHL03", "", letAnz, Dates.today(), Dates.today() );
        addFeeData( "DHL04", "", letAnz, Dates.today(), Dates.today() );
        addFeeData( "DHL05", "", letAnz, Dates.today(), Dates.today() );
        addFeeData("DHL06", "", letAnz, Dates.today(), Dates.today());
        addFeeData("DHL07", "", letAnz, Dates.today(), Dates.today());
        addFeeData("DHL08", "", letAnz, Dates.today(), Dates.today());
        addFeeData("DHL09", "", letAnz, Dates.today(), Dates.today());
        addFeeData("DHL10", "", letAnz, Dates.today(), Dates.today());
        addFeeData("TELEX", "", couAnz, Dates.today(), Dates.today());
        addFeeData("TELEXH", "", letAnz, Dates.today(), Dates.today());
        addFeeData("DPOST", "", letAnz, Dates.today(), Dates.today());
        addFeeData("DTELE", "", couAnz, Dates.today(), Dates.today());
        addFeeData("DDHL11", "", letAnz, Dates.today(), Dates.today());
        IStream corTypStm = new StreamImpl();
        Streams.streamClear(corTypStm);
        List<CodetableItem> list = codetableService.selectList("CORTYP", "CN");
        list.forEach(item -> corTypStm.addLine(item.getValue()));
        int stmCnt = Streams.streamCount(corTypStm);
        for (int i = 1; i <= stmCnt; i++) {
            Streams.streamSetLine(corTypStm, i, Strings.getLine(corTypStm, i) + TAB + "0");
        }
        if (StringUtils.isEmpty(setmodVo.getSetfeg().getCorchaflg())) {
            List<DoceotVo> doceotVos = setmodVo.getDoceotList();
            for (int i = 0; i < doceotVos.size(); i++) {
                DoceotVo doceot = doceotVos.get(i);
                String delFlg = doceot.getDelflg();
                String lev = doceot.getLev();
                String oriFlg = doceot.getOriflg();
                // do not count suppressed or deleted messages or send-to-copies
                if (!"D".equals(delFlg)
                        && ("1".equals(lev) || (!"1".equals(lev) && "C".equals(oriFlg)))) {
                    String docEotCorTyp = doceot.getCortyp();
                    if ("LET".equals(docEotCorTyp)/* && MdaUtils.isEmpty(ctx.absGet(Setmod.class).getChgpstflg()) */) {
                        if ("LETAML".equals(doceot.getDocsnf())) {
                            couAnz = couAnz + 1;
                            couSw = "Y";
                        }
                        if ("LETCOU".equals(doceot.getDocsnf())) {
                            //按照不同的地区，收取快邮费
                            //   $ptyinr = GetContent( $Eot + "\\\\PTA\\\\PTYINR" )
                            String ptainr = doceot.getRcv().getPts().getPtainr();
                            Adr adr = adrCacheService.selectByPtaInr(ptainr);
                            String loccty = adr.getLoccty();
                            String dhlreg = ctyMapper.getDhlregByCode(loccty);
                            line = Streams.streamSearch(dhlfee, "DHL" + dhlreg);
                            if (line == 0) {
                                Streams.streamInsert(dhlfee, 0, "DHL" + dhlreg);
                                Streams.streamInsert(dhlfee, 0, MdaUtils.toString(1));
                            } else {
                                int value = Strings.parseInt(Strings.getLine(dhlfee, line + 1)) + 1;
                                Streams.streamSetLine(dhlfee, line + 1, MdaUtils.toString(value));
                            }
                            letAnz = letAnz + 1;
                            letSw = "Y";
                        }
                    }
                    if ("SWT".equals(docEotCorTyp) || "TLX".equals(docEotCorTyp)) {
                        swtAnz = swtAnz + 1;
                        swtSw = "Y";
                    }
                }
            }
        }
        boolean dFlg = false;
        if ("DI".equals(trnNam) || "DE".equals(trnNam) || "BD".equals(trnNam)
                || "BF".equals(trnNam) || "T".equals(fintyp) || "M".equals(fintyp)
                || "G".equals(fintyp) || "V".equals(fintyp) || "DRF".equals(fintyp)) {
            dFlg = true;
        }
        // Add correspondence fees
        //收取平邮费
        if (couAnz > 0) {
            //if $TrnNam = "DI" or $TrnNam = "DE" or $TrnNam = "BD" or $TrnNam = "BF" or $FINTYP = "T" or $FINTYP = "M" or $FINTYP = "DRF" then
            if (dFlg) {
                addFeeData("DPOST", "Y", couAnz, Dates.today(), Dates.today());   //国内证交易
            } else {
                addFeeData("POST", "Y", couAnz, Dates.today(), Dates.today()); //只对airmail收取邮费.
            }
        }
        //收取快邮费
        String couexp = NULLSTR;
        String ptyextkey = NULLSTR;
        if (letAnz > 0) {
            //if $TrnNam = "DI" or $TrnNam = "DE" or $TrnNam = "BD" or $TrnNam = "BF" or $FINTYP = "T" or $FINTYP = "M" or $FINTYP = "DRF" then
            if (dFlg) {
                addFeeData("DDHL11", "Y", letAnz, Dates.today(), Dates.today());   //国内证交易
            } else {
                //国际证
                //快邮费分10个地区收取，按目的地不同分开收取
                for (int i = 1; i <= 10; i++) {
                    if (i < 10) {
                        couexp = "DHL0" + MdaUtils.toString(i);
                    } else {
                        couexp = "DHL" + MdaUtils.toString(i);
                    }
                    line = Streams.streamSearch(dhlfee, couexp);
                    if (line != 0) {
                        couSw = "Y";
                        couAnz = Strings.parseInt(Strings.getLine(dhlfee, line + 1));
                        addFeeData(couexp, couSw, couAnz, Dates.today(), Dates.today());
                    }
                }
                ptyextkey = "";
                if ("BOTDAV".equals(transName) || "BOTDAV1".equals(transName) || "CCTDAV".equals(transName)) {
                    PtsptaVo ptsptaVo = ptsptaToolComponent.getPtsFromListByRol(setmodVo.getPtsList(), "COL");
                    if (ptsptaVo != null) {
                        Pta pta = ptaCacheService.getPtaByInr(ptsptaVo.getPtainr());
                        if (pta != null) {
                            ptyextkey = pta.getPtyextkey();
                        }
                    }
                }
                if (StringUtils.isNotEmpty(ptyextkey)) {
                    if (ptyextkey.length() != 11) {
                        addFeeData("DHL01", "Y", letAnz, Dates.today(), Dates.today());
                    }
                }
            }
        }
        //收取电报费
        if (swtAnz > 0) {
            addFeeData(feeCod, "Y", swtAnz, Dates.today(), Dates.today());
        }
    }
}
