getpostcommentlistlogic.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package logic
  2. import (
  3. "context"
  4. "slowwild/internal/errorx"
  5. "slowwild/internal/model"
  6. "slowwild/internal/svc"
  7. "strconv"
  8. "strings"
  9. "git.banshen.xyz/huangguangrong/slow_wild_protobuff/slowwild/slowwildserver"
  10. "github.com/zeromicro/go-zero/core/logx"
  11. )
  12. type GetPostCommentListLogic struct {
  13. ctx context.Context
  14. svcCtx *svc.ServiceContext
  15. logx.Logger
  16. }
  17. func NewGetPostCommentListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetPostCommentListLogic {
  18. return &GetPostCommentListLogic{
  19. ctx: ctx,
  20. svcCtx: svcCtx,
  21. Logger: logx.WithContext(ctx),
  22. }
  23. }
  24. // 获取评论列表
  25. func (l *GetPostCommentListLogic) GetPostCommentList(in *slowwildserver.GetPostCommentListReq) (*slowwildserver.GetPostCommentListRsp, error) {
  26. if in.PostId <= 0 || in.Page <= 0 || in.PageSize <= 0 {
  27. return nil, errorx.ErrInvalidParam
  28. }
  29. // 获取评论列表
  30. comments, err := l.svcCtx.CommentModel.GetCommentList(l.ctx, in.PostId, int(in.Page), int(in.PageSize))
  31. if err != nil {
  32. l.Logger.Errorf("获取评论列表失败: %v", err)
  33. return nil, errorx.ErrCommentQueryFailed
  34. }
  35. // 收集所有用户ID(评论用户和被@用户)
  36. userIds := make(map[int64]bool)
  37. for _, comment := range comments {
  38. userIds[comment.UserId] = true
  39. if comment.WithUserIds != "" {
  40. for _, idStr := range strings.Split(comment.WithUserIds, ",") {
  41. if userId, err := strconv.ParseInt(idStr, 10, 64); err == nil {
  42. userIds[userId] = true
  43. }
  44. }
  45. }
  46. }
  47. // 批量获取用户信息
  48. var userIdList []int64
  49. for userId := range userIds {
  50. userIdList = append(userIdList, userId)
  51. }
  52. users, err := l.svcCtx.UserModel.FindByIds(l.ctx, userIdList)
  53. if err != nil {
  54. l.Logger.Errorf("批量获取用户信息失败: %v", err)
  55. return nil, errorx.ErrUserQueryFailed
  56. }
  57. // 构建用户信息map
  58. userMap := make(map[int64]*model.User)
  59. for _, user := range users {
  60. userMap[user.ID] = user
  61. }
  62. // 构建响应
  63. resp := &slowwildserver.GetPostCommentListRsp{
  64. List: make([]*slowwildserver.CommentItem, 0, len(comments)),
  65. }
  66. for _, comment := range comments {
  67. // 获取评论用户信息
  68. user := userMap[comment.UserId]
  69. if user == nil {
  70. continue
  71. }
  72. // 获取@用户信息
  73. var withUsers []*slowwildserver.UserInfo
  74. if comment.WithUserIds != "" {
  75. for _, idStr := range strings.Split(comment.WithUserIds, ",") {
  76. if userId, err := strconv.ParseInt(idStr, 10, 64); err == nil {
  77. if atUser := userMap[userId]; atUser != nil {
  78. withUsers = append(withUsers, &slowwildserver.UserInfo{
  79. Id: atUser.ID,
  80. Nickname: atUser.Nickname,
  81. Avatar: atUser.Avatar,
  82. Sex: int32(atUser.Sex),
  83. })
  84. }
  85. }
  86. }
  87. }
  88. // 获取最新的3条回复
  89. replies, err := l.svcCtx.CommentModel.GetLatestReplies(l.ctx, comment.ID, 3)
  90. if err != nil {
  91. l.Logger.Errorf("获取评论回复失败: %v", err)
  92. continue
  93. }
  94. // 获取评论点赞状态
  95. isUpvoted, err := l.svcCtx.CommentModel.IsCommentUpvotedByUser(l.ctx, in.UserId, comment.ID)
  96. if err != nil {
  97. l.Logger.Errorf("获取评论点赞状态失败: %v", err)
  98. continue
  99. }
  100. // 构建回复列表
  101. var replyList []*slowwildserver.RepliesItem
  102. for _, reply := range replies {
  103. replyUser := userMap[reply.UserId]
  104. if replyUser == nil {
  105. continue
  106. }
  107. // 获取回复中的@用户信息
  108. var replyWithUsers []*slowwildserver.UserInfo
  109. if reply.WithUserIds != "" {
  110. for _, idStr := range strings.Split(reply.WithUserIds, ",") {
  111. if userId, err := strconv.ParseInt(idStr, 10, 64); err == nil {
  112. if atUser := userMap[userId]; atUser != nil {
  113. replyWithUsers = append(replyWithUsers, &slowwildserver.UserInfo{
  114. Id: atUser.ID,
  115. Nickname: atUser.Nickname,
  116. Avatar: atUser.Avatar,
  117. Sex: int32(atUser.Sex),
  118. })
  119. }
  120. }
  121. }
  122. }
  123. // 获取回复点赞状态
  124. isReplyUpvoted, err := l.svcCtx.CommentModel.IsCommentUpvotedByUser(l.ctx, in.UserId, reply.ID)
  125. if err != nil {
  126. l.Logger.Errorf("获取回复点赞状态失败: %v", err)
  127. continue
  128. }
  129. replyList = append(replyList, &slowwildserver.RepliesItem{
  130. Id: reply.ID,
  131. User: &slowwildserver.UserInfo{
  132. Id: replyUser.ID,
  133. Nickname: replyUser.Nickname,
  134. Avatar: replyUser.Avatar,
  135. Sex: int32(replyUser.Sex),
  136. },
  137. Content: reply.Content,
  138. WithUser: replyWithUsers,
  139. UpvoteCount: reply.ThumbsUpCount,
  140. IpLoc: reply.IpLoc,
  141. CreatedOn: reply.CreatedOn,
  142. IsMine: in.UserId == reply.UserId,
  143. IsLiked: isReplyUpvoted,
  144. })
  145. }
  146. resp.List = append(resp.List, &slowwildserver.CommentItem{
  147. Id: comment.ID,
  148. User: &slowwildserver.UserInfo{
  149. Id: user.ID,
  150. Nickname: user.Nickname,
  151. Avatar: user.Avatar,
  152. Sex: int32(user.Sex),
  153. },
  154. Content: comment.Content,
  155. WithUser: withUsers,
  156. ReplyCount: comment.ReplyCount,
  157. UpvoteCount: comment.ThumbsUpCount,
  158. IpLoc: comment.IpLoc,
  159. CreatedOn: comment.CreatedOn,
  160. IsMine: in.UserId == comment.UserId,
  161. ReplyItem: replyList,
  162. IsLiked: isUpvoted,
  163. })
  164. }
  165. return resp, nil
  166. }