数据结构优化

This commit is contained in:
李琦
2026-01-19 13:53:32 +08:00
parent e6edf7210b
commit 3443c10c92
38 changed files with 1556 additions and 2153 deletions

View File

@@ -1,5 +1,11 @@
package models
import (
"time"
"gorm.io/gorm"
)
type Experience struct {
Year string `json:"year"`
Role string `json:"role"`
@@ -8,19 +14,45 @@ type Experience struct {
// AboutProfile 关于我页面数据模型
type AboutProfile struct {
ID uint `json:"id"`
Name string `json:"name"`
Avatar string `json:"avatar"`
Location string `json:"location"`
Bio string `json:"bio"`
Email string `json:"email"`
Wechat string `json:"wechat"`
TechStack string `json:"-"` // Stored as string in DB
TechList []string `json:"techStack"` // Exposed as array in JSON
ExperiencesStr string `json:"-"` // Stored as string in DB
ExperienceList []Experience `json:"experiences"` // Exposed as array in JSON
IsPrimary bool `json:"isPrimary"`
CreatedAt int64 `json:"createdAt"`
UpdatedAt int64 `json:"updatedAt"`
DeletedAt int64 `json:"deletedAt"`
ID uint `json:"id" gorm:"primaryKey;column:id"`
Name string `json:"name" gorm:"column:name"`
Avatar string `json:"avatar" gorm:"column:avatar"`
Location string `json:"location" gorm:"column:location"`
Bio string `json:"bio" gorm:"column:bio;type:text"`
Email string `json:"email" gorm:"column:email"`
Wechat string `json:"wechat" gorm:"column:wechat"`
TechStack string `json:"-" gorm:"column:tech_stack;type:text"` // Stored as string in DB
TechList []string `json:"techStack" gorm:"-"` // Exposed as array in JSON
ExperiencesStr string `json:"-" gorm:"column:experiences;type:text"` // Stored as string in DB
ExperienceList []Experience `json:"experiences" gorm:"-"` // Exposed as array in JSON
IsPrimary bool `json:"isPrimary" gorm:"column:is_primary;default:0"`
CreatedAt int64 `json:"createdAt" gorm:"column:created_at"`
UpdatedAt int64 `json:"updatedAt" gorm:"column:updated_at"`
DeletedAt int64 `json:"deletedAt" gorm:"column:deleted_at;default:0"`
}
// TableName 指定表名
func (AboutProfile) TableName() string {
return "about_profiles"
}
// BeforeCreate 创建前钩子
func (a *AboutProfile) BeforeCreate(tx *gorm.DB) error {
now := time.Now().Unix()
if a.CreatedAt == 0 {
a.CreatedAt = now
}
if a.UpdatedAt == 0 {
a.UpdatedAt = now
}
if a.DeletedAt == 0 {
a.DeletedAt = 0
}
return nil
}
// BeforeUpdate 更新前钩子
func (a *AboutProfile) BeforeUpdate(tx *gorm.DB) error {
a.UpdatedAt = time.Now().Unix()
return nil
}

View File

@@ -1,15 +1,37 @@
package models
import (
"time"
"gorm.io/gorm"
)
// AccessLog 访问日志模型
type AccessLog struct {
ID uint `json:"id" gorm:"primaryKey"`
IP string `json:"ip"`
UserAgent string `json:"user_agent"`
Path string `json:"path"`
Method string `json:"method"`
StatusCode int `json:"status_code"`
ResponseTime int64 `json:"response_time"` // 毫秒
Region string `json:"region"` // IP归属地
CreatedAt int64 `json:"created_at"`
DeletedAt int64 `json:"deleted_at"`
ID uint `json:"id" gorm:"primaryKey;column:id"`
IP string `json:"ip" gorm:"column:ip;index"`
UserAgent string `json:"user_agent" gorm:"column:user_agent"`
Path string `json:"path" gorm:"column:path;index"`
Method string `json:"method" gorm:"column:method"`
StatusCode int `json:"status_code" gorm:"column:status_code"`
ResponseTime int64 `json:"response_time" gorm:"column:response_time"` // 毫秒
Region string `json:"region" gorm:"column:region"` // IP归属地
CreatedAt int64 `json:"created_at" gorm:"column:created_at"`
DeletedAt int64 `json:"deleted_at" gorm:"column:deleted_at;default:0"`
}
// TableName 指定表名
func (AccessLog) TableName() string {
return "access_logs"
}
// BeforeCreate 创建前钩子
func (a *AccessLog) BeforeCreate(tx *gorm.DB) error {
if a.CreatedAt == 0 {
a.CreatedAt = time.Now().Unix()
}
if a.DeletedAt == 0 {
a.DeletedAt = 0
}
return nil
}

View File

@@ -1,13 +1,45 @@
package models
import (
"time"
"gorm.io/gorm"
)
// Category 分类模型
type Category struct {
ID uint `json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
Description string `json:"description"`
SortOrder uint `json:"sortOrder"`
CreatedAt int64 `json:"createdAt"`
UpdatedAt int64 `json:"updatedAt"`
DeletedAt int64 `json:"deletedAt"`
ID uint `json:"id" gorm:"primaryKey;column:id"`
Name string `json:"name" gorm:"column:name"`
Slug string `json:"slug" gorm:"column:slug;uniqueIndex"`
Description string `json:"description" gorm:"column:description;type:text"`
SortOrder uint `json:"sortOrder" gorm:"column:sort_order;default:0"`
CreatedAt int64 `json:"createdAt" gorm:"column:created_at"`
UpdatedAt int64 `json:"updatedAt" gorm:"column:updated_at"`
DeletedAt int64 `json:"deletedAt" gorm:"column:deleted_at;default:0"`
}
// TableName 指定表名
func (Category) TableName() string {
return "categories"
}
// BeforeCreate 创建前钩子
func (c *Category) BeforeCreate(tx *gorm.DB) error {
now := time.Now().Unix()
if c.CreatedAt == 0 {
c.CreatedAt = now
}
if c.UpdatedAt == 0 {
c.UpdatedAt = now
}
if c.DeletedAt == 0 {
c.DeletedAt = 0
}
return nil
}
// BeforeUpdate 更新前钩子
func (c *Category) BeforeUpdate(tx *gorm.DB) error {
c.UpdatedAt = time.Now().Unix()
return nil
}

View File

@@ -1,22 +1,67 @@
package models
import (
"time"
"gorm.io/gorm"
)
// Column 专栏模型
type Column struct {
ID uint `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Cover string `json:"cover"`
IsActive int `json:"isActive"`
SortOrder uint `json:"sortOrder"`
CreatedAt int64 `json:"createdAt"`
UpdatedAt int64 `json:"updatedAt"`
DeletedAt int64 `json:"deletedAt"`
ID uint `json:"id" gorm:"primaryKey;column:id"`
Name string `json:"name" gorm:"column:name"`
Description string `json:"description" gorm:"column:description;type:text"`
Cover string `json:"cover" gorm:"column:cover"`
IsActive int `json:"isActive" gorm:"column:is_active;default:1"`
SortOrder uint `json:"sortOrder" gorm:"column:sort_order;default:0"`
CreatedAt int64 `json:"createdAt" gorm:"column:created_at"`
UpdatedAt int64 `json:"updatedAt" gorm:"column:updated_at"`
DeletedAt int64 `json:"deletedAt" gorm:"column:deleted_at;default:0"`
}
// TableName 指定表名
func (Column) TableName() string {
return "columns"
}
// BeforeCreate 创建前钩子
func (c *Column) BeforeCreate(tx *gorm.DB) error {
now := time.Now().Unix()
if c.CreatedAt == 0 {
c.CreatedAt = now
}
if c.UpdatedAt == 0 {
c.UpdatedAt = now
}
if c.DeletedAt == 0 {
c.DeletedAt = 0
}
return nil
}
// BeforeUpdate 更新前钩子
func (c *Column) BeforeUpdate(tx *gorm.DB) error {
c.UpdatedAt = time.Now().Unix()
return nil
}
// ColumnPost 专栏文章关联模型
type ColumnPost struct {
ColumnID uint `json:"columnId"`
PostID uint `json:"postId"`
SortOrder uint `json:"sortOrder"`
CreatedAt int64 `json:"createdAt"`
ColumnID uint `json:"columnId" gorm:"primaryKey;column:column_id"`
PostID uint `json:"postId" gorm:"primaryKey;column:post_id"`
SortOrder uint `json:"sortOrder" gorm:"column:sort_order;default:0"`
CreatedAt int64 `json:"createdAt" gorm:"column:created_at"`
}
// TableName 指定表名
func (ColumnPost) TableName() string {
return "column_posts"
}
// BeforeCreate 创建前钩子
func (cp *ColumnPost) BeforeCreate(tx *gorm.DB) error {
if cp.CreatedAt == 0 {
cp.CreatedAt = time.Now().Unix()
}
return nil
}

View File

@@ -1,27 +1,85 @@
package models
import (
"time"
"gorm.io/gorm"
)
// Inquiry 合作咨询
type Inquiry struct {
ID uint `json:"id"`
Name string `json:"name"`
Company string `json:"company"`
ContactMethod string `json:"contactMethod"` // email, wechat, phone
ContactValue string `json:"contactValue"`
Budget string `json:"budget"`
Description string `json:"description"`
Status int `json:"status"` // 0-Unread, 1-Read, 2-Contacted
CreatedAt int64 `json:"createdAt"`
UpdatedAt int64 `json:"updatedAt"`
DeletedAt int64 `json:"deletedAt"`
ID uint `json:"id" gorm:"primaryKey;column:id"`
Name string `json:"name" gorm:"column:name"`
Company string `json:"company" gorm:"column:company"`
ContactMethod string `json:"contactMethod" gorm:"column:contact_method"` // email, wechat, phone
ContactValue string `json:"contactValue" gorm:"column:contact_value"`
Budget string `json:"budget" gorm:"column:budget"`
Description string `json:"description" gorm:"column:description;type:text"`
Status int `json:"status" gorm:"column:status;default:0"` // 0-Unread, 1-Read, 2-Contacted
CreatedAt int64 `json:"createdAt" gorm:"column:created_at"`
UpdatedAt int64 `json:"updatedAt" gorm:"column:updated_at"`
DeletedAt int64 `json:"deletedAt" gorm:"column:deleted_at;default:0"`
}
// TableName 指定表名
func (Inquiry) TableName() string {
return "inquiries"
}
// BeforeCreate 创建前钩子
func (i *Inquiry) BeforeCreate(tx *gorm.DB) error {
now := time.Now().Unix()
if i.CreatedAt == 0 {
i.CreatedAt = now
}
if i.UpdatedAt == 0 {
i.UpdatedAt = now
}
if i.DeletedAt == 0 {
i.DeletedAt = 0
}
return nil
}
// BeforeUpdate 更新前钩子
func (i *Inquiry) BeforeUpdate(tx *gorm.DB) error {
i.UpdatedAt = time.Now().Unix()
return nil
}
// EmailSuffix 邮箱后缀配置
type EmailSuffix struct {
ID uint `json:"id"`
Suffix string `json:"suffix"`
IsActive bool `json:"isActive"`
SortOrder int `json:"sortOrder"`
CreatedAt int64 `json:"createdAt"`
UpdatedAt int64 `json:"updatedAt"`
DeletedAt int64 `json:"deletedAt"`
ID uint `json:"id" gorm:"primaryKey;column:id"`
Suffix string `json:"suffix" gorm:"column:suffix;uniqueIndex"`
IsActive bool `json:"isActive" gorm:"column:is_active;default:1"`
SortOrder int `json:"sortOrder" gorm:"column:sort_order;default:0"`
CreatedAt int64 `json:"createdAt" gorm:"column:created_at"`
UpdatedAt int64 `json:"updatedAt" gorm:"column:updated_at"`
DeletedAt int64 `json:"deletedAt" gorm:"column:deleted_at;default:0"`
}
// TableName 指定表名
func (EmailSuffix) TableName() string {
return "email_suffixes"
}
// BeforeCreate 创建前钩子
func (es *EmailSuffix) BeforeCreate(tx *gorm.DB) error {
now := time.Now().Unix()
if es.CreatedAt == 0 {
es.CreatedAt = now
}
if es.UpdatedAt == 0 {
es.UpdatedAt = now
}
if es.DeletedAt == 0 {
es.DeletedAt = 0
}
return nil
}
// BeforeUpdate 更新前钩子
func (es *EmailSuffix) BeforeUpdate(tx *gorm.DB) error {
es.UpdatedAt = time.Now().Unix()
return nil
}

View File

@@ -1,18 +1,40 @@
package models
import (
"time"
"gorm.io/gorm"
)
// OperationLog 操作日志模型
type OperationLog struct {
ID uint `json:"id"`
UserID uint `json:"userId"`
Username string `json:"username"`
IP string `json:"ip"`
Path string `json:"path"`
Method string `json:"method"`
Params string `json:"params"`
Status int `json:"status"`
Duration int `json:"duration"`
CreatedAt int64 `json:"createdAt"`
DeletedAt int64 `json:"deletedAt"`
ID uint `json:"id" gorm:"primaryKey;column:id"`
UserID uint `json:"userId" gorm:"column:user_id;index"`
Username string `json:"username" gorm:"column:username"`
IP string `json:"ip" gorm:"column:ip"`
Path string `json:"path" gorm:"column:path;index"`
Method string `json:"method" gorm:"column:method"`
Params string `json:"params" gorm:"column:params;type:text"`
Status int `json:"status" gorm:"column:status"`
Duration int `json:"duration" gorm:"column:duration"`
CreatedAt int64 `json:"createdAt" gorm:"column:created_at"`
DeletedAt int64 `json:"deletedAt" gorm:"column:deleted_at;default:0"`
}
// TableName 指定表名
func (OperationLog) TableName() string {
return "operation_logs"
}
// BeforeCreate 创建前钩子
func (o *OperationLog) BeforeCreate(tx *gorm.DB) error {
if o.CreatedAt == 0 {
o.CreatedAt = time.Now().Unix()
}
if o.DeletedAt == 0 {
o.DeletedAt = 0
}
return nil
}
// OperationLogResponse 操作日志响应模型

View File

@@ -1,14 +1,46 @@
package models
import (
"time"
"gorm.io/gorm"
)
// Permission 权限模型
type Permission struct {
ID uint `json:"id"`
Name string `json:"name"`
Resource string `json:"resource"`
Action string `json:"action"`
CreatedAt int64 `json:"createdAt"`
UpdatedAt int64 `json:"updatedAt"`
DeletedAt int64 `json:"deletedAt"`
ID uint `json:"id" gorm:"primaryKey;column:id"`
Name string `json:"name" gorm:"column:name"`
Resource string `json:"resource" gorm:"column:resource"`
Action string `json:"action" gorm:"column:action"`
CreatedAt int64 `json:"createdAt" gorm:"column:created_at"`
UpdatedAt int64 `json:"updatedAt" gorm:"column:updated_at"`
DeletedAt int64 `json:"deletedAt" gorm:"column:deleted_at;default:0"`
}
// TableName 指定表名
func (Permission) TableName() string {
return "permissions"
}
// BeforeCreate 创建前钩子
func (p *Permission) BeforeCreate(tx *gorm.DB) error {
now := time.Now().Unix()
if p.CreatedAt == 0 {
p.CreatedAt = now
}
if p.UpdatedAt == 0 {
p.UpdatedAt = now
}
if p.DeletedAt == 0 {
p.DeletedAt = 0
}
return nil
}
// BeforeUpdate 更新前钩子
func (p *Permission) BeforeUpdate(tx *gorm.DB) error {
p.UpdatedAt = time.Now().Unix()
return nil
}
// PermissionResponse 权限响应模型

View File

@@ -1,20 +1,52 @@
package models
import (
"time"
"gorm.io/gorm"
)
// Post 博客文章模型
type Post struct {
ID uint `json:"id"`
OriginalID string `json:"originalId,omitempty"` // For backward compatibility
Title string `json:"title"`
CategoryID uint `json:"categoryId"`
Category *Category `json:"category,omitempty"` // For join query result
Excerpt string `json:"excerpt"`
Content string `json:"content"`
ReadCount uint `json:"readCount"`
IsPublished int `json:"isPublished"` // 0: draft, 1: published
Tags []Tag `json:"tags"`
CreatedAt int64 `json:"createdAt"`
UpdatedAt int64 `json:"updatedAt"`
DeletedAt int64 `json:"deletedAt"`
ID uint `json:"id" gorm:"primaryKey;column:id"`
OriginalID string `json:"originalId,omitempty" gorm:"-"` // For backward compatibility
Title string `json:"title" gorm:"column:title"`
CategoryID uint `json:"categoryId" gorm:"column:category_id"`
Category *Category `json:"category,omitempty" gorm:"foreignKey:CategoryID"` // For join query result
Excerpt string `json:"excerpt" gorm:"column:excerpt"`
Content string `json:"content" gorm:"column:content;type:text"`
ReadCount uint `json:"readCount" gorm:"column:read_count;default:0"`
IsPublished int `json:"isPublished" gorm:"column:is_published;default:0"` // 0: draft, 1: published
Tags []Tag `json:"tags" gorm:"many2many:post_tags;joinForeignKey:post_id;joinReferences:tag_id"`
CreatedAt int64 `json:"createdAt" gorm:"column:created_at"`
UpdatedAt int64 `json:"updatedAt" gorm:"column:updated_at"`
DeletedAt int64 `json:"deletedAt" gorm:"column:deleted_at;default:0"`
}
// TableName 指定表名
func (Post) TableName() string {
return "posts"
}
// BeforeCreate 创建前钩子
func (p *Post) BeforeCreate(tx *gorm.DB) error {
now := time.Now().Unix()
if p.CreatedAt == 0 {
p.CreatedAt = now
}
if p.UpdatedAt == 0 {
p.UpdatedAt = now
}
if p.DeletedAt == 0 {
p.DeletedAt = 0
}
return nil
}
// BeforeUpdate 更新前钩子
func (p *Post) BeforeUpdate(tx *gorm.DB) error {
p.UpdatedAt = time.Now().Unix()
return nil
}
// PostResponse 博客文章响应模型
@@ -32,17 +64,34 @@ type PostResponse struct {
// PostHistory 文章历史记录模型
type PostHistory struct {
ID uint `json:"id"`
PostID uint `json:"postId"`
Version int `json:"version"`
Title string `json:"title"`
CategoryID uint `json:"categoryId"`
Excerpt string `json:"excerpt"`
Content string `json:"content"`
IsPublished int `json:"isPublished"`
ModifiedBy uint `json:"modifiedBy"`
ModifiedAt int64 `json:"modifiedAt"`
CreatedAt int64 `json:"createdAt"`
ID uint `json:"id" gorm:"primaryKey;column:id"`
PostID uint `json:"postId" gorm:"column:post_id;index"`
Version int `json:"version" gorm:"column:version"`
Title string `json:"title" gorm:"column:title"`
CategoryID uint `json:"categoryId" gorm:"column:category_id"`
Excerpt string `json:"excerpt" gorm:"column:excerpt"`
Content string `json:"content" gorm:"column:content;type:text"`
IsPublished int `json:"isPublished" gorm:"column:is_published"`
ModifiedBy uint `json:"modifiedBy" gorm:"column:modified_by"`
ModifiedAt int64 `json:"modifiedAt" gorm:"column:modified_at"`
CreatedAt int64 `json:"createdAt" gorm:"column:created_at"`
}
// TableName 指定表名
func (PostHistory) TableName() string {
return "post_history"
}
// BeforeCreate 创建前钩子
func (ph *PostHistory) BeforeCreate(tx *gorm.DB) error {
now := time.Now().Unix()
if ph.CreatedAt == 0 {
ph.CreatedAt = now
}
if ph.ModifiedAt == 0 {
ph.ModifiedAt = now
}
return nil
}
// PostHistoryResponse 文章历史记录响应模型

View File

@@ -1,14 +1,46 @@
package models
import (
"time"
"gorm.io/gorm"
)
// Role 角色模型
type Role struct {
ID uint `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Permissions []Permission `json:"permissions,omitempty"`
CreatedAt int64 `json:"createdAt"`
UpdatedAt int64 `json:"updatedAt"`
DeletedAt int64 `json:"deletedAt"`
ID uint `json:"id" gorm:"primaryKey;column:id"`
Name string `json:"name" gorm:"column:name;uniqueIndex"`
Description string `json:"description" gorm:"column:description"`
Permissions []Permission `json:"permissions,omitempty" gorm:"many2many:role_permissions;joinForeignKey:role_id;joinReferences:permission_id"`
CreatedAt int64 `json:"createdAt" gorm:"column:created_at"`
UpdatedAt int64 `json:"updatedAt" gorm:"column:updated_at"`
DeletedAt int64 `json:"deletedAt" gorm:"column:deleted_at;default:0"`
}
// TableName 指定表名
func (Role) TableName() string {
return "roles"
}
// BeforeCreate 创建前钩子
func (r *Role) BeforeCreate(tx *gorm.DB) error {
now := time.Now().Unix()
if r.CreatedAt == 0 {
r.CreatedAt = now
}
if r.UpdatedAt == 0 {
r.UpdatedAt = now
}
if r.DeletedAt == 0 {
r.DeletedAt = 0
}
return nil
}
// BeforeUpdate 更新前钩子
func (r *Role) BeforeUpdate(tx *gorm.DB) error {
r.UpdatedAt = time.Now().Unix()
return nil
}
// RoleResponse 角色响应模型

View File

@@ -1,28 +1,86 @@
package models
import (
"time"
"gorm.io/gorm"
)
// Testimonial 客户评价模型
type Testimonial struct {
ID uint `json:"id"`
Name string `json:"name"`
Role string `json:"role"`
Content string `json:"content"`
Avatar string `json:"avatar"`
Rating uint8 `json:"rating"`
SortOrder uint `json:"sortOrder"`
CreatedAt int64 `json:"createdAt"`
UpdatedAt int64 `json:"updatedAt"`
DeletedAt int64 `json:"deletedAt"`
ID uint `json:"id" gorm:"primaryKey;column:id"`
Name string `json:"name" gorm:"column:name"`
Role string `json:"role" gorm:"column:role"`
Content string `json:"content" gorm:"column:content;type:text"`
Avatar string `json:"avatar" gorm:"column:avatar"`
Rating uint8 `json:"rating" gorm:"column:rating;default:5"`
SortOrder uint `json:"sortOrder" gorm:"column:sort_order;default:0"`
CreatedAt int64 `json:"createdAt" gorm:"column:created_at"`
UpdatedAt int64 `json:"updatedAt" gorm:"column:updated_at"`
DeletedAt int64 `json:"deletedAt" gorm:"column:deleted_at;default:0"`
}
// TableName 指定表名
func (Testimonial) TableName() string {
return "testimonials"
}
// BeforeCreate 创建前钩子
func (t *Testimonial) BeforeCreate(tx *gorm.DB) error {
now := time.Now().Unix()
if t.CreatedAt == 0 {
t.CreatedAt = now
}
if t.UpdatedAt == 0 {
t.UpdatedAt = now
}
if t.DeletedAt == 0 {
t.DeletedAt = 0
}
return nil
}
// BeforeUpdate 更新前钩子
func (t *Testimonial) BeforeUpdate(tx *gorm.DB) error {
t.UpdatedAt = time.Now().Unix()
return nil
}
// Partner 合作伙伴模型
type Partner struct {
ID uint `json:"id"`
Name string `json:"name"`
Logo string `json:"logo"`
Description string `json:"description"`
URL string `json:"url"`
SortOrder uint `json:"sortOrder"`
CreatedAt int64 `json:"createdAt"`
UpdatedAt int64 `json:"updatedAt"`
DeletedAt int64 `json:"deletedAt"`
ID uint `json:"id" gorm:"primaryKey;column:id"`
Name string `json:"name" gorm:"column:name"`
Logo string `json:"logo" gorm:"column:logo"`
Description string `json:"description" gorm:"column:description;type:text"`
URL string `json:"url" gorm:"column:url"`
SortOrder uint `json:"sortOrder" gorm:"column:sort_order;default:0"`
CreatedAt int64 `json:"createdAt" gorm:"column:created_at"`
UpdatedAt int64 `json:"updatedAt" gorm:"column:updated_at"`
DeletedAt int64 `json:"deletedAt" gorm:"column:deleted_at;default:0"`
}
// TableName 指定表名
func (Partner) TableName() string {
return "partners"
}
// BeforeCreate 创建前钩子
func (p *Partner) BeforeCreate(tx *gorm.DB) error {
now := time.Now().Unix()
if p.CreatedAt == 0 {
p.CreatedAt = now
}
if p.UpdatedAt == 0 {
p.UpdatedAt = now
}
if p.DeletedAt == 0 {
p.DeletedAt = 0
}
return nil
}
// BeforeUpdate 更新前钩子
func (p *Partner) BeforeUpdate(tx *gorm.DB) error {
p.UpdatedAt = time.Now().Unix()
return nil
}

View File

@@ -1,14 +1,46 @@
package models
import (
"time"
"gorm.io/gorm"
)
// Setting 系统配置模型
type Setting struct {
ID uint `json:"id"`
KeyName string `json:"keyName"`
Value string `json:"value"`
Description string `json:"description"`
CreatedAt int64 `json:"createdAt"`
UpdatedAt int64 `json:"updatedAt"`
DeletedAt int64 `json:"deletedAt"`
ID uint `json:"id" gorm:"primaryKey;column:id"`
KeyName string `json:"keyName" gorm:"column:key_name;uniqueIndex"`
Value string `json:"value" gorm:"column:value;type:text"`
Description string `json:"description" gorm:"column:description"`
CreatedAt int64 `json:"createdAt" gorm:"column:created_at"`
UpdatedAt int64 `json:"updatedAt" gorm:"column:updated_at"`
DeletedAt int64 `json:"deletedAt" gorm:"column:deleted_at;default:0"`
}
// TableName 指定表名
func (Setting) TableName() string {
return "settings"
}
// BeforeCreate 创建前钩子
func (s *Setting) BeforeCreate(tx *gorm.DB) error {
now := time.Now().Unix()
if s.CreatedAt == 0 {
s.CreatedAt = now
}
if s.UpdatedAt == 0 {
s.UpdatedAt = now
}
if s.DeletedAt == 0 {
s.DeletedAt = 0
}
return nil
}
// BeforeUpdate 更新前钩子
func (s *Setting) BeforeUpdate(tx *gorm.DB) error {
s.UpdatedAt = time.Now().Unix()
return nil
}
// SettingResponse 系统配置响应模型

View File

@@ -1,16 +1,48 @@
package models
import (
"time"
"gorm.io/gorm"
)
// Snippet 代码片段模型
type Snippet struct {
ID string `json:"id"`
Title string `json:"title"`
Code string `json:"code"`
Type string `json:"type"`
Description string `json:"description"`
ViewCount uint `json:"viewCount"`
CreatedAt int64 `json:"createdAt"`
UpdatedAt int64 `json:"updatedAt"`
DeletedAt int64 `json:"deletedAt"`
ID string `json:"id" gorm:"primaryKey;column:id"`
Title string `json:"title" gorm:"column:title"`
Code string `json:"code" gorm:"column:code;type:text"`
Type string `json:"type" gorm:"column:type"`
Description string `json:"description" gorm:"column:description;type:text"`
ViewCount uint `json:"viewCount" gorm:"column:view_count;default:0"`
CreatedAt int64 `json:"createdAt" gorm:"column:created_at"`
UpdatedAt int64 `json:"updatedAt" gorm:"column:updated_at"`
DeletedAt int64 `json:"deletedAt" gorm:"column:deleted_at;default:0"`
}
// TableName 指定表名
func (Snippet) TableName() string {
return "snippets"
}
// BeforeCreate 创建前钩子
func (s *Snippet) BeforeCreate(tx *gorm.DB) error {
now := time.Now().Unix()
if s.CreatedAt == 0 {
s.CreatedAt = now
}
if s.UpdatedAt == 0 {
s.UpdatedAt = now
}
if s.DeletedAt == 0 {
s.DeletedAt = 0
}
return nil
}
// BeforeUpdate 更新前钩子
func (s *Snippet) BeforeUpdate(tx *gorm.DB) error {
s.UpdatedAt = time.Now().Unix()
return nil
}
// SnippetResponse 代码片段响应模型

View File

@@ -1,18 +1,63 @@
package models
import (
"time"
"gorm.io/gorm"
)
// Tag 标签模型
type Tag struct {
ID uint `json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
CreatedAt int64 `json:"createdAt"`
UpdatedAt int64 `json:"updatedAt"`
DeletedAt int64 `json:"deletedAt"`
ID uint `json:"id" gorm:"primaryKey;column:id"`
Name string `json:"name" gorm:"column:name"`
Slug string `json:"slug" gorm:"column:slug;uniqueIndex"`
CreatedAt int64 `json:"createdAt" gorm:"column:created_at"`
UpdatedAt int64 `json:"updatedAt" gorm:"column:updated_at"`
DeletedAt int64 `json:"deletedAt" gorm:"column:deleted_at;default:0"`
}
// TableName 指定表名
func (Tag) TableName() string {
return "tags"
}
// BeforeCreate 创建前钩子
func (t *Tag) BeforeCreate(tx *gorm.DB) error {
now := time.Now().Unix()
if t.CreatedAt == 0 {
t.CreatedAt = now
}
if t.UpdatedAt == 0 {
t.UpdatedAt = now
}
if t.DeletedAt == 0 {
t.DeletedAt = 0
}
return nil
}
// BeforeUpdate 更新前钩子
func (t *Tag) BeforeUpdate(tx *gorm.DB) error {
t.UpdatedAt = time.Now().Unix()
return nil
}
// PostTag 文章标签关联模型
type PostTag struct {
PostID string `json:"postId"`
TagID uint `json:"tagId"`
CreatedAt int64 `json:"createdAt"`
PostID uint `json:"postId" gorm:"primaryKey;column:post_id"`
TagID uint `json:"tagId" gorm:"primaryKey;column:tag_id"`
CreatedAt int64 `json:"createdAt" gorm:"column:created_at"`
}
// TableName 指定表名
func (PostTag) TableName() string {
return "post_tags"
}
// BeforeCreate 创建前钩子
func (pt *PostTag) BeforeCreate(tx *gorm.DB) error {
if pt.CreatedAt == 0 {
pt.CreatedAt = time.Now().Unix()
}
return nil
}

View File

@@ -1,18 +1,50 @@
package models
import (
"time"
"gorm.io/gorm"
)
// User 用户模型
type User struct {
ID uint `json:"id"`
Username string `json:"username"`
Email string `json:"email"`
ID uint `json:"id" gorm:"primaryKey;column:id"`
Username string `json:"username" gorm:"column:username;uniqueIndex;not null"`
Email string `json:"email" gorm:"column:email"`
Password string `json:"password,omitempty" gorm:"-"` // Virtual field for input
PasswordHash string `json:"-"`
RoleID uint `json:"roleId"`
Role string `json:"role"` // 保持兼容或者作为Role Name
IsActive int `json:"isActive"`
CreatedAt int64 `json:"createdAt"`
UpdatedAt int64 `json:"updatedAt"`
DeletedAt int64 `json:"deletedAt"`
PasswordHash string `json:"-" gorm:"column:password_hash"`
RoleID uint `json:"roleId" gorm:"column:role_id"`
Role string `json:"role" gorm:"column:role"` // 保持兼容或者作为Role Name
IsActive int `json:"isActive" gorm:"column:is_active;default:1"`
CreatedAt int64 `json:"createdAt" gorm:"column:created_at"`
UpdatedAt int64 `json:"updatedAt" gorm:"column:updated_at"`
DeletedAt int64 `json:"deletedAt" gorm:"column:deleted_at;default:0"`
}
// TableName 指定表名
func (User) TableName() string {
return "users"
}
// BeforeCreate 创建前钩子
func (u *User) BeforeCreate(tx *gorm.DB) error {
now := time.Now().Unix()
if u.CreatedAt == 0 {
u.CreatedAt = now
}
if u.UpdatedAt == 0 {
u.UpdatedAt = now
}
if u.DeletedAt == 0 {
u.DeletedAt = 0
}
return nil
}
// BeforeUpdate 更新前钩子
func (u *User) BeforeUpdate(tx *gorm.DB) error {
u.UpdatedAt = time.Now().Unix()
return nil
}
// UserResponse 用户响应模型

View File

@@ -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
}

View File

@@ -1,38 +1,102 @@
package models
import (
"time"
"gorm.io/gorm"
)
// Work 作品模型
type Work struct {
ID string `json:"id"`
Title string `json:"title"`
Category string `json:"category"`
Year string `json:"year"`
HeroImg string `json:"heroImg"`
Description string `json:"desc"`
IsFeatured int `json:"isFeatured"`
CreatedAt int64 `json:"createdAt"`
UpdatedAt int64 `json:"updatedAt"`
DeletedAt int64 `json:"deletedAt"`
ID string `json:"id" gorm:"primaryKey;column:id"`
Title string `json:"title" gorm:"column:title"`
Category string `json:"category" gorm:"column:category"`
Year string `json:"year" gorm:"column:year"`
HeroImg string `json:"heroImg" gorm:"column:hero_img"`
Description string `json:"desc" gorm:"column:description;type:text"`
IsFeatured int `json:"isFeatured" gorm:"column:is_featured;default:0"`
CreatedAt int64 `json:"createdAt" gorm:"column:created_at"`
UpdatedAt int64 `json:"updatedAt" gorm:"column:updated_at"`
DeletedAt int64 `json:"deletedAt" gorm:"column:deleted_at;default:0"`
}
// TableName 指定表名
func (Work) TableName() string {
return "works"
}
// BeforeCreate 创建前钩子
func (w *Work) BeforeCreate(tx *gorm.DB) error {
now := time.Now().Unix()
if w.CreatedAt == 0 {
w.CreatedAt = now
}
if w.UpdatedAt == 0 {
w.UpdatedAt = now
}
if w.DeletedAt == 0 {
w.DeletedAt = 0
}
return nil
}
// BeforeUpdate 更新前钩子
func (w *Work) BeforeUpdate(tx *gorm.DB) error {
w.UpdatedAt = time.Now().Unix()
return nil
}
// WorkTechStack 作品技术栈模型
type WorkTechStack struct {
ID uint `json:"id"`
WorkID string `json:"workId"`
Category string `json:"category"`
Item string `json:"item"`
CreatedAt int64 `json:"createdAt"`
DeletedAt int64 `json:"deletedAt"`
ID uint `json:"id" gorm:"primaryKey;column:id"`
WorkID string `json:"workId" gorm:"column:work_id;index"`
Category string `json:"category" gorm:"column:category"`
Item string `json:"item" gorm:"column:item"`
CreatedAt int64 `json:"createdAt" gorm:"column:created_at"`
DeletedAt int64 `json:"deletedAt" gorm:"column:deleted_at;default:0"`
}
// TableName 指定表名
func (WorkTechStack) TableName() string {
return "work_tech_stack"
}
// BeforeCreate 创建前钩子
func (wts *WorkTechStack) BeforeCreate(tx *gorm.DB) error {
if wts.CreatedAt == 0 {
wts.CreatedAt = time.Now().Unix()
}
if wts.DeletedAt == 0 {
wts.DeletedAt = 0
}
return nil
}
// WorkGallery 作品图库模型
type WorkGallery struct {
ID uint `json:"id"`
WorkID string `json:"workId"`
ImageURL string `json:"imageUrl"`
SortOrder uint `json:"sortOrder"`
Description string `json:"description"`
CreatedAt int64 `json:"createdAt"`
DeletedAt int64 `json:"deletedAt"`
ID uint `json:"id" gorm:"primaryKey;column:id"`
WorkID string `json:"workId" gorm:"column:work_id;index"`
ImageURL string `json:"imageUrl" gorm:"column:image_url"`
SortOrder uint `json:"sortOrder" gorm:"column:sort_order;default:0"`
Description string `json:"description" gorm:"column:description"`
CreatedAt int64 `json:"createdAt" gorm:"column:created_at"`
DeletedAt int64 `json:"deletedAt" gorm:"column:deleted_at;default:0"`
}
// TableName 指定表名
func (WorkGallery) TableName() string {
return "work_gallery"
}
// BeforeCreate 创建前钩子
func (wg *WorkGallery) BeforeCreate(tx *gorm.DB) error {
if wg.CreatedAt == 0 {
wg.CreatedAt = time.Now().Unix()
}
if wg.DeletedAt == 0 {
wg.DeletedAt = 0
}
return nil
}
// WorkResponse 作品响应模型,包含关联数据