connection = $database->connection( (string) config('upstream_mirrors.connection', 'business_mirror'), ); } /** * @return array{flows:int,files:int,stored:int,skipped:int,errors:list} */ public function import( string $manifestPath, string $month, string $baseDirectory, string $scope = 'personal_permission', ): array { if (preg_match('/^\d{4}-\d{2}$/', $month) !== 1) { throw new RuntimeException('月份格式应为 YYYY-MM。'); } $manifest = json_decode((string) file_get_contents($manifestPath), true); if (! is_array($manifest)) { throw new RuntimeException('附件清单不是有效 JSON。'); } $base = realpath($baseDirectory); if ($base === false) { throw new RuntimeException('附件基础目录不存在。'); } $manifestRows = array_is_list($manifest) ? $manifest : (array) ($manifest['flows'] ?? []); $businessIds = array_values(array_filter(array_map( static fn (mixed $row): string => is_array($row) ? trim((string) ($row['business_id'] ?? $row['审批编号'] ?? '')) : '', $manifestRows, ))); $flows = $this->connection->table('ding_invoice_flow_mirror') ->where('source_scope', $scope) ->whereIn('business_id', $businessIds) ->get() ->keyBy('business_id'); $result = [ 'flows' => 0, 'files' => 0, 'stored' => 0, 'skipped' => 0, 'errors' => [], ]; $seenFlows = []; foreach ($manifestRows as $row) { if (! is_array($row)) { continue; } $businessId = trim((string) ($row['business_id'] ?? $row['审批编号'] ?? '')); $flow = $flows->get($businessId); if ($flow === null) { $result['skipped']++; continue; } $seenFlows[$businessId] = true; foreach ((array) ($row['files'] ?? []) as $file) { if (! is_array($file)) { continue; } $result['files']++; $relativePath = trim((string) ($file['relative_path'] ?? $file['path'] ?? '')); $candidate = str_starts_with($relativePath, DIRECTORY_SEPARATOR) ? $relativePath : $base . DIRECTORY_SEPARATOR . $relativePath; $realPath = realpath($candidate); if ($realPath === false || ! is_file($realPath) || ! str_starts_with($realPath, $base . DIRECTORY_SEPARATOR)) { $result['skipped']++; $result['errors'][] = $businessId . ':附件不存在或超出基础目录'; continue; } $fileName = trim((string) ( $file['file_name'] ?? $file['name'] ?? basename($realPath) )); $extension = strtolower((string) pathinfo($fileName, PATHINFO_EXTENSION)); $hash = hash_file('sha256', $realPath) ?: null; $preview = null; try { $preview = $this->previewStore->persistLocalFile( $realPath, [ 'file_name' => $fileName, 'file_type' => $extension, ], $hash, ); $this->attachmentStore->record( $flow, [ 'file_id' => (string) ($file['file_id'] ?? ''), 'file_name' => $fileName, 'file_type' => $extension, 'source_field' => (string) ($file['source_field'] ?? $file['source'] ?? '历史附件'), 'source_path' => 'local-import:' . substr($hash ?? '', 0, 16), 'file_size' => filesize($realPath) ?: 0, 'downloaded_at' => now(), ], 'local_import', $preview, 'pending', 0, '', null, $hash, ); $result['stored']++; } catch (Throwable $exception) { report($exception); $result['errors'][] = $businessId . ':' . $exception->getMessage(); } } } $result['flows'] = count($seenFlows); return $result; } }