-
Notifications
You must be signed in to change notification settings - Fork 0
/
feedback.go
646 lines (632 loc) · 17.1 KB
/
feedback.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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
package service
import (
"context"
"net"
"sort"
"strconv"
"strings"
"time"
"unicode/utf8"
"go-common/app/interface/main/feedback/model"
locmdl "go-common/app/service/main/location/model"
"go-common/library/database/sql"
"go-common/library/ecode"
"go-common/library/log"
"go-common/library/net/metadata"
xtime "go-common/library/time"
"go-common/library/xstr"
)
const (
_androidFeedback = "android-feedback"
_androidFeedbackI = "android-fb-i"
_iosFeedback = "ios-feedback"
_iosFeedbackI = "ios-fb-i"
_androidPlayer = "android-player"
_iosPlayer = "ios-player"
_androidCreative = "android-creative"
_iosCreative = "ios-creative"
_androidLivePink = "android-live-pink"
_iosLivePink = "ios-live-pink"
_tvYST = "tv-yst"
_creativeCenter = "creative-center"
)
var (
defaultTag = &model.Tag{
ID: 314,
Name: "其它问题",
Type: 0,
}
)
// AddReply add feedback raply and create session if session isn't exist
func (s *Service) AddReply(c context.Context, mid, tagID int64, buvid, system, version, mobiApp, content, imgURL, logURL, device, channel, entrance,
netState, netOperator, agencyArea, platform, browser, qq, email string, now time.Time) (r *model.Reply, err error) {
// check size
if utf8.RuneCountInString(content) > s.c.Feedback.MaxContentSize {
err = ecode.FeedbackBodyTooLarge
return
}
var (
ssn *model.Session
reply *model.Reply
cTime xtime.Time
replyID = buvid
id int64
imgURLs []string
ip = metadata.String(c, metadata.RemoteIP)
)
if entrance == "view" {
if model.IsAndroid(model.Plat(mobiApp, device)) {
platform = _androidPlayer
} else if model.IsIOS(model.Plat(mobiApp, device)) {
platform = _iosPlayer
}
} else if entrance == _tvYST || entrance == _creativeCenter {
platform = entrance
} else if platform == "android" {
if entrance == _androidCreative {
platform = _androidCreative
} else if entrance == _androidLivePink {
platform = _androidLivePink
} else if model.Plat(mobiApp, device) == model.PlatAndroid {
platform = _androidFeedback
} else if model.Plat(mobiApp, device) == model.PlatAndroidI {
platform = _androidFeedbackI
}
} else if platform == "ios" {
if entrance == _iosCreative {
platform = _iosCreative
} else if entrance == _iosLivePink {
platform = _iosLivePink
} else if model.Plat(mobiApp, device) == model.PlatIPhone {
platform = _iosFeedback
} else if model.Plat(mobiApp, device) == model.PlatIPhoneI {
platform = _iosFeedbackI
}
}
if ssn, err = s.dao.Session(c, buvid, system, version, mid); err != nil {
log.Error("s.dao.Session(%s,%s,%s,%d) error(%v)", buvid, system, version, mid, err)
return
}
if mid != 0 {
replyID = strconv.FormatInt(mid, 10)
}
cTime = xtime.Time(now.Unix())
if ssn == nil {
if content != "" || logURL != "" || imgURL != "" {
// TODO delete ;
imgURLs = strings.Split(imgURL, ";")
var sid int64
sid, err = s.session(c, mid, tagID, buvid, system, version, "", content, imgURLs[0], logURL, device, channel, ip, entrance, netState, netOperator, agencyArea, platform, browser, qq, email, now)
if err == nil {
for _, v := range imgURLs[1:] {
if v != "" {
reply = &model.Reply{
SessionID: sid,
ReplyID: replyID,
Content: "",
ImgURL: v,
LogURL: "",
CTime: cTime,
MTime: cTime,
}
if id, err = s.dao.AddReply(c, reply); err != nil {
log.Error("s.dao.AddReply error(%v)", err)
continue
}
}
}
}
}
} else {
if entrance == "view" || entrance == _androidLivePink || entrance == _iosLivePink || platform == _androidLivePink || platform == _iosLivePink || model.IsPlayerScreen(tagID) || entrance == _tvYST || platform == _tvYST || entrance == _creativeCenter || platform == _creativeCenter {
_, err = s.session(c, mid, tagID, buvid, system, version, "", content, imgURL, logURL, device, channel, ip, entrance, netState, netOperator, agencyArea, platform, browser, qq, email, now)
if err != nil {
log.Error("s.session error (%v)", err)
return
}
} else {
stat := ssn.State
if ssn.State == model.StateReplied {
stat = model.StateRepeated
}
var ip32 uint32
ipv := net.ParseIP(ip)
if ip2 := ipv.To4(); ip2 != nil {
ip32 = model.InetAtoN(ip)
}
ssn = &model.Session{
ID: ssn.ID,
Device: device,
Channel: channel,
IP: ip32,
NetState: netState,
NetOperator: netOperator,
AgencyArea: agencyArea,
Platform: platform,
Browser: browser,
QQ: qq,
Email: email,
State: stat,
LasterTime: cTime,
MTime: cTime,
}
if _, err = s.dao.UpdateSession(c, ssn); err != nil {
log.Error("s.dao.UpdateSession error(%v)", err)
return
}
if content != "" || logURL != "" {
reply = &model.Reply{
SessionID: ssn.ID,
ReplyID: replyID,
Content: content,
ImgURL: "",
LogURL: logURL,
CTime: cTime,
MTime: cTime,
}
if id, err = s.dao.AddReply(c, reply); err != nil {
log.Error("s.dao.AddReply error(%v)", err)
return
}
}
for _, v := range strings.Split(imgURL, ";") {
if v != "" {
cTime = xtime.Time(now.Unix())
reply = &model.Reply{
SessionID: ssn.ID,
ReplyID: replyID,
Content: "",
ImgURL: v,
LogURL: "",
CTime: cTime,
MTime: cTime,
}
if id, err = s.dao.AddReply(c, reply); err != nil {
log.Error("s.dao.AddReply error(%v)", err)
continue
}
}
}
}
}
r = &model.Reply{
ID: id,
ReplyID: replyID,
Type: model.TypeCustomer,
Content: content,
ImgURL: imgURL,
LogURL: logURL,
CTime: cTime,
}
return
}
// AddWebReply add web reply.
func (s *Service) AddWebReply(c context.Context, mid, sid, tagID int64, aid, content, imgURL, netState, netOperator, agencyArea, platform, version, buvid, browser, qq, email string, now time.Time) (r *model.Reply, err error) {
var (
cTime xtime.Time
reply *model.Reply
tx *sql.Tx
rid int
id int64
sessionID int64
replyID string
ip = metadata.String(c, metadata.RemoteIP)
)
if mid != 0 {
replyID = strconv.FormatInt(mid, 10)
}
sessionID = sid
cTime = xtime.Time(now.Unix())
if buvid == "" {
buvid = strconv.FormatInt(now.Unix(), 10)
}
rid, err = s.dao.JudgeSsnRecord(c, sessionID)
if err != nil {
log.Error("s.dao.JudgeSsnRecord error(%v)", err)
return
}
if sessionID > 0 || rid > 0 {
reply = &model.Reply{
SessionID: sessionID,
ReplyID: replyID,
Content: content,
ImgURL: imgURL,
CTime: cTime,
MTime: cTime,
}
tx, err = s.dao.BeginTran(c)
if err != nil {
log.Error("s.dao.Begin error(%v)", err)
return
}
if id, err = s.dao.TxAddReply(tx, reply); err != nil {
log.Error("s.dao.TxAddReply error(%v)", err)
tx.Rollback()
return
}
if err = s.dao.TxUpdateSessionState(tx, model.StateNoReply, sessionID); err != nil {
log.Error("s.dao.TxUpdateSessionState error(%v)", err)
tx.Rollback()
return
}
if err = tx.Commit(); err != nil {
log.Error("tx.Commit(), error(%v)", err)
return
}
r = &model.Reply{
ID: id,
ReplyID: replyID,
Type: model.TypeCustomer,
Content: content,
ImgURL: imgURL,
CTime: cTime,
}
} else {
sessionID, err = s.session(c, mid, tagID, buvid, "", version, aid, content, imgURL, "", "", "", ip, "", netState, netOperator, agencyArea, platform, browser, qq, email, now)
if err == nil {
reply = &model.Reply{
SessionID: sessionID,
ReplyID: replyID,
Content: content,
ImgURL: imgURL,
CTime: cTime,
MTime: cTime,
}
if _, err = s.dao.AddReply(c, reply); err != nil {
log.Error("s.dao.AddReplyNoTx error(%v)", err)
return
}
}
}
return
}
func (s *Service) session(c context.Context, mid, tagID int64, buvid, system, version string, aid, content, imgURL, logURL, device, channel, ip, entrance,
netState, netOperator, agencyArea, platform, browser, qq, email string, now time.Time) (sid int64, err error) {
if platform == "ugc" {
var count int
// 通过平台进行处理
count, err = s.dao.SessionCount(c, mid)
if err != nil {
log.Error("s.dao.SessionCount error(%v)", err)
return
}
if count >= 10 {
err = ecode.FeedbackContentOver
return
}
}
cTime := xtime.Time(now.Unix())
var ip32 uint32
ipv := net.ParseIP(ip)
if ip2 := ipv.To4(); ip2 != nil {
ip32 = model.InetAtoN(ip)
}
ssn := &model.Session{
Buvid: buvid,
System: system,
Version: version,
Mid: mid,
Aid: aid,
Content: content,
ImgURL: imgURL,
LogURL: logURL,
Device: device,
Channel: channel,
IP: ip32,
NetState: netState,
NetOperator: netOperator,
AgencyArea: agencyArea,
Platform: platform,
Browser: browser,
QQ: qq,
Email: email,
State: model.StateNoReply,
LasterTime: cTime,
CTime: cTime,
MTime: cTime,
}
if entrance == "view" || content == "播放器反馈日志" || entrance == _androidLivePink || entrance == _iosLivePink || platform == _androidLivePink || platform == _iosLivePink || model.IsPlayerScreen(tagID) || entrance == _tvYST || platform == _tvYST || entrance == _creativeCenter || platform == _creativeCenter {
ssn.State = model.StateOther
}
var tx *sql.Tx
tx, err = s.dao.BeginTran(c)
if err != nil {
log.Error("s.dao.Begin error(%v)", err)
return
}
sid, err = s.dao.TxAddSession(tx, ssn)
if err != nil {
log.Error("s.dao.TxAddSession error(%v)", err)
tx.Rollback()
return
}
if err = s.dao.TxUpSsnMtime(tx, now, sid); err != nil {
log.Error("s.dao.TxUpSsnMtime error(%v)", err)
tx.Rollback()
return
}
ssn.ID = sid
if tagID > 0 {
if _, err = s.dao.TxAddSessionTag(tx, ssn.ID, tagID, now); err != nil {
log.Error("s.dao.TxAddSessionTag error(%v)", err)
tx.Rollback()
return
}
}
if err = tx.Commit(); err != nil {
log.Error("tx.Commit(), error(%v)", err)
}
return
}
// Replys show all feedback replays
func (s *Service) Replys(c context.Context, buvid, platform, mobiApp, device, system, version, entrance string, mid int64, pn, ps int) (rs []model.Reply, isEndReply bool, err error) {
var (
replyID = buvid
ssns []*model.Session
ssn *model.Session
r model.Reply
tmp []model.Reply
offset int
limit int
start int
end int
rsl int
)
offset = (pn - 1) * ps
limit = ps
if mid != 0 {
if platform == "android" {
if entrance == _androidCreative {
platform = _androidCreative
} else if entrance == _androidLivePink {
platform = _androidLivePink
} else if model.Plat(mobiApp, device) == model.PlatAndroid {
platform = _androidFeedback
} else if model.Plat(mobiApp, device) == model.PlatAndroidI {
platform = _androidFeedbackI
}
} else if platform == "ios" {
if entrance == _iosCreative {
platform = _iosCreative
} else if entrance == _iosLivePink {
platform = _iosLivePink
} else if model.Plat(mobiApp, device) == model.PlatIPhone {
platform = _iosFeedback
} else if model.Plat(mobiApp, device) == model.PlatIPhoneI {
platform = _iosFeedbackI
}
}
if ssns, err = s.dao.SessionByMid(c, mid, platform); err != nil {
log.Error("s.dao.SessionByMid(%d) error(%v)", mid, err)
return
}
if len(ssns) == 0 {
err = ecode.FeedbackSsnNotExist
return
}
replyID = strconv.FormatInt(mid, 10)
if tmp, err = s.dao.ReplysByMid(c, mid, offset, limit); err != nil {
log.Error("s.dao.ReplysByMid(%d) error(%v)", mid, err)
return
}
if len(tmp) > 0 {
rs = tmp
}
for _, ssn = range ssns {
r = model.Reply{
ID: ssn.ID,
ReplyID: replyID,
Type: model.TypeCustomer,
Content: ssn.Content,
ImgURL: ssn.ImgURL,
LogURL: ssn.LogURL,
CTime: ssn.CTime,
}
rs = append(rs, r)
}
sort.Sort(model.Replys(rs))
rsl = len(rs)
end = rsl
if limit > rsl {
start = 0
isEndReply = true
} else {
start = end - limit
}
rs = rs[start:end]
} else {
if ssn, err = s.dao.Session(c, buvid, system, version, mid); err != nil {
log.Error("s.dao.Session(%s,%s,%s,%d) error(%v)", buvid, system, version, mid, err)
return
}
if ssn == nil {
err = ecode.FeedbackSsnNotExist
return
}
if tmp, err = s.dao.Replys(c, ssn.ID, offset, limit); err != nil {
log.Error("s.dao.Replays(%d) error(%v)", ssn.ID, err)
return
}
if len(tmp) == limit {
rs = tmp
} else {
r = model.Reply{
ID: ssn.ID,
ReplyID: replyID,
Type: model.TypeCustomer,
Content: ssn.Content,
ImgURL: ssn.ImgURL,
LogURL: ssn.LogURL,
CTime: ssn.CTime,
}
rs = append(tmp, r)
isEndReply = true
}
sort.Sort(model.Replys(rs))
}
return
}
// Sessions sessions.
func (s *Service) Sessions(c context.Context, mid int64, state string, tagID, platform string, start, end time.Time, ps, pn int) (total int, wssns []*model.WebSession, err error) {
var (
sids, sidsTmp, tids, intersect, sidTmp, sidCut []int64
limit, st, en, sls int
ssnMap map[int64]*model.Session
ssns []*model.Session
)
if mid > 0 {
ssns, err = s.dao.SessionByMid(c, mid, platform)
if err != nil {
log.Error("s.dao.SessionByMid error(%v)", err)
return
}
for _, v := range ssns {
sids = append(sids, v.ID)
}
if tagID != "" {
tids, err = xstr.SplitInts(tagID)
if err != nil {
log.Error("xstr.SplitInts error(%v)", err)
return
}
if len(tids) > 0 {
sidsTmp, err = s.dao.SessionIDByTagID(c, tids)
if err != nil {
log.Error("s.dao.SessionIDByTagID error(%v)", err)
return
}
}
for _, v := range sids {
if contains(sidsTmp, v) {
intersect = append(intersect, v)
}
}
} else {
intersect = sids
}
}
if len(intersect) > 0 {
if state == "" {
ssns, err = s.dao.SSnBySsnIDAllSate(c, intersect, start, end)
if err != nil {
log.Error("s.dao.sSessionBySsnID error(%v)", err)
return
}
} else {
ssns, err = s.dao.SessionBySsnID(c, intersect, state, start, end)
if err != nil {
log.Error("s.dao.sSessionBySsnID error(%v)", err)
return
}
}
ssnMap = make(map[int64]*model.Session, len(ssns))
for _, v := range ssns {
sidTmp = append(sidTmp, v.ID)
ssnMap[v.ID] = v
}
sls = len(sidTmp)
total = sls
limit = ps
if limit > sls {
st = 0
en = sls
} else {
st = (pn - 1) * limit
en = pn * limit
if en > sls {
en = st + (sls % limit)
}
}
}
sidCut = sidTmp[st:en]
if len(sidCut) > 0 {
var tagsMap map[int64][]*model.Tag
tagsMap, err = s.dao.TagBySsnID(c, sidCut)
if err != nil {
log.Error("s.dao.TagBySsnID error(%v)", err)
return
}
for _, v := range sidCut {
wssn := &model.WebSession{}
wssn.Session = ssnMap[v]
tags := tagsMap[v]
if len(tags) == 0 {
wssn.Tag = defaultTag
} else {
wssn.Tag = tags[len(tags)-1]
}
wssns = append(wssns, wssn)
}
}
return
}
// UpdateSessionState up session state.
func (s *Service) UpdateSessionState(c context.Context, state int, sid int64) (err error) {
if err = s.dao.UpdateSessionState(c, state, sid); err != nil {
log.Error("s.dao.UpdateSessionState error(%v)", err)
}
return
}
// Tags tags.
func (s *Service) Tags(c context.Context, mid int64, mold int, platform string) (tag *model.UGCTag, err error) {
tags, err := s.dao.Tags(c, mold, platform)
if err != nil {
log.Error("s.dao.Tags error(%v)", err)
return
}
cnt, err := s.dao.SessionCount(c, mid)
if err != nil {
log.Error("s.dao.SessionCount error(%v)", err)
return
}
tag = &model.UGCTag{
Tags: tags,
Limit: 10 - cnt,
}
return
}
// WebReplys web replys.
func (s *Service) WebReplys(c context.Context, sid, mid int64) (mp []*model.Reply, err error) {
if mp, err = s.dao.WebReplys(c, sid, mid); err != nil {
log.Error("s.dao.WebReplys(%d, %d) error(%v)", sid, mid, err)
return
}
if len(mp) == 0 {
err = ecode.NothingFound
}
return
}
func contains(s []int64, e int64) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
// PlayerCheck check for player.
func (s *Service) PlayerCheck(c context.Context, platform, ipChangeTimes int, mid, checkTime, aid, connectSpeed, ioSpeed int64, region, school, cdnip string) (err error) {
var (
ipinfo *locmdl.Info
isp int
ipAddr = metadata.String(c, metadata.RemoteIP)
)
if ipinfo, err = s.locationRPC.Info(c, &locmdl.ArgIP{IP: ipAddr}); err != nil {
log.Error("s.locationRPC.Info(%s) error(%v)", ipAddr, err)
}
if ipinfo != nil {
if strings.Contains(ipinfo.ISP, "移动") {
isp = 1
} else if strings.Contains(ipinfo.ISP, "联通") {
isp = 2
} else if strings.Contains(ipinfo.ISP, "电信") {
isp = 3
} else {
isp = 0
}
}
if _, err = s.dao.InPlayCheck(c, platform, isp, ipChangeTimes, mid, checkTime, aid, connectSpeed, ioSpeed, region, school, ipAddr, cdnip); err != nil {
log.Error("s.dao.InPlayCheck(%d, %d, %d, %d, %d, %d, %d, %d, %s, %s, %s, %s) error(%v)", platform, isp, ipChangeTimes, mid, checkTime, aid, connectSpeed, ioSpeed, region, school, ipAddr, cdnip, err)
}
return
}