70 lines
1.8 KiB
Go
70 lines
1.8 KiB
Go
package models
|
|
|
|
import (
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// CollectionAsset 典藏藏品表模型
|
|
type CollectionAsset struct {
|
|
ID int64 `gorm:"primaryKey;autoIncrement;column:id"`
|
|
AssetID int64 `gorm:"unique;not null;column:asset_id"`
|
|
OwnerUID int64 `gorm:"not null;index:idx_collection_owner_star;column:owner_uid"`
|
|
StarID int64 `gorm:"not null;index:idx_collection_owner_star;column:star_id"`
|
|
Name string `gorm:"type:varchar(100);not null;column:name"`
|
|
CoverURL string `gorm:"type:varchar(500);not null;column:cover_url"`
|
|
Category string `gorm:"type:varchar(50);not null;index:idx_collection_category;column:category"`
|
|
LikeCount int32 `gorm:"default:0;column:like_count"`
|
|
Status int32 `gorm:"default:0;column:status"` // 0=Pending, 1=Active
|
|
Metadata JSONB `gorm:"type:jsonb;column:metadata"`
|
|
CreatedAt int64 `gorm:"not null;column:created_at"`
|
|
UpdatedAt int64 `gorm:"not null;column:updated_at"`
|
|
}
|
|
|
|
// TableName 指定表名
|
|
func (CollectionAsset) TableName() string {
|
|
return "collection_assets"
|
|
}
|
|
|
|
// BeforeCreate 创建前钩子
|
|
func (c *CollectionAsset) BeforeCreate(tx *gorm.DB) error {
|
|
now := time.Now().UnixMilli()
|
|
c.CreatedAt = now
|
|
c.UpdatedAt = now
|
|
if c.Status == 0 {
|
|
c.Status = AssetStatusPending
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// BeforeUpdate 更新前钩子
|
|
func (c *CollectionAsset) BeforeUpdate(tx *gorm.DB) error {
|
|
c.UpdatedAt = time.Now().UnixMilli()
|
|
return nil
|
|
}
|
|
|
|
// 典藏藏品状态常量
|
|
const (
|
|
CollectionAssetStatusPending = 0 // 待处理
|
|
CollectionAssetStatusActive = 1 // 已激活
|
|
)
|
|
|
|
// JSONB 是 GORM 的 jsonb 类型别名
|
|
type JSONB []byte
|
|
|
|
// Scan 实现 sql.Scanner 接口
|
|
func (j *JSONB) Scan(value interface{}) error {
|
|
if value == nil {
|
|
*j = nil
|
|
return nil
|
|
}
|
|
bytes, ok := value.([]byte)
|
|
if !ok {
|
|
*j = nil
|
|
return nil
|
|
}
|
|
*j = bytes
|
|
return nil
|
|
}
|