sys_dept.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. package system
  2. import (
  3. "errors"
  4. "hongze/hrms_api/models/base"
  5. "hongze/hrms_api/models/system"
  6. "hongze/hrms_api/utils"
  7. )
  8. func DeptTreeList(page base.IPage, req *system.DeptListReq) (count int64, list []*system.DeptListItemResp, err error, errMsg string) {
  9. dept := new(system.SysDept)
  10. var condition string
  11. var pars []interface{}
  12. //根据名称找到所有最顶层的节点
  13. var finalRootIds []int64
  14. if req.DeptName != "" {
  15. var rootIds []int64
  16. deptName := "%"+req.DeptName+"%"
  17. relationList, tErr := dept.GetDeptByName(deptName)
  18. if tErr != nil {
  19. errMsg = "查询部门出错"
  20. err = errors.New(errMsg+"Err:" + tErr.Error())
  21. return
  22. }
  23. if len(relationList) == 0 { //查不到相关记录
  24. return
  25. }
  26. for _, v := range relationList {
  27. if v.RootId == 0 {
  28. rootIds = append(rootIds, v.DeptId)
  29. }else{
  30. rootIds = append(rootIds, v.RootId)
  31. }
  32. }
  33. //分页处理 查询最顶层结点,得到最终的顶层结点
  34. condition = "parent_id=0 and dept_id in ?"
  35. pars = append(pars, rootIds)
  36. }else{
  37. condition = "parent_id=0"
  38. }
  39. count, rootList, err := dept.SelectPage(page, condition, pars)
  40. if err != nil {
  41. errMsg = "查询部门列表出错"
  42. err = errors.New(errMsg+"Err:" + err.Error())
  43. return
  44. }
  45. if len(rootList) == 0 { //查不到相关记录
  46. return
  47. }
  48. for _, v := range rootList {
  49. finalRootIds = append(finalRootIds, v.DeptId)
  50. }
  51. //查询所有该顶层节点下的子节点
  52. fCondition := "root_id in ? or dept_id in ?"
  53. fPars := make([]interface{}, 0)
  54. fPars = append(fPars, finalRootIds, finalRootIds)
  55. orderStr := "sort asc, dept_id desc"
  56. childList, err := dept.GetDeptListByCondition(fCondition, fPars, orderStr)
  57. if err != nil {
  58. errMsg = "查询部门列表出错"
  59. err = errors.New(errMsg+"Err:" + err.Error())
  60. return
  61. }
  62. list = make([]*system.DeptListItemResp, 0)
  63. for _, v:= range childList {
  64. tmp := &system.DeptListItemResp{
  65. DeptId: v.DeptId,
  66. ParentId: v.ParentId,
  67. DeptName: v.DeptName,
  68. Sort: v.Sort,
  69. CreateTime: utils.TimeTransferString(utils.FormatDateTime, v.CreateTime),
  70. ModifyTime: utils.TimeTransferString(utils.FormatDateTime, v.ModifyTime),
  71. Children: nil,
  72. }
  73. list = append(list, tmp)
  74. }
  75. //组装返回值
  76. list = getDeptTreeRecursive(list, 0)
  77. return
  78. }
  79. func getDeptTreeRecursive(list []*system.DeptListItemResp, parentId int64) []*system.DeptListItemResp {
  80. res := make([]*system.DeptListItemResp, 0)
  81. for _, v := range list {
  82. if v.ParentId == parentId {
  83. v.Children = getDeptTreeRecursive(list, v.DeptId)
  84. res = append(res, v)
  85. }
  86. }
  87. return res
  88. }
  89. // DeleteDept 删除部门,连同删除子部门
  90. func DeleteDept(req *system.DelSysDeptReq) (err error, errMsg string) {
  91. dept := new(system.SysDept)
  92. //找出当前层级,如果当前是最顶层,则批量删除和顶层相关的所有部门
  93. deptInfo, err := dept.GetDeptByDeptId(req.DeptId)
  94. if err != nil {
  95. if err == utils.ErrNoRow {
  96. errMsg = "部门不存在"
  97. err = errors.New(errMsg+"Err:" + err.Error())
  98. return
  99. }
  100. errMsg = "查询部门出错"
  101. err = errors.New(errMsg+"Err:" + err.Error())
  102. return
  103. }
  104. admin := new(system.SysAdmin)
  105. var admins []*system.SysAdmin
  106. var depts []*system.SysDept
  107. var deptIdMap map[int64][]int64
  108. if deptInfo.ParentId > 0 {
  109. //如果不是最顶层,则构建树形图,得到需要删除的dept_id
  110. //查询所有该顶层节点下的子节点
  111. deptIdMap, err, errMsg = GetChildDeptIds(deptInfo.RootId)
  112. if err != nil {
  113. return err, errMsg
  114. }
  115. cond := "dept_id in ?"
  116. pars := make([]interface{}, 0)
  117. pars = append(pars, deptIdMap[deptInfo.DeptId])
  118. //验证是否存在绑定的员工账号
  119. admins, err = admin.GetAdminListByCondition(cond, pars)
  120. if err != nil {
  121. errMsg = "查询部门下的用户出错"
  122. err = errors.New(errMsg+"Err:" + err.Error())
  123. return
  124. }
  125. if len(admins) > 0 {
  126. errMsg = "该部门绑定了员工,不可删除"
  127. err = errors.New(errMsg)
  128. return
  129. }
  130. err = dept.DeleteByCondition(cond, pars)
  131. }else{
  132. //如果是最顶层,则删除所有相关的dept_id
  133. cond := "dept_id = ? or root_id = ?"
  134. pars := make([]interface{}, 0)
  135. pars = append(pars, deptInfo.DeptId, deptInfo.DeptId)
  136. depts, err = dept.GetDeptListByCondition(cond, pars, "")
  137. if err != nil {
  138. errMsg = "查询部门信息出错"
  139. err = errors.New(errMsg+"Err:" + err.Error())
  140. return
  141. }
  142. var deptIds []int64
  143. for _, v :=range depts {
  144. deptIds = append(deptIds, v.DeptId)
  145. }
  146. //验证是否存在绑定的员工账号
  147. aCond := "dept_id in ?"
  148. aPars := make([]interface{}, 0)
  149. aPars = append(aPars, deptIds)
  150. admins, err = admin.GetAdminListByCondition(aCond, aPars)
  151. if err != nil {
  152. errMsg = "查询部门下的员工出错"
  153. err = errors.New(errMsg+"Err:" + err.Error())
  154. return
  155. }
  156. if len(admins) > 0 {
  157. errMsg = "该部门绑定了员工,不可删除"
  158. err = errors.New(errMsg)
  159. return
  160. }
  161. err = dept.DeleteByCondition(cond, pars)
  162. }
  163. return
  164. }
  165. func GetChildDeptIds(rootId int64) (deptIdMap map[int64][]int64, err error, errMsg string) {
  166. dept := new(system.SysDept)
  167. fCondition := ""
  168. fPars := make([]interface{}, 0)
  169. if rootId > 0 {
  170. fCondition = "root_id = ? or dept_id = ?"
  171. fPars = append(fPars, rootId, rootId)
  172. }
  173. orderStr := "level desc"
  174. childList, tErr := dept.GetDeptListByCondition(fCondition, fPars, orderStr)
  175. if tErr != nil {
  176. errMsg = "查询部门列表出错"
  177. err = errors.New(errMsg+"Err:" + tErr.Error())
  178. return
  179. }
  180. deptIdMap = make(map[int64][]int64)
  181. for _, v := range childList {
  182. deptIdMap[v.DeptId] = append(deptIdMap[v.DeptId], v.DeptId)
  183. if v.ParentId > 0 {
  184. deptIdMap[v.ParentId] = append(deptIdMap[v.ParentId], deptIdMap[v.DeptId]...)
  185. }
  186. }
  187. return
  188. }
  189. func GetChildDeptNames() (deptNameMap map[int64]string, err error, errMsg string) {
  190. dept := new(system.SysDept)
  191. fCondition := ""
  192. fPars := make([]interface{}, 0)
  193. orderStr := "level asc"
  194. childList, tErr := dept.GetDeptListByCondition(fCondition, fPars, orderStr)
  195. if tErr != nil {
  196. errMsg = "查询部门列表出错"
  197. err = errors.New(errMsg+"Err:" + tErr.Error())
  198. return
  199. }
  200. deptNameMap = make(map[int64]string)
  201. for _, v := range childList {
  202. if v.ParentId > 0 {
  203. deptNameMap[v.DeptId] = deptNameMap[v.ParentId] + "/" + v.DeptName
  204. }else{
  205. deptNameMap[v.DeptId] = v.DeptName
  206. }
  207. }
  208. return
  209. }
  210. func DeptAdminTreeList() (list []*system.DeptAdminListItem, err error, errMsg string) {
  211. dept := new(system.SysDept)
  212. var condition string
  213. var pars []interface{}
  214. childList, err := dept.GetDeptListByCondition(condition, pars, "")
  215. if err != nil {
  216. errMsg = "查询部门列表出错"
  217. err = errors.New(errMsg+"Err:" + err.Error())
  218. return
  219. }
  220. if len(childList) == 0 { //查不到相关记录
  221. return
  222. }
  223. // 查询管理员信息
  224. admin := new(system.SysAdmin)
  225. var list1 []*system.DeptAdminListItem
  226. adminList, err := admin.GetAdminListByCondition(condition, pars)
  227. adminMap := make(map[int64][]*system.DeptAdminListItem)
  228. for _, v := range adminList {
  229. tmp1 := &system.DeptAdminListItem{
  230. AdminId: v.AdminId,
  231. Name: v.RealName,
  232. DeptId: int64(v.AdminId)+1000,
  233. ParentId: v.DeptId,
  234. Sort: 0,
  235. Children: nil,
  236. }
  237. list1 = append(list1, tmp1)
  238. }
  239. list = make([]*system.DeptAdminListItem, 0)
  240. for _, v:= range childList {
  241. tmp := &system.DeptAdminListItem{
  242. DeptId: v.DeptId,
  243. Name: v.DeptName,
  244. ParentId: v.ParentId,
  245. Sort: v.Sort,
  246. Children: nil,
  247. }
  248. if c, ok := adminMap[v.DeptId]; ok {
  249. tmp.Children = c
  250. }
  251. list = append(list, tmp)
  252. }
  253. list = append(list, list1...)
  254. //组装返回值
  255. list = getDeptAdminTreeRecursive(list, 0)
  256. return
  257. }
  258. func getDeptAdminTreeRecursive(list []*system.DeptAdminListItem, parentId int64) []*system.DeptAdminListItem {
  259. res := make([]*system.DeptAdminListItem, 0)
  260. for _, v := range list {
  261. if v.ParentId == parentId {
  262. v.Children = getDeptAdminTreeRecursive(list, v.DeptId)
  263. res = append(res, v)
  264. }
  265. }
  266. return res
  267. }