DiagnosticChapterService.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. <?php
  2. namespace App\Services;
  3. use Illuminate\Support\Facades\Log;
  4. use App\Models\KnowledgePoint;
  5. use App\Models\StudentKnowledgeMastery;
  6. use App\Models\TextbookCatalog;
  7. use App\Models\TextbookChapterKnowledgeRelation;
  8. class DiagnosticChapterService
  9. {
  10. public function getTextbookKnowledgePointsInOrder(int $textbookId): array
  11. {
  12. $chapters = TextbookCatalog::query()
  13. ->where('textbook_id', $textbookId)
  14. ->where('node_type', 'chapter')
  15. ->orderBy('sort_order')
  16. ->orderBy('display_no')
  17. ->orderBy('id')
  18. ->get();
  19. if ($chapters->isEmpty()) {
  20. Log::warning('DiagnosticChapterService: 未找到章节节点', [
  21. 'textbook_id' => $textbookId,
  22. ]);
  23. return [];
  24. }
  25. $orderedCodes = [];
  26. $seen = [];
  27. foreach ($chapters as $chapter) {
  28. $sectionIds = TextbookCatalog::query()
  29. ->where('parent_id', $chapter->id)
  30. ->where('node_type', 'section')
  31. ->orderBy('sort_order')
  32. ->orderBy('display_no')
  33. ->orderBy('id')
  34. ->pluck('id')
  35. ->toArray();
  36. if (empty($sectionIds)) {
  37. continue;
  38. }
  39. $kpCodes = TextbookChapterKnowledgeRelation::query()
  40. ->whereIn('catalog_chapter_id', $sectionIds)
  41. ->where(function ($q) {
  42. $q->where('is_deleted', 0)->orWhereNull('is_deleted');
  43. })
  44. ->pluck('kp_code')
  45. ->filter()
  46. ->unique()
  47. ->values()
  48. ->toArray();
  49. $kpCodes = $this->expandWithChildKnowledgePoints($kpCodes);
  50. foreach ($kpCodes as $kpCode) {
  51. if (isset($seen[$kpCode])) {
  52. continue;
  53. }
  54. $seen[$kpCode] = true;
  55. $orderedCodes[] = $kpCode;
  56. }
  57. }
  58. Log::info('DiagnosticChapterService: 获取教材知识点顺序列表', [
  59. 'textbook_id' => $textbookId,
  60. 'kp_count' => count($orderedCodes),
  61. ]);
  62. return $orderedCodes;
  63. }
  64. public function getInitialChapterKnowledgePoints(int $textbookId): array
  65. {
  66. $chapter = TextbookCatalog::query()
  67. ->where('textbook_id', $textbookId)
  68. ->where('node_type', 'chapter')
  69. ->orderBy('sort_order')
  70. ->orderBy('display_no')
  71. ->orderBy('id')
  72. ->first();
  73. if (!$chapter) {
  74. Log::warning('DiagnosticChapterService: 未找到章节节点', [
  75. 'textbook_id' => $textbookId,
  76. ]);
  77. return [];
  78. }
  79. $sectionIds = TextbookCatalog::query()
  80. ->where('parent_id', $chapter->id)
  81. ->where('node_type', 'section')
  82. ->orderBy('sort_order')
  83. ->orderBy('display_no')
  84. ->orderBy('id')
  85. ->pluck('id')
  86. ->toArray();
  87. if (empty($sectionIds)) {
  88. Log::warning('DiagnosticChapterService: 章节下未找到section节点', [
  89. 'textbook_id' => $textbookId,
  90. 'chapter_id' => $chapter->id,
  91. ]);
  92. return [];
  93. }
  94. $kpCodes = TextbookChapterKnowledgeRelation::query()
  95. ->whereIn('catalog_chapter_id', $sectionIds)
  96. ->where(function ($q) {
  97. $q->where('is_deleted', 0)->orWhereNull('is_deleted');
  98. })
  99. ->pluck('kp_code')
  100. ->filter()
  101. ->unique()
  102. ->values()
  103. ->toArray();
  104. $kpCodes = $this->expandWithChildKnowledgePoints($kpCodes);
  105. Log::info('DiagnosticChapterService: 获取首章知识点', [
  106. 'textbook_id' => $textbookId,
  107. 'chapter_id' => $chapter->id,
  108. 'section_count' => count($sectionIds),
  109. 'kp_count' => count($kpCodes),
  110. ]);
  111. return [
  112. 'chapter_id' => $chapter->id,
  113. 'section_ids' => $sectionIds,
  114. 'kp_codes' => $kpCodes,
  115. ];
  116. }
  117. public function getFirstUnmasteredChapterKnowledgePoints(int $textbookId, int $studentId, float $threshold = 0.9): array
  118. {
  119. $chapters = TextbookCatalog::query()
  120. ->where('textbook_id', $textbookId)
  121. ->where('node_type', 'chapter')
  122. ->orderBy('sort_order')
  123. ->orderBy('display_no')
  124. ->orderBy('id')
  125. ->get();
  126. if ($chapters->isEmpty()) {
  127. Log::warning('DiagnosticChapterService: 未找到章节节点', [
  128. 'textbook_id' => $textbookId,
  129. ]);
  130. return [];
  131. }
  132. foreach ($chapters as $chapter) {
  133. $sectionIds = TextbookCatalog::query()
  134. ->where('parent_id', $chapter->id)
  135. ->where('node_type', 'section')
  136. ->orderBy('sort_order')
  137. ->orderBy('display_no')
  138. ->orderBy('id')
  139. ->pluck('id')
  140. ->toArray();
  141. if (empty($sectionIds)) {
  142. continue;
  143. }
  144. $kpCodes = TextbookChapterKnowledgeRelation::query()
  145. ->whereIn('catalog_chapter_id', $sectionIds)
  146. ->where(function ($q) {
  147. $q->where('is_deleted', 0)->orWhereNull('is_deleted');
  148. })
  149. ->pluck('kp_code')
  150. ->filter()
  151. ->unique()
  152. ->values()
  153. ->toArray();
  154. $kpCodes = $this->expandWithChildKnowledgePoints($kpCodes);
  155. if (empty($kpCodes)) {
  156. continue;
  157. }
  158. $allMastered = StudentKnowledgeMastery::allAtLeast($studentId, $kpCodes, $threshold);
  159. Log::info('DiagnosticChapterService: 章节掌握度评估', [
  160. 'student_id' => $studentId,
  161. 'textbook_id' => $textbookId,
  162. 'chapter_id' => $chapter->id,
  163. 'section_count' => count($sectionIds),
  164. 'kp_count' => count($kpCodes),
  165. 'all_mastered' => $allMastered,
  166. 'threshold' => $threshold,
  167. ]);
  168. if (!$allMastered) {
  169. return [
  170. 'chapter_id' => $chapter->id,
  171. 'section_ids' => $sectionIds,
  172. 'kp_codes' => $kpCodes,
  173. 'all_mastered' => $allMastered,
  174. ];
  175. }
  176. }
  177. Log::info('DiagnosticChapterService: 所有章节均达到掌握度阈值', [
  178. 'student_id' => $studentId,
  179. 'textbook_id' => $textbookId,
  180. 'threshold' => $threshold,
  181. ]);
  182. return [];
  183. }
  184. private function expandWithChildKnowledgePoints(array $kpCodes): array
  185. {
  186. if (empty($kpCodes)) {
  187. return [];
  188. }
  189. $baseCodes = collect($kpCodes)->filter()->values()->all();
  190. $children = KnowledgePoint::query()
  191. ->whereIn('parent_kp_code', $baseCodes)
  192. ->orderBy('kp_code')
  193. ->get(['parent_kp_code', 'kp_code']);
  194. $childrenMap = [];
  195. foreach ($children as $child) {
  196. $childrenMap[$child->parent_kp_code][] = $child->kp_code;
  197. }
  198. $ordered = [];
  199. $seen = [];
  200. foreach ($baseCodes as $kpCode) {
  201. if (!isset($seen[$kpCode])) {
  202. $seen[$kpCode] = true;
  203. $ordered[] = $kpCode;
  204. }
  205. foreach ($childrenMap[$kpCode] ?? [] as $childCode) {
  206. if (isset($seen[$childCode])) {
  207. continue;
  208. }
  209. $seen[$childCode] = true;
  210. $ordered[] = $childCode;
  211. }
  212. }
  213. return $ordered;
  214. }
  215. // ========== 以下是新增的方法(不扩展子知识点)==========
  216. /**
  217. * 获取章节的知识点(不扩展子知识点)
  218. * 直接返回 section 绑定的知识点
  219. */
  220. public function getChapterKnowledgePointsSimple(int $chapterId): array
  221. {
  222. $sectionIds = TextbookCatalog::query()
  223. ->where('parent_id', $chapterId)
  224. ->where('node_type', 'section')
  225. ->orderBy('sort_order')
  226. ->orderBy('display_no')
  227. ->orderBy('id')
  228. ->pluck('id')
  229. ->toArray();
  230. if (empty($sectionIds)) {
  231. return [
  232. 'section_ids' => [],
  233. 'kp_codes' => [],
  234. ];
  235. }
  236. $kpCodes = TextbookChapterKnowledgeRelation::query()
  237. ->whereIn('catalog_chapter_id', $sectionIds)
  238. ->where(function ($q) {
  239. $q->where('is_deleted', 0)->orWhereNull('is_deleted');
  240. })
  241. ->pluck('kp_code')
  242. ->filter()
  243. ->unique()
  244. ->values()
  245. ->toArray();
  246. // 不扩展子知识点,直接返回
  247. return [
  248. 'section_ids' => $sectionIds,
  249. 'kp_codes' => $kpCodes,
  250. ];
  251. }
  252. /**
  253. * 判断章节是否已经摸底过
  254. */
  255. public function hasChapterDiagnostic(int $studentId, int $chapterId): bool
  256. {
  257. return \App\Models\Paper::query()
  258. ->where('student_id', $studentId)
  259. ->where('paper_type', 0) // 摸底类型
  260. ->where('diagnostic_chapter_id', $chapterId)
  261. ->exists();
  262. }
  263. /**
  264. * 获取章节摸底目标章节
  265. * - 传入 targetChapterIds: 先在指定章节内按顺序找第一个未摸底章节;
  266. * 若指定章节都已摸底,则在该章节所属教材内继续按顺序找未摸底章节。
  267. * - 未传 targetChapterIds: 保持原逻辑,找教材内第一个未摸底章节。
  268. */
  269. public function getFirstUndiagnosedChapter(int $textbookId, int $studentId, ?array $targetChapterIds = null): ?array
  270. {
  271. $targetChapterIds = is_array($targetChapterIds)
  272. ? array_values(array_unique(array_filter(array_map('intval', $targetChapterIds), fn ($id) => $id > 0)))
  273. : [];
  274. // 指定章节摸底流程:先在传入列表找未摸底章节,若都摸底则扩展到同教材继续找。
  275. if (!empty($targetChapterIds)) {
  276. // 兜底:允许传入 section/subsection,自动向上映射到 chapter
  277. $targetChapterIds = $this->normalizeToChapterIds($targetChapterIds);
  278. if (empty($targetChapterIds)) {
  279. Log::warning('DiagnosticChapterService: 指定章节参数无可解析chapter,终止指定章节摸底', [
  280. 'textbook_id' => $textbookId,
  281. 'student_id' => $studentId,
  282. ]);
  283. return null;
  284. }
  285. // 保持传入顺序遍历,不使用教材排序字段重排
  286. $chapterMap = TextbookCatalog::query()
  287. ->whereIn('id', $targetChapterIds)
  288. ->where('node_type', 'chapter')
  289. ->get(['id', 'textbook_id', 'title', 'parent_id', 'node_type', 'sort_order', 'display_no'])
  290. ->keyBy('id');
  291. $orderedChapters = [];
  292. foreach ($targetChapterIds as $chapterId) {
  293. $chapter = $chapterMap->get($chapterId);
  294. if (!$chapter) {
  295. continue;
  296. }
  297. $orderedChapters[] = $chapter;
  298. // 指定列表优先找“未摸底”章节
  299. if ($this->hasChapterDiagnostic($studentId, (int) $chapter->id)) {
  300. continue;
  301. }
  302. $chapterData = $this->getChapterKnowledgePointsSimple($chapter->id);
  303. $kpCodesWithQuestions = $this->filterKpCodesWithQuestions($chapterData['kp_codes']);
  304. if (empty($kpCodesWithQuestions)) {
  305. continue;
  306. }
  307. Log::info('DiagnosticChapterService: 指定章节摸底命中未摸底章节', [
  308. 'textbook_id' => $chapter->textbook_id,
  309. 'student_id' => $studentId,
  310. 'target_chapter_ids' => $targetChapterIds,
  311. 'chapter_id' => $chapter->id,
  312. 'chapter_name' => $chapter->name ?? $chapter->title ?? '',
  313. 'kp_count' => count($kpCodesWithQuestions),
  314. ]);
  315. return [
  316. 'chapter_id' => $chapter->id,
  317. 'chapter_name' => $chapter->name ?? $chapter->title ?? '',
  318. 'section_ids' => $chapterData['section_ids'],
  319. 'kp_codes' => $kpCodesWithQuestions,
  320. ];
  321. }
  322. if (empty($orderedChapters)) {
  323. Log::warning('DiagnosticChapterService: 指定章节未找到有效chapter节点', [
  324. 'student_id' => $studentId,
  325. 'target_chapter_ids' => $targetChapterIds,
  326. ]);
  327. return null;
  328. }
  329. // 指定章节都已摸底:在同教材内继续按顺序找未摸底章节
  330. $anchorTextbookId = (int) ($orderedChapters[0]->textbook_id ?? $textbookId);
  331. $chaptersInTextbook = TextbookCatalog::query()
  332. ->where('textbook_id', $anchorTextbookId)
  333. ->where('node_type', 'chapter')
  334. ->orderBy('sort_order')
  335. ->orderBy('display_no')
  336. ->orderBy('id')
  337. ->get();
  338. foreach ($chaptersInTextbook as $chapter) {
  339. if ($this->hasChapterDiagnostic($studentId, (int) $chapter->id)) {
  340. continue;
  341. }
  342. $chapterData = $this->getChapterKnowledgePointsSimple($chapter->id);
  343. $kpCodesWithQuestions = $this->filterKpCodesWithQuestions($chapterData['kp_codes']);
  344. if (empty($kpCodesWithQuestions)) {
  345. continue;
  346. }
  347. Log::info('DiagnosticChapterService: 指定章节已摸底完,切换同教材后续未摸底章节', [
  348. 'anchor_textbook_id' => $anchorTextbookId,
  349. 'student_id' => $studentId,
  350. 'target_chapter_ids' => $targetChapterIds,
  351. 'chapter_id' => $chapter->id,
  352. 'chapter_name' => $chapter->name ?? $chapter->title ?? '',
  353. 'kp_count' => count($kpCodesWithQuestions),
  354. ]);
  355. return [
  356. 'chapter_id' => $chapter->id,
  357. 'chapter_name' => $chapter->name ?? $chapter->title ?? '',
  358. 'section_ids' => $chapterData['section_ids'],
  359. 'kp_codes' => $kpCodesWithQuestions,
  360. ];
  361. }
  362. Log::warning('DiagnosticChapterService: 指定章节及同教材章节均无可用未摸底章节', [
  363. 'textbook_id' => $anchorTextbookId,
  364. 'student_id' => $studentId,
  365. 'target_chapter_ids' => $targetChapterIds,
  366. ]);
  367. return null;
  368. }
  369. $chapters = TextbookCatalog::query()
  370. ->where('textbook_id', $textbookId)
  371. ->where('node_type', 'chapter')
  372. ->orderBy('sort_order')
  373. ->orderBy('display_no')
  374. ->orderBy('id')
  375. ->get();
  376. if ($chapters->isEmpty()) {
  377. return null;
  378. }
  379. foreach ($chapters as $chapter) {
  380. // 检查是否已摸底
  381. if (!$this->hasChapterDiagnostic($studentId, $chapter->id)) {
  382. $chapterData = $this->getChapterKnowledgePointsSimple($chapter->id);
  383. // 过滤掉没有题目的知识点
  384. $kpCodesWithQuestions = $this->filterKpCodesWithQuestions($chapterData['kp_codes']);
  385. if (empty($kpCodesWithQuestions)) {
  386. // 这个章节没有题目,跳过
  387. continue;
  388. }
  389. Log::info('DiagnosticChapterService: 找到第一个未摸底的章节', [
  390. 'textbook_id' => $textbookId,
  391. 'student_id' => $studentId,
  392. 'chapter_id' => $chapter->id,
  393. 'chapter_name' => $chapter->name ?? '',
  394. 'kp_count' => count($kpCodesWithQuestions),
  395. ]);
  396. return [
  397. 'chapter_id' => $chapter->id,
  398. 'chapter_name' => $chapter->name ?? '',
  399. 'section_ids' => $chapterData['section_ids'],
  400. 'kp_codes' => $kpCodesWithQuestions,
  401. ];
  402. }
  403. }
  404. // 所有章节都已摸底,返回第一章(重新开始)
  405. $firstChapter = $chapters->first();
  406. $chapterData = $this->getChapterKnowledgePointsSimple($firstChapter->id);
  407. $kpCodesWithQuestions = $this->filterKpCodesWithQuestions($chapterData['kp_codes']);
  408. Log::info('DiagnosticChapterService: 所有章节都已摸底,返回第一章', [
  409. 'textbook_id' => $textbookId,
  410. 'student_id' => $studentId,
  411. 'chapter_id' => $firstChapter->id,
  412. ]);
  413. return [
  414. 'chapter_id' => $firstChapter->id,
  415. 'chapter_name' => $firstChapter->name ?? '',
  416. 'section_ids' => $chapterData['section_ids'],
  417. 'kp_codes' => $kpCodesWithQuestions,
  418. 'is_restart' => true, // 标记是重新开始
  419. ];
  420. }
  421. /**
  422. * 将传入节点ID(chapter/section/subsection)统一映射为所属 chapter ID 列表。
  423. * 保持传入顺序去重,忽略不属于当前教材的节点。
  424. *
  425. * @param array<int> $nodeIds
  426. * @return array<int>
  427. */
  428. private function normalizeToChapterIds(array $nodeIds): array
  429. {
  430. if (empty($nodeIds)) {
  431. return [];
  432. }
  433. $chapterIds = [];
  434. $seen = [];
  435. foreach ($nodeIds as $nodeId) {
  436. $currentId = (int) $nodeId;
  437. if ($currentId <= 0) {
  438. continue;
  439. }
  440. $guard = 0;
  441. while ($currentId > 0 && $guard++ < 10) {
  442. $node = TextbookCatalog::query()
  443. ->where('id', $currentId)
  444. ->first(['id', 'parent_id', 'node_type']);
  445. if (!$node) {
  446. break;
  447. }
  448. if ($node->node_type === 'chapter') {
  449. if (!isset($seen[$node->id])) {
  450. $seen[$node->id] = true;
  451. $chapterIds[] = (int) $node->id;
  452. }
  453. break;
  454. }
  455. $currentId = (int) ($node->parent_id ?? 0);
  456. }
  457. }
  458. if (count($chapterIds) !== count($nodeIds)) {
  459. Log::info('DiagnosticChapterService: 章节参数已自动映射到chapter节点', [
  460. 'input_ids' => $nodeIds,
  461. 'resolved_chapter_ids' => $chapterIds,
  462. ]);
  463. }
  464. return $chapterIds;
  465. }
  466. /**
  467. * 获取当前应该学习的章节(第一个有未达标知识点的章节)
  468. * 用于智能组卷流程
  469. */
  470. public function getCurrentLearningChapter(int $textbookId, int $studentId, float $threshold = 0.9): ?array
  471. {
  472. $chapters = TextbookCatalog::query()
  473. ->where('textbook_id', $textbookId)
  474. ->where('node_type', 'chapter')
  475. ->orderBy('sort_order')
  476. ->orderBy('display_no')
  477. ->orderBy('id')
  478. ->get();
  479. if ($chapters->isEmpty()) {
  480. return null;
  481. }
  482. foreach ($chapters as $chapter) {
  483. $chapterData = $this->getChapterKnowledgePointsSimple($chapter->id);
  484. $kpCodes = $chapterData['kp_codes'];
  485. if (empty($kpCodes)) {
  486. continue;
  487. }
  488. // 使用新方法判断是否达标(跳过无题知识点)
  489. $allMastered = StudentKnowledgeMastery::allAtLeastSkipNoQuestions($studentId, $kpCodes, $threshold);
  490. if (!$allMastered) {
  491. // 检查是否已摸底
  492. $hasDiagnostic = $this->hasChapterDiagnostic($studentId, $chapter->id);
  493. Log::info('DiagnosticChapterService: 找到当前学习章节', [
  494. 'textbook_id' => $textbookId,
  495. 'student_id' => $studentId,
  496. 'student_id_type' => gettype($studentId),
  497. 'chapter_id' => $chapter->id,
  498. 'chapter_name' => $chapter->name ?? '',
  499. 'has_diagnostic' => $hasDiagnostic,
  500. 'kp_codes' => $kpCodes, // 显示实际的知识点列表
  501. 'kp_count' => count($kpCodes),
  502. ]);
  503. return [
  504. 'chapter_id' => $chapter->id,
  505. 'chapter_name' => $chapter->name ?? '',
  506. 'section_ids' => $chapterData['section_ids'],
  507. 'kp_codes' => $kpCodes,
  508. 'has_diagnostic' => $hasDiagnostic,
  509. ];
  510. }
  511. }
  512. // 所有章节都达标
  513. Log::info('DiagnosticChapterService: 所有章节都达标', [
  514. 'textbook_id' => $textbookId,
  515. 'student_id' => $studentId,
  516. ]);
  517. return null;
  518. }
  519. /**
  520. * 获取下一个章节
  521. */
  522. public function getNextChapter(int $textbookId, int $currentChapterId): ?array
  523. {
  524. $chapters = TextbookCatalog::query()
  525. ->where('textbook_id', $textbookId)
  526. ->where('node_type', 'chapter')
  527. ->orderBy('sort_order')
  528. ->orderBy('display_no')
  529. ->orderBy('id')
  530. ->get();
  531. $foundCurrent = false;
  532. foreach ($chapters as $chapter) {
  533. if ($foundCurrent) {
  534. $chapterData = $this->getChapterKnowledgePointsSimple($chapter->id);
  535. $kpCodesWithQuestions = $this->filterKpCodesWithQuestions($chapterData['kp_codes']);
  536. if (!empty($kpCodesWithQuestions)) {
  537. return [
  538. 'chapter_id' => $chapter->id,
  539. 'chapter_name' => $chapter->name ?? '',
  540. 'section_ids' => $chapterData['section_ids'],
  541. 'kp_codes' => $kpCodesWithQuestions,
  542. ];
  543. }
  544. }
  545. if ($chapter->id === $currentChapterId) {
  546. $foundCurrent = true;
  547. }
  548. }
  549. return null; // 没有下一章
  550. }
  551. /**
  552. * 过滤出有题目的知识点
  553. */
  554. public function filterKpCodesWithQuestions(array $kpCodes): array
  555. {
  556. if (empty($kpCodes)) {
  557. return [];
  558. }
  559. $kpCodesWithQuestions = \App\Models\Question::query()
  560. ->where('audit_status', 0) // 只获取审核通过的题目
  561. ->whereIn('kp_code', $kpCodes)
  562. ->distinct()
  563. ->pluck('kp_code')
  564. ->toArray();
  565. // 保持原有顺序
  566. return array_values(array_intersect($kpCodes, $kpCodesWithQuestions));
  567. }
  568. /**
  569. * 按顺序获取未达标的知识点(用于智能组卷)
  570. *
  571. * @param int $studentId 学生ID
  572. * @param array $kpCodes 知识点列表(按顺序)
  573. * @param float $threshold 达标阈值
  574. * @param int $maxCount 最多返回几个知识点
  575. * @param int $minQuestions 每个知识点最少需要的题目数
  576. * @return array 未达标的知识点列表
  577. */
  578. public function getUnmasteredKpCodesInOrder(
  579. int $studentId,
  580. array $kpCodes,
  581. float $threshold = 0.9,
  582. int $maxCount = 2,
  583. int $minQuestions = 20
  584. ): array {
  585. if (empty($kpCodes)) {
  586. return [];
  587. }
  588. // 获取掌握度(使用 DB::table 避免 Eloquent accessor 把数字转成文字标签)
  589. // 【新增】同时获取 direct_mastery_level 和 mastery_level,判断时优先使用 direct_mastery_level
  590. $masteryRecords = \Illuminate\Support\Facades\DB::table('student_knowledge_mastery')
  591. ->where('student_id', $studentId)
  592. ->whereIn('kp_code', $kpCodes)
  593. ->get(['kp_code', 'mastery_level', 'direct_mastery_level'])
  594. ->keyBy('kp_code');
  595. // 构建有效掌握度映射:取 direct_mastery_level 和 mastery_level 的最大值
  596. // 避免"学了之后反而从达标变成未达标"的问题
  597. $levels = [];
  598. foreach ($masteryRecords as $kpCode => $record) {
  599. $direct = $record->direct_mastery_level;
  600. $mastery = (float) $record->mastery_level;
  601. if ($direct !== null) {
  602. // 取两者最大值:直接学习达标或聚合值达标,都算达标
  603. $levels[$kpCode] = max((float) $direct, $mastery);
  604. } else {
  605. $levels[$kpCode] = $mastery;
  606. }
  607. }
  608. // 【调试】记录查询到的掌握度
  609. Log::info('DiagnosticChapterService: 查询掌握度结果', [
  610. 'student_id' => $studentId,
  611. 'student_id_type' => gettype($studentId),
  612. 'input_kp_codes' => $kpCodes,
  613. 'found_levels' => $levels,
  614. 'raw_records' => $masteryRecords->toArray(),
  615. ]);
  616. // 获取每个知识点的题目数量(只统计审核通过的题目)
  617. $questionCounts = \App\Models\Question::query()
  618. ->where('audit_status', 0) // 只获取审核通过的题目
  619. ->whereIn('kp_code', $kpCodes)
  620. ->selectRaw('kp_code, COUNT(*) as count')
  621. ->groupBy('kp_code')
  622. ->pluck('count', 'kp_code')
  623. ->toArray();
  624. $result = [];
  625. $totalQuestions = 0;
  626. foreach ($kpCodes as $kpCode) {
  627. // 跳过没有题目的知识点
  628. $count = $questionCounts[$kpCode] ?? 0;
  629. if ($count === 0) {
  630. continue;
  631. }
  632. // 检查是否达标
  633. $level = isset($levels[$kpCode]) ? (float) $levels[$kpCode] : 0.0;
  634. $isMastered = $level >= $threshold;
  635. Log::info("DiagnosticChapterService: 检查知识点", [
  636. 'kp_code' => $kpCode,
  637. 'level' => $level,
  638. 'threshold' => $threshold,
  639. 'is_mastered' => $isMastered,
  640. 'level_found_in_db' => isset($levels[$kpCode]),
  641. ]);
  642. if ($level >= $threshold) {
  643. continue;
  644. }
  645. // 添加到结果
  646. $result[] = $kpCode;
  647. $totalQuestions += $count;
  648. // 检查是否达到最大数量
  649. if (count($result) >= $maxCount) {
  650. break;
  651. }
  652. // 检查题目数量是否足够
  653. if ($totalQuestions >= $minQuestions) {
  654. break;
  655. }
  656. }
  657. Log::info('DiagnosticChapterService: 获取未达标知识点', [
  658. 'student_id' => $studentId,
  659. 'input_kp_codes' => $kpCodes,
  660. 'result_kp_codes' => $result,
  661. 'levels_found' => $levels,
  662. 'total_questions' => $totalQuestions,
  663. 'threshold' => $threshold,
  664. ]);
  665. return $result;
  666. }
  667. }