where('system_id', $params['system_id']); } if (isset($params['status'])) { $query->where('status', $params['status']); } // 添加分页 $roles = $query->paginate($params['per_page'] ?? 15); return $roles; } /** * 获取角色详情 */ public function getRoleDetail(int $roleId) { $role = Role::find($roleId); if (!$role) { throw new BusinessException('角色不存在', 404); } return $role; } /** * 创建角色 */ public function createRole(array $data) { $role = new Role(); $role->fill($data); // 根据字段填充数据 try { $role->save(); } catch (\Throwable $e) { Log::error('创建角色失败:'.$e->getMessage()); throw new BusinessException('创建角色失败', 500); } return $role; } /** * 更新角色 */ public function updateRole(array $data) { $role = Role::find($data['id']); if (!$role) { throw new BusinessException('角色不存在', 404); } $role->update($data); // 更新字段 return $role; } /** * 删除角色 */ public function deleteRole(int $roleId) { $role = Role::find($roleId); if (!$role) { throw new BusinessException('角色不存在', 404); } try { $role->delete(); } catch (\Throwable $e) { Log::error('删除角色失败:'.$e->getMessage()); throw new BusinessException('删除角色失败', 500); } } }