From 381d8f55f25a617c2d9ee17a861e5bc0a817f4bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E6=9C=A8?= <594326199@qq.com> Date: Wed, 2 Dec 2026 12:29:16 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E4=BE=9B=E5=BA=94=E5=95=86=E9=80=80?= =?UTF-8?q?=E8=B4=A7):=20=E5=88=86=E4=BA=AB=E9=A1=B5=E6=8C=89=E5=8E=82?= =?UTF-8?q?=E5=AE=B6=E6=9C=88=E4=BB=BD=E8=AF=BB=E5=8F=96=20(#1642)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 厂家退货分享页改为按 supplier_name_key 与账期定向查询,避免加载完整退货数据集导致 1GB 内存溢出和 502。 验证:定向查询约 0.011 秒;分享页、确认页及批量推送回归通过。 Co-authored-by: 苏木 <594326199@qq.com> Co-committed-by: 苏木 <594326199@qq.com> --- .../Services/SupplierPayableOverrideStore.php | 42 +++++++++++++++++++ .../View/SupplierReconciliationPageData.php | 25 ++++------- .../SupplierPayableFinalPagesTest.php | 41 ++++++++++++++++++ 3 files changed, 92 insertions(+), 16 deletions(-) diff --git a/modules/EcommerceFinance/Services/SupplierPayableOverrideStore.php b/modules/EcommerceFinance/Services/SupplierPayableOverrideStore.php index 9d6ed4775..b97f4a915 100644 --- a/modules/EcommerceFinance/Services/SupplierPayableOverrideStore.php +++ b/modules/EcommerceFinance/Services/SupplierPayableOverrideStore.php @@ -348,6 +348,48 @@ class SupplierPayableOverrideStore ->all(); } + /** + * 只读取指定厂家、月份的终版数据,避免厂家分享页加载整个历史数据集。 + * + * @return array> + */ + public function listRowsForSupplierMonth( + string $dataset, + string $supplier, + string $month, + int $companyId, + ): array { + if (preg_match('/^\d{4}-\d{2}$/', $month) !== 1) { + return []; + } + + $connection = $this->connection(); + $query = $connection->table('supplier_payable_overrides') + ->where('company_id', $companyId) + ->where('dataset', $dataset); + $this->whereSupplier($query, $supplier); + + $monthExpression = $connection->getDriverName() === 'mysql' + ? 'CASE WHEN COALESCE(NULLIF(NULLIF(JSON_UNQUOTE(JSON_EXTRACT(`values`, "$.send_time")), "null"), ""), "") <> "" ' + . 'THEN LEFT(REPLACE(REPLACE(JSON_UNQUOTE(JSON_EXTRACT(`values`, "$.send_time")), "/", "-"), ".", "-"), 7) ' + . 'ELSE LEFT(REPLACE(REPLACE(JSON_UNQUOTE(JSON_EXTRACT(`values`, "$.summary_month")), "/", "-"), ".", "-"), 7) END' + : "CASE WHEN COALESCE(NULLIF(NULLIF(json_extract(`values`, '$.send_time'), 'null'), ''), '') <> '' " + . "THEN substr(replace(replace(json_extract(`values`, '$.send_time'), '/', '-'), '.', '-'), 1, 7) " + . "ELSE substr(replace(replace(json_extract(`values`, '$.summary_month'), '/', '-'), '.', '-'), 1, 7) END"; + + return $query + ->whereRaw("{$monthExpression} = ?", [$month]) + ->orderBy('id') + ->get(['record_key', 'values']) + ->map(static function (object $record): array { + $values = json_decode((string) $record->values, true) ?: []; + $values['_action_key'] = (string) $record->record_key; + + return $values; + }) + ->all(); + } + /** @return list */ public function distinctSuppliersInDataset(string $dataset, int $companyId): array { diff --git a/modules/EcommerceFinance/View/SupplierReconciliationPageData.php b/modules/EcommerceFinance/View/SupplierReconciliationPageData.php index cfb5f9c6f..6032eb4d4 100644 --- a/modules/EcommerceFinance/View/SupplierReconciliationPageData.php +++ b/modules/EcommerceFinance/View/SupplierReconciliationPageData.php @@ -1111,21 +1111,14 @@ class SupplierReconciliationPageData $result[$supplier] = []; } - $allRows = $this->store->listRows( - PayablesFinalActions::DATASET_PREFIX . 'supplier:return-reconciliation', - $companyId, - ); - foreach ($allRows as $row) { - $supplier = trim((string) ($row['supplier'] ?? '')); - if (! array_key_exists($supplier, $result) - || $this->returnRowMonth($row) !== (string) $normalizedTargets[$supplier]) { - continue; - } - - $result[$supplier][] = $row; - } - - foreach ($result as $supplier => &$rows) { + $dataset = PayablesFinalActions::DATASET_PREFIX . 'supplier:return-reconciliation'; + foreach ($normalizedTargets as $supplier => $month) { + $rows = $this->store->listRowsForSupplierMonth( + $dataset, + $supplier, + $month, + $companyId, + ); if ($rows === []) { continue; } @@ -1135,8 +1128,8 @@ class SupplierReconciliationPageData $row = $this->returnAccounting->enrich($row); } unset($row); + $result[$supplier] = $rows; } - unset($rows); return $result; } diff --git a/tests/Feature/Business/SupplierPayableFinalPagesTest.php b/tests/Feature/Business/SupplierPayableFinalPagesTest.php index 8bb93d8e1..5725457e5 100644 --- a/tests/Feature/Business/SupplierPayableFinalPagesTest.php +++ b/tests/Feature/Business/SupplierPayableFinalPagesTest.php @@ -22,6 +22,47 @@ class SupplierPayableFinalPagesTest extends FeatureTestCase ); } + public function test_supplier_month_rows_ignore_unrelated_suppliers_and_months(): void + { + $dataset = 'final:supplier:return-reconciliation'; + app(SupplierPayableOverrideStore::class)->saveBatch($dataset, [ + 'target:month' => [ + 'summary_month' => '2026-11', + 'send_time' => '2026-11-08', + 'supplier' => '华韵', + 'barcode' => 'TARGET-MONTH', + ], + 'target:fallback-month' => [ + 'send_time' => '2026-11-09 10:00:00', + 'supplier' => '华韵', + 'barcode' => 'TARGET-FALLBACK', + ], + 'other:month' => [ + 'summary_month' => '2026-10', + 'send_time' => '2026-10-08', + 'supplier' => '华韵', + 'barcode' => 'OTHER-MONTH', + ], + 'other:supplier' => [ + 'summary_month' => '2026-11', + 'send_time' => '2026-11-08', + 'supplier' => '其他厂家', + 'barcode' => 'OTHER-SUPPLIER', + ], + ], (int) $this->company->id, (int) $this->user->id); + + $rows = app(SupplierPayableOverrideStore::class)->listRowsForSupplierMonth( + $dataset, + '华韵', + '2026-11', + (int) $this->company->id, + ); + $barcodes = array_column($rows, 'barcode'); + sort($barcodes); + + $this->assertSame(['TARGET-FALLBACK', 'TARGET-MONTH'], $barcodes); + } + public function test_final_table_page_keeps_the_maintenance_toolbar_and_starts_empty(): void { $this->loginAs()