captureUintController.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package controller
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "iot_manager_service/app/device/dao"
  5. "iot_manager_service/app/device/model"
  6. "iot_manager_service/app/device/service"
  7. "iot_manager_service/app/middleware"
  8. "iot_manager_service/util"
  9. "math"
  10. "net/http"
  11. "strconv"
  12. )
  13. // 抓拍单元管理对象
  14. var CaptureUint = new(captureUintCtl)
  15. type captureUintCtl struct{}
  16. func (c *captureUintCtl) Detail(ctx *gin.Context) {
  17. id, e := strconv.Atoi(ctx.Query("id"))
  18. if e != nil {
  19. ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(e.Error(), nil))
  20. return
  21. }
  22. deviceType := ctx.Query("type")
  23. if deviceType == model.TypeCapture {
  24. device, err := service.CaptureUintService.GetCapture(id)
  25. if err != nil {
  26. ctx.JSON(http.StatusOK, err)
  27. return
  28. }
  29. ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, device))
  30. } else if deviceType == model.TypePoint {
  31. device, err := service.CaptureUintService.GetPoint(id)
  32. if err != nil {
  33. ctx.JSON(http.StatusOK, err)
  34. return
  35. }
  36. ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, device))
  37. } else {
  38. ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(model.TypeInvalid, nil))
  39. }
  40. }
  41. func (c *captureUintCtl) CaptureList(ctx *gin.Context) {
  42. searchValue := ctx.Query("searchValue")
  43. current, _ := strconv.Atoi(ctx.Query("current"))
  44. size, _ := strconv.Atoi(ctx.Query("size"))
  45. if current == 0 {
  46. current = 1
  47. }
  48. if size <= 0 || size > 100 {
  49. size = 10
  50. }
  51. devices, err := service.CaptureUintService.CaptureList(searchValue, current, size)
  52. if err != nil {
  53. ctx.JSON(http.StatusOK, err)
  54. return
  55. }
  56. pages := math.Ceil(float64(len(devices)) / float64(size))
  57. rsp := model.RspCaptureList{
  58. Current: current,
  59. Size: size,
  60. Total: len(devices),
  61. Pages: int(pages),
  62. Records: devices,
  63. }
  64. ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, rsp))
  65. }
  66. func (c *captureUintCtl) CaptureSubmit(ctx *gin.Context) {
  67. value, _ := ctx.Get(middleware.Authorization)
  68. claims := value.(*middleware.Claims)
  69. // 参数验证
  70. var req *model.CaptureDetail
  71. if err := ctx.ShouldBind(&req); err != nil {
  72. ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
  73. return
  74. }
  75. err := service.CaptureUintService.CaptureSubmit(claims.UserId, claims.TenantId, req)
  76. if err != nil {
  77. ctx.JSON(http.StatusOK, err)
  78. return
  79. }
  80. ctx.JSON(http.StatusOK, nil)
  81. }
  82. func (c *captureUintCtl) PointList(ctx *gin.Context) {
  83. searchValue := ctx.Query("searchValue")
  84. current, _ := strconv.Atoi(ctx.Query("current"))
  85. size, _ := strconv.Atoi(ctx.Query("size"))
  86. if current == 0 {
  87. current = 1
  88. }
  89. if size <= 0 || size > 100 {
  90. size = 10
  91. }
  92. devices, err := service.CaptureUintService.PointList(searchValue, current, size)
  93. if err != nil {
  94. ctx.JSON(http.StatusOK, err)
  95. return
  96. }
  97. pages := math.Ceil(float64(len(devices)) / float64(size))
  98. rsp := model.RspPointList{
  99. Current: current,
  100. Size: size,
  101. Total: len(devices),
  102. Pages: int(pages),
  103. Records: devices,
  104. }
  105. ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, rsp))
  106. }
  107. func (c *captureUintCtl) PointSubmit(ctx *gin.Context) {
  108. value, _ := ctx.Get(middleware.Authorization)
  109. claims := value.(*middleware.Claims)
  110. // 参数验证
  111. var req *dao.CheckPoint
  112. if err := ctx.ShouldBind(&req); err != nil {
  113. ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
  114. return
  115. }
  116. err := service.CaptureUintService.PointSubmit(claims.UserId, claims.TenantId, req)
  117. if err != nil {
  118. ctx.JSON(http.StatusOK, err)
  119. return
  120. }
  121. ctx.JSON(http.StatusOK, nil)
  122. }
  123. func (c *captureUintCtl) CaptureGetList(ctx *gin.Context) {
  124. value, _ := ctx.Get(middleware.Authorization)
  125. claims := value.(*middleware.Claims)
  126. devices, err := service.CaptureUintService.CaptureGetList(claims.TenantId)
  127. if err != nil {
  128. ctx.JSON(http.StatusOK, err)
  129. return
  130. }
  131. ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, devices))
  132. }
  133. func (c *captureUintCtl) PointGetList(ctx *gin.Context) {
  134. value, _ := ctx.Get(middleware.Authorization)
  135. claims := value.(*middleware.Claims)
  136. devices, err := service.CaptureUintService.PointGetList(claims.TenantId)
  137. if err != nil {
  138. ctx.JSON(http.StatusOK, err)
  139. return
  140. }
  141. ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, devices))
  142. }
  143. func (c *captureUintCtl) Remove(ctx *gin.Context) {
  144. value, _ := ctx.Get(middleware.Authorization)
  145. claims := value.(*middleware.Claims)
  146. var req *model.ReqCaptureRemove
  147. if err := ctx.ShouldBindJSON(&req); err != nil {
  148. ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
  149. return
  150. }
  151. if req.Type == model.TypeCapture {
  152. err := service.CaptureUintService.RemoveCapture(claims.UserId, req.IDs)
  153. if err != nil {
  154. ctx.JSON(http.StatusOK, err)
  155. return
  156. }
  157. ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, nil))
  158. } else if req.Type == model.TypePoint {
  159. err := service.CaptureUintService.RemovePoint(claims.UserId, req.IDs)
  160. if err != nil {
  161. ctx.JSON(http.StatusOK, err)
  162. return
  163. }
  164. ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, nil))
  165. } else {
  166. ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(model.TypeInvalid, nil))
  167. }
  168. }