monitor.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package dao
  2. import (
  3. "context"
  4. "go-common/app/admin/main/member/model"
  5. "go-common/library/database/sql"
  6. "github.com/pkg/errors"
  7. )
  8. const (
  9. _monitorName = "user_monitor"
  10. )
  11. // Monitors is.
  12. func (d *Dao) Monitors(ctx context.Context, mid int64, includeDeleted bool, pn, ps int) (mns []*model.Monitor, total int, err error) {
  13. query := d.member.Table(_monitorName).Order("id DESC")
  14. if !includeDeleted {
  15. query = query.Where("is_deleted=?", false)
  16. }
  17. if mid > 0 {
  18. query = query.Where("mid=?", mid)
  19. }
  20. query = query.Count(&total)
  21. query = query.Offset((pn - 1) * ps).Limit(ps)
  22. if err = query.Find(&mns).Error; err != nil {
  23. if err == sql.ErrNoRows {
  24. return []*model.Monitor{}, 0, nil
  25. }
  26. err = errors.Wrap(err, "monitors")
  27. return
  28. }
  29. return
  30. }
  31. // AddMonitor is.
  32. func (d *Dao) AddMonitor(ctx context.Context, mid int64, operator, remark string) error {
  33. mn := &model.Monitor{
  34. Mid: mid,
  35. }
  36. ups := map[string]interface{}{
  37. "is_deleted": false,
  38. "operator": operator,
  39. "remark": remark,
  40. }
  41. if err := d.member.Table(_monitorName).Where("mid=?", mid).Assign(ups).FirstOrCreate(mn).Error; err != nil {
  42. return errors.Wrap(err, "add monitor")
  43. }
  44. return nil
  45. }
  46. // DelMonitor is.
  47. func (d *Dao) DelMonitor(ctx context.Context, mid int64, operator, remark string) error {
  48. ups := map[string]interface{}{
  49. "is_deleted": true,
  50. "operator": operator,
  51. "remark": remark,
  52. }
  53. if err := d.member.Table(_monitorName).Where("mid=?", mid).UpdateColumns(ups).Error; err != nil {
  54. return errors.Wrap(err, "del monitor")
  55. }
  56. return nil
  57. }