-
Notifications
You must be signed in to change notification settings - Fork 2
/
posts.go
276 lines (236 loc) · 6.71 KB
/
posts.go
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
package dao
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
"gorm.io/gorm"
"novaro-server/model"
"time"
)
type PostsDao struct {
db *gorm.DB
rdb *redis.Client
}
func NewPostsDao(db *gorm.DB) *PostsDao {
return &PostsDao{
db: db,
rdb: model.GetRedisCli(),
}
}
func (d *PostsDao) GetPostsList(p *model.PostsQuery) (resp []model.Posts, err error) {
query := d.db.Table("posts").Preload("User", func(db *gorm.DB) *gorm.DB {
return db.Select("id, user_name, wallet_public_key")
}).Limit(p.Size).Offset((p.Page - 1) * p.Size)
if p.Id != "" {
query = query.Where("id = ?", p.Id)
}
if p.UserId != "" {
query = query.Where("user_id = ?", p.UserId)
}
err = query.Order("created_at desc").Find(&resp).Error
return resp, err
}
func (d *PostsDao) GetPostsById(id string) (resp model.Posts, err error) {
err = d.db.Table("posts").Preload("User").Where("id = ?", id).First(&resp).Error
return resp, err
}
func (d *PostsDao) GetPostIdByUser(postId string) *model.Posts {
var post model.Posts
d.db.Model(model.Posts{}).Preload("User").Where("id = ?", postId).First(&post)
return &post
}
func (d *PostsDao) PostExists(id string) (bool, error) {
var count int64
err := d.db.Model(&model.Posts{}).Where("id = ?", id).Count(&count).Error
return count > 0, err
}
func (d *PostsDao) GetLikeByUser(p *model.PostsQuery) ([]model.Posts, error) {
var posts []model.Posts
err := d.db.Table("posts").Select("posts.*").Joins("RIGHT JOIN likes on posts.id = likes.post_id").Where("likes.user_id = ?", p.UserId).
Limit(p.Size).Offset((p.Page - 1) * p.Size).Scan(&posts).Error
return posts, err
}
func (d *PostsDao) GetCommentByUser(p *model.PostsQuery) ([]model.Posts, error) {
var comments []model.Comments
var posts []model.Posts
err := d.db.Table("comments").Where("user_id = ?", p.UserId).Limit(p.Size).Offset((p.Page - 1) * p.Size).Group("post_id").
Order("created_at desc").Scan(&comments).Error
if err != nil {
return nil, err
}
if len(comments) == 0 {
return []model.Posts{}, nil
}
var postIds []string
commentMap := make(map[string]model.Comments)
for _, comment := range comments {
postIds = append(postIds, comment.PostId)
commentMap[comment.PostId] = comment
}
err = d.db.Table("posts").Where("id in (?)", postIds).Find(&posts).Error
if err != nil {
return nil, err
}
for i := range posts {
if comment, ok := commentMap[posts[i].Id]; ok {
posts[i].Comments = &comment
}
}
userIds := make([]string, 0)
for _, post := range posts {
userIds = append(userIds, post.UserId)
userIds = append(userIds, post.Comments.UserId)
}
userIds = removeDuplicates(userIds)
var users []model.Users
err = d.db.Table("users").
Where("id IN (?)", userIds).
Find(&users).Error
if err != nil {
return nil, err
}
userMap := make(map[string]model.Users)
for _, user := range users {
userMap[user.Id] = user
}
// 6. 关联用户信息
for i := range posts {
if user, ok := userMap[posts[i].UserId]; ok {
posts[i].User = &user
}
if user, ok := userMap[posts[i].Comments.UserId]; ok {
posts[i].Comments.User = &user
}
}
return posts, err
}
func (d *PostsDao) GetCountByUserId(userId string) (count int64, err error) {
// 获取当前日期的开始和结束时间
now := time.Now()
startOfDay := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
endOfDay := startOfDay.Add(24 * time.Hour)
tx := d.db.Model(&model.Posts{}).
Where("user_id = ? AND created_at >= ? AND created_at < ?", userId, startOfDay, endOfDay).
Count(&count)
return count, tx.Error
}
func (d *PostsDao) Save(tx *gorm.DB, posts *model.Posts) error {
var data = model.Posts{
Id: posts.Id,
UserId: posts.UserId,
Content: posts.Content,
ImageBase64: posts.ImageBase64,
OriginalId: posts.OriginalId,
SourceId: posts.SourceId,
}
if tx == nil {
tx = d.db
}
var err error
err = tx.Create(&data).Error
if err == nil && data.OriginalId != "" {
err = d.UpdateRePostCount(data.OriginalId)
}
return err
}
func (d *PostsDao) Update(posts *model.Posts) error {
tx := d.db.Updates(&posts)
return tx.Error
}
func (d *PostsDao) UpdateCount(id string, count int64) error {
err := d.db.Model(&model.Posts{}).Where("id = ?", id).UpdateColumn("comments_amount", count).Error
return err
}
func (d *PostsDao) UpdateRePostCount(id string) error {
err := d.db.Model(&model.Posts{}).Where("id = ?", id).UpdateColumn("reposts_amount", gorm.Expr("reposts_amount + ?", 1)).Error
return err
}
func (d *PostsDao) UpdateLikeAmount(id string, amount int, types string) error {
var err error
if types == "add" {
err = d.db.Model(&model.Posts{}).Where("id = ?", id).UpdateColumn("likes_amount", gorm.Expr("likes_amount + ?", amount)).Error
} else {
err = d.db.Model(&model.Posts{}).Where("id = ?", id).UpdateColumn("likes_amount", gorm.Expr("likes_amount - ?", amount)).Error
}
return err
}
func (d *PostsDao) UpdateBatch(posts []model.Posts) error {
// 开始事务
err := d.db.Transaction(func(tx *gorm.DB) error {
for _, post := range posts {
// 更新每个 post
if err := tx.Model(&post).Updates(model.Posts{
Content: post.Content,
CommentsAmount: post.CommentsAmount,
RepostsAmount: post.RepostsAmount,
//Tags: post.Tags,
}).Error; err != nil {
return err
}
}
return nil
})
return err
}
func (d *PostsDao) Delete(tx *gorm.DB, id string) error {
if tx == nil {
tx = d.db
}
resp, err2 := d.GetPostsById(id)
if err2 == nil && resp.Id != "" {
err2 = tx.Where("id = ?", id).Delete(&model.Posts{}).Error
d.DeleteCache(resp.UserId)
}
return err2
}
func (d *PostsDao) CalculateCommission(userId string) (int64, error) {
ctx := context.Background()
key := fmt.Sprintf("user:%s:posts:count", userId)
count, err := d.rdb.Get(ctx, key).Int()
if err == redis.Nil {
count = 0
}
result, err := d.rdb.Incr(ctx, key).Result()
if err != nil {
return 0, err
}
if count == 0 {
now := time.Now()
tomorrow := time.Date(now.Year(), now.Month(), now.Day()+1, 0, 0, 0, 0, now.Location())
ttl := tomorrow.Sub(now)
d.rdb.Expire(ctx, key, ttl)
}
points := d.calculatePoints(result)
return points, nil
}
// 计算返佣
func (d *PostsDao) calculatePoints(value int64) int64 {
switch value {
case 1:
return 20
case 2:
return 10
default:
return 0
}
}
func (d *PostsDao) DeleteCache(userId string) error {
ctx := context.Background()
key := fmt.Sprintf("user:%s:posts:count", userId)
_, err := d.rdb.Get(ctx, key).Int()
if err != redis.Nil {
d.rdb.Decr(ctx, key)
}
return nil
}
func removeDuplicates(strSlice []string) []string {
keys := make(map[string]bool)
list := []string{}
for _, entry := range strSlice {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
return list
}