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

import com.brilliance.isc.common.component.PtsptaToolComponent;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.liaallg.vo.LiaallgVo;
import com.brilliance.isc.common.register.diasav.IDiasavRegister;
import com.brilliance.isc.common.trndia.service.TrndiaService;
import com.brilliance.isc.common.trndia.vo.TrndiaVo;
import com.brilliance.isc.common.util.StringUtils;
import com.brilliance.isc.common.vo.PtsptaVo;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

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

@Component
public class LiaallDiasavRegister implements IDiasavRegister {

    private static Logger log = LoggerFactory.getLogger(LiaallDiasavRegister.class);

    @Autowired(required = false)
    private TrndiaService trndiaService;

    @Autowired
    private PtsptaToolComponent ptsptaToolComponent;

    @Override
    public void diasav(TrndiaVo trndia) {
        //! store diaries (called from TRNMOD\\TRNDIA).
        //! in the moment only diaries for reduction plan are stored
        log.debug("执行表外注册备忘录逻辑");
        List<LiaallgVo> liaallgs = trndia.getLiaallg();
        if (liaallgs == null || liaallgs.size() == 0) {
            log.debug("表外条目为空,直接返回");
            return;
        }
        String frmename = StringUtils.isEmpty(SettleContext.getTransName())?"":SettleContext.getTransName().substring(0,2);
        String ref = NULLSTR;
        String rmb = NULLSTR;
        switch( frmename ) {
            case  "DI":
            case  "DE":
            case  "BD":
            case  "BF": //国内信用证
                for (int i=0; i<liaallgs.size(); i++) {
                    LiaallgVo liaallg = liaallgs.get(i);
                    if((MdaUtils.compareTo(liaallg.getCbtpfx(),"DPU") == 0
                            || MdaUtils.compareTo(liaallg.getCbtpfx(),"AKZ") == 0
                            || MdaUtils.compareTo(liaallg.getCbtpfx(),"DPZ") == 0)
                        && (MdaUtils.compareTo(liaallg.getAmt(),0)>0)
                        && MdaUtils.isEmpty(liaallg.getOldflg())
                        && !MdaUtils.isEmpty(liaallg.getMatdat())) {
                        /**
                         if LIAALLG( $i )\\BUSSEC == "BR" then
                         **/
                        if( MdaUtils.compareTo(liaallg.getBussec(),"BD") == 0 ) {
                            String cur = trndia.getSetcur();
                            switch(cur) {
                                case "USD":
                                case "EUR":
                                case "HKD":
                                    trndiaService.trnDiaSet(trndia,"RQTP" + MdaUtils.toString(i,-2), liaallg.getMatdat(), "FOL", Strings.getText("国内信用证买方远期到期付款提示:$1",liaallg.getPtegrp().getRec().getOwnref()), "BDTSET", DiaToWrite );
                                    break;
                                default:
                                    trndiaService.trnDiaSet(trndia,"RQTP" + MdaUtils.toString(i,-2), Dates.dateAddW(liaallg.getMatdat(),-1,"CN"), "FOL", Strings.getText("国内信用证买方远期到期付款提示:$1",liaallg.getPtegrp().getRec().getOwnref()), "BDTSET", DiaToWrite );
                            }
                        }
                        if( MdaUtils.compareTo(liaallg.getBussec(),"BF") == 0 ) {
                            /**
                             if LIAALLG( $i )\\BUSSEC == "BE" then
                             {
                             TrnDiaSet( GetModule( LIAALLG( $i )\\OBJMODPATH ), "BET" + Str( $i, -3 ), DateAddW( LIAALLG( $i )\\MATDAT, 7, GetUIL ), "CLM", GetText( 'LF000364', LIAALLG( 1 )\\PTEGRP\\REC\\OWNREF ), "BETSET", DiaToWrite )
                             **/
                            trndiaService.trnDiaSet(trndia,"BET" + MdaUtils.toString(i,-3), liaallg.getMatdat(), "CLM", Strings.getText("国内信用证卖方远期到期收款提示:$1",liaallg.getPtegrp().getRec().getOwnref()), "BFTSET", DiaToWrite );
                        }
                    }
                }
                break;
            default: //国际信用证
                //#########刘文青需求
                //1、有偿付行的时候，只出给偿付行的diary，否则只出给开证行
                //2、不分新索和电索，只出一条--远期信用证索汇提示
                //#########
                for (int i=0; i<liaallgs.size(); i++) {
                    LiaallgVo liaallg = liaallgs.get(i);
                    if((MdaUtils.compareTo(liaallg.getCbtpfx(),"DPU") == 0
                            || MdaUtils.compareTo(liaallg.getCbtpfx(),"AKZ") == 0
                            || MdaUtils.compareTo(liaallg.getCbtpfx(),"DPZ") == 0)
                        && (MdaUtils.compareTo(liaallg.getAmt(),0)>0)
                            && MdaUtils.isEmpty(liaallg.getOldflg())
                            && !MdaUtils.isEmpty(liaallg.getMatdat())) {
                        if(MdaUtils.compareTo(liaallg.getBussec(),"BC") == 0) {
                            if(Strings.len(liaallg.getPtegrp().getRec().getOwnref()) > 14) {
                                ref = Strings.mid(liaallg.getPtegrp().getRec().getOwnref(),1,Strings.len(liaallg.getPtegrp().getRec().getOwnref()) - 4);
                            } else {
                                ref = liaallg.getPtegrp().getRec().getOwnref();
                            }
                            trndiaService.trnDiaSet(trndia,"BCTPAY", liaallg.getMatdat(), "CLM", Strings.getText("进口代收远期付款提示:$1",ref), "BCTSET", DiaToWrite );
                        }
                        if( MdaUtils.compareTo(liaallg.getBussec(),"BE") == 0 ) {
                            //rmb = Datas.getContent(this,"\\LEDGRP\\RMB\\PTS\\EXTKEY");
                            PtsptaVo ptsptaVo = ptsptaToolComponent.getPtsFromListByRol(trndia.getPtsList(), "RMB");
                            if (ptsptaVo != null) {
                                rmb = ptsptaVo.getExtkey();
                            }
                            if(!MdaUtils.isEmpty(rmb)) {
                                trndiaService.trnDiaSet(trndia,"BET" + MdaUtils.toString(i,-3), Dates.dateAddW(liaallg.getMatdat(),7,"CN"), "CLM", Strings.getText("远期信用证索汇提示（偿付行）:$1", liaallg.getPtegrp().getRec().getOwnref()), "BETSET", DiaToWrite );
                            } else {
                                trndiaService.trnDiaSet(trndia,"CLMI" + MdaUtils.toString(i,-2), Dates.dateAddW(liaallg.getMatdat(),7,"CN"), "CLM", Strings.getText("远期信用证索汇提示（开证行）:$1", liaallg.getPtegrp().getRec().getOwnref()), "BETSET", DiaToWrite );
                            }
                        }
                        if( MdaUtils.compareTo(liaallg.getBussec(),"BR") == 0 ) {
                            String cur = trndia.getSetcur();
                            switch(cur) {
                                case "USD":
                                case "EUR":
                                case "HKD":
                                case "CNY":
                                    trndiaService.trnDiaSet(trndia,"RQTP" + MdaUtils.toString(i,-2), liaallg.getMatdat(), "FOL", Strings.getText("远期信用证到期付款提示:$1",liaallg.getPtegrp().getRec().getOwnref()), "BRTSET", DiaToWrite );
                                    break;
                                default:
                                    trndiaService.trnDiaSet(trndia,"RQTP" + MdaUtils.toString(i,-2), Dates.dateAddW(liaallg.getMatdat(),-1,"CN"), "FOL", Strings.getText("远期信用证到期付款提示:$1",liaallg.getPtegrp().getRec().getOwnref()), "BRTSET", DiaToWrite );
                            }
                        }
                    }
                }
        }
        log.debug("执行表外注册备忘录逻辑完成");
    }
}
