package models import ( "time" "gorm.io/gorm" ) // Tag 标签模型 type Tag struct { 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 } // TagResponse 标签 API 响应 type TagResponse struct { ID uint `json:"id"` Name string `json:"name"` Slug string `json:"slug"` CreatedAt string `json:"createdAt"` UpdatedAt string `json:"updatedAt"` } // PostTag 文章标签关联模型 type PostTag struct { 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 }