作者 tangxvhui

热门前5的评论

@@ -28,6 +28,10 @@ service Core { @@ -28,6 +28,10 @@ service Core {
28 @handler MiniListArticleComment 28 @handler MiniListArticleComment
29 post /article_comment/list (MiniListArticleCommentRequest) returns (MiniListArticleCommentResponse) 29 post /article_comment/list (MiniListArticleCommentRequest) returns (MiniListArticleCommentResponse)
30 30
  31 + @doc "小程序展示文章的评论列表TOP5"
  32 + @handler MiniTop5ArticleComment
  33 + post /article_comment/top5 (MiniTop5ArticleCommentRequest) returns (MiniTop5ArticleCommentResponse)
  34 +
31 @doc "小程序展示单个文章的评论" 35 @doc "小程序展示单个文章的评论"
32 @handler MiniGetArticleComment 36 @handler MiniGetArticleComment
33 get /article_comment/:id (MiniGetArticleCommentRequest) returns (MiniGetArticleCommentResponse) 37 get /article_comment/:id (MiniGetArticleCommentRequest) returns (MiniGetArticleCommentResponse)
@@ -155,4 +159,16 @@ type ( @@ -155,4 +159,16 @@ type (
155 MiniDeleteArticleCommentResponse { 159 MiniDeleteArticleCommentResponse {
156 Id int64 `json:"id"` 160 Id int64 `json:"id"`
157 } 161 }
  162 +)
  163 +
  164 +type (
  165 + MiniTop5ArticleCommentRequest {
  166 + CompanyId int64 `json:",optional"`
  167 + UserId int64 `json:",optional"`
  168 + ArticleId int64 `json:"articleId"`
  169 + }
  170 +
  171 + MiniTop5ArticleCommentResponse {
  172 + List []ArticleCommentItem `json:"list"`
  173 + }
158 ) 174 )
  1 +package comment
  2 +
  3 +import (
  4 + "net/http"
  5 +
  6 + "github.com/zeromicro/go-zero/rest/httpx"
  7 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/logic/comment"
  8 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
  9 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
  10 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/contextdata"
  11 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/result"
  12 +)
  13 +
  14 +func MiniTop5ArticleCommentHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
  15 + return func(w http.ResponseWriter, r *http.Request) {
  16 + var req types.MiniTop5ArticleCommentRequest
  17 + if err := httpx.Parse(r, &req); err != nil {
  18 + httpx.ErrorCtx(r.Context(), w, err)
  19 + return
  20 + }
  21 +
  22 + l := comment.NewMiniTop5ArticleCommentLogic(r.Context(), svcCtx)
  23 + token := contextdata.GetUserTokenFromCtx(r.Context())
  24 + req.CompanyId = token.CompanyId
  25 + req.UserId = token.UserId
  26 + resp, err := l.MiniTop5ArticleComment(&req)
  27 + result.HttpResult(r, w, resp, err)
  28 + }
  29 +}
@@ -36,6 +36,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { @@ -36,6 +36,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
36 Handler: comment.MiniListArticleCommentHandler(serverCtx), 36 Handler: comment.MiniListArticleCommentHandler(serverCtx),
37 }, 37 },
38 { 38 {
  39 + Method: http.MethodPost,
  40 + Path: "/article_comment/top5",
  41 + Handler: comment.MiniTop5ArticleCommentHandler(serverCtx),
  42 + },
  43 + {
39 Method: http.MethodGet, 44 Method: http.MethodGet,
40 Path: "/article_comment/:id", 45 Path: "/article_comment/:id",
41 Handler: comment.MiniGetArticleCommentHandler(serverCtx), 46 Handler: comment.MiniGetArticleCommentHandler(serverCtx),
@@ -2,6 +2,7 @@ package article @@ -2,6 +2,7 @@ package article
2 2
3 import ( 3 import (
4 "context" 4 "context"
  5 + "text/template"
5 6
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/svc"
7 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types" 8 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
@@ -29,6 +30,10 @@ func NewMiniCreateArticleDraftLogic(ctx context.Context, svcCtx *svc.ServiceCont @@ -29,6 +30,10 @@ func NewMiniCreateArticleDraftLogic(ctx context.Context, svcCtx *svc.ServiceCont
29 func (l *MiniCreateArticleDraftLogic) MiniCreateArticleDraft(req *types.MiniArticleDraftCreateRequest) (resp *types.MiniArticleDraftCreateResponse, err error) { 30 func (l *MiniCreateArticleDraftLogic) MiniCreateArticleDraft(req *types.MiniArticleDraftCreateRequest) (resp *types.MiniArticleDraftCreateResponse, err error) {
30 var conn = l.svcCtx.DefaultDBConn() 31 var conn = l.svcCtx.DefaultDBConn()
31 32
  33 + for i := range req.Section {
  34 + req.Section[i] = template.HTMLEscapeString(req.Section[i])
  35 + }
  36 +
32 newDraft := domain.ArticleDraft{ 37 newDraft := domain.ArticleDraft{
33 Id: 0, 38 Id: 0,
34 CompanyId: req.CompanyId, 39 CompanyId: req.CompanyId,
@@ -3,6 +3,7 @@ package article @@ -3,6 +3,7 @@ package article
3 import ( 3 import (
4 "context" 4 "context"
5 "strings" 5 "strings"
  6 + "text/template"
6 7
7 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc" 8 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
8 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types" 9 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
@@ -124,6 +125,8 @@ func (l *MiniCreateArticleLogic) MiniCreateArticle(req *types.MiniArticleCreateR @@ -124,6 +125,8 @@ func (l *MiniCreateArticleLogic) MiniCreateArticle(req *types.MiniArticleCreateR
124 if len(newStr) == 0 { 125 if len(newStr) == 0 {
125 continue 126 continue
126 } 127 }
  128 + newStr = template.HTMLEscapeString(newStr)
  129 +
127 newSection := domain.ArticleSection{ 130 newSection := domain.ArticleSection{
128 Id: 0, 131 Id: 0,
129 CompanyId: author.CompanyId, 132 CompanyId: author.CompanyId,
@@ -2,6 +2,7 @@ package article @@ -2,6 +2,7 @@ package article
2 2
3 import ( 3 import (
4 "context" 4 "context"
  5 + "text/template"
5 6
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/svc"
7 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types" 8 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
@@ -37,6 +38,10 @@ func (l *MiniUpdateArticleDraftLogic) MiniUpdateArticleDraft(req *types.MiniArti @@ -37,6 +38,10 @@ func (l *MiniUpdateArticleDraftLogic) MiniUpdateArticleDraft(req *types.MiniArti
37 return nil, xerr.NewErrMsg("更新草稿失败") 38 return nil, xerr.NewErrMsg("更新草稿失败")
38 } 39 }
39 } 40 }
  41 + for i := range req.Section {
  42 + req.Section[i] = template.HTMLEscapeString(req.Section[i])
  43 + }
  44 +
40 draftInfo.Content = req.Section 45 draftInfo.Content = req.Section
41 draftInfo.Title = req.Title 46 draftInfo.Title = req.Title
42 draftInfo.Location = domain.Location{ 47 draftInfo.Location = domain.Location{
@@ -11,7 +11,6 @@ import ( @@ -11,7 +11,6 @@ import (
11 11
12 "text/template" 12 "text/template"
13 13
14 - "github.com/samber/lo"  
15 "github.com/zeromicro/go-zero/core/logx" 14 "github.com/zeromicro/go-zero/core/logx"
16 ) 15 )
17 16
@@ -50,12 +49,12 @@ func (l *MiniCreateArticleCommentLogic) MiniCreateArticleComment(req *types.Mini @@ -50,12 +49,12 @@ func (l *MiniCreateArticleCommentLogic) MiniCreateArticleComment(req *types.Mini
50 return nil, xerr.NewErrMsg("没有评论权限") 49 return nil, xerr.NewErrMsg("没有评论权限")
51 } 50 }
52 //查看评论权限, 51 //查看评论权限,
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 - } 52 + // if len(articleInfo.WhoReview) > 0 {
  53 + // ok := lo.IndexOf(articleInfo.WhoReview, req.FromUserId)
  54 + // if ok < 0 {
  55 + // return nil, xerr.NewErrMsg("没有评论权限")
  56 + // }
  57 + // }
59 // 对段落进行评论 58 // 对段落进行评论
60 var selctionInfo *domain.ArticleSection 59 var selctionInfo *domain.ArticleSection
61 if req.SectionId > 0 { 60 if req.SectionId > 0 {
  1 +package comment
  2 +
  3 +import (
  4 + "context"
  5 +
  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"
  8 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
  9 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
  10 +
  11 + "github.com/zeromicro/go-zero/core/logx"
  12 +)
  13 +
  14 +type MiniTop5ArticleCommentLogic struct {
  15 + logx.Logger
  16 + ctx context.Context
  17 + svcCtx *svc.ServiceContext
  18 +}
  19 +
  20 +func NewMiniTop5ArticleCommentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MiniTop5ArticleCommentLogic {
  21 + return &MiniTop5ArticleCommentLogic{
  22 + Logger: logx.WithContext(ctx),
  23 + ctx: ctx,
  24 + svcCtx: svcCtx,
  25 + }
  26 +}
  27 +
  28 +// 获取前5的评论
  29 +func (l *MiniTop5ArticleCommentLogic) MiniTop5ArticleComment(req *types.MiniTop5ArticleCommentRequest) (resp *types.MiniTop5ArticleCommentResponse, err error) {
  30 + var conn = l.svcCtx.DefaultDBConn()
  31 + commentList, err := l.svcCtx.ArticleCommentRepository.Top5Comment(l.ctx, conn, req.CompanyId, req.ArticleId)
  32 + if err != nil {
  33 + return nil, xerr.NewErrMsgErr("获取评论信息失败", err)
  34 + }
  35 +
  36 + queryOption := domain.NewQueryOptions().WithFindOnly().
  37 + MustWithKV("articleId", req.ArticleId).
  38 + MustWithKV("userId", req.UserId)
  39 + // 获取我点赞的评论
  40 + _, userFlagList, err := l.svcCtx.UserLoveFlagRepository.Find(l.ctx, conn, queryOption)
  41 + if err != nil {
  42 + return nil, xerr.NewErrMsgErr("获取评论信息失败", err)
  43 + }
  44 + // 我点赞的
  45 + flagMap := map[int64]struct{}{}
  46 + for _, val := range userFlagList {
  47 + flagMap[val.CommentId] = struct{}{}
  48 + }
  49 +
  50 + resp = &types.MiniTop5ArticleCommentResponse{
  51 + List: make([]types.ArticleCommentItem, len(commentList)),
  52 + }
  53 + for i, val := range commentList {
  54 + item := types.ArticleCommentItem{
  55 + Id: val.Id,
  56 + Pid: val.Pid,
  57 + TopId: val.TopId,
  58 + ArtitcleId: val.ArticleId,
  59 + SectionId: val.SectionId,
  60 + FromUserId: val.FromUserId,
  61 + FromUser: types.CommentAuthor{
  62 + Id: val.FromUser.Id,
  63 + Name: val.FromUser.Name,
  64 + Avatar: val.FromUser.Avatar,
  65 + Position: val.FromUser.Position,
  66 + Company: val.FromUser.Company,
  67 + },
  68 + ToUserId: val.ToUserId,
  69 + ToUser: types.CommentAuthor{
  70 + Id: val.ToUser.Id,
  71 + Name: val.ToUser.Name,
  72 + Avatar: val.ToUser.Avatar,
  73 + Position: val.ToUser.Position,
  74 + Company: val.ToUser.Company,
  75 + },
  76 + SectionContent: val.SectionContent,
  77 + CountReply: val.CountReply,
  78 + CountUserLove: val.CountUserLove,
  79 + CountAdminLove: val.CountAdminLove,
  80 + AtWho: []types.CommentAtWho{},
  81 + CreatedAt: val.CreatedAt,
  82 + MeLoveFlag: 0,
  83 + }
  84 +
  85 + if _, ok := flagMap[val.Id]; ok {
  86 + item.MeLoveFlag = 1
  87 + }
  88 + for _, val2 := range val.AtWho {
  89 + item.AtWho = append(item.AtWho, types.CommentAtWho{
  90 + Id: val2.Id,
  91 + Name: val2.Name,
  92 + })
  93 + }
  94 + resp.List[i] = item
  95 + }
  96 + return
  97 +}
@@ -108,6 +108,16 @@ type MiniDeleteArticleCommentResponse struct { @@ -108,6 +108,16 @@ type MiniDeleteArticleCommentResponse struct {
108 Id int64 `json:"id"` 108 Id int64 `json:"id"`
109 } 109 }
110 110
  111 +type MiniTop5ArticleCommentRequest struct {
  112 + CompanyId int64 `json:",optional"`
  113 + UserId int64 `json:",optional"`
  114 + ArticleId int64 `json:"articleId"`
  115 +}
  116 +
  117 +type MiniTop5ArticleCommentResponse struct {
  118 + List []ArticleCommentItem `json:"list"`
  119 +}
  120 +
111 type MessageSystemRequest struct { 121 type MessageSystemRequest struct {
112 Page int `json:"page"` 122 Page int `json:"page"`
113 Size int `json:"size"` 123 Size int `json:"size"`
@@ -570,16 +580,16 @@ type MiniArticleGetRequest struct { @@ -570,16 +580,16 @@ type MiniArticleGetRequest struct {
570 } 580 }
571 581
572 type MiniArticleGetResponse struct { 582 type MiniArticleGetResponse struct {
573 - Id int64 `json:"id"` //id  
574 - Title string `json:"title"` //标题  
575 - AuthorId int64 `json:"authorId"` //发布人id  
576 - Author ArticleAuthor `json:"author"` //发布人  
577 - CreatedAt int64 `json:"createdAt"` //文章的发布时间  
578 - Section []ArticleSection `json:"section"` //文章的文本内容  
579 - Images []string `json:"images"` //图片  
580 - WhoRead []int64 `json:"whoRead"` //谁可查看  
581 - WhoReview []int64 `json:"whoReview"` //谁可评论  
582 - Location Location `json:"location"` //定位坐标 583 + Id int64 `json:"id"` // id
  584 + Title string `json:"title"` // 标题
  585 + AuthorId int64 `json:"authorId"` // 发布人id
  586 + Author ArticleAuthor `json:"author"` // 发布人
  587 + CreatedAt int64 `json:"createdAt"` // 文章的发布时间
  588 + Section []ArticleSection `json:"section"` // 文章的文本内容
  589 + Images []string `json:"images"` // 图片
  590 + WhoRead []int64 `json:"whoRead"` // 谁可查看
  591 + WhoReview []int64 `json:"whoReview"` // 谁可评论
  592 + Location Location `json:"location"` // 定位坐标
583 CountLove int `json:"countLove"` // 点赞数量 593 CountLove int `json:"countLove"` // 点赞数量
584 CountComment int `json:"countComment"` // 评论数量 594 CountComment int `json:"countComment"` // 评论数量
585 CountRead int `json:"countRead"` // 浏览数量 595 CountRead int `json:"countRead"` // 浏览数量
@@ -228,7 +228,10 @@ func (repository *ArticleCommentRepository) IncreaseCountUserLove(ctx context.Co @@ -228,7 +228,10 @@ func (repository *ArticleCommentRepository) IncreaseCountUserLove(ctx context.Co
228 ) 228 )
229 m = &models.ArticleComment{Id: commnetId} 229 m = &models.ArticleComment{Id: commnetId}
230 queryFunc := func() (interface{}, error) { 230 queryFunc := func() (interface{}, error) {
231 - tx = tx.Model(m).Update("count_user_love", gorm.Expr("count_user_love+?", incr)) 231 + tx = tx.Model(m).Updates(map[string]interface{}{
  232 + "count_user_love": gorm.Expr("count_user_love+?", incr),
  233 + "version": gorm.Expr("version+1"),
  234 + })
232 return nil, tx.Error 235 return nil, tx.Error
233 } 236 }
234 if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil { 237 if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
@@ -247,7 +250,10 @@ func (repository *ArticleCommentRepository) IncreaseCountReply(ctx context.Conte @@ -247,7 +250,10 @@ func (repository *ArticleCommentRepository) IncreaseCountReply(ctx context.Conte
247 ) 250 )
248 m = &models.ArticleComment{Id: commnetId} 251 m = &models.ArticleComment{Id: commnetId}
249 queryFunc := func() (interface{}, error) { 252 queryFunc := func() (interface{}, error) {
250 - tx = tx.Model(m).Update("count_reply", gorm.Expr("count_reply+?", incr)) 253 + tx = tx.Model(m).Updates(map[string]interface{}{
  254 + "count_reply": gorm.Expr("count_reply+?", incr),
  255 + "version": gorm.Expr("version+1"),
  256 + })
251 return nil, tx.Error 257 return nil, tx.Error
252 } 258 }
253 if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil { 259 if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
@@ -257,6 +263,60 @@ func (repository *ArticleCommentRepository) IncreaseCountReply(ctx context.Conte @@ -257,6 +263,60 @@ func (repository *ArticleCommentRepository) IncreaseCountReply(ctx context.Conte
257 263
258 } 264 }
259 265
  266 +// 获取热度前5的
  267 +// 规则 热门=(回复数+赞数量+运营点赞)Top5
  268 +func (repository *ArticleCommentRepository) Top5Comment(ctx context.Context, conn transaction.Conn, companyId int64, articleId int64) ([]*domain.ArticleComment, error) {
  269 +
  270 + sql1 := `select
  271 + article_comment.id ,
  272 + (article_comment.count_reply +article_comment.count_user_love +article_comment.count_admin_love ) cnt
  273 + from article_comment
  274 + where top_id =0 and article_id =? and deleted_at=0 and show=0 and company_id=?
  275 + order by cnt desc,article_comment.id desc
  276 + limit 5 `
  277 + db := conn.DB()
  278 + rows, err := db.Raw(sql1, articleId, companyId).Rows()
  279 + if err != nil {
  280 + return nil, err
  281 + }
  282 + defer rows.Close()
  283 + commentIds := []int64{}
  284 + var cid int64
  285 + var cnt int64
  286 + for rows.Next() {
  287 + err = rows.Scan(&cid, &cnt)
  288 + if err != nil {
  289 + return nil, err
  290 + }
  291 + commentIds = append(commentIds, cid)
  292 + }
  293 + if len(commentIds) == 0 {
  294 + return nil, nil
  295 + }
  296 + ms := make([]*models.ArticleComment, 0)
  297 + dms := make([]*domain.ArticleComment, 0)
  298 + queryFunc := func() (interface{}, error) {
  299 + tx := db.Model(&ms).Where("id in(?)", commentIds)
  300 + queryOptions := domain.NewQueryOptions().WithFindOnly()
  301 + if _, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil {
  302 + return dms, tx.Error
  303 + }
  304 + return dms, nil
  305 + }
  306 +
  307 + if _, err := repository.Query(queryFunc); err != nil {
  308 + return nil, err
  309 + }
  310 +
  311 + for _, item := range ms {
  312 + if dm, err := repository.ModelToDomainModel(item); err != nil {
  313 + return dms, err
  314 + } else {
  315 + dms = append(dms, dm)
  316 + }
  317 + }
  318 + return dms, nil
  319 +}
260 func NewArticleCommentRepository(cache *cache.CachedRepository) domain.ArticleCommentRepository { 320 func NewArticleCommentRepository(cache *cache.CachedRepository) domain.ArticleCommentRepository {
261 return &ArticleCommentRepository{CachedRepository: cache} 321 return &ArticleCommentRepository{CachedRepository: cache}
262 } 322 }
@@ -189,6 +189,7 @@ func (repository *ArticleSectionRepository) IncreaseCountComment(ctx context.Con @@ -189,6 +189,7 @@ func (repository *ArticleSectionRepository) IncreaseCountComment(ctx context.Con
189 queryFunc := func() (interface{}, error) { 189 queryFunc := func() (interface{}, error) {
190 tx = tx.Model(m).Updates(map[string]interface{}{ 190 tx = tx.Model(m).Updates(map[string]interface{}{
191 "total_comment": gorm.Expr("total_comment+?", incr), 191 "total_comment": gorm.Expr("total_comment+?", incr),
  192 + "version": gorm.Expr("version+1"),
192 }) 193 })
193 return nil, tx.Error 194 return nil, tx.Error
194 } 195 }
@@ -59,4 +59,5 @@ type ArticleCommentRepository interface { @@ -59,4 +59,5 @@ type ArticleCommentRepository interface {
59 Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*ArticleComment, error) 59 Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*ArticleComment, error)
60 IncreaseCountUserLove(ctx context.Context, conn transaction.Conn, incr int, commentId int64) error //点赞数量变动 60 IncreaseCountUserLove(ctx context.Context, conn transaction.Conn, incr int, commentId int64) error //点赞数量变动
61 IncreaseCountReply(ctx context.Context, conn transaction.Conn, incr int, commentId int64) error // 评论回复数量变动 61 IncreaseCountReply(ctx context.Context, conn transaction.Conn, incr int, commentId int64) error // 评论回复数量变动
  62 + Top5Comment(ctx context.Context, conn transaction.Conn, companyId int64, articleId int64) ([]*ArticleComment, error)
62 } 63 }