environmentController.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package controller
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "iot_manager_service/app/middleware"
  5. "iot_manager_service/app/operation/service"
  6. "iot_manager_service/util/common"
  7. "net/http"
  8. "strconv"
  9. "time"
  10. )
  11. var Environment = new(environmentCtl)
  12. type environmentCtl struct{}
  13. func (c *environmentCtl) EnvironmentList(ctx *gin.Context) {
  14. value, _ := ctx.Get(middleware.Authorization)
  15. claims := value.(*middleware.Claims)
  16. records, err := service.EnvironmentService.EnvironmentList(claims.TenantId)
  17. if err != nil {
  18. ctx.JSON(http.StatusOK, err)
  19. return
  20. }
  21. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, records))
  22. }
  23. func (c *environmentCtl) GetData(ctx *gin.Context) {
  24. id, _ := strconv.Atoi(ctx.Query("id"))
  25. records, err := service.EnvironmentService.Get(id)
  26. if err != nil {
  27. ctx.JSON(http.StatusOK, err)
  28. return
  29. }
  30. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, records))
  31. }
  32. func (c *environmentCtl) GetHistoryData(ctx *gin.Context) {
  33. id, _ := strconv.Atoi(ctx.Query("id"))
  34. scope := ctx.Query("scope")
  35. queryType, _ := strconv.Atoi(ctx.Query("type"))
  36. startTimeStr := ctx.Query("startTime")
  37. endTimeStr := ctx.Query("endTime")
  38. start := time.Time{}
  39. end := time.Time{}
  40. if startTimeStr != "" && endTimeStr != "" {
  41. startTime, err := time.Parse("2006-01-02 15:03:04", startTimeStr)
  42. if err != nil {
  43. ctx.JSON(http.StatusOK, common.ParamsInvalidResponse("startTime invalid", nil))
  44. return
  45. }
  46. start = startTime
  47. endTime, err := time.Parse("2006-01-02 15:03:04", endTimeStr)
  48. if err != nil {
  49. ctx.JSON(http.StatusOK, common.ParamsInvalidResponse("endTime invalid", nil))
  50. return
  51. }
  52. end = endTime
  53. }
  54. records, err := service.EnvironmentService.GetHistoryData(id, scope, queryType, start, end)
  55. if err != nil {
  56. ctx.JSON(http.StatusOK, common.FailResponse(err.Error(), nil))
  57. return
  58. }
  59. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, records))
  60. }
  61. func (c *environmentCtl) GetScopeList(ctx *gin.Context) {
  62. //hour 使用 EnvironmentData 小时数据
  63. //day 使用 EnvironmentDataSummary 天数据
  64. }