cameraController.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package controller
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "iot_manager_service/app/device/model"
  5. "iot_manager_service/app/device/service"
  6. "iot_manager_service/app/middleware"
  7. "iot_manager_service/util"
  8. "math"
  9. "net/http"
  10. "strconv"
  11. )
  12. // 摄像头管理对象
  13. var Camera = new(cameraCtl)
  14. type cameraCtl struct{}
  15. func (c *cameraCtl) 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. device, err := service.CameraService.Get(id)
  22. if err != nil {
  23. ctx.JSON(http.StatusOK, err)
  24. return
  25. }
  26. ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, device))
  27. }
  28. func (c *cameraCtl) List(ctx *gin.Context) {
  29. searchValue := ctx.Query("searchValue")
  30. cameraType := ctx.Query("cameraType")
  31. current, _ := strconv.Atoi(ctx.Query("current"))
  32. size, _ := strconv.Atoi(ctx.Query("size"))
  33. if current == 0 {
  34. current = 1
  35. }
  36. if size <= 0 || size > 100 {
  37. size = 10
  38. }
  39. devices, err := service.CameraService.List(searchValue, cameraType, current, size)
  40. if err != nil {
  41. ctx.JSON(http.StatusOK, err)
  42. return
  43. }
  44. pages := math.Ceil(float64(len(devices)) / float64(size))
  45. rsp := model.RspCameraList{
  46. Current: current,
  47. Size: size,
  48. Total: len(devices),
  49. Pages: int(pages),
  50. Records: devices,
  51. }
  52. ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, rsp))
  53. }
  54. func (c *cameraCtl) CreateOrUpdate(ctx *gin.Context) {
  55. value, _ := ctx.Get(middleware.Authorization)
  56. claims := value.(*middleware.Claims)
  57. // 参数验证
  58. var req *model.CameraDetail
  59. if err := ctx.ShouldBind(&req); err != nil {
  60. ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
  61. return
  62. }
  63. err := service.CameraService.CreateOrUpdate(claims.UserId, claims.TenantId, req)
  64. if err != nil {
  65. ctx.JSON(http.StatusOK, err)
  66. return
  67. }
  68. ctx.JSON(http.StatusOK, nil)
  69. }
  70. func (c *cameraCtl) Remove(ctx *gin.Context) {
  71. value, _ := ctx.Get(middleware.Authorization)
  72. claims := value.(*middleware.Claims)
  73. var req *model.ReqCameraRemove
  74. if err := ctx.ShouldBindJSON(&req); err != nil {
  75. ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
  76. return
  77. }
  78. err := service.CameraService.Remove(claims.UserId, req.IDs)
  79. if err != nil {
  80. ctx.JSON(http.StatusOK, err)
  81. return
  82. }
  83. ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, nil))
  84. }
  85. func (c *cameraCtl) Count(ctx *gin.Context) {
  86. }
  87. func (c *cameraCtl) ImportExcel(ctx *gin.Context) {
  88. }
  89. func (c *cameraCtl) ExportExcel(ctx *gin.Context) {
  90. }
  91. func (c *cameraCtl) ExportTemplate(ctx *gin.Context) {
  92. }
  93. func (c *cameraCtl) GetList(ctx *gin.Context) {
  94. value, _ := ctx.Get(middleware.Authorization)
  95. claims := value.(*middleware.Claims)
  96. devices, err := service.CameraService.GetList(claims.TenantId)
  97. if err != nil {
  98. ctx.JSON(http.StatusOK, err)
  99. return
  100. }
  101. ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, devices))
  102. }
  103. func (c *cameraCtl) Enable(ctx *gin.Context) {
  104. var req *model.ReqCameraEnable
  105. if err := ctx.ShouldBindJSON(&req); err != nil {
  106. ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
  107. return
  108. }
  109. if req.Status != model.Enable_Enable && req.Status != model.Enable_Disable {
  110. ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(model.EnableInvalid, nil))
  111. return
  112. }
  113. err := service.CameraService.Enable(req.ID, req.Status)
  114. if err != nil {
  115. ctx.JSON(http.StatusOK, err)
  116. return
  117. }
  118. ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, nil))
  119. }