QuestionManagement.php 4.2 KB

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