QuestionManagement.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 = 11;
  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. '填空题' => 'FILL_IN_THE_BLANK',
  125. '解答题' => 'CALCULATION',
  126. ];
  127. $englishType = $typeMapping[$typeName] ?? 'CALCULATION';
  128. $result[$englishType] = $count;
  129. }
  130. return $result;
  131. } catch (\Exception $e) {
  132. \Log::error('获取题型统计失败', ['error' => $e->getMessage()]);
  133. return [];
  134. }
  135. }
  136. // ✅ 检查待处理的回调任务(简化版)
  137. public function mount(): void
  138. {
  139. // 从 URL 参数初始化筛选条件
  140. $this->selectedKpCode = request()->get('kp_code');
  141. $this->selectedDifficulty = request()->get('difficulty');
  142. $this->selectedType = request()->get('type');
  143. $this->search = request()->get('search');
  144. // 检查是否有从回调生成的通知
  145. $notification = Session::get('notification');
  146. if ($notification) {
  147. $color = $notification['color'] ?? 'info';
  148. // 使用 persistent 确保通知持续显示
  149. Notification::make()
  150. ->title($notification['title'] ?? '通知')
  151. ->body($notification['body'] ?? '')
  152. ->$color()
  153. ->persistent()
  154. ->send();
  155. }
  156. }
  157. /**
  158. * 更新 URL 参数
  159. */
  160. public function updated($field): void
  161. {
  162. // 当筛选条件变化时,更新 URL 参数
  163. if (in_array($field, ['selectedKpCode', 'selectedDifficulty', 'selectedType', 'search'])) {
  164. $params = array_filter([
  165. 'kp_code' => $this->selectedKpCode,
  166. 'difficulty' => $this->selectedDifficulty,
  167. 'type' => $this->selectedType,
  168. 'search' => $this->search,
  169. ], fn($value) => filled($value));
  170. // 重置到第一页
  171. $this->currentPage = 1;
  172. // 更新 URL 参数(不刷新页面)
  173. $this->redirect(route('filament.admin.pages.question-management', $params), navigate: true);
  174. }
  175. }
  176. #[Computed(cache: false)]
  177. public function skillNameMapping(): array
  178. {
  179. $service = app(QuestionServiceApi::class);
  180. return $service->getSkillNameMapping($this->selectedKpCode);
  181. }
  182. public function deleteQuestion(string $questionCode): void
  183. {
  184. try {
  185. $service = app(\App\Services\QuestionBankService::class);
  186. $result = $service->deleteQuestion($questionCode);
  187. if ($result) {
  188. Notification::make()->title('删除成功')->body("题目 {$questionCode} 已删除")->success()->send();
  189. // 清除所有缓存,确保页面刷新
  190. Cache::flush();
  191. // 重新加载页面数据
  192. $this->dispatch('$refresh');
  193. } else {
  194. Notification::make()->title('删除失败')->body("题目 {$questionCode} 不存在或已被删除")->warning()->send();
  195. }
  196. } catch (\Exception $e) {
  197. Notification::make()->title('删除异常')->body($e->getMessage())->danger()->send();
  198. }
  199. }
  200. public function updatedSearch(): void
  201. {
  202. $this->currentPage = 1;
  203. }
  204. public function updatedSelectedKpCode(): void
  205. {
  206. $this->currentPage = 1;
  207. }
  208. public function updatedSelectedDifficulty(): void
  209. {
  210. $this->currentPage = 1;
  211. }
  212. public function updatedSelectedType(): void
  213. {
  214. $this->currentPage = 1;
  215. }
  216. public function updatedPerPage(): void
  217. {
  218. $this->currentPage = 1;
  219. }
  220. public function viewQuestion(string $questionCode): void
  221. {
  222. $service = app(QuestionBankService::class);
  223. $detail = $service->getQuestion($questionCode);
  224. if (!$detail) {
  225. Notification::make()
  226. ->title('获取题目详情失败')
  227. ->danger()
  228. ->send();
  229. return;
  230. }
  231. // 将技能数组转为逗号字符串便于编辑
  232. if (isset($detail['skills']) && is_array($detail['skills'])) {
  233. $detail['skills_text'] = implode(',', $detail['skills']);
  234. } else {
  235. $detail['skills_text'] = (string)($detail['skills'] ?? '');
  236. }
  237. $this->editing = $detail;
  238. $this->showDetailModal = true;
  239. }
  240. public function saveQuestion(): void
  241. {
  242. if (empty($this->editing['question_code'])) {
  243. Notification::make()->title('缺少题目编号').danger()->send();
  244. return;
  245. }
  246. $payload = [
  247. 'stem' => $this->editing['stem'] ?? '',
  248. 'answer' => $this->editing['answer'] ?? '',
  249. 'solution' => $this->editing['solution'] ?? '',
  250. 'difficulty' => $this->editing['difficulty'] ?? null,
  251. 'tags' => $this->editing['tags'] ?? null,
  252. 'question_type' => $this->editing['question_type'] ?? null,
  253. 'kp_code' => $this->editing['kp_code'] ?? null,
  254. ];
  255. // skills 处理
  256. $skillsText = $this->editing['skills_text'] ?? '';
  257. $payload['skills'] = $skillsText;
  258. $service = app(QuestionBankService::class);
  259. $ok = $service->updateQuestion($this->editing['question_code'], array_filter(
  260. $payload,
  261. fn($v) => $v !== null
  262. ));
  263. if ($ok) {
  264. Notification::make()->title('保存成功')->success()->send();
  265. $this->showDetailModal = false;
  266. $this->dispatch('$refresh');
  267. $this->dispatch('math:render');
  268. } else {
  269. Notification::make()->title('保存失败')->danger()->send();
  270. }
  271. }
  272. public function gotoPage(int $page): void
  273. {
  274. $this->currentPage = $page;
  275. }
  276. public function previousPage(): void
  277. {
  278. if ($this->currentPage > 1) {
  279. $this->currentPage--;
  280. }
  281. }
  282. public function nextPage(): void
  283. {
  284. if ($this->currentPage < ($this->meta['total_pages'] ?? 1)) {
  285. $this->currentPage++;
  286. }
  287. }
  288. public function getPages(): array
  289. {
  290. $totalPages = $this->meta['total_pages'] ?? 1;
  291. $currentPage = $this->currentPage;
  292. $pages = [];
  293. $start = max(1, $currentPage - 2);
  294. $end = min($totalPages, $currentPage + 2);
  295. for ($i = $start; $i <= $end; $i++) {
  296. $pages[] = $i;
  297. }
  298. return $pages;
  299. }
  300. }