63 lines
1.7 KiB
Go
Raw Permalink Normal View History

package dbs
import (
"errors"
"nf-repo/internal/interfaces/enums"
"nf-repo/internal/sqlType"
"time"
)
type User struct {
Id uint64 `json:"id" gorm:"primaryKey;column:id"`
CreatedAt int64 `json:"created_at" gorm:"column:created_at;autoCreateTime:milli"`
UpdatedAt int64 `json:"updated_at" gorm:"column:updated_at;autoUpdateTime:milli"`
DeletedAt int64 `json:"deleted_at" gorm:"index;column:deleted_at;default:0"`
Username string `json:"username" gorm:"column:username;type:varchar(64);unique"`
Password string `json:"-" gorm:"column:password;type:varchar(256)"`
Status enums.Status `json:"status" gorm:"column:status;default:0"`
Nickname string `json:"nickname" gorm:"column:nickname;type:varchar(64)"`
Comment string `json:"comment" gorm:"column:comment"`
Role enums.Role `json:"role" gorm:"column:role"`
Privileges sqlType.NumSlice[enums.Privilege] `json:"privileges" gorm:"column:privileges;type:bigint[]"`
CreatedById uint64 `json:"created_by_id" gorm:"column:created_by_id"`
CreatedByName string `json:"created_by_name" gorm:"column:created_by_name;type:varchar(64)"`
ActiveAt int64 `json:"active_at" gorm:"column:active_at"`
Deadline int64 `json:"deadline" gorm:"column:deadline"`
LoginAt int64 `json:"login_at" gorm:"-"`
}
func (u *User) IsValid(mustOk bool) error {
now := time.Now()
if now.UnixMilli() >= u.Deadline {
return errors.New("用户已过期")
}
if now.UnixMilli() < u.ActiveAt {
return errors.New("用户未启用")
}
if u.DeletedAt > 0 {
return errors.New("用户不存在")
}
switch u.Status {
case enums.StatusNormal:
case enums.StatusFrozen:
if mustOk {
return errors.New("用户被冻结")
}
default:
return errors.New("用户状态未知")
}
return nil
}