report.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package dao
  2. import (
  3. "context"
  4. sql "database/sql"
  5. "fmt"
  6. "time"
  7. "go-common/app/admin/main/reply/model"
  8. xsql "go-common/library/database/sql"
  9. "go-common/library/log"
  10. "go-common/library/xstr"
  11. )
  12. const (
  13. // report
  14. _upRptStateSQL = "UPDATE reply_report_%d SET state=?,mtime=? WHERE rpid IN (%s)"
  15. _upRptReasonSQL = "UPDATE reply_report_%d SET state=?,reason=?,content=?,mtime=? WHERE rpid IN (%s)"
  16. _upRptAttrBitSQL = "UPDATE reply_report_%d SET attr=attr&(~(1<<?))|(?<<?),mtime=? WHERE rpid IN (%s)"
  17. _selReportSQL = "SELECT oid,type,rpid,mid,reason,content,count,score,state,attr,ctime,mtime FROM reply_report_%d WHERE rpid=?"
  18. _selReportsSQL = "SELECT oid,type,rpid,mid,reason,content,count,score,state,attr,ctime,mtime FROM reply_report_%d WHERE rpid IN(%s)"
  19. _selReportOidsSQL = "SELECT oid,type,rpid,mid,reason,content,count,score,state,attr,ctime,mtime FROM reply_report_%d WHERE oid IN(?) and type=?"
  20. // report_user
  21. _setRptUserStateSQL = "UPDATE reply_report_user_%d SET state=?,mtime=? WHERE rpid=?"
  22. _selRptUsersSQL = "SELECT oid,type,rpid,mid,reason,content,state,ctime,mtime FROM reply_report_user_%d WHERE rpid=? and state=?"
  23. )
  24. // Report get a reply report.
  25. func (d *Dao) Report(c context.Context, oid, rpID int64) (rpt *model.Report, err error) {
  26. row := d.db.QueryRow(c, fmt.Sprintf(_selReportSQL, hit(oid)), rpID)
  27. rpt = new(model.Report)
  28. if err = row.Scan(&rpt.Oid, &rpt.Type, &rpt.RpID, &rpt.Mid, &rpt.Reason, &rpt.Content, &rpt.Count, &rpt.Score, &rpt.State, &rpt.Attr, &rpt.CTime, &rpt.MTime); err != nil {
  29. if err == xsql.ErrNoRows {
  30. rpt = nil
  31. err = nil
  32. }
  33. }
  34. return
  35. }
  36. // Reports return report map by oid.
  37. func (d *Dao) Reports(c context.Context, oids, rpIDs []int64) (res map[int64]*model.Report, err error) {
  38. hits := make(map[int64][]int64)
  39. for i, oid := range oids {
  40. hit := hit(oid)
  41. hits[hit] = append(hits[hit], rpIDs[i])
  42. }
  43. res = make(map[int64]*model.Report)
  44. for hit, ids := range hits {
  45. var rows *xsql.Rows
  46. if rows, err = d.db.Query(c, fmt.Sprintf(_selReportsSQL, hit, xstr.JoinInts(ids))); err != nil {
  47. return
  48. }
  49. for rows.Next() {
  50. rpt := new(model.Report)
  51. if err = rows.Scan(&rpt.Oid, &rpt.Type, &rpt.RpID, &rpt.Mid, &rpt.Reason, &rpt.Content, &rpt.Count, &rpt.Score, &rpt.State, &rpt.Attr, &rpt.CTime, &rpt.MTime); err != nil {
  52. rows.Close()
  53. return
  54. }
  55. res[rpt.RpID] = rpt
  56. }
  57. if err = rows.Err(); err != nil {
  58. rows.Close()
  59. return
  60. }
  61. rows.Close()
  62. }
  63. return
  64. }
  65. // ReportByOids return report map by oid.
  66. func (d *Dao) ReportByOids(c context.Context, typ int32, oids ...int64) (res map[int64]*model.Report, err error) {
  67. hits := make(map[int64][]int64)
  68. for _, oid := range oids {
  69. hit := hit(oid)
  70. hits[hit] = append(hits[hit], oid)
  71. }
  72. res = make(map[int64]*model.Report)
  73. for hit, oids := range hits {
  74. var rows *xsql.Rows
  75. if rows, err = d.db.Query(c, fmt.Sprintf(_selReportOidsSQL, hit), xstr.JoinInts(oids), typ); err != nil {
  76. return
  77. }
  78. for rows.Next() {
  79. rpt := new(model.Report)
  80. if err = rows.Scan(&rpt.Oid, &rpt.Type, &rpt.RpID, &rpt.Mid, &rpt.Reason, &rpt.Content, &rpt.Count, &rpt.Score, &rpt.State, &rpt.Attr, &rpt.CTime, &rpt.MTime); err != nil {
  81. rows.Close()
  82. return
  83. }
  84. res[rpt.RpID] = rpt
  85. }
  86. if err = rows.Err(); err != nil {
  87. rows.Close()
  88. return
  89. }
  90. rows.Close()
  91. }
  92. return
  93. }
  94. // UpReportsState update the report state.
  95. func (d *Dao) UpReportsState(c context.Context, oids, rpIDs []int64, state int32, now time.Time) (rows int64, err error) {
  96. hitMap := make(map[int64][]int64)
  97. for i, oid := range oids {
  98. hitMap[hit(oid)] = append(hitMap[hit(oid)], rpIDs[i])
  99. }
  100. for hit, ids := range hitMap {
  101. var res sql.Result
  102. res, err = d.db.Exec(c, fmt.Sprintf(_upRptStateSQL, hit, xstr.JoinInts(ids)), state, now)
  103. if err != nil {
  104. log.Error("mysqlDB.Exec error(%v)", err)
  105. return
  106. }
  107. var row int64
  108. row, err = res.RowsAffected()
  109. if err != nil {
  110. log.Error("res.RowsAffected error(%v)", err)
  111. return
  112. }
  113. rows += row
  114. }
  115. return
  116. }
  117. // UpReportsStateWithReason update the report state.
  118. func (d *Dao) UpReportsStateWithReason(c context.Context, oids, rpIDs []int64, state, reason int32, content string, now time.Time) (rows int64, err error) {
  119. hitMap := make(map[int64][]int64)
  120. for i, oid := range oids {
  121. hitMap[hit(oid)] = append(hitMap[hit(oid)], rpIDs[i])
  122. }
  123. for hit, ids := range hitMap {
  124. var res sql.Result
  125. res, err = d.db.Exec(c, fmt.Sprintf(_upRptReasonSQL, hit, xstr.JoinInts(ids)), state, reason, content, now)
  126. if err != nil {
  127. log.Error("mysqlDB.Exec error(%v)", err)
  128. return
  129. }
  130. var row int64
  131. row, err = res.RowsAffected()
  132. if err != nil {
  133. log.Error("res.RowsAffected error(%v)", err)
  134. return
  135. }
  136. rows += row
  137. }
  138. return
  139. }
  140. // UpReportsAttrBit update the report attr.
  141. func (d *Dao) UpReportsAttrBit(c context.Context, oids, rpIDs []int64, bit uint32, val uint32, now time.Time) (rows int64, err error) {
  142. hitMap := make(map[int64][]int64)
  143. for i, oid := range oids {
  144. hitMap[hit(oid)] = append(hitMap[hit(oid)], rpIDs[i])
  145. }
  146. for hit, ids := range hitMap {
  147. var res sql.Result
  148. res, err = d.db.Exec(c, fmt.Sprintf(_upRptAttrBitSQL, hit, xstr.JoinInts(ids)), bit, val, bit, now)
  149. if err != nil {
  150. log.Error("mysqlDB.Exec error(%v)", err)
  151. return
  152. }
  153. var row int64
  154. row, err = res.RowsAffected()
  155. if err != nil {
  156. log.Error("res.RowsAffected error(%v)", err)
  157. return
  158. }
  159. rows += row
  160. }
  161. return
  162. }
  163. // ReportUsers return a report users from mysql.
  164. func (d *Dao) ReportUsers(c context.Context, oid int64, tp int32, rpID int64) (res map[int64]*model.ReportUser, err error) {
  165. rows, err := d.db.Query(c, fmt.Sprintf(_selRptUsersSQL, hit(oid)), rpID, model.ReportUserStateNew)
  166. if err != nil {
  167. log.Error("db.Query error(%v)", err)
  168. return
  169. }
  170. defer rows.Close()
  171. res = make(map[int64]*model.ReportUser)
  172. for rows.Next() {
  173. rpt := &model.ReportUser{}
  174. if err = rows.Scan(&rpt.Oid, &rpt.Type, &rpt.RpID, &rpt.Mid, &rpt.Reason, &rpt.Content, &rpt.State, &rpt.CTime, &rpt.MTime); err != nil {
  175. log.Error("rows.Scan error(%v)", err)
  176. return
  177. }
  178. res[rpt.Mid] = rpt
  179. }
  180. if err = rows.Err(); err != nil {
  181. log.Error("rows.err error(%v)", err)
  182. return
  183. }
  184. return
  185. }
  186. // SetUserReported set a user report state by rpID.
  187. func (d *Dao) SetUserReported(c context.Context, oid int64, tp int32, rpID int64, now time.Time) (rows int64, err error) {
  188. res, err := d.db.Exec(c, fmt.Sprintf(_setRptUserStateSQL, hit(oid)), model.ReportUserStateReported, now, rpID)
  189. if err != nil {
  190. log.Error("db.Exec error(%v)", err)
  191. return
  192. }
  193. return res.RowsAffected()
  194. }