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

import com.brilliance.isc.common.glemod.service.GlemodService;
import com.brilliance.isc.common.register.settle.ISettleRegister;
import com.brilliance.isc.common.setglg.service.SetglgService;
import com.brilliance.isc.common.setmod.service.SetmodService;
import com.brilliance.isc.common.setmod.vo.SetmodVo;
import com.brilliance.isc.vo.component.dftcre.DftcreVo;
import com.brilliance.isc.vo.model.CbbVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Component("dftcre.settleRegister")
public class DftcreSettleRegister implements ISettleRegister {

    @Autowired
    private SetglgService setglgService;

    @Autowired
    private GlemodService glemodService;

    @Autowired
    private SetmodService setmodService;

    @Override
    public void registerSettlement(SetmodVo setmodVo) {
        DftcreVo dftcreVo = setmodVo.getRec().getDftcreVo();
        if (dftcreVo != null && "X".equalsIgnoreCase(dftcreVo.getDfflag())) {
            setglgService.setglgClrAmount(setmodVo.getSetglg(), "DFTCRE");
            String trntyp = dftcreVo.getTrntyp();
            if ("A".equalsIgnoreCase(trntyp) || "B".equalsIgnoreCase(trntyp)) {
                CbbVo maxcbb = dftcreVo.getDfdgrp().getCbs().getMax();
                if (maxcbb.getAmt() == null) {
                    maxcbb.setAmt(BigDecimal.ZERO);
                }
                String dfuAcc = glemodService.getAvailAcc("AR-TT3", maxcbb.getCur(), "", maxcbb.getAmt());
                setglgService.setglgAddAmount(setmodVo.getSetglg(), "DFTCRE", "DFF", dftcreVo.getOricur(), maxcbb.getAmt().negate(), "DFF", "ARC", "A", dfuAcc, "");
                setmodService.setPtyBasics(setmodVo, "DFF", dftcreVo.getDfdgrp().getDff().getPtyinr(), dftcreVo.getDfdgrp().getDff().getPtainr(), dftcreVo.getDfdgrp().getDff().getNam());
            }
        }
    }
}
