OCRRecord.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. 'exam_id',
  12. 'student_id',
  13. 'image_path',
  14. 'image_filename',
  15. 'image_size',
  16. 'image_width',
  17. 'image_height',
  18. 'qr_code_data',
  19. 'paper_type',
  20. 'status',
  21. 'error_message',
  22. 'total_questions',
  23. 'processed_questions',
  24. 'confidence_avg',
  25. 'processed_at',
  26. 'ai_analyzed_at',
  27. 'ai_analysis_count',
  28. ];
  29. protected $dates = [
  30. 'processed_at',
  31. 'ai_analyzed_at',
  32. 'created_at',
  33. 'updated_at',
  34. ];
  35. protected $casts = [
  36. 'qr_code_data' => 'array',
  37. 'image_size' => 'integer',
  38. 'image_width' => 'integer',
  39. 'image_height' => 'integer',
  40. 'total_questions' => 'integer',
  41. 'processed_questions' => 'integer',
  42. 'confidence_avg' => 'float',
  43. 'processed_at' => 'datetime',
  44. 'created_at' => 'datetime',
  45. 'updated_at' => 'datetime',
  46. ];
  47. public function questions(): HasMany
  48. {
  49. return $this->hasMany(OCRQuestionResult::class, 'ocr_record_id', 'id');
  50. }
  51. public function student()
  52. {
  53. return $this->belongsTo(Student::class, 'student_id', 'student_id');
  54. }
  55. public function getStatusBadgeAttribute(): string
  56. {
  57. return match ($this->status) {
  58. 'pending' => '<span class="px-2 py-1 text-xs rounded bg-gray-100 text-gray-800">待处理</span>',
  59. 'processing' => '<span class="px-2 py-1 text-xs rounded bg-blue-100 text-blue-800">处理中</span>',
  60. 'completed' => '<span class="px-2 py-1 text-xs rounded bg-green-100 text-green-800">已完成</span>',
  61. 'failed' => '<span class="px-2 py-1 text-xs rounded bg-red-100 text-red-800">失败</span>',
  62. default => '<span class="px-2 py-1 text-xs rounded bg-gray-100 text-gray-800">未知</span>',
  63. };
  64. }
  65. public function getImageUrlAttribute(): string
  66. {
  67. if ($this->image_path && file_exists(public_path($this->image_path))) {
  68. return asset($this->image_path);
  69. }
  70. return '';
  71. }
  72. public function getProgressPercentageAttribute(): int
  73. {
  74. if ($this->total_questions === 0) {
  75. return 0;
  76. }
  77. return intval(($this->processed_questions / $this->total_questions) * 100);
  78. }
  79. public function getPaperTypeLabelAttribute(): string
  80. {
  81. return match($this->paper_type) {
  82. 'unit_test' => '单元测试',
  83. 'midterm' => '期中考试',
  84. 'final' => '期末考试',
  85. 'homework' => '家庭作业',
  86. 'quiz' => '随堂测验',
  87. 'other' => '其他',
  88. default => '未分类',
  89. };
  90. }
  91. }