/*
 * Copyright 2022-2025 兮玥(190785909@qq.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.chestnut.common.db.domain;

import cn.idev.excel.annotation.ExcelIgnore;
import cn.idev.excel.annotation.ExcelProperty;
import cn.idev.excel.annotation.write.style.ColumnWidth;
import com.baomidou.mybatisplus.annotation.TableField;
import com.chestnut.common.annotation.XComment;
import com.chestnut.common.utils.poi.converter.LocalDateTimeConverter;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import lombok.Setter;

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

/**
 * Entity基类
 */
@Getter
@Setter
public abstract class BaseEntity implements Serializable {

	public static final String Field_createTime = "create_time";

	public static final String Field_updateTime = "update_time";

	@XComment("{CC.ENTITY.CREATE_BY}")
	@ExcelProperty("{CC.ENTITY.CREATE_BY}")
	@TableField("create_by")
	private String createBy;

	@XComment("{CC.ENTITY.CREATE_TIME}")
	@ColumnWidth(16)
	@ExcelProperty(value = "{CC.ENTITY.CREATE_TIME}", converter = LocalDateTimeConverter.class)
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	@TableField(Field_createTime)
	private LocalDateTime createTime;

	@XComment("{CC.ENTITY.UPDATE_BY}")
	@ExcelProperty("{CC.ENTITY.UPDATE_BY}")
	@TableField("update_by")
	private String updateBy;

	@XComment("{CC.ENTITY.UPDATE_TIME}")
	@ColumnWidth(16)
	@ExcelProperty(value = "{CC.ENTITY.UPDATE_TIME}", converter = LocalDateTimeConverter.class)
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	@TableField(Field_updateTime)
	private LocalDateTime updateTime;

	@XComment("{CC.ENTITY.REMARK}")
	@ExcelProperty("{CC.ENTITY.REMARK}")
	private String remark;

	@TableField(exist = false)
	@JsonInclude(JsonInclude.Include.NON_EMPTY)
	@ExcelIgnore
	private Map<String, Object> params;

	public void createBy(String username) {
		this.createBy = username;
		this.createTime = LocalDateTime.now();
		this.updateBy = this.createBy;
		this.updateTime = this.createTime;
	}

	public void updateBy(String username) {
		this.updateBy = username;
		this.updateTime = LocalDateTime.now();
	}

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