OCRRecord.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. 'image_path', // 添加兼容性字段访问器
  23. ];
  24. protected $casts = [
  25. 'created_at' => 'datetime',
  26. 'updated_at' => 'datetime',
  27. 'image_count' => 'integer',
  28. 'total_questions' => 'integer',
  29. ];
  30. public function questions(): HasMany
  31. {
  32. return $this->hasMany(OCRQuestionResult::class, 'ocr_record_id', 'id');
  33. }
  34. public function student()
  35. {
  36. return $this->belongsTo(Student::class, 'user_id', 'student_id');
  37. }
  38. public function getStatusBadgeAttribute(): string
  39. {
  40. return match ($this->status) {
  41. 'pending' => '<span class="px-2 py-1 text-xs rounded bg-gray-100 text-gray-800">待处理</span>',
  42. 'processing' => '<span class="px-2 py-1 text-xs rounded bg-blue-100 text-blue-800">处理中</span>',
  43. 'completed' => '<span class="px-2 py-1 text-xs rounded bg-green-100 text-green-800">已完成</span>',
  44. 'failed' => '<span class="px-2 py-1 text-xs rounded bg-red-100 text-red-800">失败</span>',
  45. default => '<span class="px-2 py-1 text-xs rounded bg-gray-100 text-gray-800">未知</span>',
  46. };
  47. }
  48. // 兼容性方法:获取 student_id
  49. public function getStudentIdAttribute(): ?string
  50. {
  51. return $this->user_id;
  52. }
  53. // 兼容性方法:设置 student_id
  54. public function setStudentIdAttribute(?string $value): void
  55. {
  56. $this->attributes['user_id'] = $value;
  57. }
  58. public function getImagePathAttribute(): string
  59. {
  60. // 将 file_path 映射为 image_path 以保持兼容性
  61. return $this->file_path ?? '';
  62. }
  63. public function getImageUrlAttribute(): string
  64. {
  65. // 使用 file_path(通过上面的访问器也可以通过 image_path 访问)
  66. $path = $this->file_path;
  67. if ($path && file_exists(public_path($path))) {
  68. return asset($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. }