DiagnosticChapterService.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  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. * - 未传 targetChapterIds: 保持原逻辑,找第一个未摸底章节
  267. */
  268. public function getFirstUndiagnosedChapter(int $textbookId, int $studentId, ?array $targetChapterIds = null): ?array
  269. {
  270. $query = TextbookCatalog::query()
  271. ->where('textbook_id', $textbookId)
  272. ->where('node_type', 'chapter');
  273. $targetChapterIds = is_array($targetChapterIds)
  274. ? array_values(array_unique(array_filter(array_map('intval', $targetChapterIds), fn ($id) => $id > 0)))
  275. : [];
  276. // 兜底:允许传入 section/subsection 节点,自动映射到所属 chapter 节点
  277. if (!empty($targetChapterIds)) {
  278. $targetChapterIds = $this->normalizeToChapterIds($textbookId, $targetChapterIds);
  279. }
  280. $useTargetChapters = !empty($targetChapterIds);
  281. if ($useTargetChapters) {
  282. $query->whereIn('id', $targetChapterIds);
  283. }
  284. $chapters = $query
  285. ->orderBy('sort_order')
  286. ->orderBy('display_no')
  287. ->orderBy('id')
  288. ->get();
  289. if ($chapters->isEmpty()) {
  290. return null;
  291. }
  292. // 用户指定章节:按顺序找“有题知识点”的章节,不判断是否已摸底(允许重复摸底)
  293. if ($useTargetChapters) {
  294. foreach ($chapters as $chapter) {
  295. $chapterData = $this->getChapterKnowledgePointsSimple($chapter->id);
  296. $kpCodesWithQuestions = $this->filterKpCodesWithQuestions($chapterData['kp_codes']);
  297. if (empty($kpCodesWithQuestions)) {
  298. continue;
  299. }
  300. Log::info('DiagnosticChapterService: 指定章节摸底命中章节(允许重复)', [
  301. 'textbook_id' => $textbookId,
  302. 'student_id' => $studentId,
  303. 'target_chapter_ids' => $targetChapterIds,
  304. 'chapter_id' => $chapter->id,
  305. 'chapter_name' => $chapter->name ?? '',
  306. 'kp_count' => count($kpCodesWithQuestions),
  307. ]);
  308. return [
  309. 'chapter_id' => $chapter->id,
  310. 'chapter_name' => $chapter->name ?? '',
  311. 'section_ids' => $chapterData['section_ids'],
  312. 'kp_codes' => $kpCodesWithQuestions,
  313. ];
  314. }
  315. Log::warning('DiagnosticChapterService: 指定章节均无可用题目知识点', [
  316. 'textbook_id' => $textbookId,
  317. 'student_id' => $studentId,
  318. 'target_chapter_ids' => $targetChapterIds,
  319. ]);
  320. return null;
  321. }
  322. foreach ($chapters as $chapter) {
  323. // 检查是否已摸底
  324. if (!$this->hasChapterDiagnostic($studentId, $chapter->id)) {
  325. $chapterData = $this->getChapterKnowledgePointsSimple($chapter->id);
  326. // 过滤掉没有题目的知识点
  327. $kpCodesWithQuestions = $this->filterKpCodesWithQuestions($chapterData['kp_codes']);
  328. if (empty($kpCodesWithQuestions)) {
  329. // 这个章节没有题目,跳过
  330. continue;
  331. }
  332. Log::info('DiagnosticChapterService: 找到第一个未摸底的章节', [
  333. 'textbook_id' => $textbookId,
  334. 'student_id' => $studentId,
  335. 'chapter_id' => $chapter->id,
  336. 'chapter_name' => $chapter->name ?? '',
  337. 'kp_count' => count($kpCodesWithQuestions),
  338. ]);
  339. return [
  340. 'chapter_id' => $chapter->id,
  341. 'chapter_name' => $chapter->name ?? '',
  342. 'section_ids' => $chapterData['section_ids'],
  343. 'kp_codes' => $kpCodesWithQuestions,
  344. ];
  345. }
  346. }
  347. // 所有章节都已摸底,返回第一章(重新开始)
  348. $firstChapter = $chapters->first();
  349. $chapterData = $this->getChapterKnowledgePointsSimple($firstChapter->id);
  350. $kpCodesWithQuestions = $this->filterKpCodesWithQuestions($chapterData['kp_codes']);
  351. Log::info('DiagnosticChapterService: 所有章节都已摸底,返回第一章', [
  352. 'textbook_id' => $textbookId,
  353. 'student_id' => $studentId,
  354. 'chapter_id' => $firstChapter->id,
  355. ]);
  356. return [
  357. 'chapter_id' => $firstChapter->id,
  358. 'chapter_name' => $firstChapter->name ?? '',
  359. 'section_ids' => $chapterData['section_ids'],
  360. 'kp_codes' => $kpCodesWithQuestions,
  361. 'is_restart' => true, // 标记是重新开始
  362. ];
  363. }
  364. /**
  365. * 将传入节点ID(chapter/section/subsection)统一映射为所属 chapter ID 列表。
  366. * 保持传入顺序去重,忽略不属于当前教材的节点。
  367. *
  368. * @param array<int> $nodeIds
  369. * @return array<int>
  370. */
  371. private function normalizeToChapterIds(int $textbookId, array $nodeIds): array
  372. {
  373. if (empty($nodeIds)) {
  374. return [];
  375. }
  376. $chapterIds = [];
  377. $seen = [];
  378. foreach ($nodeIds as $nodeId) {
  379. $currentId = (int) $nodeId;
  380. if ($currentId <= 0) {
  381. continue;
  382. }
  383. $guard = 0;
  384. while ($currentId > 0 && $guard++ < 10) {
  385. $node = TextbookCatalog::query()
  386. ->where('id', $currentId)
  387. ->where('textbook_id', $textbookId)
  388. ->first(['id', 'parent_id', 'node_type']);
  389. if (!$node) {
  390. break;
  391. }
  392. if ($node->node_type === 'chapter') {
  393. if (!isset($seen[$node->id])) {
  394. $seen[$node->id] = true;
  395. $chapterIds[] = (int) $node->id;
  396. }
  397. break;
  398. }
  399. $currentId = (int) ($node->parent_id ?? 0);
  400. }
  401. }
  402. if (count($chapterIds) !== count($nodeIds)) {
  403. Log::info('DiagnosticChapterService: 章节参数已自动映射到chapter节点', [
  404. 'textbook_id' => $textbookId,
  405. 'input_ids' => $nodeIds,
  406. 'resolved_chapter_ids' => $chapterIds,
  407. ]);
  408. }
  409. return $chapterIds;
  410. }
  411. /**
  412. * 获取当前应该学习的章节(第一个有未达标知识点的章节)
  413. * 用于智能组卷流程
  414. */
  415. public function getCurrentLearningChapter(int $textbookId, int $studentId, float $threshold = 0.9): ?array
  416. {
  417. $chapters = TextbookCatalog::query()
  418. ->where('textbook_id', $textbookId)
  419. ->where('node_type', 'chapter')
  420. ->orderBy('sort_order')
  421. ->orderBy('display_no')
  422. ->orderBy('id')
  423. ->get();
  424. if ($chapters->isEmpty()) {
  425. return null;
  426. }
  427. foreach ($chapters as $chapter) {
  428. $chapterData = $this->getChapterKnowledgePointsSimple($chapter->id);
  429. $kpCodes = $chapterData['kp_codes'];
  430. if (empty($kpCodes)) {
  431. continue;
  432. }
  433. // 使用新方法判断是否达标(跳过无题知识点)
  434. $allMastered = StudentKnowledgeMastery::allAtLeastSkipNoQuestions($studentId, $kpCodes, $threshold);
  435. if (!$allMastered) {
  436. // 检查是否已摸底
  437. $hasDiagnostic = $this->hasChapterDiagnostic($studentId, $chapter->id);
  438. Log::info('DiagnosticChapterService: 找到当前学习章节', [
  439. 'textbook_id' => $textbookId,
  440. 'student_id' => $studentId,
  441. 'student_id_type' => gettype($studentId),
  442. 'chapter_id' => $chapter->id,
  443. 'chapter_name' => $chapter->name ?? '',
  444. 'has_diagnostic' => $hasDiagnostic,
  445. 'kp_codes' => $kpCodes, // 显示实际的知识点列表
  446. 'kp_count' => count($kpCodes),
  447. ]);
  448. return [
  449. 'chapter_id' => $chapter->id,
  450. 'chapter_name' => $chapter->name ?? '',
  451. 'section_ids' => $chapterData['section_ids'],
  452. 'kp_codes' => $kpCodes,
  453. 'has_diagnostic' => $hasDiagnostic,
  454. ];
  455. }
  456. }
  457. // 所有章节都达标
  458. Log::info('DiagnosticChapterService: 所有章节都达标', [
  459. 'textbook_id' => $textbookId,
  460. 'student_id' => $studentId,
  461. ]);
  462. return null;
  463. }
  464. /**
  465. * 获取下一个章节
  466. */
  467. public function getNextChapter(int $textbookId, int $currentChapterId): ?array
  468. {
  469. $chapters = TextbookCatalog::query()
  470. ->where('textbook_id', $textbookId)
  471. ->where('node_type', 'chapter')
  472. ->orderBy('sort_order')
  473. ->orderBy('display_no')
  474. ->orderBy('id')
  475. ->get();
  476. $foundCurrent = false;
  477. foreach ($chapters as $chapter) {
  478. if ($foundCurrent) {
  479. $chapterData = $this->getChapterKnowledgePointsSimple($chapter->id);
  480. $kpCodesWithQuestions = $this->filterKpCodesWithQuestions($chapterData['kp_codes']);
  481. if (!empty($kpCodesWithQuestions)) {
  482. return [
  483. 'chapter_id' => $chapter->id,
  484. 'chapter_name' => $chapter->name ?? '',
  485. 'section_ids' => $chapterData['section_ids'],
  486. 'kp_codes' => $kpCodesWithQuestions,
  487. ];
  488. }
  489. }
  490. if ($chapter->id === $currentChapterId) {
  491. $foundCurrent = true;
  492. }
  493. }
  494. return null; // 没有下一章
  495. }
  496. /**
  497. * 过滤出有题目的知识点
  498. */
  499. public function filterKpCodesWithQuestions(array $kpCodes): array
  500. {
  501. if (empty($kpCodes)) {
  502. return [];
  503. }
  504. $kpCodesWithQuestions = \App\Models\Question::query()
  505. ->where('audit_status', 0) // 只获取审核通过的题目
  506. ->whereIn('kp_code', $kpCodes)
  507. ->distinct()
  508. ->pluck('kp_code')
  509. ->toArray();
  510. // 保持原有顺序
  511. return array_values(array_intersect($kpCodes, $kpCodesWithQuestions));
  512. }
  513. /**
  514. * 按顺序获取未达标的知识点(用于智能组卷)
  515. *
  516. * @param int $studentId 学生ID
  517. * @param array $kpCodes 知识点列表(按顺序)
  518. * @param float $threshold 达标阈值
  519. * @param int $maxCount 最多返回几个知识点
  520. * @param int $minQuestions 每个知识点最少需要的题目数
  521. * @return array 未达标的知识点列表
  522. */
  523. public function getUnmasteredKpCodesInOrder(
  524. int $studentId,
  525. array $kpCodes,
  526. float $threshold = 0.9,
  527. int $maxCount = 2,
  528. int $minQuestions = 20
  529. ): array {
  530. if (empty($kpCodes)) {
  531. return [];
  532. }
  533. // 获取掌握度(使用 DB::table 避免 Eloquent accessor 把数字转成文字标签)
  534. // 【新增】同时获取 direct_mastery_level 和 mastery_level,判断时优先使用 direct_mastery_level
  535. $masteryRecords = \Illuminate\Support\Facades\DB::table('student_knowledge_mastery')
  536. ->where('student_id', $studentId)
  537. ->whereIn('kp_code', $kpCodes)
  538. ->get(['kp_code', 'mastery_level', 'direct_mastery_level'])
  539. ->keyBy('kp_code');
  540. // 构建有效掌握度映射:取 direct_mastery_level 和 mastery_level 的最大值
  541. // 避免"学了之后反而从达标变成未达标"的问题
  542. $levels = [];
  543. foreach ($masteryRecords as $kpCode => $record) {
  544. $direct = $record->direct_mastery_level;
  545. $mastery = (float) $record->mastery_level;
  546. if ($direct !== null) {
  547. // 取两者最大值:直接学习达标或聚合值达标,都算达标
  548. $levels[$kpCode] = max((float) $direct, $mastery);
  549. } else {
  550. $levels[$kpCode] = $mastery;
  551. }
  552. }
  553. // 【调试】记录查询到的掌握度
  554. Log::info('DiagnosticChapterService: 查询掌握度结果', [
  555. 'student_id' => $studentId,
  556. 'student_id_type' => gettype($studentId),
  557. 'input_kp_codes' => $kpCodes,
  558. 'found_levels' => $levels,
  559. 'raw_records' => $masteryRecords->toArray(),
  560. ]);
  561. // 获取每个知识点的题目数量(只统计审核通过的题目)
  562. $questionCounts = \App\Models\Question::query()
  563. ->where('audit_status', 0) // 只获取审核通过的题目
  564. ->whereIn('kp_code', $kpCodes)
  565. ->selectRaw('kp_code, COUNT(*) as count')
  566. ->groupBy('kp_code')
  567. ->pluck('count', 'kp_code')
  568. ->toArray();
  569. $result = [];
  570. $totalQuestions = 0;
  571. foreach ($kpCodes as $kpCode) {
  572. // 跳过没有题目的知识点
  573. $count = $questionCounts[$kpCode] ?? 0;
  574. if ($count === 0) {
  575. continue;
  576. }
  577. // 检查是否达标
  578. $level = isset($levels[$kpCode]) ? (float) $levels[$kpCode] : 0.0;
  579. $isMastered = $level >= $threshold;
  580. Log::info("DiagnosticChapterService: 检查知识点", [
  581. 'kp_code' => $kpCode,
  582. 'level' => $level,
  583. 'threshold' => $threshold,
  584. 'is_mastered' => $isMastered,
  585. 'level_found_in_db' => isset($levels[$kpCode]),
  586. ]);
  587. if ($level >= $threshold) {
  588. continue;
  589. }
  590. // 添加到结果
  591. $result[] = $kpCode;
  592. $totalQuestions += $count;
  593. // 检查是否达到最大数量
  594. if (count($result) >= $maxCount) {
  595. break;
  596. }
  597. // 检查题目数量是否足够
  598. if ($totalQuestions >= $minQuestions) {
  599. break;
  600. }
  601. }
  602. Log::info('DiagnosticChapterService: 获取未达标知识点', [
  603. 'student_id' => $studentId,
  604. 'input_kp_codes' => $kpCodes,
  605. 'result_kp_codes' => $result,
  606. 'levels_found' => $levels,
  607. 'total_questions' => $totalQuestions,
  608. 'threshold' => $threshold,
  609. ]);
  610. return $result;
  611. }
  612. }