blacklist.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package dao
  2. import (
  3. "fmt"
  4. "go-common/app/admin/main/space/model"
  5. "go-common/library/log"
  6. )
  7. // BlacklistAdd add blacklist
  8. func (d *Dao) BlacklistAdd(addmids, upmids []int64) (err error) {
  9. tx := d.DB.Begin()
  10. if err = tx.Error; err != nil {
  11. log.Error("dao.BlacklistAdd.Begin error(%v)", err)
  12. return
  13. }
  14. if len(addmids) > 0 {
  15. if err = tx.Model(&model.Blacklist{}).Exec(model.BlacklistBatchAddSQL(addmids)).Error; err != nil {
  16. log.Error("dao.BlacklistAdd.BlacklistBatchAddSQL(%+v) error(%v)", addmids, err)
  17. err = tx.Rollback().Error
  18. return
  19. }
  20. }
  21. if len(upmids) > 0 {
  22. if err = tx.Model(&model.Blacklist{}).Exec(model.BlacklistBatchUpdateSQL(upmids)).Error; err != nil {
  23. log.Error("dao.BlacklistAdd.BlacklistBatchUpdateSQL(%+v) error(%v)", upmids, err)
  24. err = tx.Rollback().Error
  25. return
  26. }
  27. }
  28. err = tx.Commit().Error
  29. return
  30. }
  31. // BlacklistIn query blackist count
  32. func (d *Dao) BlacklistIn(mids []int64) (blacks map[int64]*model.Blacklist, err error) {
  33. var (
  34. blacklist []*model.Blacklist
  35. )
  36. blacks = make(map[int64]*model.Blacklist, len(mids))
  37. if len(mids) == 0 {
  38. return nil, fmt.Errorf("mid不能为空")
  39. }
  40. if err = d.DB.Model(&model.Blacklist{}).Where("mid in (?)", mids).Find(&blacklist).
  41. Error; err != nil {
  42. log.Error("dao.BlacklistIn.Count(%+v) error(%v)", mids, err)
  43. return
  44. }
  45. for _, v := range blacklist {
  46. blacks[v.Mid] = v
  47. }
  48. return
  49. }
  50. // BlacklistUp blackist update
  51. func (d *Dao) BlacklistUp(id int64, status int) (err error) {
  52. w := map[string]interface{}{
  53. "id": id,
  54. }
  55. up := map[string]interface{}{
  56. "status": status,
  57. }
  58. if err = d.DB.Model(&model.Blacklist{}).Where(w).Update(up).Error; err != nil {
  59. log.Error("dao.BlacklistUp.Update error(%v)", err)
  60. return
  61. }
  62. return
  63. }
  64. // BlacklistIndex blackist
  65. func (d *Dao) BlacklistIndex(mid int64, pn, ps int) (pager *model.BlacklistPager, err error) {
  66. var (
  67. blacklist []*model.Blacklist
  68. )
  69. pager = &model.BlacklistPager{
  70. Page: model.Page{
  71. Num: pn,
  72. Size: ps,
  73. },
  74. }
  75. query := d.DB.Model(&model.Blacklist{})
  76. if mid != 0 {
  77. query = query.Where("mid = ?", mid)
  78. }
  79. if err = query.Count(&pager.Page.Total).Error; err != nil {
  80. log.Error("dao.BlacklistIndex.Count error(%v)", err)
  81. return
  82. }
  83. if err = query.Order("`id` DESC").Offset((pn - 1) * ps).Limit(ps).Find(&blacklist).Error; err != nil {
  84. log.Error("dao.BlacklistIndex.Find error(%v)", err)
  85. return
  86. }
  87. pager.Item = blacklist
  88. return
  89. }