package com.changfa.entity;

import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.TableField;
import com.changfa.constants.LoginConstant;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;
@Data
public class BaseEntity implements Serializable {
    @TableField("create_by")
    @ExcelIgnore
    private String createBy;
    @ExcelIgnore
    @TableField("modify_by")
    private String modifyBy;

    @TableField("create_time")
    @ExcelIgnore
    @JSONField(format="yyyy-MM-dd")//数据库导出页面时json格式化
    @JsonFormat(pattern = "yyyy-MM-dd",timezone = "GMT+08")
    private Date createTime;

    @ExcelIgnore
    @JSONField(format="yyyy-MM-dd")//数据库导出页面时json格式化
    @JsonFormat(pattern = "yyyy-MM-dd",timezone = "GMT+08")
    @TableField("modify_time")
    private Date modifyTime;


    public <T extends BaseEntity>T fillCreated(String userName){
        //JwtUtil.getTokenData()
        this.createBy = userName;
        Date current = new Date();
        this.createTime = current;
        return (T)this;
    }

    public <T extends BaseEntity>T fillUpdate(String userName){
        //JwtUtil.getTokenData()
        this.modifyBy = userName;
        Date current = new Date();
        this.modifyTime = current;
        return (T)this;
    }


    /**
     * 信息填充
     */
    public <T extends BaseEntity>T fillCreated(){
        //JwtUtil.getTokenData()
        TUser sysUser = LoginConstant.currentUser();
        String userName = sysUser.getUserName();
        this.createBy = userName;
        Date current = new Date();
        this.createTime = current;
        return (T)this;
    }

    /**
     * 信息填充
     */
    public <T extends BaseEntity>T fillUpdate(){
        TUser sysUser = LoginConstant.currentUser();
        String userName = sysUser.getUserName();
        this.modifyBy = userName;
        Date current = new Date();
        this.modifyTime = current;
        return (T)this;
    }

    public Integer currentUserId(){
        TUser sysUser = LoginConstant.currentUser();
        return sysUser.getId();
    }

}
