table('jst_oms_shipping')->insert([ // 旧数据:实称记成了逐件重量 ['source_id' => 7001, 'sku_source_id' => 1, 'ship_time' => '2026-10-31 18:00:00', 'express_no' => 'W1001', 'express_name1' => '新北中通', 'online_weight' => 1.2, 'actual_weight' => 0.6], ['source_id' => 7001, 'sku_source_id' => 2, 'ship_time' => '2026-10-31 18:00:00', 'express_no' => 'W1001', 'express_name1' => '新北中通', 'online_weight' => 1.2, 'actual_weight' => 0.6], ['source_id' => 7002, 'sku_source_id' => 1, 'ship_time' => '2026-11-02 09:00:00', 'express_no' => 'W1002', 'express_name1' => '新北中通', 'online_weight' => 272000, 'actual_weight' => null], ]); $sqls = []; $upstream = $this->createMock(UpstreamReadOnlyQuery::class); $upstream->method('select')->willReturnCallback(function (string $sql, array $bindings) use (&$sqls): array { $sqls[] = $sql; if (! str_contains($sql, 'so.io_date >= ?')) { return []; } $rows = [ ['io_id' => 1, 'l_id' => 'W1001', 'io_date' => '2026-10-31 18:00:00', 'status' => '已出库', 'weight' => 2.4, 'f_weight' => 2.31], ['io_id' => 2, 'l_id' => 'W1002', 'io_date' => '2026-11-02 09:00:00', 'status' => '已出库', 'weight' => 2720, 'f_weight' => null], ]; return array_values(array_filter($rows, static fn (array $r): bool => $r['io_date'] >= $bindings[0] && $r['io_date'] < $bindings[1])); }); $factory = $this->createMock(UpstreamReadOnlyQueryFactory::class); $factory->method('forSource')->willReturn($upstream); $this->app->instance(UpstreamReadOnlyQueryFactory::class, $factory); $this->artisan('jushuitan:align-weights', ['--from' => '2026-10-01', '--to' => '2026-12-01']) ->expectsOutputToContain('2026-10-01 ~ 2026-11-01 按销售出库单校正') ->expectsOutputToContain('2026-11-01 ~ 2026-12-01 重算运费') ->assertSuccessful(); $w = fn (int $sid, int $sku) => $db->table('jst_oms_shipping')->where('source_id', $sid)->where('sku_source_id', $sku)->first(['online_weight', 'actual_weight']); $this->assertEquals([2.4, 2.31], [(float) $w(7001, 1)->online_weight, (float) $w(7001, 1)->actual_weight]); $this->assertNull($w(7001, 2)->online_weight); // 整单重量只记在包裹首行 $this->assertEquals(2720, (float) $w(7002, 1)->online_weight); $this->assertSame([], array_filter($sqls, static fn (string $s): bool => str_contains($s, 'jst_order_express_details') || str_contains($s, 'jst_Orders'))); $this->assertSame(2, $db->table('jst_oms_shipping_freight')->whereIn('source_id', [7001, 7002])->count()); // 运费已重算(有核算结果) } public function test_replaced_tracking_number_is_switched_to_the_outbound_one(): void { $db = DB::connection('business_mirror'); $db->table('jst_oms_shipping')->insert([ // 换单没跟上:系统还是中通快运旧单号,出库单是顺丰新单号 ['source_id' => 7101, 'sku_source_id' => 1, 'ship_time' => '2026-11-16 14:35:17', 'express_no' => 'OLD-TEST-7101', 'trade_no' => 'SO-TEST-7101', 'express_name1' => '常州中通快运', 'online_weight' => 16.9, 'actual_weight' => 16.9], // 京东单号出库单带 @:不算换单 ['source_id' => 7102, 'sku_source_id' => 1, 'ship_time' => '2026-11-16 10:00:00', 'express_no' => 'JD-TEST-7102', 'trade_no' => 'SO-TEST-7102', 'express_name1' => '京东快递', 'online_weight' => 5, 'actual_weight' => null], // 一个订单两个包裹:不动 ['source_id' => 7103, 'sku_source_id' => 1, 'ship_time' => '2026-11-17 10:00:00', 'express_no' => 'OLD-A', 'trade_no' => 'SO-TWO', 'express_name1' => '武进韵达', 'online_weight' => 1, 'actual_weight' => null], ['source_id' => 7104, 'sku_source_id' => 1, 'ship_time' => '2026-11-17 10:00:00', 'express_no' => 'OLD-B', 'trade_no' => 'SO-TWO', 'express_name1' => '武进韵达', 'online_weight' => 1, 'actual_weight' => null], ['source_id' => 7105, 'sku_source_id' => 1, 'ship_time' => '2026-11-17 10:00:00', 'express_no' => 'OLD-DUP', 'trade_no' => 'SO-DUP', 'express_name1' => '武进韵达', 'online_weight' => 1, 'actual_weight' => null], ['source_id' => 7199, 'sku_source_id' => 1, 'ship_time' => '2026-10-17 10:00:00', 'express_no' => 'NEW-DUP', 'trade_no' => 'SO-PREVIOUS', 'express_name1' => '武进韵达', 'online_weight' => 1, 'actual_weight' => null], ]); $upstream = $this->createMock(UpstreamReadOnlyQuery::class); $upstream->method('select')->willReturnCallback(function (string $sql, array $bindings): array { $rows = [ ['io_id' => 1, 'l_id' => 'SF-TEST-7101', 'so_id' => 'SO-TEST-7101', 'logistics_company' => '横林顺丰干配', 'io_date' => '2026-11-16 14:35:17', 'status' => '已出库', 'weight' => 16.9, 'f_weight' => 0], ['io_id' => 2, 'l_id' => '@JD-TEST-7102', 'so_id' => 'SO-TEST-7102', 'logistics_company' => '京东快递', 'io_date' => '2026-11-16 10:00:00', 'status' => '已出库', 'weight' => 5, 'f_weight' => null], ['io_id' => 3, 'l_id' => 'NEW-X', 'so_id' => 'SO-TWO', 'logistics_company' => '武进韵达', 'io_date' => '2026-11-17 10:00:00', 'status' => '已出库', 'weight' => 2, 'f_weight' => null], ['io_id' => 4, 'l_id' => 'NEW-DUP', 'so_id' => 'SO-DUP', 'logistics_company' => '武进韵达', 'io_date' => '2026-11-17 10:00:00', 'status' => '已出库', 'weight' => 1, 'f_weight' => null], ]; return str_contains($sql, 'so.io_date >= ?') ? array_values(array_filter($rows, static fn (array $r): bool => $r['io_date'] >= $bindings[0] && $r['io_date'] < $bindings[1])) : []; }); $factory = $this->createMock(UpstreamReadOnlyQueryFactory::class); $factory->method('forSource')->willReturn($upstream); $this->app->instance(UpstreamReadOnlyQueryFactory::class, $factory); $this->artisan('jushuitan:align-weights', ['--from' => '2026-11-01', '--to' => '2026-12-01']) ->expectsOutputToContain('换单 1 个包裹')->assertSuccessful(); $row = $db->table('jst_oms_shipping')->where('source_id', 7101)->first(['express_no', 'express_name1', 'front_no', 'online_weight', 'actual_weight']); $this->assertSame(['SF-TEST-7101', '横林顺丰干配', 'OLD-TEST-7101'], [$row->express_no, $row->express_name1, $row->front_no]); $this->assertEquals([16.9, null], [(float) $row->online_weight, $row->actual_weight]); // 换单后按新单号的出库单校正重量 $this->assertSame('JD-TEST-7102', $db->table('jst_oms_shipping')->where('source_id', 7102)->value('express_no')); $this->assertSame(['OLD-A', 'OLD-B'], $db->table('jst_oms_shipping')->whereIn('source_id', [7103, 7104])->orderBy('source_id')->pluck('express_no')->all()); $this->assertSame('OLD-DUP', $db->table('jst_oms_shipping')->where('source_id', 7105)->value('express_no')); } public function test_rejects_bad_dates(): void { $this->artisan('jushuitan:align-weights', ['--from' => '2026-12-01', '--to' => '2026-11-01'])->assertFailed(); $this->artisan('jushuitan:align-weights', ['--from' => '2026-02-30', '--to' => '2026-12-01'])->assertFailed(); } public function test_month_failure_is_reported_and_later_months_continue_through_partial_end_date(): void { $sync = $this->createMock(ShippingReplicaSynchronizer::class); $windows = []; $sync->expects($this->exactly(3))->method('alignWithOutbound')->willReturnCallback(function ($from, $to) use (&$windows): array { $windows[] = [$from, $to]; if ($from === '2026-10-15 00:00:00') throw new \RuntimeException('测试上游超时'); return ['ship_time' => 0, 'weight' => 1]; }); $freight = $this->createMock(ShippingFreightService::class); $freight->expects($this->exactly(2))->method('recalculate')->willReturn(['packages' => 1, 'ok' => 1]); $this->app->instance(ShippingReplicaSynchronizer::class, $sync); $this->app->instance(ShippingFreightService::class, $freight); $this->artisan('jushuitan:align-weights', ['--from' => '2026-10-15', '--to' => '2026-12-02']) ->expectsOutputToContain('2026-10-15 ~ 2026-11-01 失败:测试上游超时') ->expectsOutputToContain('2026-12-01 ~ 2026-12-02 重算运费') ->assertFailed(); $this->assertSame([ ['2026-10-15 00:00:00', '2026-11-01 00:00:00'], ['2026-11-01 00:00:00', '2026-12-01 00:00:00'], ['2026-12-01 00:00:00', '2026-12-02 00:00:00'], ], $windows); } public function test_skip_freight_only_aligns_weights(): void { $sync = $this->createMock(ShippingReplicaSynchronizer::class); $sync->expects($this->once())->method('alignWithOutbound')->willReturn(['ship_time' => 0, 'weight' => 1]); $freight = $this->createMock(ShippingFreightService::class); $freight->expects($this->never())->method('recalculate'); $this->app->instance(ShippingReplicaSynchronizer::class, $sync); $this->app->instance(ShippingFreightService::class, $freight); $this->artisan('jushuitan:align-weights', ['--from' => '2026-11-01', '--to' => '2026-11-02', '--skip-freight' => true]) ->assertSuccessful(); } }