lightController.go 3.7 KB

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