| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <?php
- namespace App\Models;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- class OCRRawData extends Model
- {
- use HasFactory;
- protected $table = 'ocr_raw_data';
- protected $fillable = [
- 'ocr_record_id',
- 'raw_response',
- 'parsed_blocks',
- 'api_request_id',
- 'algo_version',
- 'total_blocks',
- 'metadata',
- ];
- protected $casts = [
- 'raw_response' => 'array',
- 'parsed_blocks' => 'array',
- 'metadata' => 'array',
- ];
- /**
- * 关联OCR记录
- */
- public function ocrRecord()
- {
- return $this->belongsTo(OCRRecord::class);
- }
- /**
- * 获取文本块数据
- */
- public function getTextBlocks(): array
- {
- return $this->parsed_blocks ?? [];
- }
- /**
- * 保存原始API响应
- */
- public static function saveRawResponse(int $ocrRecordId, array $response): self
- {
- // 提取文本块
- $blocks = [];
- $totalBlocks = 0;
- $requestId = null;
- $algoVersion = null;
- if (isset($response['data'])) {
- $requestId = $response['requestId'] ?? null;
- $algoVersion = $response['data']['algo_version'] ?? null;
- if (isset($response['data']['page_list'])) {
- foreach ($response['data']['page_list'] as $page) {
- if (isset($page['answer_list'])) {
- foreach ($page['answer_list'] as $item) {
- if (isset($item['content_list_info'])) {
- foreach ($item['content_list_info'] as $content) {
- if (isset($content['text']) && !empty(trim($content['text']))) {
- $blocks[] = [
- 'text' => trim($content['text']),
- 'position' => $content['pos'] ?? null,
- 'confidence' => $content['confidence'] ?? null,
- 'doc_index' => $content['doc_index'] ?? null,
- 'type' => null // 将在OCRDataParser中识别
- ];
- $totalBlocks++;
- }
- }
- }
- }
- }
- }
- }
- }
- return self::create([
- 'ocr_record_id' => $ocrRecordId,
- 'raw_response' => $response,
- 'parsed_blocks' => $blocks,
- 'api_request_id' => $requestId,
- 'algo_version' => $algoVersion,
- 'total_blocks' => $totalBlocks,
- 'metadata' => [
- 'saved_at' => now()->toISOString(),
- 'page_count' => count($response['data']['page_list'] ?? []),
- ]
- ]);
- }
- }
|