QuestionManagement.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 = 3;
  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. #[Computed(cache: false)]
  30. public function questions(): array
  31. {
  32. $service = app(QuestionServiceApi::class);
  33. $filters = array_filter([
  34. 'kp_code' => $this->selectedKpCode,
  35. 'difficulty' => $this->selectedDifficulty,
  36. 'type' => $this->selectedType,
  37. 'search' => $this->search,
  38. ], fn ($value) => filled($value));
  39. $response = $service->listQuestions($this->currentPage, $this->perPage, $filters);
  40. return $response['data'] ?? [];
  41. }
  42. #[Computed(cache: false)]
  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. 'type' => $this->selectedType,
  50. 'search' => $this->search,
  51. ], fn ($value) => filled($value));
  52. $response = $service->listQuestions($this->currentPage, $this->perPage, $filters);
  53. return $response['meta'] ?? ['page' => 1, 'per_page' => 25, 'total' => 0, 'total_pages' => 0];
  54. }
  55. #[Computed(cache: false)]
  56. public function statistics(): array
  57. {
  58. $service = app(QuestionServiceApi::class);
  59. $filters = array_filter([
  60. 'kp_code' => $this->selectedKpCode,
  61. 'difficulty' => $this->selectedDifficulty,
  62. 'type' => $this->selectedType,
  63. 'search' => $this->search,
  64. ], fn ($value) => filled($value));
  65. return $service->getStatistics($filters);
  66. }
  67. #[Computed(cache: false)]
  68. public function knowledgePointOptions(): array
  69. {
  70. return app(QuestionServiceApi::class)->getKnowledgePointOptions();
  71. }
  72. #[Computed(cache: false)]
  73. public function questionTypeOptions(): array
  74. {
  75. return [
  76. 'CHOICE' => '单选题',
  77. 'MULTIPLE_CHOICE' => '多选题',
  78. 'FILL_IN_THE_BLANK' => '填空题',
  79. 'CALCULATION' => '解答题',
  80. 'WORD_PROBLEM' => '应用题',
  81. 'PROOF' => '证明题',
  82. ];
  83. }
  84. #[Computed(cache: false)]
  85. public function questionTypeStatistics(): array
  86. {
  87. // 如果选择了特定知识点,获取该知识点的题型统计
  88. if ($this->selectedKpCode) {
  89. try {
  90. $service = app(QuestionServiceApi::class);
  91. $filters = ['kp_code' => $this->selectedKpCode];
  92. $response = $service->listQuestions(1, 1000, $filters);
  93. $questions = $response['data'] ?? [];
  94. $typeStats = [];
  95. // 直接使用 question_type 字段统计,而不是猜测
  96. foreach ($questions as $question) {
  97. $type = $question['type'] ?? 'CALCULATION';
  98. if (!isset($typeStats[$type])) {
  99. $typeStats[$type] = 0;
  100. }
  101. $typeStats[$type]++;
  102. }
  103. // 按数量排序
  104. arsort($typeStats);
  105. return $typeStats;
  106. } catch (\Exception $e) {
  107. \Log::error('获取知识点题型统计失败', [
  108. 'kp_code' => $this->selectedKpCode,
  109. 'error' => $e->getMessage()
  110. ]);
  111. return [];
  112. }
  113. }
  114. // 否则返回所有题目的题型统计(从统计数据中获取)
  115. try {
  116. $statistics = $this->statistics();
  117. $typeStats = $statistics['by_type'] ?? [];
  118. // 转换为键值对格式
  119. $result = [];
  120. foreach ($typeStats as $typeName => $count) {
  121. // 将中文类型名映射为英文类型名
  122. $typeMapping = [
  123. '选择题' => 'CHOICE',
  124. '多选题' => 'MULTIPLE_CHOICE',
  125. '填空题' => 'FILL_IN_THE_BLANK',
  126. '解答题' => 'CALCULATION',
  127. '证明题' => 'PROOF',
  128. '其他' => 'OTHER',
  129. ];
  130. $englishType = $typeMapping[$typeName] ?? 'CALCULATION';
  131. $result[$englishType] = $count;
  132. }
  133. return $result;
  134. } catch (\Exception $e) {
  135. \Log::error('获取题型统计失败', ['error' => $e->getMessage()]);
  136. return [];
  137. }
  138. }
  139. // ✅ 检查待处理的回调任务(简化版)
  140. public function mount(): void
  141. {
  142. // 从 URL 参数初始化筛选条件
  143. $this->selectedKpCode = request()->get('kp_code');
  144. $this->selectedDifficulty = request()->get('difficulty');
  145. $this->selectedType = request()->get('type');
  146. $this->search = request()->get('search');
  147. // 检查是否有从回调生成的通知
  148. $notification = Session::get('notification');
  149. if ($notification) {
  150. $color = $notification['color'] ?? 'info';
  151. // 使用 persistent 确保通知持续显示
  152. Notification::make()
  153. ->title($notification['title'] ?? '通知')
  154. ->body($notification['body'] ?? '')
  155. ->$color()
  156. ->persistent()
  157. ->send();
  158. }
  159. }
  160. /**
  161. * 更新 URL 参数
  162. */
  163. public function updated($field): void
  164. {
  165. // 当筛选条件变化时,更新 URL 参数
  166. if (in_array($field, ['selectedKpCode', 'selectedDifficulty', 'selectedType', 'search'])) {
  167. $params = array_filter([
  168. 'kp_code' => $this->selectedKpCode,
  169. 'difficulty' => $this->selectedDifficulty,
  170. 'type' => $this->selectedType,
  171. 'search' => $this->search,
  172. ], fn($value) => filled($value));
  173. // 重置到第一页
  174. $this->currentPage = 1;
  175. // 更新 URL 参数(不刷新页面)
  176. $this->redirect(route('filament.admin.pages.question-management', $params), navigate: true);
  177. }
  178. }
  179. #[Computed(cache: false)]
  180. public function skillNameMapping(): array
  181. {
  182. $service = app(QuestionServiceApi::class);
  183. return $service->getSkillNameMapping($this->selectedKpCode);
  184. }
  185. public function deleteQuestion(string $questionCode): void
  186. {
  187. try {
  188. $service = app(\App\Services\QuestionBankService::class);
  189. $result = $service->deleteQuestion($questionCode);
  190. if ($result) {
  191. Notification::make()->title('删除成功')->body("题目 {$questionCode} 已删除")->success()->send();
  192. // 清除所有缓存,确保页面刷新
  193. Cache::flush();
  194. // 重新加载页面数据
  195. $this->dispatch('$refresh');
  196. } else {
  197. Notification::make()->title('删除失败')->body("题目 {$questionCode} 不存在或已被删除")->warning()->send();
  198. }
  199. } catch (\Exception $e) {
  200. Notification::make()->title('删除异常')->body($e->getMessage())->danger()->send();
  201. }
  202. }
  203. public function updatedSearch(): void
  204. {
  205. $this->currentPage = 1;
  206. }
  207. public function updatedSelectedKpCode(): void
  208. {
  209. $this->currentPage = 1;
  210. }
  211. public function updatedSelectedDifficulty(): void
  212. {
  213. $this->currentPage = 1;
  214. }
  215. public function updatedSelectedType(): void
  216. {
  217. $this->currentPage = 1;
  218. }
  219. public function updatedPerPage(): void
  220. {
  221. $this->currentPage = 1;
  222. }
  223. public function viewQuestion(string $questionCode): void
  224. {
  225. $service = app(QuestionBankService::class);
  226. $detail = $service->getQuestion($questionCode);
  227. if (!$detail) {
  228. Notification::make()
  229. ->title('获取题目详情失败')
  230. ->danger()
  231. ->send();
  232. return;
  233. }
  234. // 将技能数组转为逗号字符串便于编辑
  235. if (isset($detail['skills']) && is_array($detail['skills'])) {
  236. $detail['skills_text'] = implode(',', $detail['skills']);
  237. } else {
  238. $detail['skills_text'] = (string)($detail['skills'] ?? '');
  239. }
  240. $this->editing = $detail;
  241. $this->showDetailModal = true;
  242. }
  243. public function saveQuestion(): void
  244. {
  245. if (empty($this->editing['question_code'])) {
  246. Notification::make()->title('缺少题目编号').danger()->send();
  247. return;
  248. }
  249. $payload = [
  250. 'stem' => $this->editing['stem'] ?? '',
  251. 'answer' => $this->editing['answer'] ?? '',
  252. 'solution' => $this->editing['solution'] ?? '',
  253. 'difficulty' => $this->editing['difficulty'] ?? null,
  254. 'tags' => $this->editing['tags'] ?? null,
  255. 'question_type' => $this->editing['question_type'] ?? null,
  256. 'kp_code' => $this->editing['kp_code'] ?? null,
  257. ];
  258. // skills 处理
  259. $skillsText = $this->editing['skills_text'] ?? '';
  260. $payload['skills'] = $skillsText;
  261. $service = app(QuestionBankService::class);
  262. $ok = $service->updateQuestion($this->editing['question_code'], array_filter(
  263. $payload,
  264. fn($v) => $v !== null
  265. ));
  266. if ($ok) {
  267. Notification::make()->title('保存成功')->success()->send();
  268. $this->showDetailModal = false;
  269. $this->dispatch('$refresh');
  270. $this->dispatch('math:render');
  271. } else {
  272. Notification::make()->title('保存失败')->danger()->send();
  273. }
  274. }
  275. public function gotoPage(int $page): void
  276. {
  277. $this->currentPage = $page;
  278. }
  279. public function previousPage(): void
  280. {
  281. if ($this->currentPage > 1) {
  282. $this->currentPage--;
  283. }
  284. }
  285. public function nextPage(): void
  286. {
  287. if ($this->currentPage < ($this->meta['total_pages'] ?? 1)) {
  288. $this->currentPage++;
  289. }
  290. }
  291. public function getPages(): array
  292. {
  293. $totalPages = $this->meta['total_pages'] ?? 1;
  294. $currentPage = $this->currentPage;
  295. $pages = [];
  296. $start = max(1, $currentPage - 2);
  297. $end = min($totalPages, $currentPage + 2);
  298. for ($i = $start; $i <= $end; $i++) {
  299. $pages[] = $i;
  300. }
  301. return $pages;
  302. }
  303. }