sys_dept_api.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package admin
  2. import (
  3. "strconv"
  4. "strings"
  5. "ulink-admin/frame"
  6. "ulink-admin/modules/system/models/model"
  7. "ulink-admin/modules/system/models/req"
  8. "ulink-admin/modules/system/service"
  9. "ulink-admin/pkg/jwt/admin"
  10. "ulink-admin/pkg/library/tree/tree_dept"
  11. )
  12. type DeptApi struct {
  13. DeptService *service.DeptService `inject:""`
  14. }
  15. // TreeSelect 查询部门部门树
  16. // @Summary 加载对应角色部门列表树接口
  17. // @Description 加载对应角色部门列表树接口
  18. // @Tags 部门相关接口
  19. // @Accept application/json
  20. // @Produce application/json
  21. // @Param Authorization header string false "Bearer 令牌"
  22. // @Param id path int true "id" id
  23. // @Security ApiKeyAuth
  24. // @Success 200 {object} resp.Response{data=map[INode]interface{}} "返回部门查询"
  25. // @Router /dept/teeselect [get]
  26. func (a DeptApi) TreeSelect(c *frame.Context) {
  27. query := req.DeptQuery{}
  28. c.ValidteError(c.ShouldBind(&query), &query)
  29. treeSelect := a.DeptService.TreeSelect(query)
  30. var list tree_dept.DeptList = *treeSelect
  31. c.Ok(list.GetTree())
  32. }
  33. // RoleDeptTreeSelect 加载对应角色部门列表树
  34. // @Summary 加载对应角色部门列表树接口
  35. // @Description 加载对应角色部门列表树接口
  36. // @Tags 部门相关接口
  37. // @Accept application/json
  38. // @Produce application/json
  39. // @Param Authorization header string false "Bearer 令牌"
  40. // @Param id path int true "id" id
  41. // @Security ApiKeyAuth
  42. // @Success 200 {object} resp.Response{data=map[INode]interface{}} "返回部门查询"
  43. // @Router /dept/roleDeptTreeselect [get]
  44. func (a DeptApi) RoleDeptTreeSelect(c *frame.Context) {
  45. m := make(map[string]interface{})
  46. param := c.Param("id")
  47. id, _ := strconv.ParseInt(param, 10, 64)
  48. checkedKeys := a.DeptService.SelectDeptListByRoleId(id)
  49. m["checkedKeys"] = checkedKeys
  50. treeSelect := a.DeptService.TreeSelect(req.DeptQuery{})
  51. var list tree_dept.DeptList = *treeSelect
  52. tree := list.GetTree()
  53. m["depts"] = tree
  54. c.Ok(m)
  55. }
  56. // Find 查询部门列表
  57. // @Summary 分页查询部门数据接口
  58. // @Description 分页查询部门数据接口
  59. // @Tags 部门相关接口
  60. // @Accept application/json
  61. // @Produce application/json
  62. // @Param Authorization header string false "Bearer 令牌"
  63. // @Param object query req.DeptQuery false "查询参数"
  64. // @Security ApiKeyAuth
  65. // @Success 200 {object} resp.Response{data=dept.Page} "分页获取部门列表,返回包括列表,总数,页码,每页数量"
  66. // @Router /dept/List [get]
  67. func (a DeptApi) Find(c *frame.Context) {
  68. query := req.DeptQuery{}
  69. c.ValidteError(c.ShouldBind(&query), &query)
  70. c.Ok(a.DeptService.GetList(query))
  71. }
  72. // ExcludeChild 查询部门列表(排除节点)
  73. // @Summary 分页查询部门数据接口 排除节点
  74. // @Description 分页查询部门数据接口 排除节点
  75. // @Tags 部门相关接口
  76. // @Accept application/json
  77. // @Produce application/json
  78. // @Param Authorization header string false "Bearer 令牌"
  79. // @Param object query req.DeptQuery false "查询参数"
  80. // @Security ApiKeyAuth
  81. // @Success 200 {object} resp.Response{data=model.SysDept} "分页获取部门列表,返回包括列表,总数,页码,每页数量"
  82. // @Router /dept/list/exclude [get]
  83. func (a DeptApi) ExcludeChild(c *frame.Context) {
  84. var params struct {
  85. Id int64 `uri:"id" binding:"required" msg:"id不存在"` //ids
  86. }
  87. c.ValidteError(c.ShouldBindUri(&params), &params)
  88. list := a.DeptService.GetList(req.DeptQuery{})
  89. var depts = *list
  90. deptList := make([]model.SysDept, 0)
  91. for _, dept := range depts {
  92. if dept.Id == params.Id || strings.Contains(dept.Ancestors, strconv.FormatInt(params.Id, 10)) {
  93. continue
  94. }
  95. deptList = append(deptList, dept)
  96. }
  97. c.Ok(deptList)
  98. }
  99. // GetInfo 根据部门编号获取详细信息
  100. // @Summary 部门详情查询接口
  101. // @Description 部门详情查询接口
  102. // @Tags 部门相关接口
  103. // @Accept application/json
  104. // @Produce application/json
  105. // @Param Authorization header string false "Bearer 令牌"
  106. // @Param id path int true "id" id
  107. // @Security ApiKeyAuth
  108. // @Success 200 {object} resp.Response{data=model.SysDept} "返回部门详情查询"
  109. // @Router /dept [get]
  110. func (a DeptApi) GetInfo(c *frame.Context) {
  111. var params struct {
  112. Id int64 `uri:"id" binding:"required" msg:"id不存在"` //ids
  113. }
  114. c.ValidteError(c.ShouldBindUri(&params), &params)
  115. c.Ok(a.DeptService.GetDeptById(params.Id))
  116. }
  117. // Add 添加部门
  118. // @Summary 新增部门接口
  119. // @Description 新增部门接口
  120. // @Tags 部门相关接口
  121. // @Accept application/json
  122. // @Produce application/json
  123. // @Param Authorization header string false "Bearer 令牌"
  124. // @Param data body model.SysDept true "部门实体对象"
  125. // @Success 200 {object} resp.Response{msg=string} "操作状态"
  126. // @Router /dept/add [post]
  127. func (a DeptApi) Add(c *frame.Context) {
  128. dept := model.SysDept{}
  129. c.ValidteError(c.ShouldBind(&dept), &dept)
  130. //校验部门名称是否唯一
  131. unique := a.DeptService.CheckDeptNameUnique(dept)
  132. if unique {
  133. frame.Throw(frame.BUSINESS_CODE, "新增部门'"+dept.DeptName+"'失败,部门名称已存在")
  134. return
  135. }
  136. info := a.DeptService.GetDeptById(dept.ParentId)
  137. if info == nil {
  138. frame.Throw(frame.BUSINESS_CODE, "部门查询异常'")
  139. return
  140. }
  141. if info.Status == 1 {
  142. frame.Throw(frame.BUSINESS_CODE, "部门停用,不允许新增")
  143. return
  144. }
  145. dept.Ancestors = info.Ancestors + "," + strconv.FormatInt(dept.ParentId, 10)
  146. dept.CreateBy = admin.GetUserInfo(c).UserName
  147. a.DeptService.Insert(dept)
  148. }
  149. // Edit 修改部门数据
  150. // @Summary 修改部门接口
  151. // @Description 新增部门接口
  152. // @Tags 部门相关接口
  153. // @Accept application/json
  154. // @Produce application/json
  155. // @Param Authorization header string false "Bearer 令牌"
  156. // @Param data body model.SysDept true "部门实体对象"
  157. // @Success 200 {object} resp.Response{msg=string} "操作状态"
  158. // @Router /dept/edit [put]
  159. func (a DeptApi) Edit(c *frame.Context) {
  160. dept := model.SysDept{}
  161. c.ValidteError(c.ShouldBind(&dept), &dept)
  162. a.DeptService.Update(dept)
  163. }
  164. // Delete 删除部门
  165. // @Summary 删除部门接口
  166. // @Description 删除部门接口
  167. // @Tags 部门相关接口
  168. // @Accept application/json
  169. // @Produce application/json
  170. // @Param Authorization header string false "Bearer 令牌"
  171. // @Param id path int true "id" id
  172. // @Success 200 {object} resp.Response{msg=string} "操作状态"
  173. // @Router /dept/remove [delete]
  174. func (a DeptApi) Delete(c *frame.Context) {
  175. var params struct {
  176. Id int64 `uri:"id" binding:"required" msg:"id不存在"` //ids
  177. }
  178. c.ValidteError(c.ShouldBindUri(&params), &params)
  179. //是否存在部门子节点
  180. if a.DeptService.HasChildByDeptId(params.Id) > 0 {
  181. frame.Throw(frame.BUSINESS_CODE, "存在下级部门,不允许删除")
  182. return
  183. }
  184. if a.DeptService.CheckDeptExistUser(params.Id) > 0 {
  185. frame.Throw(frame.BUSINESS_CODE, "部门存在用户,不允许删除")
  186. return
  187. }
  188. a.DeptService.Remove(params.Id)
  189. }