Skip to content

Commit

Permalink
fix dept level bug (#306)
Browse files Browse the repository at this point in the history
* fix dept level bug

fix dept level bug
修改参数名防止歧义

* composer cs
  • Loading branch information
ShaBaoFa committed Jul 11, 2024
1 parent d431f1a commit 3f11af4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
22 changes: 22 additions & 0 deletions app/System/Mapper/SystemDeptMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,18 @@ public function delLeader(int $id, array $users): bool
return true;
}

#[Transaction]
public function batchUpdate(array $update): bool
{
foreach ($update as $item) {
$result = parent::update($item['id'], $item['data']);
if (! $result) {
return false;
}
}
return true;
}

/**
* 查询部门名称.
*/
Expand All @@ -138,6 +150,12 @@ public function checkChildrenExists(int $id): bool
return $this->model::withTrashed()->where('parent_id', $id)->exists();
}

public function getDescendantsDepts(int $id): array
{
$params = ['level' => $id];
return $this->handleSearch($this->model::query(), $params)->get()->toArray();
}

/**
* 搜索处理器.
*/
Expand All @@ -147,6 +165,10 @@ public function handleSearch(Builder $query, array $params): Builder
$query->where('status', $params['status']);
}

if (isset($params['level']) && filled($params['level'])) {
$query->where('level', 'like', '%' . $params['level'] . '%');
}

if (isset($params['name']) && filled($params['name'])) {
$query->where('name', 'like', '%' . $params['name'] . '%');
}
Expand Down
4 changes: 2 additions & 2 deletions app/System/Mapper/SystemMenuMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,9 @@ public function checkChildrenExists(int $id): bool
/**
* 获取子孙menus.
*/
public function getDescendantsMenus(int $parentId): array
public function getDescendantsMenus(int $id): array
{
$params = ['level' => $parentId];
$params = ['level' => $id];
return $this->handleSearch($this->model::query(), $params)->get()->toArray();
}

Expand Down
27 changes: 26 additions & 1 deletion app/System/Service/SystemDeptService.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,23 @@ public function save(array $data): mixed
*/
public function update(mixed $id, array $data): bool
{
return $this->mapper->update($id, $this->handleData($data));
$handleData = $this->handleData($data);
if (! $this->checkChildrenExists($id)) {
return $this->mapper->update($id, $handleData);
}
$update[] = [
'id' => $id,
'data' => $handleData,
];
$descendants = $this->mapper->getDescendantsDepts((int) $id);
foreach ($descendants as $descendant) {
$handleDescendantDeptLevelData = $this->handleDescendantDeptLevels($descendant['level'], $handleData['level'], $id);
$update[] = [
'id' => $descendant['id'],
'data' => ['level' => $handleDescendantDeptLevelData],
];
}
return $this->mapper->batchUpdate($update);
}

/**
Expand Down Expand Up @@ -144,4 +160,13 @@ protected function handleData(array $data): array

return $data;
}

protected function handleDescendantDeptLevels(string $descendantLevel, string $handleDataLevel, int $id): string
{
$descendantLevelArr = explode(',', $descendantLevel);
$handleDataLevelArr = explode(',', $handleDataLevel);
$position = array_search($id, $descendantLevelArr);
array_splice($descendantLevelArr, 0, $position, $handleDataLevelArr);
return implode(',', $descendantLevelArr);
}
}

0 comments on commit 3f11af4

Please sign in to comment.