package cn.reghao.oss.console.util.db;

import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * @author reghao
 * @date 2019-10-18 14:42:48
 */
@MappedSuperclass
@Getter
@Setter
public class BaseEntity implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    // 采用 id 字段自增方式
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    // 使用额外的 seq 表
    //@SequenceGenerator(name = "seqGen", sequenceName = "seq")
    protected Integer id;
    // 逻辑删除
    @Column(nullable = false)
    private Boolean deleted;
    @CreationTimestamp
    @Column(nullable = false, updatable = false)
    protected LocalDateTime createTime;
    @UpdateTimestamp
    @Column(nullable = false)
    protected LocalDateTime updateTime;

    public BaseEntity() {
        this.deleted = false;
        this.createTime = LocalDateTime.now();
        this.updateTime = LocalDateTime.now();
    }

    public void setNull() {
        //this.setId(null);
        this.setDeleted(null);
        this.setCreateTime(null);
        this.setUpdateTime(null);
    }

    public void setEntity(BaseEntity entity) {
        this.id = entity.getId();
        this.deleted = entity.getDeleted();
        this.createTime = entity.getCreateTime();
        this.updateTime = LocalDateTime.now();
    }
}
