MistakeBook.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. <?php
  2. namespace App\Filament\Pages;
  3. use App\Filament\Traits\HasUserRole;
  4. use App\Models\Student;
  5. use App\Services\KnowledgeGraphService;
  6. use App\Services\KnowledgeServiceApi;
  7. use App\Services\MistakeBookService;
  8. use App\Services\QuestionBankService;
  9. use BackedEnum;
  10. use Filament\Pages\Page;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Support\Arr;
  13. use Illuminate\Support\Facades\Log;
  14. use UnitEnum;
  15. use Livewire\Attributes\On;
  16. use Livewire\Attributes\Computed;
  17. use App\Models\Teacher;
  18. class MistakeBook extends Page
  19. {
  20. use HasUserRole;
  21. protected static ?string $title = '错题本';
  22. protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-bookmark';
  23. protected static ?string $navigationLabel = '错题本';
  24. protected static string|UnitEnum|null $navigationGroup = '学生管理';
  25. protected static ?int $navigationSort = 3;
  26. protected static ?string $slug = 'mistake-book';
  27. protected string $view = 'filament.pages.mistake-book';
  28. public string $teacherId = '';
  29. public string $studentId = '';
  30. public array $filters = [
  31. 'kp_ids' => [],
  32. 'skill_ids' => [],
  33. 'error_types' => [],
  34. 'time_range' => 'last_30',
  35. 'start_date' => null,
  36. 'end_date' => null,
  37. 'sort_by' => 'created_at_desc',
  38. 'correct_filter' => 'incorrect', // 默认只显示错误题目
  39. 'filter' => [],
  40. ];
  41. public array $filterOptions = [
  42. 'knowledge_points' => [],
  43. 'skills' => [],
  44. ];
  45. public array $mistakes = [];
  46. public array $patterns = [];
  47. public array $summary = [];
  48. public array $recommendations = [];
  49. public array $relatedQuestions = [];
  50. public array $selectedMistakeIds = [];
  51. public bool $isLoading = false;
  52. public string $errorMessage = '';
  53. public string $actionMessage = '';
  54. public string $actionMessageType = 'success';
  55. // 分页
  56. public int $page = 1;
  57. public int $perPage = 10;
  58. public int $total = 0;
  59. public function mount(Request $request): void
  60. {
  61. // 初始化用户角色检查
  62. $this->initializeUserRole();
  63. // 如果是老师,自动选择当前老师
  64. if ($this->isTeacher) {
  65. $teacherId = $this->getCurrentTeacherId();
  66. if ($teacherId) {
  67. $this->teacherId = $teacherId;
  68. }
  69. } else {
  70. $this->teacherId = (string) ($request->input('teacher_id') ?? '');
  71. }
  72. $this->studentId = (string) ($request->input('student_id') ?? '');
  73. $this->filters['time_range'] = (string) ($request->input('range') ?? 'last_30');
  74. if ($this->studentId && empty($this->teacherId)) {
  75. $student = Student::find($this->studentId);
  76. if ($student && $student->teacher_id) {
  77. $this->teacherId = (string) $student->teacher_id;
  78. }
  79. }
  80. $this->loadFilterOptions();
  81. if ($this->studentId) {
  82. $this->loadMistakeData();
  83. }
  84. }
  85. public function updatedStudentId(): void
  86. {
  87. if ($this->studentId) {
  88. $this->loadMistakeData();
  89. } else {
  90. $this->resetPageState();
  91. }
  92. }
  93. public function loadMistakeData(): void
  94. {
  95. if (empty($this->studentId)) {
  96. $this->errorMessage = '请先选择学生';
  97. return;
  98. }
  99. $this->isLoading = true;
  100. $this->errorMessage = '';
  101. $this->actionMessage = '';
  102. try {
  103. $service = app(MistakeBookService::class);
  104. // 处理筛选参数
  105. $params = [
  106. 'student_id' => $this->studentId,
  107. 'page' => $this->page,
  108. 'per_page' => $this->perPage,
  109. ];
  110. // 基础筛选
  111. if (!empty($this->filters['kp_ids'])) {
  112. $params['kp_ids'] = $this->filters['kp_ids'];
  113. }
  114. if (!empty($this->filters['skill_ids'])) {
  115. $params['skill_ids'] = $this->filters['skill_ids'];
  116. }
  117. if (!empty($this->filters['error_types'])) {
  118. $params['error_types'] = $this->filters['error_types'];
  119. }
  120. if (isset($this->filters['time_range']) && $this->filters['time_range'] !== 'all') {
  121. $params['time_range'] = $this->filters['time_range'];
  122. }
  123. if (!empty($this->filters['start_date'])) {
  124. $params['start_date'] = $this->filters['start_date'];
  125. }
  126. if (!empty($this->filters['end_date'])) {
  127. $params['end_date'] = $this->filters['end_date'];
  128. }
  129. if (!empty($this->filters['sort_by'])) {
  130. $params['sort_by'] = $this->filters['sort_by'];
  131. }
  132. // 正确与否筛选
  133. if (isset($this->filters['correct_filter']) && $this->filters['correct_filter'] !== 'all') {
  134. if ($this->filters['correct_filter'] === 'correct') {
  135. $params['correct_only'] = true;
  136. } elseif ($this->filters['correct_filter'] === 'incorrect') {
  137. $params['incorrect_only'] = true;
  138. }
  139. }
  140. // 状态筛选
  141. if (!empty($this->filters['filter'])) {
  142. if (in_array('unreviewed', $this->filters['filter'])) {
  143. $params['unreviewed_only'] = true;
  144. }
  145. if (in_array('favorite', $this->filters['filter'])) {
  146. $params['favorite_only'] = true;
  147. }
  148. }
  149. $list = $service->listMistakes($params);
  150. $this->mistakes = $list['data'] ?? [];
  151. $this->total = $list['meta']['total'] ?? 0;
  152. $this->summary = $service->summarize($this->studentId);
  153. $this->patterns = $service->getMistakePatterns($this->studentId);
  154. // 清理无效的选中项
  155. $validIds = collect($this->mistakes)->pluck('id')->filter()->all();
  156. $this->selectedMistakeIds = array_values(
  157. array_intersect($this->selectedMistakeIds, $validIds)
  158. );
  159. } catch (\Throwable $e) {
  160. $this->errorMessage = '加载错题本数据失败:' . $e->getMessage();
  161. Log::error('Load mistake book failed', [
  162. 'student_id' => $this->studentId,
  163. 'error' => $e->getMessage(),
  164. ]);
  165. } finally {
  166. $this->isLoading = false;
  167. }
  168. }
  169. public function gotoPage(int $page): void
  170. {
  171. $this->page = max(1, $page);
  172. $this->loadMistakeData();
  173. }
  174. public function nextPage(): void
  175. {
  176. $maxPage = (int) ceil($this->total / $this->perPage);
  177. if ($this->page < $maxPage) {
  178. $this->page++;
  179. $this->loadMistakeData();
  180. }
  181. }
  182. public function prevPage(): void
  183. {
  184. if ($this->page > 1) {
  185. $this->page--;
  186. $this->loadMistakeData();
  187. }
  188. }
  189. public function refreshPatterns(): void
  190. {
  191. if (!$this->studentId) {
  192. return;
  193. }
  194. try {
  195. $service = app(MistakeBookService::class);
  196. $this->patterns = $service->getMistakePatterns($this->studentId);
  197. } catch (\Throwable $e) {
  198. Log::error('Refresh mistake patterns failed', [
  199. 'student_id' => $this->studentId,
  200. 'error' => $e->getMessage(),
  201. ]);
  202. }
  203. }
  204. public function toggleFavorite(string $mistakeId): void
  205. {
  206. $service = app(MistakeBookService::class);
  207. $current = $this->findMistakeById($mistakeId);
  208. $willFavorite = !($current['favorite'] ?? false);
  209. if ($service->toggleFavorite($mistakeId, $willFavorite)) {
  210. $this->updateMistakeField($mistakeId, 'favorite', $willFavorite);
  211. $this->notify('已更新收藏状态');
  212. } else {
  213. $this->notify('收藏操作失败,请稍后再试', 'danger');
  214. }
  215. }
  216. public function markReviewed(string $mistakeId): void
  217. {
  218. $service = app(MistakeBookService::class);
  219. if ($service->markReviewed($mistakeId)) {
  220. $this->updateMistakeField($mistakeId, 'reviewed', true);
  221. $this->notify('已标记为已复习');
  222. } else {
  223. $this->notify('标记失败,请稍后再试', 'danger');
  224. }
  225. }
  226. public function addToRetryList(string $mistakeId): void
  227. {
  228. $service = app(MistakeBookService::class);
  229. if ($service->addToRetryList($mistakeId)) {
  230. $this->notify('已加入重练清单');
  231. } else {
  232. $this->notify('加入清单失败,请稍后再试', 'danger');
  233. }
  234. }
  235. public function loadRelatedQuestions(string $mistakeId): void
  236. {
  237. $mistake = $this->findMistakeById($mistakeId);
  238. if (empty($mistake)) {
  239. return;
  240. }
  241. $questionBank = app(QuestionBankService::class);
  242. $kpIds = Arr::wrap($mistake['kp_ids'] ?? []);
  243. $skills = Arr::wrap($mistake['skill_ids'] ?? $mistake['skills'] ?? []);
  244. $response = $questionBank->filterQuestions(array_filter([
  245. 'kp_codes' => !empty($kpIds) ? implode(',', $kpIds) : null,
  246. 'skills' => !empty($skills) ? implode(',', $skills) : null,
  247. 'limit' => 5,
  248. ]));
  249. $this->relatedQuestions[$mistakeId] = $response['data'] ?? [];
  250. }
  251. public function toggleSelection(string $mistakeId): void
  252. {
  253. if (in_array($mistakeId, $this->selectedMistakeIds, true)) {
  254. $this->selectedMistakeIds = array_values(array_diff($this->selectedMistakeIds, [$mistakeId]));
  255. } else {
  256. $this->selectedMistakeIds[] = $mistakeId;
  257. }
  258. }
  259. public function generatePracticeFromSelection(): void
  260. {
  261. if (empty($this->selectedMistakeIds)) {
  262. $this->notify('请先选择至少一道错题', 'warning');
  263. return;
  264. }
  265. $selected = array_filter($this->mistakes, fn ($item) => in_array($item['id'] ?? '', $this->selectedMistakeIds, true));
  266. $kpIds = collect($selected)
  267. ->pluck('kp_ids')
  268. ->flatten()
  269. ->filter()
  270. ->unique()
  271. ->values()
  272. ->all();
  273. $skillIds = collect($selected)
  274. ->pluck('skill_ids')
  275. ->flatten()
  276. ->filter()
  277. ->unique()
  278. ->values()
  279. ->all();
  280. $service = app(MistakeBookService::class);
  281. $result = $service->recommendPractice($this->studentId, $kpIds, $skillIds);
  282. $this->recommendations = $result['data'] ?? ($result['questions'] ?? []);
  283. if (!empty($this->recommendations)) {
  284. $this->notify('已生成重练题单');
  285. } else {
  286. $this->notify('未能生成题单,请稍后再试', 'warning');
  287. }
  288. }
  289. public function batchMarkReviewed(): void
  290. {
  291. if (empty($this->selectedMistakeIds)) {
  292. $this->notify('请先选择至少一道错题', 'warning');
  293. return;
  294. }
  295. $service = app(MistakeBookService::class);
  296. $result = $service->batchOperation($this->selectedMistakeIds, 'reviewed');
  297. if ($result['success'] ?? false) {
  298. $this->selectedMistakeIds = [];
  299. $this->notify("已标记 {$result['success_count']} 道题为已复习");
  300. $this->loadMistakeData(); // 重新加载数据
  301. } else {
  302. $this->notify($result['error'] ?? '操作失败', 'danger');
  303. }
  304. }
  305. /**
  306. * 批量标记为已掌握
  307. */
  308. public function batchMarkMastered(): void
  309. {
  310. if (empty($this->selectedMistakeIds)) {
  311. $this->notify('请先选择至少一道错题', 'warning');
  312. return;
  313. }
  314. $service = app(MistakeBookService::class);
  315. $result = $service->batchOperation($this->selectedMistakeIds, 'mastered');
  316. if ($result['success'] ?? false) {
  317. $this->selectedMistakeIds = [];
  318. $this->notify("已标记 {$result['success_count']} 道题为重点掌握");
  319. $this->loadMistakeData();
  320. } else {
  321. $this->notify($result['error'] ?? '操作失败', 'danger');
  322. }
  323. }
  324. /**
  325. * 批量加入重练清单
  326. */
  327. public function batchAddToRetryList(): void
  328. {
  329. if (empty($this->selectedMistakeIds)) {
  330. $this->notify('请先选择至少一道错题', 'warning');
  331. return;
  332. }
  333. $service = app(MistakeBookService::class);
  334. $result = $service->batchOperation($this->selectedMistakeIds, 'retry_list');
  335. if ($result['success'] ?? false) {
  336. $this->notify("已加入 {$result['success_count']} 道题到重练清单");
  337. $this->loadMistakeData();
  338. } else {
  339. $this->notify($result['error'] ?? '操作失败', 'danger');
  340. }
  341. }
  342. /**
  343. * 批量从重练清单移除
  344. */
  345. public function batchRemoveFromRetryList(): void
  346. {
  347. if (empty($this->selectedMistakeIds)) {
  348. $this->notify('请先选择至少一道错题', 'warning');
  349. return;
  350. }
  351. $service = app(MistakeBookService::class);
  352. $result = $service->batchOperation($this->selectedMistakeIds, 'remove_retry_list');
  353. if ($result['success'] ?? false) {
  354. $this->notify("已从重练清单移除 {$result['success_count']} 道题");
  355. $this->loadMistakeData();
  356. } else {
  357. $this->notify($result['error'] ?? '操作失败', 'danger');
  358. }
  359. }
  360. /**
  361. * 批量设置错误类型
  362. */
  363. public function batchSetErrorType(string $errorType): void
  364. {
  365. if (empty($this->selectedMistakeIds)) {
  366. $this->notify('请先选择至少一道错题', 'warning');
  367. return;
  368. }
  369. $service = app(MistakeBookService::class);
  370. $result = $service->batchOperation($this->selectedMistakeIds, 'set_error_type', [
  371. 'error_type' => $errorType,
  372. ]);
  373. if ($result['success'] ?? false) {
  374. $this->selectedMistakeIds = [];
  375. $this->notify("已为 {$result['success_count']} 道题设置错误类型");
  376. $this->loadMistakeData();
  377. } else {
  378. $this->notify($result['error'] ?? '操作失败', 'danger');
  379. }
  380. }
  381. /**
  382. * 批量设置重要程度
  383. */
  384. public function batchSetImportance(int $importance): void
  385. {
  386. if (empty($this->selectedMistakeIds)) {
  387. $this->notify('请先选择至少一道错题', 'warning');
  388. return;
  389. }
  390. $service = app(MistakeBookService::class);
  391. $result = $service->batchOperation($this->selectedMistakeIds, 'set_importance', [
  392. 'importance' => $importance,
  393. ]);
  394. if ($result['success'] ?? false) {
  395. $this->selectedMistakeIds = [];
  396. $this->notify("已为 {$result['success_count']} 道题设置重要程度");
  397. $this->loadMistakeData();
  398. } else {
  399. $this->notify($result['error'] ?? '操作失败', 'danger');
  400. }
  401. }
  402. /**
  403. * 批量切换收藏状态
  404. */
  405. public function batchToggleFavorite(): void
  406. {
  407. if (empty($this->selectedMistakeIds)) {
  408. $this->notify('请先选择至少一道错题', 'warning');
  409. return;
  410. }
  411. $service = app(MistakeBookService::class);
  412. $result = $service->batchOperation($this->selectedMistakeIds, 'favorite');
  413. if ($result['success'] ?? false) {
  414. $this->selectedMistakeIds = [];
  415. $this->notify("已为 {$result['success_count']} 道题切换收藏状态");
  416. $this->loadMistakeData();
  417. } else {
  418. $this->notify($result['error'] ?? '操作失败', 'danger');
  419. }
  420. }
  421. public function startQuickReview(): void
  422. {
  423. if (empty($this->mistakes)) {
  424. $this->notify('没有可复习的错题', 'warning');
  425. return;
  426. }
  427. // 选取前5题进行快速复习
  428. $reviewIds = collect($this->mistakes)
  429. ->take(5)
  430. ->pluck('id')
  431. ->filter()
  432. ->values()
  433. ->all();
  434. // 自动选中这些题
  435. $this->selectedMistakeIds = $reviewIds;
  436. $this->notify('已选择前5题进行快速复习');
  437. }
  438. public function applyFilters(): void
  439. {
  440. $this->page = 1; // 重置到第一页
  441. $this->loadMistakeData();
  442. }
  443. public function clearFilters(): void
  444. {
  445. $this->filters = [
  446. 'kp_ids' => [],
  447. 'skill_ids' => [],
  448. 'error_types' => [],
  449. 'time_range' => 'last_30',
  450. 'start_date' => null,
  451. 'end_date' => null,
  452. 'sort_by' => 'created_at_desc',
  453. 'correct_filter' => 'incorrect',
  454. 'filter' => [],
  455. ];
  456. $this->page = 1;
  457. $this->loadMistakeData();
  458. }
  459. public function resetFilters(): void
  460. {
  461. $this->filters = [
  462. 'kp_ids' => [],
  463. 'skill_ids' => [],
  464. 'error_types' => [],
  465. 'time_range' => 'last_30',
  466. 'start_date' => null,
  467. 'end_date' => null,
  468. 'sort_by' => 'created_at_desc',
  469. 'correct_filter' => 'incorrect',
  470. 'filter' => [],
  471. ];
  472. $this->page = 1;
  473. $this->loadMistakeData();
  474. }
  475. public function toggleFilter(string $type, string $value): void
  476. {
  477. $current = $this->filters[$type] ?? [];
  478. if (in_array($value, $current)) {
  479. // 移除
  480. $this->filters[$type] = array_values(array_diff($current, [$value]));
  481. } else {
  482. // 添加
  483. $this->filters[$type][] = $value;
  484. }
  485. $this->applyFilters();
  486. }
  487. public function clearCustomRange(): void
  488. {
  489. $this->filters['start_date'] = null;
  490. $this->filters['end_date'] = null;
  491. }
  492. #[Computed]
  493. public function teachers(): array
  494. {
  495. try {
  496. $query = Teacher::query()
  497. ->leftJoin('users as u', 'teachers.teacher_id', '=', 'u.user_id')
  498. ->select(
  499. 'teachers.teacher_id',
  500. 'teachers.name',
  501. 'teachers.subject',
  502. 'u.username',
  503. 'u.email'
  504. );
  505. // 如果是老师,只返回自己
  506. if ($this->isTeacher) {
  507. $teacherId = $this->getCurrentTeacherId();
  508. if ($teacherId) {
  509. $query->where('teachers.teacher_id', $teacherId);
  510. }
  511. }
  512. $teachers = $query->orderBy('teachers.name')->get();
  513. $teacherIds = $teachers->pluck('teacher_id')->toArray();
  514. $missingTeacherIds = Student::query()
  515. ->distinct()
  516. ->whereNotIn('teacher_id', $teacherIds)
  517. ->pluck('teacher_id')
  518. ->toArray();
  519. $teachersArray = $teachers->all();
  520. if (!empty($missingTeacherIds)) {
  521. foreach ($missingTeacherIds as $missingId) {
  522. $teachersArray[] = (object) [
  523. 'teacher_id' => $missingId,
  524. 'name' => '未知老师 (' . $missingId . ')',
  525. 'subject' => '未知',
  526. 'username' => null,
  527. 'email' => null
  528. ];
  529. }
  530. usort($teachersArray, function($a, $b) {
  531. return strcmp($a->name, $b->name);
  532. });
  533. }
  534. return $teachersArray;
  535. } catch (\Exception $e) {
  536. Log::error('加载老师列表失败', [
  537. 'error' => $e->getMessage()
  538. ]);
  539. return [];
  540. }
  541. }
  542. #[Computed]
  543. public function students(): array
  544. {
  545. if (empty($this->teacherId)) {
  546. return [];
  547. }
  548. try {
  549. return Student::query()
  550. ->leftJoin('users as u', 'students.student_id', '=', 'u.user_id')
  551. ->where('students.teacher_id', $this->teacherId)
  552. ->select(
  553. 'students.student_id',
  554. 'students.name',
  555. 'students.grade',
  556. 'students.class_name',
  557. 'u.username',
  558. 'u.email'
  559. )
  560. ->orderBy('students.grade')
  561. ->orderBy('students.class_name')
  562. ->orderBy('students.name')
  563. ->get()
  564. ->all();
  565. } catch (\Exception $e) {
  566. Log::error('加载学生列表失败', [
  567. 'teacher_id' => $this->teacherId,
  568. 'error' => $e->getMessage()
  569. ]);
  570. return [];
  571. }
  572. }
  573. public function getStudents(): array
  574. {
  575. return Student::query()
  576. ->select(['student_id', 'name', 'grade', 'class_name'])
  577. ->orderBy('grade')
  578. ->orderBy('class_name')
  579. ->orderBy('name')
  580. ->get()
  581. ->toArray();
  582. }
  583. #[On('teacherChanged')]
  584. public function onTeacherChanged(string $teacherId): void
  585. {
  586. $this->teacherId = $teacherId;
  587. $this->studentId = '';
  588. $this->resetPageState();
  589. }
  590. #[On('studentChanged')]
  591. public function onStudentChanged(?string $teacherId, ?string $studentId): void
  592. {
  593. $this->teacherId = (string) ($teacherId ?? '');
  594. $this->studentId = (string) ($studentId ?? '');
  595. if ($this->studentId) {
  596. $this->loadMistakeData();
  597. } else {
  598. $this->resetPageState();
  599. }
  600. }
  601. protected function loadFilterOptions(): void
  602. {
  603. try {
  604. $knowledgeService = app(KnowledgeServiceApi::class);
  605. $knowledge = $knowledgeService->listKnowledgePoints(150);
  606. $this->filterOptions['knowledge_points'] = $knowledge
  607. ->map(function ($item) {
  608. $code = $item['kp_code'] ?? $item['code'] ?? null;
  609. if (!$code) {
  610. return null;
  611. }
  612. return [
  613. 'code' => $code,
  614. 'name' => $item['cn_name'] ?? $item['name'] ?? $code,
  615. ];
  616. })
  617. ->filter()
  618. ->take(200)
  619. ->values()
  620. ->toArray();
  621. $skills = $knowledgeService->listSkills(null, 200);
  622. $this->filterOptions['skills'] = $skills
  623. ->map(function ($item) {
  624. return [
  625. 'id' => $item['skill_id'] ?? $item['id'] ?? ($item['code'] ?? ''),
  626. 'name' => $item['name'] ?? $item['skill_name'] ?? ($item['code'] ?? ''),
  627. 'kp_code' => $item['kp_code'] ?? $item['knowledge_point_code'] ?? null,
  628. ];
  629. })
  630. ->filter(fn ($item) => filled($item['id']))
  631. ->values()
  632. ->toArray();
  633. } catch (\Throwable $e) {
  634. Log::error('Load filter options failed', [
  635. 'error' => $e->getMessage(),
  636. ]);
  637. $this->filterOptions = ['knowledge_points' => [], 'skills' => []];
  638. }
  639. }
  640. protected function updateMistakeField(string $mistakeId, string $field, $value): void
  641. {
  642. foreach ($this->mistakes as &$mistake) {
  643. if (($mistake['id'] ?? null) === $mistakeId) {
  644. $mistake[$field] = $value;
  645. break;
  646. }
  647. }
  648. }
  649. protected function findMistakeById(string $mistakeId): array
  650. {
  651. foreach ($this->mistakes as $mistake) {
  652. if (($mistake['id'] ?? null) === $mistakeId) {
  653. return $mistake;
  654. }
  655. }
  656. return [];
  657. }
  658. protected function resetPageState(): void
  659. {
  660. $this->mistakes = [];
  661. $this->patterns = [];
  662. $this->summary = [];
  663. $this->selectedMistakeIds = [];
  664. $this->recommendations = [];
  665. $this->relatedQuestions = [];
  666. $this->actionMessage = '';
  667. $this->errorMessage = '';
  668. }
  669. #[Computed(cache: true, key: 'kp-options')]
  670. public function knowledgePointOptions(): array
  671. {
  672. try {
  673. $service = app(KnowledgeGraphService::class);
  674. $kps = $service->listKnowledgePoints(1, 1000);
  675. $options = [];
  676. foreach ($kps['data'] ?? [] as $kp) {
  677. $code = $kp['kp_code'] ?? $kp['id'];
  678. $name = $kp['cn_name'] ?? $kp['name'] ?? $code;
  679. $options[$code] = $name;
  680. }
  681. return $options;
  682. } catch (\Throwable $e) {
  683. Log::error('Failed to load knowledge points: ' . $e->getMessage());
  684. return [];
  685. }
  686. }
  687. protected function notify(string $message, string $type = 'success'): void
  688. {
  689. $this->actionMessage = $message;
  690. $this->actionMessageType = $type;
  691. }
  692. }