captureUintController.go 4.4 KB

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