mysql_report.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package dao
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "go-common/app/admin/main/dm/model"
  7. "go-common/library/log"
  8. "go-common/library/xstr"
  9. )
  10. const (
  11. _userSharding = 100
  12. _logSharding = 100
  13. _reportSharding = 100
  14. // dm report
  15. _updateStatSQL = "UPDATE dm_report_%d SET state=? WHERE dmid IN (%s)"
  16. _ignoreStatSQL = "UPDATE dm_report_%d SET state=?,count=0 WHERE dmid IN (%s)"
  17. _selectRpt = "SELECT id,dmid,cid,uid,reason,count,up_op,state,score,rp_time,ctime,mtime FROM dm_report_%d WHERE dmid IN (%s)"
  18. _selectUsers = "SELECT id,dmid,uid,reason,state,ctime,mtime FROM dm_report_user_%d WHERE dmid IN (%s) AND state=? ORDER BY id"
  19. _updateUser = "UPDATE dm_report_user_%d SET state=? WHERE dmid IN (%s) AND state !=?"
  20. _insertLog = "INSERT dm_report_admin_log_%d (dmid,adminid,reason,result,remark,elapsed) VALUES"
  21. _selectLog = "SELECT id,dmid,adminid,reason,result,remark,elapsed,ctime,mtime FROM dm_report_admin_log_%d WHERE dmid=? ORDER BY mtime"
  22. )
  23. // RptTable return report table id by cid
  24. func RptTable(cid int64) int64 {
  25. return cid % _reportSharding
  26. }
  27. // UserTable return user table id by dmid
  28. func UserTable(dmid int64) int64 {
  29. return dmid % _userSharding
  30. }
  31. // LogTable return log table id by dmid
  32. func LogTable(dmid int64) int64 {
  33. return dmid % _logSharding
  34. }
  35. // ChangeReportStat change report state
  36. func (d *Dao) ChangeReportStat(c context.Context, cid int64, dmids []int64, state int8) (err error) {
  37. updateSQL := fmt.Sprintf(_updateStatSQL, RptTable(cid), xstr.JoinInts(dmids))
  38. if _, err = d.biliDM.Exec(c, updateSQL, state); err != nil {
  39. log.Error("d.biliDM.Exec(%d) error(%v)", state, err)
  40. }
  41. return
  42. }
  43. // IgnoreReport change report state to SecondIgnore or FirstIngnore
  44. func (d *Dao) IgnoreReport(c context.Context, cid int64, dmids []int64, state int8) (err error) {
  45. updateSQL := fmt.Sprintf(_ignoreStatSQL, RptTable(cid), xstr.JoinInts(dmids))
  46. if _, err = d.biliDM.Exec(c, updateSQL, state); err != nil {
  47. log.Error("d.biliDM.Exec(%d) error(%v)", state, err)
  48. }
  49. return
  50. }
  51. // Reports get multi dm report info.
  52. func (d *Dao) Reports(c context.Context, cid int64, dmids []int64) (res []*model.Report, err error) {
  53. res = []*model.Report{}
  54. selectSQL := fmt.Sprintf(_selectRpt, RptTable(cid), xstr.JoinInts(dmids))
  55. rows, err := d.biliDM.Query(c, selectSQL)
  56. if err != nil {
  57. log.Error("d.biliDM.Exec(cid:%d,dmids:%v) error(%v)", cid, dmids, err)
  58. return
  59. }
  60. defer rows.Close()
  61. for rows.Next() {
  62. r := &model.Report{}
  63. err = rows.Scan(&r.ID, &r.Did, &r.Cid, &r.UID, &r.RpType, &r.Count, &r.UpOP, &r.State, &r.Score, &r.RpTime, &r.Ctime, &r.Mtime)
  64. if err != nil {
  65. log.Error("rows.Scan() error(%v)", err)
  66. return
  67. }
  68. res = append(res, r)
  69. }
  70. return
  71. }
  72. // ReportUsers get user list.
  73. func (d *Dao) ReportUsers(c context.Context, tableID int64, dmids []int64, state int8) (res map[int64][]*model.ReportUser, err error) {
  74. res = make(map[int64][]*model.ReportUser, 100)
  75. selectSQL := fmt.Sprintf(_selectUsers, tableID, xstr.JoinInts(dmids))
  76. rows, err := d.biliDM.Query(c, selectSQL, state)
  77. if err != nil {
  78. log.Error("d.biliDM.Query(sql:%s) error(%v)", selectSQL, err)
  79. return
  80. }
  81. defer rows.Close()
  82. for rows.Next() {
  83. u := &model.ReportUser{}
  84. if err = rows.Scan(&u.ID, &u.Did, &u.UID, &u.Reason, &u.State, &u.Ctime, &u.Mtime); err != nil {
  85. log.Error("rows.Scan() error(%v)", err)
  86. return
  87. }
  88. res[u.Did] = append(res[u.Did], u)
  89. }
  90. return
  91. }
  92. // UpReportUserState update report user state.
  93. func (d *Dao) UpReportUserState(c context.Context, tableID int64, dmids []int64, state int8) (affect int64, err error) {
  94. selectSQL := fmt.Sprintf(_updateUser, tableID, xstr.JoinInts(dmids))
  95. res, err := d.biliDM.Exec(c, selectSQL, state, state)
  96. if err != nil {
  97. log.Error("d.updateUserStmt.Exec(dmid:%v) error(%v)", dmids, err)
  98. return
  99. }
  100. return res.RowsAffected()
  101. }
  102. // AddReportLog add dm report admin log.
  103. func (d *Dao) AddReportLog(c context.Context, tableID int64, lg []*model.ReportLog) (err error) {
  104. var (
  105. buffer bytes.Buffer
  106. insertTp string
  107. )
  108. insertTp = "(%d,%d,%d,%d,'%s',%d),"
  109. buffer.WriteString(fmt.Sprintf(_insertLog, tableID))
  110. for _, v := range lg {
  111. buffer.WriteString(fmt.Sprintf(insertTp, v.Did, v.AdminID, v.Reason, v.Result, v.Remark, v.Elapsed))
  112. }
  113. //truncate the last ','
  114. buffer.Truncate(buffer.Len() - 1)
  115. _, err = d.biliDM.Exec(c, buffer.String())
  116. if err != nil {
  117. log.Error("d.insertLogStmt.Exec(%v) error(%v)", lg, err)
  118. return
  119. }
  120. return
  121. }
  122. // ReportLog get dm report log.
  123. func (d *Dao) ReportLog(c context.Context, dmid int64) (res []*model.ReportLog, err error) {
  124. selectSQL := fmt.Sprintf(_selectLog, LogTable(dmid))
  125. rows, err := d.biliDM.Query(c, selectSQL, dmid)
  126. if err != nil {
  127. log.Error("dmreport:d.biliDM.Query(sql:%s) error(%v)", selectSQL, err)
  128. return
  129. }
  130. defer rows.Close()
  131. for rows.Next() {
  132. r := &model.ReportLog{}
  133. if err = rows.Scan(&r.ID, &r.Did, &r.AdminID, &r.Reason, &r.Result, &r.Remark, &r.Elapsed, &r.Ctime, &r.Mtime); err != nil {
  134. log.Error("rows.Scan() error(%v)", err)
  135. return
  136. }
  137. res = append(res, r)
  138. }
  139. return
  140. }