数据结构优化
This commit is contained in:
@@ -1,12 +1,34 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// UserAccessLog 用户访问日志模型
|
||||
type UserAccessLog struct {
|
||||
ID uint `json:"id"`
|
||||
UserID uint `json:"user_id"` // 用户ID(未登录用户为0)
|
||||
UserIP string `json:"user_ip"` // 用户IP地址
|
||||
UserLocation string `json:"user_location"` // 用户归属地
|
||||
ArticleID uint `json:"article_id"` // 访问的文章ID
|
||||
AccessTime int64 `json:"access_time"` // 访问时间
|
||||
DeletedAt int64 `json:"deleted_at"`
|
||||
ID uint `json:"id" gorm:"primaryKey;column:id"`
|
||||
UserID uint `json:"user_id" gorm:"column:user_id;index"` // 用户ID(未登录用户为0)
|
||||
UserIP string `json:"user_ip" gorm:"column:user_ip;index"` // 用户IP地址
|
||||
UserLocation string `json:"user_location" gorm:"column:user_location"` // 用户归属地
|
||||
ArticleID uint `json:"article_id" gorm:"column:article_id;index"` // 访问的文章ID
|
||||
AccessTime int64 `json:"access_time" gorm:"column:access_time"` // 访问时间
|
||||
DeletedAt int64 `json:"deleted_at" gorm:"column:deleted_at;default:0"`
|
||||
}
|
||||
|
||||
// TableName 指定表名
|
||||
func (UserAccessLog) TableName() string {
|
||||
return "user_access_logs"
|
||||
}
|
||||
|
||||
// BeforeCreate 创建前钩子
|
||||
func (u *UserAccessLog) BeforeCreate(tx *gorm.DB) error {
|
||||
if u.AccessTime == 0 {
|
||||
u.AccessTime = time.Now().Unix()
|
||||
}
|
||||
if u.DeletedAt == 0 {
|
||||
u.DeletedAt = 0
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user