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

import com.brilliance.isc.common.cbsmod.service.XrtmodService;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.register.fee.AbstractFeeRegister;
import com.brilliance.isc.common.setmod.service.SetmodService;
import com.brilliance.isc.common.setmod.vo.SetmodVo;
import com.brilliance.isc.common.util.Utils;
import com.brilliance.isc.common.vo.SimpleModuleVo;
import com.brilliance.isc.vo.component.dftcre.DftcreVo;
import com.brilliance.mda.runtime.mda.util.Dates;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

import static com.brilliance.mda.runtime.mda.Constants.CR;

@Component("dftcre.feeRegister")
public class DftcreFeeRegister extends AbstractFeeRegister {

    @Autowired
    private SetmodService setmodService;

    @Autowired
    private XrtmodService xrtmodService;

    @Override
    public void registerSettlement(SetmodVo setmodVo) {
        DftcreVo dftcreVo = setmodVo.getRec().getDftcreVo();
        if (dftcreVo != null && "X".equalsIgnoreCase(dftcreVo.getDfflag())) {
            String trntyp = dftcreVo.getTrntyp();
            if ("C".equalsIgnoreCase(trntyp) || "D".equalsIgnoreCase(trntyp)) {
                Date feedat1 = Dates.today();
                Date feedat2 = Dates.today();
                String licomsw = "Y";
                int licomun = 1;
                String objmod = Utils.toJson(SimpleModuleVo.buildModule(dftcreVo.getDfdgrp().getRec().getInr(), "DFD"));
                addFeeData("DFFFEE", licomsw, licomun, feedat1, feedat2, "OBJMOD="+objmod);
                setmodVo.setClisetopt("");
                if(MdaUtils.compareTo(SettleContext.getDirectTransName(),"BCTSET") == 0) {
                    setmodVo.setFeeclirol("DRE");
                } else {
                    setmodVo.setFeeclirol("APL");
                }
            }
            if ("A".equalsIgnoreCase(trntyp) || "B".equalsIgnoreCase(trntyp)) {
                Date feedat1 = dftcreVo.getDfdgrp().getRec().getStttendat();
                Date feedat2 = dftcreVo.getDfdgrp().getRec().getStttendat();
                String licomsw = "Y";
                int licomun = 1;
                String objmod = Utils.toJson(SimpleModuleVo.buildModule(dftcreVo.getDfdgrp().getRec().getInr(), "DFD"));
                String roldbt = "APL";
                addFeeData("PCINT", licomsw, licomun, feedat1, feedat2, "OBJMOD=" + objmod + CR + "ROLDBT=" + roldbt);
                setmodService.setDftFeecur(roldbt, xrtmodService.sysiso(), setmodVo);
            }
        }
    }
}
