UploadExamPaper.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. namespace App\Filament\Pages;
  3. use App\Jobs\ProcessOCRRecord;
  4. use App\Models\OCRRecord;
  5. use App\Models\Student;
  6. use App\Models\Teacher;
  7. use BackedEnum;
  8. use Filament\Notifications\Notification;
  9. use Filament\Pages\Page;
  10. use Livewire\WithFileUploads;
  11. use Livewire\Attributes\Computed;
  12. use Livewire\Attributes\On;
  13. use Illuminate\Support\Facades\Storage;
  14. use UnitEnum;
  15. class UploadExamPaper extends Page
  16. {
  17. use WithFileUploads;
  18. protected static ?string $title = '上传考试卷子';
  19. protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-arrow-up-tray';
  20. protected static ?string $navigationLabel = '上传考试卷子';
  21. protected static string|UnitEnum|null $navigationGroup = '操作';
  22. protected static ?int $navigationSort = 2;
  23. protected static ?string $slug = 'upload-exam-paper';
  24. protected string $view = 'filament.pages.upload-exam-paper';
  25. public ?string $teacherId = null;
  26. public ?string $studentId = null;
  27. public $uploadedImage = null;
  28. public bool $isUploading = false;
  29. public function mount()
  30. {
  31. $this->teacherId = null;
  32. $this->studentId = null;
  33. $this->uploadedImage = null;
  34. }
  35. #[Computed]
  36. public function teachers(): array
  37. {
  38. try {
  39. $teachers = Teacher::query()
  40. ->leftJoin('users as u', 'teachers.teacher_id', '=', 'u.user_id')
  41. ->select(
  42. 'teachers.teacher_id',
  43. 'teachers.name',
  44. 'teachers.subject',
  45. 'u.username',
  46. 'u.email'
  47. )
  48. ->orderBy('teachers.name')
  49. ->get();
  50. // 检查是否有学生没有对应的老师记录
  51. $teacherIds = $teachers->pluck('teacher_id')->toArray();
  52. $missingTeacherIds = Student::query()
  53. ->distinct()
  54. ->whereNotIn('teacher_id', $teacherIds)
  55. ->pluck('teacher_id')
  56. ->toArray();
  57. $teachersArray = $teachers->all();
  58. if (!empty($missingTeacherIds)) {
  59. foreach ($missingTeacherIds as $missingId) {
  60. $teachersArray[] = (object) [
  61. 'teacher_id' => $missingId,
  62. 'name' => '未知老师 (' . $missingId . ')',
  63. 'subject' => '未知',
  64. 'username' => null,
  65. 'email' => null
  66. ];
  67. }
  68. usort($teachersArray, function($a, $b) {
  69. return strcmp($a->name, $b->name);
  70. });
  71. }
  72. return $teachersArray;
  73. } catch (\Exception $e) {
  74. \Illuminate\Support\Facades\Log::error('加载老师列表失败', [
  75. 'error' => $e->getMessage()
  76. ]);
  77. return [];
  78. }
  79. }
  80. #[Computed]
  81. public function students(): array
  82. {
  83. if (empty($this->teacherId)) {
  84. return [];
  85. }
  86. try {
  87. return Student::query()
  88. ->leftJoin('users as u', 'students.student_id', '=', 'u.user_id')
  89. ->where('students.teacher_id', $this->teacherId)
  90. ->select(
  91. 'students.student_id',
  92. 'students.name',
  93. 'students.grade',
  94. 'students.class_name',
  95. 'u.username',
  96. 'u.email'
  97. )
  98. ->orderBy('students.grade')
  99. ->orderBy('students.class_name')
  100. ->orderBy('students.name')
  101. ->get()
  102. ->all();
  103. } catch (\Exception $e) {
  104. \Illuminate\Support\Facades\Log::error('加载学生列表失败', [
  105. 'teacher_id' => $this->teacherId,
  106. 'error' => $e->getMessage()
  107. ]);
  108. return [];
  109. }
  110. }
  111. #[Computed]
  112. public function recentRecords(): array
  113. {
  114. return OCRRecord::with('student')
  115. ->latest()
  116. ->take(5)
  117. ->get()
  118. ->toArray();
  119. }
  120. public function updatedTeacherId($value): void
  121. {
  122. // 当教师选择变化时,清空之前选择的学生
  123. $this->studentId = null;
  124. }
  125. public function submitUpload(): void
  126. {
  127. if (!$this->teacherId) {
  128. Notification::make()
  129. ->title('请选择老师')
  130. ->danger()
  131. ->send();
  132. return;
  133. }
  134. if (!$this->studentId) {
  135. Notification::make()
  136. ->title('请选择学生')
  137. ->danger()
  138. ->send();
  139. return;
  140. }
  141. if (!$this->uploadedImage) {
  142. Notification::make()
  143. ->title('请上传图片')
  144. ->danger()
  145. ->send();
  146. return;
  147. }
  148. $this->isUploading = true;
  149. try {
  150. // 保存图片
  151. $path = $this->uploadedImage->store('ocr-uploads', 'public');
  152. $filename = basename($path);
  153. // 创建OCR记录
  154. $record = OCRRecord::create([
  155. 'student_id' => $this->studentId,
  156. 'image_path' => $path,
  157. 'image_filename' => $filename,
  158. 'status' => 'pending',
  159. 'total_questions' => 0,
  160. 'processed_questions' => 0,
  161. ]);
  162. // 立即更新状态为处理中,提供更好的用户体验
  163. $record->update(['status' => 'processing']);
  164. // 自动触发OCR处理
  165. ProcessOCRRecord::dispatch($record->id);
  166. // 重置表单
  167. $this->teacherId = null;
  168. $this->studentId = null;
  169. $this->uploadedImage = null;
  170. Notification::make()
  171. ->title('上传成功')
  172. ->body("卷子已上传并开始OCR处理。记录ID: {$record->id}")
  173. ->success()
  174. ->send();
  175. // 刷新最近记录
  176. unset($this->recentRecords);
  177. } catch (\Exception $e) {
  178. Notification::make()
  179. ->title('上传失败')
  180. ->body($e->getMessage())
  181. ->danger()
  182. ->send();
  183. } finally {
  184. $this->isUploading = false;
  185. }
  186. }
  187. #[On('teacherChanged')]
  188. public function updateTeacherId($teacherId)
  189. {
  190. $this->teacherId = $teacherId;
  191. $this->studentId = null;
  192. }
  193. #[On('studentChanged')]
  194. public function updateStudentId($teacherId, $studentId)
  195. {
  196. $this->studentId = $studentId;
  197. }
  198. public function removeImage(): void
  199. {
  200. $this->uploadedImage = null;
  201. }
  202. }