package com.zd.common.core.web.domain;

import cn.hutool.core.date.CalendarUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.zd.common.core.utils.IdGen;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

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

/**
 * @author Administrator
 */
@Data
public class BaseBean implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 批量操作的id集合
     */
    @ApiModelProperty("批量操作的id集合")
    private transient List<Long> ids;

    /**
     * 数据主键
     */
    @ApiModelProperty(value = "${comment}")
    private Long id;

    /**
     * 操作人Id -只读
     */
    @ApiModelProperty("操作人Id")
    private Long userId;

    /**
     * 部门id -只读
     */
    @ApiModelProperty("部门id")
    private Long deptId;

    /**
     * 创建者 -只读
     */
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    private String createBy;

    /**
     * 创建时间
     */
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty(value = "创建时间")
    private Date createTime;

    /**
     * 更新者
     */
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    private String updateBy;

    /**
     * 更新时间
     */
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty(value = "更新时间")
    private Date updateTime;

    /**
     * 是否为新记录
     */
    protected transient boolean isNewRecord;

    @ApiModelProperty(value = "关键字搜索")
    private transient String searchValue;

    /**
     * 是否为新记录
     *
     * @return boolean
     */
    public boolean isNewRecord() {
        return this.isNewRecord || this.getId() == null;
    }

    /**
     * 设置基本属性
     *
     * @param userCode 用户编码
     */
    public void setCommonValue(String userCode, Long userId, Long deptId) {
        Date currentDate = CalendarUtil.calendar().getTime();
        if (this.isNewRecord()) {
            this.setId(IdGen.snowflakeId());
            this.setNewRecord(true);
            this.createBy = userCode;
            this.userId = userId;
            this.deptId = deptId;
            this.createTime = currentDate;
        } else {
            this.updateBy = userCode;
            this.updateTime = currentDate;
        }
    }

    /**
     * 置空属性
     */
    public void clearCommonValue() {
        this.createBy = null;
        this.createTime = null;
        this.updateBy = null;
        this.updateTime = null;
    }
}
