package logic import ( "context" "slowwild/internal/errorx" "slowwild/internal/model" "slowwild/internal/svc" "strconv" "strings" "git.banshen.xyz/huangguangrong/slow_wild_protobuff/slowwild/slowwildserver" "github.com/zeromicro/go-zero/core/logx" ) type GetPostCommentListLogic struct { ctx context.Context svcCtx *svc.ServiceContext logx.Logger } func NewGetPostCommentListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetPostCommentListLogic { return &GetPostCommentListLogic{ ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx), } } // 获取评论列表 func (l *GetPostCommentListLogic) GetPostCommentList(in *slowwildserver.GetPostCommentListReq) (*slowwildserver.GetPostCommentListRsp, error) { if in.PostId <= 0 || in.Page <= 0 || in.PageSize <= 0 { return nil, errorx.ErrInvalidParam } // 获取评论列表 comments, err := l.svcCtx.CommentModel.GetCommentList(l.ctx, in.PostId, int(in.Page), int(in.PageSize)) if err != nil { l.Logger.Errorf("获取评论列表失败: %v", err) return nil, errorx.ErrCommentQueryFailed } // 收集所有用户ID(评论用户和被@用户) userIds := make(map[int64]bool) for _, comment := range comments { userIds[comment.UserId] = true if comment.WithUserIds != "" { for _, idStr := range strings.Split(comment.WithUserIds, ",") { if userId, err := strconv.ParseInt(idStr, 10, 64); err == nil { userIds[userId] = true } } } } // 批量获取用户信息 var userIdList []int64 for userId := range userIds { userIdList = append(userIdList, userId) } users, err := l.svcCtx.UserModel.FindByIds(l.ctx, userIdList) if err != nil { l.Logger.Errorf("批量获取用户信息失败: %v", err) return nil, errorx.ErrUserQueryFailed } // 构建用户信息map userMap := make(map[int64]*model.User) for _, user := range users { userMap[user.ID] = user } // 构建响应 resp := &slowwildserver.GetPostCommentListRsp{ List: make([]*slowwildserver.CommentItem, 0, len(comments)), } for _, comment := range comments { // 获取评论用户信息 user := userMap[comment.UserId] if user == nil { continue } // 获取@用户信息 var withUsers []*slowwildserver.UserInfo if comment.WithUserIds != "" { for _, idStr := range strings.Split(comment.WithUserIds, ",") { if userId, err := strconv.ParseInt(idStr, 10, 64); err == nil { if atUser := userMap[userId]; atUser != nil { withUsers = append(withUsers, &slowwildserver.UserInfo{ Id: atUser.ID, Nickname: atUser.Nickname, Avatar: atUser.Avatar, Sex: int32(atUser.Sex), }) } } } } // 获取最新的3条回复 replies, err := l.svcCtx.CommentModel.GetLatestReplies(l.ctx, comment.ID, 3) if err != nil { l.Logger.Errorf("获取评论回复失败: %v", err) continue } // 获取评论点赞状态 isUpvoted, err := l.svcCtx.CommentModel.IsCommentUpvotedByUser(l.ctx, in.UserId, comment.ID) if err != nil { l.Logger.Errorf("获取评论点赞状态失败: %v", err) continue } // 构建回复列表 var replyList []*slowwildserver.RepliesItem for _, reply := range replies { replyUser := userMap[reply.UserId] if replyUser == nil { continue } // 获取回复中的@用户信息 var replyWithUsers []*slowwildserver.UserInfo if reply.WithUserIds != "" { for _, idStr := range strings.Split(reply.WithUserIds, ",") { if userId, err := strconv.ParseInt(idStr, 10, 64); err == nil { if atUser := userMap[userId]; atUser != nil { replyWithUsers = append(replyWithUsers, &slowwildserver.UserInfo{ Id: atUser.ID, Nickname: atUser.Nickname, Avatar: atUser.Avatar, Sex: int32(atUser.Sex), }) } } } } // 获取回复点赞状态 isReplyUpvoted, err := l.svcCtx.CommentModel.IsCommentUpvotedByUser(l.ctx, in.UserId, reply.ID) if err != nil { l.Logger.Errorf("获取回复点赞状态失败: %v", err) continue } replyList = append(replyList, &slowwildserver.RepliesItem{ Id: reply.ID, User: &slowwildserver.UserInfo{ Id: replyUser.ID, Nickname: replyUser.Nickname, Avatar: replyUser.Avatar, Sex: int32(replyUser.Sex), }, Content: reply.Content, WithUser: replyWithUsers, UpvoteCount: reply.ThumbsUpCount, IpLoc: reply.IpLoc, CreatedOn: reply.CreatedOn, IsMine: in.UserId == reply.UserId, IsLiked: isReplyUpvoted, }) } resp.List = append(resp.List, &slowwildserver.CommentItem{ Id: comment.ID, User: &slowwildserver.UserInfo{ Id: user.ID, Nickname: user.Nickname, Avatar: user.Avatar, Sex: int32(user.Sex), }, Content: comment.Content, WithUser: withUsers, ReplyCount: comment.ReplyCount, UpvoteCount: comment.ThumbsUpCount, IpLoc: comment.IpLoc, CreatedOn: comment.CreatedOn, IsMine: in.UserId == comment.UserId, ReplyItem: replyList, IsLiked: isUpvoted, }) } return resp, nil }