MistakeBook.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. <?php
  2. namespace App\Filament\Pages;
  3. use App\Models\Student;
  4. use App\Services\KnowledgeServiceApi;
  5. use App\Services\MistakeBookService;
  6. use App\Services\QuestionBankService;
  7. use BackedEnum;
  8. use Filament\Pages\Page;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Support\Arr;
  11. use Illuminate\Support\Facades\Log;
  12. use UnitEnum;
  13. use Livewire\Attributes\On;
  14. use Livewire\Attributes\Computed;
  15. use App\Models\Teacher;
  16. class MistakeBook extends Page
  17. {
  18. protected static ?string $title = '错题本';
  19. protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-bookmark';
  20. protected static ?string $navigationLabel = '错题本';
  21. protected static string|UnitEnum|null $navigationGroup = '资源';
  22. protected static ?int $navigationSort = 6;
  23. protected static ?string $slug = 'mistake-book';
  24. protected string $view = 'filament.pages.mistake-book';
  25. public string $teacherId = '';
  26. public string $studentId = '';
  27. public array $filters = [
  28. 'kp_ids' => [],
  29. 'skill_ids' => [],
  30. 'error_types' => [],
  31. 'time_range' => 'last_30',
  32. 'start_date' => null,
  33. 'end_date' => null,
  34. ];
  35. public array $filterOptions = [
  36. 'knowledge_points' => [],
  37. 'skills' => [],
  38. ];
  39. public array $mistakes = [];
  40. public array $patterns = [];
  41. public array $summary = [];
  42. public array $recommendations = [];
  43. public array $relatedQuestions = [];
  44. public array $selectedMistakeIds = [];
  45. public bool $isLoading = false;
  46. public string $errorMessage = '';
  47. public string $actionMessage = '';
  48. public string $actionMessageType = 'success';
  49. public function mount(Request $request): void
  50. {
  51. $this->teacherId = (string) ($request->input('teacher_id') ?? '');
  52. $this->studentId = (string) ($request->input('student_id') ?? '');
  53. $this->filters['time_range'] = (string) ($request->input('range') ?? 'last_30');
  54. if ($this->studentId && empty($this->teacherId)) {
  55. $student = Student::find($this->studentId);
  56. if ($student && $student->teacher_id) {
  57. $this->teacherId = (string) $student->teacher_id;
  58. }
  59. }
  60. $this->loadFilterOptions();
  61. if ($this->studentId) {
  62. $this->loadMistakeData();
  63. }
  64. }
  65. public function updatedStudentId(): void
  66. {
  67. if ($this->studentId) {
  68. $this->loadMistakeData();
  69. } else {
  70. $this->resetPageState();
  71. }
  72. }
  73. public function loadMistakeData(): void
  74. {
  75. if (empty($this->studentId)) {
  76. $this->errorMessage = '请先选择学生';
  77. return;
  78. }
  79. $this->isLoading = true;
  80. $this->errorMessage = '';
  81. $this->actionMessage = '';
  82. try {
  83. $service = app(MistakeBookService::class);
  84. $list = $service->listMistakes([
  85. ...$this->filters,
  86. 'student_id' => $this->studentId,
  87. ]);
  88. $this->mistakes = $list['data'] ?? [];
  89. $this->summary = $service->summarize($this->studentId);
  90. $this->patterns = $service->getMistakePatterns($this->studentId);
  91. // 清理无效的选中项
  92. $validIds = collect($this->mistakes)->pluck('id')->filter()->all();
  93. $this->selectedMistakeIds = array_values(
  94. array_intersect($this->selectedMistakeIds, $validIds)
  95. );
  96. } catch (\Throwable $e) {
  97. $this->errorMessage = '加载错题本数据失败:' . $e->getMessage();
  98. Log::error('Load mistake book failed', [
  99. 'student_id' => $this->studentId,
  100. 'error' => $e->getMessage(),
  101. ]);
  102. } finally {
  103. $this->isLoading = false;
  104. }
  105. }
  106. public function refreshPatterns(): void
  107. {
  108. if (!$this->studentId) {
  109. return;
  110. }
  111. try {
  112. $service = app(MistakeBookService::class);
  113. $this->patterns = $service->getMistakePatterns($this->studentId);
  114. } catch (\Throwable $e) {
  115. Log::error('Refresh mistake patterns failed', [
  116. 'student_id' => $this->studentId,
  117. 'error' => $e->getMessage(),
  118. ]);
  119. }
  120. }
  121. public function toggleFavorite(string $mistakeId): void
  122. {
  123. $service = app(MistakeBookService::class);
  124. $current = $this->findMistakeById($mistakeId);
  125. $willFavorite = !($current['favorite'] ?? false);
  126. if ($service->toggleFavorite($mistakeId, $willFavorite)) {
  127. $this->updateMistakeField($mistakeId, 'favorite', $willFavorite);
  128. $this->notify('已更新收藏状态');
  129. } else {
  130. $this->notify('收藏操作失败,请稍后再试', 'danger');
  131. }
  132. }
  133. public function markReviewed(string $mistakeId): void
  134. {
  135. $service = app(MistakeBookService::class);
  136. if ($service->markReviewed($mistakeId)) {
  137. $this->updateMistakeField($mistakeId, 'reviewed', true);
  138. $this->notify('已标记为已复习');
  139. } else {
  140. $this->notify('标记失败,请稍后再试', 'danger');
  141. }
  142. }
  143. public function addToRetryList(string $mistakeId): void
  144. {
  145. $service = app(MistakeBookService::class);
  146. if ($service->addToRetryList($mistakeId)) {
  147. $this->notify('已加入重练清单');
  148. } else {
  149. $this->notify('加入清单失败,请稍后再试', 'danger');
  150. }
  151. }
  152. public function loadRelatedQuestions(string $mistakeId): void
  153. {
  154. $mistake = $this->findMistakeById($mistakeId);
  155. if (empty($mistake)) {
  156. return;
  157. }
  158. $questionBank = app(QuestionBankService::class);
  159. $kpIds = Arr::wrap($mistake['kp_ids'] ?? []);
  160. $skills = Arr::wrap($mistake['skill_ids'] ?? $mistake['skills'] ?? []);
  161. $response = $questionBank->filterQuestions(array_filter([
  162. 'kp_codes' => !empty($kpIds) ? implode(',', $kpIds) : null,
  163. 'skills' => !empty($skills) ? implode(',', $skills) : null,
  164. 'limit' => 5,
  165. ]));
  166. $this->relatedQuestions[$mistakeId] = $response['data'] ?? [];
  167. }
  168. public function toggleSelection(string $mistakeId): void
  169. {
  170. if (in_array($mistakeId, $this->selectedMistakeIds, true)) {
  171. $this->selectedMistakeIds = array_values(array_diff($this->selectedMistakeIds, [$mistakeId]));
  172. } else {
  173. $this->selectedMistakeIds[] = $mistakeId;
  174. }
  175. }
  176. public function generatePracticeFromSelection(): void
  177. {
  178. if (empty($this->selectedMistakeIds)) {
  179. $this->notify('请先选择至少一道错题', 'warning');
  180. return;
  181. }
  182. $selected = array_filter($this->mistakes, fn ($item) => in_array($item['id'] ?? '', $this->selectedMistakeIds, true));
  183. $kpIds = collect($selected)
  184. ->pluck('kp_ids')
  185. ->flatten()
  186. ->filter()
  187. ->unique()
  188. ->values()
  189. ->all();
  190. $skillIds = collect($selected)
  191. ->pluck('skill_ids')
  192. ->flatten()
  193. ->filter()
  194. ->unique()
  195. ->values()
  196. ->all();
  197. $service = app(MistakeBookService::class);
  198. $result = $service->recommendPractice($this->studentId, $kpIds, $skillIds);
  199. $this->recommendations = $result['data'] ?? ($result['questions'] ?? []);
  200. if (!empty($this->recommendations)) {
  201. $this->notify('已生成重练题单');
  202. } else {
  203. $this->notify('未能生成题单,请稍后再试', 'warning');
  204. }
  205. }
  206. public function applyFilters(): void
  207. {
  208. $this->loadMistakeData();
  209. }
  210. public function clearCustomRange(): void
  211. {
  212. $this->filters['start_date'] = null;
  213. $this->filters['end_date'] = null;
  214. }
  215. #[Computed]
  216. public function teachers(): array
  217. {
  218. try {
  219. $teachers = Teacher::query()
  220. ->leftJoin('users as u', 'teachers.teacher_id', '=', 'u.user_id')
  221. ->select(
  222. 'teachers.teacher_id',
  223. 'teachers.name',
  224. 'teachers.subject',
  225. 'u.username',
  226. 'u.email'
  227. )
  228. ->orderBy('teachers.name')
  229. ->get();
  230. $teacherIds = $teachers->pluck('teacher_id')->toArray();
  231. $missingTeacherIds = Student::query()
  232. ->distinct()
  233. ->whereNotIn('teacher_id', $teacherIds)
  234. ->pluck('teacher_id')
  235. ->toArray();
  236. $teachersArray = $teachers->all();
  237. if (!empty($missingTeacherIds)) {
  238. foreach ($missingTeacherIds as $missingId) {
  239. $teachersArray[] = (object) [
  240. 'teacher_id' => $missingId,
  241. 'name' => '未知老师 (' . $missingId . ')',
  242. 'subject' => '未知',
  243. 'username' => null,
  244. 'email' => null
  245. ];
  246. }
  247. usort($teachersArray, function($a, $b) {
  248. return strcmp($a->name, $b->name);
  249. });
  250. }
  251. return $teachersArray;
  252. } catch (\Exception $e) {
  253. Log::error('加载老师列表失败', [
  254. 'error' => $e->getMessage()
  255. ]);
  256. return [];
  257. }
  258. }
  259. #[Computed]
  260. public function students(): array
  261. {
  262. if (empty($this->teacherId)) {
  263. return [];
  264. }
  265. try {
  266. return Student::query()
  267. ->leftJoin('users as u', 'students.student_id', '=', 'u.user_id')
  268. ->where('students.teacher_id', $this->teacherId)
  269. ->select(
  270. 'students.student_id',
  271. 'students.name',
  272. 'students.grade',
  273. 'students.class_name',
  274. 'u.username',
  275. 'u.email'
  276. )
  277. ->orderBy('students.grade')
  278. ->orderBy('students.class_name')
  279. ->orderBy('students.name')
  280. ->get()
  281. ->all();
  282. } catch (\Exception $e) {
  283. Log::error('加载学生列表失败', [
  284. 'teacher_id' => $this->teacherId,
  285. 'error' => $e->getMessage()
  286. ]);
  287. return [];
  288. }
  289. }
  290. public function getStudents(): array
  291. {
  292. return Student::query()
  293. ->select(['student_id', 'name', 'grade', 'class_name'])
  294. ->orderBy('grade')
  295. ->orderBy('class_name')
  296. ->orderBy('name')
  297. ->get()
  298. ->toArray();
  299. }
  300. #[On('teacherChanged')]
  301. public function onTeacherChanged(string $teacherId): void
  302. {
  303. $this->teacherId = $teacherId;
  304. $this->studentId = '';
  305. $this->resetPageState();
  306. }
  307. #[On('studentChanged')]
  308. public function onStudentChanged(?string $teacherId, ?string $studentId): void
  309. {
  310. $this->teacherId = (string) ($teacherId ?? '');
  311. $this->studentId = (string) ($studentId ?? '');
  312. if ($this->studentId) {
  313. $this->loadMistakeData();
  314. } else {
  315. $this->resetPageState();
  316. }
  317. }
  318. protected function loadFilterOptions(): void
  319. {
  320. try {
  321. $knowledgeService = app(KnowledgeServiceApi::class);
  322. $knowledge = $knowledgeService->listKnowledgePoints(150);
  323. $this->filterOptions['knowledge_points'] = $knowledge
  324. ->map(function ($item) {
  325. $code = $item['kp_code'] ?? $item['code'] ?? null;
  326. if (!$code) {
  327. return null;
  328. }
  329. return [
  330. 'code' => $code,
  331. 'name' => $item['cn_name'] ?? $item['name'] ?? $code,
  332. ];
  333. })
  334. ->filter()
  335. ->take(200)
  336. ->values()
  337. ->toArray();
  338. $skills = $knowledgeService->listSkills(null, 200);
  339. $this->filterOptions['skills'] = $skills
  340. ->map(function ($item) {
  341. return [
  342. 'id' => $item['skill_id'] ?? $item['id'] ?? ($item['code'] ?? ''),
  343. 'name' => $item['name'] ?? $item['skill_name'] ?? ($item['code'] ?? ''),
  344. 'kp_code' => $item['kp_code'] ?? $item['knowledge_point_code'] ?? null,
  345. ];
  346. })
  347. ->filter(fn ($item) => filled($item['id']))
  348. ->values()
  349. ->toArray();
  350. } catch (\Throwable $e) {
  351. Log::error('Load filter options failed', [
  352. 'error' => $e->getMessage(),
  353. ]);
  354. $this->filterOptions = ['knowledge_points' => [], 'skills' => []];
  355. }
  356. }
  357. protected function updateMistakeField(string $mistakeId, string $field, $value): void
  358. {
  359. foreach ($this->mistakes as &$mistake) {
  360. if (($mistake['id'] ?? null) === $mistakeId) {
  361. $mistake[$field] = $value;
  362. break;
  363. }
  364. }
  365. }
  366. protected function findMistakeById(string $mistakeId): array
  367. {
  368. foreach ($this->mistakes as $mistake) {
  369. if (($mistake['id'] ?? null) === $mistakeId) {
  370. return $mistake;
  371. }
  372. }
  373. return [];
  374. }
  375. protected function resetPageState(): void
  376. {
  377. $this->mistakes = [];
  378. $this->patterns = [];
  379. $this->summary = [];
  380. $this->selectedMistakeIds = [];
  381. $this->recommendations = [];
  382. $this->relatedQuestions = [];
  383. $this->actionMessage = '';
  384. $this->errorMessage = '';
  385. }
  386. protected function notify(string $message, string $type = 'success'): void
  387. {
  388. $this->actionMessage = $message;
  389. $this->actionMessageType = $type;
  390. }
  391. }