package cn.pluss.platform.entity;

import cn.pluss.platform.api.MercSubSplitAccount;
import cn.pluss.platform.api.SplitReq;
import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 * 分账明细详情表
 * </p>
 *
 * @author bzg
 * @since 2022-07-01
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@NoArgsConstructor
@TableName("tb_pluss_merchant_order_split_detail")
public class MerchantOrderSplitDetail implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    private Long id;


    /**
     * 商户编号
     */
    @TableField("merchantCode")
    private String merchantCode;

    /**
     * 通道商编
     */
    @TableField("merchantId")
    private String merchantId;

    /**
     * 商户名称
     */
    @TableField("merchantName")
    private String merchantName;


    /**
     * 订单号
     */
    @TableField("orderNumber")
    private String orderNumber;

    /**
     * 分账比例
     */
    @TableField("divRatio")
    private BigDecimal divRatio;


    /**
     * 分账金额
     */
    @TableField("divAmt")
    private BigDecimal divAmt;

    /**
     * 最终分账结果
     */
    @TableField("finalAmt")
    private BigDecimal finalAmt;

    /**
     * 备注
     */
    @TableField("remark")
    private String remark;


    @TableField("isChargeFee")
    private String isChargeFee;

    /**
     * 创建时间
     */
    @TableField(value = "createTime", fill = FieldFill.INSERT)
    private Date createTime;

    /**
     * 创建时间 开始时间
     */
    @TableField(exist = false, condition = "createTime &gt; #{%s}")
    private Date createTimeStart;

    /**
     * 创建时间 截止时间
     */
    @TableField(exist = false, condition = "createTime &lt; #{%s}")
    private Date createTimeEnd;

    /**
     * 状态 1：已分账 -1：已退款
     */
    @TableField("status")
    private Integer status;

    /**
     * 退款金额
     */
    @TableField("refundAmt")
    private BigDecimal refundAmt;

    @TableField("state")
    private String state;

    @TableField("channelSendSn")
    private String channelSendSn;

    @TableField("channelRecvSn")
    private String channelRecvSn;

    public MerchantOrderSplitDetail(MercSubSplitAccount account,MerchantBaseInfo merchant,MerchantChannelStatus channel,MerchantOrderSplit order) {
        this.merchantCode = account.getMerchantCode();
        this.merchantId = channel.getMerchantId();
        this.merchantName = merchant.getAlias();
        this.orderNumber = order.getOrderNumber();
        if(SplitReq.DivisionMode.SCALE.getValue().equals(order.getDivisionMode())){
            this.divRatio = new BigDecimal(account.getDivRatio());
            this.setFinalAmt(order.getOrderAmt().multiply(this.getDivRatio()).setScale(2,BigDecimal.ROUND_HALF_UP));
        }
        if (SplitReq.DivisionMode.AMT.getValue().equals(order.getDivisionMode())) {
            this.divAmt = new BigDecimal(account.getDivAmount());
        }
        this.setIsChargeFee(account.getIsChargeFee());
    }
}