group.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package apis
  2. import (
  3. "fmt"
  4. "IotAdmin/core/sdk/api"
  5. "IotAdmin/core/sdk/pkg/jwt-auth/user"
  6. _ "IotAdmin/core/sdk/pkg/response"
  7. "github.com/gin-gonic/gin"
  8. "IotAdmin/app/iot/models"
  9. "IotAdmin/app/iot/service"
  10. "IotAdmin/app/iot/service/dto"
  11. "IotAdmin/common/permission"
  12. )
  13. // IotGroupApi 分组接口
  14. type IotGroupApi struct {
  15. api.Api
  16. }
  17. // GetPage 获取分组列表
  18. // @Summary 获取分组列表
  19. // @Description 获取分组列表
  20. // @Tags 分组管理
  21. // @Param path query string false "分组路径"
  22. // @Param name query string false "分组名称"
  23. // @Param description query string false "分组描述"
  24. // @Param pageSize query int false "页条数"
  25. // @Param pageIndex query int false "页码"
  26. // @Success 200 {object} response.Response{data=response.Page{list=[]models.IotGroup}} "{"code": 200, "data": [...]}"
  27. // @Router /api/iot-group [get]
  28. // @Security Bearer
  29. func (e IotGroupApi) GetPage(c *gin.Context) {
  30. req := dto.IotGroupGetPageReq{}
  31. s := service.IotGroupService{}
  32. err := e.MakeContext(c).
  33. MakeOrm().
  34. Bind(&req).
  35. MakeService(&s.Service).
  36. Errors
  37. if err != nil {
  38. e.Logger.Error(err)
  39. e.Error(500, err, err.Error())
  40. return
  41. }
  42. p := permission.GetPermissionFromContext(c)
  43. list := make([]models.IotGroup, 0)
  44. var count int64
  45. err = s.GetPage(&req, p, &list)
  46. if err != nil {
  47. e.Error(500, err, fmt.Sprintf("获取分组失败,\r\n失败信息 %s", err.Error()))
  48. return
  49. }
  50. e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
  51. }
  52. // Get 获取分组
  53. // @Summary 获取分组
  54. // @Description 获取分组
  55. // @Tags 分组管理
  56. // @Param id path int false "id"
  57. // @Success 200 {object} response.Response{data=models.IotGroup} "{"code": 200, "data": [...]}"
  58. // @Router /api/iot-group/{id} [get]
  59. // @Security Bearer
  60. func (e IotGroupApi) Get(c *gin.Context) {
  61. req := dto.IotGroupGetReq{}
  62. s := service.IotGroupService{}
  63. err := e.MakeContext(c).
  64. MakeOrm().
  65. Bind(&req).
  66. MakeService(&s.Service).
  67. Errors
  68. if err != nil {
  69. e.Logger.Error(err)
  70. e.Error(500, err, err.Error())
  71. return
  72. }
  73. var object models.IotGroup
  74. p := permission.GetPermissionFromContext(c)
  75. err = s.Get(&req, p, &object)
  76. if err != nil {
  77. e.Error(500, err, fmt.Sprintf("获取分组失败,\r\n失败信息 %s", err.Error()))
  78. return
  79. }
  80. e.OK(object, "查询成功")
  81. }
  82. // Insert 添加分组
  83. // @Summary 添加分组
  84. // @Description 添加分组
  85. // @Tags 分组管理
  86. // @Accept application/json
  87. // @Product application/json
  88. // @Param data body dto.IotGroupInsertReq true "data"
  89. // @Success 200 {object} response.Response "{"code": 200, "message": "添加成功"}"
  90. // @Router /api/iot-group [post]
  91. // @Security Bearer
  92. func (e IotGroupApi) Insert(c *gin.Context) {
  93. req := dto.IotGroupInsertReq{}
  94. s := service.IotGroupService{}
  95. err := e.MakeContext(c).
  96. MakeOrm().
  97. Bind(&req).
  98. MakeService(&s.Service).
  99. Errors
  100. if err != nil {
  101. e.Logger.Error(err)
  102. e.Error(500, err, err.Error())
  103. return
  104. }
  105. // 设置创建人
  106. req.SetCreateBy(user.GetUserId(c))
  107. err = s.Insert(&req)
  108. if err != nil {
  109. e.Error(500, err, fmt.Sprintf("添加分组失败,\r\n失败信息 %s", err.Error()))
  110. return
  111. }
  112. e.OK(req.GetId(), "添加成功")
  113. }
  114. // Update 修改分组
  115. // @Summary 修改分组
  116. // @Description 修改分组
  117. // @Tags 分组管理
  118. // @Accept application/json
  119. // @Product application/json
  120. // @Param id path int true "id"
  121. // @Param data body dto.IotGroupUpdateReq true "body"
  122. // @Success 200 {object} response.Response "{"code": 200, "message": "修改成功"}"
  123. // @Router /api/iot-group/{id} [put]
  124. // @Security Bearer
  125. func (e IotGroupApi) Update(c *gin.Context) {
  126. req := dto.IotGroupUpdateReq{}
  127. s := service.IotGroupService{}
  128. err := e.MakeContext(c).
  129. MakeOrm().
  130. Bind(&req).
  131. MakeService(&s.Service).
  132. Errors
  133. if err != nil {
  134. e.Logger.Error(err)
  135. e.Error(500, err, err.Error())
  136. return
  137. }
  138. req.SetUpdateBy(user.GetUserId(c))
  139. p := permission.GetPermissionFromContext(c)
  140. err = s.Update(&req, p)
  141. if err != nil {
  142. e.Error(500, err, fmt.Sprintf("修改分组失败,\r\n失败信息 %s", err.Error()))
  143. return
  144. }
  145. e.OK(req.GetId(), "修改成功")
  146. }
  147. // Delete 删除分组
  148. // @Summary 删除分组
  149. // @Description 删除分组
  150. // @Tags 分组管理
  151. // @Param data body dto.IotGroupDeleteReq true "body"
  152. // @Success 200 {object} response.Response "{"code": 200, "message": "删除成功"}"
  153. // @Router /api/iot-group [delete]
  154. // @Security Bearer
  155. func (e IotGroupApi) Delete(c *gin.Context) {
  156. s := service.IotGroupService{}
  157. req := dto.IotGroupDeleteReq{}
  158. err := e.MakeContext(c).
  159. MakeOrm().
  160. Bind(&req).
  161. MakeService(&s.Service).
  162. Errors
  163. if err != nil {
  164. e.Logger.Error(err)
  165. e.Error(500, err, err.Error())
  166. return
  167. }
  168. // req.SetUpdateBy(user.GetUserId(c))
  169. p := permission.GetPermissionFromContext(c)
  170. err = s.Remove(&req, p)
  171. if err != nil {
  172. e.Error(500, err, fmt.Sprintf("删除分组失败,\r\n失败信息 %s", err.Error()))
  173. return
  174. }
  175. e.OK(req.GetId(), "删除成功")
  176. }