Files
nl-blogs/server/models/attachment.go
2026-01-19 16:14:08 +08:00

132 lines
4.3 KiB
Go

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
}