diff --git a/catch/permissions/model/Department.php b/catch/permissions/model/Department.php index c2a87cb..efdf219 100644 --- a/catch/permissions/model/Department.php +++ b/catch/permissions/model/Department.php @@ -39,4 +39,26 @@ class Department extends CatchModel ->catchOrder() ->select()->toTree(); } + + + /** + * 获取部门 + * + * @time 2020年11月04日 + * @param array $departmentIds + * @throws DbException + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\ModelNotFoundException + * @return array + */ + public static function getAllChildrenIds(array $departmentIds) + { + $childDepartmentIds = self::whereIn('parent_id', $departmentIds)->column('id'); + + if (!empty($childDepartmentIds)) { + $childDepartmentIds = array_merge($childDepartmentIds, self::getAllChildrenIds($childDepartmentIds)); + } + + return $childDepartmentIds; + } } diff --git a/catch/permissions/model/search/UserSearch.php b/catch/permissions/model/search/UserSearch.php index 2dcf4cd..0e2ea8e 100644 --- a/catch/permissions/model/search/UserSearch.php +++ b/catch/permissions/model/search/UserSearch.php @@ -22,8 +22,10 @@ trait UserSearch public function searchDepartmentIdAttr($query, $value, $data) { - $departmentIds = Department::where('parent_id', $value)->column('id'); - $departmentIds[] = $value; + $departmentIds = Department::getAllChildrenIds([$value]); + + $departmentIds[] = intval($value); + return $query->whereIn($this->aliasField('department_id'), $departmentIds); } }