作者 yangfu

权限认证

正在显示 36 个修改的文件 包含 1113 行增加33 行删除
... ... @@ -63,6 +63,15 @@ service Core {
@doc "小程序获取文章的编辑记录"
@handler MiniArticleBackupSearch
post /article_backup/search (MiniArticleBackupSearchRequest) returns (MiniArticleBackupSearchResponse)
@doc "小程序设置文章的定性标签"
@handler MiniArticleSetTag
post /article/set_tag (MiniArticleSetTagRequest) returns (MiniArticleSetTagResponse)
@doc "小程序所有的定性标签"
@handler MiniAllArticleTag
get /article_tag/list/all (MiniAllArticleTagRequest) returns (MiniAllArticleTagResponse)
}
// 管理后台接口
... ...
... ... @@ -278,6 +278,39 @@ type (
}
)
//小程序端设置文章的定性标签
type (
MiniArticleSetTagRequest{
CompanyId int64 `json:",optional"` // 公司id
UserId int64 `json:",optional"` // 公司id
ArticleId int64 `json:"articleId"` // 文章id
TagId int64 `json:"tagId"` // 标签id
}
MiniArticleSetTagResponse{
Id int64 `json:"id"`
}
)
//小程序端获取所有的定性标签
type (
MiniAllArticleTagRequest{
CompanyId int64 `json:",optional"` // 公司id
UserId int64 `json:",optional"` // 公司id
ArticleId int64 `json:"articleId"` // 文章id
TagId int64 `json:"tagId"` // 标签id
}
MiniAllArticleTagResponse{
TagGroup []string `json:"tagGroup"`
Tags []ArticleTagItem `json:"tags"`
}
ArticleTagItem {
Id int64 `json:"id"`
Group string `json:"group"`
Name string `json:"name"`
}
)
... ...
... ... @@ -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)
... ... @@ -35,6 +39,10 @@ service Core {
@doc "小程序展示删除文章评论"
@handler MiniDeleteArticleComment
delete /article_comment/:id (MiniDeleteArticleCommentRequest) returns (MiniDeleteArticleCommentResponse)
@doc "小程序展示评论时@人可选列表"
@handler MiniArticleCommentAtWho
post /article_comment/at_who/list (MiniArticleCommentAtWhoRequest) returns (MiniArticleCommentAtWhoResponse)
}
//
... ... @@ -95,10 +103,12 @@ type (
// 小程序获取文章的评论列表
type (
MiniListArticleCommentRequest {
Page int64 `json:"page"`
Size int64 `json:"size"`
Page int `json:"page"`
Size int `json:"size"`
CompanyId int64 `json:",optional"`
SectionId int64 `json:"sectionId"`
UserId int64 `json:",optional"`
ArticleId int64 `json:"articleId"`
SectionId int64 `json:"sectionId,optional"`
}
MiniListArticleCommentResponse {
Total int64 `json:"total"`
... ... @@ -154,3 +164,29 @@ type (
Id int64 `json:"id"`
}
)
// 热门前5的评论列表
type (
MiniTop5ArticleCommentRequest {
CompanyId int64 `json:",optional"`
UserId int64 `json:",optional"`
ArticleId int64 `json:"articleId"`
}
MiniTop5ArticleCommentResponse {
List []ArticleCommentItem `json:"list"`
}
)
// 填写评论时选择@人
type (
MiniArticleCommentAtWhoRequest {
CompanyId int64 `json:",optional"`
UserId int64 `json:",optional"`
ArticleId int64 `json:"articleId"`
}
MiniArticleCommentAtWhoResponse {
List []CommentAtWho `json:"list"`
}
)
\ No newline at end of file
... ...
... ... @@ -30,6 +30,10 @@ service Core {
@doc "部门-更新"
@handler systemUpdate
put /system/department/:id (DepartmentUpdateRequest) returns (DepartmentGetResponse)
@doc "部门-删除"
@handler systemDelete
delete /system/department/:id (DepartmentGetRequest) returns (DepartmentGetResponse)
}
type (
... ...
package article
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/logic/article"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
)
func MiniAllArticleTagHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.MiniAllArticleTagRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := article.NewMiniAllArticleTagLogic(r.Context(), svcCtx)
resp, err := l.MiniAllArticleTag(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
... ...
package article
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/logic/article"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
)
func MiniArticleSetTagHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.MiniArticleSetTagRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := article.NewMiniArticleSetTagLogic(r.Context(), svcCtx)
resp, err := l.MiniArticleSetTag(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
... ...
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"
)
func MiniArticleCommentAtWhoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.MiniArticleCommentAtWhoRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := comment.NewMiniArticleCommentAtWhoLogic(r.Context(), svcCtx)
resp, err := l.MiniArticleCommentAtWho(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
... ...
... ... @@ -22,6 +22,7 @@ func MiniGetArticleCommentHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
l := comment.NewMiniGetArticleCommentLogic(r.Context(), svcCtx)
token := contextdata.GetUserTokenFromCtx(r.Context())
req.CompanyId = token.CompanyId
req.UserId = token.UserId
resp, err := l.MiniGetArticleComment(&req)
result.HttpResult(r, w, resp, err)
}
... ...
... ... @@ -21,6 +21,7 @@ func MiniListArticleCommentHandler(svcCtx *svc.ServiceContext) http.HandlerFunc
l := comment.NewMiniListArticleCommentLogic(r.Context(), svcCtx)
token := contextdata.GetUserTokenFromCtx(r.Context())
req.CompanyId = token.CompanyId
req.UserId = token.UserId
resp, err := l.MiniListArticleComment(&req)
result.HttpResult(r, w, resp, err)
}
... ...
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)
}
}
... ...
package department
import (
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/result"
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/logic/department"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
)
func SystemDeleteHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.DepartmentGetRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := department.NewSystemDeleteLogic(r.Context(), svcCtx)
resp, err := l.SystemDelete(&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),
... ... @@ -45,6 +50,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/article_comment/:id",
Handler: comment.MiniDeleteArticleCommentHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/article_comment/at_who/list",
Handler: comment.MiniArticleCommentAtWhoHandler(serverCtx),
},
},
rest.WithJwt(serverCtx.Config.MiniAuth.AccessSecret),
rest.WithPrefix("/v1/mini"),
... ... @@ -334,6 +344,16 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/article_backup/search",
Handler: article.MiniArticleBackupSearchHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/article/set_tag",
Handler: article.MiniArticleSetTagHandler(serverCtx),
},
{
Method: http.MethodGet,
Path: "/article_tag/list/all",
Handler: article.MiniAllArticleTagHandler(serverCtx),
},
},
rest.WithJwt(serverCtx.Config.MiniAuth.AccessSecret),
rest.WithPrefix("/v1/mini"),
... ... @@ -415,6 +435,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/system/department/:id",
Handler: department.SystemUpdateHandler(serverCtx),
},
{
Method: http.MethodDelete,
Path: "/system/department/:id",
Handler: department.SystemDeleteHandler(serverCtx),
},
}...,
),
rest.WithJwt(serverCtx.Config.SystemAuth.AccessSecret),
... ...
package article
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"
"github.com/zeromicro/go-zero/core/logx"
)
type MiniAllArticleTagLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewMiniAllArticleTagLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MiniAllArticleTagLogic {
return &MiniAllArticleTagLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *MiniAllArticleTagLogic) MiniAllArticleTag(req *types.MiniAllArticleTagRequest) (resp *types.MiniAllArticleTagResponse, err error) {
// todo: add your logic here and delete this line
return
}
... ...
package article
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"
"github.com/zeromicro/go-zero/core/logx"
)
type MiniArticleSetTagLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewMiniArticleSetTagLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MiniArticleSetTagLogic {
return &MiniArticleSetTagLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *MiniArticleSetTagLogic) MiniArticleSetTag(req *types.MiniArticleSetTagRequest) (resp *types.MiniArticleSetTagResponse, err error) {
// todo: add your logic here and delete this line
return
}
... ...
... ... @@ -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{
... ...
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"
"github.com/zeromicro/go-zero/core/logx"
)
type MiniArticleCommentAtWhoLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewMiniArticleCommentAtWhoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MiniArticleCommentAtWhoLogic {
return &MiniArticleCommentAtWhoLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *MiniArticleCommentAtWhoLogic) MiniArticleCommentAtWho(req *types.MiniArticleCommentAtWhoRequest) (resp *types.MiniArticleCommentAtWhoResponse, err error) {
// todo: add your logic here and delete this line
return
}
... ...
... ... @@ -2,6 +2,7 @@ package comment
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/logic/message"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
... ... @@ -49,7 +50,6 @@ func (l *MiniCreateArticleCommentLogic) MiniCreateArticleComment(req *types.Mini
return nil, xerr.NewErrMsg("没有评论权限")
}
//查看评论权限,
//TODO 临时注释
// if len(articleInfo.WhoReview) > 0 {
// ok := lo.IndexOf(articleInfo.WhoReview, req.FromUserId)
// if ok < 0 {
... ... @@ -83,12 +83,12 @@ func (l *MiniCreateArticleCommentLogic) MiniCreateArticleComment(req *types.Mini
}
}
var atWhoList []*domain.User
var atWhoIds = make([]int64, 0)
if len(req.AtWho) > 0 {
uids := []int64{}
for _, val := range req.AtWho {
uids = append(uids, val.Id)
atWhoIds = append(atWhoIds, val.Id)
}
queryOption := domain.NewQueryOptions().WithFindOnly().WithKV("ids", uids)
queryOption := domain.NewQueryOptions().WithFindOnly().WithKV("ids", atWhoIds)
_, atWhoList, err = l.svcCtx.UserRepository.Find(l.ctx, conn, queryOption)
if err != nil {
return nil, xerr.NewErrMsgErr("检查@的人员失败", err)
... ... @@ -152,11 +152,23 @@ func (l *MiniCreateArticleCommentLogic) MiniCreateArticleComment(req *types.Mini
}
//保存数据
err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, c transaction.Conn) error {
//保存评论
_, err = l.svcCtx.ArticleCommentRepository.Insert(ctx, c, &newComment)
if err != nil {
return err
}
// 增加文章评论计数
err = l.svcCtx.ArticleRepository.IncreaseCountComment(ctx, c, 1, articleInfo.Id)
if err != nil {
return err
}
//增加段落评论计数
if newComment.SectionId > 0 {
err = l.svcCtx.ArticleSectionRepository.IncreaseCountComment(ctx, c, 1, newComment.SectionId)
if err != nil {
return err
}
}
//增加评论回复计数
if pComment != nil {
err = l.svcCtx.ArticleCommentRepository.IncreaseCountReply(l.ctx, c, 1, pComment.Id)
... ... @@ -171,11 +183,21 @@ func (l *MiniCreateArticleCommentLogic) MiniCreateArticleComment(req *types.Mini
return err
}
}
//保存评论
_, err = l.svcCtx.ArticleCommentRepository.Insert(ctx, c, &newComment)
// 创建回复消息
var atAllIds = make([]int64, 0)
atAllIds = append(atAllIds, newComment.ToUserId)
atAllIds = append(atAllIds, atWhoIds...)
var messageLogic = message.NewMiniBusinessLogic(l.ctx, l.svcCtx)
if pComment != nil {
err = messageLogic.CommentReply(c, pComment.ArticleId, pComment.SectionId, pComment.Id, req.Content, atAllIds) // 对评论回复
} else {
err = messageLogic.CommentArticle(c, req.ArtitcleId, req.SectionId, req.Content, atAllIds) // 对文章回复
}
if err != nil {
return err
}
return nil
}, true)
... ...
... ... @@ -60,6 +60,18 @@ func (l *MiniDeleteArticleCommentLogic) MiniDeleteArticleComment(req *types.Mini
return err
}
}
//减少加段落评论计数
if commetInfo.SectionId > 0 {
err = l.svcCtx.ArticleSectionRepository.IncreaseCountComment(ctx, c, -1, commetInfo.SectionId)
if err != nil {
return err
}
}
// 减少文章的评论数
err = l.svcCtx.ArticleRepository.IncreaseCountComment(ctx, c, -1, commetInfo.ArticleId)
if err != nil {
return err
}
return nil
}, true)
... ...
... ... @@ -5,6 +5,8 @@ import (
"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"
)
... ... @@ -24,7 +26,166 @@ func NewMiniListArticleCommentLogic(ctx context.Context, svcCtx *svc.ServiceCont
}
func (l *MiniListArticleCommentLogic) MiniListArticleComment(req *types.MiniListArticleCommentRequest) (resp *types.MiniListArticleCommentResponse, err error) {
// todo: add your logic here and delete this line
// 先获取最顶层的评论
var conn = l.svcCtx.DefaultDBConn()
if req.Page > 40 {
req.Page = 40
}
queryOption := domain.NewQueryOptions().
WithOffsetLimit(req.Page, req.Size).
MustWithKV("topId", 0).
MustWithKV("articleId", req.ArticleId).
MustWithKV("sectionId", req.SectionId).
MustWithKV("show", domain.CommentShowEnable).
MustWithKV("companyId", req.CompanyId)
cnt, commentList, err := l.svcCtx.ArticleCommentRepository.Find(l.ctx, conn, queryOption)
if err != nil {
return nil, xerr.NewErrMsgErr("获取评论信息失败", err)
}
if cnt == 0 {
resp = &types.MiniListArticleCommentResponse{
Total: 0,
List: make([]types.ArticleCommentAndReply, 0),
}
return
}
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.MiniListArticleCommentResponse{
Total: cnt,
List: make([]types.ArticleCommentAndReply, len(commentList)),
}
// 获取回复的评论
for i, val := range commentList {
item := types.ArticleCommentAndReply{
Comment: 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,
},
Reply: []types.ArticleCommentItem{},
TotalReply: int64(val.CountReply),
}
if _, ok := flagMap[val.Id]; ok {
item.Comment.MeLoveFlag = 1
}
for _, val2 := range val.AtWho {
item.Comment.AtWho = append(item.Comment.AtWho, types.CommentAtWho{
Id: val2.Id,
Name: val2.Name,
})
}
//获取回复的评论
cntReply, reply := l.listCommentReply(item.Comment.Id, flagMap)
resp.List[i] = item
resp.List[i].Reply = reply
resp.List[i].TotalReply = cntReply
}
return resp, nil
}
// listCommentReply
func (l *MiniListArticleCommentLogic) listCommentReply(commentId int64, loveFlagMap map[int64]struct{}) (cnt int64, replyList []types.ArticleCommentItem) {
var conn = l.svcCtx.DefaultDBConn()
queryOption := domain.NewQueryOptions().
WithOffsetLimit(1, 2).
MustWithKV("topId", commentId).
MustWithKV("show", domain.CommentShowEnable)
cnt, commentList, err := l.svcCtx.ArticleCommentRepository.Find(l.ctx, conn, queryOption)
if err != nil {
return cnt, []types.ArticleCommentItem{}
}
if cnt == 0 {
replyList = []types.ArticleCommentItem{}
return cnt, replyList
}
for _, 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 := loveFlagMap[val.Id]; ok {
item.MeLoveFlag = 1
}
for _, val2 := range val.AtWho {
item.AtWho = append(item.AtWho, types.CommentAtWho{
Id: val2.Id,
Name: val2.Name,
})
}
replyList = append(replyList, item)
}
return cnt, replyList
}
... ...
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
}
... ...
... ... @@ -48,7 +48,7 @@ func (l *SystemAddLogic) SystemAdd(req *types.DepartmentAddRequest) (resp *types
CompanyId: userToken.CompanyId,
Name: req.Name,
}
err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, c transaction.Conn) error {
err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, conn transaction.Conn) error {
_, err = l.svcCtx.DepartmentRepository.Insert(l.ctx, conn, insert)
if err != nil {
return err
... ...
package department
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/db/transaction"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"github.com/zeromicro/go-zero/core/logx"
)
type SystemDeleteLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewSystemDeleteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SystemDeleteLogic {
return &SystemDeleteLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *SystemDeleteLogic) SystemDelete(req *types.DepartmentGetRequest) (resp *types.DepartmentGetResponse, err error) {
var conn = l.svcCtx.DefaultDBConn()
one, err := l.svcCtx.DepartmentRepository.FindOne(l.ctx, conn, req.Id)
if err != nil {
return nil, err
}
// 获取公司下的所有用户
_, users, err := l.svcCtx.UserRepository.Find(l.ctx, conn, domain.NewQueryOptions().
WithFindOnly().
WithKV(" companyId", one.CompanyId))
if err != nil {
return nil, err
}
err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, conn transaction.Conn) error {
_, err = l.svcCtx.DepartmentRepository.Delete(l.ctx, conn, one)
if err != nil {
return err
}
// 移除用户中关联的分组
if len(users) > 0 {
var findIndex = func(ids []int64, id int64) int {
for i, _ := range ids {
if ids[i] == id {
return i
}
}
return -1
}
for i := range users {
user := users[i]
var targetIndex = findIndex(user.Departments, req.Id)
if targetIndex != -1 { // 归属分组存在,则移除
user.Departments = append(user.Departments[:targetIndex], user.Departments[targetIndex+1:]...) // 移除分组ID
_, err = l.svcCtx.UserRepository.UpdateWithVersion(l.ctx, conn, user)
if err != nil {
return err
}
}
}
}
return nil
}, true)
return
}
... ...
... ... @@ -55,18 +55,18 @@ func (l *SystemUpdateLogic) SystemUpdate(req *types.DepartmentUpdateRequest) (re
newIdMap[req.Ids[i]] = 0
}
// 更新
err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, c transaction.Conn) error {
_, err = l.svcCtx.DepartmentRepository.UpdateWithVersion(l.ctx, conn, one)
if err != nil {
return err
}
// 获取公司下的所有用户
_, users, err := l.svcCtx.UserRepository.Find(l.ctx, conn, domain.NewQueryOptions().
WithFindOnly().
WithKV(" companyId", one.CompanyId))
if err != nil {
return nil, err
}
// 更新
err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, conn transaction.Conn) error {
_, err = l.svcCtx.DepartmentRepository.UpdateWithVersion(l.ctx, conn, one)
if err != nil {
return err
}
... ...
... ... @@ -207,23 +207,23 @@ func (l *MiniBusinessLogic) MiniBusiness(req *types.MessageBusinessRequest) (res
}
// CommentArticle 评论文章
func (l *MiniBusinessLogic) CommentArticle(conn transaction.Conn, articleId int64, content string, at []int64) (err error) {
return l.createMessage(conn, domain.MsgTypeReply, domain.OptTypeArticle, articleId, 0, content, at)
func (l *MiniBusinessLogic) CommentArticle(conn transaction.Conn, articleId int64, sectionId int64, content string, at []int64) (err error) {
return l.createMessage(conn, domain.MsgTypeReply, domain.OptTypeArticle, articleId, sectionId, 0, content, at)
}
// CommentReply 评论回复
func (l *MiniBusinessLogic) CommentReply(conn transaction.Conn, articleId int64, commentId int64, content string, at []int64) (err error) {
return l.createMessage(conn, domain.MsgTypeReply, domain.OptTypeComment, articleId, commentId, content, at)
func (l *MiniBusinessLogic) CommentReply(conn transaction.Conn, articleId int64, sectionId int64, commentId int64, content string, at []int64) (err error) {
return l.createMessage(conn, domain.MsgTypeReply, domain.OptTypeComment, articleId, sectionId, commentId, content, at)
}
// LikeArticle 点赞文章
func (l *MiniBusinessLogic) LikeArticle(conn transaction.Conn, articleId int64, at int64) (err error) {
return l.createMessage(conn, domain.MsgTypeLike, domain.OptTypeArticle, articleId, 0, "", []int64{at})
return l.createMessage(conn, domain.MsgTypeLike, domain.OptTypeArticle, articleId, 0, 0, "", []int64{at})
}
// LikeComment 点赞评论
func (l *MiniBusinessLogic) LikeComment(conn transaction.Conn, articleId int64, commentId int64, at int64) (err error) {
return l.createMessage(conn, domain.MsgTypeLike, domain.OptTypeComment, articleId, commentId, "", []int64{at})
func (l *MiniBusinessLogic) LikeComment(conn transaction.Conn, articleId int64, sectionId int64, commentId int64, at int64) (err error) {
return l.createMessage(conn, domain.MsgTypeLike, domain.OptTypeComment, articleId, sectionId, commentId, "", []int64{at})
}
func (l *MiniBusinessLogic) createMessage(
... ... @@ -231,6 +231,7 @@ func (l *MiniBusinessLogic) createMessage(
msgType domain.MsgBusinessType,
optType domain.MsgBusinessOpt,
articleId int64,
sectionId int64,
commentId int64,
content string,
at []int64) (err error) {
... ... @@ -245,6 +246,7 @@ func (l *MiniBusinessLogic) createMessage(
UserId: userToken.UserId,
RecipientId: at[i],
ArticleId: articleId,
SectionId: sectionId,
CommentId: commentId,
Content: content,
}
... ...
... ... @@ -50,10 +50,12 @@ type MiniCreateArticleCommentResponse struct {
}
type MiniListArticleCommentRequest struct {
Page int64 `json:"page"`
Size int64 `json:"size"`
Page int `json:"page"`
Size int `json:"size"`
CompanyId int64 `json:",optional"`
SectionId int64 `json:"sectionId"`
UserId int64 `json:",optional"`
ArticleId int64 `json:"articleId"`
SectionId int64 `json:"sectionId,optional"`
}
type MiniListArticleCommentResponse struct {
... ... @@ -106,6 +108,26 @@ 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 MiniArticleCommentAtWhoRequest struct {
CompanyId int64 `json:",optional"`
UserId int64 `json:",optional"`
ArticleId int64 `json:"articleId"`
}
type MiniArticleCommentAtWhoResponse struct {
List []CommentAtWho `json:"list"`
}
type MessageSystemRequest struct {
Page int `json:"page"`
Size int `json:"size"`
... ... @@ -782,6 +804,35 @@ type MiniArticleMarkItem struct {
UpdatedAt int64 `json:"updatedAt"`
}
type MiniArticleSetTagRequest struct {
CompanyId int64 `json:",optional"` // 公司id
UserId int64 `json:",optional"` // 公司id
ArticleId int64 `json:"articleId"` // 文章id
TagId int64 `json:"tagId"` // 标签id
}
type MiniArticleSetTagResponse struct {
Id int64 `json:"id"`
}
type MiniAllArticleTagRequest struct {
CompanyId int64 `json:",optional"` // 公司id
UserId int64 `json:",optional"` // 公司id
ArticleId int64 `json:"articleId"` // 文章id
TagId int64 `json:"tagId"` // 标签id
}
type MiniAllArticleTagResponse struct {
TagGroup []string `json:"tagGroup"`
Tags []ArticleTagItem `json:"tags"`
}
type ArticleTagItem struct {
Id int64 `json:"id"`
Group string `json:"group"`
Name string `json:"name"`
}
type SystemArticleGetRequest struct {
Id int64 `path:"id"` //id
CompanyId int64 `path:",optional"`
... ...
package models
import (
"fmt"
"time"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gorm.io/gorm"
)
type ArticleAndTag struct {
Id int64 // 唯一标识
CompanyId int64 `json:"companyId"`
CreatedAt int64 `json:"createdAt,omitempty"`
UpdatedAt int64 `json:"updatedAt,omitempty"`
ArticleId int64 `json:"articleId"`
TagId int64 `json:"tagId"`
}
func (m *ArticleAndTag) TableName() string {
return "article_and_tag"
}
func (m *ArticleAndTag) BeforeCreate(tx *gorm.DB) (err error) {
m.CreatedAt = time.Now().Unix()
m.UpdatedAt = time.Now().Unix()
return
}
func (m *ArticleAndTag) BeforeUpdate(tx *gorm.DB) (err error) {
// m.UpdatedAt = time.Now().Unix()
return
}
func (m *ArticleAndTag) CacheKeyFunc() string {
if m.Id == 0 {
return ""
}
return fmt.Sprintf("%v:cache:%v:id:%v", domain.ProjectName, m.TableName(), m.Id)
}
func (m *ArticleAndTag) CacheKeyFuncByObject(obj interface{}) string {
if v, ok := obj.(*ArticleAndTag); ok {
return v.CacheKeyFunc()
}
return ""
}
func (m *ArticleAndTag) CachePrimaryKeyFunc() string {
if len("") == 0 {
return ""
}
return fmt.Sprintf("%v:cache:%v:primarykey:%v", domain.ProjectName, m.TableName(), "key")
}
... ...
... ... @@ -17,6 +17,7 @@ type MessageBusiness struct {
UserId int64 `json:"userId"` // 操作人用户ID
RecipientId int64 `json:"recipientId"` // 接收人用户ID
ArticleId int64 `json:"articleId,omitempty"` // 文章ID
SectionId int64 `json:"sectionId,omitempty"` // 段落ID
CommentId int64 `json:"commentId,omitempty"` // 评论ID
Content string `json:"content,omitempty"` // 消息内容
CreatedAt int64 `json:",omitempty"`
... ...
package repository
import (
"context"
"github.com/jinzhu/copier"
"github.com/pkg/errors"
"github.com/tiptok/gocomm/pkg/cache"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/models"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gorm.io/gorm"
)
type ArticleAndTagRepository struct {
*cache.CachedRepository
}
func (repository *ArticleAndTagRepository) Insert(ctx context.Context, conn transaction.Conn, dm *domain.ArticleAndTag) (*domain.ArticleAndTag, error) {
var (
err error
m = &models.ArticleAndTag{}
tx = conn.DB()
)
if m, err = repository.DomainModelToModel(dm); err != nil {
return nil, err
}
if tx = tx.Model(m).Save(m); tx.Error != nil {
return nil, tx.Error
}
dm.Id = m.Id
return repository.ModelToDomainModel(m)
}
func (repository *ArticleAndTagRepository) Update(ctx context.Context, conn transaction.Conn, dm *domain.ArticleAndTag) (*domain.ArticleAndTag, error) {
var (
err error
m *models.ArticleAndTag
tx = conn.DB()
)
if m, err = repository.DomainModelToModel(dm); err != nil {
return nil, err
}
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Updates(m)
return nil, tx.Error
}
if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *ArticleAndTagRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.ArticleAndTag) (*domain.ArticleAndTag, error) {
var (
tx = conn.DB()
m = &models.ArticleAndTag{Id: dm.Id}
)
queryFunc := func() (interface{}, error) {
tx = tx.Where("id = ?", m.Id).Delete(m)
return m, tx.Error
}
if _, err := repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
return dm, err
}
return repository.ModelToDomainModel(m)
}
func (repository *ArticleAndTagRepository) FindOne(ctx context.Context, conn transaction.Conn, id int64) (*domain.ArticleAndTag, error) {
var (
err error
tx = conn.DB()
m = new(models.ArticleAndTag)
)
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Where("id = ?", id).First(m)
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
return nil, domain.ErrNotFound
}
return m, tx.Error
}
cacheModel := new(models.ArticleAndTag)
cacheModel.Id = id
if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *ArticleAndTagRepository) Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*domain.ArticleAndTag, error) {
var (
tx = conn.DB()
ms []*models.ArticleAndTag
dms = make([]*domain.ArticleAndTag, 0)
total int64
)
queryFunc := func() (interface{}, error) {
tx = tx.Model(&ms).Order("id desc")
if total, 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 0, nil, err
}
for _, item := range ms {
if dm, err := repository.ModelToDomainModel(item); err != nil {
return 0, dms, err
} else {
dms = append(dms, dm)
}
}
return total, dms, nil
}
func (repository *ArticleAndTagRepository) ModelToDomainModel(from *models.ArticleAndTag) (*domain.ArticleAndTag, error) {
to := &domain.ArticleAndTag{}
err := copier.Copy(to, from)
return to, err
}
func (repository *ArticleAndTagRepository) DomainModelToModel(from *domain.ArticleAndTag) (*models.ArticleAndTag, error) {
to := &models.ArticleAndTag{}
err := copier.Copy(to, from)
return to, err
}
func NewArticleAndTagRepository(cache *cache.CachedRepository) domain.ArticleAndTagRepository {
return &ArticleAndTagRepository{CachedRepository: cache}
}
... ...
... ... @@ -125,11 +125,23 @@ func (repository *ArticleCommentRepository) Find(ctx context.Context, conn trans
queryFunc := func() (interface{}, error) {
tx = tx.Model(&ms).Order("id desc")
if v, ok := queryOptions["topId"]; ok {
tx = tx.Where("top_id", v)
tx = tx.Where("top_id=?", v)
}
if v, ok := queryOptions["show"]; ok {
tx = tx.Where("show", v)
tx = tx.Where("show=?", v)
}
if v, ok := queryOptions["articleId"]; ok {
tx = tx.Where("article_id=?", v)
}
if v, ok := queryOptions["sectionId"]; ok {
tx = tx.Where("section_id=?", v)
}
if v, ok := queryOptions["companyId"]; ok {
tx = tx.Where("company_id=?", v)
}
if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil {
return dms, tx.Error
}
... ... @@ -216,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 {
... ... @@ -235,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 {
... ... @@ -245,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}
}
... ...
... ... @@ -178,6 +178,27 @@ func (repository *ArticleSectionRepository) DomainModelToModel(from *domain.Arti
return to, nil
}
// 评论数量变动
func (repository *ArticleSectionRepository) IncreaseCountComment(ctx context.Context, conn transaction.Conn, incr int, sectionId int64) error {
var (
err error
m *models.ArticleSection
tx = conn.DB()
)
m = &models.ArticleSection{Id: sectionId}
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
}
if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
return err
}
return nil
}
func NewArticleSectionRepository(cache *cache.CachedRepository) domain.ArticleSectionRepository {
return &ArticleSectionRepository{CachedRepository: cache}
}
... ...
package domain
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
)
// 保存文章和标签的关系,主要用于分组统计用
type ArticleAndTag struct {
Id int64 `json:"id"`
CompanyId int64 `json:"companyId"`
CreatedAt int64 `json:"createdAt"`
UpdatedAt int64 `json:"updatedAt"`
ArticleId int64 `json:"articleId"`
TagId int64 `json:"tagId"`
}
type ArticleAndTagRepository interface {
Insert(ctx context.Context, conn transaction.Conn, dm *ArticleAndTag) (*ArticleAndTag, error)
Update(ctx context.Context, conn transaction.Conn, dm *ArticleAndTag) (*ArticleAndTag, error)
Delete(ctx context.Context, conn transaction.Conn, dm *ArticleAndTag) (*ArticleAndTag, error)
FindOne(ctx context.Context, conn transaction.Conn, id int64) (*ArticleAndTag, error)
Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*ArticleAndTag, 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)
}
... ...
... ... @@ -27,6 +27,7 @@ type ArticleSectionRepository interface {
Delete(ctx context.Context, conn transaction.Conn, dm *ArticleSection) (*ArticleSection, error)
FindOne(ctx context.Context, conn transaction.Conn, id int64) (*ArticleSection, error)
Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*ArticleSection, error)
IncreaseCountComment(ctx context.Context, conn transaction.Conn, incr int, sectionId int64) error //评论数量变动
}
// 排序文章分段列表
... ...
... ... @@ -13,6 +13,7 @@ type MessageBusiness struct {
UserId int64 `json:"userId"` // 操作人用户ID
RecipientId int64 `json:"recipientId"` // 接收人用户ID
ArticleId int64 `json:"articleId"` // 文章ID
SectionId int64 `json:"sectionId"` // 段落ID
CommentId int64 `json:"commentId"` // 评论ID
Content string `json:"content"` // 消息内容
CreatedAt int64 `json:",omitempty"`
... ... @@ -35,7 +36,7 @@ const (
const (
OptTypeArticle MsgBusinessOpt = 1 // 操作分类-针对文章
OptTypeComment MsgBusinessOpt = 2 // 操作分类-针对评论
OptTypeComment MsgBusinessOpt = 2 // 操作分类-针对评论()
OptTypeDiscussion MsgBusinessOpt = 3 // 操作分类-针对讨论
)
... ...