OCRRecord.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\Relations\HasMany;
  6. class OCRRecord extends Model
  7. {
  8. use HasFactory;
  9. protected $table = 'ocr_records';
  10. protected $fillable = [
  11. 'user_id',
  12. 'paper_title',
  13. 'paper_type',
  14. 'file_path',
  15. 'image_count',
  16. 'total_questions',
  17. 'status',
  18. 'error_message',
  19. 'created_at',
  20. 'updated_at',
  21. 'analysis_id',
  22. ];
  23. protected $casts = [
  24. 'created_at' => 'datetime',
  25. 'updated_at' => 'datetime',
  26. 'image_count' => 'integer',
  27. 'total_questions' => 'integer',
  28. ];
  29. public function questions(): HasMany
  30. {
  31. return $this->hasMany(OCRQuestionResult::class, 'ocr_record_id', 'id');
  32. }
  33. public function student()
  34. {
  35. return $this->belongsTo(Student::class, 'user_id', 'student_id');
  36. }
  37. public function getStatusBadgeAttribute(): string
  38. {
  39. return match ($this->status) {
  40. 'pending' => '<span class="px-2 py-1 text-xs rounded bg-gray-100 text-gray-800">待处理</span>',
  41. 'processing' => '<span class="px-2 py-1 text-xs rounded bg-blue-100 text-blue-800">处理中</span>',
  42. 'completed' => '<span class="px-2 py-1 text-xs rounded bg-green-100 text-green-800">已完成</span>',
  43. 'failed' => '<span class="px-2 py-1 text-xs rounded bg-red-100 text-red-800">失败</span>',
  44. default => '<span class="px-2 py-1 text-xs rounded bg-gray-100 text-gray-800">未知</span>',
  45. };
  46. }
  47. // 兼容性方法:获取 student_id
  48. public function getStudentIdAttribute(): ?string
  49. {
  50. return $this->user_id;
  51. }
  52. // 兼容性方法:设置 student_id
  53. public function setStudentIdAttribute(?string $value): void
  54. {
  55. $this->attributes['user_id'] = $value;
  56. }
  57. public function getImageUrlAttribute(): string
  58. {
  59. if ($this->image_path && file_exists(public_path($this->image_path))) {
  60. return asset($this->image_path);
  61. }
  62. return '';
  63. }
  64. public function getProgressPercentageAttribute(): int
  65. {
  66. if ($this->total_questions === 0) {
  67. return 0;
  68. }
  69. return intval(($this->processed_questions / $this->total_questions) * 100);
  70. }
  71. public function getPaperTypeLabelAttribute(): string
  72. {
  73. return match($this->paper_type) {
  74. 'unit_test' => '单元测试',
  75. 'midterm' => '期中考试',
  76. 'final' => '期末考试',
  77. 'homework' => '家庭作业',
  78. 'quiz' => '随堂测验',
  79. 'other' => '其他',
  80. default => '未分类',
  81. };
  82. }
  83. }