api.php 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576
  1. <?php
  2. use App\Http\Controllers\Api\AbilityEvaluateController;
  3. use App\Http\Controllers\Api\ExamAnalysisApiController;
  4. use App\Http\Controllers\Api\IntelligentExamController;
  5. use App\Http\Controllers\Api\KnowledgeRecommendController;
  6. use App\Http\Controllers\Api\MistakeBookController;
  7. use App\Http\Controllers\Api\PaperAssembleController;
  8. use App\Http\Controllers\Api\PaperJsonController;
  9. use App\Http\Controllers\Api\PreQuestionApiController;
  10. use App\Http\Controllers\Api\QuestionRandomController;
  11. use App\Http\Controllers\Api\QuestionSearchController;
  12. use App\Http\Controllers\Api\QuestionSolutionController;
  13. use App\Http\Controllers\Api\StudentAnswerAnalysisController;
  14. use App\Http\Controllers\Api\StudentKnowledgeController;
  15. use App\Http\Controllers\Api\TextbookApiController;
  16. use App\Services\QuestionServiceApi;
  17. use Illuminate\Auth\Middleware\Authenticate;
  18. use Illuminate\Support\Facades\Log;
  19. use Illuminate\Support\Facades\Route;
  20. /*
  21. |--------------------------------------------------------------------------
  22. | 题库管理 API 路由
  23. |--------------------------------------------------------------------------
  24. */
  25. // 给 Python/内部服务消费的“筛选题库”API(需要 X-Internal-Token)
  26. Route::middleware('internal.token')->get('/pre-questions', [PreQuestionApiController::class, 'index'])
  27. ->name('api.pre-questions.index');
  28. Route::get('/questions/search', QuestionSearchController::class)->name('api.questions.search');
  29. Route::get('/questions/random', QuestionRandomController::class)->name('api.questions.random');
  30. Route::post('/papers/assemble', PaperAssembleController::class)->name('api.papers.assemble');
  31. Route::get('/papers/{paperId}/json', [PaperJsonController::class, 'show'])->name('api.papers.json');
  32. Route::get('/questions/{id}/solution', QuestionSolutionController::class)->name('api.questions.solution');
  33. Route::get('/knowledge/recommend', KnowledgeRecommendController::class)->name('api.knowledge.recommend');
  34. Route::post('/abilities/evaluate', AbilityEvaluateController::class)->name('api.abilities.evaluate');
  35. // 接收题目生成回调
  36. Route::post('/questions/callback', function () {
  37. try {
  38. $data = request()->all();
  39. Log::info('Received question generation callback', $data);
  40. // 验证回调数据
  41. if (! isset($data['task_id']) || ! isset($data['status'])) {
  42. return response()->json(['error' => 'Invalid callback data'], 400);
  43. }
  44. // 处理回调数据并存储通知到session
  45. if ($data['status'] === 'completed') {
  46. $result = $data['result'] ?? [];
  47. $total = $result['total'] ?? $data['total'] ?? ($result['saved'] ?? 0);
  48. $kpCode = $result['kp_code'] ?? $data['kp_code'] ?? '';
  49. // 将成功通知存储到session,供下次页面刷新时显示
  50. session()->flash('notification', [
  51. 'type' => 'success',
  52. 'title' => '✅ 题目生成完成',
  53. 'body' => "任务 ID: {$data['task_id']}\n生成题目: {$total} 道".($kpCode ? "\n知识点: {$kpCode}" : ''),
  54. 'color' => 'success',
  55. ]);
  56. Log::info('题目生成成功通知已存储', [
  57. 'task_id' => $data['task_id'],
  58. 'total' => $total,
  59. 'kp_code' => $kpCode,
  60. ]);
  61. } elseif ($data['status'] === 'failed') {
  62. $error = $data['error'] ?? '未知错误';
  63. // 将失败通知存储到session
  64. session()->flash('notification', [
  65. 'type' => 'error',
  66. 'title' => '❌ 题目生成失败',
  67. 'body' => "任务 ID: {$data['task_id']}\n错误: {$error}",
  68. 'color' => 'danger',
  69. ]);
  70. Log::error('题目生成失败通知已存储', [
  71. 'task_id' => $data['task_id'],
  72. 'error' => $error,
  73. ]);
  74. }
  75. return response()->json([
  76. 'success' => true,
  77. 'message' => 'Callback received and notification stored',
  78. 'status' => $data['status'],
  79. ]);
  80. } catch (\Exception $e) {
  81. Log::error('Callback processing failed: '.$e->getMessage());
  82. return response()->json(['error' => $e->getMessage()], 500);
  83. }
  84. })->name('api.questions.callback');
  85. // 接收OCR题目生成回调
  86. Route::post('/ocr-question-callback', function () {
  87. try {
  88. $data = request()->all();
  89. Log::info('Received OCR question generation callback', $data);
  90. // 验证必要的回调数据
  91. if (! isset($data['task_id']) || ! isset($data['status']) || ! isset($data['ocr_record_id'])) {
  92. Log::error('OCR callback missing required fields', $data);
  93. return response()->json([
  94. 'success' => false,
  95. 'error' => 'Missing required fields: task_id, status, ocr_record_id',
  96. ], 400);
  97. }
  98. $taskId = $data['task_id'];
  99. $ocrRecordId = $data['ocr_record_id'];
  100. $status = $data['status'];
  101. // 将回调结果存储到缓存中,供前端查询(保留30秒)
  102. $cacheKey = "ocr_callback_{$ocrRecordId}_{$taskId}";
  103. cache([$cacheKey => $data], now()->addSeconds(30));
  104. Log::info("OCR callback cached with key: {$cacheKey}", [
  105. 'ocr_record_id' => $ocrRecordId,
  106. 'task_id' => $taskId,
  107. 'status' => $status,
  108. 'total_generated' => $data['result']['total_generated'] ?? 0,
  109. 'total_saved' => $data['result']['total_saved'] ?? 0,
  110. ]);
  111. // 处理题目关联逻辑
  112. if ($status === 'completed') {
  113. $updatedCount = 0;
  114. // 从result中提取question_mappings(QuestionBank API将它放在result字段中)
  115. $mappings = $data['result']['question_mappings'] ?? $data['question_mappings'] ?? [];
  116. Log::info('Processing OCR question associations', [
  117. 'ocr_record_id' => $ocrRecordId,
  118. 'task_id' => $taskId,
  119. 'mappings_count' => count($mappings),
  120. ]);
  121. // 更新ocr_question_results表中的关联关系
  122. foreach ($mappings as $mapping) {
  123. try {
  124. $ocrQuestionNumber = $mapping['ocr_question_number'] ?? null;
  125. $questionBankId = $mapping['question_bank_id'] ?? null;
  126. $questionCode = $mapping['question_code'] ?? null;
  127. if ($ocrQuestionNumber && $questionBankId) {
  128. // 查找对应的OCR题目结果并更新
  129. $updated = DB::table('ocr_question_results')
  130. ->where('ocr_record_id', $ocrRecordId)
  131. ->where('question_number', $ocrQuestionNumber)
  132. ->update([
  133. 'question_bank_id' => $questionBankId,
  134. 'generation_status' => 'completed',
  135. 'generation_task_id' => $taskId,
  136. 'generation_error' => null,
  137. ]);
  138. if ($updated) {
  139. $updatedCount++;
  140. Log::info('Updated OCR question association', [
  141. 'ocr_record_id' => $ocrRecordId,
  142. 'question_number' => $ocrQuestionNumber,
  143. 'question_bank_id' => $questionBankId,
  144. 'question_code' => $questionCode,
  145. ]);
  146. } else {
  147. Log::warning('No OCR question result found for association', [
  148. 'ocr_record_id' => $ocrRecordId,
  149. 'question_number' => $ocrQuestionNumber,
  150. ]);
  151. }
  152. }
  153. } catch (\Exception $e) {
  154. Log::error('Failed to update OCR question association', [
  155. 'mapping' => $mapping,
  156. 'error' => $e->getMessage(),
  157. ]);
  158. }
  159. }
  160. Log::info('OCR question association completed', [
  161. 'ocr_record_id' => $ocrRecordId,
  162. 'task_id' => $taskId,
  163. 'total_mappings' => count($mappings),
  164. 'updated_count' => $updatedCount,
  165. ]);
  166. // 更新OCR记录的整体状态为已完成
  167. try {
  168. DB::table('ocr_records')
  169. ->where('id', $ocrRecordId)
  170. ->update([
  171. 'status' => 'completed',
  172. 'processed_at' => now(),
  173. 'updated_at' => now(),
  174. ]);
  175. Log::info('Updated OCR record status to completed', [
  176. 'ocr_record_id' => $ocrRecordId,
  177. 'task_id' => $taskId,
  178. ]);
  179. } catch (\Exception $e) {
  180. Log::error('Failed to update OCR record status', [
  181. 'ocr_record_id' => $ocrRecordId,
  182. 'error' => $e->getMessage(),
  183. ]);
  184. }
  185. } elseif ($status === 'failed') {
  186. // 更新所有相关的OCR题目结果为失败状态
  187. try {
  188. $updated = DB::table('ocr_question_results')
  189. ->where('ocr_record_id', $ocrRecordId)
  190. ->where('generation_status', 'pending') // 只更新待处理的
  191. ->update([
  192. 'generation_status' => 'failed',
  193. 'generation_task_id' => $taskId,
  194. 'generation_error' => $data['error'] ?? 'Unknown error',
  195. ]);
  196. Log::info('Updated OCR questions to failed status', [
  197. 'ocr_record_id' => $ocrRecordId,
  198. 'task_id' => $taskId,
  199. 'updated_count' => $updated,
  200. 'error' => $data['error'] ?? 'Unknown error',
  201. ]);
  202. // 更新OCR记录的状态为失败
  203. DB::table('ocr_records')
  204. ->where('id', $ocrRecordId)
  205. ->update([
  206. 'status' => 'failed',
  207. 'error_message' => $data['error'] ?? 'Question generation failed',
  208. 'updated_at' => now(),
  209. ]);
  210. Log::info('Updated OCR record status to failed', [
  211. 'ocr_record_id' => $ocrRecordId,
  212. 'task_id' => $taskId,
  213. 'error' => $data['error'] ?? 'Unknown error',
  214. ]);
  215. } catch (\Exception $e) {
  216. Log::error('Failed to update OCR questions to failed status', [
  217. 'ocr_record_id' => $ocrRecordId,
  218. 'error' => $e->getMessage(),
  219. ]);
  220. }
  221. }
  222. return response()->json([
  223. 'success' => true,
  224. 'message' => 'OCR callback received and processed',
  225. 'data' => [
  226. 'task_id' => $taskId,
  227. 'ocr_record_id' => $ocrRecordId,
  228. 'status' => $status,
  229. 'cache_key' => $cacheKey,
  230. 'associations_processed' => $status === 'completed' ? count($data['question_mappings'] ?? []) : 0,
  231. ],
  232. ]);
  233. } catch (\Exception $e) {
  234. Log::error('OCR callback processing failed: '.$e->getMessage());
  235. Log::error('Exception details: '.$e->getTraceAsString());
  236. return response()->json([
  237. 'success' => false,
  238. 'error' => 'Callback processing failed: '.$e->getMessage(),
  239. ], 500);
  240. }
  241. })->name('api.ocr.callback');
  242. // 获取题目生成回调结果
  243. Route::get('/questions/callback/{taskId}', function (string $taskId) {
  244. // ✅ 优先从缓存读取(跨域友好)
  245. $callbackData = cache($taskId);
  246. if ($callbackData) {
  247. // 清除已读取的回调数据
  248. cache()->forget($taskId);
  249. session()->forget('question_gen_callback_'.$taskId);
  250. return response()->json($callbackData);
  251. }
  252. // 备选:从session读取
  253. $sessionData = session('question_gen_callback_'.$taskId);
  254. if ($sessionData) {
  255. // 清除已读取的回调数据
  256. session()->forget('question_gen_callback_'.$taskId);
  257. return response()->json($sessionData);
  258. }
  259. // 未收到回调
  260. return response()->json(['status' => 'pending'], 202);
  261. })->name('api.questions.callback.get');
  262. // 获取OCR题目生成回调结果
  263. Route::get('/ocr-question-callback/{ocrRecordId}/{taskId}', function (int $ocrRecordId, string $taskId) {
  264. $cacheKey = "ocr_callback_{$ocrRecordId}_{$taskId}";
  265. $callbackData = cache($cacheKey);
  266. if ($callbackData) {
  267. // 清除已读取的回调数据
  268. cache()->forget($cacheKey);
  269. return response()->json([
  270. 'success' => true,
  271. 'data' => $callbackData,
  272. ]);
  273. }
  274. return response()->json([
  275. 'success' => false,
  276. 'status' => 'pending',
  277. 'message' => 'OCR callback not received yet',
  278. ], 202);
  279. })->name('api.ocr.callback.get');
  280. // 题目相关 API
  281. Route::get('/questions', function (QuestionServiceApi $service) {
  282. try {
  283. $page = (int) request()->get('page', 1);
  284. $perPage = (int) request()->get('per_page', 25);
  285. $filters = [
  286. 'kp_code' => request()->get('kp_code'),
  287. 'difficulty' => request()->get('difficulty'),
  288. 'search' => request()->get('search'),
  289. ];
  290. $response = $service->listQuestions($page, $perPage, $filters);
  291. return response()->json($response);
  292. } catch (\Exception $e) {
  293. \Log::error('Failed to fetch questions: '.$e->getMessage());
  294. return response()->json([
  295. 'data' => [],
  296. 'meta' => [
  297. 'page' => 1,
  298. 'per_page' => 25,
  299. 'total' => 0,
  300. 'total_pages' => 0,
  301. ],
  302. 'error' => $e->getMessage(),
  303. ], 500);
  304. }
  305. });
  306. // 获取题目统计信息
  307. Route::get('/questions/statistics', function (QuestionServiceApi $service) {
  308. try {
  309. $stats = $service->getStatistics();
  310. return response()->json($stats);
  311. } catch (\Exception $e) {
  312. \Log::error('Failed to get question statistics: '.$e->getMessage());
  313. return response()->json(['error' => $e->getMessage()], 500);
  314. }
  315. });
  316. // 语义搜索题目
  317. Route::post('/questions/search', function (QuestionServiceApi $service) {
  318. try {
  319. $data = request()->only(['query', 'limit']);
  320. $results = $service->searchQuestions($data['query'], $data['limit'] ?? 20);
  321. return response()->json($results);
  322. } catch (\Exception $e) {
  323. \Log::error('Question search failed: '.$e->getMessage());
  324. return response()->json(['error' => $e->getMessage()], 500);
  325. }
  326. });
  327. // 获取单个题目详情
  328. Route::get('/questions/{id}', function (int $id, QuestionServiceApi $service) {
  329. try {
  330. $question = $service->getQuestionById($id);
  331. if (! $question) {
  332. return response()->json(['error' => 'Question not found'], 404);
  333. }
  334. return response()->json($question);
  335. } catch (\Exception $e) {
  336. \Log::error("Failed to get question {$id}: ".$e->getMessage());
  337. return response()->json(['error' => $e->getMessage()], 500);
  338. }
  339. });
  340. // AI 生成题目
  341. Route::post('/questions/generate', function (QuestionServiceApi $service) {
  342. try {
  343. $data = request()->only(['kp_code', 'keyword', 'count', 'strategy']);
  344. $result = $service->generateQuestions($data);
  345. return response()->json($result);
  346. } catch (\Exception $e) {
  347. \Log::error('Question generation failed: '.$e->getMessage());
  348. return response()->json([
  349. 'success' => false,
  350. 'message' => $e->getMessage(),
  351. ], 500);
  352. }
  353. });
  354. // 删除题目
  355. Route::delete('/questions/{id}', function (int $id, QuestionServiceApi $service) {
  356. try {
  357. $deleted = $service->deleteQuestion($id);
  358. return response()->json([
  359. 'success' => $deleted,
  360. 'message' => $deleted ? 'Question deleted' : 'Failed to delete',
  361. ]);
  362. } catch (\Exception $e) {
  363. \Log::error("Failed to delete question {$id}: ".$e->getMessage());
  364. return response()->json([
  365. 'success' => false,
  366. 'message' => $e->getMessage(),
  367. ], 500);
  368. }
  369. });
  370. use App\Http\Controllers\Api\KnowledgePointTreeController;
  371. // 获取知识点树形结构(从 MySQL 数据库)
  372. Route::get('/knowledge-points', [KnowledgePointTreeController::class, 'index'])
  373. ->name('api.knowledge-points.index');
  374. // 智能出卷对外接口:生成试卷并返回PDF/判卷地址
  375. Route::post('/intelligent-exams', [IntelligentExamController::class, 'store'])
  376. ->withoutMiddleware([
  377. Authenticate::class,
  378. 'auth',
  379. 'auth:sanctum',
  380. 'auth:api',
  381. ])
  382. ->name('api.intelligent-exams.store');
  383. // 智能出卷任务状态查询
  384. Route::get('/intelligent-exams/status/{taskId}', [IntelligentExamController::class, 'status'])
  385. ->withoutMiddleware([
  386. Authenticate::class,
  387. 'auth',
  388. 'auth:sanctum',
  389. 'auth:api',
  390. ])
  391. ->name('api.intelligent-exams.status');
  392. // 学情报告对外接口:生成并返回学情报告 PDF
  393. Route::post('/exam-analysis/report', [ExamAnalysisApiController::class, 'store'])
  394. ->withoutMiddleware([
  395. Authenticate::class,
  396. 'auth',
  397. 'auth:sanctum',
  398. 'auth:api',
  399. ])
  400. ->name('api.exam-analysis.report');
  401. // 学情报告任务状态查询
  402. Route::get('/exam-analysis/status/{taskId}', [ExamAnalysisApiController::class, 'status'])
  403. ->withoutMiddleware([
  404. Authenticate::class,
  405. 'auth',
  406. 'auth:sanctum',
  407. 'auth:api',
  408. ])
  409. ->name('api.exam-analysis.status');
  410. // 获取PDF报告URL(查询指定试卷的报告状态)
  411. Route::get('/exam-analysis/pdf/{paper_id}', [ExamAnalysisApiController::class, 'getPdfUrl'])
  412. ->withoutMiddleware([
  413. Authenticate::class,
  414. 'auth',
  415. 'auth:sanctum',
  416. 'auth:api',
  417. ])
  418. ->name('api.exam-analysis.pdf');
  419. /*
  420. |--------------------------------------------------------------------------
  421. | 错题本 API 路由
  422. |--------------------------------------------------------------------------
  423. */
  424. // 获取错题列表
  425. Route::get('/mistake-book', [MistakeBookController::class, 'listMistakes'])
  426. ->withoutMiddleware([
  427. Authenticate::class,
  428. 'auth',
  429. 'auth:sanctum',
  430. 'auth:api',
  431. ])
  432. ->name('api.mistake-book.list');
  433. // 新增错题
  434. Route::post('/mistake-book', [MistakeBookController::class, 'addMistake'])
  435. ->withoutMiddleware([
  436. Authenticate::class,
  437. 'auth',
  438. 'auth:sanctum',
  439. 'auth:api',
  440. ])
  441. ->name('api.mistake-book.create');
  442. // 获取单条错题详情
  443. Route::get('/mistake-book/{mistakeId}', [MistakeBookController::class, 'getMistakeDetail'])
  444. ->withoutMiddleware([
  445. Authenticate::class,
  446. 'auth',
  447. 'auth:sanctum',
  448. 'auth:api',
  449. ])
  450. ->whereNumber('mistakeId')
  451. ->name('api.mistake-book.detail');
  452. // 获取错题统计概要
  453. Route::get('/mistake-book/summary', [MistakeBookController::class, 'getSummary'])
  454. ->withoutMiddleware([
  455. Authenticate::class,
  456. 'auth',
  457. 'auth:sanctum',
  458. 'auth:api',
  459. ])
  460. ->name('api.mistake-book.summary');
  461. // 获取错误模式分析
  462. Route::get('/mistake-book/analytics/mistake-pattern', [MistakeBookController::class, 'getMistakePatterns'])
  463. ->withoutMiddleware([
  464. Authenticate::class,
  465. 'auth',
  466. 'auth:sanctum',
  467. 'auth:api',
  468. ])
  469. ->name('api.mistake-book.patterns');
  470. // 收藏/取消收藏错题
  471. Route::post('/mistake-book/{mistakeId}/favorite', [MistakeBookController::class, 'toggleFavorite'])
  472. ->withoutMiddleware([
  473. Authenticate::class,
  474. 'auth',
  475. 'auth:sanctum',
  476. 'auth:api',
  477. ])
  478. ->name('api.mistake-book.favorite');
  479. // 标记错题已复习
  480. Route::post('/mistake-book/{mistakeId}/review', [MistakeBookController::class, 'markReviewed'])
  481. ->withoutMiddleware([
  482. Authenticate::class,
  483. 'auth',
  484. 'auth:sanctum',
  485. 'auth:api',
  486. ])
  487. ->name('api.mistake-book.review');
  488. // 加入重练清单
  489. Route::post('/mistake-book/{mistakeId}/retry-list', [MistakeBookController::class, 'addToRetryList'])
  490. ->withoutMiddleware([
  491. Authenticate::class,
  492. 'auth',
  493. 'auth:sanctum',
  494. 'auth:api',
  495. ])
  496. ->name('api.mistake-book.retry-list');
  497. // 推荐练习题
  498. Route::post('/mistake-book/recommend-practice', [MistakeBookController::class, 'recommendPractice'])
  499. ->withoutMiddleware([
  500. Authenticate::class,
  501. 'auth',
  502. 'auth:sanctum',
  503. 'auth:api',
  504. ])
  505. ->name('api.mistake-book.recommend-practice');
  506. // 获取错题本快照数据(仪表板用)
  507. Route::get('/mistake-book/snapshot', [MistakeBookController::class, 'getSnapshot'])
  508. ->withoutMiddleware([
  509. Authenticate::class,
  510. 'auth',
  511. 'auth:sanctum',
  512. 'auth:api',
  513. ])
  514. ->name('api.mistake-book.snapshot');
  515. /*
  516. |--------------------------------------------------------------------------
  517. | 错题复习状态管理 API 路由
  518. |--------------------------------------------------------------------------
  519. */
  520. Route::post('/mistake-book/{mistakeId}/review-status', [MistakeBookController::class, 'updateReviewStatus'])
  521. ->withoutMiddleware([
  522. Authenticate::class,
  523. 'auth',
  524. 'auth:sanctum',
  525. 'auth:api',
  526. ])
  527. ->name('api.mistake-book.review-status.update');
  528. Route::get('/mistake-book/{mistakeId}/review-status', [MistakeBookController::class, 'getReviewStatus'])
  529. ->withoutMiddleware([
  530. Authenticate::class,
  531. 'auth',
  532. 'auth:sanctum',
  533. 'auth:api',
  534. ])
  535. ->name('api.mistake-book.review-status.get');
  536. Route::post('/mistake-book/{mistakeId}/increment-review', [MistakeBookController::class, 'incrementReview'])
  537. ->withoutMiddleware([
  538. Authenticate::class,
  539. 'auth',
  540. 'auth:sanctum',
  541. 'auth:api',
  542. ])
  543. ->name('api.mistake-book.increment-review');
  544. Route::post('/mistake-book/{mistakeId}/reset-review', [MistakeBookController::class, 'resetReview'])
  545. ->withoutMiddleware([
  546. Authenticate::class,
  547. 'auth',
  548. 'auth:sanctum',
  549. 'auth:api',
  550. ])
  551. ->name('api.mistake-book.reset-review');
  552. /*
  553. |--------------------------------------------------------------------------
  554. | 错题批量操作 API 路由
  555. |--------------------------------------------------------------------------
  556. */
  557. Route::post('/mistake-book/batch-operation', [MistakeBookController::class, 'batchOperation'])
  558. ->withoutMiddleware([
  559. Authenticate::class,
  560. 'auth',
  561. 'auth:sanctum',
  562. 'auth:api',
  563. ])
  564. ->name('api.mistake-book.batch-operation');
  565. Route::post('/mistake-book/batch/mark-reviewed', [MistakeBookController::class, 'batchMarkReviewed'])
  566. ->withoutMiddleware([
  567. Authenticate::class,
  568. 'auth',
  569. 'auth:sanctum',
  570. 'auth:api',
  571. ])
  572. ->name('api.mistake-book.batch.mark-reviewed');
  573. Route::post('/mistake-book/batch/mark-mastered', [MistakeBookController::class, 'batchMarkMastered'])
  574. ->withoutMiddleware([
  575. Authenticate::class,
  576. 'auth',
  577. 'auth:sanctum',
  578. 'auth:api',
  579. ])
  580. ->name('api.mistake-book.batch.mark-mastered');
  581. Route::post('/mistake-book/batch/add-to-retry-list', [MistakeBookController::class, 'batchAddToRetryList'])
  582. ->withoutMiddleware([
  583. Authenticate::class,
  584. 'auth',
  585. 'auth:sanctum',
  586. 'auth:api',
  587. ])
  588. ->name('api.mistake-book.batch.add-to-retry-list');
  589. Route::post('/mistake-book/batch/remove-from-retry-list', [MistakeBookController::class, 'batchRemoveFromRetryList'])
  590. ->withoutMiddleware([
  591. Authenticate::class,
  592. 'auth',
  593. 'auth:sanctum',
  594. 'auth:api',
  595. ])
  596. ->name('api.mistake-book.batch.remove-from-retry-list');
  597. Route::post('/mistake-book/batch/set-error-type', [MistakeBookController::class, 'batchSetErrorType'])
  598. ->withoutMiddleware([
  599. Authenticate::class,
  600. 'auth',
  601. 'auth:sanctum',
  602. 'auth:api',
  603. ])
  604. ->name('api.mistake-book.batch.set-error-type');
  605. Route::post('/mistake-book/batch/set-importance', [MistakeBookController::class, 'batchSetImportance'])
  606. ->withoutMiddleware([
  607. Authenticate::class,
  608. 'auth',
  609. 'auth:sanctum',
  610. 'auth:api',
  611. ])
  612. ->name('api.mistake-book.batch.set-importance');
  613. Route::post('/mistake-book/batch/toggle-favorite', [MistakeBookController::class, 'batchToggleFavorite'])
  614. ->withoutMiddleware([
  615. Authenticate::class,
  616. 'auth',
  617. 'auth:sanctum',
  618. 'auth:api',
  619. ])
  620. ->name('api.mistake-book.batch.toggle-favorite');
  621. /*
  622. |--------------------------------------------------------------------------
  623. | 知识点掌握情况 API 路由
  624. |--------------------------------------------------------------------------
  625. */
  626. use App\Http\Controllers\Api\KnowledgeMasteryController;
  627. // 获取学生知识点掌握情况统计
  628. Route::get('/knowledge-mastery/stats/{studentId}', [KnowledgeMasteryController::class, 'stats'])
  629. ->where('studentId', '[0-9]+') // 限制为数字
  630. ->withoutMiddleware([
  631. Authenticate::class,
  632. 'auth',
  633. 'auth:sanctum',
  634. 'auth:api',
  635. ])
  636. ->name('api.knowledge-mastery.stats');
  637. // 获取学生知识点掌握摘要
  638. Route::get('/knowledge-mastery/summary/{studentId}', [KnowledgeMasteryController::class, 'summary'])
  639. ->where('studentId', '[0-9]+') // 限制为数字
  640. ->withoutMiddleware([
  641. Authenticate::class,
  642. 'auth',
  643. 'auth:sanctum',
  644. 'auth:api',
  645. ])
  646. ->name('api.knowledge-mastery.summary');
  647. // 获取学生知识点图谱数据
  648. Route::get('/knowledge-mastery/graph/{studentId}', [KnowledgeMasteryController::class, 'graph'])
  649. ->where('studentId', '[0-9]+') // 限制为数字
  650. ->withoutMiddleware([
  651. Authenticate::class,
  652. 'auth',
  653. 'auth:sanctum',
  654. 'auth:api',
  655. ])
  656. ->name('api.knowledge-mastery.graph');
  657. // 获取学生知识点图谱快照列表
  658. Route::get('/knowledge-mastery/graph/snapshots/{studentId}', [KnowledgeMasteryController::class, 'graphSnapshots'])
  659. ->where('studentId', '[0-9]+') // 限制为数字
  660. ->withoutMiddleware([
  661. Authenticate::class,
  662. 'auth',
  663. 'auth:sanctum',
  664. 'auth:api',
  665. ])
  666. ->name('api.knowledge-mastery.graph.snapshots');
  667. // 获取学生知识点快照列表(简化路径)
  668. Route::get('/knowledge-mastery/snapshots/{studentId}', [KnowledgeMasteryController::class, 'snapshots'])
  669. ->where('studentId', '[0-9]+') // 限制为数字
  670. ->withoutMiddleware([
  671. Authenticate::class,
  672. 'auth',
  673. 'auth:sanctum',
  674. 'auth:api',
  675. ])
  676. ->name('api.knowledge-mastery.snapshots');
  677. // 创建知识点掌握度快照
  678. Route::post('/knowledge-mastery/snapshot/{studentId}', [KnowledgeMasteryController::class, 'createSnapshot'])
  679. ->where('studentId', '[0-9]+') // 限制为数字
  680. ->withoutMiddleware([
  681. Authenticate::class,
  682. 'auth',
  683. 'auth:sanctum',
  684. 'auth:api',
  685. ])
  686. ->name('api.knowledge-mastery.snapshot.create');
  687. /*
  688. |--------------------------------------------------------------------------
  689. | 教材管理 API 路由
  690. |--------------------------------------------------------------------------
  691. */
  692. // 获取教材列表(按年级排序)
  693. Route::get('/textbooks', [TextbookApiController::class, 'index'])
  694. ->name('api.textbooks.index');
  695. // 根据年级获取教材
  696. Route::get('/textbooks/grade/{grade}', [TextbookApiController::class, 'getByGrade'])
  697. ->name('api.textbooks.by-grade');
  698. // 获取教材系列列表(必须在 {id} 路由之前定义)
  699. Route::get('/textbooks/series', [TextbookApiController::class, 'getSeries'])
  700. ->name('api.textbooks.series');
  701. // 获取年级枚举
  702. Route::get('/textbooks/grades', [TextbookApiController::class, 'getGradeEnums'])
  703. ->name('api.textbooks.grades');
  704. // 获取单个教材详情
  705. Route::get('/textbooks/{id}', [TextbookApiController::class, 'show'])
  706. ->name('api.textbooks.show');
  707. // 获取教材目录
  708. Route::get('/textbooks/{id}/catalog', [TextbookApiController::class, 'getCatalog'])
  709. ->name('api.textbooks.catalog');
  710. /*
  711. |--------------------------------------------------------------------------
  712. | MathRecSys 集成 API 路由
  713. |--------------------------------------------------------------------------
  714. */
  715. use App\Http\Controllers\Api\StudentController;
  716. // 健康检查
  717. Route::get('/mathrecsys/health', [StudentController::class, 'checkServiceHealth'])->name('api.mathrecsys.health');
  718. // 学生相关 API
  719. Route::prefix('mathrecsys/students')->name('api.mathrecsys.students.')->group(function () {
  720. // 获取学生完整信息
  721. Route::get('{studentId}', [StudentController::class, 'show'])
  722. ->where('studentId', '[0-9]+') // 限制为数字
  723. ->name('show');
  724. // 获取个性化推荐
  725. Route::get('{studentId}/recommendations', [StudentController::class, 'getRecommendations'])
  726. ->where('studentId', '[0-9]+') // 限制为数字
  727. ->name('recommendations');
  728. // 获取学习轨迹
  729. Route::get('{studentId}/trajectory', [StudentController::class, 'getTrajectory'])
  730. ->where('studentId', '[0-9]+') // 限制为数字
  731. ->name('trajectory');
  732. // 获取学习建议
  733. Route::get('{studentId}/suggestions', [StudentController::class, 'getSuggestions'])
  734. ->where('studentId', '[0-9]+') // 限制为数字
  735. ->name('suggestions');
  736. // 智能分析题目
  737. Route::post('{studentId}/analyze', [StudentController::class, 'analyzeQuestion'])
  738. ->where('studentId', '[0-9]+') // 限制为数字
  739. ->name('analyze');
  740. // 更新掌握度
  741. Route::put('{studentId}/mastery', [StudentController::class, 'updateMastery'])
  742. ->where('studentId', '[0-9]+') // 限制为数字
  743. ->name('update-mastery');
  744. // 【新增】获取学生知识点掌握详情(直接查询MySQL)
  745. Route::get('{studentId}/knowledge-points/detail', [StudentKnowledgeController::class, 'getKnowledgePointsDetail'])
  746. ->where('studentId', '[0-9]+')
  747. ->name('knowledge-points.detail');
  748. // 【新增】获取学生知识点层级关系
  749. Route::get('{studentId}/knowledge-points/hierarchy', [StudentKnowledgeController::class, 'getKnowledgeHierarchy'])
  750. ->where('studentId', '[0-9]+')
  751. ->name('knowledge-points.hierarchy');
  752. });
  753. // 【前端直连】学生知识点详情API(直接查询MySQL)
  754. Route::prefix('students')->name('students.')->group(function () {
  755. Route::get('{studentId}/knowledge-points/detail', [StudentKnowledgeController::class, 'getKnowledgePointsDetail'])
  756. ->where('studentId', '[0-9]+')
  757. ->name('knowledge-points.detail');
  758. Route::get('{studentId}/knowledge-points/hierarchy', [StudentKnowledgeController::class, 'getKnowledgeHierarchy'])
  759. ->where('studentId', '[0-9]+')
  760. ->name('knowledge-points.hierarchy');
  761. });
  762. // 班级分析 API
  763. Route::prefix('mathrecsys/classes')->name('api.mathrecsys.classes.')->group(function () {
  764. Route::get('{classId}/analysis', [StudentController::class, 'classAnalysis'])
  765. ->where('classId', '[0-9]+') // 限制为数字
  766. ->name('analysis');
  767. });
  768. // 测试 API
  769. Route::get('/mathrecsys/test', function () {
  770. return response()->json([
  771. 'success' => true,
  772. 'message' => 'MathRecSys API integration is working',
  773. 'timestamp' => now()->toISOString(),
  774. ]);
  775. })->name('api.mathrecsys.test');
  776. // 测试OCR题目生成API调用
  777. Route::post('/test-ocr-generation', function () {
  778. try {
  779. $service = new \App\Services\QuestionBankService;
  780. // 模拟前端传递的OCR题目数据
  781. $questions = [
  782. [
  783. 'id' => 1,
  784. 'content' => '计算:2+3-4',
  785. ],
  786. [
  787. 'id' => 2,
  788. 'content' => '解方程:x+5=10',
  789. ],
  790. ];
  791. Log::info('开始测试OCR题目生成', [
  792. 'questions_count' => count($questions),
  793. 'ocr_record_id' => 12,
  794. ]);
  795. // 使用异步API,系统自动生成回调URL
  796. $response = $service->generateQuestionsFromOcrAsync(
  797. $questions,
  798. '高一',
  799. '数学',
  800. 12, // OCR记录ID
  801. null, // 让系统自动生成回调URL
  802. 'api.ocr.callback' // 回调路由名称
  803. );
  804. Log::info('OCR题目生成响应', [
  805. 'response' => $response,
  806. 'status' => $response['status'] ?? 'unknown',
  807. 'task_id' => $response['task_id'] ?? 'N/A',
  808. ]);
  809. return response()->json([
  810. 'success' => true,
  811. 'message' => 'OCR题目生成测试完成',
  812. 'data' => $response,
  813. ]);
  814. } catch (\Exception $e) {
  815. Log::error('测试OCR题目生成失败', [
  816. 'error' => $e->getMessage(),
  817. 'trace' => $e->getTraceAsString(),
  818. ]);
  819. return response()->json([
  820. 'success' => false,
  821. 'error' => $e->getMessage(),
  822. ], 500);
  823. }
  824. })->name('api.test.ocr.generation');
  825. /*
  826. |--------------------------------------------------------------------------
  827. | 学生作答分析 API 路由
  828. |--------------------------------------------------------------------------
  829. */
  830. // 提交学生作答结果
  831. Route::post('/student-answers/analyze', [StudentAnswerAnalysisController::class, 'submitAnswers'])
  832. ->withoutMiddleware([
  833. Authenticate::class,
  834. 'auth',
  835. 'auth:sanctum',
  836. 'auth:api',
  837. ])
  838. ->name('api.student-answers.analyze');
  839. // 查询分析任务状态
  840. Route::get('/student-answers/analysis/status/{taskId}', [StudentAnswerAnalysisController::class, 'getAnalysisStatus'])
  841. ->withoutMiddleware([
  842. Authenticate::class,
  843. 'auth',
  844. 'auth:sanctum',
  845. 'auth:api',
  846. ])
  847. ->name('api.student-answers.analysis.status');
  848. // 获取学生学习历史
  849. Route::get('/student-answers/history/{studentId}', [StudentAnswerAnalysisController::class, 'getStudentLearningHistory'])
  850. ->withoutMiddleware([
  851. Authenticate::class,
  852. 'auth',
  853. 'auth:sanctum',
  854. 'auth:api',
  855. ])
  856. ->name('api.student-answers.history');
  857. /*
  858. |--------------------------------------------------------------------------
  859. | 考试答题分析 API 路由(步骤级分析)
  860. |--------------------------------------------------------------------------
  861. */
  862. use App\Http\Controllers\Api\ExamAnswerAnalysisController;
  863. use App\Http\Controllers\Api\HealthCheckController;
  864. use App\Http\Controllers\Api\PaperSubmitAnalysisController;
  865. // 分析考试答题数据
  866. Route::post('/exam-answer-analysis', [ExamAnswerAnalysisController::class, 'analyze'])
  867. ->withoutMiddleware([
  868. Authenticate::class,
  869. 'auth',
  870. 'auth:sanctum',
  871. 'auth:api',
  872. ])
  873. ->name('api.exam-answer-analysis.analyze');
  874. // 获取分析结果
  875. Route::get('/exam-answer-analysis/{student_id}/{paper_id}', [ExamAnswerAnalysisController::class, 'getAnalysisResult'])
  876. ->withoutMiddleware([
  877. Authenticate::class,
  878. 'auth',
  879. 'auth:sanctum',
  880. 'auth:api',
  881. ])
  882. ->where('student_id', '.*')
  883. ->where('paper_id', '.*')
  884. ->name('api.exam-answer-analysis.result');
  885. // 获取学生历史分析记录
  886. Route::get('/exam-answer-analysis/history/{student_id}', [ExamAnswerAnalysisController::class, 'getHistory'])
  887. ->withoutMiddleware([
  888. Authenticate::class,
  889. 'auth',
  890. 'auth:sanctum',
  891. 'auth:api',
  892. ])
  893. ->where('student_id', '.*')
  894. ->name('api.exam-answer-analysis.history');
  895. // 获取知识点掌握度趋势
  896. Route::get('/exam-answer-analysis/mastery-trend/{student_id}', [ExamAnswerAnalysisController::class, 'getMasteryTrend'])
  897. ->withoutMiddleware([
  898. Authenticate::class,
  899. 'auth',
  900. 'auth:sanctum',
  901. 'auth:api',
  902. ])
  903. ->where('student_id', '.*')
  904. ->name('api.exam-answer-analysis.mastery-trend');
  905. // 获取智能出卷推荐
  906. Route::get('/exam-answer-analysis/smart-quiz/{student_id}', [ExamAnswerAnalysisController::class, 'getSmartQuizRecommendation'])
  907. ->withoutMiddleware([
  908. Authenticate::class,
  909. 'auth',
  910. 'auth:sanctum',
  911. 'auth:api',
  912. ])
  913. ->where('student_id', '.*')
  914. ->name('api.exam-answer-analysis.smart-quiz');
  915. // 导出分析报告
  916. Route::get('/exam-answer-analysis/export/{student_id}/{paper_id}', [ExamAnswerAnalysisController::class, 'export'])
  917. ->withoutMiddleware([
  918. Authenticate::class,
  919. 'auth',
  920. 'auth:sanctum',
  921. 'auth:api',
  922. ])
  923. ->where('student_id', '.*')
  924. ->where('paper_id', '.*')
  925. ->name('api.exam-answer-analysis.export');
  926. // 批量分析多个学生的考试数据
  927. Route::post('/exam-answer-analysis/batch', [ExamAnswerAnalysisController::class, 'batchAnalyze'])
  928. ->withoutMiddleware([
  929. Authenticate::class,
  930. 'auth',
  931. 'auth:sanctum',
  932. 'auth:api',
  933. ])
  934. ->name('api.exam-answer-analysis.batch');
  935. Route::get('/tasks/status/{taskId}', function (string $taskId) {
  936. $task = app(\App\Services\TaskManager::class)->getTaskStatus($taskId);
  937. if (! $task) {
  938. return response()->json([
  939. 'success' => false,
  940. 'message' => '任务不存在',
  941. ], 404);
  942. }
  943. return response()->json([
  944. 'success' => true,
  945. 'data' => $task,
  946. ]);
  947. })->name('api.tasks.status');
  948. /*
  949. |--------------------------------------------------------------------------
  950. | 试卷提交分析 API 路由(前端提交答题数据)
  951. |--------------------------------------------------------------------------
  952. */
  953. // 提交试卷答题数据进行分析
  954. Route::post('/paper-submit-analysis', [PaperSubmitAnalysisController::class, 'analyze'])
  955. ->withoutMiddleware([
  956. Authenticate::class,
  957. 'auth',
  958. 'auth:sanctum',
  959. 'auth:api',
  960. ])
  961. ->name('api.paper-submit-analysis.analyze');
  962. // 获取试卷分析结果
  963. Route::get('/paper-submit-analysis/{paperId}', [PaperSubmitAnalysisController::class, 'getResult'])
  964. ->withoutMiddleware([
  965. Authenticate::class,
  966. 'auth',
  967. 'auth:sanctum',
  968. 'auth:api',
  969. ])
  970. ->name('api.paper-submit-analysis.result');
  971. /*
  972. |--------------------------------------------------------------------------
  973. | 健康检查 API 路由
  974. |--------------------------------------------------------------------------
  975. */
  976. // 检查系统健康状态
  977. Route::get('/health', [HealthCheckController::class, 'index'])
  978. ->withoutMiddleware([
  979. Authenticate::class,
  980. 'auth',
  981. 'auth:sanctum',
  982. 'auth:api',
  983. ])
  984. ->name('api.health.index');
  985. /*
  986. |--------------------------------------------------------------------------
  987. | 学生学习进度 API 路由
  988. |--------------------------------------------------------------------------
  989. */
  990. use App\Http\Controllers\Api\QuestionPdfController;
  991. use App\Http\Controllers\Api\StudentProgressController;
  992. // 获取单个学生学习进度
  993. Route::get('/students/{studentId}/learning-progress', [StudentProgressController::class, 'show'])
  994. ->withoutMiddleware([Authenticate::class, 'auth', 'auth:sanctum', 'auth:api'])
  995. ->name('api.students.learning-progress.show');
  996. // 批量获取学生学习进度
  997. Route::post('/students/learning-progress/batch', [StudentProgressController::class, 'batch'])
  998. ->withoutMiddleware([Authenticate::class, 'auth', 'auth:sanctum', 'auth:api'])
  999. ->name('api.students.learning-progress.batch');
  1000. /*
  1001. |--------------------------------------------------------------------------
  1002. | 题目PDF生成 API 路由
  1003. |--------------------------------------------------------------------------
  1004. */
  1005. // 根据指定题目ID生成PDF
  1006. Route::post('/questions/pdf', [QuestionPdfController::class, 'generate'])
  1007. ->withoutMiddleware([Authenticate::class, 'auth', 'auth:sanctum', 'auth:api'])
  1008. ->name('api.questions.pdf.generate');
  1009. /*
  1010. |--------------------------------------------------------------------------
  1011. | 试卷 PDF 重新生成 API 路由
  1012. |--------------------------------------------------------------------------
  1013. */
  1014. // 重新生成统一 PDF(卷子 + 判卷)
  1015. Route::post('/papers/{paper_id}/regenerate', [\App\Http\Controllers\ExamPdfController::class, 'regeneratePdf'])
  1016. ->withoutMiddleware([Authenticate::class, 'auth', 'auth:sanctum', 'auth:api'])
  1017. ->where('paper_id', '^paper_\d+$')
  1018. ->name('api.papers.regenerate');
  1019. // 重新生成试卷 PDF(不含答案)
  1020. Route::post('/papers/{paper_id}/regenerate-exam', [\App\Http\Controllers\ExamPdfController::class, 'regenerateExamPdf'])
  1021. ->withoutMiddleware([Authenticate::class, 'auth', 'auth:sanctum', 'auth:api'])
  1022. ->where('paper_id', '^paper_\d+$')
  1023. ->name('api.papers.regenerate-exam');
  1024. // 重新生成判卷 PDF(含答案)
  1025. Route::post('/papers/{paper_id}/regenerate-grading', [\App\Http\Controllers\ExamPdfController::class, 'regenerateGradingPdf'])
  1026. ->withoutMiddleware([Authenticate::class, 'auth', 'auth:sanctum', 'auth:api'])
  1027. ->where('paper_id', '^paper_\d+$')
  1028. ->name('api.papers.regenerate-grading');
  1029. /*
  1030. |--------------------------------------------------------------------------
  1031. | 以下为旧代码(已迁移到 Controller,保留注释供参考)
  1032. |--------------------------------------------------------------------------
  1033. */
  1034. // [已迁移] 获取学生学习进度
  1035. /*
  1036. Route::get('/students/{studentId}/learning-progress', function (string $studentId) {
  1037. try {
  1038. Log::info('获取学生学习进度', ['student_id' => $studentId]);
  1039. // 1. 获取所有父知识点代码(需要排除的)
  1040. $parentKpCodes = [];
  1041. try {
  1042. $parentCodes = DB::connection('remote_mysql')
  1043. ->table('knowledge_points')
  1044. ->whereNotNull('parent_kp_code')
  1045. ->distinct()
  1046. ->pluck('parent_kp_code')
  1047. ->toArray();
  1048. $parentKpCodes = array_filter($parentCodes);
  1049. } catch (\Exception $e) {
  1050. Log::warning('获取父知识点代码失败', ['error' => $e->getMessage()]);
  1051. }
  1052. // 2. 获取学生所有知识点的掌握度数据(合并两个表)
  1053. $mergedData = [];
  1054. try {
  1055. // 从 student_knowledge_mastery 表获取数据
  1056. $detailedData = DB::connection('remote_mysql')
  1057. ->table('student_knowledge_mastery')
  1058. ->where('student_id', $studentId)
  1059. ->select([
  1060. 'kp_code',
  1061. 'mastery_level',
  1062. 'total_attempts',
  1063. 'correct_attempts',
  1064. 'updated_at'
  1065. ])
  1066. ->get()
  1067. ->toArray();
  1068. foreach ($detailedData as $item) {
  1069. $mergedData[$item->kp_code] = [
  1070. 'kp_code' => $item->kp_code,
  1071. 'mastery_level' => (float) $item->mastery_level,
  1072. 'total_attempts' => $item->total_attempts,
  1073. 'correct_attempts' => $item->correct_attempts,
  1074. 'source_table' => 'student_knowledge_mastery',
  1075. 'updated_at' => $item->updated_at
  1076. ];
  1077. }
  1078. } catch (\Exception $e) {
  1079. Log::warning('从 student_knowledge_mastery 表获取数据失败', [
  1080. 'student_id' => $studentId,
  1081. 'error' => $e->getMessage()
  1082. ]);
  1083. }
  1084. try {
  1085. // 从 student_mastery 表获取数据(补充或覆盖)
  1086. $simpleData = DB::connection('remote_mysql')
  1087. ->table('student_mastery')
  1088. ->where('student_id', $studentId)
  1089. ->select([
  1090. 'kp as kp_code',
  1091. 'mastery',
  1092. 'attempts as total_attempts',
  1093. 'correct as correct_attempts',
  1094. 'updated_at'
  1095. ])
  1096. ->get()
  1097. ->toArray();
  1098. foreach ($simpleData as $item) {
  1099. $kpCode = $item->kp_code;
  1100. $masteryLevel = (float) $item->mastery;
  1101. // 如果已存在,优先使用 mastery_level 更高的数据
  1102. if (isset($mergedData[$kpCode])) {
  1103. if ($masteryLevel > $mergedData[$kpCode]['mastery_level']) {
  1104. $mergedData[$kpCode]['mastery_level'] = $masteryLevel;
  1105. $mergedData[$kpCode]['source_table'] = 'student_mastery (updated)';
  1106. }
  1107. } else {
  1108. $mergedData[$kpCode] = [
  1109. 'kp_code' => $kpCode,
  1110. 'mastery_level' => $masteryLevel,
  1111. 'total_attempts' => $item->total_attempts ?? 0,
  1112. 'correct_attempts' => $item->correct_attempts ?? 0,
  1113. 'source_table' => 'student_mastery',
  1114. 'updated_at' => $item->updated_at ?? null
  1115. ];
  1116. }
  1117. }
  1118. } catch (\Exception $e) {
  1119. Log::warning('从 student_mastery 表获取数据失败', [
  1120. 'student_id' => $studentId,
  1121. 'error' => $e->getMessage()
  1122. ]);
  1123. }
  1124. // 3. 获取学生所有知识点掌握度数据(只使用student_knowledge_mastery表,因为student_mastery表为空)
  1125. $allMasteryData = collect($mergedData);
  1126. if ($allMasteryData->isEmpty()) {
  1127. return response()->json([
  1128. 'success' => false,
  1129. 'error' => '该学生没有掌握度数据'
  1130. ], 400);
  1131. }
  1132. // 4. 获取知识图谱中所有知识点,找出真正的子知识点(叶子节点)
  1133. $allKps = DB::connection('remote_mysql')
  1134. ->table('knowledge_points')
  1135. ->select(['kp_code', 'parent_kp_code'])
  1136. ->get();
  1137. // 找出叶子节点(没有任何其他知识点以它作为父节点)
  1138. $kpCodes = $allKps->pluck('kp_code')->toArray();
  1139. $parentCodes = $allKps->whereNotNull('parent_kp_code')->pluck('parent_kp_code')->unique()->toArray();
  1140. $leafKpCodes = array_values(array_diff($kpCodes, $parentCodes));
  1141. // 5. 筛选出学生掌握的子知识点数据
  1142. $childMasteryData = $allMasteryData->filter(function($item) use ($leafKpCodes) {
  1143. return in_array($item['kp_code'], $leafKpCodes);
  1144. });
  1145. if ($childMasteryData->isEmpty()) {
  1146. return response()->json([
  1147. 'success' => false,
  1148. 'error' => '该学生没有子知识点的掌握度数据'
  1149. ], 400);
  1150. }
  1151. // 6. 计算分子:学生已掌握的子知识点掌握度总和
  1152. $totalChildMasterySum = $childMasteryData->sum('mastery_level');
  1153. // 7. 计算分母:知识图谱中所有子知识点的最大可能总分
  1154. $maxChildScore = count($leafKpCodes) * 1.0; // 每个子知识点最高1分
  1155. $learningProgress = $maxChildScore > 0 ? ($totalChildMasterySum / $maxChildScore) : 0.0;
  1156. // 8. 统计信息
  1157. $statistics = [
  1158. 'total_knowledge_points' => count($kpCodes),
  1159. 'child_knowledge_points' => count($leafKpCodes),
  1160. 'student_mastered_child_count' => $childMasteryData->count(),
  1161. 'student_mastered_child_percentage' => round(($childMasteryData->count() / count($leafKpCodes)) * 100, 2),
  1162. 'child_mastery_sum' => round($totalChildMasterySum, 4),
  1163. 'max_child_score' => round($maxChildScore, 4),
  1164. 'learning_progress_percentage' => round($learningProgress * 100, 2),
  1165. 'data_source' => implode(', ', $allMasteryData->pluck('source_table')->unique()->toArray()),
  1166. 'child_mastery_max' => round($childMasteryData->max('mastery_level'), 4),
  1167. 'child_mastery_min' => round($childMasteryData->min('mastery_level'), 4),
  1168. 'child_mastery_avg' => round($childMasteryData->avg('mastery_level'), 4),
  1169. ];
  1170. $result = [
  1171. 'student_id' => $studentId,
  1172. 'learning_progress' => round($learningProgress, 6),
  1173. 'learning_progress_percentage' => round($learningProgress * 100, 2),
  1174. 'child_mastery_sum' => round($totalChildMasterySum, 4),
  1175. 'child_knowledge_points' => $childMasteryData->values()->toArray(),
  1176. 'statistics' => $statistics,
  1177. 'calculated_at' => now()->toISOString()
  1178. ];
  1179. Log::info('学生学习进度计算成功', [
  1180. 'student_id' => $studentId,
  1181. 'learning_progress' => $learningProgress,
  1182. 'child_mastery_sum' => $totalChildMasterySum,
  1183. 'child_knowledge_points_count' => count($leafKpCodes),
  1184. 'student_mastered_child_count' => $childMasteryData->count()
  1185. ]);
  1186. return response()->json([
  1187. 'success' => true,
  1188. 'data' => $result,
  1189. 'message' => '学习进度计算成功'
  1190. ]);
  1191. } catch (\Exception $e) {
  1192. Log::error('计算学生学习进度失败', [
  1193. 'student_id' => $studentId,
  1194. 'error' => $e->getMessage(),
  1195. 'trace' => $e->getTraceAsString()
  1196. ]);
  1197. return response()->json([
  1198. 'success' => false,
  1199. 'message' => '计算学习进度失败: ' . $e->getMessage()
  1200. ], 500);
  1201. }
  1202. })
  1203. ->withoutMiddleware([
  1204. Authenticate::class,
  1205. 'auth',
  1206. 'auth:sanctum',
  1207. 'auth:api',
  1208. ])
  1209. ->name('api.students.learning-progress.get');
  1210. // 批量获取学生学习进度(解决 N+1 问题)
  1211. Route::post('/students/learning-progress/batch', function (\Illuminate\Http\Request $request) {
  1212. try {
  1213. $studentIds = $request->input('student_ids', []);
  1214. if (empty($studentIds)) {
  1215. return response()->json([
  1216. 'success' => false,
  1217. 'error' => 'student_ids 不能为空'
  1218. ], 400);
  1219. }
  1220. if (count($studentIds) > 100) {
  1221. return response()->json([
  1222. 'success' => false,
  1223. 'error' => '单次最多查询 100 个学生'
  1224. ], 400);
  1225. }
  1226. Log::info('批量获取学生学习进度', ['student_ids' => $studentIds, 'count' => count($studentIds)]);
  1227. // 1. 获取知识图谱结构(所有学生共用)
  1228. $allKps = DB::connection('remote_mysql')
  1229. ->table('knowledge_points')
  1230. ->select(['kp_code', 'parent_kp_code'])
  1231. ->get();
  1232. // 找出叶子节点
  1233. $kpCodes = $allKps->pluck('kp_code')->toArray();
  1234. $parentCodes = $allKps->whereNotNull('parent_kp_code')->pluck('parent_kp_code')->unique()->toArray();
  1235. $leafKpCodes = array_values(array_diff($kpCodes, $parentCodes));
  1236. $leafKpCodesSet = array_flip($leafKpCodes); // 用于快速查找
  1237. $maxChildScore = count($leafKpCodes) * 1.0;
  1238. // 2. 批量获取 student_knowledge_mastery 数据
  1239. $detailedData = DB::connection('remote_mysql')
  1240. ->table('student_knowledge_mastery')
  1241. ->whereIn('student_id', $studentIds)
  1242. ->select(['student_id', 'kp_code', 'mastery_level', 'total_attempts', 'correct_attempts', 'updated_at'])
  1243. ->get()
  1244. ->groupBy('student_id');
  1245. // 3. 批量获取 student_mastery 数据
  1246. $simpleData = DB::connection('remote_mysql')
  1247. ->table('student_mastery')
  1248. ->whereIn('student_id', $studentIds)
  1249. ->select(['student_id', 'kp as kp_code', 'mastery', 'attempts as total_attempts', 'correct as correct_attempts', 'updated_at'])
  1250. ->get()
  1251. ->groupBy('student_id');
  1252. // 4. 为每个学生计算学习进度
  1253. $results = [];
  1254. foreach ($studentIds as $studentId) {
  1255. $studentId = (string) $studentId;
  1256. // 合并该学生的掌握度数据
  1257. $mergedData = [];
  1258. // 从 student_knowledge_mastery 获取
  1259. if (isset($detailedData[$studentId])) {
  1260. foreach ($detailedData[$studentId] as $item) {
  1261. $mergedData[$item->kp_code] = [
  1262. 'kp_code' => $item->kp_code,
  1263. 'mastery_level' => (float) $item->mastery_level,
  1264. ];
  1265. }
  1266. }
  1267. // 从 student_mastery 补充或更新
  1268. if (isset($simpleData[$studentId])) {
  1269. foreach ($simpleData[$studentId] as $item) {
  1270. $kpCode = $item->kp_code;
  1271. $masteryLevel = (float) $item->mastery;
  1272. if (isset($mergedData[$kpCode])) {
  1273. if ($masteryLevel > $mergedData[$kpCode]['mastery_level']) {
  1274. $mergedData[$kpCode]['mastery_level'] = $masteryLevel;
  1275. }
  1276. } else {
  1277. $mergedData[$kpCode] = [
  1278. 'kp_code' => $kpCode,
  1279. 'mastery_level' => $masteryLevel,
  1280. ];
  1281. }
  1282. }
  1283. }
  1284. // 计算学习进度
  1285. if (empty($mergedData)) {
  1286. $results[$studentId] = [
  1287. 'student_id' => $studentId,
  1288. 'learning_progress' => 0,
  1289. 'learning_progress_percentage' => 0,
  1290. 'mastered_child_count' => 0,
  1291. 'total_child_count' => count($leafKpCodes),
  1292. 'has_data' => false,
  1293. ];
  1294. continue;
  1295. }
  1296. // 筛选叶子节点并计算
  1297. $childMasterySum = 0;
  1298. $masteredChildCount = 0;
  1299. foreach ($mergedData as $item) {
  1300. if (isset($leafKpCodesSet[$item['kp_code']])) {
  1301. $childMasterySum += $item['mastery_level'];
  1302. $masteredChildCount++;
  1303. }
  1304. }
  1305. $learningProgress = $maxChildScore > 0 ? ($childMasterySum / $maxChildScore) : 0.0;
  1306. $results[$studentId] = [
  1307. 'student_id' => $studentId,
  1308. 'learning_progress' => round($learningProgress, 6),
  1309. 'learning_progress_percentage' => round($learningProgress * 100, 2),
  1310. 'mastered_child_count' => $masteredChildCount,
  1311. 'total_child_count' => count($leafKpCodes),
  1312. 'child_mastery_sum' => round($childMasterySum, 4),
  1313. 'has_data' => true,
  1314. ];
  1315. }
  1316. Log::info('批量学习进度计算完成', ['count' => count($results)]);
  1317. return response()->json([
  1318. 'success' => true,
  1319. 'data' => $results,
  1320. 'meta' => [
  1321. 'total_students' => count($studentIds),
  1322. 'total_child_knowledge_points' => count($leafKpCodes),
  1323. 'calculated_at' => now()->toISOString(),
  1324. ]
  1325. ]);
  1326. } catch (\Exception $e) {
  1327. Log::error('批量计算学习进度失败', [
  1328. 'error' => $e->getMessage(),
  1329. 'trace' => $e->getTraceAsString()
  1330. ]);
  1331. return response()->json([
  1332. 'success' => false,
  1333. 'message' => '批量计算学习进度失败: ' . $e->getMessage()
  1334. ], 500);
  1335. }
  1336. })
  1337. ->withoutMiddleware([
  1338. Authenticate::class,
  1339. 'auth',
  1340. 'auth:sanctum',
  1341. 'auth:api',
  1342. ])
  1343. ->name('api.students.learning-progress.batch');
  1344. */
  1345. /*
  1346. |--------------------------------------------------------------------------
  1347. | 注意:知识点详情API已被注释
  1348. |--------------------------------------------------------------------------
  1349. |
  1350. | 由于路由冲突问题,此API暂时被注释。可使用以下替代接口:
  1351. | - api/mathrecsys/students/{studentId}/knowledge-points/detail
  1352. | - api/students/{studentId}/knowledge-points/detail
  1353. |
  1354. */
  1355. // 获取学生知识点掌握度详情(测试版)
  1356. // Route::get('/students/{studentId}/knowledge-points', function (string $studentId) {
  1357. // // 实现代码...
  1358. // })
  1359. // ->withoutMiddleware([
  1360. // Authenticate::class,
  1361. // 'auth',
  1362. // 'auth:sanctum',
  1363. // 'auth:api',
  1364. // ])
  1365. // ->name('api.students.knowledge-points.details');