package com.brilliance.isc.common.setmod.vo;

import com.brilliance.isc.bo.Cbb;
import com.brilliance.isc.common.api.fee.vo.FeeDetailRequestVo;
import com.brilliance.isc.common.api.settle.vo.SettleDetailRequestVo;
import com.brilliance.isc.common.liaall.vo.LiaallVo;
import com.brilliance.isc.common.liaccv.vo.LiaccvVo;
import com.brilliance.isc.common.setfeg.fee.Wrksum;
import com.brilliance.isc.common.setfeg.vo.SetfegVo;
import com.brilliance.isc.common.setfog.vo.SetfogVo;
import com.brilliance.isc.common.setglg.vo.SetglgVo;
import com.brilliance.isc.common.trndoc.vo.DoceotVo;
import com.brilliance.isc.common.vo.AbstractCommonVo;
import com.brilliance.isc.common.vo.BizAmdInfoVo;
import com.brilliance.isc.common.vo.BizInfoVo;
import com.brilliance.isc.common.vo.PtsptaVo;
import com.brilliance.isc.vo.model.Fale01Vo;
import com.brilliance.mda.runtime.mda.IStream;
import com.brilliance.mda.runtime.mda.impl.StreamImpl;
import com.brilliance.mda.runtime.mda.util.Decimals;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.util.*;

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

@Data
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class SetmodVo extends AbstractCommonVo {

    //减少金额
    private BigDecimal redamt = Decimals.ZERO_SCALE2;
    //其他单据金额
    private BigDecimal othdocamt = Decimals.ZERO_SCALE2;

    private String transName = NULLSTR;

    private String userId = NULLSTR;

    //账务总金额暂存(用于比对账务金额是否发生变化)
    private BigDecimal totamtsavbog = Decimals.ZERO_SCALE2;

    //结算金额
    private BigDecimal setamt = Decimals.ZERO_SCALE2;
    //业务摘要信息
    private BizInfoVo rec;
    //业务摘要修改信息
    private BizAmdInfoVo amdRec;

    //Debit/Credit allowed
    private String debcrealwflg = NULLSTR;

    //单据币种
    private String doccur = NULLSTR;

    //单据金额
    private BigDecimal docamt = Decimals.ZERO_SCALE2;

    //开立金额
    private BigDecimal opnamt = Decimals.ZERO_SCALE2;

    //费用计算的基准日期
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    private Date fecrefdat;

    //账务计算基准日期
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    private Date refdat;

    //结算金额类型
    private String docamttyp = NULLSTR;

    //默认我方费用收取角色
    private String feeclirol = NULLSTR;

    //默认对手费用收取角色
    private String feecorrol = NULLSTR;

    //settlement option for foreign party 'S'etlle - 'P'ool - 'C'ondition
    private String forsetopt = NULLSTR;

    //settlement option for client 'S'ettle - 'P'ool - 'C'ondition
    private String clisetopt = NULLSTR;

    //F 前端、前端交易，B 任务交易
    private String terminal = "F";

    //自贸区主账号
    private String zmqacc = NULLSTR;

    //自贸区标识
    private String zmqflg = NULLSTR;

    //我方编号
    private String ref = NULLSTR;

    //开立日期
    @JsonFormat(pattern="yyyy-MM-dd",timezone="GMT+8")
    private Date opndat;

    //到期日期
    @JsonFormat(pattern="yyyy-MM-dd",timezone="GMT+8")
    private Date expdat;

    @JsonFormat(pattern="yyyy-MM-dd",timezone="GMT+8")
    private Date matdat;

    //Charges to by contract
    private String chato = NULLSTR;

    //Description of roles
    private IStream roldsc = new StreamImpl();

    //Additional contracts for fee pool defaulting
    private IStream setaddconstm = new StreamImpl();

    private IStream parstm = new StreamImpl();
    //CBB列表
    private Map<String, Cbb> cbsMap;
    //交易角色参与方列表
    private List<PtsptaVo> ptsList;
    //外行收费
    private SetfogVo setfog;
    //我行收费信息
    private SetfegVo setfeg;
    //结算分录信息
    private SetglgVo setglg;
    //费用角色详情集合
    private List<PtsFeeDetailVo> ptydet;
    //面函信息
    private List<DoceotVo> doceotList;
    //表外记账
    private LiaallVo liaall;
    //保证金
    private LiaccvVo liaccvVo;
    // 结算信息，需要赋值
    private Map<String, Wrksum> wrksumMap;
    //Current total in detailled settlement
    private  BigDecimal prtdetamt = Decimals.ZERO_SCALE2;
    //Count of positions in PrintDetailledSettlement
    private  int prtdetcnt;

    private List<Fale01Vo> fale01List;

    public SetmodVo(FeeDetailRequestVo settleDetailRequestVo) {
        this.rec = settleDetailRequestVo.getRec();
        this.amdRec = settleDetailRequestVo.getAmdRec();
        this.cbsMap = settleDetailRequestVo.getCbsMap();
        this.ptsList = settleDetailRequestVo.getPtsList();
        this.setfog = settleDetailRequestVo.getSetfog();
        this.setfeg = settleDetailRequestVo.getSetfeg();
        this.ptydet = settleDetailRequestVo.getPtydet();
        this.doceotList = settleDetailRequestVo.getDoceot();
        this.liaall = settleDetailRequestVo.getLiaall();
        this.liaccvVo = settleDetailRequestVo.getLiaccv();
    }

    public SetmodVo(SettleDetailRequestVo payDetailRequestVo) {
        this.rec = payDetailRequestVo.getRec();
        this.amdRec = payDetailRequestVo.getAmdRec();
        this.cbsMap = payDetailRequestVo.getCbsMap();
        this.ptsList = payDetailRequestVo.getPtsList();
        this.setfog = payDetailRequestVo.getSetfog();
        this.setfeg = payDetailRequestVo.getSetfeg();
        this.setglg = payDetailRequestVo.getSetglg();
        this.ptydet = payDetailRequestVo.getPtydet();
        this.liaall = payDetailRequestVo.getLiaall();
        this.liaccvVo = payDetailRequestVo.getLiaccv();
        this.doccur = payDetailRequestVo.getDoccur();
        this.docamt = payDetailRequestVo.getDocamt();
    }

    private BigDecimal ameamt = Decimals.ZERO_SCALE2;

    private BigDecimal ammamt = Decimals.ZERO_SCALE2;

    private BigDecimal conamt = Decimals.ZERO_SCALE2;

    private BigDecimal maxamt = Decimals.ZERO_SCALE2;

    private BigDecimal netamt = Decimals.ZERO_SCALE2;

    public BigDecimal getSetamt() {
        return Decimals.add(Decimals.sub(docamt, redamt), othdocamt);
    }

    @Override
    public void reset() {
        if (rec == null) {
            rec = new BizInfoVo();
        }
        if (cbsMap == null) {
            cbsMap = new HashMap<>();
        }
        if (ptsList == null) {
            ptsList = new ArrayList<>();
        }
        ptsList.forEach(p -> p.reset());
        if (setfog == null) {
            setfog = new SetfogVo();
        }
        setfog.reset();
        if (setfeg == null) {
            setfeg = new SetfegVo();
        }
        setfeg.reset();
        if (setglg == null) {
            setglg = new SetglgVo();
        }
        setglg.reset();
        if (ptydet == null) {
            ptydet = new ArrayList<>();
        }
        if (doceotList == null) {
            doceotList = new ArrayList<>();
        }
        if (liaall == null) {
            liaall = new LiaallVo();
        }
        liaall.reset();
        if (setamt == null) {
            setamt = Decimals.ZERO_SCALE2;
        }
        if (redamt == null) {
            redamt = Decimals.ZERO_SCALE2;
        }
        if (othdocamt == null) {
            othdocamt = Decimals.ZERO_SCALE2;
        }
        if (docamt == null) {
            docamt = Decimals.ZERO_SCALE2;
        }
        if (opnamt == null) {
            opnamt = Decimals.ZERO_SCALE2;
        }
        if (ameamt == null) {
            ameamt = Decimals.ZERO_SCALE2;
        }
        if (ammamt == null) {
            ammamt = Decimals.ZERO_SCALE2;
        }
        if (conamt == null) {
            conamt = Decimals.ZERO_SCALE2;
        }
        if (maxamt == null) {
            maxamt = Decimals.ZERO_SCALE2;
        }
        if (netamt == null) {
            netamt = Decimals.ZERO_SCALE2;
        }
        if (amdRec == null) {
            amdRec = new BizAmdInfoVo();
        }
        amdRec.reset();
        if (fale01List == null) {
            fale01List = new ArrayList<>();
        }
    }
}
