block.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "go-common/app/admin/main/growup/model"
  7. "go-common/library/log"
  8. xtime "go-common/library/time"
  9. )
  10. const (
  11. // insert
  12. _inBlockedSQL = "INSERT INTO up_blocked (mid,nickname,original_archive_count,category_id,fans,apply_at,is_deleted) VALUES (?,?,?,?,?,?,0) ON DUPLICATE KEY UPDATE mid=?,nickname=?,original_archive_count=?,category_id=?,fans=?,apply_at=?,is_deleted=0"
  13. // select
  14. _upInfoVideoSQL = "SELECT apply_at FROM up_info_video WHERE mid=?"
  15. _blockedCountSQL = "SELECT count(*) FROM up_blocked WHERE %s"
  16. _blockedSQL = "SELECT mid,nickname,original_archive_count,category_id,fans,apply_at FROM up_blocked WHERE %s"
  17. _isBlockedSQL = "SELECT mid FROM up_blocked WHERE mid=? AND is_deleted=0"
  18. // update
  19. _upBlockedStateSQL = "UPDATE up_blocked SET is_deleted=? WHERE mid=?"
  20. )
  21. // ApplyAt query apply_at from up_info_video by mid
  22. func (d *Dao) ApplyAt(c context.Context, mid int64) (applyAt xtime.Time, err error) {
  23. row := d.rddb.QueryRow(c, _upInfoVideoSQL, mid)
  24. if err = row.Scan(&applyAt); err != nil {
  25. if err == sql.ErrNoRows {
  26. err = nil
  27. } else {
  28. log.Error("row.Scan error(%v)", err)
  29. }
  30. }
  31. return
  32. }
  33. // InsertBlocked insert block up to blacklist
  34. func (d *Dao) InsertBlocked(c context.Context, v *model.Blocked) (rows int64, err error) {
  35. res, err := d.rddb.Exec(c, _inBlockedSQL, v.MID, v.Nickname, v.OriginalArchiveCount, v.MainCategory, v.Fans, v.ApplyAt, v.MID, v.Nickname, v.OriginalArchiveCount, v.MainCategory, v.Fans, v.ApplyAt)
  36. if err != nil {
  37. log.Error("db.inBlockedStmt.Exec(%s) error(%v)", _inBlockedSQL, err)
  38. return
  39. }
  40. return res.RowsAffected()
  41. }
  42. // UpdateBlockedState update blocked is_deleted
  43. func (d *Dao) UpdateBlockedState(c context.Context, mid int64, del int) (rows int64, err error) {
  44. res, err := d.rddb.Exec(c, _upBlockedStateSQL, del, mid)
  45. if err != nil {
  46. log.Error("db.upBlockedState.Exec(%s) error(%v)", _upBlockedStateSQL, err)
  47. return
  48. }
  49. return res.RowsAffected()
  50. }
  51. // DelFromBlocked del blocked
  52. func (d *Dao) DelFromBlocked(c context.Context, mid int64) (rows int64, err error) {
  53. return d.UpdateBlockedState(c, mid, 1)
  54. }
  55. // BlockCount get blocked count
  56. func (d *Dao) BlockCount(c context.Context, query string) (count int, err error) {
  57. row := d.rddb.QueryRow(c, fmt.Sprintf(_blockedCountSQL, query))
  58. if err = row.Scan(&count); err != nil {
  59. if err == sql.ErrNoRows {
  60. err = nil
  61. } else {
  62. log.Error("rows.Scan error(%v)", err)
  63. }
  64. }
  65. return
  66. }
  67. // QueryFromBlocked query blocked user in black list
  68. func (d *Dao) QueryFromBlocked(c context.Context, query string) (ups []*model.Blocked, err error) {
  69. rows, err := d.rddb.Query(c, fmt.Sprintf(_blockedSQL, query))
  70. if err != nil {
  71. return
  72. }
  73. defer rows.Close()
  74. for rows.Next() {
  75. v := &model.Blocked{}
  76. err = rows.Scan(&v.MID, &v.Nickname, &v.OriginalArchiveCount, &v.MainCategory, &v.Fans, &v.ApplyAt)
  77. if err != nil {
  78. log.Error("rows.Scan error(%v)", err)
  79. return
  80. }
  81. ups = append(ups, v)
  82. }
  83. return
  84. }
  85. // Blocked check user is blocked
  86. func (d *Dao) Blocked(c context.Context, mid int64) (id int64, err error) {
  87. row := d.rddb.QueryRow(c, _isBlockedSQL, mid)
  88. if err = row.Scan(&id); err != nil {
  89. if err == sql.ErrNoRows {
  90. err = nil
  91. } else {
  92. log.Error("row Scan error(%v)", err)
  93. }
  94. }
  95. return
  96. }