QuestionManagement.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. // ✅ 用于存储从URL参数获取的任务ID
  30. public ?string $pendingTaskId = null;
  31. #[Computed(cache: false)]
  32. public function questions(): array
  33. {
  34. $service = app(QuestionServiceApi::class);
  35. $filters = array_filter([
  36. 'kp_code' => $this->selectedKpCode,
  37. 'difficulty' => $this->selectedDifficulty,
  38. 'type' => $this->selectedType,
  39. 'search' => $this->search,
  40. ], fn ($value) => filled($value));
  41. $response = $service->listQuestions($this->currentPage, $this->perPage, $filters);
  42. return $response['data'] ?? [];
  43. }
  44. #[Computed(cache: false)]
  45. public function meta(): array
  46. {
  47. $service = app(QuestionServiceApi::class);
  48. $filters = array_filter([
  49. 'kp_code' => $this->selectedKpCode,
  50. 'difficulty' => $this->selectedDifficulty,
  51. 'type' => $this->selectedType,
  52. 'search' => $this->search,
  53. ], fn ($value) => filled($value));
  54. $response = $service->listQuestions($this->currentPage, $this->perPage, $filters);
  55. return $response['meta'] ?? ['page' => 1, 'per_page' => 25, 'total' => 0, 'total_pages' => 0];
  56. }
  57. #[Computed(cache: false)]
  58. public function statistics(): array
  59. {
  60. return app(QuestionServiceApi::class)->getStatistics();
  61. }
  62. #[Computed(cache: false)]
  63. public function knowledgePointOptions(): array
  64. {
  65. return app(QuestionServiceApi::class)->getKnowledgePointOptions();
  66. }
  67. // ✅ 检查待处理的回调任务(简化版)
  68. public function mount(): void
  69. {
  70. // 检查是否有从其他页面跳转带来的通知
  71. $notification = Session::get('notification');
  72. if ($notification) {
  73. $color = $notification['color'] ?? 'info';
  74. Notification::make()
  75. ->title($notification['title'] ?? '通知')
  76. ->body($notification['body'] ?? '')
  77. ->$color()
  78. ->persistent()
  79. ->send();
  80. }
  81. // 从 request 中获取 task_id 参数(来自 URL 或缓存)
  82. $taskId = request()->get('task_id');
  83. if ($taskId) {
  84. $this->pendingTaskId = $taskId;
  85. // 简化:只记录task_id,前端通过JS定期检查回调状态
  86. Notification::make()
  87. ->title('📋 任务已创建')
  88. ->body("任务 ID: {$taskId}\n等待后台生成完成,请稍候...")
  89. ->info()
  90. ->persistent()
  91. ->send();
  92. }
  93. }
  94. public function deleteQuestion(string $questionCode): void
  95. {
  96. try {
  97. $service = app(\App\Services\QuestionBankService::class);
  98. $result = $service->deleteQuestion($questionCode);
  99. if ($result) {
  100. Notification::make()->title('删除成功')->body("题目 {$questionCode} 已删除")->success()->send();
  101. // 清除所有缓存,确保页面刷新
  102. Cache::flush();
  103. // 重新加载页面数据
  104. $this->dispatch('$refresh');
  105. } else {
  106. Notification::make()->title('删除失败')->body("题目 {$questionCode} 不存在或已被删除")->warning()->send();
  107. }
  108. } catch (\Exception $e) {
  109. Notification::make()->title('删除异常')->body($e->getMessage())->danger()->send();
  110. }
  111. }
  112. public function updatedSearch(): void
  113. {
  114. $this->currentPage = 1;
  115. }
  116. public function updatedSelectedKpCode(): void
  117. {
  118. $this->currentPage = 1;
  119. }
  120. public function updatedSelectedDifficulty(): void
  121. {
  122. $this->currentPage = 1;
  123. }
  124. public function updatedSelectedType(): void
  125. {
  126. $this->currentPage = 1;
  127. }
  128. public function updatedPerPage(): void
  129. {
  130. $this->currentPage = 1;
  131. }
  132. public function viewQuestion(string $questionCode): void
  133. {
  134. $service = app(QuestionBankService::class);
  135. $detail = $service->getQuestion($questionCode);
  136. if (!$detail) {
  137. Notification::make()
  138. ->title('获取题目详情失败')
  139. ->danger()
  140. ->send();
  141. return;
  142. }
  143. // 将技能数组转为逗号字符串便于编辑
  144. if (isset($detail['skills']) && is_array($detail['skills'])) {
  145. $detail['skills_text'] = implode(',', $detail['skills']);
  146. } else {
  147. $detail['skills_text'] = (string)($detail['skills'] ?? '');
  148. }
  149. $this->editing = $detail;
  150. $this->showDetailModal = true;
  151. }
  152. public function saveQuestion(): void
  153. {
  154. if (empty($this->editing['question_code'])) {
  155. Notification::make()->title('缺少题目编号').danger()->send();
  156. return;
  157. }
  158. $payload = [
  159. 'stem' => $this->editing['stem'] ?? '',
  160. 'answer' => $this->editing['answer'] ?? '',
  161. 'solution' => $this->editing['solution'] ?? '',
  162. 'difficulty' => $this->editing['difficulty'] ?? null,
  163. 'tags' => $this->editing['tags'] ?? null,
  164. 'question_type' => $this->editing['question_type'] ?? null,
  165. 'kp_code' => $this->editing['kp_code'] ?? null,
  166. ];
  167. // skills 处理
  168. $skillsText = $this->editing['skills_text'] ?? '';
  169. $payload['skills'] = $skillsText;
  170. $service = app(QuestionBankService::class);
  171. $ok = $service->updateQuestion($this->editing['question_code'], array_filter(
  172. $payload,
  173. fn($v) => $v !== null
  174. ));
  175. if ($ok) {
  176. Notification::make()->title('保存成功')->success()->send();
  177. $this->showDetailModal = false;
  178. $this->dispatch('$refresh');
  179. $this->dispatch('math:render');
  180. } else {
  181. Notification::make()->title('保存失败')->danger()->send();
  182. }
  183. }
  184. public function gotoPage(int $page): void
  185. {
  186. $this->currentPage = $page;
  187. }
  188. public function previousPage(): void
  189. {
  190. if ($this->currentPage > 1) {
  191. $this->currentPage--;
  192. }
  193. }
  194. public function nextPage(): void
  195. {
  196. if ($this->currentPage < ($this->meta['total_pages'] ?? 1)) {
  197. $this->currentPage++;
  198. }
  199. }
  200. public function getPages(): array
  201. {
  202. $totalPages = $this->meta['total_pages'] ?? 1;
  203. $currentPage = $this->currentPage;
  204. $pages = [];
  205. $start = max(1, $currentPage - 2);
  206. $end = min($totalPages, $currentPage + 2);
  207. for ($i = $start; $i <= $end; $i++) {
  208. $pages[] = $i;
  209. }
  210. return $pages;
  211. }
  212. }