作者 tangxvhui

调整逻辑,列表上 获取人员信息

... ... @@ -5,6 +5,7 @@ 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/db/transaction"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
... ... @@ -13,21 +14,27 @@ import (
type MiniGetArticleCommentLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
ctx context.Context
svcCtx *svc.ServiceContext
userCache map[int64]types.CommentAuthor
}
func NewMiniGetArticleCommentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MiniGetArticleCommentLogic {
return &MiniGetArticleCommentLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
userCache: make(map[int64]types.CommentAuthor),
}
}
// 获取单条评论详情
func (l *MiniGetArticleCommentLogic) MiniGetArticleComment(req *types.MiniGetArticleCommentRequest) (resp *types.MiniGetArticleCommentResponse, err error) {
var conn = l.svcCtx.DefaultDBConn()
companyInfo, err := l.svcCtx.CompanyRepository.FindOne(l.ctx, conn, req.CompanyId)
if err != nil {
return nil, xerr.NewErrMsgErr("读取公司数据失败", err)
}
//获取主评论
commentInfo, err := l.svcCtx.ArticleCommentRepository.FindOne(l.ctx, conn, req.CommentId)
if err != nil {
... ... @@ -38,10 +45,14 @@ func (l *MiniGetArticleCommentLogic) MiniGetArticleComment(req *types.MiniGetArt
}
if commentInfo.Show == domain.CommentShowDisable {
return nil, xerr.NewErrMsg("没有查看权限")
return nil, xerr.NewErrMsg("具体评论已被关闭")
}
queryOption := domain.NewQueryOptions().WithFindOnly().MustWithKV("topId", commentInfo.Id).MustWithKV("show", domain.CommentShowEnable)
//获取回复的评论
queryOption := domain.NewQueryOptions().
WithFindOnly().
MustWithKV("topId", commentInfo.Id).
MustWithKV("show", domain.CommentShowEnable)
// 获取回复的评论
_, replyCommenList, err := l.svcCtx.ArticleCommentRepository.Find(l.ctx, conn, queryOption)
if err != nil {
... ... @@ -60,30 +71,17 @@ func (l *MiniGetArticleCommentLogic) MiniGetArticleComment(req *types.MiniGetArt
for _, val := range userFlagList {
flagMap[val.CommentId] = struct{}{}
}
//混合数据
commentResp := types.ArticleCommentItem{
Id: commentInfo.Id,
Pid: commentInfo.Pid,
TopId: commentInfo.TopId,
ArtitcleId: commentInfo.ArticleId,
SectionId: commentInfo.ArticleId,
FromUserId: commentInfo.FromUserId,
FromUser: types.CommentAuthor{
Id: commentInfo.FromUser.Id,
Name: commentInfo.FromUser.Name,
Avatar: commentInfo.FromUser.Avatar,
Position: commentInfo.FromUser.Position,
Company: commentInfo.FromUser.Company,
},
ToUserId: commentInfo.ToUserId,
ToUser: types.CommentAuthor{
Id: commentInfo.ToUser.Id,
Name: commentInfo.ToUser.Name,
Avatar: commentInfo.ToUser.Avatar,
Position: commentInfo.ToUser.Position,
Company: commentInfo.ToUser.Company,
},
Id: commentInfo.Id,
Pid: commentInfo.Pid,
TopId: commentInfo.TopId,
ArtitcleId: commentInfo.ArticleId,
SectionId: commentInfo.ArticleId,
FromUserId: commentInfo.FromUserId,
FromUser: l.getCommentAuthor(conn, commentInfo.FromUserId, companyInfo.Name),
ToUserId: commentInfo.ToUserId,
ToUser: l.getCommentAuthor(conn, commentInfo.ToUserId, companyInfo.Name),
SectionContent: commentInfo.SectionContent,
CountReply: commentInfo.CountReply,
CountUserLove: commentInfo.CountUserLove,
... ... @@ -110,27 +108,15 @@ func (l *MiniGetArticleCommentLogic) MiniGetArticleComment(req *types.MiniGetArt
allReply := []types.ArticleCommentItem{}
for _, val := range replyCommenList {
reply := 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,
},
Id: val.Id,
Pid: val.Pid,
TopId: val.TopId,
ArtitcleId: val.ArticleId,
SectionId: val.SectionId,
FromUserId: val.FromUserId,
FromUser: l.getCommentAuthor(conn, val.FromUserId, companyInfo.Name),
ToUserId: val.ToUserId,
ToUser: l.getCommentAuthor(conn, val.ToUserId, companyInfo.Name),
SectionContent: "",
CountReply: val.CountReply,
CountUserLove: val.CountUserLove,
... ... @@ -141,6 +127,7 @@ func (l *MiniGetArticleCommentLogic) MiniGetArticleComment(req *types.MiniGetArt
MeLoveFlag: 0,
Content: val.Content,
}
if _, ok := flagMap[val.Id]; ok {
reply.MeLoveFlag = 1
}
... ... @@ -164,3 +151,22 @@ func (l *MiniGetArticleCommentLogic) MiniGetArticleComment(req *types.MiniGetArt
}
return
}
func (l *MiniGetArticleCommentLogic) getCommentAuthor(conn transaction.Conn, userid int64, companyName string) types.CommentAuthor {
if u, ok := l.userCache[userid]; ok {
return u
}
toUser, err := l.svcCtx.UserRepository.FindOne(l.ctx, conn, userid)
if err == nil {
commentToUser := types.CommentAuthor{
Id: toUser.Id,
Name: toUser.Name,
Avatar: toUser.Avatar,
Position: toUser.Position,
Company: companyName,
}
l.userCache[toUser.Id] = commentToUser
return commentToUser
}
return types.CommentAuthor{}
}
... ...
... ... @@ -5,6 +5,7 @@ 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/db/transaction"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
... ... @@ -13,15 +14,17 @@ import (
type MiniListArticleCommentLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
ctx context.Context
svcCtx *svc.ServiceContext
userCache map[int64]types.CommentAuthor
}
func NewMiniListArticleCommentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MiniListArticleCommentLogic {
return &MiniListArticleCommentLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
userCache: make(map[int64]types.CommentAuthor),
}
}
... ... @@ -29,6 +32,10 @@ func NewMiniListArticleCommentLogic(ctx context.Context, svcCtx *svc.ServiceCont
func (l *MiniListArticleCommentLogic) MiniListArticleComment(req *types.MiniListArticleCommentRequest) (resp *types.MiniListArticleCommentResponse, err error) {
// 先获取最顶层的评论
var conn = l.svcCtx.DefaultDBConn()
companyInfo, err := l.svcCtx.CompanyRepository.FindOne(l.ctx, conn, req.CompanyId)
if err != nil {
return nil, xerr.NewErrMsgErr("获取评论信息失败", err)
}
if req.Size > 40 {
req.Size = 40
}
... ... @@ -54,7 +61,6 @@ func (l *MiniListArticleCommentLogic) MiniListArticleComment(req *types.MiniList
}
return
}
queryOption = domain.NewQueryOptions().WithFindOnly().
MustWithKV("articleId", req.ArticleId).
MustWithKV("userId", req.UserId)
... ... @@ -76,27 +82,15 @@ func (l *MiniListArticleCommentLogic) MiniListArticleComment(req *types.MiniList
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,
},
Id: val.Id,
Pid: val.Pid,
TopId: val.TopId,
ArtitcleId: val.ArticleId,
SectionId: val.SectionId,
FromUserId: val.FromUserId,
FromUser: l.getCommentAuthor(conn, val.FromUserId, companyInfo.Name),
ToUserId: val.ToUserId,
ToUser: l.getCommentAuthor(conn, val.ToUserId, companyInfo.Name),
SectionContent: val.SectionContent,
CountReply: val.CountReply,
CountUserLove: val.CountUserLove,
... ... @@ -126,7 +120,6 @@ func (l *MiniListArticleCommentLogic) MiniListArticleComment(req *types.MiniList
//获取回复的评论
cntReply, reply := l.listCommentReply(item.Comment.Id, flagMap)
resp.List[i] = item
resp.List[i].Reply = reply
resp.List[i].TotalReply = cntReply
... ... @@ -137,6 +130,10 @@ func (l *MiniListArticleCommentLogic) MiniListArticleComment(req *types.MiniList
// listCommentReply
func (l *MiniListArticleCommentLogic) listCommentReply(commentId int64, loveFlagMap map[int64]struct{}) (cnt int64, replyList []types.ArticleCommentItem) {
var conn = l.svcCtx.DefaultDBConn()
companyInfo, err := l.svcCtx.CompanyRepository.FindOne(l.ctx, conn, commentId)
if err != nil {
return 0, []types.ArticleCommentItem{}
}
queryOption := domain.NewQueryOptions().
WithOffsetLimit(1, 2).
MustWithKV("topId", commentId).
... ... @@ -153,27 +150,15 @@ func (l *MiniListArticleCommentLogic) listCommentReply(commentId int64, loveFlag
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,
},
Id: val.Id,
Pid: val.Pid,
TopId: val.TopId,
ArtitcleId: val.ArticleId,
SectionId: val.SectionId,
FromUserId: val.FromUserId,
FromUser: l.getCommentAuthor(conn, val.FromUserId, companyInfo.Name),
ToUserId: val.ToUserId,
ToUser: l.getCommentAuthor(conn, val.ToUserId, companyInfo.Name),
SectionContent: "", //不设置值
CountReply: val.CountReply,
CountUserLove: val.CountUserLove,
... ... @@ -184,7 +169,6 @@ func (l *MiniListArticleCommentLogic) listCommentReply(commentId int64, loveFlag
MeLoveFlag: 0,
Content: val.Content,
}
if _, ok := loveFlagMap[val.Id]; ok {
item.MeLoveFlag = 1
}
... ... @@ -201,3 +185,22 @@ func (l *MiniListArticleCommentLogic) listCommentReply(commentId int64, loveFlag
}
return cnt, replyList
}
func (l *MiniListArticleCommentLogic) getCommentAuthor(conn transaction.Conn, userid int64, companyName string) types.CommentAuthor {
if u, ok := l.userCache[userid]; ok {
return u
}
toUser, err := l.svcCtx.UserRepository.FindOne(l.ctx, conn, userid)
if err == nil {
commentToUser := types.CommentAuthor{
Id: toUser.Id,
Name: toUser.Name,
Avatar: toUser.Avatar,
Position: toUser.Position,
Company: companyName,
}
l.userCache[toUser.Id] = commentToUser
return commentToUser
}
return types.CommentAuthor{}
}
... ...