sys_department.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package services
  2. import (
  3. "eta/eta_mini_crm_ht/models"
  4. "strconv"
  5. "strings"
  6. )
  7. // GetSysDepartmentTree 递归获取部门目录树
  8. func GetSysDepartmentTree(list []*models.SysDepartmentList, parentId int, deepth int) []*models.SysDepartmentList {
  9. res := make([]*models.SysDepartmentList, 0)
  10. for _, v := range list {
  11. if deepth == 1 && v.Type == models.Department {
  12. continue
  13. }
  14. if deepth == 2 && (v.Level > 2 || v.Type == models.SubCompany) {
  15. continue
  16. }
  17. if deepth > 2 && (v.Level != deepth || v.Type == models.SubCompany) {
  18. continue
  19. }
  20. if v.ParentId == parentId {
  21. t := new(models.SysDepartmentList)
  22. t.SysDepartmentId = v.OutId
  23. t.SysDepartmentName = v.SysDepartmentName
  24. t.OutId = v.OutId
  25. t.Type = v.Type
  26. t.Level = v.Level
  27. t.ParentId = v.ParentId
  28. t.Child = GetSysDepartmentTree(list, v.OutId, deepth+1)
  29. res = append(res, t)
  30. }
  31. }
  32. return res
  33. }
  34. // DeleteSysDepartmentById 根据部门id去删除部门下的所有子目录,并清除用户的关联信息
  35. func DeleteSysDepartmentById(sysDepartmentId, level int) (err error) {
  36. sysDepartmentIds := make([]string, 0)
  37. sysDepartmentIds = append(sysDepartmentIds, strconv.Itoa(sysDepartmentId))
  38. var curIds = sysDepartmentIds
  39. var curStringIds string
  40. for i := level; i < 3; i++ {
  41. // 从当前层部门依次向下遍历子部门
  42. curStringIds = strings.Join(curIds, ",")
  43. if curStringIds == "" {
  44. break
  45. }
  46. sysDepartments, er := models.GetChildSysDepartmentListByIds(curStringIds)
  47. if er != nil {
  48. return er
  49. }
  50. curIds = make([]string, 0)
  51. for _, dep := range sysDepartments {
  52. if dep.SysDepartmentId == 0 {
  53. continue
  54. }
  55. sysDepartmentIds = append(sysDepartmentIds, strconv.Itoa(dep.SysDepartmentId))
  56. curIds = append(curIds, strconv.Itoa(dep.SysDepartmentId))
  57. }
  58. }
  59. err = models.DeleteSysDepartmentByIdV2(sysDepartmentIds, level)
  60. return
  61. }
  62. // GetSysDepartmentPathIdsById 根据当前部门id获取,部门层级列表pathIds [3]int,0索引表示1级部门id,1表示2级部门id...
  63. func GetSysDepartmentPathIdsById(sysDepartmentId int) (pathIds []int, err error) {
  64. sysDepartment, err := models.GetSysDepartmentById(sysDepartmentId)
  65. if err != nil {
  66. return
  67. }
  68. pathIds = make([]int, 3)
  69. pathIds[sysDepartment.Level-1] = sysDepartmentId
  70. curSysDepartmentId := sysDepartment.ParentId
  71. for curLevel := sysDepartment.Level - 1; curLevel >= 1; curLevel-- {
  72. upSysDepartment, er := models.GetSysDepartmentListByParentId(curSysDepartmentId)
  73. if er != nil {
  74. return
  75. }
  76. curSysDepartmentId = upSysDepartment.ParentId
  77. pathIds[curLevel-1] = upSysDepartment.SysDepartmentId
  78. }
  79. return
  80. }