system_get_article_logic.go
4.5 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
package article
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/logic/core"
"github.com/samber/lo"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
"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 SystemGetArticleLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewSystemGetArticleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SystemGetArticleLogic {
return &SystemGetArticleLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *SystemGetArticleLogic) SystemGetArticle(req *types.SystemArticleGetRequest) (resp *types.SystemArticleGetResponse, err error) {
var conn = l.svcCtx.DefaultDBConn()
article, err := l.svcCtx.ArticleRepository.FindOne(l.ctx, conn, req.Id)
if err != nil {
return nil, xerr.NewErrMsgErr("获取帖子异常", err)
}
images := make([]string, 0)
lo.ForEach(article.Images, func(img domain.Image, n int) {
images = append(images, img.Url)
})
resp = &types.SystemArticleGetResponse{
Id: article.Id,
Title: article.Title,
AuthorId: article.AuthorId,
Author: types.ArticleAuthor{
Id: article.Author.Id,
Name: article.Author.Name,
Avatar: article.Author.Avatar,
Position: article.Author.Position,
Company: article.Author.Company,
},
CreatedAt: article.CreatedAt,
Section: make([]types.ArticleSection, 0),
Images: images,
Attachments: NewTypesAttachments(article.Attachments),
WhoRead: article.WhoRead,
WhoReadInfo: make([]types.UserShowName, 0),
WhoReview: article.WhoReview,
WhoReviewInfo: make([]types.UserShowName, 0),
Location: types.Location{
Longitude: article.Location.Longitude,
Latitude: article.Location.Latitude,
Descript: article.Location.Descript,
},
CountLove: article.CountLove,
CountComment: article.CountComment,
CountRead: article.CountRead,
Show: int(article.Show),
Tags: make([]types.ArticleTagItem, 0),
TargetUser: int(article.TargetUser),
Videos: make([]types.Video, 0),
Cover: article.GetCover(),
}
for _, val := range article.Videos {
resp.Videos = append(resp.Videos, types.Video{
Url: val.Url,
Cover: val.Cover,
Width: val.Width,
Height: val.Height,
})
}
//标签
if len(article.Tags) > 0 {
_, tags, err := l.svcCtx.ArticleTagRepository.Find(l.ctx, conn, article.CompanyId, domain.NewQueryOptions().WithKV("ids", article.Tags))
if err == nil && len(tags) > 0 {
lo.ForEach(tags, func(tag *domain.ArticleTag, index int) {
resp.Tags = append(resp.Tags, types.ArticleTagItem{
Id: tag.Id,
Category: tag.Category,
Name: tag.Name,
Image: tag.Image.Url,
})
})
}
}
//文章段落
_, articleSections, err := l.svcCtx.ArticleSectionRepository.Find(l.ctx, conn, domain.NewQueryOptions().WithKV("articleId", req.Id))
if err != nil {
return nil, xerr.NewErrMsgErr("获取帖子异常", err)
}
lo.ForEach(articleSections, func(item *domain.ArticleSection, index int) {
resp.Section = append(resp.Section, types.ArticleSection{
Id: item.Id,
Content: item.Content,
SortBy: item.SortBy,
TotalComment: item.TotalComment,
Images: item.Images,
ParagraphTemplate: core.NewTypesParagraph(item.ParagraphTemplate),
ParagraphType: item.ParagraphType,
})
})
resp.Cover = article.GetCoverWithSections(articleSections)
userIds := lo.Union(resp.WhoRead, resp.WhoReview)
if len(userIds) > 0 {
_, users, err := l.svcCtx.UserRepository.Find(l.ctx, conn, domain.NewQueryOptions().WithKV("ids", userIds))
if err != nil {
return nil, xerr.NewErrMsgErr("获取帖子异常", err)
}
userSlices := make(map[int64]types.UserShowName)
lo.ForEach(users, func(user *domain.User, index int) {
userSlices[user.Id] = types.UserShowName{
Id: int(user.Id),
Name: user.Name,
}
})
lo.ForEach(resp.WhoRead, func(userId int64, index int) {
if value, ok := userSlices[userId]; ok {
resp.WhoReadInfo = append(resp.WhoReadInfo, value)
}
})
lo.ForEach(resp.WhoReview, func(userId int64, index int) {
if value, ok := userSlices[userId]; ok {
resp.WhoReviewInfo = append(resp.WhoReviewInfo, value)
}
})
}
return resp, nil
}