- 副本库 rpt.jst_sku 一商品一行、无历史;开放平台操作日志接口暂无授权 - 新增 JushuitanCostPriceSnapshotDiffer:ops_jst_sku 当前成本价与快照比对, 变化写入 jushuitan_cost_price_history_rows(变更前/后/差额,时间取聚水潭 modified) - 新表 jushuitan_cost_price_snapshots(迁移 + 运行时幂等建表) - business-mirror:sync-jushuitan-cost-price-history 默认走 snapshot, JUSHUITAN_COST_PRICE_HISTORY_SOURCE=open_api 可切回接口 - 生产 cron 由每日 00:00 改为每 10 分钟 - 页面“聚水潭成本价记录”列显示最近一次变价(前→后 · 时间) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0192A1X1EDtdzpaAVMhBj2nh
104 lines
4.3 KiB
PHP
104 lines
4.3 KiB
PHP
<?php
|
||
|
||
namespace App\Console\Commands;
|
||
|
||
use Illuminate\Console\Command;
|
||
use Illuminate\Support\Facades\Log;
|
||
use Modules\EcommerceFinance\Repositories\JushuitanCostPriceHistoryMirrorRepository;
|
||
use Modules\EcommerceFinance\Services\JushuitanCostPriceHistorySynchronizer;
|
||
use Modules\EcommerceFinance\Services\JushuitanCostPriceSnapshotDiffer;
|
||
use Throwable;
|
||
|
||
class SyncJushuitanCostPriceHistory extends Command
|
||
{
|
||
protected $signature = 'business-mirror:sync-jushuitan-cost-price-history
|
||
{--full : 强制启动或继续首次全量同步}
|
||
{--page-size= : 聚水潭接口每页读取条数,最大 2000}
|
||
{--max-pages= : 本轮最多读取页数,0 表示直到完成}';
|
||
|
||
protected $description = '记录聚水潭成本价变更:默认按普通商品镜像快照比对,可切换为开放平台操作日志';
|
||
|
||
public function handle(
|
||
JushuitanCostPriceHistoryMirrorRepository $mirror,
|
||
JushuitanCostPriceHistorySynchronizer $synchronizer,
|
||
JushuitanCostPriceSnapshotDiffer $snapshotDiffer,
|
||
): int {
|
||
if (! (bool) config('upstream_mirrors.refresh_enabled', false) && ! $this->option('full')) {
|
||
return self::SUCCESS;
|
||
}
|
||
|
||
// 默认走“镜像快照比对”:开放平台操作日志接口暂无授权时也能记录成本价变化。
|
||
if ((string) config('services.jushuitan.cost_price_history_source', 'snapshot') === 'snapshot') {
|
||
return $this->runSnapshot($snapshotDiffer);
|
||
}
|
||
|
||
try {
|
||
$state = $mirror->state();
|
||
$full = $this->shouldRunFull($state);
|
||
$result = $synchronizer->sync(
|
||
$full,
|
||
$this->option('page-size') === null ? null : (int) $this->option('page-size'),
|
||
$this->option('max-pages') === null ? null : (int) $this->option('max-pages'),
|
||
);
|
||
} catch (Throwable $exception) {
|
||
Log::error('聚水潭历史成本价镜像同步失败。', ['exception' => $exception::class]);
|
||
$this->error('聚水潭成本价历史同步失败,已保留上一版共享数据。');
|
||
|
||
return self::FAILURE;
|
||
}
|
||
|
||
if ($result['status'] === 'requires_full_sync') {
|
||
$this->warn('聚水潭成本价历史尚未完成首次全量同步,请在凭据和备份确认后显式使用 --full。');
|
||
|
||
return self::SUCCESS;
|
||
}
|
||
|
||
$this->info(sprintf(
|
||
'聚水潭成本价操作日志%s:%s,本轮 %d 页,累计扫描 %d 条操作日志、同步 %d 条采购价变更。',
|
||
$result['mode'] === 'full' ? '全量同步' : '增量同步',
|
||
$result['status'] === 'succeeded' ? '完成' : '进行中',
|
||
$result['pages'],
|
||
$result['source_rows'],
|
||
$result['rows'],
|
||
));
|
||
|
||
return self::SUCCESS;
|
||
}
|
||
|
||
private function runSnapshot(JushuitanCostPriceSnapshotDiffer $differ): int
|
||
{
|
||
try {
|
||
$result = $differ->sync();
|
||
} catch (Throwable $exception) {
|
||
Log::error('聚水潭成本价快照比对失败。', ['exception' => $exception::class]);
|
||
$this->error('聚水潭成本价快照比对失败,已保留上一版共享数据。');
|
||
if ($this->getOutput()->isVerbose() || $this->option('full')) {
|
||
$this->line(mb_substr($exception->getMessage(), 0, 500));
|
||
}
|
||
|
||
return self::FAILURE;
|
||
}
|
||
|
||
if ($result['mode'] === 'full') {
|
||
$this->info(sprintf('聚水潭成本价基线快照已建立:%d 个商品。之后的成本价变化会被记录。', $result['baseline_rows']));
|
||
} else {
|
||
$this->info(sprintf('聚水潭成本价比对完成:新增变价 %d 条,新商品入快照 %d 个。', $result['changes'], $result['new_skus']));
|
||
}
|
||
|
||
return self::SUCCESS;
|
||
}
|
||
|
||
/** @param array<string,mixed>|null $state */
|
||
private function shouldRunFull(?array $state): bool
|
||
{
|
||
if ((bool) $this->option('full')) {
|
||
return true;
|
||
}
|
||
|
||
// 显式全量中断后由日常任务续传同一固定时间窗,完成前不激活半份数据。
|
||
return is_array($state)
|
||
&& ($state['run_mode'] ?? null) === 'full'
|
||
&& in_array($state['status'] ?? null, ['running', 'failed'], true);
|
||
}
|
||
}
|