yemeishu hai 1 semana
pai
achega
313b7de9fa

+ 14 - 0
app/Http/Controllers/Api/PaperAssembleController.php

@@ -25,6 +25,11 @@ class PaperAssembleController extends Controller
             $query->whereIn('question_type', $types);
         }
 
+        $stageGrade = $this->normalizeStageGrade((int) $request->input('grade'));
+        if ($stageGrade !== null) {
+            $query->where('grade', $stageGrade);
+        }
+
         $questions = $query->inRandomOrder()->limit($count)->get();
 
         return response()->json([
@@ -32,4 +37,13 @@ class PaperAssembleController extends Controller
             'data' => $questions,
         ]);
     }
+
+    private function normalizeStageGrade(int $grade): ?int
+    {
+        if ($grade <= 0) {
+            return null;
+        }
+
+        return $grade <= 9 ? 2 : 3;
+    }
 }

+ 14 - 0
app/Http/Controllers/Api/QuestionRandomController.php

@@ -23,6 +23,11 @@ class QuestionRandomController extends Controller
             $query->where('question_type', $type);
         }
 
+        $stageGrade = $this->normalizeStageGrade((int) $request->input('grade'));
+        if ($stageGrade !== null) {
+            $query->where('grade', $stageGrade);
+        }
+
         $questions = $query->inRandomOrder()->limit($limit)->get();
 
         return response()->json([
@@ -30,4 +35,13 @@ class QuestionRandomController extends Controller
             'data' => $questions,
         ]);
     }
+
+    private function normalizeStageGrade(int $grade): ?int
+    {
+        if ($grade <= 0) {
+            return null;
+        }
+
+        return $grade <= 9 ? 2 : 3;
+    }
 }

+ 14 - 0
app/Http/Controllers/Api/QuestionSearchController.php

@@ -25,6 +25,11 @@ class QuestionSearchController extends Controller
             $query->where('question_type', $type);
         }
 
+        $stageGrade = $this->normalizeStageGrade((int) $request->input('grade'));
+        if ($stageGrade !== null) {
+            $query->where('grade', $stageGrade);
+        }
+
         $min = $request->float('difficulty_min');
         $max = $request->float('difficulty_max');
         if ($min !== null && $max !== null) {
@@ -35,4 +40,13 @@ class QuestionSearchController extends Controller
 
         return response()->json($query->paginate($perPage));
     }
+
+    private function normalizeStageGrade(int $grade): ?int
+    {
+        if ($grade <= 0) {
+            return null;
+        }
+
+        return $grade <= 9 ? 2 : 3;
+    }
 }

+ 31 - 0
app/Services/LearningAnalyticsService.php

@@ -1702,6 +1702,18 @@ class LearningAnalyticsService
                 Log::info('应用知识点筛选', ['kp_codes' => $kpCodes]);
             }
 
+            // 按学段筛选(题库 grade: 2=初中, 3=高中)
+            if ($grade !== null) {
+                $stageGrade = $this->normalizeQuestionStageGrade((int) $grade);
+                if ($stageGrade !== null) {
+                    $query->where('grade', $stageGrade);
+                    Log::info('应用学段筛选', [
+                        'input_grade' => $grade,
+                        'stage_grade' => $stageGrade
+                    ]);
+                }
+            }
+
             // 按技能筛选(这里使用 tags 字段模拟技能筛选)
             if (!empty($skills)) {
                 $query->where(function ($q) use ($skills) {
@@ -2966,6 +2978,15 @@ class LearningAnalyticsService
             // 查询同年级其他知识点的题目
             $query = \App\Models\Question::query();
 
+            $stageGrade = $this->normalizeQuestionStageGrade($grade);
+            if ($stageGrade !== null) {
+                $query->where('grade', $stageGrade);
+                Log::info('getSupplementaryQuestionsForGrade: 应用学段筛选', [
+                    'input_grade' => $grade,
+                    'stage_grade' => $stageGrade
+                ]);
+            }
+
             // 排除已选知识点
             if (!empty($existingKpCodes)) {
                 $query->whereNotIn('kp_code', $existingKpCodes);
@@ -3140,6 +3161,16 @@ class LearningAnalyticsService
         };
     }
 
+    private function normalizeQuestionStageGrade(int $grade): ?int
+    {
+        if ($grade <= 0) {
+            return null;
+        }
+
+        // 年级转学段 todo:下一步需要视频小学
+        return $grade <= 9 ? 2 : 3;
+    }
+
     /**
      * 【新增】获取难度标签(重载版本)
      */

+ 23 - 0
app/Services/QuestionLocalService.php

@@ -340,6 +340,13 @@ class QuestionLocalService
             }
         }
 
+        if (!empty($filters['grade'])) {
+            $stageGrade = $this->normalizeStageGrade((int) $filters['grade']);
+            if ($stageGrade !== null) {
+                $query->where('grade', $stageGrade);
+            }
+        }
+
         $questions = $query->get();
 
         $selected = $this->applyRatioSelection($questions, $totalQuestions, $filters);
@@ -378,9 +385,25 @@ class QuestionLocalService
             $query->search($filters['search']);
         }
 
+        if (!empty($filters['grade'])) {
+            $stageGrade = $this->normalizeStageGrade((int) $filters['grade']);
+            if ($stageGrade !== null) {
+                $query->where('grade', $stageGrade);
+            }
+        }
+
         return $query;
     }
 
+    private function normalizeStageGrade(int $grade): ?int
+    {
+        if ($grade <= 0) {
+            return null;
+        }
+
+        return $grade <= 9 ? 2 : 3;
+    }
+
     private function normalizePayload(array $payload): array
     {
         $normalized = [