Skip to content

Commit

Permalink
feat: enhance User and Department models with relationships and valid…
Browse files Browse the repository at this point in the history
…ation rules
  • Loading branch information
zds-s committed Mar 10, 2025
1 parent 0675c38 commit dbe139e
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 8 deletions.
20 changes: 20 additions & 0 deletions app/Http/Admin/Request/Permission/UserRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,26 @@ public function rules(): array
'integer',
'in:0,1',
],
'department' => [
'sometimes',
'array',
'min:1',
],
'department.*' => [
'sometimes',
'integer',
'exists:department,id',
],
'position' => [
'sometimes',
'array',
'min:1',
],
'position.*' => [
'sometimes',
'integer',
'exists:position,id',
],
];
}

Expand Down
2 changes: 1 addition & 1 deletion app/Model/Permission/Department.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,6 @@ public function leader(): BelongsToMany

public function children(): HasMany
{
return $this->hasMany(Department::class, 'parent_id', 'id');
return $this->hasMany(self::class, 'parent_id', 'id');
}
}
18 changes: 18 additions & 0 deletions app/Model/Permission/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
* @property null|Collection|Role[] $roles
* @property mixed $password 密码
* @property null|Policy $policy 数据权限策略
* @property Collection|Department[] $department 部门
* @property Collection|Department[] $dept_leader 部门领导
* @property Collection|Position[] $position 岗位
*/
final class User extends Model
{
Expand Down Expand Up @@ -145,4 +148,19 @@ public function policy(): BelongsTo
{
return $this->belongsTo(Policy::class, 'id', 'id');
}

public function department(): BelongsToMany
{
return $this->belongsToMany(Department::class, 'user_dept', 'user_id', 'dept_id');
}

public function dept_leader(): BelongsToMany
{
return $this->belongsToMany(Department::class, 'dept_leader', 'user_id', 'dept_id');
}

public function position(): BelongsToMany
{
return $this->belongsToMany(Position::class, 'user_position', 'user_id', 'position_id');
}
}
15 changes: 14 additions & 1 deletion app/Repository/Permission/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ public function handleSearch(Builder $query, array $params): Builder
$query->where('role_id', $roleId);
});
})
->with(['policy']);
->when(Arr::get($params, 'department_id'), static function (Builder $query, $departmentId) {
$query->where(static function (Builder $query) use ($departmentId) {
$query->whereHas('department', static function (Builder $query) use ($departmentId) {
$query->where('id', $departmentId);
});
$query->orWhereHas('dept_leader', static function (Builder $query) use ($departmentId) {
$query->where('id', $departmentId);
});
$query->orWhereHas('position.department', static function (Builder $query) use ($departmentId) {
$query->where('id', $departmentId);
});
});
})
->with(['policy', 'department', 'dept_leader', 'position']);
}
}
12 changes: 6 additions & 6 deletions app/Service/Permission/DepartmentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function create(array $data): mixed
{
return Db::transaction(function () use ($data) {
$entity = $this->repository->create($data);
$this->handleEntity($entity,$data);
$this->handleEntity($entity, $data);
return $entity;
});
}
Expand All @@ -41,19 +41,19 @@ public function updateById(mixed $id, array $data): mixed
{
return Db::transaction(function () use ($id, $data) {
$entity = $this->repository->findById($id);
if (empty($entity)){
if (empty($entity)) {
throw new BusinessException(ResultCode::NOT_FOUND);
}
$this->handleEntity($entity,$data);
$this->handleEntity($entity, $data);
});
}

protected function handleEntity(Department $entity,array $data): void
protected function handleEntity(Department $entity, array $data): void
{
if (!empty($data['department_users'])){
if (! empty($data['department_users'])) {
$entity->department_users()->sync($data['department_users']);
}
if (!empty($data['leader'])){
if (! empty($data['leader'])) {
$entity->leader()->sync($data['leader']);
}
}
Expand Down
12 changes: 12 additions & 0 deletions app/Service/Permission/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public function create(array $data): mixed
if (! empty($data['policy'])) {
$entity->policy()->create($data['policy']);
}
$this->handleWith($entity, $data);
});
}

Expand All @@ -96,6 +97,17 @@ public function updateById(mixed $id, array $data): mixed
if (! empty($data['policy'])) {
$entity->policy()->update($data['policy']);
}
$this->handleWith($entity, $data);
});
}

protected function handleWith(User $entity, array $data): void
{
if (! empty($data['department'])) {
$entity->department()->sync($data['department']);
}
if (! empty($data['position'])) {
$entity->position()->sync($data['position']);
}
}
}

0 comments on commit dbe139e

Please sign in to comment.