diff --git a/docs/商品成本占位行规则清单.md b/docs/商品成本占位行规则清单.md index 79f6a8f2..24eec4d1 100644 --- a/docs/商品成本占位行规则清单.md +++ b/docs/商品成本占位行规则清单.md @@ -99,6 +99,9 @@ store-receipt:product-cost:sync YYYY-MM --remap-warehouses-only 4. 组件商品金额合计与基础码金额误差≤0.01 元; 5. 多个组件时生成多条成本行,数量和金额使用商品利润明细;单组件时直接替换基础码。 +同一内部订单、线上订单、子订单号、店铺商品编码、店铺款式编码、商品编码、数量和金额完全相同, +仅在镜像表中主键或导入时间不同的组件行只计一次;子订单号不同的组件仍分别保留。 + 多基础码或金额不一致时不做替换,继续沿用原组合装、历史画像、订单售后导入明细和三级价格匹配逻辑。 ### 2.5 91. 基础料号映射 diff --git a/modules/EcommerceFinance/Repositories/GoodsProfitAfsRepository.php b/modules/EcommerceFinance/Repositories/GoodsProfitAfsRepository.php index 1c254f11..a9230a71 100644 --- a/modules/EcommerceFinance/Repositories/GoodsProfitAfsRepository.php +++ b/modules/EcommerceFinance/Repositories/GoodsProfitAfsRepository.php @@ -221,7 +221,8 @@ class GoodsProfitAfsRepository $columns = $this->fieldColumns(); foreach ([ - 'online_order_no', 'shop_name', 'shop_product_code', 'shop_style_code', + 'internal_order_no', 'online_order_no', 'sub_order_no', 'shop_name', + 'shop_product_code', 'shop_style_code', 'product_code', 'product_quantity', 'product_amount', ] as $field) { if (! isset($columns[$field])) { @@ -232,10 +233,23 @@ class GoodsProfitAfsRepository $select = array_values(array_unique(array_values(array_intersect_key( $columns, array_flip([ - 'online_order_no', 'shop_name', 'shop_product_code', 'shop_style_code', + 'internal_order_no', 'online_order_no', 'sub_order_no', 'shop_name', + 'shop_product_code', 'shop_style_code', 'product_code', 'product_quantity', 'product_amount', ]), )))); + $dedupeFields = array_values(array_intersect(array_keys($columns), [ + 'internal_order_no', + 'online_order_no', + 'sub_order_no', + 'shop_name', + 'shop_product_code', + 'shop_style_code', + 'product_code', + 'product_quantity', + 'product_amount', + ])); + $seenComponentRows = []; foreach (array_chunk($onlineOrderNos, 500) as $chunk) { foreach ($this->connection()->table(self::MAIN_TABLE) ->whereIn($columns['online_order_no'], $chunk) @@ -247,7 +261,8 @@ class GoodsProfitAfsRepository if ($productQuantity <= 0 || $productAmount <= 0 || $productCode === '') { continue; } - yield [ + + $row = [ 'online_order_no' => trim((string) ($record->{$columns['online_order_no']} ?? '')), 'shop_name' => trim((string) ($record->{$columns['shop_name']} ?? '')), 'shop_sku_id' => trim((string) ($record->{$columns['shop_product_code']} ?? '')), @@ -256,6 +271,26 @@ class GoodsProfitAfsRepository 'product_quantity' => $productQuantity, 'product_amount' => $productAmount, ]; + + // 上游同一业务行可能在不同导出批次获得新的主键并再次落入镜像。 + // 仅主键不同的相同组件行在这里合并一次;子订单号不同则仍分别保留。 + $identity = []; + foreach ($dedupeFields as $field) { + $identity[$field] = trim((string) ( + $record->{$columns[$field]} + ?? ($row[$field] ?? '') + )); + } + $identityKey = hash( + 'sha256', + json_encode($identity, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), + ); + if (isset($seenComponentRows[$identityKey])) { + continue; + } + $seenComponentRows[$identityKey] = true; + + yield $row; } } } diff --git a/tests/Unit/EcommerceFinance/GoodsProfitAfsRepositoryTest.php b/tests/Unit/EcommerceFinance/GoodsProfitAfsRepositoryTest.php index db411c37..58f59957 100644 --- a/tests/Unit/EcommerceFinance/GoodsProfitAfsRepositoryTest.php +++ b/tests/Unit/EcommerceFinance/GoodsProfitAfsRepositoryTest.php @@ -22,7 +22,7 @@ class GoodsProfitAfsRepositoryTest extends TestCase $schema->create(GoodsProfitAfsLookupSchema::TABLE, function (Blueprint $table): void { $table->integer('id')->primary(); foreach ([ - 'undefined1', 'undefined2', 'undefined7', 'undefined8', 'undefined10', + 'undefined1', 'undefined2', 'undefined3', 'undefined7', 'undefined8', 'undefined10', 'undefined12', 'undefined13', 'undefined20', 'undefined21', 'undefined25', '_mirror_afs_order_time', ] as $column) { @@ -40,7 +40,8 @@ class GoodsProfitAfsRepositoryTest extends TestCase 'dataset_key' => GoodsProfitAfsRepository::MAIN_DATASET, 'initial_completed_at' => '2026-10-01 00:00:00', ]); foreach ([ - 'undefined1' => '内部订单号', 'undefined2' => '线上订单号', 'undefined7' => '订单时间', + 'undefined1' => '内部订单号', 'undefined2' => '线上订单号', 'undefined3' => '子订单号', + 'undefined7' => '订单时间', 'undefined8' => '店铺名称', 'undefined10' => '商品编码', 'undefined12' => '店铺商品编码', 'undefined13' => '店铺款式编码', 'undefined20' => '商品数量', 'undefined25' => '商品金额', @@ -139,6 +140,7 @@ class GoodsProfitAfsRepositoryTest extends TestCase 'id' => 100, 'undefined1' => 'ORDER-100', 'undefined2' => 'ONLINE-100', + 'undefined3' => 'SUB-100-A', 'undefined7' => '2026/9/1 10:00:00', 'undefined8' => '测试店铺', 'undefined10' => 'INTERNAL-COMPONENT', @@ -151,6 +153,7 @@ class GoodsProfitAfsRepositoryTest extends TestCase 'id' => 101, 'undefined1' => 'ORDER-100', 'undefined2' => 'ONLINE-100', + 'undefined3' => 'SUB-100-A', 'undefined7' => '', 'undefined8' => '测试店铺', 'undefined10' => 'ZERO-PLACEHOLDER', @@ -159,13 +162,39 @@ class GoodsProfitAfsRepositoryTest extends TestCase 'undefined20' => '0', 'undefined25' => '0', ]); + $db->table(GoodsProfitAfsLookupSchema::TABLE)->insert([ + 'id' => 102, + 'undefined1' => 'ORDER-100', + 'undefined2' => 'ONLINE-100', + 'undefined3' => 'SUB-100-A', + 'undefined7' => '2026/9/1 10:05:00', + 'undefined8' => '测试店铺', + 'undefined10' => 'INTERNAL-COMPONENT', + 'undefined12' => 'SHOP-SKU', + 'undefined13' => 'SHOP-ITEM', + 'undefined20' => '2', + 'undefined25' => '199.98', + ]); + $db->table(GoodsProfitAfsLookupSchema::TABLE)->insert([ + 'id' => 103, + 'undefined1' => 'ORDER-100', + 'undefined2' => 'ONLINE-100', + 'undefined3' => 'SUB-100-B', + 'undefined7' => '2026/9/1 10:06:00', + 'undefined8' => '测试店铺', + 'undefined10' => 'INTERNAL-COMPONENT', + 'undefined12' => 'SHOP-SKU', + 'undefined13' => 'SHOP-ITEM', + 'undefined20' => '2', + 'undefined25' => '199.98', + ]); $stream = (new GoodsProfitAfsRepository()) ->streamPositiveComponentRowsForOrders(['ONLINE-100']); $this->assertInstanceOf(\Generator::class, $stream); $rows = iterator_to_array($stream, false); - $this->assertSame([[ + $expected = [ 'online_order_no' => 'ONLINE-100', 'shop_name' => '测试店铺', 'shop_sku_id' => 'SHOP-SKU', @@ -173,6 +202,7 @@ class GoodsProfitAfsRepositoryTest extends TestCase 'product_code' => 'INTERNAL-COMPONENT', 'product_quantity' => 2.0, 'product_amount' => 199.98, - ]], $rows); + ]; + $this->assertSame([$expected, $expected], $rows); } }