package cn.novalon.gym.manage.sys.core.domain;

import cn.novalon.gym.manage.common.util.SnowflakeId;
import java.time.LocalDateTime;

/**
 * 基础领域对象
 * 
 * @author 张翔
 * @date 2026-03-13
 */
public abstract class BaseDomain {

    protected Long id;
    protected String createBy;
    protected String updateBy;
    protected LocalDateTime createdAt;
    protected LocalDateTime updatedAt;
    protected LocalDateTime deletedAt;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getCreateBy() {
        return createBy;
    }

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

    public String getUpdateBy() {
        return updateBy;
    }

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

    public LocalDateTime getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }

    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }

    public LocalDateTime getDeletedAt() {
        return deletedAt;
    }

    public void setDeletedAt(LocalDateTime deletedAt) {
        this.deletedAt = deletedAt;
    }

    /**
     * 生成主键ID
     * 
     * @return 主键ID
     */
    public Long generateId() {
        this.id = SnowflakeId.nextId();
        return this.id;
    }

    /**
     * 删除（幂等操作）
     * 已删除的对象不会更新删除时间
     */
    public void delete() {
        if (this.deletedAt == null) {
            this.deletedAt = LocalDateTime.now();
        }
    }

    /**
     * 恢复已删除的对象
     */
    public void restore() {
        this.deletedAt = null;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (o == null || getClass() != o.getClass())
            return false;
        BaseDomain that = (BaseDomain) o;
        return id != null && id.equals(that.id);
    }

    @Override
    public int hashCode() {
        return id != null ? id.hashCode() : 0;
    }
}
