QuestionManagement.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. namespace App\Filament\Pages;
  3. use App\Services\QuestionServiceApi;
  4. use BackedEnum;
  5. use Filament\Actions;
  6. use Filament\Notifications\Notification;
  7. use Filament\Pages\Page;
  8. use UnitEnum;
  9. use Livewire\Attributes\Computed;
  10. use Livewire\Attributes\On;
  11. class QuestionManagement extends Page
  12. {
  13. protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-rectangle-stack';
  14. protected static string|UnitEnum|null $navigationGroup = '题库系统';
  15. protected static ?string $navigationLabel = '题库管理';
  16. protected static ?int $navigationSort = 2;
  17. protected ?string $heading = '题库管理';
  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. /**
  25. * 计算属性:从 API 获取题目列表
  26. */
  27. #[Computed]
  28. public function questions(): array
  29. {
  30. $service = app(QuestionServiceApi::class);
  31. $filters = array_filter([
  32. 'kp_code' => $this->selectedKpCode,
  33. 'difficulty' => $this->selectedDifficulty,
  34. 'search' => $this->search,
  35. ], fn ($value) => filled($value));
  36. $response = $service->listQuestions($this->currentPage, $this->perPage, $filters);
  37. return $response['data'] ?? [];
  38. }
  39. /**
  40. * 计算属性:分页信息
  41. */
  42. #[Computed]
  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. 'search' => $this->search,
  50. ], fn ($value) => filled($value));
  51. $response = $service->listQuestions($this->currentPage, $this->perPage, $filters);
  52. return $response['meta'] ?? [
  53. 'page' => 1,
  54. 'per_page' => 25,
  55. 'total' => 0,
  56. 'total_pages' => 0,
  57. ];
  58. }
  59. /**
  60. * 计算属性:统计数据
  61. */
  62. #[Computed]
  63. public function statistics(): array
  64. {
  65. $service = app(QuestionServiceApi::class);
  66. return $service->getStatistics();
  67. }
  68. /**
  69. * 计算属性:知识点选项
  70. */
  71. #[Computed]
  72. public function knowledgePointOptions(): array
  73. {
  74. $service = app(QuestionServiceApi::class);
  75. return $service->getKnowledgePointOptions();
  76. }
  77. /**
  78. * 搜索更新处理
  79. */
  80. public function updatedSearch(): void
  81. {
  82. $this->currentPage = 1;
  83. }
  84. /**
  85. * 知识点筛选更新处理
  86. */
  87. public function updatedSelectedKpCode(): void
  88. {
  89. $this->currentPage = 1;
  90. }
  91. /**
  92. * 难度筛选更新处理
  93. */
  94. public function updatedSelectedDifficulty(): void
  95. {
  96. $this->currentPage = 1;
  97. }
  98. /**
  99. * 每页数量更新处理
  100. */
  101. public function updatedPerPage(): void
  102. {
  103. $this->currentPage = 1;
  104. }
  105. /**
  106. * 刷新数据
  107. */
  108. #[On('refresh-data')]
  109. public function refreshData(): void
  110. {
  111. $this->resetCache();
  112. Notification::make()
  113. ->title('数据已刷新')
  114. ->success()
  115. ->send();
  116. }
  117. /**
  118. * 重置缓存
  119. */
  120. private function resetCache(): void
  121. {
  122. // 清除相关缓存
  123. cache()->forget('question-list-' . md5(json_encode([
  124. 'page' => $this->currentPage,
  125. 'per_page' => $this->perPage,
  126. 'filters' => array_filter([
  127. 'kp_code' => $this->selectedKpCode,
  128. 'difficulty' => $this->selectedDifficulty,
  129. 'search' => $this->search,
  130. ]),
  131. ])));
  132. cache()->forget('question-statistics');
  133. }
  134. /**
  135. * AI 生成题目
  136. */
  137. #[On('ai-generate')]
  138. public function aiGenerate(): void
  139. {
  140. // 调用智能题目生成API
  141. try {
  142. $response = Http::timeout(60)->post('http://localhost:5015/generate-intelligent-questions', [
  143. 'knowledge_points' => ['KP1001'],
  144. 'max_questions_per_skill' => 5
  145. ]);
  146. if ($response->successful()) {
  147. Notification::make()
  148. ->title('AI 生成成功')
  149. ->body('因式分解题目生成任务已启动,请稍后查看结果')
  150. ->success()
  151. ->send();
  152. } else {
  153. Notification::make()
  154. ->title('AI 生成失败')
  155. ->body('请检查API服务状态')
  156. ->danger()
  157. ->send();
  158. }
  159. } catch (\Exception $e) {
  160. Notification::make()
  161. ->title('AI 生成异常')
  162. ->body($e->getMessage())
  163. ->danger()
  164. ->send();
  165. }
  166. }
  167. /**
  168. * 智能搜索
  169. */
  170. #[On('smart-search')]
  171. public function smartSearch(): void
  172. {
  173. Notification::make()
  174. ->title('智能搜索功能')
  175. ->body('请使用搜索框输入关键词')
  176. ->info()
  177. ->send();
  178. }
  179. /**
  180. * 跳转到指定页
  181. */
  182. public function gotoPage(int $page): void
  183. {
  184. $this->currentPage = $page;
  185. }
  186. /**
  187. * 上一页
  188. */
  189. public function previousPage(): void
  190. {
  191. if ($this->currentPage > 1) {
  192. $this->currentPage--;
  193. }
  194. }
  195. /**
  196. * 下一页
  197. */
  198. public function nextPage(): void
  199. {
  200. if ($this->currentPage < ($this->meta['total_pages'] ?? 1)) {
  201. $this->currentPage++;
  202. }
  203. }
  204. /**
  205. * 获取页码数组(用于分页器)
  206. */
  207. public function getPages(): array
  208. {
  209. $totalPages = $this->meta['total_pages'] ?? 1;
  210. $currentPage = $this->currentPage;
  211. $pages = [];
  212. $start = max(1, $currentPage - 2);
  213. $end = min($totalPages, $currentPage + 2);
  214. for ($i = $start; $i <= $end; $i++) {
  215. $pages[] = $i;
  216. }
  217. return $pages;
  218. }
  219. /**
  220. * 头部操作按钮
  221. */
  222. protected function getHeaderActions(): array
  223. {
  224. return [
  225. Actions\Action::make('ai_generate')
  226. ->label('AI 生成题目')
  227. ->icon('heroicon-m-sparkles')
  228. ->color('success')
  229. ->action('aiGenerate'),
  230. Actions\Action::make('smart_search')
  231. ->label('智能搜索')
  232. ->icon('heroicon-m-magnifying-glass')
  233. ->color('info')
  234. ->action('smartSearch'),
  235. Actions\Action::make('refresh')
  236. ->label('刷新')
  237. ->icon('heroicon-m-arrow-path')
  238. ->color('warning')
  239. ->action('refreshData'),
  240. ];
  241. }
  242. }