139 lines
3.7 KiB
Go
139 lines
3.7 KiB
Go
package entities
|
|
|
|
import (
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// User 用户实体
|
|
type User struct {
|
|
ID string `gorm:"primaryKey;type:varchar(36)" json:"id"`
|
|
Username string `gorm:"uniqueIndex;type:varchar(50);not null" json:"username"`
|
|
Email string `gorm:"uniqueIndex;type:varchar(100);not null" json:"email"`
|
|
Password string `gorm:"type:varchar(255);not null" json:"-"`
|
|
FirstName string `gorm:"type:varchar(50)" json:"first_name"`
|
|
LastName string `gorm:"type:varchar(50)" json:"last_name"`
|
|
Phone string `gorm:"type:varchar(20)" json:"phone"`
|
|
Avatar string `gorm:"type:varchar(255)" json:"avatar"`
|
|
Status UserStatus `gorm:"type:varchar(20);default:'active'" json:"status"`
|
|
LastLoginAt *time.Time `json:"last_login_at"`
|
|
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
|
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
|
|
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
|
|
|
// 软删除字段
|
|
IsDeleted bool `gorm:"default:false" json:"is_deleted"`
|
|
|
|
// 版本控制
|
|
Version int `gorm:"default:1" json:"version"`
|
|
}
|
|
|
|
// UserStatus 用户状态枚举
|
|
type UserStatus string
|
|
|
|
const (
|
|
UserStatusActive UserStatus = "active"
|
|
UserStatusInactive UserStatus = "inactive"
|
|
UserStatusSuspended UserStatus = "suspended"
|
|
UserStatusPending UserStatus = "pending"
|
|
)
|
|
|
|
// 实现 Entity 接口
|
|
func (u *User) GetID() string {
|
|
return u.ID
|
|
}
|
|
|
|
func (u *User) GetCreatedAt() time.Time {
|
|
return u.CreatedAt
|
|
}
|
|
|
|
func (u *User) GetUpdatedAt() time.Time {
|
|
return u.UpdatedAt
|
|
}
|
|
|
|
// 业务方法
|
|
func (u *User) IsActive() bool {
|
|
return u.Status == UserStatusActive && !u.IsDeleted
|
|
}
|
|
|
|
func (u *User) GetFullName() string {
|
|
if u.FirstName == "" && u.LastName == "" {
|
|
return u.Username
|
|
}
|
|
return u.FirstName + " " + u.LastName
|
|
}
|
|
|
|
func (u *User) CanLogin() bool {
|
|
return u.IsActive() && u.Status != UserStatusSuspended
|
|
}
|
|
|
|
func (u *User) MarkAsDeleted() {
|
|
u.IsDeleted = true
|
|
u.Status = UserStatusInactive
|
|
}
|
|
|
|
func (u *User) Restore() {
|
|
u.IsDeleted = false
|
|
u.Status = UserStatusActive
|
|
}
|
|
|
|
func (u *User) UpdateLastLogin() {
|
|
now := time.Now()
|
|
u.LastLoginAt = &now
|
|
}
|
|
|
|
// 验证方法
|
|
func (u *User) Validate() error {
|
|
if u.Username == "" {
|
|
return NewValidationError("username is required")
|
|
}
|
|
if u.Email == "" {
|
|
return NewValidationError("email is required")
|
|
}
|
|
if u.Password == "" {
|
|
return NewValidationError("password is required")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// TableName 指定表名
|
|
func (User) TableName() string {
|
|
return "users"
|
|
}
|
|
|
|
// ValidationError 验证错误
|
|
type ValidationError struct {
|
|
Message string
|
|
}
|
|
|
|
func (e *ValidationError) Error() string {
|
|
return e.Message
|
|
}
|
|
|
|
func NewValidationError(message string) *ValidationError {
|
|
return &ValidationError{Message: message}
|
|
}
|
|
|
|
// UserProfile 用户档案(扩展信息)
|
|
type UserProfile struct {
|
|
ID string `gorm:"primaryKey;type:varchar(36)" json:"id"`
|
|
UserID string `gorm:"type:varchar(36);not null;index" json:"user_id"`
|
|
Bio string `gorm:"type:text" json:"bio"`
|
|
Location string `gorm:"type:varchar(100)" json:"location"`
|
|
Website string `gorm:"type:varchar(255)" json:"website"`
|
|
Birthday *time.Time `json:"birthday"`
|
|
Gender string `gorm:"type:varchar(10)" json:"gender"`
|
|
Timezone string `gorm:"type:varchar(50)" json:"timezone"`
|
|
Language string `gorm:"type:varchar(10);default:'zh-CN'" json:"language"`
|
|
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
|
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
|
|
|
|
// 关联关系
|
|
User *User `gorm:"foreignKey:UserID;references:ID" json:"user,omitempty"`
|
|
}
|
|
|
|
func (UserProfile) TableName() string {
|
|
return "user_profiles"
|
|
}
|