From 95f2633005fc5d531089217a05e7376dcbf431fe Mon Sep 17 00:00:00 2001 From: limeng <1131177064@qq.com> Date: Sun, 6 Dec 2026 15:32:09 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E9=80=80=E5=9B=9E=E4=BB=B6):=20=E5=90=88?= =?UTF-8?q?=E5=B9=B6=E6=99=AE=E9=80=9A=E4=B8=8E=E6=B7=98=E7=B3=BB=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E5=94=AE=E5=90=8E=E5=95=86=E5=93=81=20(#1781)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Commands/SyncReturnedParcels.php | 6 +++- docs/上游业务镜像清单.md | 2 ++ .../Services/ReturnedParcelSynchronizer.php | 36 ++++++++++++++++++- .../Feature/Business/ReturnedParcelsTest.php | 20 ++++++++++- 4 files changed, 61 insertions(+), 3 deletions(-) diff --git a/app/Console/Commands/SyncReturnedParcels.php b/app/Console/Commands/SyncReturnedParcels.php index 130407bdd..e02b8a396 100644 --- a/app/Console/Commands/SyncReturnedParcels.php +++ b/app/Console/Commands/SyncReturnedParcels.php @@ -9,7 +9,7 @@ use Throwable; class SyncReturnedParcels extends Command { - protected $signature = 'business-mirror:sync-returned-parcels {--from= : 进仓开始日期(含)} {--to= : 进仓结束日期(含)}'; + protected $signature = 'business-mirror:sync-returned-parcels {--from= : 进仓开始日期(含)} {--to= : 进仓结束日期(含)} {--deduplicate : 只合并现有镜像中两来源相同的售后商品,不读取上游}'; protected $description = '同步聚水潭售后退货退款到快递应付退回件明细'; public function handle(ReturnedParcelSynchronizer $synchronizer): int @@ -37,6 +37,10 @@ class SyncReturnedParcels extends Command return self::SUCCESS; } try { + if ($this->option('deduplicate')) { + $this->info('退回件来源校准完成:合并重复商品明细 '.$synchronizer->deduplicate().' 行。'); + return self::SUCCESS; + } $result = $synchronizer->sync($from, $to, fn (int $n, int $total) => $this->line("已扫描 {$n} / {$total} 行")); $this->info(sprintf('退回件同步完成:%s 至 %s,%d 条商品明细,耗时 %.2f 秒,PHP 峰值 %.2f MiB。', $from, $to, $result['rows'], $result['seconds'], $result['peak_mb'])); diff --git a/docs/上游业务镜像清单.md b/docs/上游业务镜像清单.md index b070c12d8..6afb1b035 100644 --- a/docs/上游业务镜像清单.md +++ b/docs/上游业务镜像清单.md @@ -36,6 +36,8 @@ 生产入口:`sh /opt/caiwuxitong/run-finance-batch.sh business-mirror:sync-returned-parcels`,北京时间每天 10:00、16:00 执行。沿用全局上游锁和独立批容器(1 CPU、3 GiB、PHP 512 MiB),单次容器执行上限 30 分钟、单条上游查询上限 60 秒;每轮先读取至多 5,000 个稳定编号和进仓时间,最多取 2,000 条符合日期的商品,按 500 个编号读取明细、100 个售后号关联主表。命令不会写聚水潭或其他财务台账。 +两来源存在重叠:售后号、售后商品明细号、商品编码、进仓时间和实退数量全部相同才视为同一商品行,完整核对各源数量后保留普通来源行,淘系独有行正常发布。可用同命令加 `--deduplicate` 在独立批容器内仅校准现有派生结果,不重读上游;执行前备份镜像。 + “未发出明细”每日 07:10 通过 `courier:sync-unshipped-waybills` 在同一受限批入口刷新。先完成上述退回件快照,再分别获取普通、淘系现场拦截的原订单发出单号;不因其他全量退款镜像失败而阻断。仓内未发从当前 `operations_replica/rpt.jst_workOrderInfo` 按 `Id` 每批读取 5,000 个类型索引,再按匹配类型的明确 ID 读取业务字段;不恢复退役 `jiansun` 连接。派生暂存 `courier_unshipped_stage` 及数据集 `derived.courier-unshipped-warehouse` 保留断点,完整后才发布,历史导入保留。`--max-batches` 可控制单轮批数,`--source` 可指定来源,刚完成退回件校准时可用 `--skip-returned`。换单除人工操作日志外,还读取发货镜像明确记录的 `front_no`;该依据时间是发货时间,不冒充换单操作时间,也不把国际物流号当旧单号。无历史导入且退回件覆盖不足时拒绝发布拦截结果,避免把不完整范围当全量。 | 状态 | 说明 | diff --git a/modules/EcommerceFinance/Services/ReturnedParcelSynchronizer.php b/modules/EcommerceFinance/Services/ReturnedParcelSynchronizer.php index ea6ec4e3a..99e589b94 100644 --- a/modules/EcommerceFinance/Services/ReturnedParcelSynchronizer.php +++ b/modules/EcommerceFinance/Services/ReturnedParcelSynchronizer.php @@ -92,10 +92,12 @@ class ReturnedParcelSynchronizer } } // 同一事务替换范围,失败或中断不影响页面上一版;只操作派生镜像。 + $duplicates = $this->duplicates($db, self::STAGE)->where('generation', $generation)->count(); $db->transaction(function () use ($db, $from, $until, $generation, $count): void { $db->table(self::TABLE)->where('received_at', '>=', $from)->where('received_at', '<', $until)->delete(); $columns = $db->getSchemaBuilder()->getColumnListing(self::TABLE); $db->table(self::TABLE)->insertUsing($columns, $db->table(self::STAGE)->select($columns)->where('generation', $generation)); + $this->removeDuplicates($db); $db->table('business_mirror_table_states')->where('dataset_key', self::DATASET)->update([ 'status' => 'succeeded', 'cursor' => null, 'initial_completed_at' => now(), 'last_succeeded_at' => now(), 'last_full_succeeded_at' => now(), @@ -105,7 +107,7 @@ class ReturnedParcelSynchronizer ]); $db->table(self::STAGE)->where('generation', $generation)->delete(); }); - return ['rows' => $count, 'from' => $from, 'to' => $to, 'seconds' => round(microtime(true) - $started, 2), + return ['rows' => $count - $duplicates, 'duplicate_rows' => $duplicates, 'from' => $from, 'to' => $to, 'seconds' => round(microtime(true) - $started, 2), 'peak_mb' => round(memory_get_peak_usage(true) / 1048576, 2), 'sources' => $expectedBySource]; } catch (Throwable $exception) { $db->table('business_mirror_table_states')->where('dataset_key', self::DATASET)->update([ @@ -115,6 +117,38 @@ class ReturnedParcelSynchronizer } } + /** 两份来源均可包含同一售后商品。保留普通来源行,淘系独有行正常保留。 */ + public function deduplicate(): int + { + $db = DB::connection(config('upstream_mirrors.connection', 'business_mirror')); + return $db->transaction(function () use ($db): int { + $removed = $this->removeDuplicates($db); + $db->table('business_mirror_table_states')->where('dataset_key', self::DATASET)->update([ + 'mirror_row_count' => $db->table(self::TABLE)->count(), 'updated_at' => now(), + ]); + return $removed; + }); + } + + private function duplicates($db, string $table) + { + return $db->table($table)->where($table.'.source_kind', 'taobao')->whereExists(function ($query) use ($table): void { + $query->selectRaw('1')->from($table.' as regular')->where('regular.source_kind', 'regular'); + foreach (['after_sales_id', 'item_id', 'sku', 'received_at', 'returned_qty'] as $key) { + $query->whereColumn('regular.'.$key, $table.'.'.$key); + } + }); + } + + private function removeDuplicates($db): int + { + $count = 0; + $this->duplicates($db, self::TABLE)->select(self::TABLE.'.source_id')->chunkById(500, function ($rows) use ($db, &$count): void { + $count += $db->table(self::TABLE)->where('source_kind', 'taobao')->whereIn('source_id', $rows->pluck('source_id'))->delete(); + }, 'source_id'); + return $count; + } + protected function sourceCount(string $from, string $until): int { return (int) $this->upstream()->scalar('SELECT COUNT_BIG(*) FROM rpt.'.$this->itemTable().' WHERE receive_date >= CAST(? AS datetime) AND receive_date < CAST(? AS datetime)', [$from, $until]); diff --git a/tests/Feature/Business/ReturnedParcelsTest.php b/tests/Feature/Business/ReturnedParcelsTest.php index f8b1f58cd..05a18ee2a 100644 --- a/tests/Feature/Business/ReturnedParcelsTest.php +++ b/tests/Feature/Business/ReturnedParcelsTest.php @@ -128,7 +128,8 @@ class ReturnedParcelsTest extends FeatureTestCase public function test_both_sources_keep_same_detail_number_and_export_every_row(): void { $sync=$this->synchronizer([]); - $sync->multipleSources=['regular'=>[$this->item(7)],'taobao'=>[$this->item(7),$this->item(8)]]; + $separateItem = array_replace($this->item(7), ['asi_id' => 9999]); + $sync->multipleSources=['regular'=>[$this->item(7)],'taobao'=>[$separateItem,$this->item(8)]]; $result=$sync->sync('2026-10-01','2026-12-02',null,1); $this->assertSame(['regular'=>1,'taobao'=>2],$result['sources']); $db=DB::connection('business_mirror'); @@ -141,6 +142,23 @@ class ReturnedParcelsTest extends FeatureTestCase $this->assertStringContainsString('普通',$csv);$this->assertStringContainsString('淘系',$csv); } + public function test_same_business_item_across_sources_is_only_published_once(): void + { + $sync = $this->synchronizer([]); + $sync->multipleSources = ['regular'=>[$this->item(7)], 'taobao'=>[$this->item(7),$this->item(8)]]; + $result = $sync->sync('2026-10-01','2026-12-02'); + $this->assertSame(2, $result['rows']); + $this->assertSame(1, $result['duplicate_rows']); + $db = DB::connection('business_mirror'); + $this->assertSame(['regular','taobao'], $db->table(ReturnedParcelSynchronizer::TABLE)->orderBy('source_id')->pluck('source_kind')->all()); + $duplicate = (array) $db->table(ReturnedParcelSynchronizer::TABLE)->where('source_id',7)->first(); + $duplicate['source_kind'] = 'taobao'; + $db->table(ReturnedParcelSynchronizer::TABLE)->insert($duplicate); + $this->assertSame(1, $sync->deduplicate()); + $this->assertSame(0, $sync->deduplicate()); + $this->assertSame(2, (int) $db->table('business_mirror_table_states')->value('mirror_row_count')); + } + public function test_failure_in_second_source_does_not_publish_half_snapshot_and_can_resume(): void { $sync=$this->synchronizer([$this->item(1)]);$sync->sync('2026-10-01','2026-12-02');