OCRRecord.php 3.9 KB

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