QuestionTemReviewService.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Question;
  4. use App\Support\AnswerSolutionStepMarkerInjector;
  5. use Illuminate\Support\Facades\Cache;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Facades\Schema;
  8. use Illuminate\Support\Str;
  9. /**
  10. * questions_tem 质检入库页:知识点排序、PDF 口径预览、写入 questions
  11. */
  12. class QuestionTemReviewService
  13. {
  14. /** Session 键:供「入库题目调难度」页列举的本轮已入库 question.id */
  15. public const SESSION_TUNING_QUESTION_IDS = 'import_difficulty_tune_question_ids';
  16. /**
  17. * 将成功入库的正式题 ID 合并进会话,供调难度页使用。
  18. *
  19. * @param list<int> $questionIds
  20. */
  21. public static function mergeQuestionIdsIntoTuningSession(array $questionIds): void
  22. {
  23. $questionIds = array_values(array_unique(array_filter(array_map('intval', $questionIds))));
  24. if ($questionIds === []) {
  25. return;
  26. }
  27. $existing = session(self::SESSION_TUNING_QUESTION_IDS, []);
  28. if (! is_array($existing)) {
  29. $existing = [];
  30. }
  31. session([
  32. self::SESSION_TUNING_QUESTION_IDS => array_values(array_unique(array_merge($existing, $questionIds))),
  33. ]);
  34. }
  35. /**
  36. * 左侧:按 questions 表中该知识点正式题数量升序(题少的在前),仅包含 questions_tem 中出现过的 kp_code
  37. *
  38. * @param ?int $limit 为 null 时不截断(质检页需完整列表 + 搜索,否则题量大的 KP 如 B01 会落在 500 条之后而无法检索)
  39. * @return list<array{kp_code: string, kp_name: string, questions_count: int, tem_count: int, tem_importable_count: int}>
  40. */
  41. public function listKnowledgePointsByQuestionsAsc(?int $limit = null, ?int $gradeFilter = null, ?int $semesterFilter = null): array
  42. {
  43. if (! Schema::hasTable('questions_tem')) {
  44. return [];
  45. }
  46. $gradeKpWhitelist = null;
  47. if ($gradeFilter !== null || $semesterFilter !== null) {
  48. $gradeKpWhitelist = $this->kpCodesFromCatalogFilter($gradeFilter, $semesterFilter);
  49. if ($gradeKpWhitelist === []) {
  50. return [];
  51. }
  52. }
  53. $temQuery = DB::table('questions_tem')
  54. ->whereNotNull('kp_code')
  55. ->where('kp_code', '!=', '');
  56. if ($gradeKpWhitelist !== null) {
  57. $temQuery->whereIn('kp_code', $gradeKpWhitelist);
  58. }
  59. $temKps = $temQuery->distinct()->pluck('kp_code')->all();
  60. if ($temKps === []) {
  61. return [];
  62. }
  63. $kpNames = [];
  64. if (Schema::hasTable('knowledge_points')) {
  65. $kpNames = DB::table('knowledge_points')
  66. ->whereIn('kp_code', $temKps)
  67. ->pluck('name', 'kp_code')
  68. ->toArray();
  69. }
  70. $counts = [];
  71. if (Schema::hasTable('questions')) {
  72. $countQuery = DB::table('questions')->whereIn('kp_code', $temKps);
  73. $counts = $countQuery
  74. ->selectRaw('kp_code, COUNT(*) as c')
  75. ->groupBy('kp_code')
  76. ->pluck('c', 'kp_code')
  77. ->toArray();
  78. }
  79. $temCountQuery = DB::table('questions_tem')->whereIn('kp_code', $temKps);
  80. $temCounts = $temCountQuery
  81. ->selectRaw('kp_code, COUNT(*) as c')
  82. ->groupBy('kp_code')
  83. ->pluck('c', 'kp_code')
  84. ->toArray();
  85. $importableMap = $this->importableTemCountsForKpCodes($temKps, $gradeFilter);
  86. $rows = [];
  87. foreach ($temKps as $kp) {
  88. $name = isset($kpNames[$kp]) ? trim((string) $kpNames[$kp]) : '';
  89. $rows[] = [
  90. 'kp_code' => $kp,
  91. 'kp_name' => $name,
  92. 'questions_count' => (int) ($counts[$kp] ?? 0),
  93. 'tem_count' => (int) ($temCounts[$kp] ?? 0),
  94. 'tem_importable_count' => (int) ($importableMap[$kp] ?? 0),
  95. ];
  96. }
  97. usort($rows, function ($a, $b) {
  98. if ($a['questions_count'] === $b['questions_count']) {
  99. return strcmp($a['kp_code'], $b['kp_code']);
  100. }
  101. return $a['questions_count'] <=> $b['questions_count'];
  102. });
  103. if ($limit !== null && $limit > 0) {
  104. return array_slice($rows, 0, $limit);
  105. }
  106. return $rows;
  107. }
  108. /**
  109. * 与中间列表 {@see listTemQuestionsForKp}(excludeFormalDuplicates=true)同口径:本 KP 下、题干未与正式库重复、且题干非空的 questions_tem 行数。
  110. *
  111. * @param list<string> $kpCodes
  112. * @return array<string, int> kp_code => count
  113. */
  114. private function importableTemCountsForKpCodes(array $kpCodes, ?int $gradeFilter = null): array
  115. {
  116. $kpCodes = array_values(array_unique(array_filter($kpCodes)));
  117. if ($kpCodes === [] || ! Schema::hasTable('questions_tem')) {
  118. return [];
  119. }
  120. if (! Schema::hasTable('questions')) {
  121. return collect($kpCodes)
  122. ->mapWithKeys(function (string $k): array {
  123. $q = DB::table('questions_tem')->where('kp_code', $k);
  124. return [$k => (int) $q->count()];
  125. })
  126. ->all();
  127. }
  128. $placeholders = implode(',', array_fill(0, count($kpCodes), '?'));
  129. $sql = "
  130. SELECT t.kp_code AS kp_code, COUNT(*) AS c
  131. FROM questions_tem t
  132. WHERE t.kp_code IN ($placeholders)
  133. AND t.stem IS NOT NULL AND t.stem != ''
  134. AND NOT EXISTS (
  135. SELECT 1 FROM questions q
  136. WHERE q.kp_code = t.kp_code
  137. AND q.stem != ''
  138. AND q.stem = t.stem
  139. )
  140. GROUP BY t.kp_code
  141. ";
  142. $out = array_fill_keys($kpCodes, 0);
  143. foreach (DB::select($sql, $kpCodes) as $row) {
  144. $k = (string) ($row->kp_code ?? '');
  145. if ($k !== '') {
  146. $out[$k] = (int) ($row->c ?? 0);
  147. }
  148. }
  149. return $out;
  150. }
  151. /**
  152. * 与入库、判重一致:questions_tem.stem(当前库表仅此 longtext 题干列,无 content)。
  153. */
  154. public function normalizedStemFromTemRow(object|array $row): string
  155. {
  156. $arr = is_array($row) ? $row : (array) $row;
  157. return (string) ($arr['stem'] ?? '');
  158. }
  159. /**
  160. * 中间:某知识点下 questions_tem 题目(限制条数)
  161. *
  162. * @param bool $excludeFormalDuplicates 为 true 时排除「正式库 questions 已存在同 kp_code + 同 stem」的待审行,与 {@see existsDuplicateInQuestions} 一致,减少无效质检
  163. * @return list<object>
  164. */
  165. public function listTemQuestionsForKp(string $kpCode, int $limit = 300, bool $excludeFormalDuplicates = true, ?int $gradeFilter = null): array
  166. {
  167. if (! Schema::hasTable('questions_tem') || $kpCode === '') {
  168. return [];
  169. }
  170. $formalStemSet = [];
  171. if ($excludeFormalDuplicates && Schema::hasTable('questions')) {
  172. $qFormal = DB::table('questions')->where('kp_code', $kpCode);
  173. foreach ($qFormal->pluck('stem') as $stem) {
  174. if ($stem === null || $stem === '') {
  175. continue;
  176. }
  177. $formalStemSet[(string) $stem] = true;
  178. }
  179. }
  180. $out = [];
  181. $q = DB::table('questions_tem')->where('kp_code', $kpCode)->orderBy('id');
  182. foreach ($q->lazyById(200) as $row) {
  183. if ($excludeFormalDuplicates && $formalStemSet !== []) {
  184. $stem = $this->normalizedStemFromTemRow($row);
  185. if ($stem !== '' && isset($formalStemSet[$stem])) {
  186. continue;
  187. }
  188. }
  189. $out[] = $row;
  190. if (count($out) >= $limit) {
  191. break;
  192. }
  193. }
  194. return $out;
  195. }
  196. /**
  197. * 与 ExamPdfExportService::renderPreviewHtml 一致:公式预处理 + 解析换行,供页面 KaTeX 渲染
  198. *
  199. * @param array<string, mixed> $row questions_tem 一行转数组
  200. * @return array{stem: string, options: ?array, answer: string, solution: string, question_type: string}
  201. */
  202. public function buildPdfStylePreviewFields(array $row): array
  203. {
  204. $stem = (string) ($row['stem'] ?? '');
  205. $answer = (string) ($row['answer'] ?? $row['correct_answer'] ?? '');
  206. $solution = AnswerSolutionStepMarkerInjector::enrichIfNeeded(
  207. (string) ($row['solution'] ?? ''),
  208. $row['question_type'] ?? $row['tags'] ?? 'answer'
  209. );
  210. $questionType = strtolower((string) ($row['question_type'] ?? $row['tags'] ?? 'answer'));
  211. $options = $row['options'] ?? null;
  212. if (is_string($options) && trim($options) !== '') {
  213. $decoded = json_decode($options, true);
  214. $options = is_array($decoded) ? $decoded : null;
  215. }
  216. $processedStem = MathFormulaProcessor::processFormulas($stem);
  217. $processedAnswer = MathFormulaProcessor::processFormulas($answer);
  218. $processedSolution = MathFormulaProcessor::processFormulas($this->formatNewlinesForPdf($solution));
  219. $processedOptions = null;
  220. if (is_array($options)) {
  221. $processedOptions = [];
  222. foreach ($options as $key => $value) {
  223. if (is_array($value)) {
  224. $text = (string) ($value['text'] ?? $value['value'] ?? reset($value) ?? '');
  225. $processedOptions[$key] = MathFormulaProcessor::processFormulas($text);
  226. } else {
  227. $processedOptions[$key] = MathFormulaProcessor::processFormulas((string) $value);
  228. }
  229. }
  230. }
  231. return [
  232. 'stem' => $processedStem,
  233. 'options' => $processedOptions,
  234. 'answer' => $processedAnswer,
  235. 'solution' => $processedSolution,
  236. 'question_type' => $questionType,
  237. ];
  238. }
  239. private function formatNewlinesForPdf(?string $text): string
  240. {
  241. if ($text === null || $text === '') {
  242. return '';
  243. }
  244. $text = preg_replace('/\\\\n(?![a-zA-Z])/', '<br>', $text);
  245. return (string) preg_replace('/(<br>\s*){3,}/', '<br><br>', $text);
  246. }
  247. /**
  248. * 是否已在 questions 中存在(同 kp + 题干完全一致则视为重复)
  249. */
  250. public function existsDuplicateInQuestions(string $kpCode, string $stem): bool
  251. {
  252. if ($stem === '' || ! Schema::hasTable('questions')) {
  253. return false;
  254. }
  255. return Question::query()
  256. ->where('kp_code', $kpCode)
  257. ->where('stem', $stem)
  258. ->exists();
  259. }
  260. /**
  261. * 待审行默认难度:与入库写入规则一致,限制在 [0.00, 0.90] 并保留两位小数
  262. *
  263. * @param object|array<string, mixed> $row
  264. */
  265. public function defaultDifficultyForTemRow(object|array $row): float
  266. {
  267. $arr = is_array($row) ? $row : (array) $row;
  268. $d = 0.5;
  269. if (array_key_exists('difficulty', $arr) && $arr['difficulty'] !== null && $arr['difficulty'] !== '') {
  270. $d = (float) $arr['difficulty'];
  271. }
  272. return max(0.0, min(0.9, round($d, 2)));
  273. }
  274. /**
  275. * 将 questions_tem 一行写入 questions(入库)
  276. *
  277. * @param ?float $difficultyOverride 若传入则作为 questions.difficulty(仍限制 0.00–0.90、两位小数);null 时按表内字段或默认 0.5
  278. * @return array{ok: bool, message: string, question_id: ?int}
  279. */
  280. public function importTemRowToQuestions(int $temId, ?float $difficultyOverride = null): array
  281. {
  282. if (! Schema::hasTable('questions_tem')) {
  283. return ['ok' => false, 'message' => 'questions_tem 表不存在', 'question_id' => null];
  284. }
  285. $row = DB::table('questions_tem')->where('id', $temId)->first();
  286. if (! $row) {
  287. return ['ok' => false, 'message' => '待入库题目不存在', 'question_id' => null];
  288. }
  289. $arr = (array) $row;
  290. $stem = $this->normalizedStemFromTemRow($arr);
  291. $kp = (string) ($arr['kp_code'] ?? '');
  292. if ($stem === '' || $kp === '') {
  293. return ['ok' => false, 'message' => '题干或知识点为空', 'question_id' => null];
  294. }
  295. if ($this->existsDuplicateInQuestions($kp, $stem)) {
  296. return ['ok' => false, 'message' => '正式库已存在相同知识点且题干一致的题目', 'question_id' => null];
  297. }
  298. $options = $arr['options'] ?? null;
  299. if (is_string($options) && trim($options) !== '') {
  300. $decoded = json_decode($options, true);
  301. $options = is_array($decoded) ? $decoded : null;
  302. }
  303. $questionType = $this->normalizeQuestionTypeForDb($arr['question_type'] ?? $arr['tags'] ?? 'answer');
  304. $difficulty = $difficultyOverride !== null
  305. ? max(0.0, min(0.9, round($difficultyOverride, 2)))
  306. : $this->defaultDifficultyForTemRow($arr);
  307. $rawSolution = (string) ($arr['solution'] ?? '');
  308. $solutionStored = AnswerSolutionStepMarkerInjector::enrichIfNeeded($rawSolution, $arr['question_type'] ?? $arr['tags'] ?? 'answer');
  309. $payload = [
  310. 'question_code' => 'QT'.strtoupper(Str::random(12)),
  311. 'question_type' => $questionType,
  312. 'kp_code' => $kp,
  313. 'stem' => $stem,
  314. 'options' => $options,
  315. 'answer' => (string) ($arr['answer'] ?? $arr['correct_answer'] ?? ''),
  316. 'solution' => $solutionStored,
  317. 'difficulty' => $difficulty,
  318. 'source' => 'questions_tem_review',
  319. 'tags' => is_string($arr['tags'] ?? null) ? $arr['tags'] : null,
  320. 'meta' => [
  321. 'imported_from' => 'questions_tem',
  322. 'questions_tem_id' => $temId,
  323. ],
  324. ];
  325. if (isset($arr['textbook_id'])) {
  326. $payload['textbook_id'] = (int) $arr['textbook_id'];
  327. }
  328. try {
  329. $question = Question::query()->create($payload);
  330. $updates = [];
  331. if (Schema::hasColumn('questions', 'audit_status')) {
  332. $updates['audit_status'] = 0;
  333. }
  334. if (Schema::hasColumn('questions', 'grade') && isset($arr['grade'])) {
  335. $updates['grade'] = (int) $arr['grade'];
  336. }
  337. if ($updates !== []) {
  338. DB::table('questions')->where('id', $question->id)->update($updates);
  339. }
  340. return [
  341. 'ok' => true,
  342. 'message' => '已入库',
  343. 'question_id' => (int) $question->id,
  344. ];
  345. } catch (\Throwable $e) {
  346. return [
  347. 'ok' => false,
  348. 'message' => '入库失败:'.$e->getMessage(),
  349. 'question_id' => null,
  350. ];
  351. }
  352. }
  353. /**
  354. * 批量将 questions_tem 行写入 questions(每行逻辑与 importTemRowToQuestions 相同)
  355. *
  356. * @param list<int> $temIds
  357. * @return array{imported: int, skipped: int, failed: int, lines: list<string>}
  358. */
  359. /**
  360. * @return array{imported: int, skipped: int, failed: int, lines: list<string>, imported_question_ids: list<int>}
  361. */
  362. public function importTemIdsToQuestions(array $temIds): array
  363. {
  364. $imported = 0;
  365. $skipped = 0;
  366. $failed = 0;
  367. $lines = [];
  368. $importedQuestionIds = [];
  369. foreach (array_unique(array_filter(array_map('intval', $temIds))) as $id) {
  370. if ($id <= 0) {
  371. continue;
  372. }
  373. $r = $this->importTemRowToQuestions($id);
  374. if ($r['ok']) {
  375. $imported++;
  376. if (! empty($r['question_id'])) {
  377. $importedQuestionIds[] = (int) $r['question_id'];
  378. }
  379. continue;
  380. }
  381. $msg = $r['message'];
  382. if (
  383. str_contains($msg, '正式库已存在')
  384. || str_contains($msg, '题干或知识点为空')
  385. ) {
  386. $skipped++;
  387. } else {
  388. $failed++;
  389. }
  390. if (count($lines) < 30) {
  391. $lines[] = "#{$id}: {$msg}";
  392. }
  393. }
  394. return [
  395. 'imported' => $imported,
  396. 'skipped' => $skipped,
  397. 'failed' => $failed,
  398. 'lines' => $lines,
  399. 'imported_question_ids' => $importedQuestionIds,
  400. ];
  401. }
  402. /**
  403. * 年级下拉项来自教材章节-知识点关联链路(非 questions_tem.grade)。
  404. *
  405. * @return list<int>
  406. */
  407. public function catalogGradeOptions(?int $semesterFilter = null): array
  408. {
  409. if (! Schema::hasTable('textbooks') || ! Schema::hasTable('textbook_catalog_nodes') || ! Schema::hasTable('textbook_chapter_knowledge_relation')) {
  410. return [];
  411. }
  412. $cacheKey = 'qtr:catalog-grades:' . ($semesterFilter ?? 'all');
  413. return Cache::remember($cacheKey, now()->addMinutes(10), function () use ($semesterFilter) {
  414. return DB::table('textbooks as t')
  415. ->join('textbook_catalog_nodes as n', 'n.textbook_id', '=', 't.id')
  416. ->join('textbook_chapter_knowledge_relation as r', 'r.catalog_chapter_id', '=', 'n.id')
  417. ->whereNotNull('t.grade')
  418. ->where('n.node_type', 'section')
  419. ->when($semesterFilter !== null, fn ($q) => $q->where('t.semester', $semesterFilter))
  420. ->when(Schema::hasColumn('r', 'is_deleted'), fn ($q) => $q->where(function ($x) {
  421. $x->where('r.is_deleted', 0)->orWhereNull('r.is_deleted');
  422. }))
  423. ->distinct()
  424. ->orderBy('t.grade')
  425. ->pluck('t.grade')
  426. ->map(fn ($g) => (int) $g)
  427. ->filter(fn ($g) => $g > 0)
  428. ->values()
  429. ->all();
  430. });
  431. }
  432. /**
  433. * 指定年级在教材章节关联中出现过的 kp_code(含子知识点扩展)。
  434. *
  435. * @return list<string>
  436. */
  437. /** @return list<int> */
  438. public function catalogSemesterOptions(?int $gradeFilter = null): array
  439. {
  440. if (! Schema::hasTable('textbooks') || ! Schema::hasTable('textbook_catalog_nodes') || ! Schema::hasTable('textbook_chapter_knowledge_relation')) {
  441. return [];
  442. }
  443. $cacheKey = 'qtr:catalog-semesters:' . ($gradeFilter ?? 'all');
  444. return Cache::remember($cacheKey, now()->addMinutes(10), function () use ($gradeFilter) {
  445. return DB::table('textbooks as t')
  446. ->join('textbook_catalog_nodes as n', 'n.textbook_id', '=', 't.id')
  447. ->join('textbook_chapter_knowledge_relation as r', 'r.catalog_chapter_id', '=', 'n.id')
  448. ->whereNotNull('t.semester')
  449. ->where('n.node_type', 'section')
  450. ->when($gradeFilter !== null, fn ($q) => $q->where('t.grade', $gradeFilter))
  451. ->when(Schema::hasColumn('r', 'is_deleted'), fn ($q) => $q->where(function ($x) {
  452. $x->where('r.is_deleted', 0)->orWhereNull('r.is_deleted');
  453. }))
  454. ->distinct()
  455. ->orderBy('t.semester')
  456. ->pluck('t.semester')
  457. ->map(fn ($s) => (int) $s)
  458. ->filter(fn ($s) => $s > 0)
  459. ->values()
  460. ->all();
  461. });
  462. }
  463. private function kpCodesFromCatalogFilter(?int $grade, ?int $semester): array
  464. {
  465. if (! Schema::hasTable('textbooks') || ! Schema::hasTable('textbook_catalog_nodes') || ! Schema::hasTable('textbook_chapter_knowledge_relation')) {
  466. return [];
  467. }
  468. $cacheKey = sprintf('qtr:catalog-kps:g%s:s%s', $grade ?? 'all', $semester ?? 'all');
  469. return Cache::remember($cacheKey, now()->addMinutes(10), function () use ($grade, $semester) {
  470. $q = DB::table('textbooks as t')
  471. ->join('textbook_catalog_nodes as n', 'n.textbook_id', '=', 't.id')
  472. ->join('textbook_chapter_knowledge_relation as r', 'r.catalog_chapter_id', '=', 'n.id')
  473. ->where('n.node_type', 'section')
  474. ->whereNotNull('r.kp_code')
  475. ->where('r.kp_code', '!=', '')
  476. ->when($grade !== null && $grade > 0, fn ($x) => $x->where('t.grade', $grade))
  477. ->when($semester !== null && $semester > 0, fn ($x) => $x->where('t.semester', $semester))
  478. ->when(Schema::hasColumn('r', 'is_deleted'), fn ($x) => $x->where(function ($w) {
  479. $w->where('r.is_deleted', 0)->orWhereNull('r.is_deleted');
  480. }));
  481. return $q->distinct()->pluck('r.kp_code')
  482. ->map(fn ($v) => trim((string) $v))
  483. ->filter(fn ($v) => $v !== '')
  484. ->values()
  485. ->all();
  486. });
  487. }
  488. private function normalizeQuestionTypeForDb(mixed $raw): string
  489. {
  490. $t = strtolower(trim((string) $raw));
  491. if (str_contains($t, 'choice') || str_contains($t, '选择')) {
  492. return 'choice';
  493. }
  494. if (str_contains($t, 'fill') || str_contains($t, 'blank') || str_contains($t, '填空')) {
  495. return 'fill';
  496. }
  497. return 'answer';
  498. }
  499. }