ot_alert_policy.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. "go-admin/utils"
  11. )
  12. type OtAlertPolicy struct {
  13. service.Service
  14. }
  15. // GetPage 获取OtAlertPolicy列表
  16. func (e *OtAlertPolicy) GetPage(c *dto.OtAlertPolicyGetPageReq, p *actions.DataPermission, list *[]models.OtAlertPolicy, count *int64) error {
  17. var err error
  18. var data models.OtAlertPolicy
  19. err = e.Orm.Model(&data).
  20. Scopes(
  21. cDto.MakeCondition(c.GetNeedSearch()),
  22. cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  23. actions.Permission(data.TableName(), p),
  24. ).
  25. Find(list).Limit(-1).Offset(-1).
  26. Count(count).Error
  27. if err != nil {
  28. e.Log.Errorf("OtAlertPolicyService GetPage error:%s \r\n", err)
  29. return err
  30. }
  31. return nil
  32. }
  33. // Get 获取OtAlertPolicy对象
  34. func (e *OtAlertPolicy) Get(d *dto.OtAlertPolicyGetReq, p *actions.DataPermission, model *models.OtAlertPolicy) error {
  35. var data models.OtAlertPolicy
  36. err := e.Orm.Model(&data).
  37. Scopes(
  38. actions.Permission(data.TableName(), p),
  39. ).
  40. First(model, d.GetId()).Error
  41. if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
  42. err = errors.New("查看对象不存在或无权查看")
  43. e.Log.Errorf("Service GetOtAlertPolicy error:%s \r\n", err)
  44. return err
  45. }
  46. if err != nil {
  47. e.Log.Errorf("db error:%s", err)
  48. return err
  49. }
  50. return nil
  51. }
  52. // Insert 创建OtAlertPolicy对象
  53. func (e *OtAlertPolicy) Insert(c *dto.OtAlertPolicyInsertReq) error {
  54. var err error
  55. var data models.OtAlertPolicy
  56. c.Generate(&data)
  57. err = e.Orm.Create(&data).Error
  58. if err != nil {
  59. if utils.MysqlErrorHandler(err) == gorm.ErrDuplicatedKey {
  60. data.Policy = ""
  61. if err = e.Orm.Create(&data).Error; err != nil {
  62. e.Log.Errorf("OtAlertPolicyService double insert error:%s \r\n", err)
  63. return utils.MysqlErrorHandler(err)
  64. }
  65. }
  66. e.Log.Errorf("OtAlertPolicyService insert error:%s \r\n", err)
  67. return utils.MysqlErrorHandler(err)
  68. }
  69. return nil
  70. }
  71. // Update 修改OtAlertPolicy对象
  72. func (e *OtAlertPolicy) Update(c *dto.OtAlertPolicyUpdateReq, p *actions.DataPermission) error {
  73. var err error
  74. var data = models.OtAlertPolicy{}
  75. e.Orm.Scopes(
  76. actions.Permission(data.TableName(), p),
  77. ).First(&data, c.GetId())
  78. c.Generate(&data)
  79. db := e.Orm.Save(&data)
  80. if err = db.Error; err != nil {
  81. e.Log.Errorf("OtAlertPolicyService Save error:%s \r\n", err)
  82. return err
  83. }
  84. if db.RowsAffected == 0 {
  85. return errors.New("无权更新该数据")
  86. }
  87. return nil
  88. }
  89. // Remove 删除OtAlertPolicy
  90. func (e *OtAlertPolicy) Remove(d *dto.OtAlertPolicyDeleteReq, p *actions.DataPermission) error {
  91. var data models.OtAlertPolicy
  92. db := e.Orm.Model(&data).
  93. Scopes(
  94. actions.Permission(data.TableName(), p),
  95. ).Delete(&data, d.GetId())
  96. if err := db.Error; err != nil {
  97. e.Log.Errorf("Service RemoveOtAlertPolicy error:%s \r\n", err)
  98. return err
  99. }
  100. if db.RowsAffected == 0 {
  101. return errors.New("无权删除该数据")
  102. }
  103. return nil
  104. }
  105. /*
  106. 获取当前可以导入的规则
  107. Select
  108. mr.*,
  109. ap.policy AS policy
  110. From ot_monitor_rules mr
  111. LEFT JOIN ot_alert_policy ap ON ap.rule_id = mr.id and ap.app_id = 999999
  112. where mr.id not in (select rule_id from ot_alert_policy where app_id = 1 group by rule_id)
  113. and ap.deleted_at is null and mr.power =1 and mr.verify = 1 AND `mr`.`deleted_at` IS NULL
  114. */
  115. func (e *OtAlertPolicy) ImportPolicy(c *dto.OtAlertPolicyImportReq, p *actions.DataPermission, count *int64) error {
  116. var err error
  117. e.Log.Debug("Import policys to app: ", c.AppId)
  118. ruleList := make([]models.OtMRAndAP, 0)
  119. err = e.Orm.Table("ot_monitor_rules as mr").
  120. Select(`mr.*,
  121. ap.policy AS policy`).
  122. Joins("LEFT JOIN ot_alert_policy ap ON ap.rule_id = mr.id and ap.app_id = 999999").
  123. Where("mr.id not in (select rule_id from ot_alert_policy where app_id = ? group by rule_id)", c.AppId).
  124. Where("ap.deleted_at is null and mr.power =1 and mr.verify = 1 ").
  125. Find(&ruleList).Limit(-1).Offset(-1).Error
  126. if err != nil {
  127. e.Log.Errorf("Get all rules error:%s \r\n", err.Error())
  128. return err
  129. }
  130. list := make([]models.OtAlertPolicy, 0)
  131. for _, v := range ruleList {
  132. ap := models.OtAlertPolicy{
  133. AppId: c.AppId,
  134. RuleId: int64(v.Id),
  135. Policy: v.Policy,
  136. Power: 0,
  137. }
  138. list = append(list, ap)
  139. }
  140. var data models.OtAlertPolicy
  141. result := e.Orm.Model(&data).Create(&list)
  142. if result.Error != nil {
  143. e.Log.Errorf("Get all rules error:%s \r\n", err)
  144. return err
  145. }
  146. *count = result.RowsAffected
  147. e.Log.Debug("插入成功的记录数:", count)
  148. return nil
  149. }