DiagnosticChapterService.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  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. // 与普通摸底逻辑保持一致:全教材都摸底后重启,返回第一章
  363. $firstChapter = $chaptersInTextbook->first();
  364. if ($firstChapter) {
  365. $chapterData = $this->getChapterKnowledgePointsSimple((int) $firstChapter->id);
  366. $kpCodesWithQuestions = $this->filterKpCodesWithQuestions($chapterData['kp_codes']);
  367. Log::info('DiagnosticChapterService: 指定章节及同教材已摸底完成,重启到第一章', [
  368. 'textbook_id' => $anchorTextbookId,
  369. 'student_id' => $studentId,
  370. 'target_chapter_ids' => $targetChapterIds,
  371. 'chapter_id' => $firstChapter->id,
  372. ]);
  373. return [
  374. 'chapter_id' => $firstChapter->id,
  375. 'chapter_name' => $firstChapter->name ?? $firstChapter->title ?? '',
  376. 'section_ids' => $chapterData['section_ids'],
  377. 'kp_codes' => $kpCodesWithQuestions,
  378. 'is_restart' => true,
  379. ];
  380. }
  381. Log::warning('DiagnosticChapterService: 指定章节所属教材无章节可用于重启', [
  382. 'textbook_id' => $anchorTextbookId,
  383. 'student_id' => $studentId,
  384. 'target_chapter_ids' => $targetChapterIds,
  385. ]);
  386. return null;
  387. }
  388. $chapters = TextbookCatalog::query()
  389. ->where('textbook_id', $textbookId)
  390. ->where('node_type', 'chapter')
  391. ->orderBy('sort_order')
  392. ->orderBy('display_no')
  393. ->orderBy('id')
  394. ->get();
  395. if ($chapters->isEmpty()) {
  396. return null;
  397. }
  398. foreach ($chapters as $chapter) {
  399. // 检查是否已摸底
  400. if (!$this->hasChapterDiagnostic($studentId, $chapter->id)) {
  401. $chapterData = $this->getChapterKnowledgePointsSimple($chapter->id);
  402. // 过滤掉没有题目的知识点
  403. $kpCodesWithQuestions = $this->filterKpCodesWithQuestions($chapterData['kp_codes']);
  404. if (empty($kpCodesWithQuestions)) {
  405. // 这个章节没有题目,跳过
  406. continue;
  407. }
  408. Log::info('DiagnosticChapterService: 找到第一个未摸底的章节', [
  409. 'textbook_id' => $textbookId,
  410. 'student_id' => $studentId,
  411. 'chapter_id' => $chapter->id,
  412. 'chapter_name' => $chapter->name ?? '',
  413. 'kp_count' => count($kpCodesWithQuestions),
  414. ]);
  415. return [
  416. 'chapter_id' => $chapter->id,
  417. 'chapter_name' => $chapter->name ?? '',
  418. 'section_ids' => $chapterData['section_ids'],
  419. 'kp_codes' => $kpCodesWithQuestions,
  420. ];
  421. }
  422. }
  423. // 所有章节都已摸底,返回第一章(重新开始)
  424. $firstChapter = $chapters->first();
  425. $chapterData = $this->getChapterKnowledgePointsSimple($firstChapter->id);
  426. $kpCodesWithQuestions = $this->filterKpCodesWithQuestions($chapterData['kp_codes']);
  427. Log::info('DiagnosticChapterService: 所有章节都已摸底,返回第一章', [
  428. 'textbook_id' => $textbookId,
  429. 'student_id' => $studentId,
  430. 'chapter_id' => $firstChapter->id,
  431. ]);
  432. return [
  433. 'chapter_id' => $firstChapter->id,
  434. 'chapter_name' => $firstChapter->name ?? '',
  435. 'section_ids' => $chapterData['section_ids'],
  436. 'kp_codes' => $kpCodesWithQuestions,
  437. 'is_restart' => true, // 标记是重新开始
  438. ];
  439. }
  440. /**
  441. * 将传入节点ID(chapter/section/subsection)统一映射为所属 chapter ID 列表。
  442. * 保持传入顺序去重,忽略不属于当前教材的节点。
  443. *
  444. * @param array<int> $nodeIds
  445. * @return array<int>
  446. */
  447. private function normalizeToChapterIds(array $nodeIds): array
  448. {
  449. if (empty($nodeIds)) {
  450. return [];
  451. }
  452. $chapterIds = [];
  453. $seen = [];
  454. foreach ($nodeIds as $nodeId) {
  455. $currentId = (int) $nodeId;
  456. if ($currentId <= 0) {
  457. continue;
  458. }
  459. $guard = 0;
  460. while ($currentId > 0 && $guard++ < 10) {
  461. $node = TextbookCatalog::query()
  462. ->where('id', $currentId)
  463. ->first(['id', 'parent_id', 'node_type']);
  464. if (!$node) {
  465. break;
  466. }
  467. if ($node->node_type === 'chapter') {
  468. if (!isset($seen[$node->id])) {
  469. $seen[$node->id] = true;
  470. $chapterIds[] = (int) $node->id;
  471. }
  472. break;
  473. }
  474. $currentId = (int) ($node->parent_id ?? 0);
  475. }
  476. }
  477. if (count($chapterIds) !== count($nodeIds)) {
  478. Log::info('DiagnosticChapterService: 章节参数已自动映射到chapter节点', [
  479. 'input_ids' => $nodeIds,
  480. 'resolved_chapter_ids' => $chapterIds,
  481. ]);
  482. }
  483. return $chapterIds;
  484. }
  485. /**
  486. * 获取当前应该学习的章节(第一个有未达标知识点的章节)
  487. * 用于智能组卷流程
  488. */
  489. public function getCurrentLearningChapter(int $textbookId, int $studentId, float $threshold = 0.9): ?array
  490. {
  491. $chapters = TextbookCatalog::query()
  492. ->where('textbook_id', $textbookId)
  493. ->where('node_type', 'chapter')
  494. ->orderBy('sort_order')
  495. ->orderBy('display_no')
  496. ->orderBy('id')
  497. ->get();
  498. if ($chapters->isEmpty()) {
  499. return null;
  500. }
  501. foreach ($chapters as $chapter) {
  502. $chapterData = $this->getChapterKnowledgePointsSimple($chapter->id);
  503. $kpCodes = $chapterData['kp_codes'];
  504. if (empty($kpCodes)) {
  505. continue;
  506. }
  507. // 使用新方法判断是否达标(跳过无题知识点)
  508. $allMastered = StudentKnowledgeMastery::allAtLeastSkipNoQuestions($studentId, $kpCodes, $threshold);
  509. if (!$allMastered) {
  510. // 检查是否已摸底
  511. $hasDiagnostic = $this->hasChapterDiagnostic($studentId, $chapter->id);
  512. Log::info('DiagnosticChapterService: 找到当前学习章节', [
  513. 'textbook_id' => $textbookId,
  514. 'student_id' => $studentId,
  515. 'student_id_type' => gettype($studentId),
  516. 'chapter_id' => $chapter->id,
  517. 'chapter_name' => $chapter->name ?? '',
  518. 'has_diagnostic' => $hasDiagnostic,
  519. 'kp_codes' => $kpCodes, // 显示实际的知识点列表
  520. 'kp_count' => count($kpCodes),
  521. ]);
  522. return [
  523. 'chapter_id' => $chapter->id,
  524. 'chapter_name' => $chapter->name ?? '',
  525. 'section_ids' => $chapterData['section_ids'],
  526. 'kp_codes' => $kpCodes,
  527. 'has_diagnostic' => $hasDiagnostic,
  528. ];
  529. }
  530. }
  531. // 所有章节都达标
  532. Log::info('DiagnosticChapterService: 所有章节都达标', [
  533. 'textbook_id' => $textbookId,
  534. 'student_id' => $studentId,
  535. ]);
  536. return null;
  537. }
  538. /**
  539. * 获取下一个章节
  540. */
  541. public function getNextChapter(int $textbookId, int $currentChapterId): ?array
  542. {
  543. $chapters = TextbookCatalog::query()
  544. ->where('textbook_id', $textbookId)
  545. ->where('node_type', 'chapter')
  546. ->orderBy('sort_order')
  547. ->orderBy('display_no')
  548. ->orderBy('id')
  549. ->get();
  550. $foundCurrent = false;
  551. foreach ($chapters as $chapter) {
  552. if ($foundCurrent) {
  553. $chapterData = $this->getChapterKnowledgePointsSimple($chapter->id);
  554. $kpCodesWithQuestions = $this->filterKpCodesWithQuestions($chapterData['kp_codes']);
  555. if (!empty($kpCodesWithQuestions)) {
  556. return [
  557. 'chapter_id' => $chapter->id,
  558. 'chapter_name' => $chapter->name ?? '',
  559. 'section_ids' => $chapterData['section_ids'],
  560. 'kp_codes' => $kpCodesWithQuestions,
  561. ];
  562. }
  563. }
  564. if ($chapter->id === $currentChapterId) {
  565. $foundCurrent = true;
  566. }
  567. }
  568. return null; // 没有下一章
  569. }
  570. /**
  571. * 过滤出有题目的知识点
  572. */
  573. public function filterKpCodesWithQuestions(array $kpCodes): array
  574. {
  575. if (empty($kpCodes)) {
  576. return [];
  577. }
  578. $kpCodesWithQuestions = \App\Models\Question::query()
  579. ->where('audit_status', 0) // 只获取审核通过的题目
  580. ->whereIn('kp_code', $kpCodes)
  581. ->distinct()
  582. ->pluck('kp_code')
  583. ->toArray();
  584. // 保持原有顺序
  585. return array_values(array_intersect($kpCodes, $kpCodesWithQuestions));
  586. }
  587. /**
  588. * 按顺序获取未达标的知识点(用于智能组卷)
  589. *
  590. * @param int $studentId 学生ID
  591. * @param array $kpCodes 知识点列表(按顺序)
  592. * @param float $threshold 达标阈值
  593. * @param int $maxCount 最多返回几个知识点
  594. * @param int $minQuestions 每个知识点最少需要的题目数
  595. * @return array 未达标的知识点列表
  596. */
  597. public function getUnmasteredKpCodesInOrder(
  598. int $studentId,
  599. array $kpCodes,
  600. float $threshold = 0.9,
  601. int $maxCount = 2,
  602. int $minQuestions = 20
  603. ): array {
  604. if (empty($kpCodes)) {
  605. return [];
  606. }
  607. // 获取掌握度(使用 DB::table 避免 Eloquent accessor 把数字转成文字标签)
  608. // 【新增】同时获取 direct_mastery_level 和 mastery_level,判断时优先使用 direct_mastery_level
  609. $masteryRecords = \Illuminate\Support\Facades\DB::table('student_knowledge_mastery')
  610. ->where('student_id', $studentId)
  611. ->whereIn('kp_code', $kpCodes)
  612. ->get(['kp_code', 'mastery_level', 'direct_mastery_level'])
  613. ->keyBy('kp_code');
  614. // 构建有效掌握度映射:取 direct_mastery_level 和 mastery_level 的最大值
  615. // 避免"学了之后反而从达标变成未达标"的问题
  616. $levels = [];
  617. foreach ($masteryRecords as $kpCode => $record) {
  618. $direct = $record->direct_mastery_level;
  619. $mastery = (float) $record->mastery_level;
  620. if ($direct !== null) {
  621. // 取两者最大值:直接学习达标或聚合值达标,都算达标
  622. $levels[$kpCode] = max((float) $direct, $mastery);
  623. } else {
  624. $levels[$kpCode] = $mastery;
  625. }
  626. }
  627. // 【调试】记录查询到的掌握度
  628. Log::info('DiagnosticChapterService: 查询掌握度结果', [
  629. 'student_id' => $studentId,
  630. 'student_id_type' => gettype($studentId),
  631. 'input_kp_codes' => $kpCodes,
  632. 'found_levels' => $levels,
  633. 'raw_records' => $masteryRecords->toArray(),
  634. ]);
  635. // 获取每个知识点的题目数量(只统计审核通过的题目)
  636. $questionCounts = \App\Models\Question::query()
  637. ->where('audit_status', 0) // 只获取审核通过的题目
  638. ->whereIn('kp_code', $kpCodes)
  639. ->selectRaw('kp_code, COUNT(*) as count')
  640. ->groupBy('kp_code')
  641. ->pluck('count', 'kp_code')
  642. ->toArray();
  643. $result = [];
  644. $totalQuestions = 0;
  645. foreach ($kpCodes as $kpCode) {
  646. // 跳过没有题目的知识点
  647. $count = $questionCounts[$kpCode] ?? 0;
  648. if ($count === 0) {
  649. continue;
  650. }
  651. // 检查是否达标
  652. $level = isset($levels[$kpCode]) ? (float) $levels[$kpCode] : 0.0;
  653. $isMastered = $level >= $threshold;
  654. Log::info("DiagnosticChapterService: 检查知识点", [
  655. 'kp_code' => $kpCode,
  656. 'level' => $level,
  657. 'threshold' => $threshold,
  658. 'is_mastered' => $isMastered,
  659. 'level_found_in_db' => isset($levels[$kpCode]),
  660. ]);
  661. if ($level >= $threshold) {
  662. continue;
  663. }
  664. // 添加到结果
  665. $result[] = $kpCode;
  666. $totalQuestions += $count;
  667. // 检查是否达到最大数量
  668. if (count($result) >= $maxCount) {
  669. break;
  670. }
  671. // 检查题目数量是否足够
  672. if ($totalQuestions >= $minQuestions) {
  673. break;
  674. }
  675. }
  676. Log::info('DiagnosticChapterService: 获取未达标知识点', [
  677. 'student_id' => $studentId,
  678. 'input_kp_codes' => $kpCodes,
  679. 'result_kp_codes' => $result,
  680. 'levels_found' => $levels,
  681. 'total_questions' => $totalQuestions,
  682. 'threshold' => $threshold,
  683. ]);
  684. return $result;
  685. }
  686. }