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

import com.brilliance.isc.common.cbsmod.service.XrtmodService;
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.utils.StringUtils;
import com.brilliance.isc.vo.component.trtcre.TrtcreVo;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Component
public class TrtcreSettleRegister implements ISettleRegister {

    @Autowired
    private SetglgService setglgService;

    @Autowired
    private GlemodService glemodService;

    @Autowired
    private SetmodService setmodService;

    @Autowired
    private XrtmodService xrtmodService;

    @Override
    public void registerSettlement(SetmodVo setmodVo) {
        //! move financed amount from debit of FIPROL to Financing
        setglgService.setglgClrAmount(setmodVo.getSetglg(),"TRTCRE");
        TrtcreVo trtcreVo = setmodVo.getRec().getTrtcreVo();
        if (trtcreVo == null) {
            return;
        }
        BigDecimal amt = trtcreVo.getTrdgrp().getCbs().getMax().getAmt();
        if ("X".equals(trtcreVo.getCrefinflg()) && amt.compareTo(BigDecimal.ZERO) > 0) {
            String cur = trtcreVo.getTrdgrp().getCbs().getMax().getCur();
            BigDecimal intAmt = BigDecimal.ZERO;
            if(MdaUtils.compareTo(trtcreVo.getFinmod().getInttyp(),"0") == 0) {
                intAmt = trtcreVo.getFinmod().getIntamt();
            }
            String acc = "";
            String finAccStream = glemodService.getAvailAcc("BF-TTT", cur, "", amt);
            if (StringUtils.isNotEmpty(finAccStream)) {
                String[] finAccs = finAccStream.split("[\\r\\n]{1,2}");
                acc = trtcreVo.getFinmod().getAccbch() + Strings.mid(finAccs[0],5);
            }
            BigDecimal oriamt = xrtmodService.amtRnd(trtcreVo.getOriamt().multiply(trtcreVo.getTrdgrp().getRec().getPctfin()).divide(new BigDecimal(100)), trtcreVo.getOricur());
            if(MdaUtils.compareTo(trtcreVo.getOricur(), cur) != 0) {
                //错币种情况下，需要重新计算oriamt
                BigDecimal rate = trtcreVo.getExtrat();
                if ("CNY".equals(trtcreVo.getOricur())) {
                    rate = new BigDecimal("1.00").divide(trtcreVo.getExtrat(), 12, BigDecimal.ROUND_HALF_UP);
                }
                oriamt = xrtmodService.amtcnv(trtcreVo.getTrdgrp().getCbs().getMax().getAmt(), rate, trtcreVo.getTrdgrp().getCbs().getMax().getCur(), trtcreVo.getOricur());
                String finKey = setglgService.setglgAddAmount(setmodVo.getSetglg(),"TRTCRE", trtcreVo.getFiprol(), trtcreVo.getOricur(), Decimals.sub(oriamt, intAmt), "FIN", "BF", "F", acc, "TRTCRE");
                setglgService.setglgAddAmtFldAccCur(setmodVo.getSetglg(),"TRTCRE", finKey, cur);
                if ("CNY".equals(cur)) {
                    setglgService.setglgAddAmtFldExtRat(setmodVo.getSetglg(),"TRTCRE", finKey, new BigDecimal("1.00"), trtcreVo.getExtrat());
                } else {
                    setglgService.setglgAddAmtFldExtRat(setmodVo.getSetglg(),"TRTCRE", finKey, trtcreVo.getExtrat(), new BigDecimal("1.00"));
                }
            } else {
                String finKey = setglgService.setglgAddAmount(setmodVo.getSetglg(),"TRTCRE", trtcreVo.getFiprol(), trtcreVo.getOricur(), Decimals.sub(oriamt, intAmt), "FIN", "BF", "A", acc, "");
            }
            setmodService.setGlgAmt(setmodVo,"TRTCRE", trtcreVo.getFiprol(), trtcreVo.getOricur(), Decimals.add(oriamt.negate(), intAmt), "FIN");
            setmodService.setPtyBasics(setmodVo,"FIP", trtcreVo.getTrdgrp().getFip().getPtyinr(), trtcreVo.getTrdgrp().getFip().getPtainr(), trtcreVo.getTrdgrp().getFip().getNam());
        }
    }
}
