article_repository.go
8.8 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
package repository
import (
"context"
"github.com/pkg/errors"
"github.com/tiptok/gocomm/pkg/cache"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/models"
"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"
"gorm.io/gorm"
)
type ArticleRepository struct {
*cache.CachedRepository
}
func (repository *ArticleRepository) Insert(ctx context.Context, conn transaction.Conn, dm *domain.Article) (*domain.Article, error) {
var (
err error
m = &models.Article{}
tx = conn.DB()
)
if m, err = repository.DomainModelToModel(dm); err != nil {
return nil, err
}
if tx = tx.Model(m).Save(m); tx.Error != nil {
return nil, tx.Error
}
dm.Id = m.Id
return repository.ModelToDomainModel(m)
}
func (repository *ArticleRepository) Update(ctx context.Context, conn transaction.Conn, dm *domain.Article) (*domain.Article, error) {
var (
err error
m *models.Article
tx = conn.DB()
)
if m, err = repository.DomainModelToModel(dm); err != nil {
return nil, err
}
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Updates(m)
return nil, tx.Error
}
if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *ArticleRepository) UpdateWithVersion(ctx context.Context, transaction transaction.Conn, dm *domain.Article) (*domain.Article, error) {
var (
err error
m *models.Article
tx = transaction.DB()
)
if m, err = repository.DomainModelToModel(dm); err != nil {
return nil, err
}
oldVersion := dm.Version
m.Version += 1
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Select("*").Where("id = ?", m.Id).Where("version = ?", oldVersion).Updates(m)
if tx.RowsAffected == 0 {
return nil, domain.ErrUpdateFail
}
return nil, tx.Error
}
if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *ArticleRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.Article) (*domain.Article, error) {
var (
tx = conn.DB()
m = &models.Article{Id: dm.Id}
)
queryFunc := func() (interface{}, error) {
tx = tx.Where("id = ?", m.Id).Delete(m)
return m, tx.Error
}
if _, err := repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
return dm, err
}
return repository.ModelToDomainModel(m)
}
func (repository *ArticleRepository) FindOne(ctx context.Context, conn transaction.Conn, id int64) (*domain.Article, error) {
var (
err error
tx = conn.DB()
m = new(models.Article)
)
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Where("id = ?", id).First(m)
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
return nil, domain.ErrNotFound
}
return m, tx.Error
}
cacheModel := new(models.Article)
cacheModel.Id = id
if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *ArticleRepository) Find(ctx context.Context, conn transaction.Conn, companyId int64, queryOptions map[string]interface{}) (int64, []*domain.Article, error) {
var (
tx = conn.DB()
ms []*models.Article
dms = make([]*domain.Article, 0)
total int64
)
queryFunc := func() (interface{}, error) {
tx = tx.Model(&ms).Order("id desc").Where("company_id=?", companyId)
if v, ok := queryOptions["ids"]; ok {
tx = tx.Where("id in (?)", v)
}
if v, ok := queryOptions["title"]; ok && v.(string) != "" {
tx = tx.Where("title like ?", "%"+v.(string)+"%")
}
if v, ok := queryOptions["author"]; ok && v.(string) != "" {
tx = tx.Where(`author #>> '{"name"}' like ?`, "%"+v.(string)+"%")
}
if v, ok := queryOptions["beginCreatedAt"]; ok {
tx = tx.Where("created_at >= ?", v)
}
if v, ok := queryOptions["endCreatedAt"]; ok {
tx = tx.Where("created_at < ?", v)
}
if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil {
return dms, tx.Error
}
return dms, nil
}
if _, err := repository.Query(queryFunc); err != nil {
return 0, nil, err
}
for _, item := range ms {
if dm, err := repository.ModelToDomainModel(item); err != nil {
return 0, dms, err
} else {
dms = append(dms, dm)
}
}
return total, dms, nil
}
func (repository *ArticleRepository) ModelToDomainModel(from *models.Article) (*domain.Article, error) {
to := &domain.Article{
Id: from.Id,
CompanyId: from.CompanyId,
CreatedAt: from.CreatedAt,
UpdatedAt: from.UpdatedAt,
DeletedAt: from.DeletedAt,
Version: from.Version,
AuthorId: from.AuthorId,
Author: from.Author,
Title: from.Title,
Images: from.Images,
WhoRead: from.WhoRead,
WhoReview: from.WhoReview,
Location: from.Location,
TargetUser: domain.ArticleTarget(from.TargetUser),
CountLove: from.CountLove,
CountComment: from.CountComment,
CountRead: from.CountRead,
Show: domain.ArticleShow(from.Show),
Tags: from.Tags,
Summary: from.Summary,
}
return to, nil
}
func (repository *ArticleRepository) DomainModelToModel(from *domain.Article) (*models.Article, error) {
to := &models.Article{
Id: from.Id,
CompanyId: from.CompanyId,
CreatedAt: from.CreatedAt,
UpdatedAt: from.UpdatedAt,
DeletedAt: from.DeletedAt,
IsDel: 0,
Version: from.Version,
AuthorId: from.AuthorId,
Author: from.Author,
Title: from.Title,
Images: from.Images,
WhoRead: from.WhoRead,
WhoReview: from.WhoReview,
Location: from.Location,
TargetUser: int(from.TargetUser),
CountLove: from.CountLove,
CountRead: from.CountRead,
CountComment: from.CountComment,
Show: int(from.Show),
Tags: from.Tags,
Summary: from.Summary,
}
// err := copier.Copy(to, from)
return to, nil
}
// 点赞数量变动
func (repository *ArticleRepository) IncreaseCountLove(ctx context.Context, conn transaction.Conn, incr int, articleId int64) error {
//
var (
err error
m *models.Article
tx = conn.DB()
)
m = &models.Article{Id: articleId}
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Updates(map[string]interface{}{
"count_love": gorm.Expr("count_love+?", incr),
"version": gorm.Expr("version+1"),
})
return nil, tx.Error
}
if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
return err
}
return nil
}
// 浏览数量变动
func (repository *ArticleRepository) IncreaseCountRead(ctx context.Context, conn transaction.Conn, incr int, articleId int64) error {
var (
err error
m *models.Article
tx = conn.DB()
)
m = &models.Article{Id: articleId}
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Updates(map[string]interface{}{
"version": gorm.Expr("version+1"),
"count_read": gorm.Expr("count_read+?", incr),
})
return nil, tx.Error
}
if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
return err
}
return nil
}
// 评论数量变动
func (repository *ArticleRepository) IncreaseCountComment(ctx context.Context, conn transaction.Conn, incr int, articleId int64) error {
var (
err error
m *models.Article
tx = conn.DB()
)
m = &models.Article{Id: articleId}
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Updates(map[string]interface{}{
"version": gorm.Expr("version+1"),
"count_comment": gorm.Expr("count_comment+?", incr),
})
return nil, tx.Error
}
if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
return err
}
return nil
}
func NewArticleRepository(cache *cache.CachedRepository) domain.ArticleRepository {
return &ArticleRepository{CachedRepository: cache}
}
// -- 首页统计数据
// with
// -- 按查看权限查询文章
// -- 获取有标签的文章
// -- 过滤出可展示的文章id
// t_article_and_tag_2 as (
// select article_and_tag.article_id , article_and_tag.tag_id
// from article_and_tag
// join article on article_and_tag.article_id = article.id
// where article.deleted_at=0
// and article.company_id =1598224576532189184
// and article."show" =0
// and (article.target_user =0 or article.who_read @>'[1]')
// ),
// -- 查询人员已查看的文章
// t_user_read as(
// select user_read_article.article_id from user_read_article where user_read_article.user_id =1
// )
// -- 汇总统计 cnt_1符合条件的文章总数,cnt_2 已浏览的数量
// select count(t_article_and_tag_2.article_id) as cnt_1 ,count(t_user_read.article_id) as cnt_2, t_article_and_tag_2.tag_id
// from t_article_and_tag_2
// left join t_user_read on t_article_and_tag_2.article_id=t_user_read.article_id
// group by t_article_and_tag_2.tag_id
// ;