fix(供应商退货): 分享页按厂家月份读取 (#1642)

厂家退货分享页改为按 supplier_name_key 与账期定向查询,避免加载完整退货数据集导致 1GB 内存溢出和 502。

验证:定向查询约 0.011 秒;分享页、确认页及批量推送回归通过。
Co-authored-by: 苏木 <594326199@qq.com>
Co-committed-by: 苏木 <594326199@qq.com>
This commit is contained in:
苏木 2026-12-02 12:29:16 +08:00 committed by sumu
parent 501c973216
commit 381d8f55f2
3 changed files with 92 additions and 16 deletions

View File

@ -348,6 +348,48 @@ class SupplierPayableOverrideStore
->all();
}
/**
* 只读取指定厂家、月份的终版数据,避免厂家分享页加载整个历史数据集。
*
* @return array<int,array<string,mixed>>
*/
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<string> */
public function distinctSuppliersInDataset(string $dataset, int $companyId): array
{

View File

@ -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;
}

View File

@ -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()