package cn.pluss.platform.entity;

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 crystal
 * @since 2022-06-14
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@NoArgsConstructor
@TableName("tb_pluss_open_account_record")
public class OpenAccountRecord implements Serializable {

    private static final long serialVersionUID = 1L;

    public static final String  TYPE_PAY = "01";

    public static final String  REFUND_PAY = "02";

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


    /**
     * 用户ID(付款用户ID)
     */
    @TableField("userId")
    private Long userId;


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


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


    /**
     * 订单金额
     */
    @TableField("orderAmt")
    private BigDecimal orderAmt;


    /**
     * 商品code
     */
    @TableField("goodCode")
    private String goodCode;

    /**
     * 商品名称
     */
    @TableField("goodName")
    private String goodName;

    /**
     * 类型 01: 表示设备下单 02：设备退款
     */
    @TableField("type")
    private String type;


    /**
     * 奖励金额 可以为正数 也可以为负数
     */
    @TableField("rewardAmt")
    private BigDecimal rewardAmt;


    /**
     * 状态 01：正常 02：禁用
     */
    @TableField("status")
    private String status;


    /**
     * 奖励对应的用户ID(表示收益人)
     */
    @TableField("rewardUserId")
    private Long rewardUserId;


    /**
     * 区域code
     */
    @TableField("areaCode")
    private String areaCode;


    /**
     * 区域名称
     */
    @TableField("areaName")
    private String areaName;

    /**
     * 角色code
     */
    @TableField("roleCode")
    private String roleCode;


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


    /**
     * 创建日期
     */
    @TableField(insertStrategy = FieldStrategy.NEVER, updateStrategy = FieldStrategy.NEVER)
    private Date createDate;

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

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

    /**
     * 开始日期
     */
    @TableField(exist = false)
    private String startTime;

    /**
     * 结束日期
     */
    @TableField(exist = false)
    private String endTime;

    /**
     * 金额分界线
     */
    public static BigDecimal DEFAULT_AMT = BigDecimal.valueOf(100);

    public static BigDecimal LIBAO_1_AMT = BigDecimal.valueOf(169);

    public static BigDecimal LIBAO_2_AMT = BigDecimal.valueOf(299);

    public static BigDecimal DEFAULT_ADD_LIBAO_AMT = BigDecimal.valueOf(30);
    /**
     *
     * @param order
     * @param rewardAmt
     * @param merchant
     */
    public OpenAccountRecord(Object order, BigDecimal rewardAmt, MerchantBaseInfo merchant,UserApp userApp) {
        if(order instanceof MercOrderDetail){
            MercOrderDetail o = (MercOrderDetail)order;
            this.setOrderNumber(o.getOrderNo());
            this.setOrderAmt(o.getPrice());
            this.setGoodCode(o.getCode());
            this.setGoodName(o.getName());
        }else if(order instanceof DeviceMerchantBuy){
            DeviceMerchantBuy o = (DeviceMerchantBuy)order;
            this.setOrderNumber(o.getOrderNo());
            this.setOrderAmt(BigDecimal.valueOf(o.getOrderAmount()));
            this.setGoodCode(o.getCode());
            this.setGoodName(o.getName());
        }
        this.setUserId(Long.valueOf(merchant.getUserId()));
        this.setMerchantName(merchant.getAlias());
        if(rewardAmt.compareTo(DEFAULT_AMT) >= 0){
            this.setRewardAmt(rewardAmt);
        }else{
            if(this.getOrderAmt().compareTo(LIBAO_2_AMT) == 0){
                this.setRewardAmt(rewardAmt.add(DEFAULT_ADD_LIBAO_AMT));
            }else{
                this.setRewardAmt(rewardAmt);
            }
        }
        this.setRewardUserId(userApp.getUserId());
        this.setAreaCode(merchant.getAddressNo());
        this.setRoleCode(userApp.getRoleCode());
        this.setAreaName(merchant.getProvince() + "-" + merchant.getCity() + "-" + merchant.getDistrict());
    }

    public static OpenAccountRecord build(Object order, BigDecimal rewardAmt, MerchantBaseInfo merchant, UserApp userApp) {
        OpenAccountRecord record = new OpenAccountRecord(order,rewardAmt,merchant,userApp);
        return record;
    }
}