MistakeBook.php 15 KB

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