forbid.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package dao
  2. import (
  3. "context"
  4. "go-common/app/job/main/click/model"
  5. "go-common/library/log"
  6. )
  7. const (
  8. _forbidSQL = "SELECT aid,plat,lv,locked FROM archive_click_forbid WHERE locked = ?"
  9. _upForbidSQL = "INSERT INTO archive_click_forbid(aid,plat,lv,locked) VALUES(?,?,?,?) ON DUPLICATE KEY UPDATE lv=?,locked=?"
  10. _allForbidMidsSQL = "SELECT mid FROM archive_mid_forbid WHERE status=1"
  11. _upForbidMidSQL = "INSERT INTO archive_mid_forbid(mid,status) VALUE(?,?) ON DUPLICATE KEY UPDATE status=?"
  12. )
  13. // ForbidMids is
  14. func (d *Dao) ForbidMids(c context.Context) (mids map[int64]struct{}, err error) {
  15. rows, err := d.db.Query(c, _allForbidMidsSQL)
  16. if err != nil {
  17. log.Error("d.db.Query(%s) error(%v)", _allForbidMidsSQL, err)
  18. return
  19. }
  20. defer rows.Close()
  21. mids = make(map[int64]struct{})
  22. for rows.Next() {
  23. var mid int64
  24. if err = rows.Scan(&mid); err != nil {
  25. log.Error("rows.Scan error(%v)", err)
  26. return
  27. }
  28. mids[mid] = struct{}{}
  29. }
  30. err = rows.Err()
  31. return
  32. }
  33. // UpMidForbidStatus is
  34. func (d *Dao) UpMidForbidStatus(c context.Context, mid int64, status int8) (err error) {
  35. _, err = d.db.Exec(c, _upForbidMidSQL, mid, status, status)
  36. return
  37. }
  38. // Forbids is
  39. func (d *Dao) Forbids(c context.Context) (forbids map[int64]map[int8]*model.Forbid, err error) {
  40. rows, err := d.db.Query(c, _forbidSQL, model.ValueForLocked)
  41. if err != nil {
  42. log.Error("d.db.Query(%s) error(%v)", _forbidSQL, model.ValueForLocked, err)
  43. return
  44. }
  45. defer rows.Close()
  46. forbids = make(map[int64]map[int8]*model.Forbid)
  47. for rows.Next() {
  48. var f = &model.Forbid{}
  49. if err = rows.Scan(&f.AID, &f.Plat, &f.Lv, &f.Locked); err != nil {
  50. log.Error("rows.Scan error(%v)", err)
  51. return
  52. }
  53. if _, ok := forbids[f.AID]; !ok {
  54. forbids[f.AID] = make(map[int8]*model.Forbid)
  55. }
  56. forbids[f.AID][f.Plat] = f
  57. }
  58. err = rows.Err()
  59. return
  60. }
  61. // UpForbid is
  62. func (d *Dao) UpForbid(c context.Context, aid int64, plat, lock, lv int8) (rows int64, err error) {
  63. res, err := d.db.Exec(c, _upForbidSQL, aid, plat, lv, lock, lv, lock)
  64. if err != nil {
  65. log.Error("d.db.Exec(%s) error(%v)", _upForbidSQL, err)
  66. return
  67. }
  68. return res.RowsAffected()
  69. }