作者 tangxvhui

通用的获取评论列表

@@ -95,10 +95,12 @@ type ( @@ -95,10 +95,12 @@ type (
95 // 小程序获取文章的评论列表 95 // 小程序获取文章的评论列表
96 type ( 96 type (
97 MiniListArticleCommentRequest { 97 MiniListArticleCommentRequest {
98 - Page int64 `json:"page"`  
99 - Size int64 `json:"size"` 98 + Page int `json:"page"`
  99 + Size int `json:"size"`
100 CompanyId int64 `json:",optional"` 100 CompanyId int64 `json:",optional"`
101 - SectionId int64 `json:"sectionId"` 101 + UserId int64 `json:",optional"`
  102 + ArticleId int64 `json:"articleId"`
  103 + SectionId int64 `json:"sectionId,optional"`
102 } 104 }
103 MiniListArticleCommentResponse { 105 MiniListArticleCommentResponse {
104 Total int64 `json:"total"` 106 Total int64 `json:"total"`
@@ -22,6 +22,7 @@ func MiniGetArticleCommentHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { @@ -22,6 +22,7 @@ func MiniGetArticleCommentHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
22 l := comment.NewMiniGetArticleCommentLogic(r.Context(), svcCtx) 22 l := comment.NewMiniGetArticleCommentLogic(r.Context(), svcCtx)
23 token := contextdata.GetUserTokenFromCtx(r.Context()) 23 token := contextdata.GetUserTokenFromCtx(r.Context())
24 req.CompanyId = token.CompanyId 24 req.CompanyId = token.CompanyId
  25 + req.UserId = token.UserId
25 resp, err := l.MiniGetArticleComment(&req) 26 resp, err := l.MiniGetArticleComment(&req)
26 result.HttpResult(r, w, resp, err) 27 result.HttpResult(r, w, resp, err)
27 } 28 }
@@ -21,6 +21,7 @@ func MiniListArticleCommentHandler(svcCtx *svc.ServiceContext) http.HandlerFunc @@ -21,6 +21,7 @@ func MiniListArticleCommentHandler(svcCtx *svc.ServiceContext) http.HandlerFunc
21 l := comment.NewMiniListArticleCommentLogic(r.Context(), svcCtx) 21 l := comment.NewMiniListArticleCommentLogic(r.Context(), svcCtx)
22 token := contextdata.GetUserTokenFromCtx(r.Context()) 22 token := contextdata.GetUserTokenFromCtx(r.Context())
23 req.CompanyId = token.CompanyId 23 req.CompanyId = token.CompanyId
  24 + req.UserId = token.UserId
24 resp, err := l.MiniListArticleComment(&req) 25 resp, err := l.MiniListArticleComment(&req)
25 result.HttpResult(r, w, resp, err) 26 result.HttpResult(r, w, resp, err)
26 } 27 }
@@ -11,6 +11,7 @@ import ( @@ -11,6 +11,7 @@ import (
11 11
12 "text/template" 12 "text/template"
13 13
  14 + "github.com/samber/lo"
14 "github.com/zeromicro/go-zero/core/logx" 15 "github.com/zeromicro/go-zero/core/logx"
15 ) 16 )
16 17
@@ -49,13 +50,12 @@ func (l *MiniCreateArticleCommentLogic) MiniCreateArticleComment(req *types.Mini @@ -49,13 +50,12 @@ func (l *MiniCreateArticleCommentLogic) MiniCreateArticleComment(req *types.Mini
49 return nil, xerr.NewErrMsg("没有评论权限") 50 return nil, xerr.NewErrMsg("没有评论权限")
50 } 51 }
51 //查看评论权限, 52 //查看评论权限,
52 - //TODO 临时注释  
53 - // if len(articleInfo.WhoReview) > 0 {  
54 - // ok := lo.IndexOf(articleInfo.WhoReview, req.FromUserId)  
55 - // if ok < 0 {  
56 - // return nil, xerr.NewErrMsg("没有评论权限")  
57 - // }  
58 - // } 53 + if len(articleInfo.WhoReview) > 0 {
  54 + ok := lo.IndexOf(articleInfo.WhoReview, req.FromUserId)
  55 + if ok < 0 {
  56 + return nil, xerr.NewErrMsg("没有评论权限")
  57 + }
  58 + }
59 // 对段落进行评论 59 // 对段落进行评论
60 var selctionInfo *domain.ArticleSection 60 var selctionInfo *domain.ArticleSection
61 if req.SectionId > 0 { 61 if req.SectionId > 0 {
@@ -5,6 +5,8 @@ import ( @@ -5,6 +5,8 @@ import (
5 5
6 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc" 6 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
7 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types" 7 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
  8 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
  9 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
8 10
9 "github.com/zeromicro/go-zero/core/logx" 11 "github.com/zeromicro/go-zero/core/logx"
10 ) 12 )
@@ -24,7 +26,166 @@ func NewMiniListArticleCommentLogic(ctx context.Context, svcCtx *svc.ServiceCont @@ -24,7 +26,166 @@ func NewMiniListArticleCommentLogic(ctx context.Context, svcCtx *svc.ServiceCont
24 } 26 }
25 27
26 func (l *MiniListArticleCommentLogic) MiniListArticleComment(req *types.MiniListArticleCommentRequest) (resp *types.MiniListArticleCommentResponse, err error) { 28 func (l *MiniListArticleCommentLogic) MiniListArticleComment(req *types.MiniListArticleCommentRequest) (resp *types.MiniListArticleCommentResponse, err error) {
27 - // todo: add your logic here and delete this line 29 + // 先获取最顶层的评论
  30 + var conn = l.svcCtx.DefaultDBConn()
  31 + if req.Page > 40 {
  32 + req.Page = 40
  33 + }
  34 +
  35 + queryOption := domain.NewQueryOptions().
  36 + WithOffsetLimit(req.Page, req.Size).
  37 + MustWithKV("topId", 0).
  38 + MustWithKV("articleId", req.ArticleId).
  39 + MustWithKV("sectionId", req.SectionId).
  40 + MustWithKV("show", domain.CommentShowEnable).
  41 + MustWithKV("companyId", req.CompanyId)
28 42
  43 + cnt, commentList, err := l.svcCtx.ArticleCommentRepository.Find(l.ctx, conn, queryOption)
  44 + if err != nil {
  45 + return nil, xerr.NewErrMsgErr("获取评论信息失败", err)
  46 + }
  47 + if cnt == 0 {
  48 + resp = &types.MiniListArticleCommentResponse{
  49 + Total: 0,
  50 + List: make([]types.ArticleCommentAndReply, 0),
  51 + }
29 return 52 return
  53 + }
  54 +
  55 + queryOption = domain.NewQueryOptions().WithFindOnly().
  56 + MustWithKV("articleId", req.ArticleId).
  57 + MustWithKV("userId", req.UserId)
  58 + // 获取我点赞的评论
  59 + _, userFlagList, err := l.svcCtx.UserLoveFlagRepository.Find(l.ctx, conn, queryOption)
  60 + if err != nil {
  61 + return nil, xerr.NewErrMsgErr("获取评论信息失败", err)
  62 + }
  63 + // 我点赞的
  64 + flagMap := map[int64]struct{}{}
  65 + for _, val := range userFlagList {
  66 + flagMap[val.CommentId] = struct{}{}
  67 + }
  68 + resp = &types.MiniListArticleCommentResponse{
  69 + Total: cnt,
  70 + List: make([]types.ArticleCommentAndReply, len(commentList)),
  71 + }
  72 + // 获取回复的评论
  73 + for i, val := range commentList {
  74 + item := types.ArticleCommentAndReply{
  75 + Comment: types.ArticleCommentItem{
  76 + Id: val.Id,
  77 + Pid: val.Pid,
  78 + TopId: val.TopId,
  79 + ArtitcleId: val.ArticleId,
  80 + SectionId: val.SectionId,
  81 + FromUserId: val.FromUserId,
  82 + FromUser: types.CommentAuthor{
  83 + Id: val.FromUser.Id,
  84 + Name: val.FromUser.Name,
  85 + Avatar: val.FromUser.Avatar,
  86 + Position: val.FromUser.Position,
  87 + Company: val.FromUser.Company,
  88 + },
  89 + ToUserId: val.ToUserId,
  90 + ToUser: types.CommentAuthor{
  91 + Id: val.ToUser.Id,
  92 + Name: val.ToUser.Name,
  93 + Avatar: val.ToUser.Avatar,
  94 + Position: val.ToUser.Position,
  95 + Company: val.ToUser.Company,
  96 + },
  97 + SectionContent: val.SectionContent,
  98 + CountReply: val.CountReply,
  99 + CountUserLove: val.CountUserLove,
  100 + CountAdminLove: val.CountAdminLove,
  101 + AtWho: []types.CommentAtWho{},
  102 + CreatedAt: val.CreatedAt,
  103 + MeLoveFlag: 0,
  104 + },
  105 + Reply: []types.ArticleCommentItem{},
  106 + TotalReply: int64(val.CountReply),
  107 + }
  108 +
  109 + if _, ok := flagMap[val.Id]; ok {
  110 + item.Comment.MeLoveFlag = 1
  111 + }
  112 +
  113 + for _, val2 := range val.AtWho {
  114 + item.Comment.AtWho = append(item.Comment.AtWho, types.CommentAtWho{
  115 + Id: val2.Id,
  116 + Name: val2.Name,
  117 + })
  118 + }
  119 +
  120 + //获取回复的评论
  121 + cntReply, reply := l.listCommentReply(item.Comment.Id, flagMap)
  122 +
  123 + resp.List[i] = item
  124 + resp.List[i].Reply = reply
  125 + resp.List[i].TotalReply = cntReply
  126 + }
  127 + return resp, nil
  128 +}
  129 +
  130 +// listCommentReply
  131 +func (l *MiniListArticleCommentLogic) listCommentReply(commentId int64, loveFlagMap map[int64]struct{}) (cnt int64, replyList []types.ArticleCommentItem) {
  132 + var conn = l.svcCtx.DefaultDBConn()
  133 + queryOption := domain.NewQueryOptions().
  134 + WithOffsetLimit(1, 2).
  135 + MustWithKV("topId", commentId).
  136 + MustWithKV("show", domain.CommentShowEnable)
  137 +
  138 + cnt, commentList, err := l.svcCtx.ArticleCommentRepository.Find(l.ctx, conn, queryOption)
  139 + if err != nil {
  140 + return cnt, []types.ArticleCommentItem{}
  141 + }
  142 + if cnt == 0 {
  143 + replyList = []types.ArticleCommentItem{}
  144 + return cnt, replyList
  145 + }
  146 +
  147 + for _, val := range commentList {
  148 + item := types.ArticleCommentItem{
  149 + Id: val.Id,
  150 + Pid: val.Pid,
  151 + TopId: val.TopId,
  152 + ArtitcleId: val.ArticleId,
  153 + SectionId: val.SectionId,
  154 + FromUserId: val.FromUserId,
  155 + FromUser: types.CommentAuthor{
  156 + Id: val.FromUser.Id,
  157 + Name: val.FromUser.Name,
  158 + Avatar: val.FromUser.Avatar,
  159 + Position: val.FromUser.Position,
  160 + Company: val.FromUser.Company,
  161 + },
  162 + ToUserId: val.ToUserId,
  163 + ToUser: types.CommentAuthor{
  164 + Id: val.ToUser.Id,
  165 + Name: val.ToUser.Name,
  166 + Avatar: val.ToUser.Avatar,
  167 + Position: val.ToUser.Position,
  168 + Company: val.ToUser.Company,
  169 + },
  170 + SectionContent: val.SectionContent,
  171 + CountReply: val.CountReply,
  172 + CountUserLove: val.CountUserLove,
  173 + CountAdminLove: val.CountAdminLove,
  174 + AtWho: []types.CommentAtWho{},
  175 + CreatedAt: val.CreatedAt,
  176 + MeLoveFlag: 0,
  177 + }
  178 +
  179 + if _, ok := loveFlagMap[val.Id]; ok {
  180 + item.MeLoveFlag = 1
  181 + }
  182 + for _, val2 := range val.AtWho {
  183 + item.AtWho = append(item.AtWho, types.CommentAtWho{
  184 + Id: val2.Id,
  185 + Name: val2.Name,
  186 + })
  187 + }
  188 + replyList = append(replyList, item)
  189 + }
  190 + return cnt, replyList
30 } 191 }
@@ -50,10 +50,12 @@ type MiniCreateArticleCommentResponse struct { @@ -50,10 +50,12 @@ type MiniCreateArticleCommentResponse struct {
50 } 50 }
51 51
52 type MiniListArticleCommentRequest struct { 52 type MiniListArticleCommentRequest struct {
53 - Page int64 `json:"page"`  
54 - Size int64 `json:"size"` 53 + Page int `json:"page"`
  54 + Size int `json:"size"`
55 CompanyId int64 `json:",optional"` 55 CompanyId int64 `json:",optional"`
56 - SectionId int64 `json:"sectionId"` 56 + UserId int64 `json:",optional"`
  57 + ArticleId int64 `json:"articleId"`
  58 + SectionId int64 `json:"sectionId,optional"`
57 } 59 }
58 60
59 type MiniListArticleCommentResponse struct { 61 type MiniListArticleCommentResponse struct {
@@ -125,11 +125,23 @@ func (repository *ArticleCommentRepository) Find(ctx context.Context, conn trans @@ -125,11 +125,23 @@ func (repository *ArticleCommentRepository) Find(ctx context.Context, conn trans
125 queryFunc := func() (interface{}, error) { 125 queryFunc := func() (interface{}, error) {
126 tx = tx.Model(&ms).Order("id desc") 126 tx = tx.Model(&ms).Order("id desc")
127 if v, ok := queryOptions["topId"]; ok { 127 if v, ok := queryOptions["topId"]; ok {
128 - tx = tx.Where("top_id", v) 128 + tx = tx.Where("top_id=?", v)
129 } 129 }
130 if v, ok := queryOptions["show"]; ok { 130 if v, ok := queryOptions["show"]; ok {
131 - tx = tx.Where("show", v) 131 + tx = tx.Where("show=?", v)
132 } 132 }
  133 + if v, ok := queryOptions["articleId"]; ok {
  134 + tx = tx.Where("article_id=?", v)
  135 + }
  136 +
  137 + if v, ok := queryOptions["sectionId"]; ok {
  138 + tx = tx.Where("section_id=?", v)
  139 + }
  140 +
  141 + if v, ok := queryOptions["companyId"]; ok {
  142 + tx = tx.Where("company_id=?", v)
  143 + }
  144 +
133 if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil { 145 if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil {
134 return dms, tx.Error 146 return dms, tx.Error
135 } 147 }