onDemandGroupController.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 OnDemandGroup = new(onDemandGroupCtl)
  15. type onDemandGroupCtl struct{}
  16. func (c *onDemandGroupCtl) 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. device, err := service.OnDemandGroupService.Get(id)
  23. if err != nil {
  24. ctx.JSON(http.StatusOK, err)
  25. return
  26. }
  27. ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, device))
  28. }
  29. func (c *onDemandGroupCtl) List(ctx *gin.Context) {
  30. searchValue := ctx.Query("searchValue")
  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.OnDemandGroupService.List(searchValue, 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.RspOnDemandGroupList{
  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 *onDemandGroupCtl) CreateOrUpdate(ctx *gin.Context) {
  55. value, _ := ctx.Get(middleware.Authorization)
  56. claims := value.(*middleware.Claims)
  57. var req *dao.OnDemandGroup
  58. if err := ctx.ShouldBindJSON(&req); err != nil {
  59. ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
  60. return
  61. }
  62. err := service.OnDemandGroupService.CreateOrUpdate(claims.UserId, claims.TenantId, req)
  63. ctx.JSON(http.StatusOK, err)
  64. }
  65. func (c *onDemandGroupCtl) Remove(ctx *gin.Context) {
  66. value, _ := ctx.Get(middleware.Authorization)
  67. claims := value.(*middleware.Claims)
  68. var req *model.ReqOnDemandGroupRemove
  69. if err := ctx.ShouldBindJSON(&req); err != nil {
  70. ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
  71. return
  72. }
  73. err := service.OnDemandGroupService.Remove(claims.UserId, req.IDs)
  74. if err != nil {
  75. ctx.JSON(http.StatusOK, err)
  76. return
  77. }
  78. ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, nil))
  79. }
  80. func (c *onDemandGroupCtl) GetList(ctx *gin.Context) {
  81. value, _ := ctx.Get(middleware.Authorization)
  82. claims := value.(*middleware.Claims)
  83. devices, err := service.OnDemandGroupService.GetList(claims.TenantId)
  84. if err != nil {
  85. ctx.JSON(http.StatusOK, err)
  86. return
  87. }
  88. ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, devices))
  89. }
  90. func (c *onDemandGroupCtl) GroupNumber(ctx *gin.Context) {
  91. id, e := strconv.Atoi(ctx.Query("id"))
  92. if e != nil {
  93. ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(e.Error(), nil))
  94. return
  95. }
  96. if id <= 0 {
  97. ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(model.ParamsInvalid, nil))
  98. return
  99. }
  100. device, err := service.OnDemandGroupService.GroupNumberList(id)
  101. if err != nil {
  102. ctx.JSON(http.StatusOK, err)
  103. return
  104. }
  105. ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, device))
  106. }