From 85bfeb6e7336511cb7207c78cd4ae39dd861e966 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 13:16:25 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E4=BE=9B=E5=BA=94=E5=95=86=E5=AF=B9?= =?UTF-8?q?=E8=B4=A6):=20=E5=82=AC=E5=8A=9E=E5=90=8D=E5=8D=95=E6=8C=89?= =?UTF-8?q?=E8=B4=A6=E6=9C=9F=E7=B4=A2=E5=BC=95=E8=AF=BB=E5=8F=96=20(#1645?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 对账单催办改为按账期索引查询供应商名单,避免整库加载导致内存溢出。验证:SupplierStatementReminderTest 7 passed / 65 assertions。 Co-authored-by: 苏木 <594326199@qq.com> Co-committed-by: 苏木 <594326199@qq.com> --- .../Services/SupplierPayableOverrideStore.php | 54 +++++++++++++++++++ .../Services/SupplierStatementReminder.php | 20 ++----- .../SupplierStatementReminderTest.php | 16 ++++++ 3 files changed, 75 insertions(+), 15 deletions(-) diff --git a/modules/EcommerceFinance/Services/SupplierPayableOverrideStore.php b/modules/EcommerceFinance/Services/SupplierPayableOverrideStore.php index b97f4a915..8fe196b0f 100644 --- a/modules/EcommerceFinance/Services/SupplierPayableOverrideStore.php +++ b/modules/EcommerceFinance/Services/SupplierPayableOverrideStore.php @@ -390,6 +390,60 @@ class SupplierPayableOverrideStore ->all(); } + /** + * 只返回指定账期涉及的供应商,供月度催办统计使用。 + * + * @return list + */ + public function distinctSuppliersForMonth( + string $dataset, + 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); + $schema = Schema::connection($connection->getName()); + + if ($connection->getDriverName() === 'mysql' + && $schema->hasColumn('supplier_payable_overrides', 'summary_month_key') + && $schema->hasColumn('supplier_payable_overrides', 'supplier_name_key')) { + $values = $query + ->where('summary_month_key', $month) + ->whereNotNull('supplier_name_key') + ->where('supplier_name_key', '<>', '') + ->distinct() + ->pluck('supplier_name_key'); + } else { + $monthExpression = $connection->getDriverName() === 'mysql' + ? 'LEFT(REPLACE(REPLACE(JSON_UNQUOTE(JSON_EXTRACT(`values`, "$.summary_month")), "/", "-"), ".", "-"), 7)' + : "substr(replace(replace(json_extract(`values`, '$.summary_month'), '/', '-'), '.', '-'), 1, 7)"; + $supplierExpression = $connection->getDriverName() === 'mysql' + ? 'TRIM(JSON_UNQUOTE(JSON_EXTRACT(`values`, "$.supplier")))' + : "TRIM(json_extract(`values`, '$.supplier'))"; + $values = $query + ->whereRaw("{$monthExpression} = ?", [$month]) + ->selectRaw("{$supplierExpression} AS supplier") + ->distinct() + ->pluck('supplier'); + } + + $suppliers = $values + ->map(static fn (mixed $value): string => trim((string) $value)) + ->filter(static fn (string $value): bool => $value !== '') + ->unique() + ->values() + ->all(); + natcasesort($suppliers); + + return array_values($suppliers); + } + /** @return list */ public function distinctSuppliersInDataset(string $dataset, int $companyId): array { diff --git a/modules/EcommerceFinance/Services/SupplierStatementReminder.php b/modules/EcommerceFinance/Services/SupplierStatementReminder.php index 0f62ae7ef..c1969bb1e 100644 --- a/modules/EcommerceFinance/Services/SupplierStatementReminder.php +++ b/modules/EcommerceFinance/Services/SupplierStatementReminder.php @@ -129,21 +129,11 @@ class SupplierStatementReminder /** @return list */ private function sourceSuppliersForMonth(string $month, int $companyId): array { - $suppliers = []; - foreach ($this->payables->listRows(self::PAYMENT_RECONCILIATION_DATASET, $companyId) as $row) { - if (substr(trim((string) ($row['summary_month'] ?? '')), 0, 7) !== $month) { - continue; - } - $supplier = trim((string) ($row['supplier'] ?? '')); - if ($supplier !== '') { - $suppliers[$supplier] = true; - } - } - - $suppliers = array_keys($suppliers); - natcasesort($suppliers); - - return array_values($suppliers); + return $this->payables->distinctSuppliersForMonth( + self::PAYMENT_RECONCILIATION_DATASET, + $month, + $companyId, + ); } private function state(): array diff --git a/tests/Feature/Business/SupplierStatementReminderTest.php b/tests/Feature/Business/SupplierStatementReminderTest.php index 278cdfe4e..28571cebb 100644 --- a/tests/Feature/Business/SupplierStatementReminderTest.php +++ b/tests/Feature/Business/SupplierStatementReminderTest.php @@ -4,6 +4,7 @@ namespace Tests\Feature\Business; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Crypt; +use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Http; use Modules\EcommerceFinance\Services\SupplierDingTalkNotifier; use Modules\EcommerceFinance\Services\SupplierPayableOverrideStore; @@ -43,10 +44,25 @@ class SupplierStatementReminderTest extends FeatureTestCase $notifier->save('历史供应商', self::WEBHOOK . '-history', '', false); Http::fake(['*' => Http::response(['errcode' => 0, 'errmsg' => 'ok'])]); + $datasetQueries = []; + DB::connection(config('database.supplier_payable_maintenance_connection')) + ->listen(function ($query) use (&$datasetQueries): void { + if (str_contains($query->sql, 'supplier_payable_overrides') + && in_array('final:supplier:payment-reconciliation', $query->bindings, true)) { + $datasetQueries[] = $query->sql; + } + }); $this->artisan('supplier-statement:send-reminders', ['--date' => '2026-11-01']) ->assertSuccessful(); $this->artisan('supplier-statement:send-reminders', ['--date' => '2026-11-01']) ->assertSuccessful(); + $this->assertNotEmpty($datasetQueries); + foreach ($datasetQueries as $sql) { + $this->assertMatchesRegularExpression( + '/summary_month_key|json_extract\\([^)]*summary_month/i', + $sql, + ); + } Http::assertSentCount(2); $texts = Http::recorded()