OCRRawData.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Database\Eloquent\Factories\HasFactory;
  5. class OCRRawData extends Model
  6. {
  7. use HasFactory;
  8. protected $table = 'ocr_raw_data';
  9. protected $fillable = [
  10. 'ocr_record_id',
  11. 'raw_response',
  12. 'parsed_blocks',
  13. 'api_request_id',
  14. 'algo_version',
  15. 'total_blocks',
  16. 'metadata',
  17. ];
  18. protected $casts = [
  19. 'raw_response' => 'array',
  20. 'parsed_blocks' => 'array',
  21. 'metadata' => 'array',
  22. ];
  23. /**
  24. * 关联OCR记录
  25. */
  26. public function ocrRecord()
  27. {
  28. return $this->belongsTo(OCRRecord::class);
  29. }
  30. /**
  31. * 获取文本块数据
  32. */
  33. public function getTextBlocks(): array
  34. {
  35. return $this->parsed_blocks ?? [];
  36. }
  37. /**
  38. * 保存原始API响应
  39. */
  40. public static function saveRawResponse(int $ocrRecordId, array $response): self
  41. {
  42. // 提取文本块
  43. $blocks = [];
  44. $totalBlocks = 0;
  45. $requestId = null;
  46. $algoVersion = null;
  47. if (isset($response['data'])) {
  48. $requestId = $response['requestId'] ?? null;
  49. $algoVersion = $response['data']['algo_version'] ?? null;
  50. if (isset($response['data']['page_list'])) {
  51. foreach ($response['data']['page_list'] as $page) {
  52. if (isset($page['answer_list'])) {
  53. foreach ($page['answer_list'] as $item) {
  54. if (isset($item['content_list_info'])) {
  55. foreach ($item['content_list_info'] as $content) {
  56. if (isset($content['text']) && !empty(trim($content['text']))) {
  57. $blocks[] = [
  58. 'text' => trim($content['text']),
  59. 'position' => $content['pos'] ?? null,
  60. 'confidence' => $content['confidence'] ?? null,
  61. 'doc_index' => $content['doc_index'] ?? null,
  62. 'type' => null // 将在OCRDataParser中识别
  63. ];
  64. $totalBlocks++;
  65. }
  66. }
  67. }
  68. }
  69. }
  70. }
  71. }
  72. }
  73. return self::create([
  74. 'ocr_record_id' => $ocrRecordId,
  75. 'raw_response' => $response,
  76. 'parsed_blocks' => $blocks,
  77. 'api_request_id' => $requestId,
  78. 'algo_version' => $algoVersion,
  79. 'total_blocks' => $totalBlocks,
  80. 'metadata' => [
  81. 'saved_at' => now()->toISOString(),
  82. 'page_count' => count($response['data']['page_list'] ?? []),
  83. ]
  84. ]);
  85. }
  86. }