system_update_article_logic.go
7.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
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
package article
import (
"context"
"strconv"
"unicode/utf8"
"strings"
"github.com/samber/lo"
"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/cmd/discuss/interanl/pkg/gateway/authlib"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/contextdata"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/tool/oss"
"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 SystemUpdateArticleLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewSystemUpdateArticleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SystemUpdateArticleLogic {
return &SystemUpdateArticleLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *SystemUpdateArticleLogic) SystemUpdateArticle(req *types.SystemArticleUpdateRequest) (resp *types.SystemArticleUpdateResponse, err error) {
var conn = l.svcCtx.DefaultDBConn()
userToken := contextdata.GetUserTokenFromCtx(l.ctx)
// 预处理参数
if req.TargetUser == 0 {
//TargetUser 设定为分发给所有人,清空 WhoRead
req.WhoRead = make([]int64, 0)
}
// 检查文字数量
wordNum := 0
for i := range req.Section {
num := utf8.RuneCountInString(req.Section[i].Content)
wordNum += num
}
if wordNum >= 1000 {
return nil, xerr.NewErrMsgErr("最多只能输入1000字", err)
}
// 获取当前用户信息
userMe, err := l.svcCtx.ApiAuthService.MeInfo(l.ctx, authlib.RequestUserMeQuery{Token: req.AccessToken})
if err != nil {
return nil, xerr.NewErrMsgErr("获取当前用户信息失败", err)
}
// 文章数据
article, err := l.svcCtx.ArticleRepository.FindOne(l.ctx, conn, req.Id)
if err != nil {
return nil, xerr.NewErrMsgErr("帖子不存在", err)
}
_, sectionList, err := l.svcCtx.ArticleSectionRepository.Find(l.ctx, conn, map[string]interface{}{"articleId": article.Id})
if err != nil {
return nil, xerr.NewErrMsgErr("获取文章段落内容失败", err)
}
//备份数据
backup := article.MakeBackup(domain.UserSimple{
Id: userToken.UserId,
Name: userMe.User.NickName,
Avatar: userMe.User.Avatar,
CompanyId: userToken.CompanyId,
Company: userMe.CurrentCompany.Name,
}, sectionList)
backup.Action = "编辑"
// 获取图片的尺寸大小
images := []domain.Image{}
for _, val := range req.Images {
fInfo, _ := oss.GetImageInfo(val)
w, _ := strconv.Atoi(fInfo.ImageWidth.Value)
h, _ := strconv.Atoi(fInfo.ImageHeight.Value)
images = append(images, domain.Image{
Url: val,
Width: w,
Height: h,
})
}
//检查文章可被哪些人查看
whoRead := []int64{}
if len(req.WhoRead) > 0 {
whoRead = lo.Uniq(req.WhoRead)
var u *domain.User
for _, val := range whoRead {
u, err = l.svcCtx.UserRepository.FindOne(l.ctx, conn, val)
if err != nil {
return nil, xerr.NewErrMsgErr("文章可查看人设置错误", err)
}
if u.CompanyId != article.CompanyId {
return nil, xerr.NewErrMsg("文章可查看人设置错误")
}
}
}
//检查文章可被哪些人评论
whoReview := []int64{}
if len(req.WhoReview) > 0 {
whoReview = lo.Uniq(req.WhoReview)
}
//有指定可查看人的情况
if len(whoRead) > 0 {
if len(whoReview) > 0 {
// 检查 whoRead 是否 完全包含 whoReview
ok := lo.Every(whoRead, whoReview)
if !ok {
return nil, xerr.NewErrMsg("文章可评论人设置错误")
}
}
if len(whoReview) == 0 {
//有指定可查看人 ,但未指定可评论人
return nil, xerr.NewErrMsg("文章可评论人设置错误")
}
}
//没有指定可查看人的情况
if len(whoRead) == 0 {
if len(whoReview) > 0 {
// 未指定可查看人(全员可看),有指定可评论人,
var u *domain.User
for _, val := range whoReview {
u, err = l.svcCtx.UserRepository.FindOne(l.ctx, conn, val)
if err != nil {
return nil, xerr.NewErrMsgErr("文章可评论人设置错误", err)
}
if u.CompanyId != article.CompanyId {
return nil, xerr.NewErrMsg("文章可评论人设置错误")
}
}
}
}
//验证tag
if len(req.Tags) > 0 {
req.Tags = lo.Uniq(req.Tags)
for _, value := range req.Tags {
t, err := l.svcCtx.ArticleTagRepository.FindOne(l.ctx, conn, value)
if err != nil {
return nil, xerr.NewErrMsgErr("文章标签设置错误", err)
}
if t.CompanyId != article.CompanyId {
return nil, xerr.NewErrMsgErr("文章标签设置错误", err)
}
}
}
article.Title = req.Title
article.Version = article.Version + 1
article.Images = images
article.WhoRead = whoRead
article.WhoReview = whoReview
article.TargetUser = domain.ArticleTarget(req.TargetUser)
article.Tags = req.Tags
//文章内容
articleSections := []domain.ArticleSection{}
sortBy := 1
lo.ForEach(req.Section, func(item types.ArticleSection, index int) {
strList := strings.Split(item.Content, "\n")
for key, value := range strList {
if value == "" {
continue
}
section := domain.ArticleSection{
CompanyId: article.CompanyId,
Version: article.Version,
ArticleId: article.Id,
Content: value,
SortBy: sortBy,
}
if key == 0 {
section.Id = item.Id
}
articleSections = append(articleSections, section)
sortBy++
}
})
//设置内容概要
if len(req.Section) > 0 {
// 截取内容 50个字
runeNumber := 0 //字数
stringIndex := 0 //字符串长度
for i := range req.Section[0].Content {
if runeNumber > 50 {
break
}
runeNumber += 1
stringIndex = i
}
article.Summary = req.Section[0].Content[0:stringIndex]
}
err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, c transaction.Conn) error {
_, err = l.svcCtx.ArticleRepository.Update(l.ctx, c, article)
if err != nil {
return xerr.NewErrMsgErr("保存文章失败", err)
}
//文章内容
updateSectionIds := []int64{}
for _, item := range articleSections {
if item.Id > 0 {
section, err := l.svcCtx.ArticleSectionRepository.FindOne(ctx, c, item.Id)
if err != nil {
return xerr.NewErrMsgErr("获取文章段落内容失败", err)
}
section.Content = item.Content
section.ArticleId = item.ArticleId
section.Version = item.Version
section.SortBy = item.SortBy
section.CompanyId = item.CompanyId
_, err = l.svcCtx.ArticleSectionRepository.Update(ctx, c, section)
if err != nil {
return xerr.NewErrMsgErr("保存文章段落内容失败", err)
}
} else {
_, err = l.svcCtx.ArticleSectionRepository.Insert(ctx, c, &item)
if err != nil {
return xerr.NewErrMsgErr("保存文章段落内容失败", err)
}
}
updateSectionIds = append(updateSectionIds, item.Id)
}
if len(updateSectionIds) > 0 {
err = l.svcCtx.ArticleSectionRepository.DeleteBy(ctx, c, domain.NewQueryOptions().WithKV("articleId", article.Id).WithKV("notIds", updateSectionIds))
if err != nil {
return xerr.NewErrMsgErr("保存文章内容失败", err)
}
}
_, err = l.svcCtx.ArticleBackupRepository.Insert(ctx, c, backup)
if err != nil {
return xerr.NewErrMsgErr("保存文章内容失败", err)
}
return nil
}, true)
resp = &types.SystemArticleUpdateResponse{
Id: article.Id,
Title: article.Title,
Images: req.Images,
CreatedAt: article.CreatedAt,
CountLove: article.CountLove,
CountComment: article.CountComment,
Show: int(article.Show),
TargetUser: int(article.TargetUser),
Tags: article.Tags,
}
return
}