ot_monitor_rules.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package service
  2. import (
  3. "errors"
  4. "github.com/go-admin-team/go-admin-core/sdk/service"
  5. "gorm.io/gorm"
  6. "go-admin/app/admin/models"
  7. "go-admin/app/admin/service/dto"
  8. "go-admin/common/actions"
  9. cDto "go-admin/common/dto"
  10. )
  11. type OtMonitorRules struct {
  12. service.Service
  13. }
  14. /*
  15. @@ 全局事件规则获取
  16. SELECT
  17. mr.*,
  18. ap.policy AS policy,
  19. ap.id AS policy_id
  20. FROM ot_monitor_rules mr
  21. LEFT JOIN ot_alert_policy ap ON mr.id = ap.rule_id AND ap.app_id = 999999
  22. Where ap.deleted_at is null
  23. Order by mr.id
  24. @@ 业务系统事件规则获取
  25. SELECT
  26. mr.*,
  27. ap.policy AS policy,
  28. ap.id AS policy_id
  29. FROM ot_monitor_rules as mr
  30. LEFT JOIN ot_alert_policy ap ON ap.rule_id = mr.id
  31. WHERE (ap.app_id = 1 and ap.deleted_at is null and mr.power =1 and mr.verify = 1) AND `mr`.`deleted_at` IS NULL ORDER BY mr.id LIMIT 20
  32. */
  33. // GetPage 获取OtMonitorRules列表
  34. func (e *OtMonitorRules) GetPage(c *dto.OtMonitorRulesGetPageReq, p *actions.DataPermission, list *[]models.OtMRAndAP, count *int64) error {
  35. // var err error
  36. var data models.OtMonitorRules
  37. db := e.Orm.Table("ot_monitor_rules as mr").
  38. Scopes(
  39. cDto.MakeCondition(c.GetNeedSearch()),
  40. cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  41. actions.Permission(data.TableName(), p),
  42. ).
  43. Select(`mr.*,
  44. ap.power AS ap_power,
  45. ap.policy AS policy,
  46. ap.id AS policy_id`)
  47. if c.AppID == 999999 {
  48. db = db.Joins("LEFT JOIN ot_alert_policy ap ON ap.rule_id = mr.id and ap.app_id = 999999").
  49. Where("ap.deleted_at is null")
  50. } else {
  51. db = db.Joins("LEFT JOIN ot_alert_policy ap ON ap.rule_id = mr.id").
  52. Where("ap.app_id = ? and ap.deleted_at is null and mr.power =1 and mr.verify = 1", c.AppID)
  53. }
  54. if err := db.Order("policy_id").Find(list).Limit(-1).Offset(-1).
  55. Count(count).Error; err != nil {
  56. e.Log.Errorf("OtMonitorRulesService GetPage error:%s \r\n", err)
  57. return err
  58. }
  59. return nil
  60. }
  61. func (e *OtMonitorRules) GetPageBak(c *dto.OtMonitorRulesGetPageReq, p *actions.DataPermission, list *[]models.OtMonitorRules, count *int64) error {
  62. var err error
  63. var data models.OtMonitorRules
  64. err = e.Orm.Model(&data).
  65. Scopes(
  66. cDto.MakeCondition(c.GetNeedSearch()),
  67. cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  68. actions.Permission(data.TableName(), p),
  69. ).
  70. Find(list).Limit(-1).Offset(-1).
  71. Count(count).Error
  72. if err != nil {
  73. e.Log.Errorf("OtMonitorRulesService GetPage error:%s \r\n", err)
  74. return err
  75. }
  76. return nil
  77. }
  78. // Get 获取OtMonitorRules对象
  79. func (e *OtMonitorRules) Get(d *dto.OtMonitorRulesGetReq, p *actions.DataPermission, model *models.OtMonitorRules) error {
  80. var data models.OtMonitorRules
  81. err := e.Orm.Model(&data).
  82. Scopes(
  83. actions.Permission(data.TableName(), p),
  84. ).
  85. First(model, d.GetId()).Error
  86. if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
  87. err = errors.New("查看对象不存在或无权查看")
  88. e.Log.Errorf("Service GetOtMonitorRules error:%s \r\n", err)
  89. return err
  90. }
  91. if err != nil {
  92. e.Log.Errorf("db error:%s", err)
  93. return err
  94. }
  95. return nil
  96. }
  97. // Insert 创建OtMonitorRules对象
  98. func (e *OtMonitorRules) Insert(c *dto.OtMonitorRulesInsertReq) error {
  99. var err error
  100. var data models.OtMonitorRules
  101. c.Generate(&data)
  102. err = e.Orm.Create(&data).Error
  103. if err != nil {
  104. e.Log.Errorf("OtMonitorRulesService Insert error:%s \r\n", err)
  105. return err
  106. }
  107. return nil
  108. }
  109. // Update 修改OtMonitorRules对象
  110. func (e *OtMonitorRules) Update(c *dto.OtMonitorRulesUpdateReq, p *actions.DataPermission) error {
  111. var err error
  112. var data = models.OtMonitorRules{}
  113. e.Orm.Scopes(
  114. actions.Permission(data.TableName(), p),
  115. ).First(&data, c.GetId())
  116. c.Generate(&data)
  117. db := e.Orm.Save(&data)
  118. if err = db.Error; err != nil {
  119. e.Log.Errorf("OtMonitorRulesService Save error:%s \r\n", err)
  120. return err
  121. }
  122. if db.RowsAffected == 0 {
  123. return errors.New("无权更新该数据")
  124. }
  125. return nil
  126. }
  127. // Remove 删除OtMonitorRules
  128. func (e *OtMonitorRules) Remove(d *dto.OtMonitorRulesDeleteReq, p *actions.DataPermission) error {
  129. var data models.OtMonitorRules
  130. db := e.Orm.Model(&data).
  131. Scopes(
  132. actions.Permission(data.TableName(), p),
  133. ).Delete(&data, d.GetId())
  134. if err := db.Error; err != nil {
  135. e.Log.Errorf("Service RemoveOtMonitorRules error:%s \r\n", err)
  136. return err
  137. }
  138. if db.RowsAffected == 0 {
  139. return errors.New("无权删除该数据")
  140. }
  141. return nil
  142. }