getpostlogic.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package logic
  2. import (
  3. "context"
  4. "errors"
  5. "slowwild/internal/constants"
  6. "slowwild/internal/errorx"
  7. "slowwild/internal/svc"
  8. "strconv"
  9. "strings"
  10. "git.banshen.xyz/huangguangrong/slow_wild_protobuff/slowwild/slowwildserver"
  11. "gorm.io/gorm"
  12. "github.com/spf13/cast"
  13. "github.com/zeromicro/go-zero/core/logx"
  14. )
  15. type GetPostLogic struct {
  16. ctx context.Context
  17. svcCtx *svc.ServiceContext
  18. logx.Logger
  19. }
  20. func NewGetPostLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetPostLogic {
  21. return &GetPostLogic{
  22. ctx: ctx,
  23. svcCtx: svcCtx,
  24. Logger: logx.WithContext(ctx),
  25. }
  26. }
  27. // 获取动态详情
  28. func (l *GetPostLogic) GetPost(in *slowwildserver.GetPostReq) (*slowwildserver.GetPostRsp, error) {
  29. userId, _ := cast.ToInt64E(l.ctx.Value(constants.UserIDKey))
  30. if in.PostId <= 0 {
  31. return nil, errorx.ErrInvalidParam
  32. }
  33. // 获取帖子基本信息
  34. post, err := l.svcCtx.PostModel.GetPostDetail(l.ctx, in.PostId)
  35. if err != nil {
  36. l.Logger.Errorf("获取帖子详情失败: %v", err)
  37. return nil, errorx.ErrPostQueryFailed
  38. }
  39. // 获取帖子内容
  40. content, err := l.svcCtx.PostModel.GetPostContent(l.ctx, in.PostId)
  41. if err != nil {
  42. l.Logger.Errorf("获取帖子内容失败: %v", err)
  43. return nil, errorx.ErrPostQueryFailed
  44. }
  45. // 获取用户信息
  46. user, err := l.svcCtx.UserModel.FindOne(l.ctx, post.UserId)
  47. if err != nil {
  48. l.Logger.Errorf("获取用户信息失败: %v", err)
  49. return nil, errorx.ErrUserQueryFailed
  50. }
  51. // 获取话题信息
  52. var tagItems []*slowwildserver.TagItem
  53. if post.Tags != "" {
  54. tagStrs := strings.Split(post.Tags, ",")
  55. var tagIds []int64
  56. for _, tagStr := range tagStrs {
  57. tagId, err := strconv.ParseInt(tagStr, 10, 64)
  58. if err != nil {
  59. continue
  60. }
  61. tagIds = append(tagIds, tagId)
  62. }
  63. tags, err := l.svcCtx.TagModel.FindByIds(l.ctx, tagIds)
  64. if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
  65. l.Logger.Errorf("获取话题信息失败: %v", err)
  66. return nil, errorx.ErrPostQueryFailed
  67. }
  68. for _, tag := range tags {
  69. tagItems = append(tagItems, &slowwildserver.TagItem{
  70. Id: tag.ID,
  71. Name: tag.Name,
  72. })
  73. }
  74. }
  75. var isLiked, isCollected bool
  76. // 获取用户交互状态
  77. if userId > 0 {
  78. isLiked, err = l.svcCtx.UserModel.IsPostLikedByUser(l.ctx, userId, post.ID)
  79. if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
  80. l.Logger.Errorf("查询用户点赞状态失败: %v", err)
  81. return nil, errorx.ErrPostQueryFailed
  82. }
  83. isCollected, err = l.svcCtx.UserModel.IsPostCollectedByUser(l.ctx, userId, post.ID)
  84. if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
  85. l.Logger.Errorf("查询用户收藏状态失败: %v", err)
  86. return nil, errorx.ErrPostQueryFailed
  87. }
  88. }
  89. // 获取@用户信息
  90. var atUserInfos []*slowwildserver.UserInfo
  91. if post.WithUserIds != "" {
  92. // 解析用户ID列表
  93. atUserIdStrs := strings.Split(post.WithUserIds, ",")
  94. var atUserIds []int64
  95. for _, idStr := range atUserIdStrs {
  96. userId, err := strconv.ParseInt(idStr, 10, 64)
  97. if err != nil {
  98. continue
  99. }
  100. atUserIds = append(atUserIds, userId)
  101. }
  102. // 批量获取用户信息
  103. if len(atUserIds) > 0 {
  104. atUsers, err := l.svcCtx.UserModel.FindByIds(l.ctx, atUserIds)
  105. if err != nil {
  106. l.Logger.Errorf("获取@用户信息失败: %v", err)
  107. return nil, errorx.ErrUserQueryFailed
  108. }
  109. // 构建用户信息列表
  110. for _, atUser := range atUsers {
  111. atUserInfos = append(atUserInfos, &slowwildserver.UserInfo{
  112. Id: atUser.ID,
  113. Nickname: atUser.Nickname,
  114. Avatar: atUser.Avatar,
  115. Sex: int32(atUser.Sex),
  116. })
  117. }
  118. }
  119. }
  120. // 构建响应
  121. resp := &slowwildserver.GetPostRsp{
  122. Id: post.ID,
  123. User: &slowwildserver.UserInfo{
  124. Id: user.ID,
  125. Nickname: user.Nickname,
  126. Avatar: user.Avatar,
  127. Sex: int32(user.Sex),
  128. },
  129. PostType: post.PostType,
  130. Title: content.Title,
  131. Content: content.Content,
  132. ContentSummary: content.ContentSummary,
  133. Images: strings.Split(content.PostCovers, ","),
  134. VideoUrl: content.PostVideoUrl,
  135. VideoCover: content.PostVideoCover,
  136. CommentCount: post.CommentCount,
  137. CollectionCount: post.CollectionCount,
  138. UpvoteCount: post.UpvoteCount,
  139. ShareCount: post.ShareCount,
  140. Tags: tagItems,
  141. IpLoc: post.IpLoc,
  142. HotNum: post.HotNum,
  143. CreatedOn: post.CreatedOn,
  144. IsLiked: isLiked,
  145. IsCollected: isCollected,
  146. IsMine: userId == post.UserId,
  147. LatestRepliedOn: post.LatestRepliedOn,
  148. WithUser: atUserInfos,
  149. }
  150. return resp, nil
  151. }