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.common.util.Utils;
import com.brilliance.isc.vo.component.bptbck.BptbckVo;
import com.brilliance.isc.vo.component.bptbck.BptbckgVo;
import com.brilliance.isc.vo.model.BpdgrpVo;
import com.brilliance.mda.runtime.mda.util.Dates;
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("bptbck.settleRegister")
public class BptbckSettleRegister implements ISettleRegister {

    @Autowired
    private SetglgService setglgService;

    @Autowired
    private GlemodService glemodService;

    @Autowired
    private SetmodService setmodService;

    @Autowired
    private XrtmodService xrtmodService;

    @Override
    public void registerSettlement(SetmodVo setmodVo) {
        BptbckVo bptbckVo = setmodVo.getRec().getBptbckVo();
        if (MdaUtils.compareTo(bptbckVo.getDscbckamt(), 0) != 0) {
            setglgService.setglgClrAmount(setmodVo.getSetglg(), "BPTBCK");
            for (int i=0; i<bptbckVo.getBptbckg().size(); i++) {
                BptbckgVo bptbckgVo = bptbckVo.getBptbckg().get(i);
                if (bptbckgVo.getBckamt().compareTo(BigDecimal.ZERO) != 0) {
                    if ("BPD".equals(bptbckgVo.getObjtyp())) {
                        BpdgrpVo bpdgrpVo = bptbckVo.getWrkbpdgrp();
                        String rol = "";
                        String cdtKey = "";
                        String dbtKey = "";
                        switch (bpdgrpVo.getRec().getFintyp()) {
                            case "F":
                                rol = "INV";
                                break;
                            case "U":
                                rol = "BU1";
                                break;
                            default:
                                rol = "FIA";
                        }
                        String dbrol = setmodVo.getFeeclirol();
                        if ("BFD|BDD|BED".indexOf(bpdgrpVo.getRec().getPnttyp()) > -1) {
                            dbrol = Utils.getFieldValue(setmodVo.getRec(), "docprbrol", setmodVo.getFeeclirol());
                        }
                        BigDecimal amount = Decimals.add(Decimals.add(Decimals.add(Decimals.add(Decimals.add(bptbckVo.getWrkfinmod().getCbs().getCbe().getIntamt1(), bptbckVo.getWrkfinmod().getCbs().getCbe().getIntamty1()), bptbckVo.getWrkfinmod().getCbs().getCbe().getIntamt2()), bptbckVo.getWrkfinmod().getCbs().getCbe().getIntamty2()), bptbckVo.getWrkfinmod().getCbs().getCbe().getIntamt()), bptbckVo.getWrkfinmod().getCbs().getCbe().getAmt());
                        String opttyp = bpdgrpVo.getRec().getOpttyp();
                        if ("2".equals(opttyp)) {
                            //转卖还款
                            String cur = bpdgrpVo.getCbs().getMax().getCur();
                            BigDecimal amt = bpdgrpVo.getCbs().getMax().getAmt();
                            int days = Dates.diff(bpdgrpVo.getRec().getMatdat(), bpdgrpVo.getRec().getMcdate());
                            // 利息
                            BigDecimal allint = Decimals.div(Decimals.div(Decimals.mul(Decimals.mul(amt, new BigDecimal(days)), bpdgrpVo.getRec().getTolrat()), new BigDecimal("100")), new BigDecimal("360"));;
                            allint = xrtmodService.amtRnd(allint, cur);
                            // 包买商包买金额
                            BigDecimal bmhamt = bpdgrpVo.getRec().getBmhamt();
                            //计算损益
                            BigDecimal fftcce = amt.subtract(allint).subtract(bmhamt);
                            String accbch = bptbckVo.getWrkfinmod().getAccbch();
                            String trntyp = getTrnTyp(bpdgrpVo);
                            // 亏损
                            if (MdaUtils.compareTo(fftcce, 0) > 0) {
                                String finAcc3 = glemodService.getAvailAcc("AR-TT3", cur, "", fftcce);
                                String acc3 = accbch + Strings.mid(finAcc3, 5);
                                cdtKey = setglgService.setglgAddAmount(setmodVo.getSetglg(), "BPTBCK", rol, cur, fftcce.negate(), "", "ARC", "D", acc3, "");
                                //设置默认的dsp和acc

                                String finAcc4 = glemodService.getAvailAcc(trntyp, cur, "", fftcce);
                                String acc4 = accbch + Strings.mid(finAcc4, 5);
                                dbtKey = setglgService.setglgAddAmount(setmodVo.getSetglg(), "BPTBCK", rol, cur, fftcce, "", "TPD", "A", acc4, "");
                            } else {
                                // 收益
                                if (MdaUtils.compareTo(fftcce, 0) < 0) {
                                    String finAcc5 = glemodService.getAvailAcc(trntyp, cur, "", fftcce.abs());
                                    String acc5 = accbch + Strings.mid(finAcc5, 5);
                                    cdtKey = setglgService.setglgAddAmount(setmodVo.getSetglg(), "BPTBCK", rol, cur, fftcce, "", "TPC", "J", acc5, "");
                                    String finAcc6 = glemodService.getAvailAcc("AR-TT3", cur, "", fftcce.abs());
                                    String acc6 = accbch + Strings.mid(finAcc6, 5);
                                    dbtKey = setglgService.setglgAddAmount(setmodVo.getSetglg(), "BPTBCK", rol, cur, fftcce.abs(), "", "ARD", "D", acc6, "");
                                }
                            }
                            String finAcc8 = glemodService.getAvailAcc("SP-FFT", bptbckgVo.getOpncur(), "", bmhamt);
                            String acc8 = accbch + Strings.mid(finAcc8, 5);
                            dbtKey = setglgService.setglgAddAmount(setmodVo.getSetglg(), "BPTBCK", rol, bptbckgVo.getOpncur(), bmhamt, "", "SPD", "D", acc8, "");
                            //原始金额置灰
                            setglgService.setglgAddAmtFldDisabled(setmodVo.getSetglg(), "BPTBCK", dbtKey, "fmtamt");
                            String finAcc7 = glemodService.getAvailAcc("AR-TT3", cur, "", fftcce.abs());
                            String acc7 = accbch + Strings.mid(finAcc7, 5);
                            dbtKey = setglgService.setglgAddAmount(setmodVo.getSetglg(), "BPTBCK", rol, bptbckgVo.getOpncur(), amount.subtract(bmhamt), "", "ARD", "D", acc7, "");
                            cdtKey = setglgService.setglgAddAmount(setmodVo.getSetglg(), "BPTBCK", rol, bptbckgVo.getOpncur(), amount.negate(), "", "BF", "A", bptbckVo.getSetact(), "");
                        } else {
                            cdtKey = setglgService.setglgAddAmount(setmodVo.getSetglg(), "BPTBCK", rol, bptbckgVo.getOpncur(), amount.negate(), "", "BF", "A", bptbckVo.getSetact(), "" );
                            dbtKey = setmodService.setGlgAmt(setmodVo, "BPTBCK", dbrol, bptbckgVo.getOpncur(), amount, "" );
                        }
                    }
                }
            }
        }
    }

    public String getTrnTyp(BpdgrpVo bpdgrpVo) {
        // FINTYP  F:票据买断（国际）
        //	  G:国内证包买单据（国内）
        // FORTYP  1 - 一级市场买入
        //         2 - 二级市场买入
        String finTyp = bpdgrpVo.getRec().getFintyp();
        String forTyp = bpdgrpVo.getRec().getFortyp();
        String trntyp = "";
        //国内一级福费廷价差损益
        if (MdaUtils.compareTo(finTyp, "G") == 0 && MdaUtils.compareTo(forTyp, "1") == 0) {
            trntyp = "TP-T11";
        }
        //国内二级福费廷价差损益
        if (MdaUtils.compareTo(finTyp, "G") == 0 && MdaUtils.compareTo(forTyp, "2") == 0) {
            trntyp = "TP-T12";
        }
        //国际一级福费廷价差损益
        if (MdaUtils.compareTo(finTyp, "F") == 0 && MdaUtils.compareTo(forTyp, "1") == 0) {
            trntyp = "TP-T13";
        }
        //国际二级福费廷价差损益
        if (MdaUtils.compareTo(finTyp, "F") == 0 && MdaUtils.compareTo(forTyp, "2") == 0) {
            trntyp = "TP-T14";
        }

        return trntyp;
    }
}
