/
user_relation_invite.go
326 lines (263 loc) · 9.93 KB
/
user_relation_invite.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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package database
import (
"database/sql"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/jerbe/jim/errors"
"github.com/jerbe/jim/log"
"github.com/jerbe/jim/utils"
"github.com/jerbe/jcache/v2"
"github.com/jmoiron/sqlx"
)
/**
@author : Jerbe - The porter from Earth
@time : 2023/8/21 12:03
@describe :
*/
const (
// UserRelationInviteStatusPending 等待确认用户关系邀请
UserRelationInviteStatusPending = iota
// UserRelationInviteStatusAgree 接收用户关系邀请
UserRelationInviteStatusAgree
// UserRelationInviteStatusReject 拒绝用户关系邀请
UserRelationInviteStatusReject
)
// UserRelationInvite 用户关系邀请
type UserRelationInvite struct {
// ID 请求ID
ID int64 `db:"id" json:"id"`
// UserID 用户ID
UserID int64 `db:"user_id" json:"user_id"`
// TargetID 申请目标的用户ID
TargetID int64 `db:"target_id" json:"target_id"`
// Note 申请备注
Note string `db:"note" json:"note,omitempty"`
// Reply 申请回复
Reply string `db:"reply" json:"reply,omitempty"`
// Status 状态 0:待确认,1:已通过,2:已拒绝
Status int `db:"status" json:"status"`
// UpdatedAt 更新时间
UpdatedAt time.Time `db:"updated_at" json:"updated_at,omitempty"`
// CreatedAt 创建时间
CreatedAt time.Time `db:"created_at" json:"created_at"`
// UQFlag 控制记录是唯一的申请记录标记, 使用userID跟targetID联合
UQFlag string `db:"uq_flag" json:"uq_flag,omitempty"`
}
func (i *UserRelationInvite) MarshalBinary() (data []byte, err error) {
return json.Marshal(i)
}
func (i *UserRelationInvite) UnmarshalBinary(data []byte) error {
return json.Unmarshal(data, i)
}
// AddUserRelationInvite 添加一条用户关系建立的请求记录
func AddUserRelationInvite(invite *UserRelationInvite, opts ...*SetOptions) error {
opt := MergeSetOptions(opts)
if invite.UpdatedAt.IsZero() && !invite.CreatedAt.IsZero() {
invite.UpdatedAt = invite.CreatedAt
}
if !invite.UpdatedAt.IsZero() && invite.CreatedAt.IsZero() {
invite.CreatedAt = invite.UpdatedAt
}
if invite.UpdatedAt.IsZero() && invite.CreatedAt.IsZero() {
now := time.Now()
invite.UpdatedAt, invite.CreatedAt = now, now
}
invite.Status = 0
// 升序排序
a, b := utils.SortInt(invite.UserID, invite.TargetID)
invite.UQFlag = fmt.Sprintf("%d_%d", a, b)
sqlQuery := fmt.Sprintf("INSERT INTO %s (`user_id`,`target_id`,`note`,`reply`,`status`,`updated_at`,`created_at`,`uq_flag`) VALUES (:user_id, :target_id, :note, :reply, :status, :updated_at, :created_at, :uq_flag)", TableUserRelationInvite)
result, err := sqlx.NamedExec(opt.SQLExt(), sqlQuery, invite)
if err != nil {
return errors.Wrap(err)
}
id, err := result.LastInsertId()
if err != nil {
return errors.Wrap(err)
}
invite.ID = id
if opt.UpdateCache() {
GlobCache.Set(GlobCtx, cacheKeyFormatUserRelationInviteID(invite.ID), invite, jcache.DefaultExpirationDuration)
GlobCache.Set(GlobCtx, cacheKeyFormatUserRelationInviteUserIDs(invite.UserID, invite.TargetID), invite, jcache.DefaultExpirationDuration)
}
return nil
}
// GetUserRelationInvite 获取一条建立关系邀请记录
func GetUserRelationInvite(id int64, opts ...*GetOptions) (*UserRelationInvite, error) {
opt := MergeGetOptions(opts)
if opt.UseCache() {
cacheKey := cacheKeyFormatUserRelationInviteID(id)
exits := GlobCache.Exists(GlobCtx, cacheKey).Val()
if exits > 0 {
invite := &UserRelationInvite{}
value := GlobCache.Get(GlobCtx, cacheKey)
if value.Err() == nil && value.Val() != "" {
err := value.Scan(invite)
return invite, err
}
if value.Err() == nil && value.Val() == "" {
return nil, errors.NoRecords
}
}
}
// 通过数据库获取一条记录
sqlQuery := fmt.Sprintf("SELECT `id`, `user_id`,`target_id`,`note`,`reply`,`status`,`updated_at`,`created_at`,`uq_flag` FROM %s WHERE `id` = ?", TableUserRelationInvite)
invite := new(UserRelationInvite)
err := sqlx.Get(opt.SQLExt(), invite, sqlQuery, id)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
// 写入缓存,如果key不存在的话
var cacheKey = cacheKeyFormatUserRelationInviteID(id)
if err := GlobCache.SetNX(GlobCtx, cacheKey, nil, jcache.DefaultEmptySetNXDuration).Err(); err != nil {
log.Error().Err(err).Str("key", cacheKey).Msg("写入缓存失败")
}
}
return nil, errors.Wrap(err)
}
if opt.UpdateCache() {
GlobCache.Set(GlobCtx, cacheKeyFormatUserRelationInviteID(id), invite, jcache.DefaultExpirationDuration)
GlobCache.Set(GlobCtx, cacheKeyFormatUserRelationInviteUserIDs(invite.UserID, invite.TargetID), invite, jcache.DefaultExpirationDuration)
}
return invite, nil
}
// GetLastUserRelationInvite 获取一条建立用户关系请求记录
func GetLastUserRelationInvite(userID, targetID int64, opts ...*GetOptions) (*UserRelationInvite, error) {
opt := MergeGetOptions(opts)
if opt.UseCache() {
cacheKey := cacheKeyFormatUserRelationInviteUserIDs(userID, targetID)
exists := GlobCache.Exists(GlobCtx, cacheKey).Val()
if exists > 0 {
invite := &UserRelationInvite{}
value := GlobCache.Get(GlobCtx, cacheKey)
if value.Err() == nil && value.Val() != "" {
err := value.Scan(invite)
return invite, err
}
if value.Err() == nil && value.Val() == "" {
return nil, errors.NoRecords
}
}
}
sqlQuery := fmt.Sprintf("SELECT `id`, `user_id`,`target_id`,`note`,`reply`,`status`,`updated_at`,`created_at`,`uq_flag` FROM %s WHERE ((`user_id` = ? AND `target_id` = ?) OR (`user_id` = ? AND `target_id` = ?)) AND `status` = 0", TableUserRelationInvite)
invite := new(UserRelationInvite)
err := sqlx.Get(opt.SQLExt(), invite, sqlQuery, userID, targetID, targetID, userID)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
// 写入缓存,如果key不存在的话
var cacheKey = cacheKeyFormatUserRelationInviteUserIDs(userID, targetID)
if err := GlobCache.SetNX(GlobCtx, cacheKey, nil, jcache.DefaultEmptySetNXDuration).Err(); err != nil {
log.Warn().Err(err).Str("key", cacheKey).Str("err_format", fmt.Sprintf("%+v", err)).Msg("写入缓存失败")
}
}
return nil, errors.Wrap(err)
}
if opt.UpdateCache() {
GlobCache.Set(GlobCtx, cacheKeyFormatUserRelationInviteID(invite.ID), invite, jcache.DefaultExpirationDuration)
GlobCache.Set(GlobCtx, cacheKeyFormatUserRelationInviteUserIDs(invite.UserID, invite.TargetID), invite, jcache.DefaultExpirationDuration)
}
return invite, nil
}
// UpdateUserRelationInviteFilter 更新用户关系邀请过滤器
type UpdateUserRelationInviteFilter struct {
// ID 邀请记录ID. (查询条件)
ID int64 `db:"id"`
// UserID 用户ID
UserID int64 `db:"user_id"`
// TargetID 目标ID
TargetID int64 `db:"target_id"`
}
// UpdateUserRelationInviteData 更新用户关系邀请数据
type UpdateUserRelationInviteData struct {
// Status 邀请状态,更新是只能大于0. (设置结果)
Status int `db:"status"`
// Reply 状态回复. (设置结果)
Reply string `db:"reply"`
// UpdatedAt 更新时间,如果未设置,将自动生成当前时间. (设置结果)
UpdatedAt time.Time `db:"updated_at"`
}
// UpdateUserRelationInvite 更新用户关系邀请
func UpdateUserRelationInvite(filter *UpdateUserRelationInviteFilter, data *UpdateUserRelationInviteData, opts ...*SetOptions) (int64, error) {
opt := MergeSetOptions(opts)
// 基本参数判断
// 如果 ID 未设置,则返回
if filter.ID < 0 || filter.UserID < 0 || filter.TargetID < 0 {
return 0, errors.ParamsInvalid
}
// 查询参数
whereSQLs := []string{}
if filter.ID > 0 {
whereSQLs = append(whereSQLs, " `id` = :id ")
}
if filter.UserID > 0 {
whereSQLs = append(whereSQLs, " ((`user_id` = :user_id AND `target_id` = :target_id) OR (`user_id` = :target_id AND `target_id` = :user_id))")
}
whereSQLs = append(whereSQLs, "`status` = 0 ")
// 设置参数
setSQLs := []string{
"`status` = :status ",
}
if data.Reply != "" {
setSQLs = append(setSQLs, "`reply` = :reply ")
}
if data.UpdatedAt.IsZero() {
now := time.Now()
data.UpdatedAt = now
}
setSQLs = append(setSQLs, fmt.Sprintf("`uq_flag` = CONCAT(`uq_flag`, '_%d')", data.UpdatedAt.Unix()), "`updated_at` = :updated_at")
// 邀请记录,提前查出.才可判断是否有必要进行更新
var err error
type named struct {
*UpdateUserRelationInviteFilter
*UpdateUserRelationInviteData
}
// 执行更新
sqlQuery := fmt.Sprintf("UPDATE %s SET %s WHERE %s", TableUserRelationInvite, strings.Join(setSQLs, ","), strings.Join(whereSQLs, " AND "))
rs, err := sqlx.NamedExec(opt.SQLExt(), sqlQuery, named{filter, data})
if err != nil {
return 0, errors.Wrap(err)
}
cnt, err := rs.RowsAffected()
if err != nil {
return 0, errors.Wrap(err)
}
if opt.UpdateCache() {
var got bool
idKey := cacheKeyFormatUserRelationInviteID(filter.ID)
usersKey := cacheKeyFormatUserRelationInviteUserIDs(filter.UserID, filter.TargetID)
if filter.ID > 0 {
got = true
val := GlobCache.Get(GlobCtx, idKey)
err = val.Err()
if err == nil && val.Val() == "" {
err = errors.NoRecords
}
}
if filter.UserID > 0 && (!got || err != nil) {
got = true
val := GlobCache.Get(GlobCtx, usersKey)
err = val.Err()
if err == nil && val.Val() == "" {
err = errors.NoRecords
}
}
if got && err == nil {
GlobCache.Del(GlobCtx, idKey)
GlobCache.Del(GlobCtx, usersKey)
}
}
return cnt, nil
}
// ========================================================================
// ======================= CACHE CONTROL ==================================
// ========================================================================
// cacheKeyFormatUserRelationInviteID 格式化根据ID获取建立用户关系请求的键
func cacheKeyFormatUserRelationInviteID(id int64) string {
return fmt.Sprintf("%s:user_relation_invite:id:%d", CacheKeyPrefix, id)
}
// cacheKeyFormatUserRelationInviteUserIDs 格式化根据用户ID跟目标用户ID获取建立用户关系请求的键
func cacheKeyFormatUserRelationInviteUserIDs(userID, targetID int64) string {
a, b := utils.SortInt(userID, targetID)
return fmt.Sprintf("%s:user_relation_invite:user_id:%d_%d", CacheKeyPrefix, a, b)
}