monitor.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/admin/main/member/model"
  7. "go-common/library/log"
  8. "go-common/library/queue/databus/report"
  9. )
  10. const (
  11. _logActionMonitorAdd = "monitor_user_add"
  12. _logActionMonitorDel = "monitor_user_del"
  13. )
  14. // Monitors is.
  15. func (s *Service) Monitors(ctx context.Context, arg *model.ArgMonitor) ([]*model.Monitor, int, error) {
  16. includeDeleted := false
  17. if arg.Mid > 0 {
  18. includeDeleted = true
  19. }
  20. mns, total, err := s.dao.Monitors(ctx, arg.Mid, includeDeleted, arg.Pn, arg.Ps)
  21. if err != nil {
  22. return nil, 0, err
  23. }
  24. s.monitorsName(ctx, mns)
  25. return mns, total, nil
  26. }
  27. // AddMonitor is.
  28. func (s *Service) AddMonitor(ctx context.Context, arg *model.ArgAddMonitor) error {
  29. remark := fmt.Sprintf("加入监控列表:%s", arg.Remark)
  30. if err := s.dao.AddMonitor(ctx, arg.Mid, arg.Operator, remark); err != nil {
  31. return err
  32. }
  33. report.Manager(&report.ManagerInfo{
  34. Uname: arg.Operator,
  35. UID: arg.OperatorID,
  36. Business: model.ManagerLogID,
  37. Type: 0,
  38. Oid: arg.Mid,
  39. Action: _logActionMonitorAdd,
  40. Ctime: time.Now(),
  41. // extra
  42. Index: []interface{}{},
  43. Content: map[string]interface{}{
  44. "remark": remark,
  45. },
  46. })
  47. return nil
  48. }
  49. // DelMonitor is.
  50. func (s *Service) DelMonitor(ctx context.Context, arg *model.ArgDelMonitor) error {
  51. remark := fmt.Sprintf("移出监控列表:%s", arg.Remark)
  52. if err := s.dao.DelMonitor(ctx, arg.Mid, arg.Operator, remark); err != nil {
  53. return err
  54. }
  55. report.Manager(&report.ManagerInfo{
  56. Uname: arg.Operator,
  57. UID: arg.OperatorID,
  58. Business: model.ManagerLogID,
  59. Type: 0,
  60. Oid: arg.Mid,
  61. Action: _logActionMonitorDel,
  62. Ctime: time.Now(),
  63. // extra
  64. Index: []interface{}{},
  65. Content: map[string]interface{}{
  66. "remark": remark,
  67. },
  68. })
  69. return nil
  70. }
  71. func (s *Service) monitorsName(ctx context.Context, mns []*model.Monitor) {
  72. mids := make([]int64, 0, len(mns))
  73. for _, mn := range mns {
  74. mids = append(mids, mn.Mid)
  75. }
  76. bs, err := s.dao.Bases(ctx, mids)
  77. if err != nil {
  78. log.Error("Failed to fetch bases with mids: %+v: %+v", mids, err)
  79. return
  80. }
  81. for _, mn := range mns {
  82. b, ok := bs[mn.Mid]
  83. if !ok {
  84. continue
  85. }
  86. mn.Name = b.Name
  87. }
  88. }