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


import com.brilliance.isc.common.cbsmod.service.XrtmodService;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.liaccv.vo.LiaccvVo;
import com.brilliance.isc.common.liaccv.vo.LiaccvgVo;
import com.brilliance.isc.common.register.fee.IFeeRegister;
import com.brilliance.isc.common.setglg.service.SetglgService;
import com.brilliance.isc.common.setmod.vo.SetmodVo;
import com.brilliance.isc.common.util.Utils;
import com.brilliance.isc.utils.StringUtils;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Component("liaccv.settleRegister")
public class LiaccvSettleRegister implements IFeeRegister {

    @Autowired
    private SetglgService setglgService;

    @Autowired
    private XrtmodService xrtmodService;

    @Override
    public void registerSettlement(SetmodVo setmodVo) {
        //! pass cover amounts to settlement (exchange Hold account against current account)
        setglgService.setglgClrAmount(setmodVo.getSetglg(), "CCV");
        LiaccvVo liaccvVo = setmodVo.getLiaccvVo();
        //垫单逻辑
        String paypsb = Utils.getFieldValue(setmodVo.getRec(), "paypsb", "");
        if (null!=liaccvVo && "Y".equals(liaccvVo.getGleflg())) {
            String cur = xrtmodService.sysiso();
            for (int i = 0; i < liaccvVo.getLiaccvg().size(); i++) {
                LiaccvgVo liaccvg = liaccvVo.getLiaccvg().get(i);
                String role = liaccvg.getRole();
                if (StringUtils.isEmpty(role)) {
                    if (StringUtils.isEmpty(liaccvVo.getNewrol())) {
                        role = "APL"; //liaall init100()
                    } else {
                        role = liaccvVo.getNewrol();
                    }
                }
                if (liaccvg.getAmt().compareTo(BigDecimal.ZERO) != 0) {
                    //String frame = SettleContext.getTransName();
                    String frame = SettleContext.getDirectTransName();
                    if (liaccvg.getAmt().compareTo(BigDecimal.ZERO) < 0 &&
                            (("BRTSET".equals(frame) || "GITSET".equals(frame) || "GITPSE".equals(frame) || "NITSET".equals(frame) || "NITPSE".equals(frame) || "BDTSET".equals(frame) ||
                                    (("LITENG".equals(frame) || "GITENG".equals(frame) || "NITENG".equals(frame) || "DITENG".equals(frame)) && StringUtils.isNotEmpty(liaccvVo.getChgcurflg())))
                                    && (cur.equals(liaccvg.getCur()) || cur.equals(liaccvg.getConcur())))) {
                        if (StringUtils.isEmpty(paypsb)) {
                            setglgService.setglgAddAmount(setmodVo.getSetglg(), "CCV" + MdaUtils.toString(i, -3), role, liaccvg.getConcur(), liaccvg.getAcccovamt(), "", "", "", "", "");
                        }
                        String hldKey = setglgService.setglgAddAmount(setmodVo.getSetglg(), "CCV" + MdaUtils.toString(i, -3), role, liaccvg.getConcur(), liaccvg.getAcccovamt().negate(), "HLD", "CCD", "D", liaccvg.getAcc(), "CCV" + MdaUtils.toString(i, -3));
                        setglgService.setglgAddAmtFldAccCurAmt(setmodVo.getSetglg(), "CCV" + MdaUtils.toString(i, -3), hldKey, liaccvg.getCur(), liaccvg.getAmt());
                        setglgService.setglgAddAmtFldExtRat(setmodVo.getSetglg(), "CCV" + MdaUtils.toString(i, -3), hldKey, new BigDecimal(Double.toString(1.00)), liaccvg.getRat());
                    } else {
                        //外币保证金付款且勾选垫款时不应多出一条贷记客户账
                        if (StringUtils.isEmpty(paypsb)) {
                            setglgService.setglgAddAmount(setmodVo.getSetglg(), "CCV", role, liaccvg.getCur(), liaccvg.getAmt(), "", "", "", "", "");
                        }
                        if (liaccvg.getAmt().compareTo(BigDecimal.ZERO) > 0) {
                            setglgService.setglgAddAmount(setmodVo.getSetglg(), "CCV", role, liaccvg.getCur(), liaccvg.getAmt().negate(), "HLD", "CCC", "A", liaccvg.getAcc(), "");
                        } else {
                            setglgService.setglgAddAmount(setmodVo.getSetglg(), "CCV", role, liaccvg.getCur(), liaccvg.getAmt().negate(), "HLD", "CCD", "A", liaccvg.getAcc(), "");
                        }
                    }
                }

            }
        }
    }
}
