mini_my_like_logic.go
3.7 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
package user
import (
"context"
"github.com/samber/lo"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/contextdata"
"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 MiniMyLikeLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewMiniMyLikeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MiniMyLikeLogic {
return &MiniMyLikeLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *MiniMyLikeLogic) MiniMyLike(req *types.MiniMyLikeRequest) (resp *types.MiniMyLikeResponse, err error) {
var userToken = contextdata.GetUserTokenFromCtx(l.ctx)
var conn = l.svcCtx.DefaultDBConn()
total, list, err := l.svcCtx.UserLoveFlagRepository.Find(l.ctx, conn, domain.NewQueryOptions().
WithOffsetLimit(req.Page, req.Size).
//WithKV("companyId", userToken.CompanyId). 不存在公司id
WithKV("userId", userToken.UserId))
if err != nil {
return nil, err
}
resp = &types.MiniMyLikeResponse{}
resp.Total = total
resp.List = make([]types.MyLikeItem, 0)
if total == 0 || len(list) == 0 {
return resp, nil
}
var companyMap = make(map[int64]*domain.Company)
var userMap = make(map[int64]*domain.User)
var articleMap = make(map[int64]*domain.Article)
var commentMap = make(map[int64]*domain.ArticleComment)
lo.ForEach(list, func(item *domain.UserLoveFlag, index int) {
var (
company *domain.Company
user *domain.User
article *domain.Article
comment *domain.ArticleComment
)
user, _ = domain.LazyLoad(userMap, l.ctx, conn, item.ToUserId, l.svcCtx.UserRepository.FindOne)
if user != nil {
company, _ = domain.LazyLoad(companyMap, l.ctx, conn, user.CompanyId, l.svcCtx.CompanyRepository.FindOne)
}
article, _ = domain.LazyLoad(articleMap, l.ctx, conn, item.ArticleId, l.svcCtx.ArticleRepository.FindOne)
// 点赞评论
if item.CommentId != 0 {
comment, _ = domain.LazyLoad(commentMap, l.ctx, conn, item.CommentId, l.svcCtx.ArticleCommentRepository.FindOne)
}
resp.List = append(resp.List, l.NewItemSimple(item, company, user, article, comment))
})
return resp, nil
}
func (l *MiniMyLikeLogic) NewItemSimple(love *domain.UserLoveFlag, company *domain.Company, user *domain.User, article *domain.Article, comment *domain.ArticleComment) types.MyLikeItem {
item := types.MyLikeItem{
UserId: love.ToUserId,
ArticleId: love.ArticleId,
CommentId: love.CommentId,
CreatedAt: love.CreatedAt,
}
if user != nil {
item.User = &types.SimpleUser{
Id: user.Id,
CompanyId: user.CompanyId,
Name: user.Name,
Avatar: user.Avatar,
Position: user.Position,
}
if company != nil {
item.User.CompanyName = company.Name
}
}
if article != nil {
item.Article = &types.SimpleArticle{
Id: article.Id,
Title: article.Title,
Summary: article.Summary,
CountLove: article.CountLove,
CountComment: article.CountComment,
Show: int(article.Show),
}
}
if comment != nil {
item.Comment = &types.SimpleComment{
Id: comment.Id,
Content: comment.Content,
Show: int(comment.Show),
CountReply: comment.CountReply,
CountUserLove: comment.CountUserLove,
MatchUrl: comment.MatchUrl,
}
item.Comment.AtWho = make([]types.CommentAtWho, 0)
for _, who := range comment.AtWho {
item.Comment.AtWho = append(item.Comment.AtWho, types.CommentAtWho{
Id: who.Id,
Name: who.Name,
})
}
}
return item
}