/
chatgroups.go
556 lines (490 loc) · 13.9 KB
/
chatgroups.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
package domains
import (
"database/sql"
"fmt"
"strings"
"time"
//"github.com/jmoiron/sqlx"
"github.com/go-sql-driver/mysql"
"github.com/google/uuid"
"github.com/hawkwithwind/chat-bot-hub/server/dbx"
"github.com/hawkwithwind/chat-bot-hub/server/utils"
)
type ChatGroup struct {
ChatGroupId string `db:"chatgroupid"`
GroupName string `db:"groupname"`
Type string `db:"type"`
Alias sql.NullString `db:"alias"`
NickName string `db:"nickname"`
Owner string `db:"owner"`
Avatar sql.NullString `db:"avatar"`
MemberCount int `db:"membercount"`
MaxMemberCount int `db:"maxmembercount"`
Ext sql.NullString `db:"ext" search:"-"`
LastMsgId sql.NullString `db:"lastmsgid"`
LastSendAt mysql.NullTime `db:"lastsendat"`
CreateAt mysql.NullTime `db:"createat"`
UpdateAt mysql.NullTime `db:"updateat"`
DeleteAt mysql.NullTime `db:"deleteat"`
LastSyncMembersAt mysql.NullTime `db:"lastsyncmembersat"`
}
type ChatGroupExtend struct {
ChatGroup
OwnerUserName sql.NullString `db:"owner_username"`
OwnerAlias sql.NullString `db:"owner_alias"`
OwnerNickName sql.NullString `db:"owner_nickname"`
OwnerAvatar sql.NullString `db:"owner_avatar"`
OwnerSex sql.NullInt64 `db:"owner_sex"`
OwnerCountry sql.NullString `db:"owner_country"`
OwnerProvince sql.NullString `db:"owner_province"`
OwnerCity sql.NullString `db:"owner_city"`
OwnerSignature sql.NullString `db:"owner_signature"`
OwnerRemark sql.NullString `db:"owner_remark"`
OwnerLabel sql.NullString `db:"owner_label"`
OwnerLastMsgId sql.NullString `db:"owner_lastmsgid"`
OwnerLastSendAt mysql.NullTime `db:"owner_lastsendat"`
}
const (
TN_CHATGROUPS string = "chatgroups"
)
func (o *ErrorHandler) NewDefaultChatGroup() dbx.Searchable {
return &ChatGroup{}
}
func (g *ChatGroup) Fields() []dbx.Field {
return dbx.GetFieldsFromStruct(TN_CHATGROUPS, (*ChatGroup)(nil))
}
func (g *ChatGroup) SelectFrom() string {
return " `chatgroups` LEFT JOIN `chatcontactgroups` " +
" ON `chatgroups`.`chatgroupid` = `chatcontactgroups`.`chatgroupid` " +
" LEFT JOIN `bots` ON `bots`.`botid` = `chatcontactgroups`.`botid` "
}
func (cc *ChatGroup) CriteriaAlias(fieldname string) (dbx.Field, error) {
fn := strings.ToLower(fieldname)
if fn == "botid" {
return dbx.Field{
TN_BOTS, "botid",
}, nil
}
return dbx.NormalCriteriaAlias(cc, fieldname)
}
func (chatgroup *ChatGroup) SetAlias(alias string) {
chatgroup.Alias = sql.NullString{
String: alias,
Valid: true,
}
}
func (chatgroup *ChatGroup) SetAvatar(avatar string) {
chatgroup.Avatar = sql.NullString{
String: avatar,
Valid: true,
}
}
func (chatgroup *ChatGroup) SetExt(ext string) {
chatgroup.Ext = sql.NullString{
String: ext,
Valid: true,
}
}
func (chatgroup *ChatGroup) SetLastSendAt(sendAt time.Time) {
chatgroup.LastSendAt = mysql.NullTime{
Time: sendAt,
Valid: true,
}
}
func (chatgroup *ChatGroup) SetLastMsgId(msgId string) {
chatgroup.LastMsgId = sql.NullString{
String: msgId,
Valid: true,
}
}
func (ctx *ErrorHandler) NewChatGroup(groupname string, ctype string, nickname string, owner string, membercount int, maxmembercount int) *ChatGroup {
if ctx.Err != nil {
return nil
}
var rid uuid.UUID
if rid, ctx.Err = uuid.NewRandom(); ctx.Err != nil {
return nil
} else {
return &ChatGroup{
ChatGroupId: rid.String(),
GroupName: groupname,
Type: ctype,
NickName: nickname,
Owner: owner,
MemberCount: membercount,
MaxMemberCount: maxmembercount,
}
}
}
func (o *ErrorHandler) SaveChatGroup(q dbx.Queryable, chatgroup *ChatGroup) {
if o.Err != nil {
return
}
query := `
INSERT INTO chatgroups
(chatgroupid, groupname, type, alias, nickname, owner, avatar, membercount, maxmembercount,
ext, lastsendat, lastmsgid)
VALUES
(:chatgroupid, :groupname, :type, :alias, :nickname, :owner, :avatar, :membercount, :maxmembercount,
:ext, :lastsendat, :lastmsgid)
`
ctx, _ := o.DefaultContext()
_, o.Err = q.NamedExecContext(ctx, query, chatgroup)
}
func (o *ErrorHandler) UpdateOrCreateChatGroup(q dbx.Queryable, chatgroup *ChatGroup) {
if o.Err != nil {
return
}
query := `
INSERT INTO chatgroups
(chatgroupid, groupname, type, alias, nickname, owner, avatar, membercount, maxmembercount, ext)
VALUES
(:chatgroupid, :groupname, :type, :alias, :nickname, :owner, :avatar, :membercount, :maxmembercount, :ext)
ON DUPLICATE KEY UPDATE
groupname=VALUES(groupname),
alias=VALUES(alias),
nickname=VALUES(nickname),
owner=VALUES(owner),
avatar=VALUES(avatar),
membercount=VALUES(membercount),
maxmembercount=VALUES(maxmembercount),
ext=VALUES(ext)
`
ctx, _ := o.DefaultContext()
_, o.Err = q.NamedExecContext(ctx, query, chatgroup)
}
func (o *ErrorHandler) FindOrCreateChatGroups(q dbx.Queryable, chatgroups []*ChatGroup) []ChatGroup {
if o.Err != nil {
return []ChatGroup{}
}
if len(chatgroups) == 0 {
return []ChatGroup{}
}
o.UpdateOrCreateChatGroups(q, chatgroups)
if o.Err != nil {
return []ChatGroup{}
}
gns := []string{}
for _, cgn := range chatgroups {
gns = append(gns, cgn.GroupName)
}
ret := o.GetChatGroupByNames(q, chatgroups[0].Type, gns)
if o.Err != nil {
return []ChatGroup{}
} else {
return ret
}
}
func (o *ErrorHandler) UpdateChatGroup(q dbx.Queryable, chatgroup *ChatGroup) {
if o.Err != nil {
return
}
query := `
UPDATE chatgroups
SET alias = :alias
, nickname = :nickname
, owner = :owner
, avatar = :avatar
, membercount = :membercount
, maxmembercount = :maxmembercount
, ext = :ext
, lastsendat = :lastsendat
, lastmsgid = :lastmsgid
, lastsyncmembersat = :lastsyncmembersat
WHERE chatgroupid = :chatgroupid
`
ctx, _ := o.DefaultContext()
_, o.Err = q.NamedExecContext(ctx, query, chatgroup)
}
func (o *ErrorHandler) UpdateOrCreateChatGroups(q dbx.Queryable, chatgroups []*ChatGroup) {
if o.Err != nil {
return
}
query := `
INSERT INTO chatgroups
(chatgroupid, groupname, type, nickname, owner, membercount, maxmembercount, alias, avatar, ext)
VALUES
%s
ON DUPLICATE KEY UPDATE
`
vls := []string{}
for _, field := range []string{
"groupname",
"alias",
"nickname",
"owner",
"avatar",
"ext",
} {
vls = append(vls, fmt.Sprintf("%s=IF(CHAR_LENGTH(VALUES(%s)) > 0, VALUES(%s), %s)",
field, field, field, field))
}
for _, field := range []string{
"membercount",
"maxmembercount",
} {
vls = append(vls, fmt.Sprintf("%s=IF(VALUES(%s) > 0, VALUES(%s), %s)",
field, field, field, field))
}
query += strings.Join(vls, ",\n")
var valueStrings []string
var valueArgs []interface{}
for _, chatgroup := range chatgroups {
valueStrings = append(valueStrings, "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
valueArgs = append(valueArgs,
chatgroup.ChatGroupId,
chatgroup.GroupName,
chatgroup.Type,
chatgroup.NickName,
chatgroup.Owner,
chatgroup.MemberCount,
chatgroup.MaxMemberCount,
)
if chatgroup.Alias.Valid {
valueArgs = append(valueArgs, chatgroup.Alias.String)
} else {
valueArgs = append(valueArgs, nil)
}
if chatgroup.Avatar.Valid {
valueArgs = append(valueArgs, chatgroup.Avatar.String)
} else {
valueArgs = append(valueArgs, nil)
}
if chatgroup.Ext.Valid {
valueArgs = append(valueArgs, chatgroup.Ext.String)
} else {
valueArgs = append(valueArgs, nil)
}
}
ctx, _ := o.DefaultContext()
_, o.Err = q.ExecContext(ctx, fmt.Sprintf(query, strings.Join(valueStrings, ",\n")), valueArgs...)
}
func (o *ErrorHandler) GetChatGroupById(q dbx.Queryable, cgid string) *ChatGroup {
if o.Err != nil {
return nil
}
chatgroups := []ChatGroup{}
ctx, _ := o.DefaultContext()
o.Err = q.SelectContext(ctx, &chatgroups, "SELECT * FROM chatgroups WHERE chatgroupid=? AND deleteat is NULL", cgid)
if chatgroup := o.Head(chatgroups, fmt.Sprintf("chatgroup %s more than one instance", cgid)); chatgroup != nil {
return chatgroup.(*ChatGroup)
} else {
return nil
}
}
func (o *ErrorHandler) GetChatGroupByName(q dbx.Queryable, ctype string, groupname string) *ChatGroup {
if o.Err != nil {
return nil
}
chatgroups := []ChatGroup{}
ctx, _ := o.DefaultContext()
o.Err = q.SelectContext(ctx, &chatgroups,
`SELECT * FROM chatgroups
WHERE groupname=?
AND type=?
AND deleteat is NULL`, groupname, ctype)
if chatgroup := o.Head(chatgroups, fmt.Sprintf("chatgroup %s more than one instance", groupname)); chatgroup != nil {
return chatgroup.(*ChatGroup)
} else {
return nil
}
}
func (o *ErrorHandler) GetChatGroupByNames(q dbx.Queryable, ctype string, groupnames []string) []ChatGroup {
if o.Err != nil {
return []ChatGroup{}
}
if len(groupnames) == 0 {
return []ChatGroup{}
}
const query string = `
SELECT * FROM chatgroups
WHERE type = "%s"
AND groupname IN ("%s")
AND deleteat is NULL
`
chatgroups := []ChatGroup{}
ctx, _ := o.DefaultContext()
o.Err = q.SelectContext(ctx, &chatgroups, fmt.Sprintf(query, ctype, strings.Join(groupnames, `", "`)))
return chatgroups
}
type ChatGroupCriteria struct {
GroupName sql.NullString
NickName sql.NullString
Type sql.NullString
BotId sql.NullString
}
func (o *ErrorHandler) GetChatGroups(q dbx.Queryable, accountId string, criteria ChatGroupCriteria, paging utils.Paging) []ChatGroupExtend {
if o.Err != nil {
return []ChatGroupExtend{}
}
const query string = `
SELECT
g.chatgroupid
, g.groupname
, g.type
, g.alias
, g.nickname
, g.owner
, g.avatar
, g.membercount
, g.maxmembercount
, g.ext
, g.lastsendat
, g.lastmsgid
, g.createat
, g.updateat
, g.deleteat
, u.username as owner_username
, u.alias as owner_alias
, u.nickname as owner_nickname
, u.avatar as owner_avatar
, u.sex as owner_sex
, u.country as owner_country
, u.province as owner_province
, u.city as owner_city
, u.signature as owner_signature
, u.remark as owner_remark
, u.label as owner_label
, u.lastmsgid as owner_lastmsgid
, u.lastsendat as owner_lastsendat
FROM chatgroups as g
LEFT JOIN chatcontactgroups as c ON g.chatgroupid = c.chatgroupid
LEFT JOIN bots as b ON c.botid = b.botid
LEFT JOIN chatusers as u ON g.owner = u.chatuserid
WHERE g.deleteat is NULL
AND b.accountid = ?
%s /* groupname */
%s /* nickname */
%s /* type */
GROUP BY g.chatgroupid
ORDER BY createat desc
LIMIT ?, ?
`
chatgroups := []ChatGroupExtend{}
ctx, _ := o.DefaultContext()
o.Err = q.SelectContext(ctx, &chatgroups,
fmt.Sprintf(query,
o.AndEqualString("groupname", criteria.GroupName),
o.AndLikeStringT("g", "nickname", criteria.NickName),
o.AndEqualStringT("g", "type", criteria.Type)),
accountId,
criteria.GroupName.String,
fmt.Sprintf("%%%s%%", criteria.NickName.String),
criteria.Type.String,
(paging.Page-1)*paging.PageSize,
paging.PageSize)
if o.Err != nil {
return []ChatGroupExtend{}
} else {
return chatgroups
}
}
func (o *ErrorHandler) GetChatGroupCount(q dbx.Queryable, criteria ChatGroupCriteria) int64 {
if o.Err != nil {
return 0
}
const query string = `
SELECT COUNT(*) from chatgroups
WHERE deleteat is NULL
%s /* groupname */
%s /* nickname */
%s /* type */
`
var count []int64
ctx, _ := o.DefaultContext()
o.Err = q.SelectContext(ctx, &count,
fmt.Sprintf(query,
o.AndEqualString("groupname", criteria.GroupName),
o.AndLikeString("nickname", criteria.NickName),
o.AndEqualString("type", criteria.Type)),
criteria.GroupName.String,
fmt.Sprintf("%%%s%%", criteria.NickName.String),
criteria.Type.String)
return count[0]
}
func (o *ErrorHandler) GetChatGroupsWithBotId(q dbx.Queryable, criteria ChatGroupCriteria, paging utils.Paging) []ChatGroupExtend {
if o.Err != nil {
return []ChatGroupExtend{}
}
if criteria.BotId.Valid == false {
o.Err = fmt.Errorf("GetChatGroupsWithBotId must set param botId")
return []ChatGroupExtend{}
}
const query string = `
SELECT g.*,
u.username as owner_username,
u.alias as owner_alias,
u.nickname as owner_nickname,
u.avatar as owner_avatar,
u.sex as owner_sex,
u.country as owner_country,
u.province as owner_province,
u.city as owner_city,
u.signature as owner_signature,
u.remark as owner_remark,
u.label as owner_label,
u.lastmsgid as owner_lastmsgid,
u.lastsendat as owner_lastsendat
FROM chatgroups as g
LEFT JOIN chatcontactgroups as c ON g.chatgroupid = c.chatgroupid
LEFT JOIN chatusers as u ON g.owner = u.chatuserid
WHERE g.deleteat is NULL
AND c.deleteat is NULL
AND c.botid = ?
%s /* groupname */
%s /* nickname */
%s /* type */
ORDER BY g.createat desc
LIMIT ?, ?
`
chatgroups := []ChatGroupExtend{}
ctx, _ := o.DefaultContext()
o.Err = q.SelectContext(ctx, &chatgroups,
fmt.Sprintf(query,
o.AndEqualString("groupname", criteria.GroupName),
o.AndLikeStringT("g", "nickname", criteria.NickName),
o.AndEqualStringT("g", "type", criteria.Type)),
criteria.BotId.String,
criteria.GroupName.String,
fmt.Sprintf("%%%s%%", criteria.NickName.String),
criteria.Type.String,
(paging.Page-1)*paging.PageSize,
paging.PageSize)
if o.Err != nil {
return []ChatGroupExtend{}
} else {
return chatgroups
}
}
func (o *ErrorHandler) GetChatGroupCountWithBotId(q dbx.Queryable, criteria ChatGroupCriteria) int64 {
if o.Err != nil {
return 0
}
if criteria.BotId.Valid == false {
o.Err = fmt.Errorf("GetChatGroupsWithBotId must set param botId")
return 0
}
const query string = `
SELECT COUNT(*)
FROM chatgroups as g
LEFT JOIN chatcontactgroups as c ON g.chatgroupid = c.chatgroupid
WHERE g.deleteat is NULL
AND c.deleteat is NULL
AND c.botid = ?
%s /* groupname */
%s /* nickname */
%s /* type */
`
var count []int64
ctx, _ := o.DefaultContext()
o.Err = q.SelectContext(ctx, &count,
fmt.Sprintf(query,
o.AndEqualString("groupname", criteria.GroupName),
o.AndLikeString("nickname", criteria.NickName),
o.AndEqualString("type", criteria.Type)),
criteria.BotId.String,
criteria.GroupName.String,
fmt.Sprintf("%%%s%%", criteria.NickName.String),
criteria.Type.String)
return count[0]
}