150 lines
5.4 KiB
Go
150 lines
5.4 KiB
Go
package entity
|
|
|
|
import (
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// UserRole 用户角色枚举
|
|
type UserRole string
|
|
|
|
const (
|
|
UserRoleUser UserRole = "user"
|
|
UserRoleAdmin UserRole = "admin"
|
|
UserRolePhotographer UserRole = "photographer"
|
|
)
|
|
|
|
// User 用户实体
|
|
type User struct {
|
|
ID uint `json:"id" gorm:"primarykey"`
|
|
Username string `json:"username" gorm:"uniqueIndex;not null;size:50"`
|
|
Email string `json:"email" gorm:"uniqueIndex;not null;size:100"`
|
|
Password string `json:"-" gorm:"not null;size:255"`
|
|
Name string `json:"name" gorm:"size:100"`
|
|
Avatar string `json:"avatar" gorm:"size:500"`
|
|
Bio string `json:"bio" gorm:"type:text"`
|
|
Website string `json:"website" gorm:"size:200"`
|
|
Location string `json:"location" gorm:"size:100"`
|
|
Role UserRole `json:"role" gorm:"default:user;size:20"`
|
|
IsActive bool `json:"is_active" gorm:"default:true"`
|
|
IsPublic bool `json:"is_public" gorm:"default:true"`
|
|
EmailVerified bool `json:"email_verified" gorm:"default:false"`
|
|
LastLogin *time.Time `json:"last_login"`
|
|
LoginCount int `json:"login_count" gorm:"default:0"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index"`
|
|
|
|
// 关联
|
|
Photos []Photo `json:"photos,omitempty" gorm:"foreignKey:UserID"`
|
|
Albums []Album `json:"albums,omitempty" gorm:"foreignKey:UserID"`
|
|
}
|
|
|
|
// TableName 指定表名
|
|
func (User) TableName() string {
|
|
return "users"
|
|
}
|
|
|
|
// IsAdmin 检查是否为管理员
|
|
func (u *User) IsAdmin() bool {
|
|
return u.Role == UserRoleAdmin
|
|
}
|
|
|
|
// IsPhotographer 检查是否为摄影师
|
|
func (u *User) IsPhotographer() bool {
|
|
return u.Role == UserRolePhotographer || u.Role == UserRoleAdmin
|
|
}
|
|
|
|
// CanManagePhoto 检查是否可以管理指定照片
|
|
func (u *User) CanManagePhoto(photo *Photo) bool {
|
|
return u.ID == photo.UserID || u.IsAdmin()
|
|
}
|
|
|
|
// CanManageAlbum 检查是否可以管理指定相册
|
|
func (u *User) CanManageAlbum(album *Album) bool {
|
|
return u.ID == album.UserID || u.IsAdmin()
|
|
}
|
|
|
|
// UserStats 用户统计信息
|
|
type UserStats struct {
|
|
Total int64 `json:"total"` // 总用户数
|
|
Active int64 `json:"active"` // 活跃用户数
|
|
ThisMonth int64 `json:"this_month"` // 本月新增
|
|
Today int64 `json:"today"` // 今日新增
|
|
RoleStats map[string]int64 `json:"role_stats"` // 角色统计
|
|
}
|
|
|
|
// CreateUserRequest 创建用户请求
|
|
type CreateUserRequest struct {
|
|
Username string `json:"username" binding:"required,min=3,max=50"`
|
|
Email string `json:"email" binding:"required,email"`
|
|
Password string `json:"password" binding:"required,min=6"`
|
|
Name string `json:"name" binding:"max=100"`
|
|
Role UserRole `json:"role" binding:"oneof=user admin photographer"`
|
|
}
|
|
|
|
// UpdateUserRequest 更新用户请求
|
|
type UpdateUserRequest struct {
|
|
Username *string `json:"username" binding:"omitempty,min=3,max=50"`
|
|
Email *string `json:"email" binding:"omitempty,email"`
|
|
Name *string `json:"name" binding:"omitempty,max=100"`
|
|
Avatar *string `json:"avatar" binding:"omitempty,max=500"`
|
|
Bio *string `json:"bio" binding:"omitempty,max=1000"`
|
|
Website *string `json:"website" binding:"omitempty,max=200"`
|
|
Location *string `json:"location" binding:"omitempty,max=100"`
|
|
Role *UserRole `json:"role" binding:"omitempty,oneof=user admin photographer"`
|
|
IsActive *bool `json:"is_active"`
|
|
}
|
|
|
|
// UpdateCurrentUserRequest 更新当前用户请求
|
|
type UpdateCurrentUserRequest struct {
|
|
Username *string `json:"username" binding:"omitempty,min=3,max=50"`
|
|
Email *string `json:"email" binding:"omitempty,email"`
|
|
Name *string `json:"name" binding:"omitempty,max=100"`
|
|
Avatar *string `json:"avatar" binding:"omitempty,max=500"`
|
|
Bio *string `json:"bio" binding:"omitempty,max=1000"`
|
|
Website *string `json:"website" binding:"omitempty,max=200"`
|
|
Location *string `json:"location" binding:"omitempty,max=100"`
|
|
}
|
|
|
|
// ChangePasswordRequest 修改密码请求
|
|
type ChangePasswordRequest struct {
|
|
OldPassword string `json:"old_password" binding:"required"`
|
|
NewPassword string `json:"new_password" binding:"required,min=6"`
|
|
}
|
|
|
|
// UserStatus 用户状态
|
|
type UserStatus string
|
|
|
|
const (
|
|
UserStatusActive UserStatus = "active"
|
|
UserStatusInactive UserStatus = "inactive"
|
|
UserStatusBanned UserStatus = "banned"
|
|
UserStatusPending UserStatus = "pending"
|
|
)
|
|
|
|
// UserListParams 用户列表查询参数
|
|
type UserListParams struct {
|
|
Page int `json:"page"`
|
|
Limit int `json:"limit"`
|
|
Sort string `json:"sort"`
|
|
Order string `json:"order"`
|
|
Role *UserRole `json:"role"`
|
|
Status *UserStatus `json:"status"`
|
|
Search string `json:"search"`
|
|
CreatedFrom *time.Time `json:"created_from"`
|
|
CreatedTo *time.Time `json:"created_to"`
|
|
LastLoginFrom *time.Time `json:"last_login_from"`
|
|
LastLoginTo *time.Time `json:"last_login_to"`
|
|
}
|
|
|
|
// UserGlobalStats 全局用户统计信息
|
|
type UserGlobalStats struct {
|
|
Total int64 `json:"total"`
|
|
Active int64 `json:"active"`
|
|
Admins int64 `json:"admins"`
|
|
Editors int64 `json:"editors"`
|
|
Users int64 `json:"users"`
|
|
MonthlyRegistrations int64 `json:"monthly_registrations"`
|
|
} |