sys_dept.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package system
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "github.com/go-playground/validator/v10"
  5. "hongze/fms_api/controller/resp"
  6. "hongze/fms_api/global"
  7. "hongze/fms_api/models/base"
  8. "hongze/fms_api/models/system"
  9. systemService "hongze/fms_api/services/system"
  10. "hongze/fms_api/utils"
  11. )
  12. type SysDeptController struct {
  13. }
  14. // List 部门列表
  15. func (s *SysDeptController) List(c *gin.Context) {
  16. req := new(system.DeptListReq)
  17. err := c.BindQuery(&req)
  18. if err != nil {
  19. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  20. return
  21. }
  22. page := new(base.Page)
  23. page.SetPageSize(req.PageSize)
  24. page.SetCurrent(req.Current)
  25. count, list, err, _ := systemService.DeptTreeList(page, req)
  26. if err != nil {
  27. resp.FailMsg("获取失败", "获取失败,Err:"+err.Error(), c)
  28. return
  29. }
  30. page.SetTotal(count)
  31. resultPage := &base.BaseData{
  32. Page: page,
  33. List: list,
  34. }
  35. resp.OkData("获取成功", resultPage, c)
  36. }
  37. // Add 新增部门
  38. func (s *SysDeptController) Add(c *gin.Context) {
  39. req := new(system.SysDeptAddReq)
  40. err := c.ShouldBind(&req)
  41. if err != nil {
  42. errs, ok := err.(validator.ValidationErrors)
  43. if !ok {
  44. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  45. return
  46. }
  47. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  48. return
  49. }
  50. item := new(system.SysDept)
  51. item.ParentId = req.ParentId
  52. item.DeptName = req.DeptName
  53. item.Sort = req.Sort
  54. if req.ParentId > 0 {
  55. parentInfo, tErr := item.GetDeptByDeptId(req.ParentId)
  56. if tErr != nil {
  57. if tErr == utils.ErrNoRow {
  58. resp.Fail("上级部门不存在", c)
  59. return
  60. }
  61. resp.FailMsg("查询上级部门出错", tErr.Error(), c)
  62. return
  63. }
  64. if parentInfo.RootId == 0 {
  65. item.RootId = parentInfo.DeptId
  66. } else {
  67. item.RootId = parentInfo.RootId
  68. }
  69. item.Level = parentInfo.Level + 1
  70. }
  71. // 判断是否存在相同的部门名称
  72. cond := "parent_id = ? and dept_name = ?"
  73. pars := make([]interface{}, 0)
  74. pars = append(pars, item.ParentId, item.DeptName)
  75. exitList, err := item.GetDeptListByCondition(cond, pars, "")
  76. if err != nil {
  77. resp.FailMsg("查询部门出错", err.Error(), c)
  78. return
  79. }
  80. if len(exitList) > 0 {
  81. resp.Fail("该部门名称已存在", c)
  82. return
  83. }
  84. err = item.Add()
  85. if err != nil {
  86. resp.FailMsg("保存失败", "保存失败,Err:"+err.Error(), c)
  87. return
  88. }
  89. resp.Ok("保存成功", c)
  90. }
  91. // Edit 编辑部门
  92. func (s *SysDeptController) Edit(c *gin.Context) {
  93. req := new(system.SysDeptEditReq)
  94. err := c.ShouldBind(&req)
  95. if err != nil {
  96. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  97. return
  98. }
  99. item := new(system.SysDept)
  100. item.DeptId = req.DeptId
  101. item.ParentId = req.ParentId
  102. item.DeptName = req.DeptName
  103. item.Sort = req.Sort
  104. //查询当前部门是否已存在
  105. deptInfo, err := item.GetDeptByDeptId(req.DeptId)
  106. if err != nil {
  107. if err == utils.ErrNoRow {
  108. resp.Fail("该部门不存在", c)
  109. return
  110. }
  111. resp.FailMsg("查询部门出错", err.Error(), c)
  112. return
  113. }
  114. updateCols := make([]string, 0)
  115. if deptInfo.ParentId > 0 && deptInfo.ParentId != item.ParentId {
  116. parentInfo, tErr := item.GetDeptByDeptId(req.ParentId)
  117. if tErr != nil {
  118. if tErr == utils.ErrNoRow {
  119. resp.Fail("上级部门不存在", c)
  120. return
  121. }
  122. resp.FailMsg("查询上级部门出错", tErr.Error(), c)
  123. return
  124. }
  125. if parentInfo.RootId == 0 {
  126. item.RootId = parentInfo.DeptId
  127. } else {
  128. item.RootId = parentInfo.RootId
  129. }
  130. item.Level = parentInfo.Level + 1
  131. updateCols = append(updateCols, "parent_id", "root_id", "level")
  132. } else if req.ParentId == 0 && deptInfo.ParentId != item.ParentId {
  133. item.RootId = 0
  134. item.Level = 0
  135. updateCols = append(updateCols, "parent_id", "root_id", "level")
  136. }
  137. if deptInfo.Sort != item.Sort {
  138. updateCols = append(updateCols, "sort")
  139. }
  140. if deptInfo.DeptName != item.DeptName {
  141. cond := "parent_id = ? and dept_name = ?"
  142. pars := make([]interface{}, 0)
  143. pars = append(pars, item.ParentId, item.DeptName)
  144. exitList, err := item.GetDeptListByCondition(cond, pars, "")
  145. if err != nil {
  146. resp.FailMsg("查询部门出错", err.Error(), c)
  147. return
  148. }
  149. if len(exitList) > 0 {
  150. resp.Fail("该部门名称已存在", c)
  151. return
  152. }
  153. updateCols = append(updateCols, "dept_name")
  154. }
  155. if len(updateCols) == 0 {
  156. resp.Ok("保存成功", c)
  157. return
  158. }
  159. err = item.Update(updateCols)
  160. if err != nil {
  161. resp.FailMsg("保存失败", "保存失败,Err:"+err.Error(), c)
  162. return
  163. }
  164. resp.Ok("保存成功", c)
  165. }
  166. // Delete 删除部门
  167. func (s *SysDeptController) Delete(c *gin.Context) {
  168. req := new(system.DelSysDeptReq)
  169. err := c.ShouldBind(&req)
  170. if err != nil {
  171. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  172. return
  173. }
  174. err, errMsg := systemService.DeleteDept(req)
  175. if err != nil {
  176. resp.FailMsg(errMsg, err.Error(), c)
  177. return
  178. }
  179. resp.Ok("删除成功", c)
  180. }
  181. // AdminList 部门列表
  182. func (s *SysDeptController) AdminList(c *gin.Context) {
  183. list, err, errMsg := systemService.DeptAdminTreeList()
  184. if err != nil {
  185. resp.FailMsg(errMsg, err.Error(), c)
  186. return
  187. }
  188. resultPage := &base.BaseData{
  189. List: list,
  190. }
  191. resp.OkData("获取成功", resultPage, c)
  192. }