QuestionManagement.php 8.6 KB

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