发货数据还原平台和无物流仓库 (#1493)

This commit is contained in:
limeng 2026-11-27 10:36:00 +08:00
parent b2f7a072c8
commit 6a6c6f94c1
2 changed files with 159 additions and 2 deletions

View File

@ -264,6 +264,10 @@ class ShippingReplicaSynchronizer
$outb = $this->syncOutbound($fromS, $toS, $now, $progress);
$pout = $this->syncPurchaseOut($fromS, $toS, $now, $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 . ' 行');
}
@ -847,6 +851,87 @@ class ShippingReplicaSynchronizer
return $map[$base] ?? $raw;
}
/** 订单来源代码(售后补发/导入/历史单等):工作平台显示的是店铺所属平台,不显示代码。 */
private const PLATFORM_CODES = ['AS', '历史AS', 'IMPORT', '历史', 'ERP', 'JDDROPSHIP'];
private const PLATFORM_CODE_DIRECT = ['JDDROPSHIP' => '京东厂家直送'];
private const SHOP_PREFIX_PLATFORM = [
'京喜' => '京东商城', '京东' => '京东商城', '天猫' => '淘宝天猫', '淘宝' => '淘宝天猫',
'拼多多' => '拼多多', '阿里巴巴' => '阿里巴巴', '抖音' => '头条放心购', '得物' => '得物',
];
private static function isPlatformCode(?string $p): bool
{
$p = trim((string) $p);
if ($p === '') {
return false;
}
foreach (explode(',', $p) as $part) {
if (! in_array(trim($part), self::PLATFORM_CODES, true)) {
return false;
}
}
return true;
}
/**
* 平台为订单来源代码的包裹:按同店铺最常见的平台还原(11 月 53 个对上工作平台 52 个);
* JDDROPSHIP 直接为京东厂家直送;店铺无历史时按店铺名前缀;都判断不了则不动。
*/
public function normalizePlatformCodes(string $fromS, string $toS): int
{
$rows = $this->db->table('jst_oms_shipping')
->where('ship_time', '>=', $fromS)->where('ship_time', '<', $toS)
->where(function ($q): void {
$q->whereIn('platform', self::PLATFORM_CODES)->orWhere('platform', 'like', '%,%');
})
->select('platform', 'shop_name')->distinct()->get();
$fixed = 0;
$byShop = [];
foreach ($rows as $r) {
if (! self::isPlatformCode($r->platform)) {
continue;
}
$codes = array_map('trim', explode(',', (string) $r->platform));
$shop = trim((string) $r->shop_name);
$target = null;
foreach ($codes as $c) {
if (isset(self::PLATFORM_CODE_DIRECT[$c])) {
$target = self::PLATFORM_CODE_DIRECT[$c];
}
}
if ($target === null && $shop !== '') {
if (! array_key_exists($shop, $byShop)) {
$top = $this->db->table('jst_oms_shipping')->where('shop_name', $shop)
->whereNotNull('platform')->where('platform', '!=', '')
->whereNotIn('platform', self::PLATFORM_CODES)->where('platform', 'not like', '%,%')
->groupBy('platform')->orderByRaw('COUNT(*) DESC')->limit(1)
->selectRaw('platform, COUNT(*) AS n')->first();
$byShop[$shop] = $top ? (string) $top->platform : null;
}
$target = $byShop[$shop];
if ($target === null) {
foreach (self::SHOP_PREFIX_PLATFORM as $prefix => $pf) {
if (str_starts_with($shop, $prefix)) {
$target = $pf;
break;
}
}
}
}
if ($target === null) {
continue;
}
$q = $this->db->table('jst_oms_shipping')
->where('ship_time', '>=', $fromS)->where('ship_time', '<', $toS)
->where('platform', $r->platform);
$shop === '' ? $q->where(fn ($w) => $w->whereNull('shop_name')->orWhere('shop_name', '')) : $q->where('shop_name', $r->shop_name);
$fixed += $q->update(['platform' => $target]);
}
return $fixed;
}
/** 记下按规则排除的包裹;同步结束后清掉它在本窗口里的旧记录(此前版本可能写入过)。 */
private function excludeSource(int $sourceId): void
{
@ -1252,6 +1337,16 @@ class ShippingReplicaSynchronizer
$items = $virtualItems;
}
}
if ($this->whName($pkg['warehouse_name'] ?? null) === null) {
// 包裹级仓库为空:取第一个有效商品行仓库,整包统一(避免同一包裹一行 "0"、一行仓库名)
foreach ($items as $it) {
$w = $this->whName($it['warehouse'] ?? null);
if ($w !== null) {
$pkg['warehouse_name'] = $w;
break;
}
}
}
$n = count($items);
$tk = ltrim(trim((string) ($tracking ?? '')), '@');
if ($tk !== '') {
@ -1336,7 +1431,8 @@ class ShippingReplicaSynchronizer
{
$w = trim((string) ($w ?? ''));
return ($w === '' || $w === '{未指定发货仓}') ? null : $w;
// 聚水潭部分虚拟/补差单的仓库字段是 "0"(无仓库),工作平台不显示 "0"
return ($w === '' || $w === '0' || $w === '{未指定发货仓}') ? null : $w;
}
private function buildRow(array $pkg, array $it, array $sk, array $f, bool $first, string $countStr, $now, bool $auto): array
@ -1369,7 +1465,8 @@ class ShippingReplicaSynchronizer
'shop_name' => $pkg['shop_name'],
'platform' => $pkg['platform'],
'supplier_name' => ($it['supplier'] ?? null) ?: (($sk['supplier'] ?? null) ?: $pkg['dis_co_name']),
'warehouse_name' => $this->whName($pkg['warehouse_name'] ?: ($it['warehouse'] ?? null)),
'warehouse_name' => $this->whName($pkg['warehouse_name'] ?: ($it['warehouse'] ?? null))
?? (trim((string) ($pkg['express_name1'] ?? '')) === self::NO_LOGISTICS ? self::NO_LOGISTICS_WAREHOUSE : null),
'receiver_state' => $pkg['r_state'],
'receiver_city' => $pkg['r_city'],
'receiver_district' => $pkg['r_district'],

View File

@ -1718,6 +1718,66 @@ class ShippingReplicaSynchronizerTest extends TestCase
$this->assertSame(['1/2'], $rows->pluck('pkg_count')->unique()->values()->all());
}
public function test_platform_codes_are_restored_to_the_shop_platform_like_the_work_platform(): void
{
$db = DB::connection('business_mirror');
$row = fn (int $sid, string $time, string $shop, ?string $platform) => [
'source_id' => $sid, 'sku_source_id' => $sid, 'ship_time' => $time, 'express_no' => 'E' . $sid,
'barcode' => 'B', 'qty' => 1, 'pkg_count' => '1/1', 'shop_name' => $shop, 'platform' => $platform,
];
foreach ([
$row(9101, '2026-10-20 10:00:00', '拼多多-梦格朗MOGGELAN安崇专卖店', '拼多多'),
$row(9102, '2026-11-02 10:00:00', '拼多多-梦格朗MOGGELAN安崇专卖店', '拼多多'),
$row(9103, '2026-11-03 10:00:00', '拼多多-梦格朗MOGGELAN安崇专卖店', 'AS'),
$row(9104, '2026-11-03 11:00:00', '京东-慕加旗舰店', '京东商城'),
$row(9105, '2026-11-04 11:00:00', '京东-慕加旗舰店', 'IMPORT'),
$row(9106, '2026-11-05 11:00:00', '京东-某厂直店', 'JDDROPSHIP'),
$row(9107, '2026-11-05 12:00:00', '京喜-梦格朗喜店京喜自营工厂店', 'AS,历史AS'),
$row(9108, '2026-11-05 13:00:00', '无前缀店铺', '历史'),
$row(9109, '2026-11-05 14:00:00', '拼多多-梦格朗MOGGELAN安崇专卖店', '淘宝天猫,阿里巴巴'),
$row(9110, '2026-12-05 14:00:00', '拼多多-梦格朗MOGGELAN安崇专卖店', 'AS'),
] as $r) {
$db->table('jst_oms_shipping')->insert($r);
}
$factory = $this->createMock(UpstreamReadOnlyQueryFactory::class);
$factory->method('forSource')->willReturn($this->createMock(UpstreamReadOnlyQuery::class));
$n = (new ShippingReplicaSynchronizer($factory))->normalizePlatformCodes('2026-11-01 00:00:00', '2026-12-01 00:00:00');
$p = fn (int $sid) => $db->table('jst_oms_shipping')->where('source_id', $sid)->value('platform');
$this->assertSame(4, $n);
$this->assertSame('拼多多', $p(9103));
$this->assertSame('京东商城', $p(9105));
$this->assertSame('京东厂家直送', $p(9106));
$this->assertSame('京东商城', $p(9107));
$this->assertSame('历史', $p(9108)); // 无历史、无前缀:不猜
$this->assertSame('淘宝天猫,阿里巴巴', $p(9109)); // 不是代码:不动
$this->assertSame('AS', $p(9110)); // 窗口外:不动
}
public function test_zero_or_missing_warehouse_on_no_logistics_package_becomes_auto_ship_warehouse(): void
{
$factory = $this->createMock(UpstreamReadOnlyQueryFactory::class);
$factory->method('forSource')->willReturn($this->createMock(UpstreamReadOnlyQuery::class));
$sync = new ShippingReplicaSynchronizer($factory);
$build = new \ReflectionMethod($sync, 'buildRow');
$build->setAccessible(true);
$pkg = fn (string $express, ?string $wh) => [
'tracking' => null, 'so_id' => '261102-1', 'express_name1' => $express, 'ship_time' => '2026-11-02 10:00:00',
'pay_time' => null, 'shop_name' => 'S', 'platform' => '拼多多', 'dis_co_name' => null, 'warehouse_name' => $wh,
'r_state' => null, 'r_city' => null, 'r_district' => null, 'r_address' => '', 'r_name' => null, 'r_mobile' => null,
'r_phone' => null, 'buyer_memo' => null, 'weight' => null,
];
$it = ['sku' => 'X00002', 'name' => '信誉订单虚拟条码', 'qty' => 1];
$wh = fn (array $row) => $row['warehouse_name'] ?? null;
$this->assertSame('简尚-自动发货仓', $wh($build->invoke($sync, $pkg('无需物流', '0'), $it, [], [], true, '1/1', now(), false)));
$this->assertSame('简尚-自动发货仓', $wh($build->invoke($sync, $pkg('无需物流', null), $it + ['warehouse' => '0'], [], [], true, '1/1', now(), false)));
$this->assertSame('简尚-POP仓', $wh($build->invoke($sync, $pkg('无需物流', '简尚-POP仓'), $it, [], [], true, '1/1', now(), false)));
$this->assertNull($wh($build->invoke($sync, $pkg('京东快递', '0'), $it, [], [], true, '1/1', now(), false)));
$this->assertSame('简尚-POP仓', $wh($build->invoke($sync, $pkg('京东快递', ''), $it + ['warehouse' => '简尚-POP仓'], [], [], true, '1/1', now(), false)));
}
private function setScanBatchLimit(ShippingReplicaSynchronizer $synchronizer, string $property, int $limit): void
{
$reflection = new \ReflectionProperty($synchronizer, $property);