| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <?php
- namespace App\Filament\Pages;
- use App\Services\QuestionServiceApi;
- use App\Services\QuestionBankService;
- use BackedEnum;
- use Filament\Notifications\Notification;
- use Filament\Pages\Page;
- use UnitEnum;
- use Livewire\Attributes\Computed;
- use Illuminate\Support\Facades\Cache;
- class QuestionManagement extends Page
- {
- protected static ?string $title = '题库管理';
- protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-document-text';
- protected static ?string $navigationLabel = '题库管理';
- protected static string|UnitEnum|null $navigationGroup = '管理';
- protected static ?int $navigationSort = 11;
- protected string $view = 'filament.pages.question-management-simple';
- public ?string $search = null;
- public ?string $selectedKpCode = null;
- public ?string $selectedDifficulty = null;
- public ?string $selectedType = null;
- public int $currentPage = 1;
- public int $perPage = 25;
- #[Computed(cache: false)]
- public function questions(): array
- {
- $service = app(QuestionServiceApi::class);
- $filters = array_filter([
- 'kp_code' => $this->selectedKpCode,
- 'difficulty' => $this->selectedDifficulty,
- 'type' => $this->selectedType,
- 'search' => $this->search,
- ], fn ($value) => filled($value));
- $response = $service->listQuestions($this->currentPage, $this->perPage, $filters);
- return $response['data'] ?? [];
- }
- #[Computed(cache: false)]
- public function meta(): array
- {
- $service = app(QuestionServiceApi::class);
- $filters = array_filter([
- 'kp_code' => $this->selectedKpCode,
- 'difficulty' => $this->selectedDifficulty,
- 'type' => $this->selectedType,
- '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(cache: false)]
- public function statistics(): array
- {
- return app(QuestionServiceApi::class)->getStatistics();
- }
- #[Computed(cache: false)]
- public function knowledgePointOptions(): array
- {
- return app(QuestionServiceApi::class)->getKnowledgePointOptions();
- }
- public function deleteQuestion(string $questionCode): void
- {
- try {
- $service = app(\App\Services\QuestionBankService::class);
- $result = $service->deleteQuestion($questionCode);
- if ($result) {
- Notification::make()->title('删除成功')->body("题目 {$questionCode} 已删除")->success()->send();
- // 清除所有缓存,确保页面刷新
- Cache::flush();
- // 重新加载页面数据
- $this->dispatch('$refresh');
- } 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 updatedSelectedType(): 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;
- }
- }
|