package model import ( "time" "github.com/google/uuid" "gorm.io/gorm" ) // CommentStatus 评论状态 type CommentStatus string const ( CommentStatusDraft CommentStatus = "draft" CommentStatusPending CommentStatus = "pending" CommentStatusPublished CommentStatus = "published" CommentStatusRejected CommentStatus = "rejected" CommentStatusDeleted CommentStatus = "deleted" ) // Comment 评论实体 type Comment struct { ID string `json:"id" gorm:"type:varchar(36);primaryKey"` PostID string `json:"post_id" gorm:"type:varchar(36);not null;index:idx_comments_post_parent_status_created,priority:1"` UserID string `json:"user_id" gorm:"type:varchar(36);index;not null"` ParentID *string `json:"parent_id" gorm:"type:varchar(36);index:idx_comments_post_parent_status_created,priority:2"` // 父评论 ID(支持嵌套) RootID *string `json:"root_id" gorm:"type:varchar(36);index:idx_comments_root_status_created,priority:1"` // 根评论 ID(用于高效查询) Content string `json:"content" gorm:"type:text;not null"` Images string `json:"images" gorm:"type:text"` // 图片URL列表,JSON数组格式 // 关联 User *User `json:"-" gorm:"foreignKey:UserID"` Replies []*Comment `json:"-" gorm:"-"` // 子回复(手动加载,非 GORM 关联) // 审核状态 Status CommentStatus `json:"status" gorm:"type:varchar(20);default:published;index:idx_comments_post_parent_status_created,priority:3;index:idx_comments_root_status_created,priority:2"` // 统计 LikesCount int `json:"likes_count" gorm:"default:0"` RepliesCount int `json:"replies_count" gorm:"default:0"` // 软删除 DeletedAt gorm.DeletedAt `json:"-" gorm:"index"` // 时间戳 CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime;index:idx_comments_post_parent_status_created,priority:4,sort:asc;index:idx_comments_root_status_created,priority:3,sort:asc"` UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime"` } // BeforeCreate 创建前生成UUID func (c *Comment) BeforeCreate(tx *gorm.DB) error { if c.ID == "" { c.ID = uuid.New().String() } return nil } func (Comment) TableName() string { return "comments" } // CommentLike 评论点赞 type CommentLike struct { ID string `json:"id" gorm:"type:varchar(36);primaryKey"` CommentID string `json:"comment_id" gorm:"type:varchar(36);index;not null"` UserID string `json:"user_id" gorm:"type:varchar(36);index;not null"` CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"` } // BeforeCreate 创建前生成UUID func (cl *CommentLike) BeforeCreate(tx *gorm.DB) error { if cl.ID == "" { cl.ID = uuid.New().String() } return nil } func (CommentLike) TableName() string { return "comment_likes" }