noticeSetContoller.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package controller
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "iot_manager_service/app/middleware"
  5. "iot_manager_service/app/warn/model"
  6. "iot_manager_service/app/warn/service"
  7. "iot_manager_service/util/common"
  8. "math"
  9. "net/http"
  10. "strconv"
  11. )
  12. // NoticeSet 告警设置
  13. var NoticeSet = new(noticeSetCtl)
  14. type noticeSetCtl struct{}
  15. func (c noticeSetCtl) List(ctx *gin.Context) {
  16. value, _ := ctx.Get(middleware.Authorization)
  17. claims := value.(*middleware.Claims)
  18. current, _ := strconv.Atoi(ctx.Query("current"))
  19. size, _ := strconv.Atoi(ctx.Query("size"))
  20. searchValue := ctx.Query("searchValue")
  21. if current == 0 {
  22. current = 1
  23. }
  24. if size <= 0 || size > 100 {
  25. size = 10
  26. }
  27. records, err := service.NoticeSetService.GetList(claims.TenantId, searchValue)
  28. if err != nil {
  29. ctx.JSON(http.StatusBadRequest, err)
  30. return
  31. }
  32. pages := math.Ceil(float64(len(records)) / float64(size))
  33. rsp := model.ResposeNoticeSetRecords{
  34. Current: current,
  35. Size: size,
  36. Total: len(records),
  37. Pages: int(pages),
  38. Records: records,
  39. }
  40. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, rsp))
  41. }
  42. func (c noticeSetCtl) GetUserList(ctx *gin.Context) {
  43. current, _ := strconv.Atoi(ctx.Query("current"))
  44. size, _ := strconv.Atoi(ctx.Query("size"))
  45. account := ctx.Query("account")
  46. realName := ctx.Query("realName")
  47. if current == 0 {
  48. current = 1
  49. }
  50. if size <= 0 || size > 100 {
  51. size = 10
  52. }
  53. users, err := service.NoticeSetService.UserList(account, realName, current, size)
  54. if err != nil {
  55. ctx.JSON(http.StatusBadRequest, err)
  56. return
  57. }
  58. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, users))
  59. }
  60. func (c noticeSetCtl) Detail(ctx *gin.Context) {
  61. id := ctx.Query("id")
  62. data, err := service.NoticeSetService.GetById(id)
  63. if err != nil {
  64. ctx.JSON(http.StatusOK, err)
  65. return
  66. }
  67. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, data))
  68. }
  69. func (c noticeSetCtl) Update(ctx *gin.Context) {
  70. value, _ := ctx.Get(middleware.Authorization)
  71. claims := value.(*middleware.Claims)
  72. post := model.NoticeUpdateData{}
  73. err := ctx.ShouldBindJSON(&post)
  74. if err != nil {
  75. ctx.JSON(http.StatusBadRequest, err)
  76. return
  77. }
  78. err = service.NoticeSetService.Update(post, claims.TenantId)
  79. if err != nil {
  80. ctx.JSON(http.StatusBadRequest, err)
  81. return
  82. }
  83. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, nil))
  84. }
  85. func (c noticeSetCtl) Remove(ctx *gin.Context) {
  86. value, _ := ctx.Get(middleware.Authorization)
  87. claims := value.(*middleware.Claims)
  88. id := ctx.Query("ids")
  89. err := service.NoticeSetService.Remove(id, claims.TenantId)
  90. if err != nil {
  91. ctx.JSON(http.StatusBadRequest, err)
  92. return
  93. }
  94. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, nil))
  95. }