diff --git a/app/Http/Admin/Request/Permission/UserRequest.php b/app/Http/Admin/Request/Permission/UserRequest.php index 201332ce..5667f78a 100644 --- a/app/Http/Admin/Request/Permission/UserRequest.php +++ b/app/Http/Admin/Request/Permission/UserRequest.php @@ -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', + ], ]; } diff --git a/app/Model/Permission/Department.php b/app/Model/Permission/Department.php index 9352fbe7..44c56798 100644 --- a/app/Model/Permission/Department.php +++ b/app/Model/Permission/Department.php @@ -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'); } } diff --git a/app/Model/Permission/User.php b/app/Model/Permission/User.php index 14c40b61..6b497af8 100644 --- a/app/Model/Permission/User.php +++ b/app/Model/Permission/User.php @@ -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 { @@ -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'); + } } diff --git a/app/Repository/Permission/UserRepository.php b/app/Repository/Permission/UserRepository.php index ce37714e..f112ec79 100644 --- a/app/Repository/Permission/UserRepository.php +++ b/app/Repository/Permission/UserRepository.php @@ -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']); } } diff --git a/app/Service/Permission/DepartmentService.php b/app/Service/Permission/DepartmentService.php index 402f2e10..8977d837 100644 --- a/app/Service/Permission/DepartmentService.php +++ b/app/Service/Permission/DepartmentService.php @@ -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; }); } @@ -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']); } } diff --git a/app/Service/Permission/UserService.php b/app/Service/Permission/UserService.php index cd345f5e..0ea0f27f 100644 --- a/app/Service/Permission/UserService.php +++ b/app/Service/Permission/UserService.php @@ -81,6 +81,7 @@ public function create(array $data): mixed if (! empty($data['policy'])) { $entity->policy()->create($data['policy']); } + $this->handleWith($entity, $data); }); } @@ -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']); + } + } }