123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- package service
- import (
- "errors"
- "github.com/go-admin-team/go-admin-core/sdk/service"
- "gorm.io/gorm"
- "go-admin/app/admin/models"
- "go-admin/app/admin/service/dto"
- "go-admin/common/actions"
- cDto "go-admin/common/dto"
- "go-admin/utils"
- )
- type OtAlertPolicy struct {
- service.Service
- }
- // GetPage 获取OtAlertPolicy列表
- func (e *OtAlertPolicy) GetPage(c *dto.OtAlertPolicyGetPageReq, p *actions.DataPermission, list *[]models.OtAlertPolicy, count *int64) error {
- var err error
- var data models.OtAlertPolicy
- err = e.Orm.Model(&data).
- Scopes(
- cDto.MakeCondition(c.GetNeedSearch()),
- cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
- actions.Permission(data.TableName(), p),
- ).
- Find(list).Limit(-1).Offset(-1).
- Count(count).Error
- if err != nil {
- e.Log.Errorf("OtAlertPolicyService GetPage error:%s \r\n", err)
- return err
- }
- return nil
- }
- // Get 获取OtAlertPolicy对象
- func (e *OtAlertPolicy) Get(d *dto.OtAlertPolicyGetReq, p *actions.DataPermission, model *models.OtAlertPolicy) error {
- var data models.OtAlertPolicy
- err := e.Orm.Model(&data).
- Scopes(
- actions.Permission(data.TableName(), p),
- ).
- First(model, d.GetId()).Error
- if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
- err = errors.New("查看对象不存在或无权查看")
- e.Log.Errorf("Service GetOtAlertPolicy error:%s \r\n", err)
- return err
- }
- if err != nil {
- e.Log.Errorf("db error:%s", err)
- return err
- }
- return nil
- }
- // Insert 创建OtAlertPolicy对象
- func (e *OtAlertPolicy) Insert(c *dto.OtAlertPolicyInsertReq) error {
- var err error
- var data models.OtAlertPolicy
- c.Generate(&data)
- err = e.Orm.Create(&data).Error
- if err != nil {
- if utils.MysqlErrorHandler(err) == gorm.ErrDuplicatedKey {
- data.Policy = ""
- if err = e.Orm.Create(&data).Error; err != nil {
- e.Log.Errorf("OtAlertPolicyService double insert error:%s \r\n", err)
- return utils.MysqlErrorHandler(err)
- }
- }
- e.Log.Errorf("OtAlertPolicyService insert error:%s \r\n", err)
- return utils.MysqlErrorHandler(err)
- }
- return nil
- }
- // Update 修改OtAlertPolicy对象
- func (e *OtAlertPolicy) Update(c *dto.OtAlertPolicyUpdateReq, p *actions.DataPermission) error {
- var err error
- var data = models.OtAlertPolicy{}
- e.Orm.Scopes(
- actions.Permission(data.TableName(), p),
- ).First(&data, c.GetId())
- c.Generate(&data)
- db := e.Orm.Save(&data)
- if err = db.Error; err != nil {
- e.Log.Errorf("OtAlertPolicyService Save error:%s \r\n", err)
- return err
- }
- if db.RowsAffected == 0 {
- return errors.New("无权更新该数据")
- }
- return nil
- }
- // Remove 删除OtAlertPolicy
- func (e *OtAlertPolicy) Remove(d *dto.OtAlertPolicyDeleteReq, p *actions.DataPermission) error {
- var data models.OtAlertPolicy
- db := e.Orm.Model(&data).
- Scopes(
- actions.Permission(data.TableName(), p),
- ).Delete(&data, d.GetId())
- if err := db.Error; err != nil {
- e.Log.Errorf("Service RemoveOtAlertPolicy error:%s \r\n", err)
- return err
- }
- if db.RowsAffected == 0 {
- return errors.New("无权删除该数据")
- }
- return nil
- }
- /*
- 获取当前可以导入的规则
- Select
- mr.*,
- ap.policy AS policy
- From ot_monitor_rules mr
- LEFT JOIN ot_alert_policy ap ON ap.rule_id = mr.id and ap.app_id = 999999
- where mr.id not in (select rule_id from ot_alert_policy where app_id = 1 group by rule_id)
- and ap.deleted_at is null and mr.power =1 and mr.verify = 1 AND `mr`.`deleted_at` IS NULL
- */
- func (e *OtAlertPolicy) ImportPolicy(c *dto.OtAlertPolicyImportReq, p *actions.DataPermission, count *int64) error {
- var err error
- e.Log.Debug("Import policys to app: ", c.AppId)
- ruleList := make([]models.OtMRAndAP, 0)
- err = e.Orm.Table("ot_monitor_rules as mr").
- Select(`mr.*,
- ap.policy AS policy`).
- Joins("LEFT JOIN ot_alert_policy ap ON ap.rule_id = mr.id and ap.app_id = 999999").
- Where("mr.id not in (select rule_id from ot_alert_policy where app_id = ? group by rule_id)", c.AppId).
- Where("ap.deleted_at is null and mr.power =1 and mr.verify = 1 ").
- Find(&ruleList).Limit(-1).Offset(-1).Error
- if err != nil {
- e.Log.Errorf("Get all rules error:%s \r\n", err.Error())
- return err
- }
- list := make([]models.OtAlertPolicy, 0)
- for _, v := range ruleList {
- ap := models.OtAlertPolicy{
- AppId: c.AppId,
- RuleId: int64(v.Id),
- Policy: v.Policy,
- Power: 0,
- }
- list = append(list, ap)
- }
- var data models.OtAlertPolicy
- result := e.Orm.Model(&data).Create(&list)
- if result.Error != nil {
- e.Log.Errorf("Get all rules error:%s \r\n", err)
- return err
- }
- *count = result.RowsAffected
- e.Log.Debug("插入成功的记录数:", count)
- return nil
- }
|