lampPoleGroupController.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 LampPoleGroup = new(lampPoleGroupCtl)
  15. type lampPoleGroupCtl struct{}
  16. // Detail 获取详情
  17. func (c *lampPoleGroupCtl) Detail(ctx *gin.Context) {
  18. id, e := strconv.Atoi(ctx.Query("id"))
  19. if e != nil {
  20. ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(e.Error(), nil))
  21. return
  22. }
  23. device, err := service.LampPoleGroupService.Get(id)
  24. if err != nil {
  25. ctx.JSON(http.StatusOK, err)
  26. return
  27. }
  28. ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, lampPoleGroupDaoToModel(*device)))
  29. }
  30. // CreateOrUpdate 新增、更新
  31. func (c *lampPoleGroupCtl) CreateOrUpdate(ctx *gin.Context) {
  32. value, _ := ctx.Get(middleware.Authorization)
  33. claims := value.(*middleware.Claims)
  34. var req *dao.LampPoleGroup
  35. if err := ctx.ShouldBindJSON(&req); err != nil {
  36. ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
  37. return
  38. }
  39. err := service.LampPoleGroupService.CreateOrUpdate(claims.UserId, claims.TenantId, req)
  40. ctx.JSON(http.StatusOK, err)
  41. }
  42. // List 获取分页列表
  43. func (c *lampPoleGroupCtl) List(ctx *gin.Context) {
  44. poleGroupName := ctx.Query("poleGroupName")
  45. current, _ := strconv.Atoi(ctx.Query("current"))
  46. size, _ := strconv.Atoi(ctx.Query("size"))
  47. if current == 0 {
  48. current = 1
  49. }
  50. if size <= 0 || size > 100 {
  51. size = 10
  52. }
  53. devices, err := service.LampPoleGroupService.List(poleGroupName, current, size)
  54. if err != nil {
  55. ctx.JSON(http.StatusOK, err)
  56. return
  57. }
  58. pages := math.Ceil(float64(len(devices)) / float64(size))
  59. rsp := model.RspLampPoleGroupList{
  60. Current: current,
  61. Size: size,
  62. Total: len(devices),
  63. Pages: int(pages),
  64. }
  65. for _, device := range devices {
  66. rsp.Records = append(rsp.Records, lampPoleGroupDaoToModel(device))
  67. }
  68. ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, rsp))
  69. }
  70. // Remove 删除
  71. func (c *lampPoleGroupCtl) Remove(ctx *gin.Context) {
  72. value, _ := ctx.Get(middleware.Authorization)
  73. claims := value.(*middleware.Claims)
  74. var req *model.ReqLampPoleGroupRemove
  75. if err := ctx.ShouldBindJSON(&req); err != nil {
  76. ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
  77. return
  78. }
  79. err := service.LampPoleGroupService.Remove(claims.UserId, claims.TenantId, req.IDs, req.Name)
  80. if err != nil {
  81. ctx.JSON(http.StatusOK, err)
  82. return
  83. }
  84. ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, nil))
  85. }
  86. // GetList 获取下拉列表
  87. func (c *lampPoleGroupCtl) GetList(ctx *gin.Context) {
  88. value, _ := ctx.Get(middleware.Authorization)
  89. claims := value.(*middleware.Claims)
  90. devices, err := service.LampPoleGroupService.GetList(claims.TenantId)
  91. if err != nil {
  92. ctx.JSON(http.StatusOK, err)
  93. return
  94. }
  95. ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, devices))
  96. }
  97. // FiltrationList 获取智能照明灯杆分组下拉(过滤掉没有灯控的分组显示)
  98. func (c *lampPoleGroupCtl) FiltrationList(ctx *gin.Context) {
  99. value, _ := ctx.Get(middleware.Authorization)
  100. claims := value.(*middleware.Claims)
  101. devices, err := service.LampPoleGroupService.GetFiltration(claims.TenantId)
  102. if err != nil {
  103. ctx.JSON(http.StatusOK, err)
  104. return
  105. }
  106. ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, devices))
  107. }
  108. // GetTree 获取所有的灯杆分组所属灯杆
  109. func (c *lampPoleGroupCtl) GetTree(ctx *gin.Context) {
  110. value, _ := ctx.Get(middleware.Authorization)
  111. claims := value.(*middleware.Claims)
  112. //todo 灯杆列表赋值
  113. // var rsp model.ReqLampPoleGroupSubmit
  114. devices, err := service.LampPoleGroupService.GetTree(claims.TenantId)
  115. if err != nil {
  116. ctx.JSON(http.StatusOK, err)
  117. return
  118. }
  119. ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, devices))
  120. }
  121. func lampPoleGroupDaoToModel(device dao.LampPoleGroup) model.LampPoleGroupDetail {
  122. return model.LampPoleGroupDetail{
  123. LampPoleGroup: device,
  124. PublicName: "",
  125. LampPoleVOList: []model.LampPoleDetail{},
  126. CameraList: []model.Camera{},
  127. SwitchBoxList: []model.SwitchBox{},
  128. InfoBoardList: []model.InfoBoard{},
  129. }
  130. }