option('file')); $fromStdin = (bool) $this->option('stdin'); try { $raw = $fromStdin ? stream_get_contents(STDIN) : (($path !== '' && is_file($path)) ? file_get_contents($path) : ''); } catch (\Throwable) { $raw = ''; } if ($raw === '') { $this->error('未读取到聚胜算订单利润 JSON。'); return self::FAILURE; } try { $decoded = json_decode($raw, true, 512, JSON_THROW_ON_ERROR); } catch (\JsonException) { $this->error('聚胜算订单利润 JSON 无法解析。'); return self::FAILURE; } $rows = $mapper->rows($decoded); if ($rows === []) { $this->warn('JSON 中没有订单利润数据行。'); return self::FAILURE; } $connection = DB::connection((string) config('upstream_mirrors.connection', 'business_mirror')); $connection->beginTransaction(); try { foreach (array_chunk($rows, 200) as $chunk) { $connection->table($mapper->table())->upsert( $chunk, ['order_key'], [ 'shop_name', 'online_order_no', 'internal_order_no', 'ship_time', 'product_cost', 'matched_package_fee', 'matched_freight_fee', 'matched_bill_fee', 'generated_classification_fee', 'generated_product_fee', 'manual_fee', 'order_fee', 'promotion_fee', 'source_payload', 'synced_at', 'updated_at', ], ); } $connection->commit(); } catch (\Throwable $exception) { $connection->rollBack(); report($exception); $this->error('订单利润页面结果导入失败,已回滚。'); return self::FAILURE; } $this->info(sprintf('订单利润页面结果导入完成:%d 条。', count($rows))); return self::SUCCESS; } }