mysql_complaint.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "go-common/library/log"
  6. )
  7. const (
  8. _addComplaintsSQL = "INSERT INTO article_complaints(article_id,mid,type,reason,image_urls) VALUES (?,?,?,?,?)"
  9. _complaintExistSQL = "SELECT id FROM article_complaints WHERE article_id=? AND mid=? AND state=0"
  10. _complaintProtectSQL = "SELECT protect FROM article_complain_articles WHERE article_id=? AND deleted_time=0"
  11. _addComplaintCountSQL = "INSERT INTO article_complain_articles(article_id,count) VALUES (?,1) ON DUPLICATE KEY UPDATE count=count+1,state=0"
  12. _articleProtected = 1 // 0: no pretected 1: protected
  13. )
  14. // AddComplaint add complaint.
  15. func (d *Dao) AddComplaint(c context.Context, aid, mid, ctype int64, reason, imageUrls string) (err error) {
  16. if _, err = d.addComplaintStmt.Exec(c, aid, mid, ctype, reason, imageUrls); err != nil {
  17. PromError("db:新增投诉")
  18. log.Error("dao.addComplaintStmt.exec(%s, %v, %v, %v, %v) error(%+v)", aid, mid, ctype, reason, imageUrls, err)
  19. }
  20. return
  21. }
  22. // ComplaintExist .
  23. func (d *Dao) ComplaintExist(c context.Context, aid, mid int64) (exist bool, err error) {
  24. var id int
  25. if err = d.complaintExistStmt.QueryRow(c, aid, mid).Scan(&id); err != nil {
  26. if err == sql.ErrNoRows {
  27. err = nil
  28. } else {
  29. log.Error("d.complaintExistStmt.QueryRow(%d,%d) error(%+v)", aid, mid, err)
  30. PromError("db:判断之前是否投诉过")
  31. }
  32. return
  33. }
  34. exist = true
  35. return
  36. }
  37. // ComplaintProtected .
  38. func (d *Dao) ComplaintProtected(c context.Context, aid int64) (protected bool, err error) {
  39. var p int
  40. if err = d.complaintProtectStmt.QueryRow(c, aid).Scan(&p); err != nil {
  41. if err == sql.ErrNoRows {
  42. err = nil
  43. } else {
  44. log.Error("d.complaintProtectStmt.QueryRow(%d) error(%+v)", aid, err)
  45. PromError("db:判断文章是否被保护")
  46. }
  47. return
  48. }
  49. if p == _articleProtected {
  50. protected = true
  51. }
  52. return
  53. }
  54. // AddComplaintCount .
  55. func (d *Dao) AddComplaintCount(c context.Context, aid int64) (err error) {
  56. if _, err = d.addComplaintCountStmt.Exec(c, aid); err != nil {
  57. log.Error("d.addComplaintCountStmt.Exec(%d) error(%+v)", aid, err)
  58. PromError("db:增加投诉计数")
  59. }
  60. return
  61. }