, * store_count: int, * labels: array, * updated: int, * unchanged: int, * file_sha256: string * } */ public function import( string $path, string $period, ?string $sheetName = null, ?string $labelColumn = null, int $companyId = 1, bool $dryRun = false, ): array { if (preg_match('/^\d{4}-(0[1-9]|1[0-2])$/', $period) !== 1) { throw new InvalidArgumentException('账期格式必须为 YYYY-MM。'); } if (! is_file($path) || ! is_readable($path)) { throw new InvalidArgumentException('月度过账标签文件不存在或不可读。'); } $connection = $this->connection(); if (! Schema::connection($this->connectionName())->hasTable('kuka_posting_base_rows')) { throw new RuntimeException('顾家过账基础数据表尚未建立,请先执行数据库迁移。'); } $allowedStores = array_values(array_filter(array_map( static fn (mixed $store): string => trim((string) $store), (array) config('kuka_posting.stores', []), ))); if ($allowedStores === []) { throw new RuntimeException('顾家过账基础数据店铺范围未配置。'); } $labelColumn = trim((string) ($labelColumn ?? '')); if ($labelColumn === '') { $labelColumn = CarbonImmutable::createFromFormat('!Y-m', $period) ->format('Y年n月过账'); } $sheetName = trim((string) ($sheetName ?? '')); if ($sheetName === '') { $sheetName = 'Sheet1'; } $fileHash = hash_file('sha256', $path); if (! is_string($fileHash)) { throw new RuntimeException('无法计算月度过账标签文件摘要。'); } $records = $this->readRecords( $path, $sheetName, $labelColumn, $allowedStores, ); if ($dryRun) { return $this->validateRows( $connection, $records, $period, $labelColumn, $companyId, false, ) + [ 'dry_run' => true, 'file_sha256' => strtolower($fileHash), ]; } $result = []; $connection->transaction(function () use ( &$result, $connection, $records, $period, $labelColumn, $companyId, ): void { $result = $this->validateRows( $connection, $records, $period, $labelColumn, $companyId, true, ); $this->applyLabels($connection, $records, $period, $companyId); }); return $result + [ 'dry_run' => false, 'file_sha256' => strtolower($fileHash), ]; } /** * @param list $allowedStores * @return list}> */ private function readRecords( string $path, string $sheetName, string $labelColumn, array $allowedStores, ): array { $records = []; $excelRowNumber = 1; foreach ($this->reader->rows( $path, array_merge([$labelColumn], self::IDENTITY_FIELDS), [], $sheetName, ) as $values) { $excelRowNumber++; $label = trim((string) ($values[$labelColumn] ?? '')); if (! in_array($label, self::ALLOWED_LABELS, true)) { throw new RuntimeException(sprintf( 'Excel 第 %d 行的“%s”只能为“%s”,当前值为“%s”。', $excelRowNumber, $labelColumn, implode('”或“', self::ALLOWED_LABELS), $label, )); } $identity = []; foreach (self::IDENTITY_FIELDS as $field) { $identity[$field] = trim((string) ($values[$field] ?? '')); } if (! in_array($identity['店铺'], $allowedStores, true)) { throw new RuntimeException(sprintf( 'Excel 第 %d 行的店铺“%s”不在顾家过账基础数据范围内。', $excelRowNumber, $identity['店铺'], )); } $records[] = [ 'excel_row_number' => $excelRowNumber, 'label' => $label, 'identity' => $identity, ]; } if ($records === []) { throw new RuntimeException('月度过账标签文件没有数据行。'); } return $records; } /** * @param list}> $records * @return array{ * period: string, * label_column: string, * rows: int, * stores: array, * store_count: int, * labels: array, * updated: int, * unchanged: int * } */ private function validateRows( ConnectionInterface $connection, array $records, string $period, string $labelColumn, int $companyId, bool $lockRows, ): array { $expectedRows = count($records); $databaseRows = $this->baseRowsQuery($connection, $period, $companyId) ->count(); if ($databaseRows !== $expectedRows) { throw new RuntimeException(sprintf( '月度过账标签文件有 %s 行,但共享镜像 %s 有 %s 行,行数不一致,未写入任何标签。', number_format($expectedRows), $period, number_format($databaseRows), )); } $labels = array_fill_keys(self::ALLOWED_LABELS, 0); $stores = []; $updated = 0; $unchanged = 0; $index = 0; $query = $this->baseRowsQuery($connection, $period, $companyId) ->orderBy('source_row_number'); if ($lockRows) { $query->lockForUpdate(); } foreach ($query->cursor() as $row) { $record = $records[$index] ?? null; if ($record === null) { throw new RuntimeException('共享镜像行数多于月度过账标签文件,未写入任何标签。'); } $sourceValues = $this->decodeSourceValues($row); $this->assertIdentity($row, $sourceValues, $record); $label = $record['label']; $labels[$label]++; $store = $record['identity']['店铺']; $stores[$store] = ($stores[$store] ?? 0) + 1; if (trim((string) ($sourceValues[self::LABEL_FIELD] ?? '')) === $label) { $unchanged++; } else { $updated++; } $index++; } if ($index !== $expectedRows) { throw new RuntimeException('共享镜像行数少于月度过账标签文件,未写入任何标签。'); } ksort($stores); return [ 'period' => $period, 'label_column' => $labelColumn, 'rows' => $expectedRows, 'stores' => $stores, 'store_count' => count($stores), 'labels' => $labels, 'updated' => $updated, 'unchanged' => $unchanged, ]; } /** * @param list}> $records */ private function applyLabels( ConnectionInterface $connection, array $records, string $period, int $companyId, ): void { $index = 0; $batch = []; $flush = static function () use (&$batch, $connection): void { if ($batch === []) { return; } $connection->table('kuka_posting_base_rows')->upsert( $batch, ['company_id', 'period', 'source_row_number'], ['source_values', 'row_hash'], ); $batch = []; }; foreach ($this->baseRowsQuery($connection, $period, $companyId) ->orderBy('source_row_number') ->cursor() as $row) { $record = $records[$index] ?? null; if ($record === null) { throw new RuntimeException('共享镜像行数多于月度过账标签文件,标签写入已回滚。'); } $sourceValues = $this->decodeSourceValues($row); $this->assertIdentity($row, $sourceValues, $record); $index++; if (trim((string) ($sourceValues[self::LABEL_FIELD] ?? '')) === $record['label']) { continue; } $sourceValues[self::LABEL_FIELD] = $record['label']; $encoded = json_encode( $sourceValues, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR, ); $batch[] = [ 'company_id' => $companyId, 'period' => $period, 'source_row_number' => (int) $row->source_row_number, 'store_name' => $row->store_name, 'internal_order_no' => $row->internal_order_no, 'online_order_no' => $row->online_order_no, 'product_code' => $row->product_code, 'source_values' => $encoded, 'row_hash' => hash('sha256', $encoded), 'imported_at' => $row->imported_at, ]; if (count($batch) >= self::UPDATE_CHUNK) { $flush(); } } if ($index !== count($records)) { throw new RuntimeException('共享镜像行数少于月度过账标签文件,标签写入已回滚。'); } $flush(); } /** * @return array */ private function decodeSourceValues(object $row): array { $values = json_decode( (string) $row->source_values, true, 512, JSON_THROW_ON_ERROR, ); if (! is_array($values)) { throw new RuntimeException(sprintf( '共享镜像 source_row_number=%d 的源数据不是有效对象。', (int) $row->source_row_number, )); } return $values; } /** * @param array $sourceValues * @param array{excel_row_number: int, label: string, identity: array} $record */ private function assertIdentity(object $row, array $sourceValues, array $record): void { $databaseIdentity = [ '店铺' => trim((string) ($sourceValues['店铺'] ?? '')), '内部订单号' => trim((string) ($sourceValues['内部订单号'] ?? '')), '线上订单号' => trim((string) ($sourceValues['线上订单号'] ?? '')), '商品编码' => trim((string) ($sourceValues['商品编码'] ?? '')), '原始线上订单号' => trim((string) ($sourceValues['原始线上订单号'] ?? '')), ]; foreach ($databaseIdentity as $field => $value) { if ($value !== $record['identity'][$field]) { throw new RuntimeException(sprintf( 'Excel 第 %d 行与共享镜像 source_row_number=%d 的“%s”不一致,未写入任何标签。', $record['excel_row_number'], (int) $row->source_row_number, $field, )); } } // 顶层订单字段是页面搜索用的冗余索引,历史数据可能保留旧值。 // 行身份以 source_values 中的业务源字段为准,本命令不会改动这些索引字段。 } private function baseRowsQuery( ConnectionInterface $connection, string $period, int $companyId, ) { return $connection->table('kuka_posting_base_rows') ->where('company_id', $companyId) ->where('period', $period) ->select([ 'id', 'source_row_number', 'store_name', 'internal_order_no', 'online_order_no', 'product_code', 'source_values', 'imported_at', ]); } private function connection(): ConnectionInterface { return $this->databases->connection($this->connectionName()); } private function connectionName(): string { return (string) config('upstream_mirrors.connection', 'business_mirror'); } }