OCRRecord.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. 'status',
  20. 'error_message',
  21. 'total_questions',
  22. 'processed_questions',
  23. 'confidence_avg',
  24. 'processed_at',
  25. 'ai_analyzed_at',
  26. 'ai_analysis_count',
  27. ];
  28. protected $dates = [
  29. 'processed_at',
  30. 'ai_analyzed_at',
  31. 'created_at',
  32. 'updated_at',
  33. ];
  34. protected $casts = [
  35. 'qr_code_data' => 'array',
  36. 'image_size' => 'integer',
  37. 'image_width' => 'integer',
  38. 'image_height' => 'integer',
  39. 'total_questions' => 'integer',
  40. 'processed_questions' => 'integer',
  41. 'confidence_avg' => 'float',
  42. 'processed_at' => 'datetime',
  43. 'created_at' => 'datetime',
  44. 'updated_at' => 'datetime',
  45. ];
  46. public function questions(): HasMany
  47. {
  48. return $this->hasMany(OCRQuestionResult::class, 'ocr_record_id', 'id');
  49. }
  50. public function student()
  51. {
  52. return $this->belongsTo(Student::class, 'student_id', 'student_id');
  53. }
  54. public function getStatusBadgeAttribute(): string
  55. {
  56. return match ($this->status) {
  57. 'pending' => '<span class="px-2 py-1 text-xs rounded bg-gray-100 text-gray-800">待处理</span>',
  58. 'processing' => '<span class="px-2 py-1 text-xs rounded bg-blue-100 text-blue-800">处理中</span>',
  59. 'completed' => '<span class="px-2 py-1 text-xs rounded bg-green-100 text-green-800">已完成</span>',
  60. 'failed' => '<span class="px-2 py-1 text-xs rounded bg-red-100 text-red-800">失败</span>',
  61. default => '<span class="px-2 py-1 text-xs rounded bg-gray-100 text-gray-800">未知</span>',
  62. };
  63. }
  64. public function getImageUrlAttribute(): string
  65. {
  66. if ($this->image_path && file_exists(public_path($this->image_path))) {
  67. return asset($this->image_path);
  68. }
  69. return '';
  70. }
  71. public function getProgressPercentageAttribute(): int
  72. {
  73. if ($this->total_questions === 0) {
  74. return 0;
  75. }
  76. return intval(($this->processed_questions / $this->total_questions) * 100);
  77. }
  78. }