getreplylistlogic.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 GetReplyListLogic struct {
  13. ctx context.Context
  14. svcCtx *svc.ServiceContext
  15. logx.Logger
  16. }
  17. func NewGetReplyListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetReplyListLogic {
  18. return &GetReplyListLogic{
  19. ctx: ctx,
  20. svcCtx: svcCtx,
  21. Logger: logx.WithContext(ctx),
  22. }
  23. }
  24. // 获取回复列表
  25. func (l *GetReplyListLogic) GetReplyList(in *slowwildserver.GetReplyListReq) (*slowwildserver.GetReplyListRsp, error) {
  26. if in.CommentId <= 0 || in.Page <= 0 || in.PageSize <= 0 {
  27. return nil, errorx.ErrInvalidParam
  28. }
  29. // 获取回复列表
  30. replies, err := l.svcCtx.CommentModel.GetReplyList(l.ctx, in.CommentId, 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 _, reply := range replies {
  38. userIds[reply.UserId] = true
  39. if reply.WithUserIds != "" {
  40. for _, idStr := range strings.Split(reply.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.GetReplyListRsp{
  64. List: make([]*slowwildserver.RepliesItem, 0, len(replies)),
  65. }
  66. for _, reply := range replies {
  67. // 获取回复用户信息
  68. user := userMap[reply.UserId]
  69. if user == nil {
  70. continue
  71. }
  72. // 获取@用户信息
  73. var withUsers []*slowwildserver.UserInfo
  74. if reply.WithUserIds != "" {
  75. for _, idStr := range strings.Split(reply.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. // 获取回复点赞状态
  89. isUpvoted, err := l.svcCtx.CommentModel.IsCommentUpvotedByUser(l.ctx, in.UserId, reply.ID)
  90. if err != nil {
  91. l.Logger.Errorf("获取回复点赞状态失败: %v", err)
  92. continue
  93. }
  94. resp.List = append(resp.List, &slowwildserver.RepliesItem{
  95. Id: reply.ID,
  96. User: &slowwildserver.UserInfo{
  97. Id: user.ID,
  98. Nickname: user.Nickname,
  99. Avatar: user.Avatar,
  100. Sex: int32(user.Sex),
  101. },
  102. Content: reply.Content,
  103. WithUser: withUsers,
  104. UpvoteCount: reply.ThumbsUpCount,
  105. IpLoc: reply.IpLoc,
  106. CreatedOn: reply.CreatedOn,
  107. IsMine: in.UserId == reply.UserId,
  108. IsLiked: isUpvoted,
  109. })
  110. }
  111. return resp, nil
  112. }