| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- <?php
- namespace App\Filament\Pages;
- use App\Services\QuestionServiceApi;
- use BackedEnum;
- use Filament\Actions;
- use Filament\Notifications\Notification;
- use Filament\Pages\Page;
- use UnitEnum;
- use Livewire\Attributes\Computed;
- use Livewire\Attributes\On;
- class QuestionManagement extends Page
- {
- protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-rectangle-stack';
- protected static string|UnitEnum|null $navigationGroup = '题库系统';
- protected static ?string $navigationLabel = '题库管理';
- protected static ?int $navigationSort = 2;
- protected ?string $heading = '题库管理';
- protected string $view = 'filament.pages.question-management';
- public ?string $search = null;
- public ?string $selectedKpCode = null;
- public ?string $selectedDifficulty = null;
- public int $currentPage = 1;
- public int $perPage = 25;
- /**
- * 计算属性:从 API 获取题目列表
- */
- #[Computed]
- public function questions(): 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['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'),
- ];
- }
- }
|