数据结构优化

This commit is contained in:
李琦
2026-01-19 16:14:08 +08:00
parent 6d789c7c2a
commit 68c4c6df1c
36 changed files with 4063 additions and 760 deletions

131
server/models/attachment.go Normal file
View File

@@ -0,0 +1,131 @@
package models
import (
"time"
"gorm.io/gorm"
)
// AttachmentCategory 附件分类模型
type AttachmentCategory struct {
ID uint `json:"id" gorm:"primaryKey;column:id"`
Name string `json:"name" gorm:"column:name"`
Description string `json:"description" gorm:"column:description;type:text"`
SortOrder uint `json:"sortOrder" gorm:"column:sort_order;default:0"`
DeletedAt int64 `json:"deletedAt" gorm:"column:deleted_at;default:0"`
CreatedAt int64 `json:"createdAt" gorm:"column:created_at"`
UpdatedAt int64 `json:"updatedAt" gorm:"column:updated_at"`
}
// TableName 指定表名
func (AttachmentCategory) TableName() string {
return "attachment_categories"
}
// BeforeCreate 创建前钩子
func (ac *AttachmentCategory) BeforeCreate(tx *gorm.DB) error {
now := time.Now().Unix()
if ac.CreatedAt == 0 {
ac.CreatedAt = now
}
if ac.UpdatedAt == 0 {
ac.UpdatedAt = now
}
if ac.DeletedAt == 0 {
ac.DeletedAt = 0
}
return nil
}
// BeforeUpdate 更新前钩子
func (ac *AttachmentCategory) BeforeUpdate(tx *gorm.DB) error {
ac.UpdatedAt = time.Now().Unix()
return nil
}
// Attachment 附件模型
type Attachment struct {
ID uint `json:"id" gorm:"primaryKey;column:id"`
CategoryID *uint `json:"categoryId,omitempty" gorm:"column:category_id"`
Category *AttachmentCategory `json:"category,omitempty" gorm:"foreignKey:CategoryID"`
OriginalName string `json:"originalName" gorm:"column:original_name"`
StoredName string `json:"storedName" gorm:"column:stored_name"`
FilePath string `json:"filePath" gorm:"column:file_path"`
FileURL string `json:"fileUrl" gorm:"column:file_url"`
FileSize int64 `json:"fileSize" gorm:"column:file_size"`
FileType string `json:"fileType" gorm:"column:file_type"` // image/video/document/other
MimeType string `json:"mimeType" gorm:"column:mime_type"`
StorageType string `json:"storageType" gorm:"column:storage_type;default:local"` // local/qcloud/aliyun/qiniu
OSSConfigID *uint `json:"ossConfigId,omitempty" gorm:"column:oss_config_id"`
DeletedAt int64 `json:"deletedAt" gorm:"column:deleted_at;default:0"`
CreatedAt int64 `json:"createdAt" gorm:"column:created_at"`
UpdatedAt int64 `json:"updatedAt" gorm:"column:updated_at"`
}
// TableName 指定表名
func (Attachment) TableName() string {
return "attachments"
}
// BeforeCreate 创建前钩子
func (a *Attachment) 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 *Attachment) BeforeUpdate(tx *gorm.DB) error {
a.UpdatedAt = time.Now().Unix()
return nil
}
// OSSConfig OSS配置模型
type OSSConfig struct {
ID uint `json:"id" gorm:"primaryKey;column:id"`
Name string `json:"name" gorm:"column:name"`
StorageType string `json:"storageType" gorm:"column:storage_type"` // local/qcloud/aliyun/qiniu
AccessKey string `json:"accessKey" gorm:"column:access_key;type:text"` // AES加密存储
SecretKey string `json:"secretKey" gorm:"column:secret_key;type:text"` // AES加密存储
Bucket string `json:"bucket" gorm:"column:bucket"`
Region string `json:"region" gorm:"column:region"`
Domain string `json:"domain" gorm:"column:domain"`
IsActive int `json:"isActive" gorm:"column:is_active;default:0"`
DeletedAt int64 `json:"deletedAt" gorm:"column:deleted_at;default:0"`
CreatedAt int64 `json:"createdAt" gorm:"column:created_at"`
UpdatedAt int64 `json:"updatedAt" gorm:"column:updated_at"`
}
// TableName 指定表名
func (OSSConfig) TableName() string {
return "oss_configs"
}
// BeforeCreate 创建前钩子
func (oc *OSSConfig) BeforeCreate(tx *gorm.DB) error {
now := time.Now().Unix()
if oc.CreatedAt == 0 {
oc.CreatedAt = now
}
if oc.UpdatedAt == 0 {
oc.UpdatedAt = now
}
if oc.DeletedAt == 0 {
oc.DeletedAt = 0
}
return nil
}
// BeforeUpdate 更新前钩子
func (oc *OSSConfig) BeforeUpdate(tx *gorm.DB) error {
oc.UpdatedAt = time.Now().Unix()
return nil
}

View File

@@ -12,7 +12,9 @@ type Post struct {
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
Category *Category `json:"category,omitempty" gorm:"foreignKey:CategoryID"` // For join query result
ColumnID *uint `json:"columnId,omitempty" gorm:"column:column_id;default:NULL"` // Optional column association
Column *Column `json:"column,omitempty" gorm:"foreignKey:ColumnID"` // 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"`
@@ -51,15 +53,20 @@ func (p *Post) BeforeUpdate(tx *gorm.DB) error {
// PostResponse 博客文章响应模型
type PostResponse struct {
ID uint `json:"id"`
Title string `json:"title"`
CategoryID uint `json:"categoryId"`
CategoryName string `json:"categoryName"`
CategorySlug string `json:"categorySlug"`
Date string `json:"date"`
Excerpt string `json:"excerpt,omitempty"`
Content string `json:"content,omitempty"`
Tags []Tag `json:"tags,omitempty"`
ID uint `json:"id"`
Title string `json:"title"`
CategoryID uint `json:"categoryId"`
CategoryName string `json:"categoryName"`
CategorySlug string `json:"categorySlug"`
ColumnID *uint `json:"columnId,omitempty"`
ColumnName string `json:"columnName,omitempty"`
ColumnSlug string `json:"columnSlug,omitempty"`
Date string `json:"date"`
Excerpt string `json:"excerpt,omitempty"`
Content *string `json:"content,omitempty"` // 使用指针类型,当 includeContent=true 时总是设置
Tags []Tag `json:"tags,omitempty"`
IsPublished int `json:"isPublished,omitempty"`
ReadCount uint `json:"readCount,omitempty"`
}
// PostHistory 文章历史记录模型

View File

@@ -0,0 +1,35 @@
package models
import (
"time"
"gorm.io/gorm"
)
// SearchLog 搜索记录模型
type SearchLog struct {
ID uint `json:"id" gorm:"primaryKey;column:id"`
Keyword string `json:"keyword" gorm:"column:keyword"`
SearchType string `json:"searchType" gorm:"column:search_type"` // category/tag/column/keyword
UserIP string `json:"userIp" gorm:"column:user_ip"`
UserLocation string `json:"userLocation" gorm:"column:user_location"`
DeletedAt int64 `json:"deletedAt" gorm:"column:deleted_at;default:0"`
CreatedAt int64 `json:"createdAt" gorm:"column:created_at"`
}
// TableName 指定表名
func (SearchLog) TableName() string {
return "search_logs"
}
// BeforeCreate 创建前钩子
func (sl *SearchLog) BeforeCreate(tx *gorm.DB) error {
now := time.Now().Unix()
if sl.CreatedAt == 0 {
sl.CreatedAt = now
}
if sl.DeletedAt == 0 {
sl.DeletedAt = 0
}
return nil
}