fix(发货同步): 增量模式跳过整日兜底查询 (#1626)

白天增量仅按 updatedAt 补最近 6 小时发货明细变化;整日自动发货仓、出库/采购兜底和重量校正继续由夜间完整同步校准。

验证:ShippingReplicaSynchronizerTest 63 passed / 402 assertions。
Co-authored-by: 苏木 <594326199@qq.com>
Co-committed-by: 苏木 <594326199@qq.com>
This commit is contained in:
苏木 2026-12-01 20:28:13 +08:00 committed by sumu
parent 754501d701
commit a64a9730f5
3 changed files with 86 additions and 58 deletions

View File

@ -11,7 +11,7 @@ use Modules\EcommerceFinance\Services\Jushuitan\ShippingReplicaSynchronizer;
*
* php artisan jushuitan:sync-shipping --from=2026-09-01 --to=2026-09-17 # 按发货时间窗口拉全量
* php artisan jushuitan:sync-shipping --days=3 # 滚动最近 3 天(每日调度用)
* php artisan jushuitan:sync-shipping --days=7 --changes-only # 只补最近变化
* php artisan jushuitan:sync-shipping --days=7 --changes-only # 只补最近 6 小时发货明细变化,重兜底留给夜间完整同步
* php artisan jushuitan:sync-shipping # 最近 2000 条(快速看效果)
*
* 按 id 去重 upsert,任何时候补拉早期区间都不会重复。
@ -20,7 +20,7 @@ class SyncJushuitanShipping extends Command
{
protected $signature = 'jushuitan:sync-shipping {--from=} {--to=} {--days=} {--limit=2000}
{--skip-freight : 仅补发货数量;保留已有运费,新记录运费留空并标记部分完成}
{--changes-only : 按聚水潭 updatedAt 补最近变化,不扫描整个发货历史}';
{--changes-only : 按聚水潭 updatedAt 补最近 6 小时发货明细变化;跳过整日自动发货/出库兜底}';
protected $description = '从经营分析副本库直读聚水潭发货明细,清洗写入 caiwu 发货数据表';

View File

@ -304,6 +304,9 @@ class ShippingReplicaSynchronizer
$this->pendingWarehouse = [];
$this->ioOk = [];
$this->pairingSkipped = 0;
$warehousesBackfilled = 0;
$purged = 0;
$platformFixed = 0;
if ($windowed) {
$this->preloadWarehouseDirectory();
$this->preloadDws($from, $to);
@ -319,68 +322,82 @@ class ShippingReplicaSynchronizer
$total = $this->syncExpress(true, $fromS, $toS, $limit, $now, $progress, $changesOnly, $changesFromS);
$this->applyPendingWarehouses($progress);
gc_collect_cycles();
$auto = $this->syncAutoShip($fromS, $toS, $now, $progress);
$outb = $this->syncOutbound($fromS, $toS, $now, $progress);
$pout = $this->syncPurchaseOut($fromS, $toS, $now, $progress);
$warehousesBackfilled = $this->backfillMissingWarehouses($fromS, $toS, $progress);
$purged = $this->purgeExcludedPackages($fromS, $toS);
$platformFixed = $this->normalizePlatformCodes($fromS, $toS);
if ($progress && $platformFixed > 0) {
$progress('平台代码(AS/IMPORT/历史等)按店铺还原:' . $platformFixed . ' 行');
}
if ($progress && $purged > 0) {
$progress('清除已排除包裹(取消/拆分/合并/线上发货):' . $purged . ' 行');
}
$this->staleMirrorPackages = $changesOnly ? 0 : $this->countUnseenMirrorPackages($fromS, $toS);
if (! $skipFreight && $this->staleMirrorPackages === 0) {
$removed = $this->dedupAtPrefix($fromS, $toS);
if ($progress && $removed > 0) {
$progress('去除@重复单号:' . $removed . ' 条');
if ($changesOnly) {
// updatedAt 增量只处理命中的发货明细。自动发货仓、出库/采购兜底和
// 出库重量校正仍需扫描整日大表,放到夜间完整同步,避免白天副本库超时后反复重试。
$changeFromS = $changesFromS ?? $fromS;
$warehousesBackfilled = $this->backfillMissingWarehouses($changeFromS, $toS, $progress);
$purged = $this->purgeExcludedPackages($changeFromS, $toS);
$platformFixed = $this->normalizePlatformCodes($changeFromS, $toS);
$this->placeholderPurged = $this->purgeZeroQtyPlaceholders($changeFromS, $toS);
$this->staleMirrorPackages = 0;
if ($progress) {
$progress('增量补变化完成:跳过自动发货仓、出库/采购兜底与重量校正,交由夜间完整同步校准');
}
}
try {
$this->placeholderPurged = $this->purgeZeroQtyPlaceholders($fromS, $toS);
// 出库单校正查副本库,偶发超时:重试几次,仍失败才跳过(并在进度里报出)
for ($try = 1; ; $try++) {
try {
$al = $this->alignWithOutbound($fromS, $toS);
break;
} catch (Throwable $e) {
if ($try >= 3) {
throw $e;
}
if ($progress) {
$progress('按销售出库单校正失败,第 ' . $try . ' 次重试:' . mb_substr($e->getMessage(), 0, 120));
}
if (! app()->runningUnitTests()) {
sleep(30);
}
} else {
$auto = $this->syncAutoShip($fromS, $toS, $now, $progress);
$outb = $this->syncOutbound($fromS, $toS, $now, $progress);
$pout = $this->syncPurchaseOut($fromS, $toS, $now, $progress);
$warehousesBackfilled = $this->backfillMissingWarehouses($fromS, $toS, $progress);
$purged = $this->purgeExcludedPackages($fromS, $toS);
$platformFixed = $this->normalizePlatformCodes($fromS, $toS);
if ($progress && $platformFixed > 0) {
$progress('平台代码(AS/IMPORT/历史等)按店铺还原:' . $platformFixed . ' 行');
}
if ($progress && $purged > 0) {
$progress('清除已排除包裹(取消/拆分/合并/线上发货):' . $purged . ' 行');
}
$this->staleMirrorPackages = $this->countUnseenMirrorPackages($fromS, $toS);
if (! $skipFreight && $this->staleMirrorPackages === 0) {
$removed = $this->dedupAtPrefix($fromS, $toS);
if ($progress && $removed > 0) {
$progress('去除@重复单号:' . $removed . ' 条');
}
}
$this->shipTimeFixed = $al['ship_time'];
$this->weightFixed = $al['weight'];
if ($progress && ($this->shipTimeFixed > 0 || $this->weightFixed > 0)) {
$progress('按销售出库单校正:发货时间 ' . $this->shipTimeFixed . ' 个包裹,线上/实称重量 ' . $this->weightFixed . ' 个包裹');
}
if ($progress && $this->placeholderPurged > 0) {
$progress('清理订单占位商品行(数量0):' . $this->placeholderPurged . ' 个包裹');
}
} catch (Throwable $e) {
if ($progress) {
$progress('按销售出库单校正(发货时间/线上实称重量)跳过:' . $e->getMessage());
}
}
// 报价核算运费(报价管理的报价):只核算核算起始日以后的发货数据,失败不影响同步结果
if ($toS > \Modules\EcommerceFinance\Services\Freight\FreightCarrierRules::START_DATE) {
try {
$fr = app(\Modules\EcommerceFinance\Services\Freight\ShippingFreightService::class)->recalculate($fromS, $toS);
if ($progress && $fr['packages'] > 0) {
$progress('报价核算运费:' . $fr['packages'] . ' 个包裹,核算成功 ' . $fr['ok'] . ' 个');
$this->placeholderPurged = $this->purgeZeroQtyPlaceholders($fromS, $toS);
// 出库单校正查副本库,偶发超时:重试几次,仍失败才跳过(并在进度里报出)
for ($try = 1; ; $try++) {
try {
$al = $this->alignWithOutbound($fromS, $toS);
break;
} catch (Throwable $e) {
if ($try >= 3) {
throw $e;
}
if ($progress) {
$progress('按销售出库单校正失败,第 ' . $try . ' 次重试:' . mb_substr($e->getMessage(), 0, 120));
}
if (! app()->runningUnitTests()) {
sleep(30);
}
}
}
$this->shipTimeFixed = $al['ship_time'];
$this->weightFixed = $al['weight'];
if ($progress && ($this->shipTimeFixed > 0 || $this->weightFixed > 0)) {
$progress('按销售出库单校正:发货时间 ' . $this->shipTimeFixed . ' 个包裹,线上/实称重量 ' . $this->weightFixed . ' 个包裹');
}
if ($progress && $this->placeholderPurged > 0) {
$progress('清理订单占位商品行(数量0):' . $this->placeholderPurged . ' 个包裹');
}
} catch (Throwable $e) {
report($e);
if ($progress) {
$progress('报价核算运费跳过:' . mb_substr($e->getMessage(), 0, 160));
$progress('按销售出库单校正(发货时间/线上实称重量)跳过:' . $e->getMessage());
}
}
// 报价核算运费(报价管理的报价):只核算核算起始日以后的发货数据,失败不影响同步结果
if ($toS > \Modules\EcommerceFinance\Services\Freight\FreightCarrierRules::START_DATE) {
try {
$fr = app(\Modules\EcommerceFinance\Services\Freight\ShippingFreightService::class)->recalculate($fromS, $toS);
if ($progress && $fr['packages'] > 0) {
$progress('报价核算运费:' . $fr['packages'] . ' 个包裹,核算成功 ' . $fr['ok'] . ' 个');
}
} catch (Throwable $e) {
report($e);
if ($progress) {
$progress('报价核算运费跳过:' . mb_substr($e->getMessage(), 0, 160));
}
}
}
}

View File

@ -106,9 +106,10 @@ class ShippingReplicaSynchronizerTest extends TestCase
$detailQueries = [];
$detailBindings = [];
$freightQueries = [];
$fullDayFallbackQueries = [];
$detailCalls = 0;
$upstream = $this->createMock(UpstreamReadOnlyQuery::class);
$upstream->method('select')->willReturnCallback(function (string $sql, array $bindings) use (&$detailQueries, &$detailBindings, &$freightQueries, &$detailCalls): array {
$upstream->method('select')->willReturnCallback(function (string $sql, array $bindings) use (&$detailQueries, &$detailBindings, &$freightQueries, &$fullDayFallbackQueries, &$detailCalls): array {
if (str_contains($sql, 'FROM rpt.jst_order_express_details e')) {
$detailQueries[] = $sql;
$detailBindings[] = $bindings;
@ -128,6 +129,12 @@ class ShippingReplicaSynchronizerTest extends TestCase
if (str_contains($sql, 'rpt.fys_jstFreightCostDetail') && str_contains($sql, 'SUM(freightCost)')) {
$freightQueries[] = $sql;
}
if (str_contains($sql, 'FROM rpt.jst_Orders o WHERE')
|| str_contains($sql, 'FROM rpt.jst_purchaseout')
|| str_contains($sql, 'SELECT so.l_id, so.io_date, so.status')
|| str_contains($sql, 'SELECT MIN(Id) AS lo, MAX(Id) AS hi FROM rpt.jst_salesOutboundInfo')) {
$fullDayFallbackQueries[] = $sql;
}
return [];
});
@ -149,7 +156,11 @@ class ShippingReplicaSynchronizerTest extends TestCase
$this->assertStringNotContainsString('ORDER BY e.updatedAt', $detailQueries[0]);
$this->assertSame(['2026-09-04 18:00:00', '2026-09-05 00:00:00'], $detailBindings[0]);
$this->assertSame([], $freightQueries);
$this->assertSame([], $fullDayFallbackQueries);
$this->assertSame(1, $result['processed']);
$this->assertSame(0, $result['autoship']);
$this->assertSame(0, $result['outbound']);
$this->assertSame(0, $result['purchase_out']);
$this->assertSame(0, $result['stale_mirror_packages']);
$this->assertSame('succeeded', DB::connection('business_mirror')->table('jst_sync_state')
->where('dataset_key', 'jst.sctrade')->value('status'));