QuestionManagement.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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';
  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]
  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]
  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]
  59. public function statistics(): array
  60. {
  61. return app(QuestionServiceApi::class)->getStatistics();
  62. }
  63. #[Computed]
  64. public function knowledgePointOptions(): array
  65. {
  66. return app(QuestionServiceApi::class)->getKnowledgePointOptions();
  67. }
  68. #[Computed]
  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. }
  86. public function updatedGenerateKpCode(): void
  87. {
  88. // 选择新知识点时重置技能选择
  89. $this->selectedSkills = [];
  90. }
  91. public function toggleAllSkills(): void
  92. {
  93. $skills = $this->skillsOptions;
  94. if (count($this->selectedSkills) === count($skills)) {
  95. $this->selectedSkills = [];
  96. } else {
  97. $this->selectedSkills = array_column($skills, 'code');
  98. }
  99. }
  100. public function executeGenerate(): void
  101. {
  102. if (!$this->generateKpCode) {
  103. Notification::make()->title('请选择知识点')->danger()->send();
  104. return;
  105. }
  106. if (empty($this->selectedSkills)) {
  107. Notification::make()->title('请选择至少一个技能')->danger()->send();
  108. return;
  109. }
  110. try {
  111. $service = app(QuestionBankService::class);
  112. $callbackUrl = route('api.questions.callback');
  113. $result = $service->generateIntelligentQuestions([
  114. 'kp_code' => $this->generateKpCode,
  115. 'skills' => $this->selectedSkills,
  116. 'count' => $this->questionCount,
  117. 'prompt_template' => $this->promptTemplate ?? null
  118. ], $callbackUrl);
  119. if ($result['success'] ?? false) {
  120. $this->currentTaskId = $result['task_id'] ?? null;
  121. $this->showGenerateModal = false;
  122. Notification::make()->title('任务已创建')->body("任务 ID: {$this->currentTaskId}")->info()->send();
  123. } else {
  124. Notification::make()->title('创建任务失败')->body($result['message'] ?? '未知错误')->danger()->send();
  125. }
  126. } catch (\Exception $e) {
  127. Notification::make()->title('生成异常')->body($e->getMessage())->danger()->send();
  128. }
  129. }
  130. public function deleteQuestion(string $questionCode): void
  131. {
  132. try {
  133. $service = app(QuestionBankService::class);
  134. $result = $service->deleteQuestion($questionCode);
  135. if ($result) {
  136. Notification::make()->title('删除成功')->body("题目 {$questionCode} 已删除")->success()->send();
  137. $this->dispatch('refresh-page');
  138. } else {
  139. Notification::make()->title('删除失败')->body("题目 {$questionCode} 不存在或已被删除")->warning()->send();
  140. }
  141. } catch (\Exception $e) {
  142. Notification::make()->title('删除异常')->body($e->getMessage())->danger()->send();
  143. }
  144. }
  145. public function updatedSearch(): void
  146. {
  147. $this->currentPage = 1;
  148. }
  149. public function updatedSelectedKpCode(): void
  150. {
  151. $this->currentPage = 1;
  152. }
  153. public function updatedSelectedDifficulty(): void
  154. {
  155. $this->currentPage = 1;
  156. }
  157. public function updatedPerPage(): void
  158. {
  159. $this->currentPage = 1;
  160. }
  161. public function gotoPage(int $page): void
  162. {
  163. $this->currentPage = $page;
  164. }
  165. public function previousPage(): void
  166. {
  167. if ($this->currentPage > 1) {
  168. $this->currentPage--;
  169. }
  170. }
  171. public function nextPage(): void
  172. {
  173. if ($this->currentPage < ($this->meta['total_pages'] ?? 1)) {
  174. $this->currentPage++;
  175. }
  176. }
  177. public function getPages(): array
  178. {
  179. $totalPages = $this->meta['total_pages'] ?? 1;
  180. $currentPage = $this->currentPage;
  181. $pages = [];
  182. $start = max(1, $currentPage - 2);
  183. $end = min($totalPages, $currentPage + 2);
  184. for ($i = $start; $i <= $end; $i++) {
  185. $pages[] = $i;
  186. }
  187. return $pages;
  188. }
  189. }