package com.sdtool.common.entity.base;

import com.alibaba.excel.annotation.ExcelIgnore;
import org.rex.db.RColumn;

import java.util.Date;

/**
 * Created by jlutt on 2018-07-12.
 *
 * @author jlutt
 */
public class BaseEntity {

    @RColumn("createby")
    @ExcelIgnore
    private long createBy;

    @RColumn("createtime")
    @ExcelIgnore
    private Date createTime;

    @RColumn("createtimelong")
    @ExcelIgnore
    private long createTimeLong;

    @RColumn("updateby")
    @ExcelIgnore
    private long updateBy;

    @RColumn("updatetime")
    @ExcelIgnore
    private Date updateTime;

    @RColumn("updatetimelong")
    @ExcelIgnore
    private long updateTimeLong;

    public static void create(BaseEntity entity, long userId) {
        entity.setCreateBy(userId);
        entity.setCreateTime(new Date());
        entity.setCreateTimeLong(entity.getCreateTime().getTime());

        entity.setUpdateBy(userId);
        entity.setUpdateTime(new Date());
        entity.setUpdateTimeLong(entity.getUpdateTime().getTime());
    }

    public static void update(BaseEntity entity, long userId) {
        entity.setUpdateBy(userId);
        entity.setUpdateTime(new Date());
        entity.setUpdateTimeLong(entity.getUpdateTime().getTime());
    }

    @SuppressWarnings("unchecked")
    public <T extends BaseEntity> T create2(long userId) {
        this.setCreateBy(userId);
        this.setCreateTime(new Date());
        this.setCreateTimeLong(this.getCreateTime().getTime());

        this.setUpdateBy(userId);
        this.setUpdateTime(new Date());
        this.setUpdateTimeLong(this.getUpdateTime().getTime());

        return (T) this;
    }

    @SuppressWarnings("unchecked")
    public <T extends BaseEntity> T update2(long userId) {
        this.setUpdateBy(userId);
        this.setUpdateTime(new Date());
        this.setUpdateTimeLong(this.getUpdateTime().getTime());

        return (T) this;
    }

    public long getCreateBy() {
        return createBy;
    }

    public void setCreateBy(long createBy) {
        this.createBy = createBy;
    }

    public Date getCreateTime() {
        return createTime;
    }

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

    public long getUpdateBy() {
        return updateBy;
    }

    public void setUpdateBy(long updateBy) {
        this.updateBy = updateBy;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

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

    public long getCreateTimeLong() {
        return createTimeLong;
    }

    public void setCreateTimeLong(long createTimeLong) {
        this.createTimeLong = createTimeLong;
    }

    public long getUpdateTimeLong() {
        return updateTimeLong;
    }

    public void setUpdateTimeLong(long updateTimeLong) {
        this.updateTimeLong = updateTimeLong;
    }
}
