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

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.mda.runtime.mda.util.Decimals;
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.CR;

@Component
public class LiaallDiasavZZZRegister implements IDiasavRegister {

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

    @Autowired
    private TrndiaService trndiaService;

    @Override
    public void diasav(TrndiaVo trndiaVo) {
        // handle diaries of tenors
        log.debug("执行表外注册备忘录逻辑");
        List<LiaallgVo> liaallgs = trndiaVo.getLiaallg();
        if (liaallgs == null || liaallgs.size() == 0) {
            log.debug("表外条目为空,直接返回");
            return;
        }
        String lst = "DPU" + CR + "DPZ" + CR + "AKZ";
        liaallgs.forEach(liaallgVo -> {
            if( Strings.searchBlock(lst,liaallgVo.getCbtpfx()) > 0) {
                // close diary if tenor is settled
                if( MdaUtils.compareTo(liaallgVo.getOldamt(),0) != 0 && MdaUtils.compareTo(Decimals.add(liaallgVo.getOldamt(),liaallgVo.getAmt()),0) == 0 ) {
                    trndiaService.trnDiaSubSetDone(trndiaVo, "DOCMAT", liaallgVo.getExtid() + "/" + liaallgVo.getPtegrp().getRec().getSubid());
                } else {
                    // clear donflg if it was set
                    trndiaService.trnDiaSubClearDonFlg(trndiaVo,"DOCMAT", liaallgVo.getExtid() + "/" + liaallgVo.getPtegrp().getRec().getSubid());
                    /**
                     // Diary for maturity of documents, if maturity set
                     if not IsEmpty( LIAALLG( $i )\\MATDAT ) then
                     $diadat = DateAddW( LIAALLG( $i )\\MATDAT, $Diff, GetUIL )
                     if $diadat >= Today then
                     TrnDiaSubSetDia( $Module, "PTE", LIAALLG( $i ).GetTenorId, "DOCMAT", $diadat, "MAT", $Nam, $Frm, DiaToReplace )
                     endif
                     endif
                     **/
                }
            }
        });
        log.debug("执行表外注册备忘录逻辑完成");
    }
}
