package services import ( "eta/eta_mini_crm_ht/models" "strconv" "strings" ) // GetSysDepartmentTree 递归获取部门目录树 func GetSysDepartmentTree(list []*models.SysDepartmentList, parentId int, deepth int) []*models.SysDepartmentList { res := make([]*models.SysDepartmentList, 0) for _, v := range list { if deepth == 1 && v.Type == models.Department { continue } if deepth == 2 && (v.Level > 2 || v.Type == models.SubCompany) { continue } if deepth > 2 && (v.Level != deepth || v.Type == models.SubCompany) { continue } if v.ParentId == parentId { t := new(models.SysDepartmentList) t.SysDepartmentId = v.OutId t.SysDepartmentName = v.SysDepartmentName t.OutId = v.OutId t.Type = v.Type t.Level = v.Level t.ParentId = v.ParentId t.Child = GetSysDepartmentTree(list, v.OutId, deepth+1) res = append(res, t) } } return res } // DeleteSysDepartmentById 根据部门id去删除部门下的所有子目录,并清除用户的关联信息 func DeleteSysDepartmentById(sysDepartmentId, level int) (err error) { sysDepartmentIds := make([]string, 0) sysDepartmentIds = append(sysDepartmentIds, strconv.Itoa(sysDepartmentId)) var curIds = sysDepartmentIds var curStringIds string for i := level; i < 3; i++ { // 从当前层部门依次向下遍历子部门 curStringIds = strings.Join(curIds, ",") if curStringIds == "" { break } sysDepartments, er := models.GetChildSysDepartmentListByIds(curStringIds) if er != nil { return er } curIds = make([]string, 0) for _, dep := range sysDepartments { if dep.SysDepartmentId == 0 { continue } sysDepartmentIds = append(sysDepartmentIds, strconv.Itoa(dep.SysDepartmentId)) curIds = append(curIds, strconv.Itoa(dep.SysDepartmentId)) } } err = models.DeleteSysDepartmentByIdV2(sysDepartmentIds, level) return } // GetSysDepartmentPathIdsById 根据当前部门id获取,部门层级列表pathIds [3]int,0索引表示1级部门id,1表示2级部门id... func GetSysDepartmentPathIdsById(sysDepartmentId int) (pathIds []int, err error) { sysDepartment, err := models.GetSysDepartmentById(sysDepartmentId) if err != nil { return } pathIds = make([]int, 3) pathIds[sysDepartment.Level-1] = sysDepartmentId curSysDepartmentId := sysDepartment.ParentId for curLevel := sysDepartment.Level - 1; curLevel >= 1; curLevel-- { upSysDepartment, er := models.GetSysDepartmentListByParentId(curSysDepartmentId) if er != nil { return } curSysDepartmentId = upSysDepartment.ParentId pathIds[curLevel-1] = upSysDepartment.SysDepartmentId } return }