68 lines
1.8 KiB
Go
68 lines
1.8 KiB
Go
package models
|
|
|
|
import (
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// UserAccountStatus 用户账号状态表模型
|
|
type UserAccountStatus struct {
|
|
ID int64 `gorm:"primaryKey;autoIncrement;column:id"`
|
|
UserID int64 `gorm:"not null;uniqueIndex:uk_user_account_status_user_id;column:user_id"`
|
|
Status string `gorm:"type:varchar(20);not null;column:status"` // frozen/normal/banned
|
|
Reason *string `gorm:"type:text;column:reason"` // 冻结/封号原因
|
|
FrozenUntil *int64 `gorm:"column:frozen_until"` // 冻结解封时间(毫秒时间戳)
|
|
CreatedAt int64 `gorm:"not null;column:created_at"`
|
|
UpdatedAt int64 `gorm:"not null;column:updated_at"`
|
|
}
|
|
|
|
// TableName 指定表名
|
|
func (UserAccountStatus) TableName() string {
|
|
return "user_account_status"
|
|
}
|
|
|
|
// BeforeCreate 创建前钩子
|
|
func (u *UserAccountStatus) BeforeCreate(tx *gorm.DB) error {
|
|
now := time.Now().UnixMilli()
|
|
u.CreatedAt = now
|
|
u.UpdatedAt = now
|
|
return nil
|
|
}
|
|
|
|
// BeforeUpdate 更新前钩子
|
|
func (u *UserAccountStatus) BeforeUpdate(tx *gorm.DB) error {
|
|
u.UpdatedAt = time.Now().UnixMilli()
|
|
return nil
|
|
}
|
|
|
|
// Status constants
|
|
const (
|
|
AccountStatusNormal = "normal"
|
|
AccountStatusFrozen = "frozen"
|
|
AccountStatusBanned = "banned"
|
|
)
|
|
|
|
// IsNormal 判断账号状态是否正常
|
|
func (u *UserAccountStatus) IsNormal() bool {
|
|
return u.Status == AccountStatusNormal
|
|
}
|
|
|
|
// IsFrozen 判断账号是否被冻结
|
|
func (u *UserAccountStatus) IsFrozen() bool {
|
|
return u.Status == AccountStatusFrozen
|
|
}
|
|
|
|
// IsBanned 判断账号是否被封号
|
|
func (u *UserAccountStatus) IsBanned() bool {
|
|
return u.Status == AccountStatusBanned
|
|
}
|
|
|
|
// GetFrozenUntilTime 返回冻结解封时间(如果是被冻结状态)
|
|
func (u *UserAccountStatus) GetFrozenUntilTime() time.Time {
|
|
if u.FrozenUntil != nil {
|
|
return time.UnixMilli(*u.FrozenUntil)
|
|
}
|
|
return time.Time{}
|
|
}
|