package com.sysu.admin.support.sms;

import com.xiesx.fastboot.core.jpa.entity.JpaPlusEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import lombok.experimental.FieldNameConstants;
import org.hibernate.annotations.*;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.*;
import java.util.Date;

/**
 * @title Sms.java (generator)
 * @description 短信
 * @author 谢思贤
 * @date 2020-05-28
 */
@Data
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = false)
@FieldNameConstants(innerTypeName = "FIELDS")
@Table(name = "sys_sms")
@Entity
@EntityListeners(AuditingEntityListener.class)
@DynamicInsert
@DynamicUpdate
@Where(clause = "is_use=0")
@SQLDelete(sql = "update sys_sms set is_use=1 where id = ?")
@SQLDeleteAll(sql = "update sys_sms set is_use=1 where id = ?")
public class Sms extends JpaPlusEntity<Sms> {

    private static final long serialVersionUID = 1L;

    /**
     * 主键
     */
    @Id
    @GeneratedValue(generator = "idGenerator")
    @GenericGenerator(name = "idGenerator", strategy = "com.xiesx.fastboot.core.jpa.identifier.IdWorkerGenerator")
    private Long id;

    /**
     * 手机号
     */
    @Column
    private String mobile;

    /**
     * 验证码
     */
    @Column
    private String code;

    /**
     * 创建时间
     */
    @Column
    private Date createDate;

    /**
     * 过期时间
     */
    @Column
    private Date expireDate;

    /**
     * 是否使用（0:未使用、1:已使用、2:已过期）
     */
    @Column
    private Integer isUse;

    public boolean isExpried() {
        return new Date().after(expireDate);
    }
}
