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

import com.brilliance.isc.common.context.SettleContext;
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.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 com.brilliance.mda.runtime.mda.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

@Component
public class DftcreDiasavRegister implements IDiasavRegister {

    @Autowired
    private TrndiaService trndiaService;

    @Override
    public void diasav(TrndiaVo trndia) {
        DftcreVo dftcreVo = trndia.getRec().getDftcreVo();
        if (dftcreVo != null && "X".equalsIgnoreCase(dftcreVo.getDfflag())) {
            String inifrm = Strings.mid(SettleContext.getTransName(), 1, 2);
            switch (inifrm) {
                case "BR":
                case "BC":
                case "TR":
                case "BP":
                case "BD":
                    SimpleModuleVo linkModule = SimpleModuleVo.buildModule(dftcreVo.getDfdgrp().getRec().getInr(), "DFD");
                    if (MdaUtils.compareTo(dftcreVo.getDfdgrp().getCbs().getMax().getCur(), "USD") == 0
                            || MdaUtils.compareTo(dftcreVo.getDfdgrp().getCbs().getMax().getCur(), "CNY") == 0
                            || MdaUtils.compareTo(dftcreVo.getDfdgrp().getCbs().getMax().getCur(), "EUR") == 0
                            || MdaUtils.compareTo(dftcreVo.getDfdgrp().getCbs().getMax().getCur(), "HKD") == 0) {
                        trndiaService.trnDiaSet(trndia, "DFTDUE", dftcreVo.getDfdgrp().getRec().getMatdat(), "PSD", Strings.getText("委托方代付到期还款提示：$1", dftcreVo.getDfdgrp().getRec().getOwnref()), "DFTSET", DiaToWrite, linkModule, dftcreVo.getDfdgrp().getRec().getOwnref());
                    } else {
                        if (dftcreVo.getDfdgrp().getRec().getMatdat() != null) {
                            trndiaService.trnDiaSet(trndia, "DFTDUE", Dates.dateSub(dftcreVo.getDfdgrp().getRec().getMatdat(), 1), "PSD", Strings.getText("委托方代付到期还款提示：$1", dftcreVo.getDfdgrp().getRec().getOwnref()), "DFTSET", DiaToWrite, linkModule, dftcreVo.getDfdgrp().getRec().getOwnref());
                        }
                    }
                    break;
                default:
            }
        }
    }
}
