MistakeBookService.php 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  1. <?php
  2. namespace App\Services;
  3. use App\Models\MistakeRecord;
  4. use App\Models\Student;
  5. use App\Models\Teacher;
  6. use Illuminate\Support\Arr;
  7. use Illuminate\Support\Facades\Cache;
  8. use Illuminate\Support\Facades\Log;
  9. use Illuminate\Support\Facades\Http;
  10. class MistakeBookService
  11. {
  12. protected string $learningAnalyticsBase;
  13. protected int $timeout;
  14. private array $attemptTimelineCache = [];
  15. // 缓存时间(秒)
  16. const CACHE_TTL_SUMMARY = 600; // 10分钟
  17. const CACHE_TTL_PATTERNS = 1800; // 30分钟
  18. public function __construct(
  19. ?string $learningAnalyticsBase = null,
  20. ?int $timeout = null
  21. ) {
  22. // 已迁移到本地,不再使用LearningAnalytics
  23. $this->learningAnalyticsBase = '';
  24. $this->timeout = 20;
  25. }
  26. /**
  27. * 新增错题
  28. */
  29. public function createMistake(array $payload): array
  30. {
  31. $studentId = $payload['student_id'] ?? null;
  32. $questionId = $payload['question_id'] ?? null;
  33. $paperId = $payload['paper_id'] ?? null;
  34. $myAnswer = $payload['my_answer'] ?? null;
  35. $correctAnswer = $payload['correct_answer'] ?? null;
  36. $questionText = $payload['question_text'] ?? null;
  37. $knowledgePoint = $payload['knowledge_point'] ?? null;
  38. $explanation = $payload['explanation'] ?? null;
  39. $kpIds = $payload['kp_ids'] ?? null;
  40. $source = $payload['source'] ?? MistakeRecord::SOURCE_PRACTICE;
  41. $happenedAt = $payload['happened_at'] ?? now();
  42. if (!$studentId) {
  43. throw new \InvalidArgumentException('学生ID不能为空');
  44. }
  45. // 使用事务确保数据一致性
  46. return \DB::transaction(function () use (
  47. $studentId, $questionId, $paperId, $myAnswer, $correctAnswer,
  48. $questionText, $knowledgePoint, $explanation, $kpIds, $source, $happenedAt
  49. ) {
  50. // 【修复】检查是否已存在相同错题(避免重复)
  51. // 重复定义:同一学生 + 同一题目 + 同一卷子 的组合
  52. // 这样允许同一题目在不同卷子中重复,也允许同一卷子中的新题目被记录
  53. $query = MistakeRecord::where('student_id', $studentId)
  54. ->where('source', $source);
  55. // 如果有 question_id 和 paper_id,用它们去重
  56. // 否则用 question_text 去重
  57. if ($questionId && $paperId) {
  58. $query->where('question_id', $questionId)
  59. ->where('paper_id', $paperId);
  60. Log::debug('错题记录重复检查', [
  61. 'student_id' => $studentId,
  62. 'question_id' => $questionId,
  63. 'paper_id' => $paperId,
  64. 'check_type' => 'question_id + paper_id'
  65. ]);
  66. } elseif ($questionId) {
  67. $query->where('question_id', $questionId);
  68. Log::debug('错题记录重复检查', [
  69. 'student_id' => $studentId,
  70. 'question_id' => $questionId,
  71. 'check_type' => 'question_id only'
  72. ]);
  73. } elseif ($questionText) {
  74. $query->where('question_text', $questionText);
  75. Log::debug('错题记录重复检查', [
  76. 'student_id' => $studentId,
  77. 'question_text' => substr($questionText, 0, 50) . '...',
  78. 'check_type' => 'question_text'
  79. ]);
  80. } else {
  81. Log::warning('错题记录保存失败:缺少题目ID和题目文本', [
  82. 'student_id' => $studentId,
  83. 'paper_id' => $paperId
  84. ]);
  85. return [
  86. 'duplicate' => false,
  87. 'error' => '缺少题目ID和题目文本,无法创建错题记录'
  88. ];
  89. }
  90. $existingMistake = $query->first();
  91. if ($existingMistake) {
  92. Log::debug('发现重复错题记录,合并知识点', [
  93. 'student_id' => $studentId,
  94. 'question_id' => $questionId,
  95. 'paper_id' => $paperId,
  96. 'existing_mistake_id' => $existingMistake->id
  97. ]);
  98. // 合并知识点到已有记录中
  99. $updates = ['updated_at' => now()];
  100. if ($kpIds) {
  101. $existingKpIds = $existingMistake->kp_ids ?? [];
  102. $newKpIds = is_array($kpIds) ? $kpIds : [$kpIds];
  103. // 合并并去重
  104. $mergedKpIds = array_values(array_unique(array_merge($existingKpIds, $newKpIds)));
  105. // 更新记录(仅更新 kp_ids)
  106. $updates['kp_ids'] = $mergedKpIds;
  107. Log::info('错题记录知识点合并成功', [
  108. 'student_id' => $studentId,
  109. 'question_id' => $questionId,
  110. 'paper_id' => $paperId,
  111. 'existing_kp_ids' => $existingKpIds,
  112. 'new_kp_ids' => $newKpIds,
  113. 'merged_kp_ids' => $mergedKpIds
  114. ]);
  115. }
  116. $existingMistake->update($updates);
  117. return [
  118. 'duplicate' => true,
  119. 'mistake_id' => $existingMistake->id,
  120. 'message' => '错题已存在,已合并知识点',
  121. ];
  122. }
  123. // 创建错题记录(合并知识点到数组)
  124. $mistake = MistakeRecord::create([
  125. 'student_id' => $studentId,
  126. 'question_id' => $questionId,
  127. 'paper_id' => $paperId,
  128. 'student_answer' => $myAnswer,
  129. 'correct_answer' => $correctAnswer,
  130. 'question_text' => $questionText,
  131. 'knowledge_point' => $knowledgePoint,
  132. 'explanation' => $explanation,
  133. 'kp_ids' => is_array($kpIds) ? $kpIds : ($kpIds ? [$kpIds] : null), // 确保是数组
  134. 'source' => $source,
  135. 'created_at' => $happenedAt,
  136. 'review_status' => MistakeRecord::REVIEW_STATUS_PENDING,
  137. 'review_count' => 0,
  138. ]);
  139. // 清除相关缓存
  140. $this->clearCache($studentId);
  141. return [
  142. 'duplicate' => false,
  143. 'mistake_id' => $mistake->id,
  144. 'created_at' => $mistake->created_at,
  145. ];
  146. });
  147. }
  148. /**
  149. * 批量新增错题(同一学生/卷子)
  150. *
  151. * @param array<int, array> $payloads
  152. * @return array
  153. */
  154. public function createMistakesBatch(array $payloads): array
  155. {
  156. if (empty($payloads)) {
  157. return ['created' => 0, 'duplicates' => 0, 'updated' => 0];
  158. }
  159. $studentId = $payloads[0]['student_id'] ?? null;
  160. $paperId = $payloads[0]['paper_id'] ?? null;
  161. $source = $payloads[0]['source'] ?? MistakeRecord::SOURCE_PRACTICE;
  162. if (!$studentId) {
  163. throw new \InvalidArgumentException('学生ID不能为空');
  164. }
  165. $questionIds = [];
  166. foreach ($payloads as $payload) {
  167. if (!empty($payload['question_id'])) {
  168. $questionIds[] = $payload['question_id'];
  169. }
  170. }
  171. $questionIds = array_values(array_unique($questionIds));
  172. return \DB::transaction(function () use ($payloads, $studentId, $paperId, $source, $questionIds) {
  173. $existingMap = [];
  174. if (!empty($questionIds)) {
  175. $existing = MistakeRecord::where('student_id', $studentId)
  176. ->where('source', $source)
  177. ->when($paperId, fn ($q) => $q->where('paper_id', $paperId))
  178. ->whereIn('question_id', $questionIds)
  179. ->get();
  180. foreach ($existing as $item) {
  181. $existingMap[$item->question_id] = $item;
  182. }
  183. }
  184. $toInsert = [];
  185. $created = 0;
  186. $duplicates = 0;
  187. $updated = 0;
  188. foreach ($payloads as $payload) {
  189. $questionId = $payload['question_id'] ?? null;
  190. if (!$questionId) {
  191. continue;
  192. }
  193. $kpIds = $payload['kp_ids'] ?? null;
  194. $kpIds = is_array($kpIds) ? $kpIds : ($kpIds ? [$kpIds] : null);
  195. if (isset($existingMap[$questionId])) {
  196. $duplicates++;
  197. $updates = ['updated_at' => now()];
  198. if ($kpIds) {
  199. $existingKpIds = $existingMap[$questionId]->kp_ids ?? [];
  200. $merged = array_values(array_unique(array_merge($existingKpIds, $kpIds)));
  201. if ($merged !== $existingKpIds) {
  202. $updates['kp_ids'] = $merged;
  203. $updated++;
  204. }
  205. }
  206. $existingMap[$questionId]->update($updates);
  207. continue;
  208. }
  209. $toInsert[] = [
  210. 'student_id' => $studentId,
  211. 'question_id' => $questionId,
  212. 'paper_id' => $payload['paper_id'] ?? null,
  213. 'student_answer' => $payload['my_answer'] ?? null,
  214. 'correct_answer' => $payload['correct_answer'] ?? null,
  215. 'question_text' => $payload['question_text'] ?? null,
  216. 'knowledge_point' => $payload['knowledge_point'] ?? null,
  217. 'explanation' => $payload['explanation'] ?? null,
  218. 'kp_ids' => $kpIds ? json_encode($kpIds, JSON_UNESCAPED_UNICODE) : null,
  219. 'source' => $payload['source'] ?? MistakeRecord::SOURCE_PRACTICE,
  220. 'created_at' => $payload['happened_at'] ?? now(),
  221. 'review_status' => MistakeRecord::REVIEW_STATUS_PENDING,
  222. 'review_count' => 0,
  223. ];
  224. }
  225. if (!empty($toInsert)) {
  226. // MistakeRecord::insert 不会触发模型事件,但速度更快
  227. MistakeRecord::insert($toInsert);
  228. $created = count($toInsert);
  229. }
  230. // 统一刷新本次题目的 updated_at(无论是否合并)
  231. if (!empty($questionIds)) {
  232. \DB::table('mistake_records')
  233. ->where('student_id', $studentId)
  234. ->where('source', $source)
  235. ->when($paperId, fn ($q) => $q->where('paper_id', $paperId))
  236. ->whereIn('question_id', $questionIds)
  237. ->update(['updated_at' => now()]);
  238. }
  239. $this->clearCache($studentId);
  240. return [
  241. 'created' => $created,
  242. 'duplicates' => $duplicates,
  243. 'updated' => $updated,
  244. ];
  245. });
  246. }
  247. /**
  248. * 获取错题列表
  249. */
  250. public function listMistakes(array $params = []): array
  251. {
  252. $studentId = $params['student_id'] ?? null;
  253. $page = (int) ($params['page'] ?? 1);
  254. $perPage = (int) ($params['per_page'] ?? 20);
  255. if (!$studentId) {
  256. return [
  257. 'data' => [],
  258. 'meta' => ['total' => 0, 'page' => $page, 'per_page' => $perPage],
  259. 'statistics' => [ // ✅ 无 student_id 时也返回空统计
  260. 'total' => 0,
  261. 'pending' => 0,
  262. 'reviewed' => 0,
  263. 'mastered' => 0,
  264. 'favorites' => 0,
  265. 'in_retry_list' => 0,
  266. 'this_week' => 0,
  267. 'mastery_rate' => 0.0,
  268. ],
  269. ];
  270. }
  271. try {
  272. $query = MistakeRecord::forStudent($studentId)
  273. ->with(['student']) // 预加载学生信息
  274. ->orderByDesc('created_at');
  275. // 应用筛选条件
  276. $this->applyFilters($query, $params);
  277. // 获取总数
  278. $total = $query->count();
  279. // 分页获取数据
  280. $mistakes = $query->skip(($page - 1) * $perPage)
  281. ->take($perPage)
  282. ->get();
  283. // 批量预加载当前页作答轨迹,避免逐条查询
  284. $this->preloadAttemptTimelines($mistakes);
  285. // 转换数据格式
  286. $data = $mistakes->map(function ($mistake) {
  287. return $this->transformMistakeRecord($mistake, false);
  288. })->toArray();
  289. if (($params['sort_by'] ?? 'priority_default') === 'priority_default') {
  290. usort($data, function (array $a, array $b): int {
  291. $createdCmp = strcmp((string) ($b['created_at'] ?? ''), (string) ($a['created_at'] ?? ''));
  292. if ($createdCmp !== 0) {
  293. return $createdCmp;
  294. }
  295. $aPending = (($a['review_status'] ?? '') === MistakeRecord::REVIEW_STATUS_PENDING) ? 0 : 1;
  296. $bPending = (($b['review_status'] ?? '') === MistakeRecord::REVIEW_STATUS_PENDING) ? 0 : 1;
  297. if ($aPending !== $bPending) {
  298. return $aPending <=> $bPending;
  299. }
  300. $aLastCorrect = (($a['last_attempt_result'] ?? null) === '对') ? 1 : 0;
  301. $bLastCorrect = (($b['last_attempt_result'] ?? null) === '对') ? 1 : 0;
  302. return $aLastCorrect <=> $bLastCorrect;
  303. });
  304. }
  305. // 获取统计信息
  306. $summary = $this->summarize($studentId);
  307. return [
  308. 'data' => $data,
  309. 'meta' => [
  310. 'total' => $total,
  311. 'page' => $page,
  312. 'per_page' => $perPage,
  313. 'last_page' => (int) ceil($total / $perPage),
  314. ],
  315. 'statistics' => $summary,
  316. ];
  317. } catch (\Throwable $e) {
  318. Log::error('获取错题列表失败', [
  319. 'student_id' => $studentId,
  320. 'error' => $e->getMessage(),
  321. 'params' => $params,
  322. ]);
  323. return [
  324. 'data' => [],
  325. 'meta' => ['total' => 0, 'page' => $page, 'per_page' => $perPage],
  326. 'statistics' => [ // ✅ 错误时也返回空统计
  327. 'total' => 0,
  328. 'pending' => 0,
  329. 'reviewed' => 0,
  330. 'mastered' => 0,
  331. 'favorites' => 0,
  332. 'in_retry_list' => 0,
  333. 'this_week' => 0,
  334. 'mastery_rate' => 0.0,
  335. ],
  336. ];
  337. }
  338. }
  339. /**
  340. * 获取错题详情
  341. */
  342. public function getMistakeDetail(string $mistakeId, ?string $studentId = null): array
  343. {
  344. try {
  345. $query = MistakeRecord::with(['student']);
  346. if ($studentId) {
  347. $query->forStudent($studentId);
  348. }
  349. $mistake = $query->find($mistakeId);
  350. if (!$mistake) {
  351. return [];
  352. }
  353. return $this->transformMistakeRecord($mistake, true);
  354. } catch (\Throwable $e) {
  355. Log::error('获取错题详情失败', [
  356. 'mistake_id' => $mistakeId,
  357. 'student_id' => $studentId,
  358. 'error' => $e->getMessage(),
  359. ]);
  360. return [];
  361. }
  362. }
  363. /**
  364. * 获取错题统计概要
  365. */
  366. public function summarize(string $studentId): array
  367. {
  368. $cacheKey = "mistake_book:summary:{$studentId}";
  369. return Cache::remember($cacheKey, self::CACHE_TTL_SUMMARY, function () use ($studentId) {
  370. return MistakeRecord::getSummary($studentId);
  371. });
  372. }
  373. /**
  374. * 获取错误模式分析
  375. */
  376. public function getMistakePatterns(string $studentId): array
  377. {
  378. $cacheKey = "mistake_book:patterns:{$studentId}";
  379. return Cache::remember($cacheKey, self::CACHE_TTL_PATTERNS, function () use ($studentId) {
  380. return MistakeRecord::getMistakePatterns($studentId);
  381. });
  382. }
  383. /**
  384. * 收藏/取消收藏错题
  385. */
  386. public function toggleFavorite(string $mistakeId, bool $favorite = true): bool
  387. {
  388. try {
  389. $mistake = MistakeRecord::find($mistakeId);
  390. if (!$mistake) {
  391. return false;
  392. }
  393. $mistake->update(['is_favorite' => $favorite]);
  394. // 清除缓存
  395. $this->clearCache($mistake->student_id);
  396. return true;
  397. } catch (\Throwable $e) {
  398. Log::error('收藏错题失败', [
  399. 'mistake_id' => $mistakeId,
  400. 'favorite' => $favorite,
  401. 'error' => $e->getMessage(),
  402. ]);
  403. return false;
  404. }
  405. }
  406. /**
  407. * 标记已复习
  408. */
  409. public function markReviewed(string $mistakeId): bool
  410. {
  411. try {
  412. $mistake = MistakeRecord::find($mistakeId);
  413. if (!$mistake) {
  414. return false;
  415. }
  416. $mistake->markAsReviewed();
  417. // 清除缓存
  418. $this->clearCache($mistake->student_id);
  419. return true;
  420. } catch (\Throwable $e) {
  421. Log::error('标记已复习失败', [
  422. 'mistake_id' => $mistakeId,
  423. 'error' => $e->getMessage(),
  424. ]);
  425. return false;
  426. }
  427. }
  428. /**
  429. * 修改复习状态
  430. */
  431. public function updateReviewStatus(string $mistakeId, string $action = 'increment', bool $forceReview = false): array
  432. {
  433. try {
  434. $mistake = MistakeRecord::find($mistakeId);
  435. if (!$mistake) {
  436. return [
  437. 'success' => false,
  438. 'error' => '错题记录不存在',
  439. ];
  440. }
  441. match ($action) {
  442. 'increment' => $mistake->markAsReviewed(),
  443. 'mastered' => $mistake->markAsMastered(),
  444. 'reset' => $mistake->update([
  445. 'review_status' => MistakeRecord::REVIEW_STATUS_PENDING,
  446. 'review_count' => 0,
  447. 'mastery_level' => null,
  448. 'reviewed_at' => null,
  449. 'next_review_at' => null,
  450. ]),
  451. default => throw new \InvalidArgumentException('无效的操作类型'),
  452. };
  453. // 重新加载模型数据以获取最新状态
  454. $mistake->refresh();
  455. return [
  456. 'success' => true,
  457. 'mistake_id' => $mistakeId,
  458. 'review_status' => $mistake->review_status,
  459. 'review_count' => $mistake->review_count,
  460. 'reviewed_at' => $mistake->reviewed_at?->toISOString(),
  461. 'next_review_at' => $mistake->next_review_at?->toISOString(),
  462. 'mastery_level' => $mistake->mastery_level,
  463. 'message' => '复习状态更新成功',
  464. ];
  465. } catch (\Throwable $e) {
  466. Log::error('更新复习状态失败', [
  467. 'mistake_id' => $mistakeId,
  468. 'action' => $action,
  469. 'error' => $e->getMessage(),
  470. ]);
  471. return [
  472. 'success' => false,
  473. 'error' => $e->getMessage(),
  474. ];
  475. }
  476. }
  477. /**
  478. * 获取复习状态
  479. */
  480. public function getReviewStatus(string $mistakeId): array
  481. {
  482. try {
  483. $mistake = MistakeRecord::find($mistakeId);
  484. if (!$mistake) {
  485. return [
  486. 'success' => false,
  487. 'error' => '错题记录不存在',
  488. ];
  489. }
  490. return [
  491. 'success' => true,
  492. 'mistake_id' => $mistakeId,
  493. 'review_status' => $mistake->review_status,
  494. 'review_count' => $mistake->review_count,
  495. 'reviewed_at' => $mistake->reviewed_at?->toISOString(),
  496. 'next_review_at' => $mistake->next_review_at?->toISOString(),
  497. 'mastery_level' => $mistake->mastery_level,
  498. 'force_review' => $mistake->force_review,
  499. ];
  500. } catch (\Throwable $e) {
  501. Log::error('获取复习状态失败', [
  502. 'mistake_id' => $mistakeId,
  503. 'error' => $e->getMessage(),
  504. ]);
  505. return [
  506. 'success' => false,
  507. 'error' => $e->getMessage(),
  508. ];
  509. }
  510. }
  511. /**
  512. * 增加复习次数
  513. */
  514. public function incrementReviewCount(string $mistakeId, bool $forceReview = false): array
  515. {
  516. return $this->updateReviewStatus($mistakeId, 'increment', $forceReview);
  517. }
  518. /**
  519. * 重置为强制复习状态
  520. */
  521. public function resetReviewStatus(string $mistakeId): array
  522. {
  523. return $this->updateReviewStatus($mistakeId, 'reset');
  524. }
  525. /**
  526. * 添加到重练清单
  527. */
  528. public function addToRetryList(string $mistakeId): bool
  529. {
  530. try {
  531. $mistake = MistakeRecord::find($mistakeId);
  532. if (!$mistake) {
  533. return false;
  534. }
  535. $mistake->addToRetryList();
  536. // 清除缓存
  537. $this->clearCache($mistake->student_id);
  538. return true;
  539. } catch (\Throwable $e) {
  540. Log::error('加入重练清单失败', [
  541. 'mistake_id' => $mistakeId,
  542. 'error' => $e->getMessage(),
  543. ]);
  544. return false;
  545. }
  546. }
  547. /**
  548. * 批量操作错题
  549. */
  550. public function batchOperation(array $mistakeIds, string $operation, array $params = []): array
  551. {
  552. if (empty($mistakeIds)) {
  553. return [
  554. 'success' => false,
  555. 'error' => '请选择要操作的错题',
  556. ];
  557. }
  558. try {
  559. $mistakes = MistakeRecord::whereIn('id', $mistakeIds)->get();
  560. $successCount = 0;
  561. $errors = [];
  562. foreach ($mistakes as $mistake) {
  563. try {
  564. match ($operation) {
  565. 'favorite' => $mistake->toggleFavorite(),
  566. 'reviewed' => $mistake->markAsReviewed(),
  567. 'mastered' => $mistake->markAsMastered(),
  568. 'retry_list' => $mistake->addToRetryList(),
  569. 'remove_retry_list' => $mistake->removeFromRetryList(),
  570. 'set_error_type' => $mistake->update(['error_type' => $params['error_type'] ?? null]),
  571. 'set_importance' => $mistake->update(['importance' => $params['importance'] ?? 5]),
  572. default => throw new \InvalidArgumentException('不支持的操作'),
  573. };
  574. $successCount++;
  575. } catch (\Throwable $e) {
  576. $errors[] = [
  577. 'mistake_id' => $mistake->id,
  578. 'error' => $e->getMessage(),
  579. ];
  580. }
  581. }
  582. // 清除缓存
  583. if ($successCount > 0) {
  584. $studentIds = $mistakes->pluck('student_id')->unique();
  585. foreach ($studentIds as $studentId) {
  586. $this->clearCache($studentId);
  587. }
  588. }
  589. return [
  590. 'success' => true,
  591. 'total' => count($mistakeIds),
  592. 'success_count' => $successCount,
  593. 'error_count' => count($errors),
  594. 'errors' => $errors,
  595. ];
  596. } catch (\Throwable $e) {
  597. Log::error('批量操作失败', [
  598. 'operation' => $operation,
  599. 'mistake_ids' => $mistakeIds,
  600. 'error' => $e->getMessage(),
  601. ]);
  602. return [
  603. 'success' => false,
  604. 'error' => $e->getMessage(),
  605. ];
  606. }
  607. }
  608. /**
  609. * 基于错题推荐练习题(本地实现)
  610. */
  611. public function recommendPractice(string $studentId, array $kpIds = [], array $skillIds = []): array
  612. {
  613. try {
  614. // 获取学生未掌握的知识点
  615. $weakKnowledgePoints = MistakeRecord::forStudent($studentId)
  616. ->where('review_status', '!=', MistakeRecord::REVIEW_STATUS_MASTERED)
  617. ->pluck('knowledge_point')
  618. ->filter()
  619. ->unique()
  620. ->values()
  621. ->toArray();
  622. // 合并传入的知识点
  623. $allKpIds = array_unique(array_merge($kpIds, $weakKnowledgePoints));
  624. // TODO: 这里应该调用本地题库服务
  625. // 目前返回模拟数据
  626. $recommendations = [];
  627. foreach (array_slice($allKpIds, 0, 5) as $kpId) {
  628. $recommendations[] = [
  629. 'id' => "rec_{$kpId}_{$studentId}",
  630. 'kp_code' => $kpId,
  631. 'question_text' => "针对知识点 {$kpId} 的练习题",
  632. 'difficulty' => 0.5,
  633. 'source' => 'recommendation',
  634. ];
  635. }
  636. return ['data' => $recommendations];
  637. } catch (\Throwable $e) {
  638. Log::error('推荐练习题失败', [
  639. 'student_id' => $studentId,
  640. 'error' => $e->getMessage(),
  641. ]);
  642. return ['data' => []];
  643. }
  644. }
  645. /**
  646. * 获取仪表板快照数据
  647. */
  648. public function getPanelSnapshot(string $studentId, int $limit = 5): array
  649. {
  650. try {
  651. $recentMistakes = MistakeRecord::forStudent($studentId)
  652. ->orderByDesc('created_at')
  653. ->limit($limit)
  654. ->get()
  655. ->map(fn($m) => $this->transformMistakeRecord($m))
  656. ->toArray();
  657. $patterns = $this->getMistakePatterns($studentId);
  658. $summary = $this->summarize($studentId);
  659. return [
  660. 'recent' => $recentMistakes,
  661. 'weak_skills' => array_slice($patterns['error_types'] ?? [], 0, 5, true),
  662. 'weak_kps' => array_slice($patterns['knowledge_points'] ?? [], 0, 5, true),
  663. 'error_types' => $patterns['error_types'] ?? [],
  664. 'stats' => $summary,
  665. ];
  666. } catch (\Throwable $e) {
  667. Log::error('获取快照数据失败', [
  668. 'student_id' => $studentId,
  669. 'error' => $e->getMessage(),
  670. ]);
  671. return [
  672. 'recent' => [],
  673. 'weak_skills' => [],
  674. 'weak_kps' => [],
  675. 'error_types' => [],
  676. 'stats' => [
  677. 'total' => 0,
  678. 'pending' => 0,
  679. 'this_week' => 0,
  680. ],
  681. ];
  682. }
  683. }
  684. /**
  685. * 应用筛选条件
  686. */
  687. private function applyFilters($query, array $params): void
  688. {
  689. // 知识点筛选
  690. if (!empty($params['kp_ids'])) {
  691. $kpIds = is_array($params['kp_ids']) ? $params['kp_ids'] : explode(',', $params['kp_ids']);
  692. $query->byKnowledgePoint($kpIds);
  693. }
  694. // 技能筛选
  695. if (!empty($params['skill_ids'])) {
  696. $skillIds = is_array($params['skill_ids']) ? $params['skill_ids'] : explode(',', $params['skill_ids']);
  697. $query->where(function ($q) use ($skillIds) {
  698. foreach ($skillIds as $skillId) {
  699. $q->orWhereJsonContains('skill_ids', $skillId);
  700. }
  701. });
  702. }
  703. // 错误类型筛选
  704. if (!empty($params['error_types'])) {
  705. $errorTypes = is_array($params['error_types']) ? $params['error_types'] : explode(',', $params['error_types']);
  706. $query->whereIn('error_type', $errorTypes);
  707. }
  708. // 时间范围筛选
  709. if (!empty($params['time_range'])) {
  710. match ($params['time_range']) {
  711. 'last_7' => $query->where('created_at', '>=', now()->subDays(7)),
  712. 'last_30' => $query->where('created_at', '>=', now()->subDays(30)),
  713. 'last_90' => $query->where('created_at', '>=', now()->subDays(90)),
  714. default => null,
  715. };
  716. }
  717. // 自定义时间范围
  718. if (!empty($params['start_date'])) {
  719. $query->whereDate('created_at', '>=', $params['start_date']);
  720. }
  721. if (!empty($params['end_date'])) {
  722. $query->whereDate('created_at', '<=', $params['end_date']);
  723. }
  724. // 复习状态筛选
  725. if (!empty($params['unreviewed_only'])) {
  726. $query->pending();
  727. }
  728. if (!empty($params['favorite_only'])) {
  729. $query->favorites();
  730. }
  731. if (!empty($params['in_retry_list_only'])) {
  732. $query->inRetryList();
  733. }
  734. // 排序
  735. $sortBy = $params['sort_by'] ?? 'priority_default';
  736. match ($sortBy) {
  737. 'priority_default' => $query
  738. // 数据库只做主排序,次排序在当前页内完成,避免相关子查询拖慢列表
  739. ->orderByDesc('created_at'),
  740. 'created_at_asc' => $query->orderBy('created_at'),
  741. 'created_at_desc' => $query->orderByDesc('created_at'),
  742. 'review_status_asc' => $query->orderBy('review_status'),
  743. 'difficulty_desc' => $query->orderByDesc('difficulty'),
  744. default => null,
  745. };
  746. }
  747. /**
  748. * 转换错题记录格式
  749. */
  750. private function transformMistakeRecord(MistakeRecord $mistake, bool $detailed = false): array
  751. {
  752. // 【新增】通过 question_id 关联获取题目详情
  753. $questionDetails = $this->getQuestionDetails($mistake->question_id);
  754. // 【新增】通过 kp_ids 或 textbook_catalog_nodes_id 获取知识点信息
  755. $knowledgePoints = $this->getKnowledgePoints(
  756. $mistake->kp_ids,
  757. $mistake->question_id,
  758. $questionDetails['textbook_catalog_nodes_id'] ?? null
  759. );
  760. $attemptTimeline = $this->buildAttemptTimeline($mistake);
  761. $data = [
  762. // ========== 错题记录基本信息 ==========
  763. 'id' => $mistake->id,
  764. 'student_id' => $mistake->student_id,
  765. 'question_id' => $mistake->question_id,
  766. 'paper_id' => $mistake->paper_id,
  767. 'created_at' => $mistake->created_at?->toISOString(),
  768. 'review_status' => $mistake->review_status,
  769. 'review_status_label' => $mistake->review_status_label,
  770. 'review_count' => $mistake->review_count,
  771. 'is_favorite' => $mistake->is_favorite,
  772. 'in_retry_list' => $mistake->in_retry_list,
  773. 'reviewed_at' => $mistake->reviewed_at?->toISOString(),
  774. 'next_review_at' => $mistake->next_review_at?->toISOString(),
  775. 'error_type' => $mistake->error_type,
  776. 'error_type_label' => $mistake->error_type_label,
  777. 'explanation' => $mistake->explanation,
  778. 'skill_ids' => $mistake->skill_ids,
  779. 'difficulty' => $mistake->difficulty,
  780. 'difficulty_level' => $mistake->difficulty_level,
  781. 'importance' => $mistake->importance,
  782. 'mastery_level' => $mistake->mastery_level,
  783. // ========== 题目信息(优先呈现)==========
  784. // 题目内容:优先使用 questions.stem,其次使用错题本自带
  785. 'question_text' => $questionDetails['stem'] ?? $mistake->question_text,
  786. // 答案:优先使用 questions.answer,其次使用错题本自带
  787. 'correct_answer' => $questionDetails['answer'] ?? $mistake->correct_answer,
  788. // 解题过程/解析:优先使用 questions.solution
  789. 'solution' => $questionDetails['solution'] ?? $mistake->explanation,
  790. // 选项
  791. 'options' => $questionDetails['options'] ?? null,
  792. // 题目类型
  793. 'question_type' => $questionDetails['question_type'] ?? null,
  794. // 题目难度(从题目表获取)
  795. 'question_difficulty' => $questionDetails['difficulty'] ?? null,
  796. // 题目标签
  797. 'question_tags' => $questionDetails['tags'] ?? null,
  798. // 题目来源
  799. 'question_source' => $questionDetails['source'] ?? null,
  800. // 学生答案(始终使用错题本中的记录)
  801. 'student_answer' => $mistake->student_answer,
  802. // ========== 知识点信息 ==========
  803. 'knowledge_points' => $knowledgePoints,
  804. // ========== 作答轨迹(错题创建后)==========
  805. 'attempt_timeline' => $attemptTimeline['timeline'],
  806. 'attempt_timeline_text' => $attemptTimeline['timeline_text'],
  807. 'attempt_count' => $attemptTimeline['attempt_count'],
  808. 'correct_count_after_mistake' => $attemptTimeline['correct_count'],
  809. 'last_attempt_result' => $attemptTimeline['last_result'],
  810. 'last_attempt_at' => $attemptTimeline['last_attempt_at'],
  811. ];
  812. if ($detailed) {
  813. $data['student'] = [
  814. 'id' => $mistake->student?->student_id,
  815. 'name' => $mistake->student?->name,
  816. 'grade' => $mistake->student?->grade,
  817. 'class_name' => $mistake->student?->class_name,
  818. ];
  819. }
  820. return $data;
  821. }
  822. /**
  823. * 构建错题创建后的作答轨迹(权威口径:student_answer_steps)
  824. *
  825. * 规则:
  826. * 1. 仅统计当前错题创建时间之后的作答
  827. * 2. 同一 exam_id + question_id 视为一次尝试
  828. * 3. 一次尝试中所有步骤都正确(min_correct=1)记为「对」,否则记为「错」
  829. */
  830. private function buildAttemptTimeline(MistakeRecord $mistake): array
  831. {
  832. $empty = [
  833. 'timeline' => [],
  834. 'timeline_text' => '',
  835. 'attempt_count' => 0,
  836. 'correct_count' => 0,
  837. 'last_result' => null,
  838. 'last_attempt_at' => null,
  839. ];
  840. if (empty($mistake->student_id) || empty($mistake->question_id) || empty($mistake->created_at)) {
  841. return $empty;
  842. }
  843. if (isset($this->attemptTimelineCache[$mistake->id])) {
  844. return $this->attemptTimelineCache[$mistake->id];
  845. }
  846. try {
  847. $attemptRows = \DB::connection('mysql')
  848. ->table('student_answer_steps')
  849. ->selectRaw('exam_id, question_id, MIN(is_correct) AS min_correct, MAX(created_at) AS created_at')
  850. ->where('student_id', $mistake->student_id)
  851. ->where('question_id', $mistake->question_id)
  852. ->where('created_at', '>', $mistake->created_at)
  853. ->groupBy('exam_id', 'question_id')
  854. ->orderBy('created_at', 'asc')
  855. ->get();
  856. if ($attemptRows->isEmpty()) {
  857. return $empty;
  858. }
  859. $timeline = [];
  860. $correctCount = 0;
  861. $lastAttemptAt = null;
  862. foreach ($attemptRows as $row) {
  863. $isCorrect = (int) ($row->min_correct ?? 0) === 1;
  864. $result = $isCorrect ? '对' : '错';
  865. $timeline[] = $result;
  866. if ($isCorrect) {
  867. $correctCount++;
  868. }
  869. if (!empty($row->created_at)) {
  870. $lastAttemptAt = $row->created_at;
  871. }
  872. }
  873. return [
  874. 'timeline' => $timeline,
  875. 'timeline_text' => implode(',', $timeline),
  876. 'attempt_count' => count($timeline),
  877. 'correct_count' => $correctCount,
  878. 'last_result' => end($timeline) ?: null,
  879. 'last_attempt_at' => $lastAttemptAt ? \Carbon\Carbon::parse($lastAttemptAt)->toISOString() : null,
  880. ];
  881. } catch (\Throwable $e) {
  882. Log::warning('构建错题作答轨迹失败', [
  883. 'mistake_id' => $mistake->id,
  884. 'student_id' => $mistake->student_id,
  885. 'question_id' => $mistake->question_id,
  886. 'error' => $e->getMessage(),
  887. ]);
  888. return $empty;
  889. }
  890. }
  891. /**
  892. * 批量预加载当前页错题的作答轨迹
  893. */
  894. private function preloadAttemptTimelines($mistakes): void
  895. {
  896. $this->attemptTimelineCache = [];
  897. if ($mistakes->isEmpty()) {
  898. return;
  899. }
  900. $studentIds = [];
  901. $questionIds = [];
  902. $pairKeys = [];
  903. $minCreatedByPair = [];
  904. foreach ($mistakes as $mistake) {
  905. $studentId = (string) ($mistake->student_id ?? '');
  906. $questionId = (string) ($mistake->question_id ?? '');
  907. $createdAt = (string) ($mistake->created_at ?? '');
  908. if ($studentId === '' || $questionId === '' || $createdAt === '') {
  909. continue;
  910. }
  911. $pairKey = $studentId.'|'.$questionId;
  912. $pairKeys[$pairKey] = true;
  913. $studentIds[$studentId] = true;
  914. $questionIds[$questionId] = true;
  915. if (!isset($minCreatedByPair[$pairKey]) || strcmp($createdAt, $minCreatedByPair[$pairKey]) < 0) {
  916. $minCreatedByPair[$pairKey] = $createdAt;
  917. }
  918. }
  919. if (empty($pairKeys)) {
  920. return;
  921. }
  922. $rows = \DB::connection('mysql')
  923. ->table('student_answer_steps')
  924. ->whereIn('student_id', array_keys($studentIds))
  925. ->whereIn('question_id', array_keys($questionIds))
  926. ->orderBy('created_at', 'asc')
  927. ->get(['student_id', 'question_id', 'exam_id', 'is_correct', 'created_at']);
  928. $rowsByPair = [];
  929. foreach ($rows as $row) {
  930. $pairKey = (string) $row->student_id.'|'.(string) $row->question_id;
  931. if (!isset($pairKeys[$pairKey])) {
  932. continue;
  933. }
  934. $minCreatedAt = $minCreatedByPair[$pairKey] ?? null;
  935. if ($minCreatedAt !== null && strcmp((string) $row->created_at, (string) $minCreatedAt) <= 0) {
  936. continue;
  937. }
  938. $rowsByPair[$pairKey][] = $row;
  939. }
  940. foreach ($mistakes as $mistake) {
  941. $pairKey = (string) ($mistake->student_id ?? '').'|'.(string) ($mistake->question_id ?? '');
  942. $this->attemptTimelineCache[$mistake->id] = $this->buildAttemptTimelineFromRows(
  943. $rowsByPair[$pairKey] ?? [],
  944. (string) ($mistake->created_at ?? '')
  945. );
  946. }
  947. }
  948. /**
  949. * 基于步骤记录构建错题轨迹
  950. *
  951. * @param array<int, object> $rows
  952. */
  953. private function buildAttemptTimelineFromRows(array $rows, string $thresholdCreatedAt): array
  954. {
  955. $empty = [
  956. 'timeline' => [],
  957. 'timeline_text' => '',
  958. 'attempt_count' => 0,
  959. 'correct_count' => 0,
  960. 'last_result' => null,
  961. 'last_attempt_at' => null,
  962. ];
  963. if (empty($rows) || $thresholdCreatedAt === '') {
  964. return $empty;
  965. }
  966. $examAgg = [];
  967. foreach ($rows as $row) {
  968. $createdAt = (string) ($row->created_at ?? '');
  969. if ($createdAt === '' || strcmp($createdAt, $thresholdCreatedAt) <= 0) {
  970. continue;
  971. }
  972. $examId = (string) ($row->exam_id ?? '');
  973. if ($examId === '') {
  974. continue;
  975. }
  976. if (!isset($examAgg[$examId])) {
  977. $examAgg[$examId] = [
  978. 'min_correct' => 1,
  979. 'created_at' => $createdAt,
  980. ];
  981. }
  982. $examAgg[$examId]['min_correct'] = min($examAgg[$examId]['min_correct'], (int) ($row->is_correct ?? 0));
  983. if (strcmp($createdAt, (string) $examAgg[$examId]['created_at']) > 0) {
  984. $examAgg[$examId]['created_at'] = $createdAt;
  985. }
  986. }
  987. if (empty($examAgg)) {
  988. return $empty;
  989. }
  990. uasort($examAgg, fn ($a, $b) => strcmp((string) ($a['created_at'] ?? ''), (string) ($b['created_at'] ?? '')));
  991. $timeline = [];
  992. $correctCount = 0;
  993. $lastAttemptAt = null;
  994. foreach ($examAgg as $agg) {
  995. $isCorrect = (int) ($agg['min_correct'] ?? 0) === 1;
  996. $timeline[] = $isCorrect ? '对' : '错';
  997. if ($isCorrect) {
  998. $correctCount++;
  999. }
  1000. $lastAttemptAt = (string) ($agg['created_at'] ?? $lastAttemptAt);
  1001. }
  1002. return [
  1003. 'timeline' => $timeline,
  1004. 'timeline_text' => implode(',', $timeline),
  1005. 'attempt_count' => count($timeline),
  1006. 'correct_count' => $correctCount,
  1007. 'last_result' => end($timeline) ?: null,
  1008. 'last_attempt_at' => $lastAttemptAt ? \Carbon\Carbon::parse($lastAttemptAt)->toISOString() : null,
  1009. ];
  1010. }
  1011. /**
  1012. * 清除缓存
  1013. */
  1014. private function clearCache(string|int $studentId): void
  1015. {
  1016. try {
  1017. $patterns = [
  1018. "mistake_book:summary:{$studentId}",
  1019. "mistake_book:patterns:{$studentId}",
  1020. ];
  1021. foreach ($patterns as $key) {
  1022. Cache::forget($key);
  1023. }
  1024. } catch (\Exception $e) {
  1025. // 缓存清除失败不影响主流程
  1026. Log::debug('缓存清除失败(可忽略)', ['error' => $e->getMessage()]);
  1027. }
  1028. }
  1029. /**
  1030. * 【新增】通过 question_id 获取题目详情
  1031. */
  1032. private function getQuestionDetails(?string $questionId): ?array
  1033. {
  1034. if (!$questionId) {
  1035. return null;
  1036. }
  1037. try {
  1038. $question = \DB::connection('mysql')
  1039. ->table('questions')
  1040. ->where('id', $questionId)
  1041. ->first();
  1042. if (!$question) {
  1043. return null;
  1044. }
  1045. return [
  1046. 'stem' => $question->stem ?? null, // 题目内容(题干)
  1047. 'options' => $question->options ?? null, // 选项
  1048. 'answer' => $question->answer ?? null, // 答案
  1049. 'solution' => $question->solution ?? null, // 解题过程/解析
  1050. 'difficulty' => $question->difficulty ?? null,
  1051. 'question_type' => $question->question_type ?? null,
  1052. 'textbook_catalog_nodes_id' => $question->textbook_catalog_nodes_id ?? null,
  1053. 'tags' => $question->tags ?? null, // 标签
  1054. 'source' => $question->source ?? null, // 来源
  1055. ];
  1056. } catch (\Exception $e) {
  1057. Log::warning('获取题目详情失败', [
  1058. 'question_id' => $questionId,
  1059. 'error' => $e->getMessage()
  1060. ]);
  1061. return null;
  1062. }
  1063. }
  1064. /**
  1065. * 【新增】通过 kp_ids 或 textbook_catalog_nodes_id 获取知识点信息
  1066. *
  1067. * 逻辑:
  1068. * 1. 如果有 kp_ids,直接从 knowledge_points 表查询
  1069. * 2. 如果没有 kp_ids,通过 textbook_catalog_nodes_id 关联 textbook_chapter_knowledge_relation 表获取 kp_id
  1070. */
  1071. private function getKnowledgePoints(?array $kpIds, ?string $questionId = null, ?int $catalogNodesId = null): array
  1072. {
  1073. // 如果有 kp_ids,直接查询
  1074. if (!empty($kpIds)) {
  1075. return $this->queryKnowledgePointsByCodes($kpIds);
  1076. }
  1077. // 如果没有 kp_ids,通过题目ID和目录节点ID关联查询
  1078. if ($questionId || $catalogNodesId) {
  1079. $kpCodesFromRelation = $this->getKpCodesFromRelation($questionId, $catalogNodesId);
  1080. if (!empty($kpCodesFromRelation)) {
  1081. return $this->queryKnowledgePointsByCodes($kpCodesFromRelation);
  1082. }
  1083. }
  1084. return [];
  1085. }
  1086. /**
  1087. * 通过 kp_codes 查询知识点信息
  1088. */
  1089. private function queryKnowledgePointsByCodes(array $kpCodes): array
  1090. {
  1091. try {
  1092. $knowledgePoints = \DB::connection('mysql')
  1093. ->table('knowledge_points')
  1094. ->whereIn('kp_code', $kpCodes)
  1095. ->select(['kp_code', 'name', 'parent_kp_code'])
  1096. ->get()
  1097. ->toArray();
  1098. return array_map(function ($kp) {
  1099. return [
  1100. 'kp_code' => $kp->kp_code,
  1101. 'name' => $kp->name ?? $kp->kp_code,
  1102. 'parent_kp_code' => $kp->parent_kp_code,
  1103. ];
  1104. }, $knowledgePoints);
  1105. } catch (\Exception $e) {
  1106. Log::warning('通过kp_codes获取知识点信息失败', [
  1107. 'kp_codes' => $kpCodes,
  1108. 'error' => $e->getMessage()
  1109. ]);
  1110. return [];
  1111. }
  1112. }
  1113. /**
  1114. * 通过 textbook_catalog_nodes_id 关联 textbook_chapter_knowledge_relation 表获取 kp_codes
  1115. */
  1116. private function getKpCodesFromRelation(?string $questionId, ?int $catalogNodesId): array
  1117. {
  1118. try {
  1119. $query = \DB::connection('mysql')
  1120. ->table('textbook_chapter_knowledge_relation')
  1121. ->where('is_deleted', 0);
  1122. if ($catalogNodesId) {
  1123. $query->where('catalog_chapter_id', $catalogNodesId);
  1124. } elseif ($questionId) {
  1125. // 如果没有catalog_nodes_id,尝试从questions表获取
  1126. $question = \DB::connection('mysql')
  1127. ->table('questions')
  1128. ->where('id', $questionId)
  1129. ->first();
  1130. if ($question && $question->textbook_catalog_nodes_id) {
  1131. $query->where('catalog_chapter_id', $question->textbook_catalog_nodes_id);
  1132. }
  1133. }
  1134. $relations = $query->select('kp_code')->get();
  1135. if ($relations->isNotEmpty()) {
  1136. Log::debug('通过目录节点关联获取到知识点', [
  1137. 'question_id' => $questionId,
  1138. 'catalog_nodes_id' => $catalogNodesId,
  1139. 'kp_codes' => $relations->pluck('kp_code')->toArray()
  1140. ]);
  1141. }
  1142. return $relations->pluck('kp_code')->toArray();
  1143. } catch (\Exception $e) {
  1144. Log::warning('通过目录节点关联获取kp_codes失败', [
  1145. 'question_id' => $questionId,
  1146. 'catalog_nodes_id' => $catalogNodesId,
  1147. 'error' => $e->getMessage()
  1148. ]);
  1149. return [];
  1150. }
  1151. }
  1152. }