programController.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package controller
  2. import (
  3. "github.com/gin-gonic/gin"
  4. dev_controller "iot_manager_service/app/device/controller"
  5. "iot_manager_service/app/middleware"
  6. "iot_manager_service/app/multimedia/model"
  7. "iot_manager_service/app/multimedia/service"
  8. "iot_manager_service/util/common"
  9. "math"
  10. "net/http"
  11. "strconv"
  12. "strings"
  13. )
  14. var Program = new(programCtl)
  15. type programCtl struct{}
  16. func (c *programCtl) Detail(ctx *gin.Context) {
  17. id, e := strconv.Atoi(ctx.Query("id"))
  18. if e != nil {
  19. ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(e.Error(), nil))
  20. return
  21. }
  22. Program, err := service.ProgramService.Get(id)
  23. if err != nil {
  24. ctx.JSON(http.StatusOK, err)
  25. return
  26. }
  27. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, Program))
  28. }
  29. func (c *programCtl) List(ctx *gin.Context) {
  30. searchValue := ctx.Query("searchValue")
  31. current, _ := strconv.Atoi(ctx.Query("current"))
  32. size, _ := strconv.Atoi(ctx.Query("size"))
  33. s := ctx.Query("sysType")
  34. if current == 0 {
  35. current = 1
  36. }
  37. if size <= 0 || size > 100 {
  38. size = 10
  39. }
  40. sysType := 0
  41. if s != "" {
  42. sysType, _ = strconv.Atoi(s)
  43. }
  44. value, _ := ctx.Get(middleware.Authorization)
  45. claims := value.(*middleware.Claims)
  46. libraries, total, err := service.ProgramService.List(claims.TenantId, searchValue, current, size, sysType)
  47. if err != nil {
  48. ctx.JSON(http.StatusOK, err)
  49. return
  50. }
  51. pages := math.Ceil(float64(total) / float64(size))
  52. rsp := model.RsqProgramList{
  53. Current: current,
  54. Size: size,
  55. Total: int(total),
  56. Pages: int(pages),
  57. Records: libraries,
  58. }
  59. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, rsp))
  60. }
  61. func (c *programCtl) Remove(ctx *gin.Context) {
  62. ids := ctx.Query("ids")
  63. id, e := strconv.Atoi(ids)
  64. if e != nil {
  65. ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(e.Error(), nil))
  66. return
  67. }
  68. value, _ := ctx.Get(middleware.Authorization)
  69. claims := value.(*middleware.Claims)
  70. err := service.ProgramService.Remove(claims.UserId, claims.TenantId, id)
  71. if err != nil {
  72. ctx.JSON(http.StatusOK, err)
  73. return
  74. }
  75. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, nil))
  76. }
  77. func (c *programCtl) GetList(ctx *gin.Context) {
  78. name := ctx.Query("name")
  79. value, _ := ctx.Get(middleware.Authorization)
  80. claims := value.(*middleware.Claims)
  81. Program, err := service.ProgramService.GetList(claims.TenantId, name)
  82. if err != nil {
  83. ctx.JSON(http.StatusOK, err)
  84. return
  85. }
  86. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, Program))
  87. }
  88. func (c *programCtl) Submit(ctx *gin.Context) {
  89. var req model.ReqProgramSubmit
  90. if err := ctx.ShouldBindJSON(&req); err != nil {
  91. ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
  92. return
  93. }
  94. value, _ := ctx.Get(middleware.Authorization)
  95. claims := value.(*middleware.Claims)
  96. e := service.ProgramService.Submit(claims.TenantId, claims.UserId, req)
  97. if e != nil {
  98. ctx.JSON(http.StatusOK, e)
  99. return
  100. }
  101. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, nil))
  102. }
  103. func (c *programCtl) GetLibraryList(ctx *gin.Context) {
  104. id := ctx.Query("id")
  105. programId, e := strconv.Atoi(id)
  106. if e != nil {
  107. ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(e.Error(), nil))
  108. return
  109. }
  110. value, _ := ctx.Get(middleware.Authorization)
  111. claims := value.(*middleware.Claims)
  112. libraries, err := service.ProgramService.GetLibraryList(claims.TenantId, programId)
  113. if err != nil {
  114. ctx.JSON(http.StatusOK, e)
  115. return
  116. }
  117. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, libraries))
  118. }
  119. func (c *programCtl) RelationDeviceList(ctx *gin.Context) {
  120. resolutionStr := ctx.Query("resolution")
  121. sysTypeStr := ctx.Query("sysType")
  122. resolution := 0
  123. if resolutionStr != "" {
  124. resolution, _ = strconv.Atoi(resolutionStr)
  125. }
  126. sysType := 0
  127. if sysTypeStr != "" {
  128. sysType, _ = strconv.Atoi(sysTypeStr)
  129. }
  130. value, _ := ctx.Get(middleware.Authorization)
  131. claims := value.(*middleware.Claims)
  132. devices, err := service.ProgramService.RelationDeviceList(claims.TenantId, sysType, resolution)
  133. if err != nil {
  134. ctx.JSON(http.StatusOK, err)
  135. return
  136. }
  137. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, devices))
  138. }
  139. func (c *programCtl) RelationIpBroadcastList(ctx *gin.Context) {
  140. value, _ := ctx.Get(middleware.Authorization)
  141. claims := value.(*middleware.Claims)
  142. devices, err := service.ProgramService.RelationDeviceList(claims.TenantId, 1, 0)
  143. if err != nil {
  144. ctx.JSON(http.StatusOK, err)
  145. return
  146. }
  147. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, devices))
  148. }
  149. func (c *programCtl) Audit(ctx *gin.Context) {
  150. var req model.ReqProgramAudit
  151. if err := ctx.ShouldBindJSON(&req); err != nil {
  152. ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
  153. return
  154. }
  155. // 链接
  156. err := service.PublishLibrariesService.Audit(req)
  157. if err != nil {
  158. ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
  159. return
  160. }
  161. split := strings.Split(req.InfoId, ",")
  162. for _, s := range split {
  163. id, err := strconv.Atoi(s)
  164. if err != nil {
  165. dev_controller.InfoBoard.CronSyncLedPaying(id)
  166. }
  167. }
  168. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, nil))
  169. }