ot_fire_events.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package service
  2. import (
  3. "errors"
  4. "time"
  5. "github.com/go-admin-team/go-admin-core/sdk/service"
  6. "gorm.io/gorm"
  7. "go-admin/app/admin/models"
  8. "go-admin/app/admin/service/dto"
  9. "go-admin/common/actions"
  10. cDto "go-admin/common/dto"
  11. )
  12. type OtFireEvents struct {
  13. service.Service
  14. }
  15. // GetPage 获取OtFireEvents列表
  16. func (e *OtFireEvents) GetPage(c *dto.OtFireEventsGetPageReq, p *actions.DataPermission, list *[]models.OtFireEvents, count *int64) error {
  17. var err error
  18. var data models.OtFireEvents
  19. c.CheckFilling(time.Hour)
  20. start, end := time.Unix(c.StartTime, 0).Format(time.DateTime), time.Unix(c.EndTime, 0).Format(time.DateTime)
  21. db := e.Orm.Model(&data).
  22. Scopes(
  23. // cDto.MakeCondition(c.GetNeedSearch()),
  24. cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  25. // actions.Permission(data.TableName(), p),
  26. ).
  27. Where("is_know = 0 AND is_resolve = 0 AND is_ignore = 0").
  28. Where("created_at>=? and created_at<?", start, end)
  29. if c.AppAlias != "" {
  30. db.Where("app_alias=?", c.AppAlias)
  31. }
  32. if c.RuleId > 0 {
  33. db.Where("rule_id=?", c.RuleId)
  34. }
  35. if c.AlertStatus > 0 {
  36. db.Where("alert_status=?", c.AlertStatus)
  37. }
  38. if c.ExceptionName != "" {
  39. db.Where("exception_name", c.ExceptionName)
  40. }
  41. err = db.Order("append_time DESC").Find(list).Limit(-1).Offset(-1).
  42. Count(count).Error
  43. if err != nil {
  44. e.Log.Errorf("OtFireEventsService GetPage error:%s \r\n", err)
  45. return err
  46. }
  47. return nil
  48. }
  49. // Get 获取OtFireEvents对象
  50. func (e *OtFireEvents) Get(d *dto.OtFireEventsGetReq, p *actions.DataPermission, model *models.OtFireEvents) error {
  51. var data models.OtFireEvents
  52. err := e.Orm.Model(&data).
  53. Scopes(
  54. actions.Permission(data.TableName(), p),
  55. ).
  56. First(model, d.GetId()).Error
  57. if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
  58. err = errors.New("查看对象不存在或无权查看")
  59. e.Log.Errorf("Service GetOtFireEvents error:%s \r\n", err)
  60. return err
  61. }
  62. if err != nil {
  63. e.Log.Errorf("db error:%s", err)
  64. return err
  65. }
  66. return nil
  67. }
  68. // Insert 创建OtFireEvents对象
  69. func (e *OtFireEvents) Insert(c *dto.OtFireEventsInsertReq) error {
  70. var err error
  71. var data models.OtFireEvents
  72. c.Generate(&data)
  73. err = e.Orm.Create(&data).Error
  74. if err != nil {
  75. e.Log.Errorf("OtFireEventsService Insert error:%s \r\n", err)
  76. return err
  77. }
  78. return nil
  79. }
  80. // Update 修改OtFireEvents对象
  81. func (e *OtFireEvents) Update(c *dto.OtFireEventsUpdateReq, p *actions.DataPermission) error {
  82. var err error
  83. var data = models.OtFireEvents{}
  84. e.Orm.Scopes(
  85. actions.Permission(data.TableName(), p),
  86. ).First(&data, c.GetId())
  87. c.Generate(&data)
  88. db := e.Orm.Save(&data)
  89. if err = db.Error; err != nil {
  90. e.Log.Errorf("OtFireEventsService Save error:%s \r\n", err)
  91. return err
  92. }
  93. if db.RowsAffected == 0 {
  94. return errors.New("无权更新该数据")
  95. }
  96. return nil
  97. }
  98. // Remove 删除OtFireEvents
  99. func (e *OtFireEvents) Remove(d *dto.OtFireEventsDeleteReq, p *actions.DataPermission) error {
  100. var data models.OtFireEvents
  101. db := e.Orm.Model(&data).
  102. Scopes(
  103. actions.Permission(data.TableName(), p),
  104. ).Delete(&data, d.GetId())
  105. if err := db.Error; err != nil {
  106. e.Log.Errorf("Service RemoveOtFireEvents error:%s \r\n", err)
  107. return err
  108. }
  109. if db.RowsAffected == 0 {
  110. return errors.New("无权删除该数据")
  111. }
  112. return nil
  113. }
  114. // Notate 修改OtFireEvents对象
  115. func (e *OtFireEvents) Notate(c *dto.OtFireEventsUpdateReq, p *actions.DataPermission) error {
  116. var err error
  117. var data = models.OtFireEvents{}
  118. e.Orm.Scopes(
  119. actions.Permission(data.TableName(), p),
  120. ).First(&data, c.GetId())
  121. data.IsIgnore = c.IsIgnore
  122. data.IsKnow = c.IsKnow
  123. data.IsResolve = c.IsResolve
  124. db := e.Orm.Save(&data)
  125. if err = db.Error; err != nil {
  126. e.Log.Errorf("OtFireEventsService Notate error:%s \r\n", err)
  127. return err
  128. }
  129. if db.RowsAffected == 0 {
  130. return errors.New("无权更新该数据")
  131. }
  132. return nil
  133. }