lampPoleGroupController.go 4.2 KB

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