lampPoleController.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 LampPole = new(lampPoleCtl)
  15. type lampPoleCtl struct{}
  16. func (c *lampPoleCtl) 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.LampPoleService.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 *lampPoleCtl) CreateOrUpdate(ctx *gin.Context) {
  30. value, _ := ctx.Get(middleware.Authorization)
  31. claims := value.(*middleware.Claims)
  32. var req *dao.LampPole
  33. if err := ctx.ShouldBindJSON(&req); err != nil {
  34. ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
  35. return
  36. }
  37. err := service.LampPoleService.CreateOrUpdate(claims.UserId, claims.TenantId, req)
  38. ctx.JSON(http.StatusOK, err)
  39. }
  40. func (c *lampPoleCtl) List(ctx *gin.Context) {
  41. searchValue := ctx.Query("searchValue")
  42. groupId := ctx.Query("groupId")
  43. boxId := ctx.Query("boxId")
  44. current, _ := strconv.Atoi(ctx.Query("current"))
  45. size, _ := strconv.Atoi(ctx.Query("size"))
  46. if current == 0 {
  47. current = 1
  48. }
  49. if size <= 0 || size > 100 {
  50. size = 10
  51. }
  52. devices, err := service.LampPoleService.List(searchValue, groupId, boxId, current, size)
  53. if err != nil {
  54. ctx.JSON(http.StatusOK, err)
  55. return
  56. }
  57. pages := math.Ceil(float64(len(devices)) / float64(size))
  58. rsp := model.RspLampPoleList{
  59. Current: current,
  60. Size: size,
  61. Total: len(devices),
  62. Pages: int(pages),
  63. }
  64. for _, device := range devices {
  65. rsp.Records = append(rsp.Records, lampPoleDaoToModel(device))
  66. }
  67. ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, rsp))
  68. }
  69. func (c *lampPoleCtl) GetRelevanceDetail(ctx *gin.Context) {
  70. id, e := strconv.Atoi(ctx.Query("id"))
  71. if e != nil {
  72. ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(e.Error(), nil))
  73. return
  74. }
  75. device, err := service.LampPoleService.GetRelevanceDetail(id)
  76. if err != nil {
  77. ctx.JSON(http.StatusOK, err)
  78. return
  79. }
  80. ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, device))
  81. }
  82. func (c *lampPoleCtl) Remove(ctx *gin.Context) {
  83. value, _ := ctx.Get(middleware.Authorization)
  84. claims := value.(*middleware.Claims)
  85. var req *model.ReqLampPoleRemove
  86. if err := ctx.ShouldBindJSON(&req); err != nil {
  87. ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
  88. return
  89. }
  90. err := service.LampPoleService.Remove(claims.UserId, req.IDs)
  91. if err != nil {
  92. ctx.JSON(http.StatusOK, err)
  93. return
  94. }
  95. ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, nil))
  96. }
  97. func (c *lampPoleCtl) ImportExcel(ctx *gin.Context) {
  98. }
  99. func (c *lampPoleCtl) ExportExcel(ctx *gin.Context) {
  100. }
  101. func (c *lampPoleCtl) ExportTemplate(ctx *gin.Context) {
  102. }
  103. func (c *lampPoleCtl) GetList(ctx *gin.Context) {
  104. value, _ := ctx.Get(middleware.Authorization)
  105. claims := value.(*middleware.Claims)
  106. devices, err := service.LampPoleService.GetList(claims.TenantId)
  107. if err != nil {
  108. ctx.JSON(http.StatusOK, err)
  109. return
  110. }
  111. ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, devices))
  112. }
  113. func lampPoleDaoToModel(device dao.LampPole) model.LampPoleDetail {
  114. return model.LampPoleDetail{
  115. LampPole: device,
  116. AlarmTerminalList: nil,
  117. CameraList: nil,
  118. CaptureUnitList: nil,
  119. GatewayList: nil,
  120. InfoBoardList: nil,
  121. IpBroadcastList: nil,
  122. LightControlList: nil,
  123. SensorList: nil,
  124. ZigbeeList: nil,
  125. }
  126. }