QuestionLocalService.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  1. <?php
  2. namespace App\Services;
  3. use App\Models\KnowledgePoint;
  4. use App\Models\Question;
  5. use App\Services\KnowledgeServiceApi;
  6. use Illuminate\Support\Collection;
  7. use Illuminate\Support\Facades\Cache;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Log;
  10. use Illuminate\Support\Str;
  11. class QuestionLocalService
  12. {
  13. public function listQuestions(int $page = 1, int $perPage = 50, array $filters = []): array
  14. {
  15. $query = $this->applyFilters(Question::query(), $filters);
  16. $paginator = $query->orderByDesc('id')->paginate($perPage, ['*'], 'page', $page);
  17. $data = $this->mapQuestions(collect($paginator->items()));
  18. return [
  19. 'data' => $data,
  20. 'meta' => [
  21. 'page' => $paginator->currentPage(),
  22. 'per_page' => $paginator->perPage(),
  23. 'total' => $paginator->total(),
  24. 'total_pages' => $paginator->lastPage(),
  25. ],
  26. ];
  27. }
  28. public function getQuestionById(int $id): ?array
  29. {
  30. $question = Question::find($id);
  31. if (!$question) {
  32. return null;
  33. }
  34. return $this->mapQuestion($question);
  35. }
  36. public function getQuestionByCode(string $questionCode): ?array
  37. {
  38. $question = Question::where('question_code', $questionCode)->first();
  39. if (!$question) {
  40. return null;
  41. }
  42. return $this->mapQuestion($question);
  43. }
  44. public function updateQuestionByCode(string $questionCode, array $payload): bool
  45. {
  46. $question = Question::where('question_code', $questionCode)->first();
  47. if (!$question) {
  48. return false;
  49. }
  50. $question->fill($this->normalizePayload($payload));
  51. $question->save();
  52. return true;
  53. }
  54. public function deleteQuestionByCode(string $questionCode): bool
  55. {
  56. $question = Question::where('question_code', $questionCode)->first();
  57. if (!$question) {
  58. return false;
  59. }
  60. $question->delete();
  61. return true;
  62. }
  63. public function deleteQuestionById(int $id): bool
  64. {
  65. $question = Question::find($id);
  66. if (!$question) {
  67. return false;
  68. }
  69. $question->delete();
  70. return true;
  71. }
  72. public function searchQuestions(string $query, int $limit = 20): array
  73. {
  74. $questions = Question::query()
  75. ->search($query)
  76. ->orderByDesc('id')
  77. ->limit($limit)
  78. ->get();
  79. return [
  80. 'data' => $this->mapQuestions($questions),
  81. ];
  82. }
  83. public function getQuestionsByIds(array $ids): array
  84. {
  85. if (empty($ids)) {
  86. return ['data' => []];
  87. }
  88. $questions = Question::query()
  89. ->whereIn('id', $ids)
  90. ->orderByDesc('id')
  91. ->get();
  92. return [
  93. 'data' => $this->mapQuestions($questions),
  94. ];
  95. }
  96. public function getQuestionsByKpCode(string $kpCode, int $limit = 100): array
  97. {
  98. $questions = Question::query()
  99. ->where('kp_code', $kpCode)
  100. ->orderByDesc('id')
  101. ->limit($limit)
  102. ->get();
  103. return [
  104. 'data' => $this->mapQuestions($questions),
  105. ];
  106. }
  107. public function getStatistics(array $filters = []): array
  108. {
  109. $baseQuery = $this->applyFilters(Question::query(), $filters);
  110. $total = (clone $baseQuery)->count();
  111. $byDifficulty = (clone $baseQuery)
  112. ->selectRaw('difficulty, COUNT(*) as total')
  113. ->groupBy('difficulty')
  114. ->pluck('total', 'difficulty')
  115. ->toArray();
  116. $byTypeRaw = (clone $baseQuery)
  117. ->selectRaw('question_type, COUNT(*) as total')
  118. ->groupBy('question_type')
  119. ->pluck('total', 'question_type')
  120. ->toArray();
  121. $byType = [];
  122. foreach ($byTypeRaw as $type => $count) {
  123. $label = $this->mapQuestionTypeLabel((string) $type);
  124. $byType[$label] = ($byType[$label] ?? 0) + $count;
  125. }
  126. $byKp = (clone $baseQuery)
  127. ->selectRaw('kp_code, COUNT(*) as total')
  128. ->groupBy('kp_code')
  129. ->pluck('total', 'kp_code')
  130. ->toArray();
  131. $bySource = (clone $baseQuery)
  132. ->selectRaw('source, COUNT(*) as total')
  133. ->groupBy('source')
  134. ->pluck('total', 'source')
  135. ->toArray();
  136. return [
  137. 'total' => $total,
  138. 'by_difficulty' => $byDifficulty,
  139. 'by_type' => $byType,
  140. 'by_kp' => $byKp,
  141. 'by_source' => $bySource,
  142. ];
  143. }
  144. public function generateQuestions(array $params): array
  145. {
  146. $kpCode = $params['kp_code'] ?? null;
  147. // 允许 kp_code 为空,此时从所有可用题目中选择
  148. if (!$kpCode) {
  149. // 从 params 中获取 kp_codes 数组
  150. $kpCodes = $params['kp_codes'] ?? [];
  151. if (is_string($kpCodes)) {
  152. $kpCodes = array_map('trim', explode(',', $kpCodes));
  153. }
  154. if (is_array($kpCodes) && !empty($kpCodes)) {
  155. $kpCode = $kpCodes[0]; // 使用第一个知识点
  156. } else {
  157. // 如果没有指定知识点,从数据库中随机选择一个可用的知识点
  158. $availableKp = Question::query()
  159. ->whereNotNull('kp_code')
  160. ->where('kp_code', '!=', '')
  161. ->distinct()
  162. ->pluck('kp_code')
  163. ->first();
  164. if ($availableKp) {
  165. $kpCode = $availableKp;
  166. } else {
  167. return [
  168. 'success' => false,
  169. 'message' => '系统中没有可用的题目,请先添加题目数据',
  170. ];
  171. }
  172. }
  173. }
  174. $count = max(1, (int) ($params['count'] ?? 1));
  175. $keyword = (string) ($params['keyword'] ?? '');
  176. $type = $params['type'] ?? null;
  177. $difficulty = $params['difficulty'] ?? null;
  178. $skills = $params['skills'] ?? [];
  179. $solutionService = app(AiSolutionService::class);
  180. $created = [];
  181. DB::transaction(function () use (
  182. $count,
  183. $kpCode,
  184. $keyword,
  185. $type,
  186. $difficulty,
  187. $skills,
  188. $solutionService,
  189. &$created,
  190. $params
  191. ) {
  192. for ($i = 1; $i <= $count; $i++) {
  193. $questionCode = $this->generateQuestionCode();
  194. $stemSuffix = $keyword ? "({$keyword})" : '';
  195. $stem = "【AI生成】{$kpCode} 题目 {$i}{$stemSuffix}";
  196. $options = null;
  197. $answer = null;
  198. $questionType = $type ?? 'CALCULATION';
  199. if (in_array($questionType, ['CHOICE', 'MULTIPLE_CHOICE'], true)) {
  200. $options = [
  201. ['label' => 'A', 'text' => '选项 A'],
  202. ['label' => 'B', 'text' => '选项 B'],
  203. ['label' => 'C', 'text' => '选项 C'],
  204. ['label' => 'D', 'text' => '选项 D'],
  205. ];
  206. $answer = 'A';
  207. }
  208. $solution = $solutionService->generateSolution($stem, [
  209. 'kp_code' => $kpCode,
  210. 'difficulty' => $difficulty,
  211. 'question_type' => $questionType,
  212. ]);
  213. $question = Question::create([
  214. 'question_code' => $questionCode,
  215. 'kp_code' => $kpCode,
  216. 'stem' => $stem,
  217. 'options' => $options,
  218. 'answer' => $answer,
  219. 'solution' => $solution['solution'] ?? null,
  220. 'difficulty' => $difficulty,
  221. 'source' => 'ai::local',
  222. 'question_type' => $questionType,
  223. 'meta' => [
  224. 'skills' => $skills,
  225. 'prompt_template' => $params['prompt_template'] ?? null,
  226. 'strategy' => $params['strategy'] ?? null,
  227. 'generated_at' => now()->toDateTimeString(),
  228. 'solution_steps' => $solution['steps'] ?? [],
  229. ],
  230. ]);
  231. $created[] = $this->mapQuestion($question);
  232. }
  233. });
  234. return [
  235. 'success' => true,
  236. 'message' => '生成完成',
  237. 'count' => count($created),
  238. 'data' => $created,
  239. ];
  240. }
  241. public function importQuestions(array $questions): array
  242. {
  243. if (empty($questions)) {
  244. return [
  245. 'success' => false,
  246. 'message' => '题目为空',
  247. 'count' => 0,
  248. ];
  249. }
  250. $created = 0;
  251. DB::transaction(function () use ($questions, &$created) {
  252. foreach ($questions as $payload) {
  253. $questionCode = $payload['question_code'] ?? $this->generateQuestionCode();
  254. $question = Question::firstOrNew(['question_code' => $questionCode]);
  255. $question->fill($this->normalizePayload($payload));
  256. $question->save();
  257. $created++;
  258. }
  259. });
  260. return [
  261. 'success' => true,
  262. 'message' => '导入完成',
  263. 'count' => $created,
  264. ];
  265. }
  266. public function selectQuestionsForExam(int $totalQuestions, array $filters): array
  267. {
  268. $query = Question::query();
  269. // 【新增】只获取审核通过的题目(audit_status = 0 表示合格)
  270. $query->where('audit_status', 0);
  271. if (!empty($filters['kp_codes'])) {
  272. $query->whereIn('kp_code', $filters['kp_codes']);
  273. }
  274. if (!empty($filters['skills'])) {
  275. $skills = array_values(array_filter($filters['skills']));
  276. if (!empty($skills)) {
  277. $query->where(function ($q) use ($skills) {
  278. foreach ($skills as $skill) {
  279. $q->orWhereJsonContains('meta->skills', $skill);
  280. }
  281. });
  282. }
  283. }
  284. $questions = $query->get();
  285. $selected = $this->applyRatioSelection($questions, $totalQuestions, $filters);
  286. return $this->mapQuestions(collect($selected));
  287. }
  288. public function getKnowledgePointOptions(): array
  289. {
  290. return KnowledgePoint::query()
  291. ->orderBy('kp_code')
  292. ->pluck('name', 'kp_code')
  293. ->toArray();
  294. }
  295. public function getSkillNameMapping(?string $kpCode = null): array
  296. {
  297. return [];
  298. }
  299. private function applyFilters($query, array $filters)
  300. {
  301. if (!empty($filters['kp_code'])) {
  302. $query->where('kp_code', $filters['kp_code']);
  303. }
  304. if (!empty($filters['difficulty'])) {
  305. $query->where('difficulty', $filters['difficulty']);
  306. }
  307. if (!empty($filters['type'])) {
  308. $query->where('question_type', $filters['type']);
  309. }
  310. if (!empty($filters['search'])) {
  311. $query->search($filters['search']);
  312. }
  313. return $query;
  314. }
  315. private function normalizePayload(array $payload): array
  316. {
  317. $normalized = [
  318. 'question_code' => $payload['question_code'] ?? null,
  319. 'kp_code' => $payload['kp_code'] ?? null,
  320. 'stem' => $payload['stem'] ?? ($payload['content'] ?? ''),
  321. 'options' => $payload['options'] ?? null,
  322. 'answer' => $payload['answer'] ?? null,
  323. 'solution' => $payload['solution'] ?? null,
  324. 'difficulty' => $payload['difficulty'] ?? null,
  325. 'source' => $payload['source'] ?? null,
  326. 'tags' => $payload['tags'] ?? null,
  327. 'question_type' => $payload['question_type'] ?? ($payload['type'] ?? null),
  328. 'meta' => $payload['meta'] ?? null,
  329. ];
  330. if (isset($payload['skills'])) {
  331. $meta = $normalized['meta'] ?? [];
  332. $meta['skills'] = is_array($payload['skills'])
  333. ? $payload['skills']
  334. : array_filter(array_map('trim', explode(',', (string) $payload['skills'])));
  335. $normalized['meta'] = $meta;
  336. }
  337. return array_filter($normalized, static fn ($value) => $value !== null);
  338. }
  339. private function mapQuestions(Collection $questions): array
  340. {
  341. $kpCodes = $questions->pluck('kp_code')->filter()->unique()->values();
  342. $kpMap = $this->resolveKnowledgePointNames($kpCodes->all());
  343. return $questions->map(function (Question $question) use ($kpMap) {
  344. $data = $this->mapQuestion($question);
  345. $data['kp_name'] = $kpMap[$question->kp_code] ?? null;
  346. return $data;
  347. })->values()->all();
  348. }
  349. private function mapQuestion(Question $question): array
  350. {
  351. $meta = $question->meta ?? [];
  352. $data = [
  353. 'id' => $question->id,
  354. 'question_code' => $question->question_code,
  355. 'kp_code' => $question->kp_code,
  356. 'stem' => $question->stem,
  357. 'options' => $question->options,
  358. 'answer' => $question->answer,
  359. 'solution' => $question->solution,
  360. 'difficulty' => $question->difficulty,
  361. 'source' => $question->source,
  362. 'tags' => $question->tags,
  363. 'type' => $question->question_type,
  364. 'question_type' => $question->question_type,
  365. 'skills' => $meta['skills'] ?? [],
  366. 'meta' => $meta,
  367. 'created_at' => $question->created_at?->toDateTimeString(),
  368. 'updated_at' => $question->updated_at?->toDateTimeString(),
  369. ];
  370. return MathFormulaProcessor::processQuestionData($data);
  371. }
  372. private function generateQuestionCode(): string
  373. {
  374. return 'Q' . Str::upper(Str::random(10));
  375. }
  376. private function mapQuestionTypeLabel(string $type): string
  377. {
  378. return match (strtoupper($type)) {
  379. 'CHOICE' => '选择题',
  380. 'MULTIPLE_CHOICE' => '多选题',
  381. 'FILL_IN_THE_BLANK', 'FILL' => '填空题',
  382. 'CALCULATION', 'WORD_PROBLEM', 'ANSWER' => '解答题',
  383. 'PROOF' => '证明题',
  384. default => '其他',
  385. };
  386. }
  387. private function applyRatioSelection(Collection $questions, int $totalQuestions, array $filters): array
  388. {
  389. $questionsByType = $questions->groupBy(fn (Question $q) => $q->question_type ?? 'CALCULATION');
  390. $questionsByDifficulty = $questions->groupBy(function (Question $q) {
  391. $difficulty = (float) ($q->difficulty ?? 0);
  392. if ($difficulty <= 0.4) {
  393. return 'easy';
  394. }
  395. if ($difficulty <= 0.7) {
  396. return 'medium';
  397. }
  398. return 'hard';
  399. });
  400. $typeRatio = $filters['question_type_ratio'] ?? [];
  401. $difficultyRatio = $filters['difficulty_ratio'] ?? [];
  402. $selected = collect();
  403. if (!empty($typeRatio)) {
  404. foreach ($typeRatio as $type => $ratio) {
  405. $bucket = $questionsByType->get($type, collect());
  406. $count = (int) round($totalQuestions * (float) $ratio);
  407. $selected = $selected->merge($bucket->shuffle()->take($count));
  408. }
  409. }
  410. if (!empty($difficultyRatio)) {
  411. foreach ($difficultyRatio as $key => $ratio) {
  412. $bucketKey = $this->normalizeDifficultyKey($key);
  413. $bucket = $questionsByDifficulty->get($bucketKey, collect());
  414. $count = (int) round($totalQuestions * (float) $ratio);
  415. $selected = $selected->merge($bucket->shuffle()->take($count));
  416. }
  417. }
  418. if ($selected->isEmpty()) {
  419. return $questions->shuffle()->take($totalQuestions)->values()->all();
  420. }
  421. if ($selected->count() < $totalQuestions) {
  422. $missing = $totalQuestions - $selected->count();
  423. $fill = $questions->diff($selected)->shuffle()->take($missing);
  424. $selected = $selected->merge($fill);
  425. }
  426. return $selected->values()->all();
  427. }
  428. private function normalizeDifficultyKey(string $key): string
  429. {
  430. if (in_array($key, ['easy', 'medium', 'hard'], true)) {
  431. return $key;
  432. }
  433. $value = (float) $key;
  434. if ($value <= 0.4) {
  435. return 'easy';
  436. }
  437. if ($value <= 0.7) {
  438. return 'medium';
  439. }
  440. return 'hard';
  441. }
  442. private function resolveKnowledgePointNames(array $kpCodes): array
  443. {
  444. $kpCodes = array_values(array_filter(array_unique($kpCodes)));
  445. if (empty($kpCodes)) {
  446. return [];
  447. }
  448. $cacheKey = 'kp-name-map-' . md5(implode('|', $kpCodes));
  449. return Cache::remember($cacheKey, now()->addMinutes(30), function () use ($kpCodes) {
  450. $kpMap = KnowledgePoint::query()
  451. ->whereIn('kp_code', $kpCodes)
  452. ->pluck('name', 'kp_code')
  453. ->toArray();
  454. $missing = array_values(array_filter($kpCodes, fn ($code) => empty($kpMap[$code])));
  455. if (empty($missing)) {
  456. return $kpMap;
  457. }
  458. try {
  459. $api = app(KnowledgeServiceApi::class);
  460. $all = $api->listKnowledgePoints();
  461. foreach ($all as $kp) {
  462. $code = $kp['kp_code'] ?? null;
  463. $name = $kp['cn_name'] ?? $kp['name'] ?? null;
  464. if ($code && $name && in_array($code, $missing, true)) {
  465. $kpMap[$code] = $name;
  466. }
  467. }
  468. } catch (\Throwable $e) {
  469. // Fallback: keep existing mapping
  470. }
  471. return $kpMap;
  472. });
  473. }
  474. /**
  475. * 根据难度系数分布选择题目
  476. *
  477. * @param array $questions 候选题目数组
  478. * @param int $totalQuestions 总题目数
  479. * @param int $difficultyCategory 难度类别 (1-4)
  480. * - 1: 0-0.25范围占50%,其他占50%
  481. * - 2: 0.25-0.5范围占50%,<0.25占25%,>0.5占25%
  482. * - 3: 0.5-0.75范围占50%,<0.5占25%,>0.75占25%
  483. * - 4: 0.75-1范围占50%,其他占50%
  484. * @param array $filters 其他筛选条件
  485. * @return array 分布后的题目
  486. */
  487. public function selectQuestionsByDifficultyDistribution(array $questions, int $totalQuestions, int $difficultyCategory = 1, array $filters = []): array
  488. {
  489. Log::info('QuestionLocalService: 根据难度系数分布选择题目', [
  490. 'total_questions' => $totalQuestions,
  491. 'difficulty_category' => $difficultyCategory,
  492. 'input_questions' => count($questions)
  493. ]);
  494. if (empty($questions)) {
  495. Log::warning('QuestionLocalService: 输入题目为空');
  496. return [];
  497. }
  498. // 【恢复】简化逻辑,避免复杂处理
  499. $distribution = $this->calculateDifficultyDistribution($difficultyCategory, $totalQuestions);
  500. Log::info('QuestionLocalService: 难度分布计算', [
  501. 'distribution' => $distribution,
  502. 'target_count' => $totalQuestions
  503. ]);
  504. // 按难度范围分桶
  505. $buckets = $this->groupQuestionsByDifficultyRange($questions, $difficultyCategory);
  506. Log::info('QuestionLocalService: 题目分桶', [
  507. 'buckets' => array_map(fn($bucket) => count($bucket), $buckets),
  508. 'total_input' => count($questions)
  509. ]);
  510. // 根据分布选择题目
  511. $selected = [];
  512. $usedIds = [];
  513. foreach ($distribution as $level => $config) {
  514. $targetCount = $config['count'];
  515. if ($targetCount <= 0) {
  516. Log::debug('QuestionLocalService: 跳过难度层级', [
  517. 'level' => $level,
  518. 'target_count' => $targetCount
  519. ]);
  520. continue;
  521. }
  522. $rangeKey = $this->mapDifficultyLevelToRangeKey($level, $difficultyCategory);
  523. $bucket = $buckets[$rangeKey] ?? [];
  524. Log::debug('QuestionLocalService: 处理难度层级', [
  525. 'level' => $level,
  526. 'range_key' => $rangeKey,
  527. 'target_count' => $targetCount,
  528. 'bucket_size' => count($bucket)
  529. ]);
  530. // 随机打乱
  531. shuffle($bucket);
  532. // 选择题目
  533. $taken = 0;
  534. foreach ($bucket as $question) {
  535. if ($taken >= $targetCount) break;
  536. $questionId = $question['id'] ?? null;
  537. if ($questionId && !in_array($questionId, $usedIds)) {
  538. $selected[] = $question;
  539. $usedIds[] = $questionId;
  540. $taken++;
  541. }
  542. }
  543. // 【修复】如果某个难度范围题目不足,记录日志但不截断
  544. if ($taken < $targetCount) {
  545. Log::warning('QuestionLocalService: 难度范围题目不足,允许后续补充', [
  546. 'level' => $level,
  547. 'range_key' => $rangeKey,
  548. 'target' => $targetCount,
  549. 'actual' => $taken,
  550. 'bucket_size' => count($bucket),
  551. 'note' => '将在后续步骤中从其他范围补充题目'
  552. ]);
  553. } else {
  554. Log::debug('QuestionLocalService: 难度层级选择', [
  555. 'level' => $level,
  556. 'target' => $targetCount,
  557. 'actual' => $taken,
  558. 'bucket_size' => count($bucket),
  559. 'range_key' => $rangeKey
  560. ]);
  561. }
  562. }
  563. Log::info('QuestionLocalService: 分布选择后统计', [
  564. 'selected_count' => count($selected),
  565. 'target_count' => $totalQuestions,
  566. 'need_more' => max(0, $totalQuestions - count($selected))
  567. ]);
  568. // 如果数量不足,从剩余题目中补充
  569. if (count($selected) < $totalQuestions) {
  570. Log::warning('QuestionLocalService: 开始补充题目(难度分布无法满足要求)', [
  571. 'need_more' => $totalQuestions - count($selected),
  572. 'selected_count' => count($selected),
  573. 'difficulty_category' => $difficultyCategory,
  574. 'note' => '由于难度分布限制,将从所有剩余题目中补充'
  575. ]);
  576. $remaining = [];
  577. foreach ($questions as $q) {
  578. $id = $q['id'] ?? null;
  579. if ($id && !in_array($id, $usedIds)) {
  580. $remaining[] = $q;
  581. }
  582. }
  583. Log::info('QuestionLocalService: 剩余题目统计', [
  584. 'remaining_count' => count($remaining),
  585. 'need_more' => $totalQuestions - count($selected)
  586. ]);
  587. // 【修复】打乱剩余题目顺序,确保随机性
  588. shuffle($remaining);
  589. $needMore = $totalQuestions - count($selected);
  590. $supplementCount = min($needMore, count($remaining));
  591. $selected = array_merge($selected, array_slice($remaining, 0, $supplementCount));
  592. Log::warning('QuestionLocalService: 补充完成', [
  593. 'supplement_added' => $supplementCount,
  594. 'final_count_before_truncate' => count($selected),
  595. 'remaining_unused' => count($remaining) - $supplementCount
  596. ]);
  597. }
  598. // 截断至目标数量
  599. $selected = array_slice($selected, 0, $totalQuestions);
  600. Log::info('QuestionLocalService: 难度分布选择完成', [
  601. 'final_count' => count($selected),
  602. 'target_count' => $totalQuestions,
  603. 'success' => count($selected) === $totalQuestions,
  604. 'input_count' => count($questions),
  605. 'distribution_applied' => true
  606. ]);
  607. return $selected;
  608. }
  609. /**
  610. * 计算难度分布配置
  611. *
  612. * @param int $category 难度类别 (1-4)
  613. * @param int $totalQuestions 总题目数
  614. * @return array 分布配置
  615. */
  616. private function calculateDifficultyDistribution(int $category, int $totalQuestions): array
  617. {
  618. // 标准化:25% 低级,50% 基准,25% 拔高
  619. // $lowPercentage = 25;
  620. // $mediumPercentage = 50;
  621. // $highPercentage = 25;
  622. // 根据难度类别调整分布
  623. switch ($category) {
  624. case 1:
  625. // 基础型:0-0.25占50%,其他占50%
  626. $mediumPercentage = 90; // 0-0.25作为基准
  627. $lowPercentage = 0; // 其他低难度
  628. $highPercentage = 10; // 其他高难度
  629. break;
  630. case 2:
  631. // 进阶型:0.25-0.5占50%,<0.25占25%,>0.5占25%
  632. $mediumPercentage = 50; // 0.25-0.5作为基准
  633. $lowPercentage = 25; // <0.25
  634. $highPercentage = 25; // >0.5
  635. break;
  636. case 3:
  637. // 中等型:0.5-0.75占50%,<0.5占25%,>0.75占25%
  638. $mediumPercentage = 50; // 0.5-0.75作为基准
  639. $lowPercentage = 25; // <0.5
  640. $highPercentage = 25; // >0.75
  641. break;
  642. case 4:
  643. // 拔高型:0.75-1占50%,其他占50%
  644. $mediumPercentage = 50; // 0.75-1作为基准
  645. $lowPercentage = 25; // 其他低难度
  646. $highPercentage = 25; // 其他高难度
  647. break;
  648. default:
  649. $lowPercentage = 25;
  650. $mediumPercentage = 50;
  651. $highPercentage = 25;
  652. }
  653. // 计算题目数量
  654. $lowCount = (int) round($totalQuestions * $lowPercentage / 100);
  655. $mediumCount = (int) round($totalQuestions * $mediumPercentage / 100);
  656. $highCount = $totalQuestions - $lowCount - $mediumCount;
  657. return [
  658. 'low' => [
  659. 'percentage' => $lowPercentage,
  660. 'count' => $lowCount,
  661. 'label' => '低级难度'
  662. ],
  663. 'medium' => [
  664. 'percentage' => $mediumPercentage,
  665. 'count' => $mediumCount,
  666. 'label' => '基准难度'
  667. ],
  668. 'high' => [
  669. 'percentage' => $highPercentage,
  670. 'count' => $highCount,
  671. 'label' => '拔高难度'
  672. ]
  673. ];
  674. }
  675. /**
  676. * 将题目按难度范围分桶
  677. *
  678. * @param array $questions 题目数组
  679. * @param int $category 难度类别
  680. * @return array 分桶结果
  681. */
  682. private function groupQuestionsByDifficultyRange(array $questions, int $category): array
  683. {
  684. $buckets = [
  685. 'primary_low' => [], // 主要低难度范围
  686. 'primary_medium' => [], // 主要中等难度范围
  687. 'primary_high' => [], // 主要高难度范围
  688. 'secondary' => [], // 次要范围
  689. 'other' => [] // 其他
  690. ];
  691. foreach ($questions as $question) {
  692. $difficulty = (float) ($question['difficulty'] ?? 0);
  693. $rangeKey = $this->classifyQuestionByDifficulty($difficulty, $category);
  694. $buckets[$rangeKey][] = $question;
  695. }
  696. return $buckets;
  697. }
  698. /**
  699. * 根据难度值和类别分类题目
  700. *
  701. * @param float $difficulty 难度值 (0-1)
  702. * @param int $category 难度类别 (1-4)
  703. * @return string 范围键
  704. */
  705. private function classifyQuestionByDifficulty(float $difficulty, int $category): string
  706. {
  707. switch ($category) {
  708. case 1:
  709. // 基础型:0-0.25作为主要中等,0.25-1作为其他
  710. if ($difficulty >= 0 && $difficulty <= 0.25) {
  711. return 'primary_medium';
  712. }
  713. return 'other';
  714. case 2:
  715. // 进阶型:0.25-0.5作为主要中等,<0.25作为主要低,>0.5作为主要高
  716. if ($difficulty >= 0.25 && $difficulty <= 0.5) {
  717. return 'primary_medium';
  718. } elseif ($difficulty < 0.25) {
  719. return 'primary_low';
  720. }
  721. return 'primary_high';
  722. case 3:
  723. // 中等型:0.5-0.75作为主要中等,<0.5作为主要低,>0.75作为主要高
  724. if ($difficulty >= 0.5 && $difficulty <= 0.75) {
  725. return 'primary_medium';
  726. } elseif ($difficulty < 0.5) {
  727. return 'primary_low';
  728. }
  729. return 'primary_high';
  730. case 4:
  731. // 拔高型:0.75-1作为主要中等,0-0.75作为其他
  732. if ($difficulty >= 0.75 && $difficulty <= 1.0) {
  733. return 'primary_medium';
  734. }
  735. return 'other';
  736. default:
  737. return 'other';
  738. }
  739. }
  740. /**
  741. * 将难度层级映射到范围键
  742. *
  743. * @param string $level 难度层级 (low/medium/high)
  744. * @param int $category 难度类别
  745. * @return string 范围键
  746. */
  747. private function mapDifficultyLevelToRangeKey(string $level, int $category): string
  748. {
  749. // 【修复】难度分布映射逻辑:确保 'other' 桶中的题目能被正确选择
  750. // 对于 difficulty_category=1,'other' 桶中的题目应该映射到 'secondary' 桶
  751. switch ($category) {
  752. case 1:
  753. return match($level) {
  754. 'low' => 'secondary', // 修复:映射到 secondary 桶,而非 other
  755. 'medium' => 'primary_medium',
  756. 'high' => 'secondary', // 修复:映射到 secondary 桶,而非 other
  757. default => 'secondary'
  758. };
  759. case 2:
  760. return match($level) {
  761. 'low' => 'primary_low',
  762. 'medium' => 'primary_medium',
  763. 'high' => 'primary_high',
  764. default => 'other'
  765. };
  766. case 3:
  767. return match($level) {
  768. 'low' => 'primary_low',
  769. 'medium' => 'primary_medium',
  770. 'high' => 'primary_high',
  771. default => 'other'
  772. };
  773. case 4:
  774. return match($level) {
  775. 'low' => 'secondary', // 修复:映射到 secondary 桶,而非 other
  776. 'medium' => 'primary_medium',
  777. 'high' => 'secondary', // 修复:映射到 secondary 桶,而非 other
  778. default => 'secondary'
  779. };
  780. default:
  781. return 'other';
  782. }
  783. }
  784. }