package service import ( "errors" "strings" "time" "github.com/freefire/jiang13-bbs/model" "gorm.io/gorm" ) // CommentService 评论服务 type CommentService struct { db *gorm.DB } func NewCommentService(db *gorm.DB) *CommentService { return &CommentService{db: db} } // ListByPost 获取帖子的评论列表 func (s *CommentService) ListByPost(postID uint) ([]model.Comment, error) { var comments []model.Comment err := s.db.Where("post_id = ? AND status = ?", postID, model.ContentStatusPublished). Order("created_at ASC"). Preload("User"). Find(&comments).Error return comments, err } // Create 创建评论 func (s *CommentService) Create(userID, postID uint, content string) (*model.Comment, error) { content = strings.TrimSpace(content) if content == "" { return nil, errors.New("评论内容不能为空") } // 检查帖子是否存在且未锁定评论 var post model.Post if err := s.db.First(&post, postID).Error; err != nil { return nil, errors.New("帖子不存在") } comment := &model.Comment{ PostID: postID, UserID: userID, Content: content, Status: model.ContentStatusPublished, } if err := s.db.Create(comment).Error; err != nil { return nil, err } // 更新帖子评论数 s.db.Model(&post).UpdateColumn("comment_count", gorm.Expr("comment_count + 1")) // 预加载用户 s.db.Preload("User").First(comment, comment.ID) return comment, nil } // UserCommentItem 用户评论列表项(含帖子标题便于跳转) type UserCommentItem struct { ID uint `json:"id"` PostID uint `json:"post_id"` PostTitle string `json:"post_title"` Content string `json:"content"` CreatedAt time.Time `json:"created_at"` } // ListByUser 获取指定用户发表的评论(含所属帖子标题) func (s *CommentService) ListByUser(userID uint, page, size int) ([]UserCommentItem, int64, error) { if page < 1 { page = 1 } if size < 1 || size > 50 { size = 20 } query := s.db.Table("comments"). Select("comments.id, comments.post_id, posts.title AS post_title, comments.content, comments.created_at"). Joins("JOIN posts ON posts.id = comments.post_id"). Where("comments.user_id = ? AND comments.status = ? AND posts.deleted_at IS NULL", userID, model.ContentStatusPublished) var total int64 if err := query.Count(&total).Error; err != nil { return nil, 0, err } var items []UserCommentItem offset := (page - 1) * size if err := query.Order("comments.created_at DESC").Offset(offset).Limit(size).Scan(&items).Error; err != nil { return nil, 0, err } return items, total, nil } // CountByUser 统计用户评论数(仅统计未删除帖子上的评论,与列表保持一致) func (s *CommentService) CountByUser(userID uint) (int64, error) { var total int64 err := s.db.Table("comments"). Joins("JOIN posts ON posts.id = comments.post_id"). Where("comments.user_id = ? AND comments.status = ? AND posts.deleted_at IS NULL", userID, model.ContentStatusPublished). Count(&total).Error return total, err } // Delete 删除评论(仅作者或管理员可操作) func (s *CommentService) Delete(commentID, userID uint, role string) error { var comment model.Comment if err := s.db.First(&comment, commentID).Error; err != nil { return errors.New("评论不存在") } // 权限校验:作者本人或管理员 if comment.UserID != userID && role != RoleAdmin { return errors.New("无权限删除此评论") } // 软删除 if err := s.db.Delete(&comment).Error; err != nil { return err } // 更新帖子评论数 s.db.Model(&model.Post{}).Where("id = ?", comment.PostID). UpdateColumn("comment_count", gorm.Expr("GREATEST(comment_count - 1, 0)")) return nil }