sys_department.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package services
  2. import (
  3. "eta/eta_mini_crm/models"
  4. "eta/eta_mini_crm/utils"
  5. )
  6. // GetSysDepartmentTree 递归获取部门目录树
  7. func GetSysDepartmentTree(list []*models.SysDepartmentList, parentId int) []*models.SysDepartmentList {
  8. res := make([]*models.SysDepartmentList, 0)
  9. for _, v := range list {
  10. if v.ParentId == parentId {
  11. t := new(models.SysDepartmentList)
  12. t.SysDepartmentId = v.SysDepartmentId
  13. t.SysDepartmentName = v.SysDepartmentName
  14. t.Level = v.Level
  15. t.ParentId = v.ParentId
  16. t.Child = GetSysDepartmentTree(list, v.SysDepartmentId)
  17. res = append(res, t)
  18. }
  19. }
  20. return res
  21. }
  22. // DeleteSysDepartmentById 根据部门id去删除部门下的所有子目录,并清除用户的关联信息
  23. func DeleteSysDepartmentById(sysDepartmentId, level int) (msg string, err error) {
  24. sysDepartment, err := models.GetSysDepartmentById(sysDepartmentId)
  25. if err != nil {
  26. if err.Error() == utils.ErrNoRow() {
  27. msg = "部门不存在"
  28. return
  29. }
  30. return
  31. }
  32. if sysDepartment.SysDepartmentName == "营业部" {
  33. msg = "营业部不能被删除"
  34. return
  35. }
  36. parentDep, err := models.GetSysDepartmentById(sysDepartment.ParentId)
  37. if err != nil {
  38. if err.Error() == utils.ErrNoRow() {
  39. msg = "父部门不存在"
  40. return
  41. }
  42. return
  43. }
  44. if parentDep.SysDepartmentName == "营业部" {
  45. condition := ` AND seller_department_id=? `
  46. count, er := models.GetUserCount(condition, []interface{}{sysDepartment.SysDepartmentId})
  47. if er != nil {
  48. err = er
  49. return
  50. }
  51. if count > 0 {
  52. msg = "该营业部有关联用户,无法删除"
  53. return
  54. }
  55. }
  56. sysDepartmentIds := make([]int, 0)
  57. sysDepartmentIds = append(sysDepartmentIds, sysDepartmentId)
  58. var curIds = sysDepartmentIds
  59. var curStringIds []int
  60. for i := level; i < 3; i++ {
  61. // 从当前层部门依次向下遍历子部门
  62. curStringIds = curIds
  63. if len(curStringIds) == 0 {
  64. break
  65. }
  66. sysDepartments, er := models.GetChildSysDepartmentListByIds(curStringIds)
  67. if er != nil {
  68. err = er
  69. return
  70. }
  71. curIds = make([]int, 0)
  72. for _, dep := range sysDepartments {
  73. if dep.SysDepartmentId == 0 {
  74. continue
  75. }
  76. sysDepartmentIds = append(sysDepartmentIds, dep.SysDepartmentId)
  77. curIds = append(curIds, dep.SysDepartmentId)
  78. }
  79. }
  80. err = models.DeleteSysDepartmentByIdV2(sysDepartmentIds, level)
  81. return
  82. }
  83. // GetSysDepartmentPathIdsById 根据当前部门id获取,部门层级列表pathIds [3]int,0索引表示1级部门id,1表示2级部门id...
  84. func GetSysDepartmentPathIdsById(sysDepartmentId int) (pathIds []int, err error) {
  85. sysDepartment, err := models.GetSysDepartmentById(sysDepartmentId)
  86. if err != nil {
  87. return
  88. }
  89. pathIds = make([]int, 3)
  90. pathIds[sysDepartment.Level-1] = sysDepartmentId
  91. curSysDepartmentId := sysDepartment.ParentId
  92. for curLevel := sysDepartment.Level - 1; curLevel >= 1; curLevel-- {
  93. upSysDepartment, er := models.GetSysDepartmentById(curSysDepartmentId)
  94. if er != nil {
  95. return
  96. }
  97. curSysDepartmentId = upSysDepartment.ParentId
  98. pathIds[curLevel-1] = upSysDepartment.SysDepartmentId
  99. }
  100. return
  101. }