QuestionManagement.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace App\Filament\Pages;
  3. use App\Services\QuestionServiceApi;
  4. use App\Services\QuestionBankService;
  5. use BackedEnum;
  6. use Filament\Notifications\Notification;
  7. use Filament\Pages\Page;
  8. use UnitEnum;
  9. use Livewire\Attributes\Computed;
  10. use Illuminate\Support\Facades\Cache;
  11. class QuestionManagement extends Page
  12. {
  13. protected static ?string $title = '题库管理';
  14. protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-document-text';
  15. protected static ?string $navigationLabel = '题库管理';
  16. protected static string|UnitEnum|null $navigationGroup = '管理';
  17. protected static ?int $navigationSort = 11;
  18. protected string $view = 'filament.pages.question-management-simple';
  19. public ?string $search = null;
  20. public ?string $selectedKpCode = null;
  21. public ?string $selectedDifficulty = null;
  22. public ?string $selectedType = null;
  23. public int $currentPage = 1;
  24. public int $perPage = 25;
  25. #[Computed(cache: false)]
  26. public function questions(): array
  27. {
  28. $service = app(QuestionServiceApi::class);
  29. $filters = array_filter([
  30. 'kp_code' => $this->selectedKpCode,
  31. 'difficulty' => $this->selectedDifficulty,
  32. 'type' => $this->selectedType,
  33. 'search' => $this->search,
  34. ], fn ($value) => filled($value));
  35. $response = $service->listQuestions($this->currentPage, $this->perPage, $filters);
  36. return $response['data'] ?? [];
  37. }
  38. #[Computed(cache: false)]
  39. public function meta(): array
  40. {
  41. $service = app(QuestionServiceApi::class);
  42. $filters = array_filter([
  43. 'kp_code' => $this->selectedKpCode,
  44. 'difficulty' => $this->selectedDifficulty,
  45. 'type' => $this->selectedType,
  46. 'search' => $this->search,
  47. ], fn ($value) => filled($value));
  48. $response = $service->listQuestions($this->currentPage, $this->perPage, $filters);
  49. return $response['meta'] ?? ['page' => 1, 'per_page' => 25, 'total' => 0, 'total_pages' => 0];
  50. }
  51. #[Computed(cache: false)]
  52. public function statistics(): array
  53. {
  54. return app(QuestionServiceApi::class)->getStatistics();
  55. }
  56. #[Computed(cache: false)]
  57. public function knowledgePointOptions(): array
  58. {
  59. return app(QuestionServiceApi::class)->getKnowledgePointOptions();
  60. }
  61. public function deleteQuestion(string $questionCode): void
  62. {
  63. try {
  64. $service = app(\App\Services\QuestionBankService::class);
  65. $result = $service->deleteQuestion($questionCode);
  66. if ($result) {
  67. Notification::make()->title('删除成功')->body("题目 {$questionCode} 已删除")->success()->send();
  68. // 清除所有缓存,确保页面刷新
  69. Cache::flush();
  70. // 重新加载页面数据
  71. $this->dispatch('$refresh');
  72. } else {
  73. Notification::make()->title('删除失败')->body("题目 {$questionCode} 不存在或已被删除")->warning()->send();
  74. }
  75. } catch (\Exception $e) {
  76. Notification::make()->title('删除异常')->body($e->getMessage())->danger()->send();
  77. }
  78. }
  79. public function updatedSearch(): void
  80. {
  81. $this->currentPage = 1;
  82. }
  83. public function updatedSelectedKpCode(): void
  84. {
  85. $this->currentPage = 1;
  86. }
  87. public function updatedSelectedDifficulty(): void
  88. {
  89. $this->currentPage = 1;
  90. }
  91. public function updatedSelectedType(): void
  92. {
  93. $this->currentPage = 1;
  94. }
  95. public function updatedPerPage(): void
  96. {
  97. $this->currentPage = 1;
  98. }
  99. public function gotoPage(int $page): void
  100. {
  101. $this->currentPage = $page;
  102. }
  103. public function previousPage(): void
  104. {
  105. if ($this->currentPage > 1) {
  106. $this->currentPage--;
  107. }
  108. }
  109. public function nextPage(): void
  110. {
  111. if ($this->currentPage < ($this->meta['total_pages'] ?? 1)) {
  112. $this->currentPage++;
  113. }
  114. }
  115. public function getPages(): array
  116. {
  117. $totalPages = $this->meta['total_pages'] ?? 1;
  118. $currentPage = $this->currentPage;
  119. $pages = [];
  120. $start = max(1, $currentPage - 2);
  121. $end = min($totalPages, $currentPage + 2);
  122. for ($i = $start; $i <= $end; $i++) {
  123. $pages[] = $i;
  124. }
  125. return $pages;
  126. }
  127. }