package com.chushang.common.mybatis.base;

import cn.hutool.core.date.DatePattern;
import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import lombok.Builder;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.SuperBuilder;

import java.io.Serial;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 抽象实体
 */
@Data
public class BaseEntity implements Serializable {
	/**
	 * 创建时间
	 */
	@JsonInclude(JsonInclude.Include.NON_NULL)
	@JsonSerialize(using = LocalDateTimeSerializer.class)
	@JsonDeserialize(using = LocalDateTimeDeserializer.class)
	@TableField(value = "create_time",fill = FieldFill.INSERT, updateStrategy = FieldStrategy.NEVER)
	@JsonFormat(pattern = DatePattern.NORM_DATETIME_PATTERN)
	protected LocalDateTime createTime;
	/**
	 * 更新时间
	 */
	@JsonInclude(JsonInclude.Include.NON_NULL)
	@JsonSerialize(using = LocalDateTimeSerializer.class)
	@JsonDeserialize(using = LocalDateTimeDeserializer.class)
	@TableField(value = "update_time",fill = FieldFill.INSERT_UPDATE, updateStrategy = FieldStrategy.NOT_NULL)
	@JsonFormat(pattern = DatePattern.NORM_DATETIME_PATTERN)
	protected LocalDateTime updateTime;
	/**
	 * 删除状态
	 */
	@JsonInclude(JsonInclude.Include.NON_NULL)
	@TableLogic(value = "false",delval = "true")
	protected Boolean delState;
	/**
	 * 版本号
	 */
	@Version
	@TableField(
			value = "version",
			update = "%s+1",
			fill = FieldFill.INSERT
	)
	protected Integer version;

	@TableField(exist = false)
	private transient Map<String, Object> sqlParam;

	public Map<String, Object> getSqlParam()
	{
		if (sqlParam == null)
		{
			sqlParam = new HashMap<>();
		}
		return sqlParam;
	}

	@Serial
	private static final long serialVersionUID = 1L;

}
