QuestionManagement.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. public bool $showDetailModal = false;
  26. public array $editing = [];
  27. #[Computed(cache: false)]
  28. public function questions(): array
  29. {
  30. $service = app(QuestionServiceApi::class);
  31. $filters = array_filter([
  32. 'kp_code' => $this->selectedKpCode,
  33. 'difficulty' => $this->selectedDifficulty,
  34. 'type' => $this->selectedType,
  35. 'search' => $this->search,
  36. ], fn ($value) => filled($value));
  37. $response = $service->listQuestions($this->currentPage, $this->perPage, $filters);
  38. return $response['data'] ?? [];
  39. }
  40. #[Computed(cache: false)]
  41. public function meta(): array
  42. {
  43. $service = app(QuestionServiceApi::class);
  44. $filters = array_filter([
  45. 'kp_code' => $this->selectedKpCode,
  46. 'difficulty' => $this->selectedDifficulty,
  47. 'type' => $this->selectedType,
  48. 'search' => $this->search,
  49. ], fn ($value) => filled($value));
  50. $response = $service->listQuestions($this->currentPage, $this->perPage, $filters);
  51. return $response['meta'] ?? ['page' => 1, 'per_page' => 25, 'total' => 0, 'total_pages' => 0];
  52. }
  53. #[Computed(cache: false)]
  54. public function statistics(): array
  55. {
  56. return app(QuestionServiceApi::class)->getStatistics();
  57. }
  58. #[Computed(cache: false)]
  59. public function knowledgePointOptions(): array
  60. {
  61. return app(QuestionServiceApi::class)->getKnowledgePointOptions();
  62. }
  63. public function deleteQuestion(string $questionCode): void
  64. {
  65. try {
  66. $service = app(\App\Services\QuestionBankService::class);
  67. $result = $service->deleteQuestion($questionCode);
  68. if ($result) {
  69. Notification::make()->title('删除成功')->body("题目 {$questionCode} 已删除")->success()->send();
  70. // 清除所有缓存,确保页面刷新
  71. Cache::flush();
  72. // 重新加载页面数据
  73. $this->dispatch('$refresh');
  74. } else {
  75. Notification::make()->title('删除失败')->body("题目 {$questionCode} 不存在或已被删除")->warning()->send();
  76. }
  77. } catch (\Exception $e) {
  78. Notification::make()->title('删除异常')->body($e->getMessage())->danger()->send();
  79. }
  80. }
  81. public function updatedSearch(): void
  82. {
  83. $this->currentPage = 1;
  84. }
  85. public function updatedSelectedKpCode(): void
  86. {
  87. $this->currentPage = 1;
  88. }
  89. public function updatedSelectedDifficulty(): void
  90. {
  91. $this->currentPage = 1;
  92. }
  93. public function updatedSelectedType(): void
  94. {
  95. $this->currentPage = 1;
  96. }
  97. public function updatedPerPage(): void
  98. {
  99. $this->currentPage = 1;
  100. }
  101. public function viewQuestion(string $questionCode): void
  102. {
  103. $service = app(QuestionBankService::class);
  104. $detail = $service->getQuestion($questionCode);
  105. if (!$detail) {
  106. Notification::make()
  107. ->title('获取题目详情失败')
  108. ->danger()
  109. ->send();
  110. return;
  111. }
  112. // 将技能数组转为逗号字符串便于编辑
  113. if (isset($detail['skills']) && is_array($detail['skills'])) {
  114. $detail['skills_text'] = implode(',', $detail['skills']);
  115. } else {
  116. $detail['skills_text'] = (string)($detail['skills'] ?? '');
  117. }
  118. $this->editing = $detail;
  119. $this->showDetailModal = true;
  120. }
  121. public function saveQuestion(): void
  122. {
  123. if (empty($this->editing['question_code'])) {
  124. Notification::make()->title('缺少题目编号').danger()->send();
  125. return;
  126. }
  127. $payload = [
  128. 'stem' => $this->editing['stem'] ?? '',
  129. 'answer' => $this->editing['answer'] ?? '',
  130. 'solution' => $this->editing['solution'] ?? '',
  131. 'difficulty' => $this->editing['difficulty'] ?? null,
  132. 'tags' => $this->editing['tags'] ?? null,
  133. 'question_type' => $this->editing['question_type'] ?? null,
  134. 'kp_code' => $this->editing['kp_code'] ?? null,
  135. ];
  136. // skills 处理
  137. $skillsText = $this->editing['skills_text'] ?? '';
  138. $payload['skills'] = $skillsText;
  139. $service = app(QuestionBankService::class);
  140. $ok = $service->updateQuestion($this->editing['question_code'], array_filter(
  141. $payload,
  142. fn($v) => $v !== null
  143. ));
  144. if ($ok) {
  145. Notification::make()->title('保存成功')->success()->send();
  146. $this->showDetailModal = false;
  147. $this->dispatch('$refresh');
  148. $this->dispatch('math:render');
  149. } else {
  150. Notification::make()->title('保存失败')->danger()->send();
  151. }
  152. }
  153. public function gotoPage(int $page): void
  154. {
  155. $this->currentPage = $page;
  156. }
  157. public function previousPage(): void
  158. {
  159. if ($this->currentPage > 1) {
  160. $this->currentPage--;
  161. }
  162. }
  163. public function nextPage(): void
  164. {
  165. if ($this->currentPage < ($this->meta['total_pages'] ?? 1)) {
  166. $this->currentPage++;
  167. }
  168. }
  169. public function getPages(): array
  170. {
  171. $totalPages = $this->meta['total_pages'] ?? 1;
  172. $currentPage = $this->currentPage;
  173. $pages = [];
  174. $start = max(1, $currentPage - 2);
  175. $end = min($totalPages, $currentPage + 2);
  176. for ($i = $start; $i <= $end; $i++) {
  177. $pages[] = $i;
  178. }
  179. return $pages;
  180. }
  181. }