DiagnosticChapterService.php 24 KB

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