OCRRecord.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. 'student_id',
  13. 'paper_title',
  14. 'paper_type',
  15. 'file_path',
  16. 'image_count',
  17. 'total_questions',
  18. 'status',
  19. 'error_message',
  20. 'created_at',
  21. 'updated_at',
  22. 'analysis_id',
  23. 'image_path', // 添加兼容性字段访问器
  24. ];
  25. protected $casts = [
  26. 'created_at' => 'datetime',
  27. 'updated_at' => 'datetime',
  28. 'image_count' => 'integer',
  29. 'total_questions' => 'integer',
  30. ];
  31. public function questions(): HasMany
  32. {
  33. return $this->hasMany(OCRQuestionResult::class, 'ocr_record_id', 'id');
  34. }
  35. public function ocrRawData()
  36. {
  37. return $this->hasOne(\App\Models\OCRRawData::class, 'ocr_record_id', 'id');
  38. }
  39. public function student()
  40. {
  41. return $this->belongsTo(Student::class, 'user_id', 'student_id');
  42. }
  43. public function getStatusBadgeAttribute(): string
  44. {
  45. return match ($this->status) {
  46. 'pending' => '<span class="px-2 py-1 text-xs rounded bg-gray-100 text-gray-800">待处理</span>',
  47. 'processing' => '<span class="px-2 py-1 text-xs rounded bg-blue-100 text-blue-800">处理中</span>',
  48. 'completed' => '<span class="px-2 py-1 text-xs rounded bg-green-100 text-green-800">已完成</span>',
  49. 'failed' => '<span class="px-2 py-1 text-xs rounded bg-red-100 text-red-800">失败</span>',
  50. default => '<span class="px-2 py-1 text-xs rounded bg-gray-100 text-gray-800">未知</span>',
  51. };
  52. }
  53. // 兼容性方法:获取 student_id
  54. public function getStudentIdAttribute(): ?string
  55. {
  56. return $this->user_id;
  57. }
  58. // 兼容性方法:设置 student_id
  59. public function setStudentIdAttribute(?string $value): void
  60. {
  61. // 同时设置两个字段以保持数据一致性
  62. $this->attributes['student_id'] = $value;
  63. $this->attributes['user_id'] = $value;
  64. }
  65. public function getImagePathAttribute(): string
  66. {
  67. // 将 file_path 映射为 image_path 以保持兼容性
  68. return $this->file_path ?? '';
  69. }
  70. public function getImageUrlAttribute(): string
  71. {
  72. // 使用 file_path(通过上面的访问器也可以通过 image_path 访问)
  73. $path = $this->file_path;
  74. if ($path && file_exists(public_path($path))) {
  75. return asset($path);
  76. }
  77. return '';
  78. }
  79. /**
  80. * 获取处理完成时间(确保返回 Carbon 对象)
  81. */
  82. public function getProcessedAtAttribute($value): ?\Carbon\Carbon
  83. {
  84. if (empty($value)) {
  85. return null;
  86. }
  87. // 如果已经是 Carbon 实例,直接返回
  88. if ($value instanceof \Carbon\Carbon) {
  89. return $value;
  90. }
  91. // 如果是字符串,解析为 Carbon 实例
  92. if (is_string($value)) {
  93. return \Carbon\Carbon::parse($value);
  94. }
  95. return null;
  96. }
  97. public function getProgressPercentageAttribute(): int
  98. {
  99. if ($this->total_questions === 0) {
  100. return 0;
  101. }
  102. return intval(($this->processed_questions / $this->total_questions) * 100);
  103. }
  104. public function getPaperTypeLabelAttribute(): string
  105. {
  106. return match($this->paper_type) {
  107. 'unit_test' => '单元测试',
  108. 'midterm' => '期中考试',
  109. 'final' => '期末考试',
  110. 'homework' => '家庭作业',
  111. 'quiz' => '随堂测验',
  112. 'other' => '其他',
  113. default => '未分类',
  114. };
  115. }
  116. }