sys_dept.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. package apis
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "github.com/gin-gonic/gin/binding"
  5. "github.com/go-admin-team/go-admin-core/sdk/api"
  6. "github.com/go-admin-team/go-admin-core/sdk/pkg"
  7. "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth/user"
  8. _ "github.com/go-admin-team/go-admin-core/sdk/pkg/response"
  9. "go-admin/app/admin/models"
  10. "go-admin/app/admin/service"
  11. "go-admin/app/admin/service/dto"
  12. )
  13. type SysDept struct {
  14. api.Api
  15. }
  16. // GetPage
  17. // @Summary 分页部门列表数据
  18. // @Description 分页列表
  19. // @Tags 部门
  20. // @Param deptName query string false "deptName"
  21. // @Param deptId query string false "deptId"
  22. // @Param position query string false "position"
  23. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  24. // @Router /api/v1/dept [get]
  25. // @Security Bearer
  26. func (e SysDept) GetPage(c *gin.Context) {
  27. s := service.SysDept{}
  28. req := dto.SysDeptGetPageReq{}
  29. err := e.MakeContext(c).
  30. MakeOrm().
  31. Bind(&req).
  32. MakeService(&s.Service).
  33. Errors
  34. if err != nil {
  35. e.Logger.Error(err)
  36. e.Error(500, err, err.Error())
  37. return
  38. }
  39. list := make([]models.SysDept, 0)
  40. list, err = s.SetDeptPage(&req)
  41. if err != nil {
  42. e.Error(500, err, "查询失败")
  43. return
  44. }
  45. e.OK(list, "查询成功")
  46. }
  47. // Get
  48. // @Summary 获取部门数据
  49. // @Description 获取JSON
  50. // @Tags 部门
  51. // @Param deptId path string false "deptId"
  52. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  53. // @Router /api/v1/dept/{deptId} [get]
  54. // @Security Bearer
  55. func (e SysDept) Get(c *gin.Context) {
  56. s := service.SysDept{}
  57. req := dto.SysDeptGetReq{}
  58. err := e.MakeContext(c).
  59. MakeOrm().
  60. Bind(&req, binding.JSON, nil).
  61. MakeService(&s.Service).
  62. Errors
  63. if err != nil {
  64. e.Logger.Error(err)
  65. e.Error(500, err, err.Error())
  66. return
  67. }
  68. var object models.SysDept
  69. err = s.Get(&req, &object)
  70. if err != nil {
  71. e.Error(500, err, "查询失败")
  72. return
  73. }
  74. e.OK(object, "查询成功")
  75. }
  76. // Insert 添加部门
  77. // @Summary 添加部门
  78. // @Description 获取JSON
  79. // @Tags 部门
  80. // @Accept application/json
  81. // @Product application/json
  82. // @Param data body dto.SysDeptInsertReq true "data"
  83. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  84. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  85. // @Router /api/v1/dept [post]
  86. // @Security Bearer
  87. func (e SysDept) Insert(c *gin.Context) {
  88. s := service.SysDept{}
  89. req := dto.SysDeptInsertReq{}
  90. err := e.MakeContext(c).
  91. MakeOrm().
  92. Bind(&req, binding.JSON).
  93. MakeService(&s.Service).
  94. Errors
  95. if err != nil {
  96. e.Logger.Error(err)
  97. e.Error(500, err, err.Error())
  98. return
  99. }
  100. // 设置创建人
  101. req.SetCreateBy(user.GetUserId(c))
  102. err = s.Insert(&req)
  103. if err != nil {
  104. e.Error(500, err, "创建失败")
  105. return
  106. }
  107. e.OK(req.GetId(), "创建成功")
  108. }
  109. // Update
  110. // @Summary 修改部门
  111. // @Description 获取JSON
  112. // @Tags 部门
  113. // @Accept application/json
  114. // @Product application/json
  115. // @Param id path int true "id"
  116. // @Param data body dto.SysDeptUpdateReq true "body"
  117. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  118. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  119. // @Router /api/v1/dept/{deptId} [put]
  120. // @Security Bearer
  121. func (e SysDept) Update(c *gin.Context) {
  122. s := service.SysDept{}
  123. req := dto.SysDeptUpdateReq{}
  124. err := e.MakeContext(c).
  125. MakeOrm().
  126. Bind(&req).
  127. MakeService(&s.Service).
  128. Errors
  129. if err != nil {
  130. e.Logger.Error(err)
  131. e.Error(500, err, err.Error())
  132. return
  133. }
  134. req.SetUpdateBy(user.GetUserId(c))
  135. err = s.Update(&req)
  136. if err != nil {
  137. e.Error(500, err, err.Error())
  138. return
  139. }
  140. e.OK(req.GetId(), "更新成功")
  141. }
  142. // Delete
  143. // @Summary 删除部门
  144. // @Description 删除数据
  145. // @Tags 部门
  146. // @Param data body dto.SysDeptDeleteReq true "body"
  147. // @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
  148. // @Success 200 {string} string "{"code": -1, "message": "删除失败"}"
  149. // @Router /api/v1/dept [delete]
  150. // @Security Bearer
  151. func (e SysDept) Delete(c *gin.Context) {
  152. s := service.SysDept{}
  153. req := dto.SysDeptDeleteReq{}
  154. err := e.MakeContext(c).
  155. MakeOrm().
  156. Bind(&req, binding.JSON, nil).
  157. MakeService(&s.Service).
  158. Errors
  159. if err != nil {
  160. e.Logger.Error(err)
  161. e.Error(500, err, err.Error())
  162. return
  163. }
  164. err = s.Remove(&req)
  165. if err != nil {
  166. e.Error(500, err, "删除失败")
  167. return
  168. }
  169. e.OK(req.GetId(), "删除成功")
  170. }
  171. // Get2Tree 用户管理 左侧部门树
  172. func (e SysDept) Get2Tree(c *gin.Context) {
  173. s := service.SysDept{}
  174. req := dto.SysDeptGetPageReq{}
  175. err := e.MakeContext(c).
  176. MakeOrm().
  177. Bind(&req,binding.Form).
  178. MakeService(&s.Service).
  179. Errors
  180. if err != nil {
  181. e.Logger.Error(err)
  182. e.Error(500, err, err.Error())
  183. return
  184. }
  185. list := make([]dto.DeptLabel, 0)
  186. list, err = s.SetDeptTree(&req)
  187. if err != nil {
  188. e.Error(500, err, "查询失败")
  189. return
  190. }
  191. e.OK(list, "")
  192. }
  193. // GetDeptTreeRoleSelect TODO: 此接口需要调整不应该将list和选中放在一起
  194. func (e SysDept) GetDeptTreeRoleSelect(c *gin.Context) {
  195. s := service.SysDept{}
  196. err := e.MakeContext(c).
  197. MakeOrm().
  198. MakeService(&s.Service).
  199. Errors
  200. if err != nil {
  201. e.Logger.Error(err)
  202. e.Error(500, err, err.Error())
  203. return
  204. }
  205. id, err := pkg.StringToInt(c.Param("roleId"))
  206. result, err := s.SetDeptLabel()
  207. if err != nil {
  208. e.Error(500, err, err.Error())
  209. return
  210. }
  211. menuIds := make([]int, 0)
  212. if id != 0 {
  213. menuIds, err = s.GetWithRoleId(id)
  214. if err != nil {
  215. e.Error(500, err, err.Error())
  216. return
  217. }
  218. }
  219. e.OK(gin.H{
  220. "depts": result,
  221. "checkedKeys": menuIds,
  222. }, "")
  223. }