Prechádzať zdrojové kódy

log 生成 pdf 流程

yemeishu 3 dní pred
rodič
commit
502122242c

+ 61 - 4
app/Http/Controllers/Api/IntelligentExamController.php

@@ -7,10 +7,13 @@ use App\Models\Paper;
 use App\Models\PaperQuestion;
 use App\Services\LearningAnalyticsService;
 use App\Services\ExamPdfExportService;
+use App\Services\ExternalIdService;
 use App\Services\QuestionBankService;
 use App\Services\PaperPayloadService;
 use App\Services\TaskManager;
 use App\Models\MistakeRecord;
+use App\Models\Student;
+use App\Models\Teacher;
 use Illuminate\Http\JsonResponse;
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Http;
@@ -24,19 +27,22 @@ class IntelligentExamController extends Controller
     private ExamPdfExportService $pdfExportService;
     private PaperPayloadService $paperPayloadService;
     private TaskManager $taskManager;
+    private ExternalIdService $externalIdService;
 
     public function __construct(
         LearningAnalyticsService $learningAnalyticsService,
         QuestionBankService $questionBankService,
         ExamPdfExportService $pdfExportService,
         PaperPayloadService $paperPayloadService,
-        TaskManager $taskManager
+        TaskManager $taskManager,
+        ExternalIdService $externalIdService
     ) {
         $this->learningAnalyticsService = $learningAnalyticsService;
         $this->questionBankService = $questionBankService;
         $this->pdfExportService = $pdfExportService;
         $this->paperPayloadService = $paperPayloadService;
         $this->taskManager = $taskManager;
+        $this->externalIdService = $externalIdService;
     }
 
     /**
@@ -53,10 +59,12 @@ class IntelligentExamController extends Controller
         $normalized = $this->normalizePayload($payload);
 
         $validator = validator($normalized, [
-            'student_id' => 'required|string|min:1',  // 接受字符串或数字类型,如"1764913638"或1764913638
-            'teacher_id' => 'nullable|string',
+            'student_id' => 'required|string|min:1|regex:/^\\d+$/',  // 接受字符串或数字类型,如"1764913638"或1764913638
+            'teacher_id' => 'required|string|min:1|regex:/^\\d+$/',
             'paper_name' => 'nullable|string|max:255',
-            'grade' => 'nullable|string|max:50',
+            'grade' => 'required|integer|in:7,8,9',
+            'student_name' => 'required|string|max:50',
+            'teacher_name' => 'required|string|max:50',
             'total_questions' => 'nullable|integer|min:6|max:100',
             'difficulty_category' => 'nullable|string',
             'kp_codes' => 'nullable|array',
@@ -83,6 +91,7 @@ class IntelligentExamController extends Controller
 
         $data = $validator->validated();
         $data['total_questions'] = $data['total_questions'] ?? 20;
+        $this->ensureStudentTeacherRelation($data);
 
         // 确保 kp_codes 是数组
         $data['kp_codes'] = $data['kp_codes'] ?? [];
@@ -394,6 +403,12 @@ class IntelligentExamController extends Controller
         if (isset($payload['student_id'])) {
             $payload['student_id'] = (string) $payload['student_id'];
         }
+        if (isset($payload['teacher_id'])) {
+            $payload['teacher_id'] = (string) $payload['teacher_id'];
+        }
+        if (isset($payload['grade'])) {
+            $payload['grade'] = (string) $payload['grade'];
+        }
 
         // 处理 kp_codes:空字符串或null转换为空数组
         if (isset($payload['kp_codes'])) {
@@ -431,6 +446,48 @@ class IntelligentExamController extends Controller
         return $payload;
     }
 
+    private function ensureStudentTeacherRelation(array $data): void
+    {
+        $studentId = (int) $data['student_id'];
+        $teacherId = (int) $data['teacher_id'];
+        $studentName = (string) ($data['student_name'] ?? '未知学生');
+        $teacherName = (string) ($data['teacher_name'] ?? '未知教师');
+        $grade = (string) ($data['grade'] ?? '未知年级');
+
+        $teacher = $this->externalIdService->handleTeacherExternalId($teacherId, [
+            'name' => $teacherName,
+            'subject' => '数学',
+        ]);
+
+        if ($teacher->name !== $teacherName && $teacherName !== '') {
+            $teacher->update(['name' => $teacherName]);
+        }
+
+        $student = Student::where('student_id', $studentId)->first();
+        if ($student) {
+            $updates = [];
+            if ($studentName !== '' && $student->name !== $studentName) {
+                $updates['name'] = $studentName;
+            }
+            if ($grade !== '' && $student->grade !== $grade) {
+                $updates['grade'] = $grade;
+            }
+            if ($teacherId > 0 && (int) $student->teacher_id !== $teacherId) {
+                $updates['teacher_id'] = $teacherId;
+            }
+            if (!empty($updates)) {
+                $student->update($updates);
+            }
+            return;
+        }
+
+        $this->externalIdService->handleStudentExternalId($studentId, [
+            'name' => $studentName,
+            'grade' => $grade,
+            'teacher_id' => $teacherId,
+        ]);
+    }
+
     private function normalizeQuestionTypeRatio(array $input): array
     {
         // 默认按 4:2:4

+ 5 - 1
app/Services/ApiDocumentation.php

@@ -270,7 +270,11 @@ class ApiDocumentation
                     'description' => '根据学生情况智能生成个性化试卷,返回 PDF 和判卷地址',
                     'params' => [
                         'body' => [
-                            ['name' => 'student_id', 'type' => 'integer', 'required' => true, 'description' => '学生 ID'],
+                            ['name' => 'student_id', 'type' => 'integer', 'required' => true, 'description' => '学生 ID(数字)'],
+                            ['name' => 'student_name', 'type' => 'string', 'required' => true, 'description' => '学生姓名'],
+                            ['name' => 'teacher_id', 'type' => 'integer', 'required' => true, 'description' => '教师 ID(数字)'],
+                            ['name' => 'teacher_name', 'type' => 'string', 'required' => true, 'description' => '教师姓名'],
+                            ['name' => 'grade', 'type' => 'string', 'required' => true, 'description' => '年级(7/8/9)'],
                             ['name' => 'knowledge_points', 'type' => 'array', 'required' => false, 'description' => '指定知识点列表'],
                             ['name' => 'difficulty', 'type' => 'string', 'required' => false, 'description' => '难度偏好:easy/medium/hard/adaptive'],
                             ['name' => 'question_count', 'type' => 'integer', 'required' => false, 'default' => '20', 'description' => '题目数量'],

+ 3 - 3
app/Services/ExternalIdService.php

@@ -29,8 +29,8 @@ class ExternalIdService
         // 创建用户记录
         $userData = [
             'user_id' => (string) $studentId, // user表是varchar类型
-            'username' => (string) $studentId,
-            'password_hash' => Hash::make('password123'),
+            'username' => $studentData['name'],
+            'password_hash' => Hash::make(env('STUDENT_HASH', 'UqB6KHYx84')),
             'role' => 'student',
             'full_name' => $studentData['name'] ?? '未知学生',
             'email' => $studentId . '@student.edu',
@@ -76,7 +76,7 @@ class ExternalIdService
         $userData = [
             'user_id' => (string) $teacherId,
             'username' => $teacherData['phone'] ?? (string) $teacherId, // 如果有手机号就用手机号
-            'password_hash' => Hash::make('password123'),
+            'password_hash' => Hash::make(env('TEACHER_HASH', 'TeGiFkEim2')),
             'role' => 'teacher',
             'full_name' => $teacherData['name'] ?? '未知教师',
             'email' => $teacherId . '@teacher.edu',