package services import ( "eta/eta_mini_crm/models" "eta/eta_mini_crm/utils" ) // GetSysDepartmentTree 递归获取部门目录树 func GetSysDepartmentTree(list []*models.SysDepartmentList, parentId int) []*models.SysDepartmentList { res := make([]*models.SysDepartmentList, 0) for _, v := range list { if v.ParentId == parentId { t := new(models.SysDepartmentList) t.SysDepartmentId = v.SysDepartmentId t.SysDepartmentName = v.SysDepartmentName t.Level = v.Level t.ParentId = v.ParentId t.Child = GetSysDepartmentTree(list, v.SysDepartmentId) res = append(res, t) } } return res } // DeleteSysDepartmentById 根据部门id去删除部门下的所有子目录,并清除用户的关联信息 func DeleteSysDepartmentById(sysDepartmentId, level int) (msg string, err error) { sysDepartment, err := models.GetSysDepartmentById(sysDepartmentId) if err != nil { if err.Error() == utils.ErrNoRow() { msg = "部门不存在" return } return } if sysDepartment.SysDepartmentName == "营业部" { msg = "营业部不能被删除" return } parentDep, err := models.GetSysDepartmentById(sysDepartment.ParentId) if err != nil { if err.Error() == utils.ErrNoRow() { msg = "父部门不存在" return } return } if parentDep.SysDepartmentName == "营业部" { condition := ` AND seller_department_id=? ` count, er := models.GetUserCount(condition, []interface{}{sysDepartment.SysDepartmentId}) if er != nil { err = er return } if count > 0 { msg = "该营业部有关联用户,无法删除" return } } sysDepartmentIds := make([]int, 0) sysDepartmentIds = append(sysDepartmentIds, sysDepartmentId) var curIds = sysDepartmentIds var curStringIds []int for i := level; i < 3; i++ { // 从当前层部门依次向下遍历子部门 curStringIds = curIds if len(curStringIds) == 0 { break } sysDepartments, er := models.GetChildSysDepartmentListByIds(curStringIds) if er != nil { err = er return } curIds = make([]int, 0) for _, dep := range sysDepartments { if dep.SysDepartmentId == 0 { continue } sysDepartmentIds = append(sysDepartmentIds, dep.SysDepartmentId) curIds = append(curIds, 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.GetSysDepartmentById(curSysDepartmentId) if er != nil { return } curSysDepartmentId = upSysDepartment.ParentId pathIds[curLevel-1] = upSysDepartment.SysDepartmentId } return }