blacklist.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package service
  2. import (
  3. "fmt"
  4. "time"
  5. "go-common/app/admin/main/space/model"
  6. "go-common/library/log"
  7. "go-common/library/queue/databus/report"
  8. "github.com/jinzhu/gorm"
  9. )
  10. const (
  11. _BlacklistAdd = "BlacklistAdd"
  12. _BlacklistUp = "BlacklistUp"
  13. _StatusNotBlack = 1
  14. )
  15. // BlacklistAdd add blacklist
  16. func (s *Service) BlacklistAdd(mids []int64, name string, uid int64) (err error) {
  17. var (
  18. blacklist map[int64]*model.Blacklist
  19. updateMids, addMids []int64
  20. )
  21. if len(mids) > 30 {
  22. err = fmt.Errorf("黑名单一次最多只能添加30个")
  23. return
  24. }
  25. if blacklist, err = s.dao.BlacklistIn(mids); err != nil {
  26. return
  27. }
  28. for _, v := range mids {
  29. if blacklist[v] != nil {
  30. if blacklist[v].Status == _StatusNotBlack {
  31. updateMids = append(updateMids, blacklist[v].ID)
  32. }
  33. } else {
  34. addMids = append(addMids, v)
  35. }
  36. }
  37. if err = s.dao.BlacklistAdd(addMids, updateMids); err != nil {
  38. return
  39. }
  40. for _, v := range mids {
  41. if err = report.Manager(&report.ManagerInfo{
  42. Uname: name,
  43. UID: uid,
  44. Business: model.NoticeLogID,
  45. Type: model.LogBlacklist,
  46. Oid: v,
  47. Action: _BlacklistAdd,
  48. Ctime: time.Now(),
  49. Content: map[string]interface{}{
  50. "mids": mids,
  51. },
  52. }); err != nil {
  53. return
  54. }
  55. }
  56. return
  57. }
  58. // BlacklistUp update blacklist
  59. func (s *Service) BlacklistUp(id int64, status int, name string, uid int64) (err error) {
  60. var (
  61. mids []int64
  62. )
  63. blacklist := &model.Blacklist{}
  64. if err = s.dao.DB.Model(&model.Blacklist{}).Where("id in (?)", []int64{id}).First(blacklist).Error; err != nil {
  65. if err == gorm.ErrRecordNotFound {
  66. err = fmt.Errorf("找不用数据")
  67. return
  68. }
  69. log.Error("Srv.BlacklistUp First error(%v)", err)
  70. return
  71. }
  72. if err = s.dao.BlacklistUp(id, status); err != nil {
  73. return
  74. }
  75. mids = []int64{blacklist.Mid}
  76. if err = report.Manager(&report.ManagerInfo{
  77. Uname: name,
  78. UID: uid,
  79. Business: model.NoticeLogID,
  80. Type: model.LogBlacklist,
  81. Oid: blacklist.Mid,
  82. Action: _BlacklistUp,
  83. Ctime: time.Now(),
  84. Content: map[string]interface{}{
  85. "id": id,
  86. "status": status,
  87. "mids": mids,
  88. },
  89. }); err != nil {
  90. return
  91. }
  92. return
  93. }
  94. // BlacklistIndex .
  95. func (s *Service) BlacklistIndex(mid int64, pn, ps int) (pager *model.BlacklistPager, err error) {
  96. if pager, err = s.dao.BlacklistIndex(mid, pn, ps); err != nil {
  97. return
  98. }
  99. return
  100. }