2025-11-28 23:30:49 +08:00
|
|
|
|
package model
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"time"
|
2025-11-30 18:56:56 +08:00
|
|
|
|
|
|
|
|
|
|
"gorm.io/datatypes"
|
2025-11-28 23:30:49 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// User 用户模型
|
2025-12-26 01:15:17 +08:00
|
|
|
|
// @Description 用户账户数据模型
|
2025-11-28 23:30:49 +08:00
|
|
|
|
type User struct {
|
2025-11-30 18:56:56 +08:00
|
|
|
|
ID int64 `gorm:"column:id;primaryKey;autoIncrement" json:"id"`
|
2025-12-02 10:33:19 +08:00
|
|
|
|
Username string `gorm:"column:username;type:varchar(255);not null;uniqueIndex:idx_user_username_status,priority:1" json:"username"`
|
2025-11-30 18:56:56 +08:00
|
|
|
|
Password string `gorm:"column:password;type:varchar(255);not null" json:"-"` // 密码不返回给前端
|
2025-12-02 10:33:19 +08:00
|
|
|
|
Email string `gorm:"column:email;type:varchar(255);not null;uniqueIndex:idx_user_email_status,priority:1" json:"email"`
|
2025-11-30 18:56:56 +08:00
|
|
|
|
Avatar string `gorm:"column:avatar;type:varchar(255);not null;default:''" json:"avatar"`
|
2025-12-02 10:33:19 +08:00
|
|
|
|
Points int `gorm:"column:points;type:integer;not null;default:0;index:idx_user_points,sort:desc" json:"points"`
|
|
|
|
|
|
Role string `gorm:"column:role;type:varchar(50);not null;default:'user';index:idx_user_role_status,priority:1" json:"role"`
|
|
|
|
|
|
Status int16 `gorm:"column:status;type:smallint;not null;default:1;index:idx_user_username_status,priority:2;index:idx_user_email_status,priority:2;index:idx_user_role_status,priority:2" json:"status"` // 1:正常, 0:禁用, -1:删除
|
2025-12-26 01:15:17 +08:00
|
|
|
|
Properties *datatypes.JSON `gorm:"column:properties;type:jsonb" json:"properties,omitempty" swaggertype:"string"` // JSON数据,存储为PostgreSQL的JSONB类型
|
2025-12-02 10:33:19 +08:00
|
|
|
|
LastLoginAt *time.Time `gorm:"column:last_login_at;type:timestamp;index:idx_user_last_login,sort:desc" json:"last_login_at,omitempty"`
|
|
|
|
|
|
CreatedAt time.Time `gorm:"column:created_at;type:timestamp;not null;default:CURRENT_TIMESTAMP;index:idx_user_created_at,sort:desc" json:"created_at"`
|
2025-11-30 18:56:56 +08:00
|
|
|
|
UpdatedAt time.Time `gorm:"column:updated_at;type:timestamp;not null;default:CURRENT_TIMESTAMP" json:"updated_at"`
|
2025-11-28 23:30:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TableName 指定表名
|
|
|
|
|
|
func (User) TableName() string {
|
|
|
|
|
|
return "user"
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// UserPointLog 用户积分变更记录
|
2025-12-26 01:15:17 +08:00
|
|
|
|
// @Description 用户积分变动日志记录
|
2025-11-28 23:30:49 +08:00
|
|
|
|
type UserPointLog struct {
|
|
|
|
|
|
ID int64 `gorm:"column:id;primaryKey;autoIncrement" json:"id"`
|
2025-12-02 10:33:19 +08:00
|
|
|
|
UserID int64 `gorm:"column:user_id;not null;index:idx_point_logs_user_created,priority:1" json:"user_id"`
|
|
|
|
|
|
ChangeType string `gorm:"column:change_type;type:varchar(50);not null;index:idx_point_logs_change_type" json:"change_type"` // EARN, SPEND, ADMIN_ADJUST
|
2025-11-28 23:30:49 +08:00
|
|
|
|
Amount int `gorm:"column:amount;type:integer;not null" json:"amount"`
|
|
|
|
|
|
BalanceBefore int `gorm:"column:balance_before;type:integer;not null" json:"balance_before"`
|
|
|
|
|
|
BalanceAfter int `gorm:"column:balance_after;type:integer;not null" json:"balance_after"`
|
|
|
|
|
|
Reason string `gorm:"column:reason;type:varchar(255);not null" json:"reason"`
|
|
|
|
|
|
ReferenceType string `gorm:"column:reference_type;type:varchar(50)" json:"reference_type,omitempty"`
|
|
|
|
|
|
ReferenceID *int64 `gorm:"column:reference_id;type:bigint" json:"reference_id,omitempty"`
|
2025-12-02 10:33:19 +08:00
|
|
|
|
OperatorID *int64 `gorm:"column:operator_id;type:bigint;index" json:"operator_id,omitempty"`
|
|
|
|
|
|
CreatedAt time.Time `gorm:"column:created_at;type:timestamp;not null;default:CURRENT_TIMESTAMP;index:idx_point_logs_user_created,priority:2,sort:desc;index:idx_point_logs_created_at,sort:desc" json:"created_at"`
|
2025-11-28 23:30:49 +08:00
|
|
|
|
|
|
|
|
|
|
// 关联
|
2025-12-02 10:33:19 +08:00
|
|
|
|
User *User `gorm:"foreignKey:UserID;constraint:OnDelete:CASCADE" json:"user,omitempty"`
|
|
|
|
|
|
Operator *User `gorm:"foreignKey:OperatorID;constraint:OnDelete:SET NULL" json:"operator,omitempty"`
|
2025-11-28 23:30:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TableName 指定表名
|
|
|
|
|
|
func (UserPointLog) TableName() string {
|
|
|
|
|
|
return "user_point_logs"
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// UserLoginLog 用户登录日志
|
2025-12-26 01:15:17 +08:00
|
|
|
|
// @Description 用户登录历史记录
|
2025-11-28 23:30:49 +08:00
|
|
|
|
type UserLoginLog struct {
|
|
|
|
|
|
ID int64 `gorm:"column:id;primaryKey;autoIncrement" json:"id"`
|
2025-12-02 10:33:19 +08:00
|
|
|
|
UserID int64 `gorm:"column:user_id;not null;index:idx_login_logs_user_created,priority:1" json:"user_id"`
|
|
|
|
|
|
IPAddress string `gorm:"column:ip_address;type:inet;not null;index:idx_login_logs_ip" json:"ip_address"`
|
2025-11-28 23:30:49 +08:00
|
|
|
|
UserAgent string `gorm:"column:user_agent;type:text" json:"user_agent,omitempty"`
|
|
|
|
|
|
LoginMethod string `gorm:"column:login_method;type:varchar(50);not null;default:'PASSWORD'" json:"login_method"`
|
2025-12-02 10:33:19 +08:00
|
|
|
|
IsSuccess bool `gorm:"column:is_success;not null;index:idx_login_logs_success" json:"is_success"`
|
2025-11-28 23:30:49 +08:00
|
|
|
|
FailureReason string `gorm:"column:failure_reason;type:varchar(255)" json:"failure_reason,omitempty"`
|
2025-12-02 10:33:19 +08:00
|
|
|
|
CreatedAt time.Time `gorm:"column:created_at;type:timestamp;not null;default:CURRENT_TIMESTAMP;index:idx_login_logs_user_created,priority:2,sort:desc;index:idx_login_logs_created_at,sort:desc" json:"created_at"`
|
2025-11-28 23:30:49 +08:00
|
|
|
|
|
|
|
|
|
|
// 关联
|
2025-12-02 10:33:19 +08:00
|
|
|
|
User *User `gorm:"foreignKey:UserID;constraint:OnDelete:CASCADE" json:"user,omitempty"`
|
2025-11-28 23:30:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TableName 指定表名
|
|
|
|
|
|
func (UserLoginLog) TableName() string {
|
|
|
|
|
|
return "user_login_logs"
|
|
|
|
|
|
}
|