ot_alert_policy.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package apis
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "github.com/go-admin-team/go-admin-core/sdk/api"
  6. "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth/user"
  7. _ "github.com/go-admin-team/go-admin-core/sdk/pkg/response"
  8. "gorm.io/gorm"
  9. "go-admin/app/admin/models"
  10. "go-admin/app/admin/service"
  11. "go-admin/app/admin/service/dto"
  12. "go-admin/common/actions"
  13. )
  14. type OtAlertPolicy struct {
  15. api.Api
  16. }
  17. // GetPage 获取OtAlertPolicy列表
  18. // @Summary 获取OtAlertPolicy列表
  19. // @Description 获取OtAlertPolicy列表
  20. // @Tags OtAlertPolicy
  21. // @Param appId query int64 false "业务ID"
  22. // @Param ruleId query int64 false "监控规则ID"
  23. // @Param monitorAlias query string false "监控项别名"
  24. // @Param power query string false "是否启用"
  25. // @Param pageSize query int false "页条数"
  26. // @Param pageIndex query int false "页码"
  27. // @Success 200 {object} response.Response{data=response.Page{list=[]models.OtAlertPolicy}} "{"code": 200, "data": [...]}"
  28. // @Router /api/v1/ot-alert-policy [get]
  29. // @Security Bearer
  30. func (e OtAlertPolicy) GetPage(c *gin.Context) {
  31. req := dto.OtAlertPolicyGetPageReq{}
  32. s := service.OtAlertPolicy{}
  33. err := e.MakeContext(c).
  34. MakeOrm().
  35. Bind(&req).
  36. MakeService(&s.Service).
  37. Errors
  38. if err != nil {
  39. e.Logger.Error(err)
  40. e.Error(500, err, err.Error())
  41. return
  42. }
  43. p := actions.GetPermissionFromContext(c)
  44. list := make([]models.OtAlertPolicy, 0)
  45. var count int64
  46. err = s.GetPage(&req, p, &list, &count)
  47. if err != nil {
  48. e.Error(500, err, fmt.Sprintf("获取OtAlertPolicy失败,\r\n失败信息 %s", err.Error()))
  49. return
  50. }
  51. e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
  52. }
  53. // Get 获取OtAlertPolicy
  54. // @Summary 获取OtAlertPolicy
  55. // @Description 获取OtAlertPolicy
  56. // @Tags OtAlertPolicy
  57. // @Param id path int false "id"
  58. // @Success 200 {object} response.Response{data=models.OtAlertPolicy} "{"code": 200, "data": [...]}"
  59. // @Router /api/v1/ot-alert-policy/{id} [get]
  60. // @Security Bearer
  61. func (e OtAlertPolicy) Get(c *gin.Context) {
  62. req := dto.OtAlertPolicyGetReq{}
  63. s := service.OtAlertPolicy{}
  64. err := e.MakeContext(c).
  65. MakeOrm().
  66. Bind(&req).
  67. MakeService(&s.Service).
  68. Errors
  69. if err != nil {
  70. e.Logger.Error(err)
  71. e.Error(500, err, err.Error())
  72. return
  73. }
  74. var object models.OtAlertPolicy
  75. p := actions.GetPermissionFromContext(c)
  76. err = s.Get(&req, p, &object)
  77. if err != nil {
  78. e.Error(500, err, fmt.Sprintf("获取OtAlertPolicy失败,\r\n失败信息 %s", err.Error()))
  79. return
  80. }
  81. e.OK(object, "查询成功")
  82. }
  83. // Insert 创建OtAlertPolicy
  84. // @Summary 创建OtAlertPolicy
  85. // @Description 创建OtAlertPolicy
  86. // @Tags OtAlertPolicy
  87. // @Accept application/json
  88. // @Product application/json
  89. // @Param data body dto.OtAlertPolicyInsertReq true "data"
  90. // @Success 200 {object} response.Response "{"code": 200, "message": "添加成功"}"
  91. // @Router /api/v1/ot-alert-policy [post]
  92. // @Security Bearer
  93. func (e OtAlertPolicy) Insert(c *gin.Context) {
  94. req := dto.OtAlertPolicyInsertReq{}
  95. s := service.OtAlertPolicy{}
  96. err := e.MakeContext(c).
  97. MakeOrm().
  98. Bind(&req).
  99. MakeService(&s.Service).
  100. Errors
  101. if err != nil {
  102. e.Logger.Error(err)
  103. e.Error(500, err, err.Error())
  104. return
  105. }
  106. // 设置创建人
  107. req.SetCreateBy(user.GetUserId(c))
  108. err = s.Insert(&req)
  109. if err != nil {
  110. if err == gorm.ErrDuplicatedKey {
  111. e.Error(500, err, fmt.Sprintf("相同策略的事件已存在,请勿重复添加"))
  112. return
  113. }
  114. e.Error(500, err, fmt.Sprintf("创建OtAlertPolicy失败,\r\n失败信息 %s", err.Error()))
  115. return
  116. }
  117. e.OK(req.GetId(), "创建成功")
  118. }
  119. // Update 修改OtAlertPolicy
  120. // @Summary 修改OtAlertPolicy
  121. // @Description 修改OtAlertPolicy
  122. // @Tags OtAlertPolicy
  123. // @Accept application/json
  124. // @Product application/json
  125. // @Param id path int true "id"
  126. // @Param data body dto.OtAlertPolicyUpdateReq true "body"
  127. // @Success 200 {object} response.Response "{"code": 200, "message": "修改成功"}"
  128. // @Router /api/v1/ot-alert-policy/{id} [put]
  129. // @Security Bearer
  130. func (e OtAlertPolicy) Update(c *gin.Context) {
  131. req := dto.OtAlertPolicyUpdateReq{}
  132. s := service.OtAlertPolicy{}
  133. err := e.MakeContext(c).
  134. MakeOrm().
  135. Bind(&req).
  136. MakeService(&s.Service).
  137. Errors
  138. if err != nil {
  139. e.Logger.Error(err)
  140. e.Error(500, err, err.Error())
  141. return
  142. }
  143. req.SetUpdateBy(user.GetUserId(c))
  144. p := actions.GetPermissionFromContext(c)
  145. err = s.Update(&req, p)
  146. if err != nil {
  147. if err == gorm.ErrDuplicatedKey {
  148. e.Error(500, err, fmt.Sprintf("相同策略的事件已存在,请勿重复添加"))
  149. return
  150. }
  151. e.Error(500, err, fmt.Sprintf("修改OtAlertPolicy失败,\r\n失败信息 %s", err.Error()))
  152. return
  153. }
  154. e.OK(req.GetId(), "修改成功")
  155. }
  156. // Delete 删除OtAlertPolicy
  157. // @Summary 删除OtAlertPolicy
  158. // @Description 删除OtAlertPolicy
  159. // @Tags OtAlertPolicy
  160. // @Param data body dto.OtAlertPolicyDeleteReq true "body"
  161. // @Success 200 {object} response.Response "{"code": 200, "message": "删除成功"}"
  162. // @Router /api/v1/ot-alert-policy [delete]
  163. // @Security Bearer
  164. func (e OtAlertPolicy) Delete(c *gin.Context) {
  165. s := service.OtAlertPolicy{}
  166. req := dto.OtAlertPolicyDeleteReq{}
  167. err := e.MakeContext(c).
  168. MakeOrm().
  169. Bind(&req).
  170. MakeService(&s.Service).
  171. Errors
  172. if err != nil {
  173. e.Logger.Error(err)
  174. e.Error(500, err, err.Error())
  175. return
  176. }
  177. // req.SetUpdateBy(user.GetUserId(c))
  178. p := actions.GetPermissionFromContext(c)
  179. err = s.Remove(&req, p)
  180. if err != nil {
  181. e.Error(500, err, fmt.Sprintf("删除OtAlertPolicy失败,\r\n失败信息 %s", err.Error()))
  182. return
  183. }
  184. e.OK(req.GetId(), "删除成功")
  185. }
  186. func (e OtAlertPolicy) SetPolicy(c *gin.Context) {
  187. req := dto.OtAlertPolicyImportReq{}
  188. s := service.OtAlertPolicy{}
  189. err := e.MakeContext(c).
  190. MakeOrm().
  191. Bind(&req).
  192. MakeService(&s.Service).Errors
  193. if err != nil {
  194. e.Logger.Error(err)
  195. e.Error(500, err, err.Error())
  196. return
  197. }
  198. p := actions.GetPermissionFromContext(c)
  199. var count int64
  200. err = s.ImportPolicy(&req, p, &count)
  201. if err != nil {
  202. e.Error(500, err, fmt.Sprintf("ImportPolicy失败 %s", err.Error()))
  203. return
  204. }
  205. // e.Logger.Debug("count:", count)
  206. e.OK(count, fmt.Sprintf("成功导入%d条策略", count))
  207. }