| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- <?php
- namespace App\Filament\Pages;
- use App\Jobs\ProcessOCRRecord;
- use App\Models\OCRRecord;
- use App\Models\Student;
- use App\Models\Teacher;
- use BackedEnum;
- use Filament\Notifications\Notification;
- use Filament\Pages\Page;
- use Livewire\WithFileUploads;
- use Livewire\Attributes\Computed;
- use Livewire\Attributes\On;
- use Illuminate\Support\Facades\Storage;
- use UnitEnum;
- class UploadExamPaper extends Page
- {
- use WithFileUploads;
- protected static ?string $title = '上传考试卷子';
- protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-arrow-up-tray';
- protected static ?string $navigationLabel = '上传考试卷子';
- protected static string|UnitEnum|null $navigationGroup = '操作';
- protected static ?int $navigationSort = 2;
- protected static ?string $slug = 'upload-exam-paper';
- protected string $view = 'filament.pages.upload-exam-paper';
- public ?string $teacherId = null;
- public ?string $studentId = null;
- public $uploadedImage = null;
- public bool $isUploading = false;
- public function mount()
- {
- $this->teacherId = null;
- $this->studentId = null;
- $this->uploadedImage = null;
- }
- #[Computed]
- public function teachers(): array
- {
- try {
- $teachers = Teacher::query()
- ->leftJoin('users as u', 'teachers.teacher_id', '=', 'u.user_id')
- ->select(
- 'teachers.teacher_id',
- 'teachers.name',
- 'teachers.subject',
- 'u.username',
- 'u.email'
- )
- ->orderBy('teachers.name')
- ->get();
- // 检查是否有学生没有对应的老师记录
- $teacherIds = $teachers->pluck('teacher_id')->toArray();
- $missingTeacherIds = Student::query()
- ->distinct()
- ->whereNotIn('teacher_id', $teacherIds)
- ->pluck('teacher_id')
- ->toArray();
- $teachersArray = $teachers->all();
- if (!empty($missingTeacherIds)) {
- foreach ($missingTeacherIds as $missingId) {
- $teachersArray[] = (object) [
- 'teacher_id' => $missingId,
- 'name' => '未知老师 (' . $missingId . ')',
- 'subject' => '未知',
- 'username' => null,
- 'email' => null
- ];
- }
- usort($teachersArray, function($a, $b) {
- return strcmp($a->name, $b->name);
- });
- }
- return $teachersArray;
- } catch (\Exception $e) {
- \Illuminate\Support\Facades\Log::error('加载老师列表失败', [
- 'error' => $e->getMessage()
- ]);
- return [];
- }
- }
- #[Computed]
- public function students(): array
- {
- if (empty($this->teacherId)) {
- return [];
- }
- try {
- return Student::query()
- ->leftJoin('users as u', 'students.student_id', '=', 'u.user_id')
- ->where('students.teacher_id', $this->teacherId)
- ->select(
- 'students.student_id',
- 'students.name',
- 'students.grade',
- 'students.class_name',
- 'u.username',
- 'u.email'
- )
- ->orderBy('students.grade')
- ->orderBy('students.class_name')
- ->orderBy('students.name')
- ->get()
- ->all();
- } catch (\Exception $e) {
- \Illuminate\Support\Facades\Log::error('加载学生列表失败', [
- 'teacher_id' => $this->teacherId,
- 'error' => $e->getMessage()
- ]);
- return [];
- }
- }
- #[Computed]
- public function recentRecords(): array
- {
- return OCRRecord::with('student')
- ->latest()
- ->take(5)
- ->get()
- ->toArray();
- }
- public function updatedTeacherId($value): void
- {
- // 当教师选择变化时,清空之前选择的学生
- $this->studentId = null;
- }
- public function submitUpload(): void
- {
- if (!$this->teacherId) {
- Notification::make()
- ->title('请选择老师')
- ->danger()
- ->send();
- return;
- }
- if (!$this->studentId) {
- Notification::make()
- ->title('请选择学生')
- ->danger()
- ->send();
- return;
- }
- if (!$this->uploadedImage) {
- Notification::make()
- ->title('请上传图片')
- ->danger()
- ->send();
- return;
- }
- $this->isUploading = true;
- try {
- // 保存图片
- $path = $this->uploadedImage->store('ocr-uploads', 'public');
- $filename = basename($path);
- // 创建OCR记录
- $record = OCRRecord::create([
- 'student_id' => $this->studentId,
- 'image_path' => $path,
- 'image_filename' => $filename,
- 'status' => 'pending',
- 'total_questions' => 0,
- 'processed_questions' => 0,
- ]);
- // 立即更新状态为处理中,提供更好的用户体验
- $record->update(['status' => 'processing']);
- // 自动触发OCR处理
- ProcessOCRRecord::dispatch($record->id);
- // 重置表单
- $this->teacherId = null;
- $this->studentId = null;
- $this->uploadedImage = null;
- Notification::make()
- ->title('上传成功')
- ->body("卷子已上传并开始OCR处理。记录ID: {$record->id}")
- ->success()
- ->send();
- // 刷新最近记录
- unset($this->recentRecords);
- } catch (\Exception $e) {
- Notification::make()
- ->title('上传失败')
- ->body($e->getMessage())
- ->danger()
- ->send();
- } finally {
- $this->isUploading = false;
- }
- }
- #[On('teacherChanged')]
- public function updateTeacherId($teacherId)
- {
- $this->teacherId = $teacherId;
- $this->studentId = null;
- }
- #[On('studentChanged')]
- public function updateStudentId($teacherId, $studentId)
- {
- $this->studentId = $studentId;
- }
- public function removeImage(): void
- {
- $this->uploadedImage = null;
- }
- }
|