programController.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package controller
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "iot_manager_service/app/middleware"
  5. "iot_manager_service/app/multimedia/model"
  6. "iot_manager_service/app/multimedia/service"
  7. "iot_manager_service/util/common"
  8. "math"
  9. "net/http"
  10. "strconv"
  11. )
  12. var Program = new(programCtl)
  13. type programCtl struct{}
  14. func (c *programCtl) Detail(ctx *gin.Context) {
  15. id, e := strconv.Atoi(ctx.Query("id"))
  16. if e != nil {
  17. ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(e.Error(), nil))
  18. return
  19. }
  20. Program, err := service.ProgramService.Get(id)
  21. if err != nil {
  22. ctx.JSON(http.StatusOK, err)
  23. return
  24. }
  25. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, Program))
  26. }
  27. func (c *programCtl) List(ctx *gin.Context) {
  28. searchValue := ctx.Query("searchValue")
  29. current, _ := strconv.Atoi(ctx.Query("current"))
  30. size, _ := strconv.Atoi(ctx.Query("size"))
  31. s := ctx.Query("sysType")
  32. if current == 0 {
  33. current = 1
  34. }
  35. if size <= 0 || size > 100 {
  36. size = 10
  37. }
  38. sysType := -1
  39. if s != "" {
  40. sysType, _ = strconv.Atoi(s)
  41. }
  42. value, _ := ctx.Get(middleware.Authorization)
  43. claims := value.(*middleware.Claims)
  44. libraries, err := service.ProgramService.List(claims.TenantId, searchValue, current, size, sysType)
  45. if err != nil {
  46. ctx.JSON(http.StatusOK, err)
  47. return
  48. }
  49. pages := math.Ceil(float64(len(libraries)) / float64(size))
  50. rsp := model.RsqProgramList{
  51. Current: current,
  52. Size: size,
  53. Total: len(libraries),
  54. Pages: int(pages),
  55. Records: libraries,
  56. }
  57. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, rsp))
  58. }
  59. func (c *programCtl) Remove(ctx *gin.Context) {
  60. ids := ctx.Query("ids")
  61. id, e := strconv.Atoi(ids)
  62. if e != nil {
  63. ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(e.Error(), nil))
  64. return
  65. }
  66. value, _ := ctx.Get(middleware.Authorization)
  67. claims := value.(*middleware.Claims)
  68. err := service.ProgramService.Remove(claims.UserId, claims.TenantId, id)
  69. if err != nil {
  70. ctx.JSON(http.StatusOK, err)
  71. return
  72. }
  73. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, nil))
  74. }
  75. func (c *programCtl) GetList(ctx *gin.Context) {
  76. name := ctx.Query("name")
  77. value, _ := ctx.Get(middleware.Authorization)
  78. claims := value.(*middleware.Claims)
  79. Program, err := service.ProgramService.GetList(claims.TenantId, name)
  80. if err != nil {
  81. ctx.JSON(http.StatusOK, err)
  82. return
  83. }
  84. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, Program))
  85. }
  86. func (c *programCtl) Submit(ctx *gin.Context) {
  87. var req model.ReqProgramSubmit
  88. if err := ctx.ShouldBindJSON(&req); err != nil {
  89. ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
  90. return
  91. }
  92. value, _ := ctx.Get(middleware.Authorization)
  93. claims := value.(*middleware.Claims)
  94. e := service.ProgramService.Submit(claims.TenantId, claims.UserId, req)
  95. if e != nil {
  96. ctx.JSON(http.StatusOK, e)
  97. return
  98. }
  99. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, nil))
  100. }
  101. func (c *programCtl) GetLibraryList(ctx *gin.Context) {
  102. id := ctx.Query("id")
  103. programId, e := strconv.Atoi(id)
  104. if e != nil {
  105. ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(e.Error(), nil))
  106. return
  107. }
  108. value, _ := ctx.Get(middleware.Authorization)
  109. claims := value.(*middleware.Claims)
  110. libraries, err := service.ProgramService.GetLibraryList(claims.TenantId, programId)
  111. if err != nil {
  112. ctx.JSON(http.StatusOK, e)
  113. return
  114. }
  115. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, libraries))
  116. }
  117. func (c *programCtl) RelationDeviceList(ctx *gin.Context) {
  118. resolutionStr := ctx.Query("resolution")
  119. resolution, e := strconv.Atoi(resolutionStr)
  120. if e != nil {
  121. ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(e.Error(), nil))
  122. return
  123. }
  124. value, _ := ctx.Get(middleware.Authorization)
  125. claims := value.(*middleware.Claims)
  126. libraries, err := service.ProgramService.RelationDeviceList(claims.TenantId, resolution)
  127. if err != nil {
  128. ctx.JSON(http.StatusOK, e)
  129. return
  130. }
  131. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, libraries))
  132. }