From 0b5c88301281f05b911e70764219f4901d2e3889 Mon Sep 17 00:00:00 2001 From: JaguarJack Date: Wed, 4 Nov 2020 08:20:15 +0800 Subject: [PATCH] =?UTF-8?q?update:=E6=94=AF=E6=8C=81=E6=97=A0=E9=99=90?= =?UTF-8?q?=E7=BA=A7=E9=83=A8=E9=97=A8=E6=90=9C=E7=B4=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- catch/permissions/model/Department.php | 22 +++++++++++++++++++ catch/permissions/model/search/UserSearch.php | 6 +++-- 2 files changed, 26 insertions(+), 2 deletions(-) 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); } }