QuestionManagement.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. use Illuminate\Support\Facades\Http;
  12. use Illuminate\Support\Facades\Session;
  13. class QuestionManagement extends Page
  14. {
  15. protected static ?string $title = '题库管理';
  16. protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-document-text';
  17. protected static ?string $navigationLabel = '题库管理';
  18. protected static string|UnitEnum|null $navigationGroup = '管理';
  19. protected static ?int $navigationSort = 11;
  20. protected string $view = 'filament.pages.question-management-simple';
  21. public ?string $search = null;
  22. public ?string $selectedKpCode = null;
  23. public ?string $selectedDifficulty = null;
  24. public ?string $selectedType = null;
  25. public int $currentPage = 1;
  26. public int $perPage = 25;
  27. public bool $showDetailModal = false;
  28. public array $editing = [];
  29. #[Computed(cache: false)]
  30. public function questions(): array
  31. {
  32. $service = app(QuestionServiceApi::class);
  33. $filters = array_filter([
  34. 'kp_code' => $this->selectedKpCode,
  35. 'difficulty' => $this->selectedDifficulty,
  36. 'type' => $this->selectedType,
  37. 'search' => $this->search,
  38. ], fn ($value) => filled($value));
  39. $response = $service->listQuestions($this->currentPage, $this->perPage, $filters);
  40. return $response['data'] ?? [];
  41. }
  42. #[Computed(cache: false)]
  43. public function meta(): array
  44. {
  45. $service = app(QuestionServiceApi::class);
  46. $filters = array_filter([
  47. 'kp_code' => $this->selectedKpCode,
  48. 'difficulty' => $this->selectedDifficulty,
  49. 'type' => $this->selectedType,
  50. 'search' => $this->search,
  51. ], fn ($value) => filled($value));
  52. $response = $service->listQuestions($this->currentPage, $this->perPage, $filters);
  53. return $response['meta'] ?? ['page' => 1, 'per_page' => 25, 'total' => 0, 'total_pages' => 0];
  54. }
  55. #[Computed(cache: false)]
  56. public function statistics(): array
  57. {
  58. return app(QuestionServiceApi::class)->getStatistics();
  59. }
  60. #[Computed(cache: false)]
  61. public function knowledgePointOptions(): array
  62. {
  63. return app(QuestionServiceApi::class)->getKnowledgePointOptions();
  64. }
  65. // ✅ 检查待处理的回调任务(简化版)
  66. public function mount(): void
  67. {
  68. // 检查是否有从回调生成的通知
  69. $notification = Session::get('notification');
  70. if ($notification) {
  71. $color = $notification['color'] ?? 'info';
  72. // 使用 persistent 确保通知持续显示
  73. Notification::make()
  74. ->title($notification['title'] ?? '通知')
  75. ->body($notification['body'] ?? '')
  76. ->$color()
  77. ->persistent()
  78. ->send();
  79. }
  80. }
  81. public function deleteQuestion(string $questionCode): void
  82. {
  83. try {
  84. $service = app(\App\Services\QuestionBankService::class);
  85. $result = $service->deleteQuestion($questionCode);
  86. if ($result) {
  87. Notification::make()->title('删除成功')->body("题目 {$questionCode} 已删除")->success()->send();
  88. // 清除所有缓存,确保页面刷新
  89. Cache::flush();
  90. // 重新加载页面数据
  91. $this->dispatch('$refresh');
  92. } else {
  93. Notification::make()->title('删除失败')->body("题目 {$questionCode} 不存在或已被删除")->warning()->send();
  94. }
  95. } catch (\Exception $e) {
  96. Notification::make()->title('删除异常')->body($e->getMessage())->danger()->send();
  97. }
  98. }
  99. public function updatedSearch(): void
  100. {
  101. $this->currentPage = 1;
  102. }
  103. public function updatedSelectedKpCode(): void
  104. {
  105. $this->currentPage = 1;
  106. }
  107. public function updatedSelectedDifficulty(): void
  108. {
  109. $this->currentPage = 1;
  110. }
  111. public function updatedSelectedType(): void
  112. {
  113. $this->currentPage = 1;
  114. }
  115. public function updatedPerPage(): void
  116. {
  117. $this->currentPage = 1;
  118. }
  119. public function viewQuestion(string $questionCode): void
  120. {
  121. $service = app(QuestionBankService::class);
  122. $detail = $service->getQuestion($questionCode);
  123. if (!$detail) {
  124. Notification::make()
  125. ->title('获取题目详情失败')
  126. ->danger()
  127. ->send();
  128. return;
  129. }
  130. // 将技能数组转为逗号字符串便于编辑
  131. if (isset($detail['skills']) && is_array($detail['skills'])) {
  132. $detail['skills_text'] = implode(',', $detail['skills']);
  133. } else {
  134. $detail['skills_text'] = (string)($detail['skills'] ?? '');
  135. }
  136. $this->editing = $detail;
  137. $this->showDetailModal = true;
  138. }
  139. public function saveQuestion(): void
  140. {
  141. if (empty($this->editing['question_code'])) {
  142. Notification::make()->title('缺少题目编号').danger()->send();
  143. return;
  144. }
  145. $payload = [
  146. 'stem' => $this->editing['stem'] ?? '',
  147. 'answer' => $this->editing['answer'] ?? '',
  148. 'solution' => $this->editing['solution'] ?? '',
  149. 'difficulty' => $this->editing['difficulty'] ?? null,
  150. 'tags' => $this->editing['tags'] ?? null,
  151. 'question_type' => $this->editing['question_type'] ?? null,
  152. 'kp_code' => $this->editing['kp_code'] ?? null,
  153. ];
  154. // skills 处理
  155. $skillsText = $this->editing['skills_text'] ?? '';
  156. $payload['skills'] = $skillsText;
  157. $service = app(QuestionBankService::class);
  158. $ok = $service->updateQuestion($this->editing['question_code'], array_filter(
  159. $payload,
  160. fn($v) => $v !== null
  161. ));
  162. if ($ok) {
  163. Notification::make()->title('保存成功')->success()->send();
  164. $this->showDetailModal = false;
  165. $this->dispatch('$refresh');
  166. $this->dispatch('math:render');
  167. } else {
  168. Notification::make()->title('保存失败')->danger()->send();
  169. }
  170. }
  171. public function gotoPage(int $page): void
  172. {
  173. $this->currentPage = $page;
  174. }
  175. public function previousPage(): void
  176. {
  177. if ($this->currentPage > 1) {
  178. $this->currentPage--;
  179. }
  180. }
  181. public function nextPage(): void
  182. {
  183. if ($this->currentPage < ($this->meta['total_pages'] ?? 1)) {
  184. $this->currentPage++;
  185. }
  186. }
  187. public function getPages(): array
  188. {
  189. $totalPages = $this->meta['total_pages'] ?? 1;
  190. $currentPage = $this->currentPage;
  191. $pages = [];
  192. $start = max(1, $currentPage - 2);
  193. $end = min($totalPages, $currentPage + 2);
  194. for ($i = $start; $i <= $end; $i++) {
  195. $pages[] = $i;
  196. }
  197. return $pages;
  198. }
  199. }