$this->selectedKpCode, 'difficulty' => $this->selectedDifficulty, 'search' => $this->search, ], fn ($value) => filled($value)); $response = $service->listQuestions($this->currentPage, $this->perPage, $filters); return $response['data'] ?? []; } /** * 计算属性:分页信息 */ #[Computed] public function meta(): array { $service = app(QuestionServiceApi::class); $filters = array_filter([ 'kp_code' => $this->selectedKpCode, 'difficulty' => $this->selectedDifficulty, 'search' => $this->search, ], fn ($value) => filled($value)); $response = $service->listQuestions($this->currentPage, $this->perPage, $filters); return $response['meta'] ?? [ 'page' => 1, 'per_page' => 25, 'total' => 0, 'total_pages' => 0, ]; } /** * 计算属性:统计数据 */ #[Computed] public function statistics(): array { $service = app(QuestionServiceApi::class); return $service->getStatistics(); } /** * 计算属性:知识点选项 */ #[Computed] public function knowledgePointOptions(): array { $service = app(QuestionServiceApi::class); return $service->getKnowledgePointOptions(); } /** * 搜索更新处理 */ public function updatedSearch(): void { $this->currentPage = 1; } /** * 知识点筛选更新处理 */ public function updatedSelectedKpCode(): void { $this->currentPage = 1; } /** * 难度筛选更新处理 */ public function updatedSelectedDifficulty(): void { $this->currentPage = 1; } /** * 每页数量更新处理 */ public function updatedPerPage(): void { $this->currentPage = 1; } /** * 刷新数据 */ #[On('refresh-data')] public function refreshData(): void { $this->resetCache(); Notification::make() ->title('数据已刷新') ->success() ->send(); } /** * 重置缓存 */ private function resetCache(): void { // 清除相关缓存 cache()->forget('question-list-' . md5(json_encode([ 'page' => $this->currentPage, 'per_page' => $this->perPage, 'filters' => array_filter([ 'kp_code' => $this->selectedKpCode, 'difficulty' => $this->selectedDifficulty, 'search' => $this->search, ]), ]))); cache()->forget('question-statistics'); } /** * AI 生成题目 */ #[On('ai-generate')] public function aiGenerate(): void { // 调用智能题目生成API try { $response = Http::timeout(60)->post('http://localhost:5015/generate-intelligent-questions', [ 'knowledge_points' => ['KP1001'], 'max_questions_per_skill' => 5 ]); if ($response->successful()) { Notification::make() ->title('AI 生成成功') ->body('因式分解题目生成任务已启动,请稍后查看结果') ->success() ->send(); } else { Notification::make() ->title('AI 生成失败') ->body('请检查API服务状态') ->danger() ->send(); } } catch (\Exception $e) { Notification::make() ->title('AI 生成异常') ->body($e->getMessage()) ->danger() ->send(); } } /** * 智能搜索 */ #[On('smart-search')] public function smartSearch(): void { Notification::make() ->title('智能搜索功能') ->body('请使用搜索框输入关键词') ->info() ->send(); } /** * 跳转到指定页 */ public function gotoPage(int $page): void { $this->currentPage = $page; } /** * 上一页 */ public function previousPage(): void { if ($this->currentPage > 1) { $this->currentPage--; } } /** * 下一页 */ public function nextPage(): void { if ($this->currentPage < ($this->meta['total_pages'] ?? 1)) { $this->currentPage++; } } /** * 获取页码数组(用于分页器) */ public function getPages(): array { $totalPages = $this->meta['total_pages'] ?? 1; $currentPage = $this->currentPage; $pages = []; $start = max(1, $currentPage - 2); $end = min($totalPages, $currentPage + 2); for ($i = $start; $i <= $end; $i++) { $pages[] = $i; } return $pages; } /** * 头部操作按钮 */ protected function getHeaderActions(): array { return [ Actions\Action::make('ai_generate') ->label('AI 生成题目') ->icon('heroicon-m-sparkles') ->color('success') ->action('aiGenerate'), Actions\Action::make('smart_search') ->label('智能搜索') ->icon('heroicon-m-magnifying-glass') ->color('info') ->action('smartSearch'), Actions\Action::make('refresh') ->label('刷新') ->icon('heroicon-m-arrow-path') ->color('warning') ->action('refreshData'), ]; } }