$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 { return app(QuestionServiceApi::class)->getStatistics(); } #[Computed] public function knowledgePointOptions(): array { return app(QuestionServiceApi::class)->getKnowledgePointOptions(); } #[Computed] public function skillsOptions(): array { if (!$this->generateKpCode) { return []; } $service = app(KnowledgeGraphService::class); return $service->getSkillsByKnowledgePoint($this->generateKpCode); } public function openGenerateModal(): void { $this->showGenerateModal = true; } public function closeGenerateModal(): void { $this->showGenerateModal = false; $this->reset(['generateKpCode', 'selectedSkills', 'questionCount']); } public function updatedGenerateKpCode(): void { // 选择新知识点时重置技能选择 $this->selectedSkills = []; } public function toggleAllSkills(): void { $skills = $this->skillsOptions; if (count($this->selectedSkills) === count($skills)) { $this->selectedSkills = []; } else { $this->selectedSkills = array_column($skills, 'code'); } } public function executeGenerate(): void { if (!$this->generateKpCode) { Notification::make()->title('请选择知识点')->danger()->send(); return; } if (empty($this->selectedSkills)) { Notification::make()->title('请选择至少一个技能')->danger()->send(); return; } try { $service = app(QuestionBankService::class); $callbackUrl = route('api.questions.callback'); $result = $service->generateIntelligentQuestions([ 'kp_code' => $this->generateKpCode, 'skills' => $this->selectedSkills, 'count' => $this->questionCount, 'prompt_template' => $this->promptTemplate ?? null ], $callbackUrl); if ($result['success'] ?? false) { $this->currentTaskId = $result['task_id'] ?? null; $this->showGenerateModal = false; Notification::make()->title('任务已创建')->body("任务 ID: {$this->currentTaskId}")->info()->send(); } else { Notification::make()->title('创建任务失败')->body($result['message'] ?? '未知错误')->danger()->send(); } } catch (\Exception $e) { Notification::make()->title('生成异常')->body($e->getMessage())->danger()->send(); } } public function deleteQuestion(string $questionCode): void { try { $service = app(QuestionBankService::class); $result = $service->deleteQuestion($questionCode); if ($result) { Notification::make()->title('删除成功')->body("题目 {$questionCode} 已删除")->success()->send(); $this->dispatch('refresh-page'); } else { Notification::make()->title('删除失败')->body("题目 {$questionCode} 不存在或已被删除")->warning()->send(); } } catch (\Exception $e) { Notification::make()->title('删除异常')->body($e->getMessage())->danger()->send(); } } 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; } 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; } }