65536) throw new RuntimeException('导入文件或清单无效。'); $meta = json_decode(file_get_contents($manifestPath), true, 32, JSON_THROW_ON_ERROR); foreach (['source_month', 'source_filename', 'source_sha256', 'data_sha256', 'row_count', 'freight_column'] as $field) { if (!isset($meta[$field])) throw new RuntimeException('导入清单缺少字段:' . $field); } if (($meta['schema_version'] ?? null) !== 1 || !preg_match('/^\d{4}-(0[1-9]|1[0-2])$/D', $meta['source_month']) || !preg_match('/^[a-f0-9]{64}$/D', $meta['source_sha256']) || !preg_match('/^[a-f0-9]{64}$/D', $meta['data_sha256']) || !is_int($meta['row_count']) || $meta['row_count'] < 1 || !in_array($meta['freight_column'], ['运费', '系统运费'], true) || !hash_equals($meta['data_sha256'], hash_file('sha256', $path))) { throw new RuntimeException('导入清单或文件摘要校验失败。'); } $db = $this->databases->connection((string) config('upstream_mirrors.connection', 'business_mirror')); $month = $meta['source_month'] . '-01'; $hash = hash('sha256', json_encode($meta, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR)); $lock = fopen(storage_path('framework/sb-shipping-import.lock'), 'c'); if ($lock === false || !flock($lock, LOCK_EX | LOCK_NB)) throw new RuntimeException('另一个发货数据导入正在运行。'); $stream = null; $generation = null; try { $state = $db->table(self::MONTHS)->where('source_month', $month)->first(); if (!$dryRun && $state && $state->active_sha256 === $hash) return ['status' => 'unchanged', 'rows' => (int) $state->active_rows]; $generation = $state && $state->pending_sha256 === $hash && $state->pending_generation ? $state->pending_generation : (string) Str::uuid(); $checkpoint = $state && $state->pending_generation === $generation ? (int) $state->checkpoint : 0; if (!$dryRun) { $db->table(self::MONTHS)->updateOrInsert(['source_month' => $month], [ 'status' => 'running', 'pending_generation' => $generation, 'pending_sha256' => $hash, 'checkpoint' => $checkpoint, 'updated_at' => now(), ]); } $stream = fopen('compress.zlib://' . $path, 'rb'); if ($stream === false || fgetcsv($stream) !== self::HEADERS) throw new RuntimeException('精简发货文件表头无效。'); $count = 0; $previous = 1; $batch = []; $refs = []; while (($values = fgetcsv($stream)) !== false) { if (count($values) !== count(self::HEADERS)) throw new RuntimeException('精简发货文件列数无效。'); $record = array_combine(self::HEADERS, $values); $sourceRow = filter_var($record['source_row'], FILTER_VALIDATE_INT); if ($sourceRow === false || $sourceRow <= $previous) throw new RuntimeException('来源行号必须递增。'); $previous = $sourceRow; $count++; if ($count > $meta['row_count']) throw new RuntimeException('文件行数超过清单。'); foreach (['qty', 'freight', 'unshipped'] as $field) { if (mb_strlen($record[$field]) > 64) throw new RuntimeException('来源字段超长:' . $field); } // 空值和错误值原样保存,核价时待确认,不把缺失金额改成0。 try { $date = $record['ship_time'] !== '' ? Carbon::parse($record['ship_time'])->format('Y-m-d H:i:s') : null; } catch (Throwable) { $date = null; } if ($date !== null && substr($date, 0, 7) !== $meta['source_month']) throw new RuntimeException('发货日期超出清单月份,须核对来源。'); $recordRefs = []; foreach (['tracking' => array_merge(Valuation::numbers($record['express_no']), Valuation::numbers($record['old_express_no'])), 'order' => Valuation::numbers($record['trade_no'])] as $kind => $numbers) { if (count($numbers) > 100) throw new RuntimeException('单行关联单号过多,须核对来源。'); foreach (array_unique($numbers) as $number) { if (mb_strlen($number) > 160) throw new RuntimeException('关联单号超长。'); $recordRefs[] = ['generation' => $generation, 'source_row' => $sourceRow, 'kind' => $kind, 'reference' => $number]; } } if ($dryRun || $count <= $checkpoint) continue; unset($record['product_id']); $record['generation'] = $generation; $record['source_row'] = $sourceRow; $record['ship_time'] = $date; $batch[] = $record; array_push($refs, ...$recordRefs); if (count($batch) >= 500) { $this->flush($db, $month, $count, $batch, $refs); $batch = $refs = []; } } if ($count !== $meta['row_count']) throw new RuntimeException('文件行数与清单不一致,未切换可见数据。'); if (!$dryRun) { if ($batch !== []) $this->flush($db, $month, $count, $batch, $refs); $db->table(self::MONTHS)->where('source_month', $month)->update([ 'status' => 'succeeded', 'active_generation' => $generation, 'active_sha256' => $hash, 'active_rows' => $count, 'pending_generation' => null, 'pending_sha256' => null, 'checkpoint' => 0, 'source_filename' => basename($meta['source_filename']), 'source_sha256' => $meta['source_sha256'], 'freight_column' => $meta['freight_column'], 'last_succeeded_at' => now(), 'updated_at' => now(), ]); } return ['status' => $dryRun ? 'validated' : 'succeeded', 'rows' => $count, 'resumed_from' => $checkpoint]; } catch (Throwable $e) { if (!$dryRun && $generation !== null) $db->table(self::MONTHS)->where('source_month', $month)->where('pending_generation', $generation)->update(['status' => 'failed', 'updated_at' => now()]); throw $e; } finally { if (is_resource($stream)) fclose($stream); flock($lock, LOCK_UN); fclose($lock); } } private function flush($db, string $month, int $checkpoint, array $rows, array $refs): void { $db->transaction(function () use ($db, $month, $checkpoint, $rows, $refs): void { $db->table(self::ROWS)->insert($rows); foreach (array_chunk($refs, 1000) as $chunk) $db->table(self::REFS)->insert($chunk); $db->table(self::MONTHS)->where('source_month', $month)->update(['checkpoint' => $checkpoint, 'updated_at' => now()]); }); } }