/
db_friend.go
273 lines (242 loc) · 10.1 KB
/
db_friend.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
package user
import (
"fmt"
"github.com/TangSengDaoDao/TangSengDaoDaoServerLib/config"
"github.com/TangSengDaoDao/TangSengDaoDaoServerLib/pkg/db"
"github.com/TangSengDaoDao/TangSengDaoDaoServerLib/pkg/util"
"github.com/gocraft/dbr/v2"
)
// DB DB
type friendDB struct {
session *dbr.Session
ctx *config.Context
}
// NewDB NewDB
func newFriendDB(ctx *config.Context) *friendDB {
return &friendDB{
session: ctx.DB(),
ctx: ctx,
}
}
// InsertTx 插入好友信息
func (d *friendDB) InsertTx(m *FriendModel, tx *dbr.Tx) error {
_, err := tx.InsertInto("friend").Columns(util.AttrToUnderscore(m)...).Record(m).Exec()
if err != nil {
return err
}
friendKey := fmt.Sprintf("%s%s", CacheKeyFriends, m.UID)
err = d.ctx.GetRedisConn().SAdd(friendKey, m.ToUID)
return err
}
// Insert 插入好友信息
func (d *friendDB) Insert(m *FriendModel) error {
_, err := d.session.InsertInto("friend").Columns(util.AttrToUnderscore(m)...).Record(m).Exec()
if err != nil {
return err
}
friendKey := fmt.Sprintf("%s%s", CacheKeyFriends, m.UID)
err = d.ctx.GetRedisConn().SAdd(friendKey, m.ToUID)
return err
}
// IsFriend 是否是好友
func (d *friendDB) IsFriend(uid, toUID string) (bool, error) {
var m *FriendModel
_, err := d.session.Select("*").From("friend").Where("uid=? and to_uid=?", uid, toUID).Load(&m)
if err != nil {
return false, err
}
var isFriend = false
if m != nil && m.IsDeleted == 0 {
isFriend = true
}
return isFriend, nil
}
// 修改好友关系
func (d *friendDB) updateRelationshipTx(uid, toUID string, isDeleted, isAlone int, sourceVercode string, version int64, tx *dbr.Tx) error {
_, err := tx.Update("friend").SetMap(map[string]interface{}{
"is_deleted": isDeleted,
"is_alone": isAlone,
"source_vercode": sourceVercode,
"version": version,
}).Where("uid=? and to_uid=?", uid, toUID).Exec()
if err != nil {
return err
}
friendKey := fmt.Sprintf("%s%s", CacheKeyFriends, uid)
if isDeleted == 1 {
err = d.ctx.GetRedisConn().SRem(friendKey, toUID)
} else {
err = d.ctx.GetRedisConn().SAdd(friendKey, toUID)
}
return err
}
func (d *friendDB) updateRelationship2Tx(uid, toUID string, isDeleted, isAlone int, version int64, tx *dbr.Tx) error {
_, err := tx.Update("friend").SetMap(map[string]interface{}{
"is_deleted": isDeleted,
"is_alone": isAlone,
"version": version,
}).Where("uid=? and to_uid=?", uid, toUID).Exec()
if err != nil {
return err
}
friendKey := fmt.Sprintf("%s%s", CacheKeyFriends, uid)
if isDeleted == 1 {
err = d.ctx.GetRedisConn().SRem(friendKey, toUID)
} else {
err = d.ctx.GetRedisConn().SAdd(friendKey, toUID)
}
return err
}
// 修改好友单项关系
func (d *friendDB) updateAloneTx(uid, toUID string, isAlone int, tx *dbr.Tx) error {
_, err := tx.Update("friend").Set("is_alone", isAlone).Where("uid=? and to_uid=?", uid, toUID).Exec()
return err
}
// 删除好友
// func (d *friendDB) delete(uid, toUID string) error {
// _, err := d.session.DeleteFrom("friend").Where("uid=? and to_uid=?", uid, toUID).Exec()
// if err != nil {
// return err
// }
// friendKey := fmt.Sprintf("%s%s", CacheKeyFriends, uid)
// err = d.ctx.GetRedisConn().SRem(friendKey, toUID)
// return err
// }
// 删除好友
// func (d *friendDB) deleteTx(uid, toUID string, tx *dbr.Tx) error {
// _, err := tx.Update("friend").SetMap(map[string]interface{}{
// "is_deleted": 1,
// "is_alone": 1,
// }).Where("uid=? and to_uid=?", uid, toUID).Exec()
// //_, err := tx.DeleteFrom("friend").Where("uid=? and to_uid=?", uid, toUID).Exec()
// if err != nil {
// return err
// }
// friendKey := fmt.Sprintf("%s%s", CacheKeyFriends, uid)
// err = d.ctx.GetRedisConn().SRem(friendKey, toUID)
// return err
// }
// 通过vercode查询好友信息
func (d *friendDB) queryWithVercode(vercode string) (*FriendModel, error) {
var friend *FriendModel
_, err := d.session.Select("*").From("friend").Where("vercode=?", vercode).Load(&friend)
return friend, err
}
// 通过vercode查询好友信息
func (d *friendDB) queryWithVercodes(vercodes []string) ([]*FriendDetailModel, error) {
var friends []*FriendDetailModel
_, err := d.session.Select("friend.*,IFNULL(user.name,'') name").From("friend").LeftJoin("user", "friend.uid=user.uid").Where("friend.vercode in ?", vercodes).Load(&friends)
return friends, err
}
// 查询某个好友
func (d *friendDB) queryWithUID(uid, toUID string) (*FriendModel, error) {
var friend *FriendModel
_, err := d.session.Select("*").From("friend").Where("uid=? and to_uid=?", uid, toUID).Load(&friend)
return friend, err
}
// 查询双方好友
func (d *friendDB) queryTwoWithUID(uid, toUID string) ([]*FriendModel, error) {
var friends []*FriendModel
_, err := d.session.Select("*").From("friend").Where("(uid=? and to_uid=?) or (uid=? and to_uid=?)", uid, toUID, toUID, uid).Load(&friends)
return friends, err
}
// 查询指定用户uid的在toUids范围内的好友
func (d *friendDB) queryWithToUIDsAndUID(toUids []string, uid string) ([]*FriendModel, error) {
var friends []*FriendModel
_, err := d.session.Select("*").From("friend").Where("uid=? and to_uid in ?", uid, toUids).Load(&friends)
return friends, err
}
// 查询uids范围内的用户与toUID是好友的数据
func (d *friendDB) queryWithToUIDAndUIDs(toUID string, uids []string) ([]*FriendModel, error) {
var friends []*FriendModel
_, err := d.session.Select("*").From("friend").Where("to_uid=? and uid in ?", toUID, uids).Load(&friends)
return friends, err
}
// QueryFriendsWithKeyword 通过关键字查询自己的好友
func (d *friendDB) QueryFriendsWithKeyword(uid string, keyword string) ([]*DetailModel, error) {
var details []*DetailModel
builder := d.session.Select("friend.id,friend.to_uid,IFNULL(user.name,'') to_name,friend.is_deleted,friend.created_at,friend.updated_at,IFNULL(user_setting.mute,0) mute,IFNULL(user_setting.top,0) top,IFNULL(user_setting.version,0)+friend.version version").From("friend").LeftJoin("user", "friend.to_uid=user.uid").LeftJoin("user_setting", "user.uid=user_setting.to_uid and user_setting.uid=friend.uid").Where("friend.uid=?", uid).OrderDir("friend.version + IFNULL(user_setting.version,0)", true)
if keyword != "" {
builder = builder.Where("user.name like ?", "%"+keyword+"%")
}
_, err := builder.Load(&details)
return details, err
}
// SyncFriendsOfDeprecated 同步好友
// Deprecated 已废弃,用SyncFriends方法。
func (d *friendDB) SyncFriendsOfDeprecated(version int64, uid string, limit uint64) ([]*DetailModel, error) {
var details []*DetailModel
builder := d.session.Select("friend.id,IFNULL(friend.vercode,'') vercode,friend.to_uid,IFNULL(user.name,'') to_name,IFNULL(user.category,'') to_category,IFNULL(user.robot,0) robot,IFNULL(user.short_no,'') short_no,IFNULL(friend.remark,'') remark,friend.is_deleted,friend.created_at,friend.updated_at,IFNULL(user_setting.mute,0) mute,IFNULL(user_setting.chat_pwd_on,0) chat_pwd_on,IFNULL(user_setting.blacklist,0) blacklist,IFNULL(user_setting.top,0) top,IFNULL(user_setting.receipt,0) receipt,friend.version + IFNULL(user_setting.version,0) version").From("friend").LeftJoin("user", "friend.to_uid=user.uid").LeftJoin("user_setting", "user.uid=user_setting.to_uid and user_setting.uid=friend.uid").Where("friend.uid=?", uid).OrderDir("friend.version + IFNULL(user_setting.version,0)", true)
var err error
if version <= 0 {
_, err = builder.Limit(limit).Load(&details)
} else {
_, err = builder.Where("IFNULL(user_setting.version,0) + friend.version > ?", version).Limit(limit).Load(&details)
}
return details, err
}
func (d *friendDB) SyncFriends(version int64, uid string, limit uint64) ([]*FriendModel, error) {
var models []*FriendModel
builder := d.session.Select("*").From("friend").Where("friend.uid=?", uid).OrderDir("friend.version", true)
_, err := builder.Where("friend.version > ?", version).Limit(limit).Load(&models)
return models, err
}
// QueryFriends 查询用户的所有好友
func (d *friendDB) QueryFriends(uid string) ([]*DetailModel, error) {
var details []*DetailModel
_, err := d.session.Select("friend.*,IFNULL(user.name,'') to_name").From("friend").LeftJoin("user", "user.uid=friend.to_uid").Where("friend.uid=? and friend.is_deleted=0", uid).Load(&details)
return details, err
}
// QueryFriendsWithUIDs 通过用户id查询好友
func (d *friendDB) QueryFriendsWithUIDs(uid string, toUIDs []string) ([]*FriendDetailModel, error) {
var friends []*FriendDetailModel
_, err := d.session.Select("friend.*,IFNULL(user.name,'') to_name").From("friend").LeftJoin("user", "user.uid=friend.to_uid").Where("friend.uid=? and friend.is_deleted=0 and friend.to_uid in ?", uid, toUIDs).Load(&friends)
return friends, err
}
func (d *friendDB) updateVersionTx(version int64, uid string, toUID string, tx *dbr.Tx) error {
_, err := tx.Update("friend").Set("version", version).Where("uid=? and to_uid=?", uid, toUID).Exec()
return err
}
func (d *friendDB) existBlacklist(uid string, toUID string) (bool, error) {
var cn int
_, err := d.session.Select("count(*)").From("user_setting").Where("((uid=? and to_uid=?) or (uid=? and to_uid=?)) and blacklist=1", uid, toUID, toUID, uid).Load(&cn)
return cn > 0, err
}
// DetailModel 好友详情
type DetailModel struct {
Remark string //好友备注
ToUID string // 好友uid
ToName string // 好友名字
ToCategory string // 用户分类
Mute int // 免打扰
Top int // 置顶
Version int64 // 版本
Vercode string // 验证码 加好友需要
IsDeleted int // 是否删除
IsAlone int // 是否为单项好友
ShortNo string //短编号
ChatPwdOn int // 是否开启聊天密码
Blacklist int //是否在黑名单
Receipt int //消息是否回执
Robot int // 机器人0.否1.是
db.BaseModel
}
// FriendModel 好友对象
type FriendModel struct {
UID string
ToUID string
Flag int
Version int64
IsDeleted int
IsAlone int // 是否为单项好友
Vercode string
SourceVercode string //来源验证码
Initiator int //1:发起方
db.BaseModel
}
// FriendDetailModel 好友资料
type FriendDetailModel struct {
FriendModel
Name string // 用户名称
ToName string //对方用户名称
}