system_search_article_logic.go
3.0 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
package article
import (
"context"
"github.com/samber/lo"
"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 SystemSearchArticleLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewSystemSearchArticleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SystemSearchArticleLogic {
return &SystemSearchArticleLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *SystemSearchArticleLogic) SystemSearchArticle(req *types.SystemArticleSearchRequest) (resp *types.SystemArticleSearchResponse, err error) {
var conn = l.svcCtx.DefaultDBConn()
queryOptions := domain.NewQueryOptions().
WithOffsetLimit(req.Page, req.Size).
WithKV("title", req.Title).
WithKV("authorId", req.Author).
WithKV("tags", req.Tags).
WithKV("beginCreatedAt", req.BeginTime).
WithKV("endCreatedAt", req.EndTime).
WithKV("orderMode", req.OrderMode)
total, articles, err := l.svcCtx.ArticleRepository.Find(l.ctx, conn, req.CompanyId, queryOptions)
if err != nil {
return nil, xerr.NewErrMsgErr("搜索帖子异常", err)
}
resp = &types.SystemArticleSearchResponse{
Total: int(total),
List: make([]types.SystemArticleSearch, 0),
}
authorIds := make([]int64, 0)
lo.ForEach(articles, func(item *domain.Article, index int) {
authorIds = append(authorIds, item.AuthorId)
})
//查询用户数据,重新赋值更新用户名称
_, users, _ := l.svcCtx.UserRepository.Find(l.ctx, conn, domain.NewQueryOptions().WithFindOnly().WithKV("ids", authorIds))
//获取标签
_, tags, _ := l.svcCtx.ArticleTagRepository.Find(l.ctx, conn, req.CompanyId, domain.NewQueryOptions())
lo.ForEach(articles, func(item *domain.Article, index int) {
//图片
images := make([]string, 0)
lo.ForEach(item.Images, func(img domain.Image, n int) {
images = append(images, img.Url)
})
//发布人
author := item.Author.Name
for _, user := range users {
if user.Id == item.AuthorId {
author = user.Name
}
}
//标签
articleTags := make([]string, 0)
lo.ForEach(item.Tags, func(tagId int64, index int) {
for _, t := range tags {
if t.Id == tagId {
articleTags = append(articleTags, t.Name)
}
}
})
resp.List = append(resp.List, types.SystemArticleSearch{
Id: item.Id,
Title: item.Title,
AuthorId: item.AuthorId,
Author: author,
Images: images,
CreatedAt: item.CreatedAt,
UpdatedAt: item.UpdatedAt,
CountLove: item.CountLove,
CountComment: item.CountComment,
Show: int(item.Show),
Tags: articleTags,
TargetUser: int(item.TargetUser),
Source: item.Source,
Operator: types.Operator{
Id: item.Operator.Id,
Name: item.Operator.Name,
},
})
})
return
}