Files
tyapi-server/internal/domains/admin/entities/admin.go
2025-07-13 16:36:20 +08:00

157 lines
6.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package entities
import (
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
// AdminRole 管理员角色枚举
// 定义系统中不同级别的管理员角色,用于权限控制和功能分配
type AdminRole string
const (
RoleSuperAdmin AdminRole = "super_admin" // 超级管理员 - 拥有所有权限
RoleAdmin AdminRole = "admin" // 普通管理员 - 拥有大部分管理权限
RoleReviewer AdminRole = "reviewer" // 审核员 - 仅拥有审核相关权限
)
// Admin 管理员实体
// 系统管理员的核心信息,包括账户信息、权限配置、操作统计等
// 支持多角色管理,提供完整的权限控制和操作审计功能
type Admin struct {
// 基础标识
ID string `gorm:"primaryKey;type:varchar(36)" comment:"管理员唯一标识"`
Username string `gorm:"type:varchar(100);not null;uniqueIndex" comment:"登录用户名"`
Password string `gorm:"type:varchar(255);not null" comment:"登录密码(加密存储)"`
Email string `gorm:"type:varchar(255);not null;uniqueIndex" comment:"邮箱地址"`
Phone string `gorm:"type:varchar(20)" comment:"手机号码"`
RealName string `gorm:"type:varchar(100);not null" comment:"真实姓名"`
Role AdminRole `gorm:"type:varchar(50);not null;default:'reviewer'" comment:"管理员角色"`
// 状态信息 - 账户状态和登录统计
IsActive bool `gorm:"default:true" comment:"账户是否激活"`
LastLoginAt *time.Time `comment:"最后登录时间"`
LoginCount int `gorm:"default:0" comment:"登录次数统计"`
// 权限信息 - 细粒度权限控制
Permissions string `gorm:"type:text" comment:"权限列表(JSON格式存储)"`
// 审核统计 - 管理员的工作绩效统计
ReviewCount int `gorm:"default:0" comment:"审核总数"`
ApprovedCount int `gorm:"default:0" comment:"通过数量"`
RejectedCount int `gorm:"default:0" comment:"拒绝数量"`
// 时间戳字段
CreatedAt time.Time `gorm:"autoCreateTime" comment:"创建时间"`
UpdatedAt time.Time `gorm:"autoUpdateTime" comment:"更新时间"`
DeletedAt gorm.DeletedAt `gorm:"index" comment:"软删除时间"`
}
// AdminLoginLog 管理员登录日志实体
// 记录管理员的所有登录尝试,包括成功和失败的登录记录
// 用于安全审计和异常登录检测
type AdminLoginLog struct {
// 基础标识
ID string `gorm:"primaryKey;type:varchar(36)" comment:"日志记录唯一标识"`
AdminID string `gorm:"type:varchar(36);not null;index" comment:"管理员ID"`
Username string `gorm:"type:varchar(100);not null" comment:"登录用户名"`
IP string `gorm:"type:varchar(45);not null" comment:"登录IP地址"`
UserAgent string `gorm:"type:varchar(500)" comment:"客户端信息"`
Status string `gorm:"type:varchar(20);not null" comment:"登录状态(success/failed)"`
Message string `gorm:"type:varchar(500)" comment:"登录结果消息"`
// 时间戳字段
CreatedAt time.Time `gorm:"autoCreateTime" comment:"创建时间"`
}
// AdminOperationLog 管理员操作日志实体
// 记录管理员在系统中的所有重要操作,用于操作审计和问题追踪
// 支持操作类型、资源、详情等完整信息的记录
type AdminOperationLog struct {
// 基础标识
ID string `gorm:"primaryKey;type:varchar(36)" comment:"操作日志唯一标识"`
AdminID string `gorm:"type:varchar(36);not null;index" comment:"操作管理员ID"`
Username string `gorm:"type:varchar(100);not null" comment:"操作管理员用户名"`
Action string `gorm:"type:varchar(100);not null" comment:"操作类型"`
Resource string `gorm:"type:varchar(100);not null" comment:"操作资源"`
ResourceID string `gorm:"type:varchar(36)" comment:"资源ID"`
Details string `gorm:"type:text" comment:"操作详情(JSON格式)"`
IP string `gorm:"type:varchar(45);not null" comment:"操作IP地址"`
UserAgent string `gorm:"type:varchar(500)" comment:"客户端信息"`
Status string `gorm:"type:varchar(20);not null" comment:"操作状态(success/failed)"`
Message string `gorm:"type:varchar(500)" comment:"操作结果消息"`
// 时间戳字段
CreatedAt time.Time `gorm:"autoCreateTime" comment:"创建时间"`
}
// AdminPermission 管理员权限实体
// 定义系统中的所有权限项,支持模块化权限管理
// 每个权限都有唯一的代码标识,便于程序中的权限检查
type AdminPermission struct {
// 基础标识
ID string `gorm:"primaryKey;type:varchar(36)" comment:"权限唯一标识"`
Name string `gorm:"type:varchar(100);not null;uniqueIndex" comment:"权限名称"`
Code string `gorm:"type:varchar(100);not null;uniqueIndex" comment:"权限代码"`
Description string `gorm:"type:varchar(500)" comment:"权限描述"`
Module string `gorm:"type:varchar(50);not null" comment:"所属模块"`
IsActive bool `gorm:"default:true" comment:"权限是否启用"`
// 时间戳字段
CreatedAt time.Time `gorm:"autoCreateTime" comment:"创建时间"`
UpdatedAt time.Time `gorm:"autoUpdateTime" comment:"更新时间"`
DeletedAt gorm.DeletedAt `gorm:"index" comment:"软删除时间"`
}
// AdminRolePermission 角色权限关联实体
// 建立角色和权限之间的多对多关系,实现基于角色的权限控制(RBAC)
type AdminRolePermission struct {
// 基础标识
ID string `gorm:"primaryKey;type:varchar(36)" comment:"关联记录唯一标识"`
Role AdminRole `gorm:"type:varchar(50);not null;index" comment:"角色"`
PermissionID string `gorm:"type:varchar(36);not null;index" comment:"权限ID"`
// 时间戳字段
CreatedAt time.Time `gorm:"autoCreateTime" comment:"创建时间"`
}
// TableName 指定数据库表名
func (Admin) TableName() string {
return "admins"
}
// IsValid 检查管理员账户是否有效
// 判断管理员账户是否处于可用状态,包括激活状态和软删除状态检查
func (a *Admin) IsValid() bool {
return a.IsActive && a.DeletedAt.Time.IsZero()
}
// UpdateLastLoginAt 更新最后登录时间
// 在管理员成功登录后调用,记录最新的登录时间
func (a *Admin) UpdateLastLoginAt() {
now := time.Now()
a.LastLoginAt = &now
}
// Deactivate 停用管理员账户
// 将管理员账户设置为非激活状态,禁止登录和操作
func (a *Admin) Deactivate() {
a.IsActive = false
}
// Activate 激活管理员账户
// 重新启用管理员账户,允许正常登录和操作
func (a *Admin) Activate() {
a.IsActive = true
}
// BeforeCreate GORM钩子创建前自动生成UUID
func (a *Admin) BeforeCreate(tx *gorm.DB) error {
if a.ID == "" {
a.ID = uuid.New().String()
}
return nil
}