|
|
@@ -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
|