All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.mybatisplus.base.BaseEntity Maven / Gradle / Ivy

There is a newer version: 1.6.8
Show newest version
package org.zodiac.mybatisplus.base;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.format.annotation.DateTimeFormat;
import org.zodiac.commons.util.DateTimes;
import org.zodiac.mybatis.base.DeletableEntity;
import org.zodiac.mybatisplus.constants.MyBatisPlusConstants;

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

/**
 * 基础实体类。
 *
 */
public abstract class BaseEntity implements DeletableEntity, Serializable {

    private static final long serialVersionUID = 8863408901164308555L;

    /**
     * 主键id
     */
    @JsonSerialize(using = ToStringSerializer.class)
    @TableId(value = "id", type = IdType.ASSIGN_ID)
    private Long id;

    /**
     * 创建人
     */
    @JsonSerialize(using = ToStringSerializer.class)
    private Long createUser;

    /**
     * 创建部门
     */
    @JsonSerialize(using = ToStringSerializer.class)
    private Long createDept;

    /**
     * 创建时间
     */
    @DateTimeFormat(pattern = DateTimes.FORMAT_19)
    @JsonFormat(pattern = DateTimes.FORMAT_19)
    private Date createTime;

    /**
     * 更新人
     */
    @JsonSerialize(using = ToStringSerializer.class)
    private Long updateUser;

    /**
     * 更新时间
     */
    @DateTimeFormat(pattern = DateTimes.FORMAT_19)
    @JsonFormat(pattern = DateTimes.FORMAT_19)
    private Date updateTime;

    /**
     * 状态[1:正常]
     */
    private Integer status;

    /**
     * 状态[0:未删除,1:删除]
     */
    @TableLogic
    private Integer isDeleted;

    @TableField(exist = false)
    private String humanCreateTime;
    @TableField(exist = false)
    private String humanUpdateTime;

    public BaseEntity() {
    }

    public Long getId() {
        return id;
    }

    public BaseEntity setId(Long id) {
        this.id = id;
        return this;
    }

    public Long getCreateUser() {
        return createUser;
    }

    public void setCreateUser(Long createUser) {
        this.createUser = createUser;
    }

    public Long getCreateDept() {
        return createDept;
    }

    public void setCreateDept(Long createDept) {
        this.createDept = createDept;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Long getUpdateUser() {
        return updateUser;
    }

    public void setUpdateUser(Long updateUser) {
        this.updateUser = updateUser;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }


    public Integer getIsDeleted() {
        return isDeleted;
    }

    public void setIsDeleted(Integer isDeleted) {
        this.isDeleted = isDeleted;
    }

    public String getHumanCreateTime() {
        return humanCreateTime;
    }

    public void setHumanCreateTime(String humanCreateTime) {
        this.humanCreateTime = humanCreateTime;
    }

    public String getHumanUpdateTime() {
        return humanUpdateTime;
    }

    public void setHumanUpdateTime(String humanUpdateTime) {
        this.humanUpdateTime = humanUpdateTime;
    }

    @JsonIgnore
    public boolean hasDeleted() {
        return isDeleted();
    }

    @Override
    @JsonIgnore
    public boolean isDeleted() {
        return isLogicDeleted();
    }

    @JsonIgnore
    public boolean hasLogicDeleted() {
        return isLogicDeleted();
    }

    @JsonIgnore
    public boolean isLogicDeleted() {
        return MyBatisPlusConstants.DB_IS_DELETED.equals(getIsDeleted());
    }

    /**
     * Execute method before inserting, need to call manually
     * 
     */
    public void preInsert() {
        setCreateTime(Objects.isNull(getCreateTime()) ? new Date() : getCreateTime());
        setUpdateTime(Objects.isNull(getUpdateTime()) ? getCreateTime() : getUpdateTime());
        setIsDeleted(Objects.isNull(getIsDeleted()) ? MyBatisPlusConstants.DELETED_FALSE : getIsDeleted());
    }

    /**
     * Execute method before update, need to call manually.
     */
    public void preUpdate() {
        setUpdateTime(new Date());
    }


    @Override
    public int hashCode() {
        return Objects.hash(createDept, createTime, createUser, humanCreateTime, humanUpdateTime, id, isDeleted, status,
            updateTime, updateUser);
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        BaseEntity other = (BaseEntity)obj;
        return Objects.equals(createDept, other.createDept) && Objects.equals(createTime, other.createTime)
            && Objects.equals(createUser, other.createUser) && Objects.equals(humanCreateTime, other.humanCreateTime)
            && Objects.equals(humanUpdateTime, other.humanUpdateTime) && Objects.equals(id, other.id)
            && Objects.equals(isDeleted, other.isDeleted) && Objects.equals(status, other.status)
            && Objects.equals(updateTime, other.updateTime) && Objects.equals(updateUser, other.updateUser);
    }

    @Override
    public String toString() {
        return "BaseEntity [id=" + id + ", createUser=" + createUser + ", createDept=" + createDept + ", createTime="
            + createTime + ", updateUser=" + updateUser + ", updateTime=" + updateTime + ", status=" + status
            + ", isDeleted=" + isDeleted + "]";
    }

    public static final class InternalUtil {

        public static void bind(BaseEntity bean) {
            BaseEntityInternalUtil.bind(bean);
        }

        public static void update() {
            BaseEntityInternalUtil.update();
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy