connection = $database->connection( (string) config('upstream_mirrors.connection', 'business_mirror'), ); } /** * Persist the source file even when OCR does not find an invoice. * * @param array $file * @param array{path:string,mime:string,extension:string}|null $preview * @param list $derivedPreviews */ public function record( object $flow, array $file, string $origin, ?array $preview, string $ocrStatus, int $invoiceCount, string $ocrProvider, ?string $error, ?string $hash = null, array $derivedPreviews = [], ): void { $scope = (string) $flow->source_scope; $businessId = (string) $flow->business_id; $fileId = trim((string) ($file['file_id'] ?? '')); $fileName = trim((string) ($file['file_name'] ?? 'OA附件')); $fileType = strtolower(trim((string) ($file['file_type'] ?? ''))); $sourceField = trim((string) ($file['source_field'] ?? '')); $sourcePath = trim((string) ($file['source_path'] ?? '')); $baseKey = hash('sha256', implode('|', [ $origin, $fileId, $fileName, $sourceField, $hash ?? '', ])); $now = now(); $downloadedAt = $file['downloaded_at'] ?? $now; $baseRow = [ 'source_scope' => $scope, 'business_id' => $businessId, 'paid_month' => (string) ($flow->paid_month ?? ''), 'attachment_key' => $baseKey, 'process_instance_id' => mb_substr(trim((string) ( $file['process_instance_id'] ?? $flow->process_instance_id ?? '' )), 0, 100) ?: null, 'source_file_id' => mb_substr($fileId, 0, 100) ?: null, 'source_file_name' => mb_substr($fileName, 0, 255), 'source_file_type' => mb_substr($fileType, 0, 32) ?: null, 'source_field' => mb_substr($sourceField, 0, 255) ?: null, 'source_origin' => mb_substr($origin, 0, 24), 'source_size' => max(0, (int) ($file['file_size'] ?? 0)), 'source_hash' => $hash === null ? null : mb_substr($hash, 0, 64), 'source_path' => mb_substr($sourcePath, 0, 1024) ?: null, 'preview_path' => $preview['path'] ?? null, 'preview_mime' => $preview['mime'] ?? null, 'ocr_status' => mb_substr($ocrStatus, 0, 24), 'ocr_provider' => mb_substr($ocrProvider, 0, 100) ?: null, 'invoice_count' => max(0, $invoiceCount), 'last_error' => $error === null ? null : mb_substr($error, 0, 4000), 'downloaded_at' => $downloadedAt, 'updated_at' => $now, ]; $this->connection->table('ding_invoice_attachment_mirror')->updateOrInsert( [ 'source_scope' => $scope, 'business_id' => $businessId, 'attachment_key' => $baseKey, ], [...$baseRow, 'created_at' => $now], ); foreach ($derivedPreviews as $index => $derived) { if (! isset($derived['path'], $derived['mime'])) { continue; } $extension = strtolower((string) ($derived['extension'] ?? '')); $baseName = pathinfo($fileName, PATHINFO_FILENAME) ?: 'OA附件'; $displayName = $extension === 'pdf' ? $baseName . '.pdf' : $baseName . ' · 图片' . ($index + 1) . '.' . ($extension ?: 'jpg'); $derivedKey = hash('sha256', $baseKey . '|preview|' . $derived['path']); $this->connection->table('ding_invoice_attachment_mirror')->updateOrInsert( [ 'source_scope' => $scope, 'business_id' => $businessId, 'attachment_key' => $derivedKey, ], [ ...$baseRow, 'attachment_key' => $derivedKey, 'source_file_id' => mb_substr($fileId . ':preview:' . $index, 0, 100) ?: null, 'source_file_name' => mb_substr($displayName, 0, 255), 'source_file_type' => mb_substr($extension, 0, 32) ?: null, 'source_size' => 0, 'source_hash' => null, 'preview_path' => $derived['path'], 'preview_mime' => $derived['mime'], 'last_error' => null, 'created_at' => $now, ], ); } } }