package cn.pluss.platform.entity;

import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.util.DateUtils;
import cn.pluss.platform.util.StringUtil;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;

/**
 * <p>
 * 动态商户会员卡生成记录
 * </p>
 *
 * @author crystal
 * @since 2022-06-07
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("tb_pluss_merchant_member_code")
public class MerchantMemberCode implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * id
     */
    @TableId("id")
    private Long id;


    /**
     * 会员码code
     */
    @TableField("code")
    private String code;


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


    /**
     * 会员卡号
     */
    @TableField("cardNo")
    private String cardNo;


    /**
     * 过期开始时间
     */
    @TableField("startTime")
    private Date startTime;

    /**
     * 收银呗门店ID
     */
    @TableField(exist = false)
    private Long storeId;

    /**
     * 过期开始时间 开始时间
     */
    @TableField(exist = false, condition = "startTime &gt; #{%s}")
    private Date startTimeStart;

    /**
     * 过期开始时间 截止时间
     */
    @TableField(exist = false, condition = "startTime &lt; #{%s}")
    private Date startTimeEnd;

    /**
     * 过期结束时间
     */
    @TableField("endTime")
    private Date endTime;

    /**
     * 过期结束时间 开始时间
     */
    @TableField(exist = false, condition = "endTime &gt; #{%s}")
    private Date endTimeStart;

    /**
     * 过期结束时间 截止时间
     */
    @TableField(exist = false, condition = "endTime &lt; #{%s}")
    private Date endTimeEnd;

    /**
     * 创建时间
     */
    @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;

    public void check() {
        MsgException.checkNull(this.getStoreId(),"缺失门店ID");
        MsgException.checkBlank(this.getCardNo(),"缺失会员卡号");
    }

    public void conver(MerchantMenber menber,Integer value) {
        Date now = DateUtils.currentDate();
        String code = DateUtils.toString(now,"yyyyMMddHHmmssSSS");
        this.code =  "70" + Long.valueOf(code) / 2 +""+ StringUtil.random(1);
        this.merchantCode = menber.getMerchantCode();
        this.cardNo = menber.getCardNo();
        this.startTime = now;
        this.endTime = DateUtils.add(Calendar.MINUTE,now,value);
    }
}