作者 tangxvhui

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

@@ -5,6 +5,7 @@ import ( @@ -5,6 +5,7 @@ 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/db/transaction"
8 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain" 9 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
9 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr" 10 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
10 11
@@ -15,6 +16,7 @@ type MiniGetArticleCommentLogic struct { @@ -15,6 +16,7 @@ type MiniGetArticleCommentLogic struct {
15 logx.Logger 16 logx.Logger
16 ctx context.Context 17 ctx context.Context
17 svcCtx *svc.ServiceContext 18 svcCtx *svc.ServiceContext
  19 + userCache map[int64]types.CommentAuthor
18 } 20 }
19 21
20 func NewMiniGetArticleCommentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MiniGetArticleCommentLogic { 22 func NewMiniGetArticleCommentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MiniGetArticleCommentLogic {
@@ -22,12 +24,17 @@ func NewMiniGetArticleCommentLogic(ctx context.Context, svcCtx *svc.ServiceConte @@ -22,12 +24,17 @@ func NewMiniGetArticleCommentLogic(ctx context.Context, svcCtx *svc.ServiceConte
22 Logger: logx.WithContext(ctx), 24 Logger: logx.WithContext(ctx),
23 ctx: ctx, 25 ctx: ctx,
24 svcCtx: svcCtx, 26 svcCtx: svcCtx,
  27 + userCache: make(map[int64]types.CommentAuthor),
25 } 28 }
26 } 29 }
27 30
28 // 获取单条评论详情 31 // 获取单条评论详情
29 func (l *MiniGetArticleCommentLogic) MiniGetArticleComment(req *types.MiniGetArticleCommentRequest) (resp *types.MiniGetArticleCommentResponse, err error) { 32 func (l *MiniGetArticleCommentLogic) MiniGetArticleComment(req *types.MiniGetArticleCommentRequest) (resp *types.MiniGetArticleCommentResponse, err error) {
30 var conn = l.svcCtx.DefaultDBConn() 33 var conn = l.svcCtx.DefaultDBConn()
  34 + companyInfo, err := l.svcCtx.CompanyRepository.FindOne(l.ctx, conn, req.CompanyId)
  35 + if err != nil {
  36 + return nil, xerr.NewErrMsgErr("读取公司数据失败", err)
  37 + }
31 //获取主评论 38 //获取主评论
32 commentInfo, err := l.svcCtx.ArticleCommentRepository.FindOne(l.ctx, conn, req.CommentId) 39 commentInfo, err := l.svcCtx.ArticleCommentRepository.FindOne(l.ctx, conn, req.CommentId)
33 if err != nil { 40 if err != nil {
@@ -38,10 +45,14 @@ func (l *MiniGetArticleCommentLogic) MiniGetArticleComment(req *types.MiniGetArt @@ -38,10 +45,14 @@ func (l *MiniGetArticleCommentLogic) MiniGetArticleComment(req *types.MiniGetArt
38 } 45 }
39 46
40 if commentInfo.Show == domain.CommentShowDisable { 47 if commentInfo.Show == domain.CommentShowDisable {
41 - return nil, xerr.NewErrMsg("没有查看权限") 48 + return nil, xerr.NewErrMsg("具体评论已被关闭")
42 } 49 }
43 - queryOption := domain.NewQueryOptions().WithFindOnly().MustWithKV("topId", commentInfo.Id).MustWithKV("show", domain.CommentShowEnable)  
44 - //获取回复的评论 50 +
  51 + queryOption := domain.NewQueryOptions().
  52 + WithFindOnly().
  53 + MustWithKV("topId", commentInfo.Id).
  54 + MustWithKV("show", domain.CommentShowEnable)
  55 + // 获取回复的评论
45 _, replyCommenList, err := l.svcCtx.ArticleCommentRepository.Find(l.ctx, conn, queryOption) 56 _, replyCommenList, err := l.svcCtx.ArticleCommentRepository.Find(l.ctx, conn, queryOption)
46 57
47 if err != nil { 58 if err != nil {
@@ -60,7 +71,6 @@ func (l *MiniGetArticleCommentLogic) MiniGetArticleComment(req *types.MiniGetArt @@ -60,7 +71,6 @@ func (l *MiniGetArticleCommentLogic) MiniGetArticleComment(req *types.MiniGetArt
60 for _, val := range userFlagList { 71 for _, val := range userFlagList {
61 flagMap[val.CommentId] = struct{}{} 72 flagMap[val.CommentId] = struct{}{}
62 } 73 }
63 -  
64 //混合数据 74 //混合数据
65 commentResp := types.ArticleCommentItem{ 75 commentResp := types.ArticleCommentItem{
66 Id: commentInfo.Id, 76 Id: commentInfo.Id,
@@ -69,21 +79,9 @@ func (l *MiniGetArticleCommentLogic) MiniGetArticleComment(req *types.MiniGetArt @@ -69,21 +79,9 @@ func (l *MiniGetArticleCommentLogic) MiniGetArticleComment(req *types.MiniGetArt
69 ArtitcleId: commentInfo.ArticleId, 79 ArtitcleId: commentInfo.ArticleId,
70 SectionId: commentInfo.ArticleId, 80 SectionId: commentInfo.ArticleId,
71 FromUserId: commentInfo.FromUserId, 81 FromUserId: commentInfo.FromUserId,
72 - FromUser: types.CommentAuthor{  
73 - Id: commentInfo.FromUser.Id,  
74 - Name: commentInfo.FromUser.Name,  
75 - Avatar: commentInfo.FromUser.Avatar,  
76 - Position: commentInfo.FromUser.Position,  
77 - Company: commentInfo.FromUser.Company,  
78 - }, 82 + FromUser: l.getCommentAuthor(conn, commentInfo.FromUserId, companyInfo.Name),
79 ToUserId: commentInfo.ToUserId, 83 ToUserId: commentInfo.ToUserId,
80 - ToUser: types.CommentAuthor{  
81 - Id: commentInfo.ToUser.Id,  
82 - Name: commentInfo.ToUser.Name,  
83 - Avatar: commentInfo.ToUser.Avatar,  
84 - Position: commentInfo.ToUser.Position,  
85 - Company: commentInfo.ToUser.Company,  
86 - }, 84 + ToUser: l.getCommentAuthor(conn, commentInfo.ToUserId, companyInfo.Name),
87 SectionContent: commentInfo.SectionContent, 85 SectionContent: commentInfo.SectionContent,
88 CountReply: commentInfo.CountReply, 86 CountReply: commentInfo.CountReply,
89 CountUserLove: commentInfo.CountUserLove, 87 CountUserLove: commentInfo.CountUserLove,
@@ -116,21 +114,9 @@ func (l *MiniGetArticleCommentLogic) MiniGetArticleComment(req *types.MiniGetArt @@ -116,21 +114,9 @@ func (l *MiniGetArticleCommentLogic) MiniGetArticleComment(req *types.MiniGetArt
116 ArtitcleId: val.ArticleId, 114 ArtitcleId: val.ArticleId,
117 SectionId: val.SectionId, 115 SectionId: val.SectionId,
118 FromUserId: val.FromUserId, 116 FromUserId: val.FromUserId,
119 - FromUser: types.CommentAuthor{  
120 - Id: val.FromUser.Id,  
121 - Name: val.FromUser.Name,  
122 - Avatar: val.FromUser.Avatar,  
123 - Position: val.FromUser.Position,  
124 - Company: val.FromUser.Company,  
125 - }, 117 + FromUser: l.getCommentAuthor(conn, val.FromUserId, companyInfo.Name),
126 ToUserId: val.ToUserId, 118 ToUserId: val.ToUserId,
127 - ToUser: types.CommentAuthor{  
128 - Id: val.ToUser.Id,  
129 - Name: val.ToUser.Name,  
130 - Avatar: val.ToUser.Avatar,  
131 - Position: val.ToUser.Position,  
132 - Company: val.ToUser.Company,  
133 - }, 119 + ToUser: l.getCommentAuthor(conn, val.ToUserId, companyInfo.Name),
134 SectionContent: "", 120 SectionContent: "",
135 CountReply: val.CountReply, 121 CountReply: val.CountReply,
136 CountUserLove: val.CountUserLove, 122 CountUserLove: val.CountUserLove,
@@ -141,6 +127,7 @@ func (l *MiniGetArticleCommentLogic) MiniGetArticleComment(req *types.MiniGetArt @@ -141,6 +127,7 @@ func (l *MiniGetArticleCommentLogic) MiniGetArticleComment(req *types.MiniGetArt
141 MeLoveFlag: 0, 127 MeLoveFlag: 0,
142 Content: val.Content, 128 Content: val.Content,
143 } 129 }
  130 +
144 if _, ok := flagMap[val.Id]; ok { 131 if _, ok := flagMap[val.Id]; ok {
145 reply.MeLoveFlag = 1 132 reply.MeLoveFlag = 1
146 } 133 }
@@ -164,3 +151,22 @@ func (l *MiniGetArticleCommentLogic) MiniGetArticleComment(req *types.MiniGetArt @@ -164,3 +151,22 @@ func (l *MiniGetArticleCommentLogic) MiniGetArticleComment(req *types.MiniGetArt
164 } 151 }
165 return 152 return
166 } 153 }
  154 +
  155 +func (l *MiniGetArticleCommentLogic) getCommentAuthor(conn transaction.Conn, userid int64, companyName string) types.CommentAuthor {
  156 + if u, ok := l.userCache[userid]; ok {
  157 + return u
  158 + }
  159 + toUser, err := l.svcCtx.UserRepository.FindOne(l.ctx, conn, userid)
  160 + if err == nil {
  161 + commentToUser := types.CommentAuthor{
  162 + Id: toUser.Id,
  163 + Name: toUser.Name,
  164 + Avatar: toUser.Avatar,
  165 + Position: toUser.Position,
  166 + Company: companyName,
  167 + }
  168 + l.userCache[toUser.Id] = commentToUser
  169 + return commentToUser
  170 + }
  171 + return types.CommentAuthor{}
  172 +}
@@ -5,6 +5,7 @@ import ( @@ -5,6 +5,7 @@ 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/db/transaction"
8 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain" 9 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
9 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr" 10 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
10 11
@@ -15,6 +16,7 @@ type MiniListArticleCommentLogic struct { @@ -15,6 +16,7 @@ type MiniListArticleCommentLogic struct {
15 logx.Logger 16 logx.Logger
16 ctx context.Context 17 ctx context.Context
17 svcCtx *svc.ServiceContext 18 svcCtx *svc.ServiceContext
  19 + userCache map[int64]types.CommentAuthor
18 } 20 }
19 21
20 func NewMiniListArticleCommentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MiniListArticleCommentLogic { 22 func NewMiniListArticleCommentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MiniListArticleCommentLogic {
@@ -22,6 +24,7 @@ func NewMiniListArticleCommentLogic(ctx context.Context, svcCtx *svc.ServiceCont @@ -22,6 +24,7 @@ func NewMiniListArticleCommentLogic(ctx context.Context, svcCtx *svc.ServiceCont
22 Logger: logx.WithContext(ctx), 24 Logger: logx.WithContext(ctx),
23 ctx: ctx, 25 ctx: ctx,
24 svcCtx: svcCtx, 26 svcCtx: svcCtx,
  27 + userCache: make(map[int64]types.CommentAuthor),
25 } 28 }
26 } 29 }
27 30
@@ -29,6 +32,10 @@ func NewMiniListArticleCommentLogic(ctx context.Context, svcCtx *svc.ServiceCont @@ -29,6 +32,10 @@ func NewMiniListArticleCommentLogic(ctx context.Context, svcCtx *svc.ServiceCont
29 func (l *MiniListArticleCommentLogic) MiniListArticleComment(req *types.MiniListArticleCommentRequest) (resp *types.MiniListArticleCommentResponse, err error) { 32 func (l *MiniListArticleCommentLogic) MiniListArticleComment(req *types.MiniListArticleCommentRequest) (resp *types.MiniListArticleCommentResponse, err error) {
30 // 先获取最顶层的评论 33 // 先获取最顶层的评论
31 var conn = l.svcCtx.DefaultDBConn() 34 var conn = l.svcCtx.DefaultDBConn()
  35 + companyInfo, err := l.svcCtx.CompanyRepository.FindOne(l.ctx, conn, req.CompanyId)
  36 + if err != nil {
  37 + return nil, xerr.NewErrMsgErr("获取评论信息失败", err)
  38 + }
32 if req.Size > 40 { 39 if req.Size > 40 {
33 req.Size = 40 40 req.Size = 40
34 } 41 }
@@ -54,7 +61,6 @@ func (l *MiniListArticleCommentLogic) MiniListArticleComment(req *types.MiniList @@ -54,7 +61,6 @@ func (l *MiniListArticleCommentLogic) MiniListArticleComment(req *types.MiniList
54 } 61 }
55 return 62 return
56 } 63 }
57 -  
58 queryOption = domain.NewQueryOptions().WithFindOnly(). 64 queryOption = domain.NewQueryOptions().WithFindOnly().
59 MustWithKV("articleId", req.ArticleId). 65 MustWithKV("articleId", req.ArticleId).
60 MustWithKV("userId", req.UserId) 66 MustWithKV("userId", req.UserId)
@@ -82,21 +88,9 @@ func (l *MiniListArticleCommentLogic) MiniListArticleComment(req *types.MiniList @@ -82,21 +88,9 @@ func (l *MiniListArticleCommentLogic) MiniListArticleComment(req *types.MiniList
82 ArtitcleId: val.ArticleId, 88 ArtitcleId: val.ArticleId,
83 SectionId: val.SectionId, 89 SectionId: val.SectionId,
84 FromUserId: val.FromUserId, 90 FromUserId: val.FromUserId,
85 - FromUser: types.CommentAuthor{  
86 - Id: val.FromUser.Id,  
87 - Name: val.FromUser.Name,  
88 - Avatar: val.FromUser.Avatar,  
89 - Position: val.FromUser.Position,  
90 - Company: val.FromUser.Company,  
91 - }, 91 + FromUser: l.getCommentAuthor(conn, val.FromUserId, companyInfo.Name),
92 ToUserId: val.ToUserId, 92 ToUserId: val.ToUserId,
93 - ToUser: types.CommentAuthor{  
94 - Id: val.ToUser.Id,  
95 - Name: val.ToUser.Name,  
96 - Avatar: val.ToUser.Avatar,  
97 - Position: val.ToUser.Position,  
98 - Company: val.ToUser.Company,  
99 - }, 93 + ToUser: l.getCommentAuthor(conn, val.ToUserId, companyInfo.Name),
100 SectionContent: val.SectionContent, 94 SectionContent: val.SectionContent,
101 CountReply: val.CountReply, 95 CountReply: val.CountReply,
102 CountUserLove: val.CountUserLove, 96 CountUserLove: val.CountUserLove,
@@ -126,7 +120,6 @@ func (l *MiniListArticleCommentLogic) MiniListArticleComment(req *types.MiniList @@ -126,7 +120,6 @@ func (l *MiniListArticleCommentLogic) MiniListArticleComment(req *types.MiniList
126 120
127 //获取回复的评论 121 //获取回复的评论
128 cntReply, reply := l.listCommentReply(item.Comment.Id, flagMap) 122 cntReply, reply := l.listCommentReply(item.Comment.Id, flagMap)
129 -  
130 resp.List[i] = item 123 resp.List[i] = item
131 resp.List[i].Reply = reply 124 resp.List[i].Reply = reply
132 resp.List[i].TotalReply = cntReply 125 resp.List[i].TotalReply = cntReply
@@ -137,6 +130,10 @@ func (l *MiniListArticleCommentLogic) MiniListArticleComment(req *types.MiniList @@ -137,6 +130,10 @@ func (l *MiniListArticleCommentLogic) MiniListArticleComment(req *types.MiniList
137 // listCommentReply 130 // listCommentReply
138 func (l *MiniListArticleCommentLogic) listCommentReply(commentId int64, loveFlagMap map[int64]struct{}) (cnt int64, replyList []types.ArticleCommentItem) { 131 func (l *MiniListArticleCommentLogic) listCommentReply(commentId int64, loveFlagMap map[int64]struct{}) (cnt int64, replyList []types.ArticleCommentItem) {
139 var conn = l.svcCtx.DefaultDBConn() 132 var conn = l.svcCtx.DefaultDBConn()
  133 + companyInfo, err := l.svcCtx.CompanyRepository.FindOne(l.ctx, conn, commentId)
  134 + if err != nil {
  135 + return 0, []types.ArticleCommentItem{}
  136 + }
140 queryOption := domain.NewQueryOptions(). 137 queryOption := domain.NewQueryOptions().
141 WithOffsetLimit(1, 2). 138 WithOffsetLimit(1, 2).
142 MustWithKV("topId", commentId). 139 MustWithKV("topId", commentId).
@@ -159,21 +156,9 @@ func (l *MiniListArticleCommentLogic) listCommentReply(commentId int64, loveFlag @@ -159,21 +156,9 @@ func (l *MiniListArticleCommentLogic) listCommentReply(commentId int64, loveFlag
159 ArtitcleId: val.ArticleId, 156 ArtitcleId: val.ArticleId,
160 SectionId: val.SectionId, 157 SectionId: val.SectionId,
161 FromUserId: val.FromUserId, 158 FromUserId: val.FromUserId,
162 - FromUser: types.CommentAuthor{  
163 - Id: val.FromUser.Id,  
164 - Name: val.FromUser.Name,  
165 - Avatar: val.FromUser.Avatar,  
166 - Position: val.FromUser.Position,  
167 - Company: val.FromUser.Company,  
168 - }, 159 + FromUser: l.getCommentAuthor(conn, val.FromUserId, companyInfo.Name),
169 ToUserId: val.ToUserId, 160 ToUserId: val.ToUserId,
170 - ToUser: types.CommentAuthor{  
171 - Id: val.ToUser.Id,  
172 - Name: val.ToUser.Name,  
173 - Avatar: val.ToUser.Avatar,  
174 - Position: val.ToUser.Position,  
175 - Company: val.ToUser.Company,  
176 - }, 161 + ToUser: l.getCommentAuthor(conn, val.ToUserId, companyInfo.Name),
177 SectionContent: "", //不设置值 162 SectionContent: "", //不设置值
178 CountReply: val.CountReply, 163 CountReply: val.CountReply,
179 CountUserLove: val.CountUserLove, 164 CountUserLove: val.CountUserLove,
@@ -184,7 +169,6 @@ func (l *MiniListArticleCommentLogic) listCommentReply(commentId int64, loveFlag @@ -184,7 +169,6 @@ func (l *MiniListArticleCommentLogic) listCommentReply(commentId int64, loveFlag
184 MeLoveFlag: 0, 169 MeLoveFlag: 0,
185 Content: val.Content, 170 Content: val.Content,
186 } 171 }
187 -  
188 if _, ok := loveFlagMap[val.Id]; ok { 172 if _, ok := loveFlagMap[val.Id]; ok {
189 item.MeLoveFlag = 1 173 item.MeLoveFlag = 1
190 } 174 }
@@ -201,3 +185,22 @@ func (l *MiniListArticleCommentLogic) listCommentReply(commentId int64, loveFlag @@ -201,3 +185,22 @@ func (l *MiniListArticleCommentLogic) listCommentReply(commentId int64, loveFlag
201 } 185 }
202 return cnt, replyList 186 return cnt, replyList
203 } 187 }
  188 +
  189 +func (l *MiniListArticleCommentLogic) getCommentAuthor(conn transaction.Conn, userid int64, companyName string) types.CommentAuthor {
  190 + if u, ok := l.userCache[userid]; ok {
  191 + return u
  192 + }
  193 + toUser, err := l.svcCtx.UserRepository.FindOne(l.ctx, conn, userid)
  194 + if err == nil {
  195 + commentToUser := types.CommentAuthor{
  196 + Id: toUser.Id,
  197 + Name: toUser.Name,
  198 + Avatar: toUser.Avatar,
  199 + Position: toUser.Position,
  200 + Company: companyName,
  201 + }
  202 + l.userCache[toUser.Id] = commentToUser
  203 + return commentToUser
  204 + }
  205 + return types.CommentAuthor{}
  206 +}