更新
This commit is contained in:
@@ -280,4 +280,61 @@ class DeptLogic extends BaseLogic
|
||||
return self::getTree($data, $pid);
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定部门及其全部下级部门 id(含自身)。筛选时选父级可匹配子级下成员(admin_dept.dept_id)。
|
||||
*
|
||||
* @return array<int>
|
||||
*/
|
||||
public static function getSelfAndDescendantIds(int $rootDeptId): array
|
||||
{
|
||||
if ($rootDeptId <= 0) {
|
||||
return [];
|
||||
}
|
||||
$rows = Dept::field(['id', 'pid'])->select()->toArray();
|
||||
if ($rows === []) {
|
||||
return [$rootDeptId];
|
||||
}
|
||||
$validIds = [];
|
||||
foreach ($rows as $r) {
|
||||
$id = (int) ($r['id'] ?? 0);
|
||||
if ($id > 0) {
|
||||
$validIds[$id] = true;
|
||||
}
|
||||
}
|
||||
if (!isset($validIds[$rootDeptId])) {
|
||||
return [$rootDeptId];
|
||||
}
|
||||
$childrenByPid = [];
|
||||
foreach ($rows as $r) {
|
||||
$pid = (int) ($r['pid'] ?? 0);
|
||||
$id = (int) ($r['id'] ?? 0);
|
||||
if ($id <= 0) {
|
||||
continue;
|
||||
}
|
||||
if (!isset($childrenByPid[$pid])) {
|
||||
$childrenByPid[$pid] = [];
|
||||
}
|
||||
$childrenByPid[$pid][] = $id;
|
||||
}
|
||||
$out = [];
|
||||
$queue = [$rootDeptId];
|
||||
$seen = [];
|
||||
while ($queue !== []) {
|
||||
$id = array_shift($queue);
|
||||
if (isset($seen[$id])) {
|
||||
continue;
|
||||
}
|
||||
$seen[$id] = true;
|
||||
$out[] = $id;
|
||||
foreach ($childrenByPid[$id] ?? [] as $cid) {
|
||||
$cid = (int) $cid;
|
||||
if ($cid > 0 && !isset($seen[$cid])) {
|
||||
$queue[] = $cid;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $out;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user