mini_business_logic.go
9.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package message
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"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/contextdata"
"github.com/zeromicro/go-zero/core/logx"
)
type MiniBusinessLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewMiniBusinessLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MiniBusinessLogic {
return &MiniBusinessLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *MiniBusinessLogic) MiniBusiness(req *types.MessageRequest, msgType domain.MsgBusinessType) (resp *types.MessageBusinessResponse, err error) {
var userToken = contextdata.GetUserTokenFromCtx(l.ctx)
var conn = l.svcCtx.DefaultDBConn()
total, list, err := l.svcCtx.MessageBusinessRepository.Find(l.ctx, conn, domain.NewQueryOptions().
WithOffsetLimit(req.Page, req.Size).
WithKV("type", msgType).
WithKV("companyId", userToken.CompanyId).
WithKV("recipientId", userToken.UserId))
if err != nil {
return nil, err
}
// 输出
resp = &types.MessageBusinessResponse{}
resp.Total = total
resp.List = make([]types.MessageBusinessItem, 0)
if total == 0 {
return resp, nil
}
var companyIdMap = map[int64]*domain.Company{}
var userIdMap = map[int64]*domain.User{}
var articleIdMap = map[int64]*domain.Article{}
var commentIdMap = map[int64]*domain.ArticleComment{}
for _, item := range list {
if item.CompanyId != 0 {
companyIdMap[item.CompanyId] = nil
}
if item.UserId != 0 {
userIdMap[item.UserId] = nil
}
if item.ArticleId != 0 {
articleIdMap[item.ArticleId] = nil
}
if item.CommentId != 0 {
commentIdMap[item.CommentId] = nil
}
if item.CommentParentId != 0 {
commentIdMap[item.CommentParentId] = nil
}
}
var companyIds = make([]int64, 0) // 公司ID
var userIds = make([]int64, 0) // 用户ID
var articleIds = make([]int64, 0) // 文章ID
var commentIds = make([]int64, 0) // 评论ID(包含回复的评论)
for k, _ := range companyIdMap {
companyIds = append(companyIds, k)
}
for k, _ := range userIdMap {
userIds = append(userIds, k)
}
for k, _ := range articleIdMap {
articleIds = append(articleIds, k)
}
for k, _ := range commentIdMap {
commentIds = append(commentIds, k)
}
// 获取公司
if len(companyIds) > 0 {
_, companyList, err := l.svcCtx.CompanyRepository.Find(l.ctx, conn, domain.NewQueryOptions().
WithFindOnly().
WithKV("companyId", userToken.CompanyId).
WithKV("ids", companyIds).
WithKV("limit", len(companyIds)))
if err != nil {
return nil, err
}
for i := range companyList {
companyIdMap[companyList[i].Id] = companyList[i]
}
}
// 获取用户
if len(userIds) > 0 {
_, userList, err := l.svcCtx.UserRepository.Find(l.ctx, conn, domain.NewQueryOptions().
WithFindOnly().
WithKV("companyId", userToken.CompanyId).
WithKV("ids", userIds).
WithKV("limit", len(userIds)))
if err != nil {
return nil, err
}
for i := range userList {
userIdMap[userList[i].Id] = userList[i]
}
}
// 获取评论
if len(commentIds) > 0 {
_, commentList, err := l.svcCtx.ArticleCommentRepository.Find(l.ctx, conn, domain.NewQueryOptions().
WithFindOnly().
WithKV("companyId", userToken.CompanyId).
WithKV("ids", commentIds).
WithKV("limit", len(commentIds)))
if err != nil {
return nil, err
}
for i := range commentList {
commentIdMap[commentList[i].Id] = commentList[i]
}
}
// 获取文章数据
if len(articleIds) > 0 {
_, articleList, err := l.svcCtx.ArticleRepository.Find(l.ctx, conn, userToken.CompanyId, domain.NewQueryOptions().
WithFindOnly().
WithKV("companyId", userToken.CompanyId).
WithKV("ids", articleIds).
WithKV("limit", len(articleIds)))
if err != nil {
return nil, err
}
for i := range articleList {
articleIdMap[articleList[i].Id] = articleList[i]
}
}
for _, item := range list {
to := types.MessageBusinessItem{
Id: item.Id,
Type: int(item.Type),
OptType: int(item.OptType),
CompanyId: item.CompanyId,
UserId: item.UserId,
RecipientId: item.RecipientId,
ArticleId: item.ArticleId,
CreatedAt: item.CreatedAt,
}
// 发布者
if v, ok := userIdMap[item.UserId]; ok && v != nil {
to.User = &types.SimpleUser{
Id: v.Id,
CompanyId: v.CompanyId,
Position: v.Position,
Name: v.Name,
Avatar: v.Avatar,
}
if v, ok := companyIdMap[item.CompanyId]; ok && v != nil {
to.User.CompanyName = v.Name
}
}
// 文章
if v, ok := articleIdMap[item.ArticleId]; ok && v != nil {
to.Article = &types.SimpleArticle{
Id: v.Id,
Title: v.Title,
CountLove: v.CountLove,
CountComment: v.CountComment,
Show: int(v.Show),
}
}
// 评论
if v, ok := commentIdMap[item.CommentId]; ok && v != nil {
to.Comment = &types.SimpleComment{
Id: v.Id,
Content: v.Content,
Show: int(v.Show),
CountReply: v.CountReply,
CountUserLove: v.CountUserLove,
MatchUrl: v.MatchUrl,
}
to.Comment.AtWho = make([]types.CommentAtWho, 0)
for _, who := range v.AtWho {
to.Comment.AtWho = append(to.Comment.AtWho, types.CommentAtWho{
Id: who.Id,
Name: who.Name,
})
}
}
// 被回复的评论
if v, ok := commentIdMap[item.CommentParentId]; ok && v != nil {
to.CommentParent = &types.SimpleComment{
Id: v.Id,
Content: v.Content,
Show: int(v.Show),
CountReply: v.CountReply,
CountUserLove: v.CountUserLove,
MatchUrl: v.MatchUrl,
}
to.CommentParent.AtWho = make([]types.CommentAtWho, 0)
for _, who := range v.AtWho {
to.CommentParent.AtWho = append(to.CommentParent.AtWho, types.CommentAtWho{
Id: who.Id,
Name: who.Name,
})
}
}
resp.List = append(resp.List, to)
}
return resp, nil
}
// CommentArticle 评论文章
func (l *MiniBusinessLogic) CommentArticle(conn transaction.Conn, articleId int64, commentId int64, at []int64) (err error) {
return l.createMessage(conn, domain.MsgTypeReply, domain.OptTypeArticle, articleId, commentId, 0, at)
}
// CommentReply 评论回复
func (l *MiniBusinessLogic) CommentReply(conn transaction.Conn, articleId int64, commentId int64, commentParentId int64, at []int64) (err error) {
return l.createMessage(conn, domain.MsgTypeReply, domain.OptTypeComment, articleId, commentId, commentParentId, 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, 0, []int64{at})
}
// LikeComment 点赞评论
func (l *MiniBusinessLogic) LikeComment(conn transaction.Conn, articleId int64, commentId int64, commentParentId int64, at int64) (err error) {
return l.createMessage(conn, domain.MsgTypeLike, domain.OptTypeComment, articleId, commentId, commentParentId, []int64{at})
}
// UnLikeArticle 取消点赞文章
func (l *MiniBusinessLogic) UnLikeArticle(conn transaction.Conn, articleId int64, recipientId int64) (err error) {
return l.deleteMessage(conn, domain.OptTypeArticle, articleId, 0, recipientId)
}
// UnLikeComment 取消点赞评论
func (l *MiniBusinessLogic) UnLikeComment(conn transaction.Conn, articleId int64, commentId int64, recipientId int64) (err error) {
return l.deleteMessage(conn, domain.OptTypeComment, articleId, commentId, recipientId)
}
func (l *MiniBusinessLogic) createMessage(
conn transaction.Conn,
msgType domain.MsgBusinessType,
optType domain.MsgBusinessOpt,
articleId int64,
commentId int64,
commentParentId int64,
at []int64) (err error) {
var userToken = contextdata.GetUserTokenFromCtx(l.ctx)
// 评论中携带了 @其他用户
for i := range at {
var msg = &domain.MessageBusiness{
Type: msgType,
OptType: optType,
CompanyId: userToken.CompanyId,
UserId: userToken.UserId,
RecipientId: at[i],
ArticleId: articleId,
CommentId: commentId,
CommentParentId: commentParentId,
}
msg, err = l.svcCtx.MessageBusinessRepository.Insert(l.ctx, conn, msg)
if err != nil {
return err
}
}
return nil
}
func (l *MiniBusinessLogic) deleteMessage(conn transaction.Conn, optType domain.MsgBusinessOpt, articleId int64, commentId int64, recipientId int64) (err error) {
var userToken = contextdata.GetUserTokenFromCtx(l.ctx)
queryOption := domain.NewQueryOptions().WithFindOnly().WithOffsetLimit(1, 1).
MustWithKV("type", domain.MsgTypeLike).
MustWithKV("optType", optType).
MustWithKV("companyId", userToken.CompanyId).
MustWithKV("recipientId", recipientId).
MustWithKV("articleId", articleId).
MustWithKV("commentId", commentId)
_, list, err := l.svcCtx.MessageBusinessRepository.Find(l.ctx, conn, queryOption)
if err != nil {
return err
}
for i := range list {
_, err = l.svcCtx.MessageBusinessRepository.Delete(l.ctx, conn, list[i])
if err != nil {
return err
}
}
return nil
}