mysql_report.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/interface/main/dm/model"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _rptSharding = 100
  11. _rptUserSharding = 100
  12. _rptLogSharding = 100
  13. _insertRpt = "INSERT INTO dm_report_%d (cid,dmid,uid,reason,content,count,state,up_op,rp_time,score,ctime,mtime) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)" +
  14. " ON DUPLICATE KEY UPDATE uid=?,reason=?,state=?,content=?,count=count+1,rp_time=?,score=score+?"
  15. _selectRpt = "SELECT id,cid,dmid,uid,reason,content,count,state,up_op,score,rp_time,ctime,mtime FROM dm_report_%d WHERE dmid=?"
  16. _updateRptUPOp = "UPDATE dm_report_%d SET up_op=? WHERE dmid=?"
  17. _updateRpt = "UPDATE dm_report_%d SET state=? WHERE dmid=?"
  18. _insertUser = "INSERT IGNORE INTO dm_report_user_%d (dmid,uid,reason,state,content,ctime,mtime) VALUES(?,?,?,?,?,?,?)"
  19. _selectRptLog = "SELECT id,dmid,adminid,reason,result,remark,elapsed,ctime,mtime FROM dm_report_admin_log_%d WHERE dmid=? ORDER BY mtime"
  20. _insertRptLog = "INSERT INTO dm_report_admin_log_%d (dmid,adminid,reason,result,remark,elapsed) VALUES (?,?,?,?,?,?)"
  21. _selectRptUser = "SELECT id,dmid,uid,reason,state,ctime,mtime FROM dm_report_user_%d WHERE dmid=? AND state=?"
  22. _updateRptUserStat = "UPDATE dm_report_user_%d SET state=? WHERE dmid=? AND state!=?"
  23. )
  24. func rptTable(cid int64) int64 {
  25. return cid % _rptSharding
  26. }
  27. func rptUserTable(dmid int64) int64 {
  28. return dmid % _rptUserSharding
  29. }
  30. // RptLogTable return log table id by dmid
  31. func RptLogTable(dmid int64) int64 {
  32. return dmid % _rptLogSharding
  33. }
  34. // AddReport insert or update dm report.
  35. func (d *Dao) AddReport(c context.Context, rpt *model.Report) (id int64, err error) {
  36. res, err := d.biliDM.Exec(c, fmt.Sprintf(_insertRpt, rptTable(rpt.Cid)), rpt.Cid, rpt.Did, rpt.UID, rpt.Reason, rpt.Content, rpt.Count, rpt.State, rpt.UpOP, rpt.RpTime, rpt.Score, rpt.Ctime, rpt.Mtime, rpt.UID, rpt.Reason, rpt.State, rpt.Content, rpt.RpTime, rpt.Score)
  37. if err != nil {
  38. log.Error("d.AddReport(%v) error(%v)", rpt, err)
  39. return
  40. }
  41. return res.LastInsertId()
  42. }
  43. // AddReportUser add dm report user.
  44. func (d *Dao) AddReportUser(c context.Context, u *model.User) (id int64, err error) {
  45. res, err := d.biliDM.Exec(c, fmt.Sprintf(_insertUser, rptUserTable(u.Did)), u.Did, u.UID, u.Reason, u.State, u.Content, u.Ctime, u.Mtime)
  46. if err != nil {
  47. log.Error("d.AddReportUser(%v) error(%v)", u, err)
  48. return
  49. }
  50. return res.LastInsertId()
  51. }
  52. // ReportLog get report log of dmid.
  53. func (d *Dao) ReportLog(c context.Context, dmid int64) (res []*model.RptLog, err error) {
  54. rows, err := d.biliDM.Query(c, fmt.Sprintf(_selectRptLog, RptLogTable(dmid)), dmid)
  55. if err != nil {
  56. log.Error("d.ReportLog(dmid:%d) error(%v)", dmid, err)
  57. return
  58. }
  59. defer rows.Close()
  60. for rows.Next() {
  61. r := &model.RptLog{}
  62. if err = rows.Scan(&r.ID, &r.Did, &r.AdminID, &r.Reason, &r.Result, &r.Remark, &r.Elapsed, &r.Ctime, &r.Mtime); err != nil {
  63. log.Error("rows.Scan() error(%v)", err)
  64. return
  65. }
  66. res = append(res, r)
  67. }
  68. if err = rows.Err(); err != nil {
  69. log.Error("rows.Err() error(%v)", err)
  70. }
  71. return
  72. }
  73. // Report dm report info by cid and dmid.
  74. func (d *Dao) Report(c context.Context, cid, dmid int64) (rpt *model.Report, err error) {
  75. rpt = &model.Report{}
  76. row := d.biliDM.QueryRow(c, fmt.Sprintf(_selectRpt, rptTable(cid)), dmid)
  77. err = row.Scan(&rpt.ID, &rpt.Cid, &rpt.Did, &rpt.UID, &rpt.Reason, &rpt.Content, &rpt.Count, &rpt.State, &rpt.UpOP, &rpt.Score, &rpt.RpTime, &rpt.Ctime, &rpt.Mtime)
  78. if err != nil {
  79. if err == sql.ErrNoRows {
  80. rpt = nil
  81. err = nil
  82. } else {
  83. log.Error("row.Scan() error(%v)", err)
  84. }
  85. }
  86. return
  87. }
  88. // UpdateReportStat update report state
  89. func (d *Dao) UpdateReportStat(c context.Context, cid, dmid int64, state int8) (affect int64, err error) {
  90. sqlStr := fmt.Sprintf(_updateRpt, rptTable(cid))
  91. res, err := d.biliDM.Exec(c, sqlStr, state, dmid)
  92. if err != nil {
  93. log.Error("d.UpdateReportStat(cid:%d, dmid:%d) error(%v)", cid, dmid, err)
  94. return
  95. }
  96. return res.RowsAffected()
  97. }
  98. // UpdateReportUPOp update dm report state.
  99. func (d *Dao) UpdateReportUPOp(c context.Context, cid, dmid int64, op int8) (affect int64, err error) {
  100. res, err := d.biliDM.Exec(c, fmt.Sprintf(_updateRptUPOp, rptTable(cid)), op, dmid)
  101. if err != nil {
  102. log.Error("d.UpdateReportUPOp(cid:%d, dmid:%d) error(%v)", cid, dmid, err)
  103. return
  104. }
  105. return res.RowsAffected()
  106. }
  107. // AddReportLog add report log.
  108. func (d *Dao) AddReportLog(c context.Context, lg *model.RptLog) (err error) {
  109. sqlStr := fmt.Sprintf(_insertRptLog, RptLogTable(lg.Did))
  110. _, err = d.biliDM.Exec(c, sqlStr, lg.Did, lg.AdminID, lg.Reason, lg.Result, lg.Remark, lg.Elapsed)
  111. if err != nil {
  112. log.Error("d.AddReportLog(%v) error(%v)", lg, err)
  113. }
  114. return
  115. }
  116. // ReportUser return report use list of dmid.
  117. func (d *Dao) ReportUser(c context.Context, dmid int64) (users []*model.User, err error) {
  118. sqlStr := fmt.Sprintf(_selectRptUser, rptUserTable(dmid))
  119. rows, err := d.biliDM.Query(c, sqlStr, dmid, model.NoticeUnsend)
  120. if err != nil {
  121. log.Error("d.ReportUser(query:%s, dmid:%d) error(%v)", sqlStr, dmid, err)
  122. return
  123. }
  124. defer rows.Close()
  125. for rows.Next() {
  126. u := &model.User{}
  127. if err = rows.Scan(&u.ID, &u.Did, &u.UID, &u.Reason, &u.State, &u.Ctime, &u.Mtime); err != nil {
  128. log.Error("rows.Scan() error(%v)", err)
  129. return
  130. }
  131. users = append(users, u)
  132. }
  133. if err = rows.Err(); err != nil {
  134. log.Error("rows.Err() error(%v)", err)
  135. }
  136. return
  137. }
  138. // SetReportUserFinished set dmid state to be noticesend.
  139. func (d *Dao) SetReportUserFinished(c context.Context, dmid int64) (err error) {
  140. sqlStr := fmt.Sprintf(_updateRptUserStat, rptUserTable(dmid))
  141. if _, err = d.biliDM.Exec(c, sqlStr, model.NoticeSend, dmid, model.NoticeSend); err != nil {
  142. log.Error("d.SetReportUserFinished(sql:%s, dmid:%d) error(%v)", sqlStr, dmid, err)
  143. }
  144. return
  145. }