81 lines
2.8 KiB
PHP
81 lines
2.8 KiB
PHP
<?php
|
||
|
||
namespace Modules\EcommerceFinance\Console;
|
||
|
||
use Illuminate\Console\Command;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Modules\EcommerceFinance\Services\ShengsuanOrderProfitRowMapper;
|
||
|
||
class ImportShengsuanOrderProfitRows extends Command
|
||
{
|
||
protected $signature = 'order-profit:import-shengsuan-rows
|
||
{--file= : 聚胜算订单利润接口返回的 JSON 文件路径}
|
||
{--stdin : 从标准输入读取 JSON,忽略 --file}';
|
||
|
||
protected $description = '把聚胜算订单利润页面的完整结果导入共享镜像,供订单利润页按订单原样展示';
|
||
|
||
public function handle(ShengsuanOrderProfitRowMapper $mapper): int
|
||
{
|
||
$path = trim((string) $this->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;
|
||
}
|
||
}
|