Compare commits

...

1 Commits

Author SHA1 Message Date
612b825ee1 fix(发货数据): 无快递单号的自动发货仓旧包裹也按被取代清除
- 10.11/10.12 剩余约 476 个省份空白包裹:凌晨同步时按自动发货仓入库、当时订单还没有快递单号;之后订单有了发货明细,
  改由发货明细来源键吐出,旧包裹因无单号未被识别为被取代而残留(重复且省份空白)。
- 窗口内未再出现的旧包裹,若为自动发货仓来源且该订单本次已有发货明细,或无单号但同一交易单号本次已由其它包裹吐出,视为被取代删除。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014nvGZkg27YMEdhRy6EiUwf
2026-10-14 15:34:40 +08:00
2 changed files with 26 additions and 6 deletions

View File

@ -101,6 +101,9 @@ class ShippingReplicaSynchronizer
/** 已实际输出的快递单号(去@),用于判断虚拟/补差单是否借用了别的包裹单号 */
private array $emittedTrack = [];
/** @var array<string,bool> 本次已吐出包裹的交易单号 */
private array $emittedSo = [];
/** 快递单号(去@) => [交易单号 => true]:同一单号被不同交易单号共用(合包/拼单)时,按交易单号区分是否已处理 */
private array $doneTrackSo = [];
@ -202,6 +205,7 @@ class ShippingReplicaSynchronizer
$this->doneIo = [];
$this->doneTrack = [];
$this->emittedTrack = [];
$this->emittedSo = [];
$this->doneTrackSo = [];
$this->excludedSourceIds = [];
$this->ioDate = [];
@ -1110,6 +1114,9 @@ class ShippingReplicaSynchronizer
if ($tk !== '') {
$this->emittedTrack[$tk] = true;
}
if (trim($so) !== '') {
$this->emittedSo[trim($so)] = true;
}
$this->seenSourceIds[$pkgId] = true;
$this->packagesProcessed++;
if ($n === 0) {
@ -2369,7 +2376,7 @@ class ShippingReplicaSynchronizer
$superseded = [];
$rows = $this->db->table('jst_oms_shipping')
->where('ship_time', '>=', $fromS)->where('ship_time', '<', $toS)
->selectRaw('source_id, MAX(express_no) AS express_no')->groupBy('source_id')->get();
->selectRaw('source_id, MAX(express_no) AS express_no, MAX(trade_no) AS trade_no')->groupBy('source_id')->get();
foreach ($rows as $row) {
$id = (int) $row->source_id;
if (isset($this->seenSourceIds[$id])) {
@ -2380,6 +2387,15 @@ class ShippingReplicaSynchronizer
$superseded[] = $id;
continue;
}
// 凌晨按自动发货仓入库、当时还没有快递单号的旧包裹:订单之后有了发货明细(改走发货明细来源键),
// 或同一交易单号本次已由其它包裹吐出 → 视为被取代
$so = trim((string) ($row->trade_no ?? ''));
$isAuto = $id >= self::AUTOSHIP_OFFSET && $id < self::OUTBOUND_OFFSET;
if (($isAuto && isset($this->expressOrderIds[(string) ($id - self::AUTOSHIP_OFFSET)]))
|| ($tk === '' && $so !== '' && isset($this->emittedSo[$so]))) {
$superseded[] = $id;
continue;
}
$unseen++;
}
foreach (array_chunk($superseded, 500) as $chunk) {

View File

@ -1320,12 +1320,15 @@ class ShippingReplicaSynchronizerTest extends TestCase
$db = DB::connection('business_mirror');
$db->table('jst_oms_shipping')->insert([
['source_id' => 5000, 'sku_source_id' => 500000, 'ship_time' => '2026-09-02 12:00:00', 'express_no' => 'YT5',
'barcode' => null, 'product_name' => '旧商品甲', 'amount' => 10.50, 'receiver_state' => null],
'trade_no' => null, 'barcode' => null, 'product_name' => '旧商品甲', 'amount' => 10.50, 'receiver_state' => null],
// 凌晨先按自动发货仓兜底入库的旧包裹,之后发货明细到齐由 6000 吐出同一单号
['source_id' => 1000000777, 'sku_source_id' => 1000000777, 'ship_time' => '2026-09-02 13:00:00', 'express_no' => 'YT6',
'barcode' => null, 'product_name' => null, 'amount' => null, 'receiver_state' => null],
'trade_no' => null, 'barcode' => null, 'product_name' => null, 'amount' => null, 'receiver_state' => null],
['source_id' => 1000000888, 'sku_source_id' => 1000000888, 'ship_time' => '2026-09-02 14:00:00', 'express_no' => 'ZZ9',
'barcode' => null, 'product_name' => null, 'amount' => null, 'receiver_state' => null],
'trade_no' => null, 'barcode' => null, 'product_name' => null, 'amount' => null, 'receiver_state' => null],
// 凌晨入库时还没有快递单号的自动发货仓包裹,同一交易单号本次已由 6000 吐出
['source_id' => 1000000999, 'sku_source_id' => 1000000999, 'ship_time' => '2026-09-02 13:30:00', 'express_no' => null,
'trade_no' => 'so-2', 'barcode' => null, 'product_name' => null, 'amount' => null, 'receiver_state' => null],
]);
$upstream = $this->createMock(UpstreamReadOnlyQuery::class);
$upstream->method('select')->willReturnCallback(function (string $sql): array {
@ -1333,7 +1336,7 @@ class ShippingReplicaSynchronizerTest extends TestCase
return [
['id' => 5000, 'inner_order_id' => 'order-1', 'logistic_id' => 'YT5', 'send_time' => '2026-09-02 12:00:00',
'receiver_address' => '河南省-郑州市-金水区'],
['id' => 6000, 'inner_order_id' => 'order-2', 'logistic_id' => 'YT6', 'send_time' => '2026-09-02 13:00:00',
['id' => 6000, 'inner_order_id' => 'order-2', 'so_id' => 'so-2', 'logistic_id' => 'YT6', 'send_time' => '2026-09-02 13:00:00',
'receiver_address' => '江苏省-常州市-武进区'],
];
}
@ -1360,7 +1363,8 @@ class ShippingReplicaSynchronizerTest extends TestCase
);
$this->assertSame(1, $result['packages_retained_for_unmatched_amount']);
$this->assertSame(1, $result['superseded_packages_removed']);
$this->assertSame(2, $result['superseded_packages_removed']);
$this->assertSame(0, $db->table('jst_oms_shipping')->where('source_id', 1000000999)->count());
$this->assertSame(1, $result['stale_mirror_packages']);
// 保留的旧包裹:商品金额不动,省市区按上游刷新
$kept = $db->table('jst_oms_shipping')->where('source_id', 5000)->sole();