QuestionManagement.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. namespace App\Filament\Pages;
  3. use App\Services\QuestionServiceApi;
  4. use App\Services\KnowledgeGraphService;
  5. use App\Services\QuestionBankService;
  6. use App\Livewire\Traits\WithMathRender;
  7. use BackedEnum;
  8. use Filament\Notifications\Notification;
  9. use Filament\Pages\Page;
  10. use UnitEnum;
  11. use Livewire\Attributes\Computed;
  12. class QuestionManagement extends Page
  13. {
  14. use WithMathRender;
  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 = 2;
  20. protected string $view = 'filament.pages.question-management';
  21. public ?string $search = null;
  22. public ?string $selectedKpCode = null;
  23. public ?string $selectedDifficulty = null;
  24. public int $currentPage = 1;
  25. public int $perPage = 25;
  26. public ?string $generateKpCode = null;
  27. public array $selectedSkills = [];
  28. public int $questionCount = 100;
  29. public ?string $promptTemplate = null;
  30. public bool $showGenerateModal = false;
  31. public bool $showPromptModal = false;
  32. public ?string $currentTaskId = null;
  33. public int $currentTaskProgress = 0;
  34. public ?string $currentTaskMessage = null;
  35. public bool $isGenerating = false;
  36. #[Computed(cache: false)]
  37. public function questions(): array
  38. {
  39. $service = app(QuestionServiceApi::class);
  40. $filters = array_filter([
  41. 'kp_code' => $this->selectedKpCode,
  42. 'difficulty' => $this->selectedDifficulty,
  43. 'search' => $this->search,
  44. ], fn ($value) => filled($value));
  45. $response = $service->listQuestions($this->currentPage, $this->perPage, $filters);
  46. return $response['data'] ?? [];
  47. }
  48. #[Computed(cache: false)]
  49. public function meta(): array
  50. {
  51. $service = app(QuestionServiceApi::class);
  52. $filters = array_filter([
  53. 'kp_code' => $this->selectedKpCode,
  54. 'difficulty' => $this->selectedDifficulty,
  55. 'search' => $this->search,
  56. ], fn ($value) => filled($value));
  57. $response = $service->listQuestions($this->currentPage, $this->perPage, $filters);
  58. return $response['meta'] ?? ['page' => 1, 'per_page' => 25, 'total' => 0, 'total_pages' => 0];
  59. }
  60. #[Computed(cache: false)]
  61. public function statistics(): array
  62. {
  63. return app(QuestionServiceApi::class)->getStatistics();
  64. }
  65. #[Computed(cache: false)]
  66. public function knowledgePointOptions(): array
  67. {
  68. return app(QuestionServiceApi::class)->getKnowledgePointOptions();
  69. }
  70. #[Computed(cache: false)]
  71. public function skillsOptions(): array
  72. {
  73. if (!$this->generateKpCode) {
  74. return [];
  75. }
  76. $service = app(KnowledgeGraphService::class);
  77. return $service->getSkillsByKnowledgePoint($this->generateKpCode);
  78. }
  79. public function openGenerateModal(): void
  80. {
  81. $this->showGenerateModal = true;
  82. }
  83. public function closeGenerateModal(): void
  84. {
  85. $this->showGenerateModal = false;
  86. $this->reset(['generateKpCode', 'selectedSkills', 'questionCount']);
  87. $this->isGenerating = false;
  88. $this->currentTaskId = null;
  89. $this->currentTaskProgress = 0;
  90. $this->currentTaskMessage = null;
  91. }
  92. public function updatedGenerateKpCode(): void
  93. {
  94. // 选择新知识点时重置技能选择
  95. $this->selectedSkills = [];
  96. }
  97. public function toggleAllSkills(): void
  98. {
  99. $skills = $this->skillsOptions;
  100. if (count($this->selectedSkills) === count($skills)) {
  101. $this->selectedSkills = [];
  102. } else {
  103. $this->selectedSkills = array_column($skills, 'code');
  104. }
  105. }
  106. public function executeGenerate(): void
  107. {
  108. // 防止重复提交
  109. if ($this->isGenerating) {
  110. return;
  111. }
  112. // ✅ 立即关闭弹窗,无论验证结果如何
  113. $this->showGenerateModal = false;
  114. // 验证参数
  115. if (!$this->generateKpCode) {
  116. Notification::make()->title('请选择知识点')->danger()->send();
  117. return;
  118. }
  119. if (empty($this->selectedSkills)) {
  120. Notification::make()->title('请选择至少一个技能')->danger()->send();
  121. return;
  122. }
  123. // 设置异步生成状态
  124. $this->isGenerating = true;
  125. $this->currentTaskId = null;
  126. try {
  127. $service = app(QuestionBankService::class);
  128. $callbackUrl = route('api.questions.callback');
  129. \Log::info("[QuestionGen] 开始生成,callback URL: " . $callbackUrl);
  130. $result = $service->generateIntelligentQuestions([
  131. 'kp_code' => $this->generateKpCode,
  132. 'skills' => $this->selectedSkills,
  133. 'count' => $this->questionCount,
  134. 'prompt_template' => $this->promptTemplate ?? null
  135. ], $callbackUrl);
  136. if ($result['success'] ?? false) {
  137. $this->currentTaskId = $result['task_id'] ?? null;
  138. \Log::info("[QuestionGen] 任务已创建: {$this->currentTaskId},启动前端监控");
  139. Notification::make()
  140. ->title('正在生成题目')
  141. ->body("任务 ID: {$this->currentTaskId}\nAI正在后台生成,预计需要30-60秒...")
  142. ->info()
  143. ->persistent()
  144. ->send();
  145. // 开始异步轮询检查任务状态
  146. $this->dispatch('start-async-task-monitoring');
  147. } else {
  148. $this->isGenerating = false;
  149. Notification::make()
  150. ->title('创建任务失败')
  151. ->body($result['message'] ?? '未知错误')
  152. ->danger()
  153. ->send();
  154. }
  155. } catch (\Exception $e) {
  156. $this->isGenerating = false;
  157. \Log::error("[QuestionGen] 生成异常: " . $e->getMessage());
  158. Notification::make()
  159. ->title('生成异常')
  160. ->body($e->getMessage())
  161. ->danger()
  162. ->send();
  163. }
  164. }
  165. // ✅ 已移除轮询机制 - 现在使用 Laravel 广播事件
  166. // 事件监听在前端 JavaScript 中直接处理,无需后端轮询
  167. public function forceCloseStatusBar(string $taskId): void
  168. {
  169. \Log::info("[QuestionGen] 强制关闭状态栏: {$taskId}");
  170. $this->isGenerating = false;
  171. $this->currentTaskId = null;
  172. Notification::make()
  173. ->title('⚠️ 任务超时')
  174. ->body('生成任务可能已完成,请刷新页面查看')
  175. ->warning()
  176. ->persistent()
  177. ->send();
  178. }
  179. public function deleteQuestion(string $questionCode): void
  180. {
  181. try {
  182. $service = app(QuestionBankService::class);
  183. $result = $service->deleteQuestion($questionCode);
  184. if ($result) {
  185. Notification::make()->title('删除成功')->body("题目 {$questionCode} 已删除")->success()->send();
  186. $this->dispatch('refresh-page');
  187. } else {
  188. Notification::make()->title('删除失败')->body("题目 {$questionCode} 不存在或已被删除")->warning()->send();
  189. }
  190. } catch (\Exception $e) {
  191. Notification::make()->title('删除异常')->body($e->getMessage())->danger()->send();
  192. }
  193. }
  194. public function updatedSearch(): void
  195. {
  196. $this->currentPage = 1;
  197. }
  198. public function updatedSelectedKpCode(): void
  199. {
  200. $this->currentPage = 1;
  201. }
  202. public function updatedSelectedDifficulty(): void
  203. {
  204. $this->currentPage = 1;
  205. }
  206. public function updatedPerPage(): void
  207. {
  208. $this->currentPage = 1;
  209. }
  210. public function gotoPage(int $page): void
  211. {
  212. $this->currentPage = $page;
  213. }
  214. public function previousPage(): void
  215. {
  216. if ($this->currentPage > 1) {
  217. $this->currentPage--;
  218. }
  219. }
  220. public function nextPage(): void
  221. {
  222. if ($this->currentPage < ($this->meta['total_pages'] ?? 1)) {
  223. $this->currentPage++;
  224. }
  225. }
  226. public function getPages(): array
  227. {
  228. $totalPages = $this->meta['total_pages'] ?? 1;
  229. $currentPage = $this->currentPage;
  230. $pages = [];
  231. $start = max(1, $currentPage - 2);
  232. $end = min($totalPages, $currentPage + 2);
  233. for ($i = $start; $i <= $end; $i++) {
  234. $pages[] = $i;
  235. }
  236. return $pages;
  237. }
  238. }