123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- 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"
- )
- type OtMonitorRules struct {
- service.Service
- }
- /*
- @@ 全局事件规则获取
- SELECT
- mr.*,
- ap.policy AS policy,
- ap.id AS policy_id
- FROM ot_monitor_rules mr
- LEFT JOIN ot_alert_policy ap ON mr.id = ap.rule_id AND ap.app_id = 999999
- Where ap.deleted_at is null
- Order by mr.id
- @@ 业务系统事件规则获取
- SELECT
- mr.*,
- ap.policy AS policy,
- ap.id AS policy_id
- FROM ot_monitor_rules as mr
- LEFT JOIN ot_alert_policy ap ON ap.rule_id = mr.id
- 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
- */
- // GetPage 获取OtMonitorRules列表
- func (e *OtMonitorRules) GetPage(c *dto.OtMonitorRulesGetPageReq, p *actions.DataPermission, list *[]models.OtMRAndAP, count *int64) error {
- // var err error
- var data models.OtMonitorRules
- db := e.Orm.Table("ot_monitor_rules as mr").
- Scopes(
- cDto.MakeCondition(c.GetNeedSearch()),
- cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
- actions.Permission(data.TableName(), p),
- ).
- Select(`mr.*,
- ap.power AS ap_power,
- ap.policy AS policy,
- ap.id AS policy_id`)
- if c.AppID == 999999 {
- db = db.Joins("LEFT JOIN ot_alert_policy ap ON ap.rule_id = mr.id and ap.app_id = 999999").
- Where("ap.deleted_at is null")
- } else {
- db = db.Joins("LEFT JOIN ot_alert_policy ap ON ap.rule_id = mr.id").
- Where("ap.app_id = ? and ap.deleted_at is null and mr.power =1 and mr.verify = 1", c.AppID)
- }
- if err := db.Order("policy_id").Find(list).Limit(-1).Offset(-1).
- Count(count).Error; err != nil {
- e.Log.Errorf("OtMonitorRulesService GetPage error:%s \r\n", err)
- return err
- }
- return nil
- }
- func (e *OtMonitorRules) GetPageBak(c *dto.OtMonitorRulesGetPageReq, p *actions.DataPermission, list *[]models.OtMonitorRules, count *int64) error {
- var err error
- var data models.OtMonitorRules
- 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("OtMonitorRulesService GetPage error:%s \r\n", err)
- return err
- }
- return nil
- }
- // Get 获取OtMonitorRules对象
- func (e *OtMonitorRules) Get(d *dto.OtMonitorRulesGetReq, p *actions.DataPermission, model *models.OtMonitorRules) error {
- var data models.OtMonitorRules
- 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 GetOtMonitorRules error:%s \r\n", err)
- return err
- }
- if err != nil {
- e.Log.Errorf("db error:%s", err)
- return err
- }
- return nil
- }
- // Insert 创建OtMonitorRules对象
- func (e *OtMonitorRules) Insert(c *dto.OtMonitorRulesInsertReq) error {
- var err error
- var data models.OtMonitorRules
- c.Generate(&data)
- err = e.Orm.Create(&data).Error
- if err != nil {
- e.Log.Errorf("OtMonitorRulesService Insert error:%s \r\n", err)
- return err
- }
- return nil
- }
- // Update 修改OtMonitorRules对象
- func (e *OtMonitorRules) Update(c *dto.OtMonitorRulesUpdateReq, p *actions.DataPermission) error {
- var err error
- var data = models.OtMonitorRules{}
- 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("OtMonitorRulesService Save error:%s \r\n", err)
- return err
- }
- if db.RowsAffected == 0 {
- return errors.New("无权更新该数据")
- }
- return nil
- }
- // Remove 删除OtMonitorRules
- func (e *OtMonitorRules) Remove(d *dto.OtMonitorRulesDeleteReq, p *actions.DataPermission) error {
- var data models.OtMonitorRules
- 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 RemoveOtMonitorRules error:%s \r\n", err)
- return err
- }
- if db.RowsAffected == 0 {
- return errors.New("无权删除该数据")
- }
- return nil
- }
|