123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- package apis
- import (
- "fmt"
- "github.com/gin-gonic/gin"
- "github.com/go-admin-team/go-admin-core/sdk/api"
- "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth/user"
- _ "github.com/go-admin-team/go-admin-core/sdk/pkg/response"
- "gorm.io/gorm"
- "go-admin/app/admin/models"
- "go-admin/app/admin/service"
- "go-admin/app/admin/service/dto"
- "go-admin/common/actions"
- )
- type OtAlertPolicy struct {
- api.Api
- }
- // GetPage 获取OtAlertPolicy列表
- // @Summary 获取OtAlertPolicy列表
- // @Description 获取OtAlertPolicy列表
- // @Tags OtAlertPolicy
- // @Param appId query int64 false "业务ID"
- // @Param ruleId query int64 false "监控规则ID"
- // @Param monitorAlias query string false "监控项别名"
- // @Param power query string false "是否启用"
- // @Param pageSize query int false "页条数"
- // @Param pageIndex query int false "页码"
- // @Success 200 {object} response.Response{data=response.Page{list=[]models.OtAlertPolicy}} "{"code": 200, "data": [...]}"
- // @Router /api/v1/ot-alert-policy [get]
- // @Security Bearer
- func (e OtAlertPolicy) GetPage(c *gin.Context) {
- req := dto.OtAlertPolicyGetPageReq{}
- s := service.OtAlertPolicy{}
- err := e.MakeContext(c).
- MakeOrm().
- Bind(&req).
- MakeService(&s.Service).
- Errors
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, err.Error())
- return
- }
- p := actions.GetPermissionFromContext(c)
- list := make([]models.OtAlertPolicy, 0)
- var count int64
- err = s.GetPage(&req, p, &list, &count)
- if err != nil {
- e.Error(500, err, fmt.Sprintf("获取OtAlertPolicy失败,\r\n失败信息 %s", err.Error()))
- return
- }
- e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
- }
- // Get 获取OtAlertPolicy
- // @Summary 获取OtAlertPolicy
- // @Description 获取OtAlertPolicy
- // @Tags OtAlertPolicy
- // @Param id path int false "id"
- // @Success 200 {object} response.Response{data=models.OtAlertPolicy} "{"code": 200, "data": [...]}"
- // @Router /api/v1/ot-alert-policy/{id} [get]
- // @Security Bearer
- func (e OtAlertPolicy) Get(c *gin.Context) {
- req := dto.OtAlertPolicyGetReq{}
- s := service.OtAlertPolicy{}
- err := e.MakeContext(c).
- MakeOrm().
- Bind(&req).
- MakeService(&s.Service).
- Errors
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, err.Error())
- return
- }
- var object models.OtAlertPolicy
- p := actions.GetPermissionFromContext(c)
- err = s.Get(&req, p, &object)
- if err != nil {
- e.Error(500, err, fmt.Sprintf("获取OtAlertPolicy失败,\r\n失败信息 %s", err.Error()))
- return
- }
- e.OK(object, "查询成功")
- }
- // Insert 创建OtAlertPolicy
- // @Summary 创建OtAlertPolicy
- // @Description 创建OtAlertPolicy
- // @Tags OtAlertPolicy
- // @Accept application/json
- // @Product application/json
- // @Param data body dto.OtAlertPolicyInsertReq true "data"
- // @Success 200 {object} response.Response "{"code": 200, "message": "添加成功"}"
- // @Router /api/v1/ot-alert-policy [post]
- // @Security Bearer
- func (e OtAlertPolicy) Insert(c *gin.Context) {
- req := dto.OtAlertPolicyInsertReq{}
- s := service.OtAlertPolicy{}
- err := e.MakeContext(c).
- MakeOrm().
- Bind(&req).
- MakeService(&s.Service).
- Errors
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, err.Error())
- return
- }
- // 设置创建人
- req.SetCreateBy(user.GetUserId(c))
- err = s.Insert(&req)
- if err != nil {
- if err == gorm.ErrDuplicatedKey {
- e.Error(500, err, fmt.Sprintf("相同策略的事件已存在,请勿重复添加"))
- return
- }
- e.Error(500, err, fmt.Sprintf("创建OtAlertPolicy失败,\r\n失败信息 %s", err.Error()))
- return
- }
- e.OK(req.GetId(), "创建成功")
- }
- // Update 修改OtAlertPolicy
- // @Summary 修改OtAlertPolicy
- // @Description 修改OtAlertPolicy
- // @Tags OtAlertPolicy
- // @Accept application/json
- // @Product application/json
- // @Param id path int true "id"
- // @Param data body dto.OtAlertPolicyUpdateReq true "body"
- // @Success 200 {object} response.Response "{"code": 200, "message": "修改成功"}"
- // @Router /api/v1/ot-alert-policy/{id} [put]
- // @Security Bearer
- func (e OtAlertPolicy) Update(c *gin.Context) {
- req := dto.OtAlertPolicyUpdateReq{}
- s := service.OtAlertPolicy{}
- err := e.MakeContext(c).
- MakeOrm().
- Bind(&req).
- MakeService(&s.Service).
- Errors
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, err.Error())
- return
- }
- req.SetUpdateBy(user.GetUserId(c))
- p := actions.GetPermissionFromContext(c)
- err = s.Update(&req, p)
- if err != nil {
- if err == gorm.ErrDuplicatedKey {
- e.Error(500, err, fmt.Sprintf("相同策略的事件已存在,请勿重复添加"))
- return
- }
- e.Error(500, err, fmt.Sprintf("修改OtAlertPolicy失败,\r\n失败信息 %s", err.Error()))
- return
- }
- e.OK(req.GetId(), "修改成功")
- }
- // Delete 删除OtAlertPolicy
- // @Summary 删除OtAlertPolicy
- // @Description 删除OtAlertPolicy
- // @Tags OtAlertPolicy
- // @Param data body dto.OtAlertPolicyDeleteReq true "body"
- // @Success 200 {object} response.Response "{"code": 200, "message": "删除成功"}"
- // @Router /api/v1/ot-alert-policy [delete]
- // @Security Bearer
- func (e OtAlertPolicy) Delete(c *gin.Context) {
- s := service.OtAlertPolicy{}
- req := dto.OtAlertPolicyDeleteReq{}
- err := e.MakeContext(c).
- MakeOrm().
- Bind(&req).
- MakeService(&s.Service).
- Errors
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, err.Error())
- return
- }
- // req.SetUpdateBy(user.GetUserId(c))
- p := actions.GetPermissionFromContext(c)
- err = s.Remove(&req, p)
- if err != nil {
- e.Error(500, err, fmt.Sprintf("删除OtAlertPolicy失败,\r\n失败信息 %s", err.Error()))
- return
- }
- e.OK(req.GetId(), "删除成功")
- }
- func (e OtAlertPolicy) SetPolicy(c *gin.Context) {
- req := dto.OtAlertPolicyImportReq{}
- s := service.OtAlertPolicy{}
- err := e.MakeContext(c).
- MakeOrm().
- Bind(&req).
- MakeService(&s.Service).Errors
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, err.Error())
- return
- }
- p := actions.GetPermissionFromContext(c)
- var count int64
- err = s.ImportPolicy(&req, p, &count)
- if err != nil {
- e.Error(500, err, fmt.Sprintf("ImportPolicy失败 %s", err.Error()))
- return
- }
- // e.Logger.Debug("count:", count)
- e.OK(count, fmt.Sprintf("成功导入%d条策略", count))
- }
|