From 232f01d0656704112119cb7cd81d25e3b963c7cc Mon Sep 17 00:00:00 2001 From: chenxi Date: Tue, 27 Oct 2026 11:00:55 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=B5=81=E5=BC=8F=E5=A4=84=E7=90=86?= =?UTF-8?q?=E5=95=86=E5=93=81=E6=88=90=E6=9C=AC=E6=8B=86=E5=88=86=E7=BB=84?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Commands/SyncStoreReceiptProductCost.php | 24 ++++++++------ .../Repositories/GoodsProfitAfsRepository.php | 32 +++++++++++++++---- .../GoodsProfitAfsRepositoryTest.php | 5 ++- 3 files changed, 45 insertions(+), 16 deletions(-) diff --git a/app/Console/Commands/SyncStoreReceiptProductCost.php b/app/Console/Commands/SyncStoreReceiptProductCost.php index 7f6bc1bd..84819c4e 100644 --- a/app/Console/Commands/SyncStoreReceiptProductCost.php +++ b/app/Console/Commands/SyncStoreReceiptProductCost.php @@ -1124,18 +1124,24 @@ class SyncStoreReceiptProductCost extends Command ]; } - $sourceRows = app(GoodsProfitAfsRepository::class) - ->positiveComponentRowsForOrders($onlineOrderNos); $componentsByOrderKey = []; $componentsByOnlineOrder = []; - foreach ($sourceRows as $sourceRow) { + foreach (app(GoodsProfitAfsRepository::class) + ->streamPositiveComponentRowsForOrders($onlineOrderNos) as $sourceRow) { $key = $sourceRow['shop_name'] . "\0" . $sourceRow['online_order_no'] . "\0" . $sourceRow['shop_sku_id'] . "\0" . $sourceRow['shop_i_id']; - $componentsByOrderKey[$key][] = $sourceRow; + $code = mb_strtoupper(trim((string) ($sourceRow['product_code'] ?? ''))); + if ($code !== '') { + $componentsByOrderKey[$key][$code]['qty'] + = ($componentsByOrderKey[$key][$code]['qty'] ?? 0.0) + + (float) ($sourceRow['product_quantity'] ?? 0); + $componentsByOrderKey[$key][$code]['amount'] + = round(($componentsByOrderKey[$key][$code]['amount'] ?? 0.0) + + (float) ($sourceRow['product_amount'] ?? 0), 2); + } $orderKey = $sourceRow['shop_name'] . "\0" . $sourceRow['online_order_no']; - $code = mb_strtoupper((string) $sourceRow['product_code']); $componentsByOnlineOrder[$orderKey][$code]['qty'] = ($componentsByOnlineOrder[$orderKey][$code]['qty'] ?? 0.0) + (float) $sourceRow['product_quantity']; @@ -1152,10 +1158,10 @@ class SyncStoreReceiptProductCost extends Command . "\0" . $group['shop_sku_id'] . "\0" . $group['shop_item_id']; $components = []; - foreach ($componentsByOrderKey[$orderKey] ?? [] as $component) { - $code = mb_strtoupper(trim((string) ($component['product_code'] ?? ''))); - $quantity = (float) ($component['product_quantity'] ?? 0); - $amount = (float) ($component['product_amount'] ?? 0); + foreach ($componentsByOrderKey[$orderKey] ?? [] as $code => $component) { + $code = mb_strtoupper(trim((string) $code)); + $quantity = (float) ($component['qty'] ?? 0); + $amount = (float) ($component['amount'] ?? 0); if ( $code === '' || $quantity <= 0 diff --git a/modules/EcommerceFinance/Repositories/GoodsProfitAfsRepository.php b/modules/EcommerceFinance/Repositories/GoodsProfitAfsRepository.php index a3e697c8..1c254f11 100644 --- a/modules/EcommerceFinance/Repositories/GoodsProfitAfsRepository.php +++ b/modules/EcommerceFinance/Repositories/GoodsProfitAfsRepository.php @@ -2,6 +2,7 @@ namespace Modules\EcommerceFinance\Repositories; +use Generator; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Schema; use Modules\EcommerceFinance\Services\GoodsProfitAfsLookupSchema; @@ -185,6 +186,28 @@ class GoodsProfitAfsRepository * }> */ public function positiveComponentRowsForOrders(array $onlineOrderNos): array + { + return iterator_to_array( + $this->streamPositiveComponentRowsForOrders($onlineOrderNos), + false, + ); + } + + /** + * 商品成本候选订单可能达到数十万,逐批产出组件行,避免一次装入全部明细。 + * + * @param list $onlineOrderNos + * @return Generator + */ + public function streamPositiveComponentRowsForOrders(array $onlineOrderNos): Generator { $onlineOrderNos = array_values(array_unique(array_filter(array_map( static fn (mixed $value): string => trim((string) $value), @@ -193,7 +216,7 @@ class GoodsProfitAfsRepository sort($onlineOrderNos, SORT_STRING); if ($onlineOrderNos === [] || ! $this->ready()) { - return []; + return; } $columns = $this->fieldColumns(); @@ -202,7 +225,7 @@ class GoodsProfitAfsRepository 'product_code', 'product_quantity', 'product_amount', ] as $field) { if (! isset($columns[$field])) { - return []; + return; } } @@ -213,7 +236,6 @@ class GoodsProfitAfsRepository 'product_code', 'product_quantity', 'product_amount', ]), )))); - $rows = []; foreach (array_chunk($onlineOrderNos, 500) as $chunk) { foreach ($this->connection()->table(self::MAIN_TABLE) ->whereIn($columns['online_order_no'], $chunk) @@ -225,7 +247,7 @@ class GoodsProfitAfsRepository if ($productQuantity <= 0 || $productAmount <= 0 || $productCode === '') { continue; } - $rows[] = [ + yield [ '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']} ?? '')), @@ -236,8 +258,6 @@ class GoodsProfitAfsRepository ]; } } - - return $rows; } /** @param array $filters */ diff --git a/tests/Unit/EcommerceFinance/GoodsProfitAfsRepositoryTest.php b/tests/Unit/EcommerceFinance/GoodsProfitAfsRepositoryTest.php index 8114b953..db411c37 100644 --- a/tests/Unit/EcommerceFinance/GoodsProfitAfsRepositoryTest.php +++ b/tests/Unit/EcommerceFinance/GoodsProfitAfsRepositoryTest.php @@ -160,7 +160,10 @@ class GoodsProfitAfsRepositoryTest extends TestCase 'undefined25' => '0', ]); - $rows = (new GoodsProfitAfsRepository())->positiveComponentRowsForOrders(['ONLINE-100']); + $stream = (new GoodsProfitAfsRepository()) + ->streamPositiveComponentRowsForOrders(['ONLINE-100']); + $this->assertInstanceOf(\Generator::class, $stream); + $rows = iterator_to_array($stream, false); $this->assertSame([[ 'online_order_no' => 'ONLINE-100', -- 2.47.2