Просмотр исходного кода

feat: 组卷接口增加截止到哪个章节的参数,用于摸底不超纲

过卫栋 3 недель назад
Родитель
Сommit
4eb457bfc4

+ 2 - 0
app/Http/Controllers/Api/IntelligentExamController.php

@@ -94,6 +94,7 @@ class IntelligentExamController extends Controller
             'chapter_id_list.*' => 'integer|min:1',
             'kp_code_list' => 'nullable|array',                   // 知识点组卷专用
             'kp_code_list.*' => 'string',
+            'end_catalog_id' => 'nullable|integer|min:1',         // 摸底专用:截止章节ID
             // 新增:专项练习选项
             'practice_options' => 'nullable|array',
             'practice_options.weakness_threshold' => 'nullable|numeric|min:0|max:1',
@@ -192,6 +193,7 @@ class IntelligentExamController extends Controller
                     'exam_type' => $data['exam_type'] ?? 'general', // 兼容旧版参数
                     'paper_ids' => $paperIds, // 错题本类型专用参数
                     'textbook_id' => $data['textbook_id'] ?? null, // 摸底和智能组卷专用
+                    'end_catalog_id' => $data['end_catalog_id'] ?? null, // 摸底专用:截止章节ID
                     'chapter_id_list' => $data['chapter_id_list'] ?? null, // 教材组卷专用
                     'kp_code_list' => $assembleType == 3 ? null : ($data['kp_code_list'] ?? null), // 知识点组卷专用
                     'practice_options' => $data['practice_options'] ?? null, // 传递专项练习选项

+ 31 - 8
app/Services/ExamTypeStrategy.php

@@ -296,14 +296,15 @@ class ExamTypeStrategy
         $textbookId = $params['textbook_id'] ?? null;
         $grade = $params['grade'] ?? null;
         $totalQuestions = $params['total_questions'] ?? 20;
+        $endCatalogId = $params['end_catalog_id'] ?? null; // 截止章节ID
 
         if (!$textbookId) {
             Log::warning('ExamTypeStrategy: 摸底测试需要 textbook_id 参数');
             return $this->buildGeneralParams($params);
         }
 
-        // 第一步:根据 textbook_id 查询章节
-        $catalogChapterIds = $this->getTextbookChapterIds($textbookId);
+        // 第一步:根据 textbook_id 查询章节(支持截止章节过滤)
+        $catalogChapterIds = $this->getTextbookChapterIds($textbookId, $endCatalogId);
 
         if (empty($catalogChapterIds)) {
             Log::warning('ExamTypeStrategy: 未找到课本章节', ['textbook_id' => $textbookId]);
@@ -312,6 +313,7 @@ class ExamTypeStrategy
 
         Log::info('ExamTypeStrategy: 获取到课本章节(摸底测试)', [
             'textbook_id' => $textbookId,
+            'end_catalog_id' => $endCatalogId,
             'chapter_count' => count($catalogChapterIds)
         ]);
 
@@ -1053,20 +1055,41 @@ class ExamTypeStrategy
 
     /**
      * 根据课本ID获取章节ID列表
+     * @param int $textbookId 教材ID
+     * @param int|null $endCatalogId 截止章节ID(仅摸底使用),只返回该章节及之前的章节
      */
-    private function getTextbookChapterIds(int $textbookId): array
+    private function getTextbookChapterIds(int $textbookId, ?int $endCatalogId = null): array
     {
         try {
-            // 查询 text_book_catalog_nodes 表
-            $chapterIds = DB::table('textbook_catalog_nodes')
+            $query = DB::table('textbook_catalog_nodes')
                 ->where('textbook_id', $textbookId)
                 ->where('node_type', 'section') // nodeType='section'
-                ->orderBy('sort_order')
-                ->pluck('id')
-                ->toArray();
+                ->orderBy('sort_order');
+
+            // 如果指定了截止章节,只获取该章节及之前的章节
+            if ($endCatalogId) {
+                $endSortOrder = DB::table('textbook_catalog_nodes')
+                    ->where('id', $endCatalogId)
+                    ->value('sort_order');
+
+                if ($endSortOrder !== null) {
+                    $query->where('sort_order', '<=', $endSortOrder);
+                    Log::debug('ExamTypeStrategy: 应用截止章节过滤', [
+                        'end_catalog_id' => $endCatalogId,
+                        'end_sort_order' => $endSortOrder
+                    ]);
+                } else {
+                    Log::warning('ExamTypeStrategy: 截止章节ID无效', [
+                        'end_catalog_id' => $endCatalogId
+                    ]);
+                }
+            }
+
+            $chapterIds = $query->pluck('id')->toArray();
 
             Log::debug('ExamTypeStrategy: 查询课本章节ID', [
                 'textbook_id' => $textbookId,
+                'end_catalog_id' => $endCatalogId,
                 'found_count' => count($chapterIds)
             ]);