fix(供应商对账): 催办名单按账期索引读取 (#1645)

对账单催办改为按账期索引查询供应商名单,避免整库加载导致内存溢出。验证:SupplierStatementReminderTest 7 passed / 65 assertions。
Co-authored-by: 苏木 <594326199@qq.com>
Co-committed-by: 苏木 <594326199@qq.com>
This commit is contained in:
苏木 2026-12-02 13:16:25 +08:00 committed by sumu
parent 5392ad1606
commit 85bfeb6e73
3 changed files with 75 additions and 15 deletions

View File

@ -390,6 +390,60 @@ class SupplierPayableOverrideStore
->all();
}
/**
* 只返回指定账期涉及的供应商,供月度催办统计使用。
*
* @return list<string>
*/
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<string> */
public function distinctSuppliersInDataset(string $dataset, int $companyId): array
{

View File

@ -129,21 +129,11 @@ class SupplierStatementReminder
/** @return list<string> */
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

View File

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