作者 tangxvhui

热门前5的评论

... ... @@ -28,6 +28,10 @@ service Core {
@handler MiniListArticleComment
post /article_comment/list (MiniListArticleCommentRequest) returns (MiniListArticleCommentResponse)
@doc "小程序展示文章的评论列表TOP5"
@handler MiniTop5ArticleComment
post /article_comment/top5 (MiniTop5ArticleCommentRequest) returns (MiniTop5ArticleCommentResponse)
@doc "小程序展示单个文章的评论"
@handler MiniGetArticleComment
get /article_comment/:id (MiniGetArticleCommentRequest) returns (MiniGetArticleCommentResponse)
... ... @@ -155,4 +159,16 @@ type (
MiniDeleteArticleCommentResponse {
Id int64 `json:"id"`
}
)
type (
MiniTop5ArticleCommentRequest {
CompanyId int64 `json:",optional"`
UserId int64 `json:",optional"`
ArticleId int64 `json:"articleId"`
}
MiniTop5ArticleCommentResponse {
List []ArticleCommentItem `json:"list"`
}
)
\ No newline at end of file
... ...
package comment
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/logic/comment"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/contextdata"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/result"
)
func MiniTop5ArticleCommentHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.MiniTop5ArticleCommentRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := comment.NewMiniTop5ArticleCommentLogic(r.Context(), svcCtx)
token := contextdata.GetUserTokenFromCtx(r.Context())
req.CompanyId = token.CompanyId
req.UserId = token.UserId
resp, err := l.MiniTop5ArticleComment(&req)
result.HttpResult(r, w, resp, err)
}
}
... ...
... ... @@ -36,6 +36,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Handler: comment.MiniListArticleCommentHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/article_comment/top5",
Handler: comment.MiniTop5ArticleCommentHandler(serverCtx),
},
{
Method: http.MethodGet,
Path: "/article_comment/:id",
Handler: comment.MiniGetArticleCommentHandler(serverCtx),
... ...
... ... @@ -2,6 +2,7 @@ package article
import (
"context"
"text/template"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
... ... @@ -29,6 +30,10 @@ func NewMiniCreateArticleDraftLogic(ctx context.Context, svcCtx *svc.ServiceCont
func (l *MiniCreateArticleDraftLogic) MiniCreateArticleDraft(req *types.MiniArticleDraftCreateRequest) (resp *types.MiniArticleDraftCreateResponse, err error) {
var conn = l.svcCtx.DefaultDBConn()
for i := range req.Section {
req.Section[i] = template.HTMLEscapeString(req.Section[i])
}
newDraft := domain.ArticleDraft{
Id: 0,
CompanyId: req.CompanyId,
... ...
... ... @@ -3,6 +3,7 @@ package article
import (
"context"
"strings"
"text/template"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
... ... @@ -124,6 +125,8 @@ func (l *MiniCreateArticleLogic) MiniCreateArticle(req *types.MiniArticleCreateR
if len(newStr) == 0 {
continue
}
newStr = template.HTMLEscapeString(newStr)
newSection := domain.ArticleSection{
Id: 0,
CompanyId: author.CompanyId,
... ...
... ... @@ -2,6 +2,7 @@ package article
import (
"context"
"text/template"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
... ... @@ -37,6 +38,10 @@ func (l *MiniUpdateArticleDraftLogic) MiniUpdateArticleDraft(req *types.MiniArti
return nil, xerr.NewErrMsg("更新草稿失败")
}
}
for i := range req.Section {
req.Section[i] = template.HTMLEscapeString(req.Section[i])
}
draftInfo.Content = req.Section
draftInfo.Title = req.Title
draftInfo.Location = domain.Location{
... ...
... ... @@ -11,7 +11,6 @@ import (
"text/template"
"github.com/samber/lo"
"github.com/zeromicro/go-zero/core/logx"
)
... ... @@ -50,12 +49,12 @@ func (l *MiniCreateArticleCommentLogic) MiniCreateArticleComment(req *types.Mini
return nil, xerr.NewErrMsg("没有评论权限")
}
//查看评论权限,
if len(articleInfo.WhoReview) > 0 {
ok := lo.IndexOf(articleInfo.WhoReview, req.FromUserId)
if ok < 0 {
return nil, xerr.NewErrMsg("没有评论权限")
}
}
// if len(articleInfo.WhoReview) > 0 {
// ok := lo.IndexOf(articleInfo.WhoReview, req.FromUserId)
// if ok < 0 {
// return nil, xerr.NewErrMsg("没有评论权限")
// }
// }
// 对段落进行评论
var selctionInfo *domain.ArticleSection
if req.SectionId > 0 {
... ...
package comment
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
"github.com/zeromicro/go-zero/core/logx"
)
type MiniTop5ArticleCommentLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewMiniTop5ArticleCommentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MiniTop5ArticleCommentLogic {
return &MiniTop5ArticleCommentLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 获取前5的评论
func (l *MiniTop5ArticleCommentLogic) MiniTop5ArticleComment(req *types.MiniTop5ArticleCommentRequest) (resp *types.MiniTop5ArticleCommentResponse, err error) {
var conn = l.svcCtx.DefaultDBConn()
commentList, err := l.svcCtx.ArticleCommentRepository.Top5Comment(l.ctx, conn, req.CompanyId, req.ArticleId)
if err != nil {
return nil, xerr.NewErrMsgErr("获取评论信息失败", err)
}
queryOption := domain.NewQueryOptions().WithFindOnly().
MustWithKV("articleId", req.ArticleId).
MustWithKV("userId", req.UserId)
// 获取我点赞的评论
_, userFlagList, err := l.svcCtx.UserLoveFlagRepository.Find(l.ctx, conn, queryOption)
if err != nil {
return nil, xerr.NewErrMsgErr("获取评论信息失败", err)
}
// 我点赞的
flagMap := map[int64]struct{}{}
for _, val := range userFlagList {
flagMap[val.CommentId] = struct{}{}
}
resp = &types.MiniTop5ArticleCommentResponse{
List: make([]types.ArticleCommentItem, len(commentList)),
}
for i, val := range commentList {
item := types.ArticleCommentItem{
Id: val.Id,
Pid: val.Pid,
TopId: val.TopId,
ArtitcleId: val.ArticleId,
SectionId: val.SectionId,
FromUserId: val.FromUserId,
FromUser: types.CommentAuthor{
Id: val.FromUser.Id,
Name: val.FromUser.Name,
Avatar: val.FromUser.Avatar,
Position: val.FromUser.Position,
Company: val.FromUser.Company,
},
ToUserId: val.ToUserId,
ToUser: types.CommentAuthor{
Id: val.ToUser.Id,
Name: val.ToUser.Name,
Avatar: val.ToUser.Avatar,
Position: val.ToUser.Position,
Company: val.ToUser.Company,
},
SectionContent: val.SectionContent,
CountReply: val.CountReply,
CountUserLove: val.CountUserLove,
CountAdminLove: val.CountAdminLove,
AtWho: []types.CommentAtWho{},
CreatedAt: val.CreatedAt,
MeLoveFlag: 0,
}
if _, ok := flagMap[val.Id]; ok {
item.MeLoveFlag = 1
}
for _, val2 := range val.AtWho {
item.AtWho = append(item.AtWho, types.CommentAtWho{
Id: val2.Id,
Name: val2.Name,
})
}
resp.List[i] = item
}
return
}
... ...
... ... @@ -108,6 +108,16 @@ type MiniDeleteArticleCommentResponse struct {
Id int64 `json:"id"`
}
type MiniTop5ArticleCommentRequest struct {
CompanyId int64 `json:",optional"`
UserId int64 `json:",optional"`
ArticleId int64 `json:"articleId"`
}
type MiniTop5ArticleCommentResponse struct {
List []ArticleCommentItem `json:"list"`
}
type MessageSystemRequest struct {
Page int `json:"page"`
Size int `json:"size"`
... ... @@ -570,16 +580,16 @@ type MiniArticleGetRequest struct {
}
type MiniArticleGetResponse struct {
Id int64 `json:"id"` //id
Title string `json:"title"` //标题
AuthorId int64 `json:"authorId"` //发布人id
Author ArticleAuthor `json:"author"` //发布人
CreatedAt int64 `json:"createdAt"` //文章的发布时间
Section []ArticleSection `json:"section"` //文章的文本内容
Images []string `json:"images"` //图片
WhoRead []int64 `json:"whoRead"` //谁可查看
WhoReview []int64 `json:"whoReview"` //谁可评论
Location Location `json:"location"` //定位坐标
Id int64 `json:"id"` // id
Title string `json:"title"` // 标题
AuthorId int64 `json:"authorId"` // 发布人id
Author ArticleAuthor `json:"author"` // 发布人
CreatedAt int64 `json:"createdAt"` // 文章的发布时间
Section []ArticleSection `json:"section"` // 文章的文本内容
Images []string `json:"images"` // 图片
WhoRead []int64 `json:"whoRead"` // 谁可查看
WhoReview []int64 `json:"whoReview"` // 谁可评论
Location Location `json:"location"` // 定位坐标
CountLove int `json:"countLove"` // 点赞数量
CountComment int `json:"countComment"` // 评论数量
CountRead int `json:"countRead"` // 浏览数量
... ...
... ... @@ -228,7 +228,10 @@ func (repository *ArticleCommentRepository) IncreaseCountUserLove(ctx context.Co
)
m = &models.ArticleComment{Id: commnetId}
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Update("count_user_love", gorm.Expr("count_user_love+?", incr))
tx = tx.Model(m).Updates(map[string]interface{}{
"count_user_love": gorm.Expr("count_user_love+?", incr),
"version": gorm.Expr("version+1"),
})
return nil, tx.Error
}
if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
... ... @@ -247,7 +250,10 @@ func (repository *ArticleCommentRepository) IncreaseCountReply(ctx context.Conte
)
m = &models.ArticleComment{Id: commnetId}
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Update("count_reply", gorm.Expr("count_reply+?", incr))
tx = tx.Model(m).Updates(map[string]interface{}{
"count_reply": gorm.Expr("count_reply+?", incr),
"version": gorm.Expr("version+1"),
})
return nil, tx.Error
}
if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
... ... @@ -257,6 +263,60 @@ func (repository *ArticleCommentRepository) IncreaseCountReply(ctx context.Conte
}
// 获取热度前5的
// 规则 热门=(回复数+赞数量+运营点赞)Top5
func (repository *ArticleCommentRepository) Top5Comment(ctx context.Context, conn transaction.Conn, companyId int64, articleId int64) ([]*domain.ArticleComment, error) {
sql1 := `select
article_comment.id ,
(article_comment.count_reply +article_comment.count_user_love +article_comment.count_admin_love ) cnt
from article_comment
where top_id =0 and article_id =? and deleted_at=0 and show=0 and company_id=?
order by cnt desc,article_comment.id desc
limit 5 `
db := conn.DB()
rows, err := db.Raw(sql1, articleId, companyId).Rows()
if err != nil {
return nil, err
}
defer rows.Close()
commentIds := []int64{}
var cid int64
var cnt int64
for rows.Next() {
err = rows.Scan(&cid, &cnt)
if err != nil {
return nil, err
}
commentIds = append(commentIds, cid)
}
if len(commentIds) == 0 {
return nil, nil
}
ms := make([]*models.ArticleComment, 0)
dms := make([]*domain.ArticleComment, 0)
queryFunc := func() (interface{}, error) {
tx := db.Model(&ms).Where("id in(?)", commentIds)
queryOptions := domain.NewQueryOptions().WithFindOnly()
if _, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil {
return dms, tx.Error
}
return dms, nil
}
if _, err := repository.Query(queryFunc); err != nil {
return nil, err
}
for _, item := range ms {
if dm, err := repository.ModelToDomainModel(item); err != nil {
return dms, err
} else {
dms = append(dms, dm)
}
}
return dms, nil
}
func NewArticleCommentRepository(cache *cache.CachedRepository) domain.ArticleCommentRepository {
return &ArticleCommentRepository{CachedRepository: cache}
}
... ...
... ... @@ -189,6 +189,7 @@ func (repository *ArticleSectionRepository) IncreaseCountComment(ctx context.Con
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Updates(map[string]interface{}{
"total_comment": gorm.Expr("total_comment+?", incr),
"version": gorm.Expr("version+1"),
})
return nil, tx.Error
}
... ...
... ... @@ -59,4 +59,5 @@ type ArticleCommentRepository interface {
Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*ArticleComment, error)
IncreaseCountUserLove(ctx context.Context, conn transaction.Conn, incr int, commentId int64) error //点赞数量变动
IncreaseCountReply(ctx context.Context, conn transaction.Conn, incr int, commentId int64) error // 评论回复数量变动
Top5Comment(ctx context.Context, conn transaction.Conn, companyId int64, articleId int64) ([]*ArticleComment, error)
}
... ...