perf(供应商对账): 拆分退货月度索引查询 (#1715)

退货厂家月度筛选改用已有日期索引并拆分查询,保持厂家名单一致,将生产筛选从约 7.5 秒降至约 1 秒。
Co-authored-by: 苏木 <594326199@qq.com>
Co-committed-by: 苏木 <594326199@qq.com>
This commit is contained in:
苏木 2026-12-03 19:11:24 +08:00 committed by sumu
parent 50359f10fb
commit d6842648e3

View File

@ -781,41 +781,57 @@ class SupplierPayableOverrideStore
$sendTimeEmpty = "COALESCE(NULLIF(NULLIF({$sendTime}, 'null'), ''), '') = ''";
$sendTimePresent = "COALESCE(NULLIF(NULLIF({$sendTime}, 'null'), ''), '') <> ''";
$values = $connection->table('supplier_payable_overrides')
->where('company_id', $companyId)
->where('dataset', $dataset);
if ($reconciler !== '') {
$values->where('values->reconciler', $reconciler);
}
$values = $values
->where(function ($query) use ($connection, $month, $start, $next, $sendTimeEmpty, $sendTimePresent): void {
$query->where(function ($dateQuery) use ($start, $next): void {
$dateQuery->whereNotNull('record_date_key')
->where('record_date_key', '>=', $start)
->where('record_date_key', '<', $next);
})->orWhere(function ($summaryQuery) use ($month, $sendTimeEmpty): void {
$summaryQuery->whereRaw($sendTimeEmpty)
->where('summary_month_key', $month);
})->orWhere(function ($fallbackQuery) use ($connection, $month, $sendTimePresent): void {
$fallbackQuery->whereNull('record_date_key')
->whereRaw($sendTimePresent)
->whereRaw(
$this->jsonMonthExpression($connection, 'send_time') . ' = ?',
[$month],
);
});
})
$baseQuery = function () use ($connection, $dataset, $companyId, $reconciler): \Illuminate\Database\Query\Builder {
$query = $connection->table('supplier_payable_overrides')
->where('company_id', $companyId)
->where('dataset', $dataset);
if ($reconciler !== '') {
$query->where('values->reconciler', $reconciler);
}
return $query;
};
$dateSuppliers = $baseQuery()
->whereNotNull('record_date_key')
->where('record_date_key', '>=', $start)
->where('record_date_key', '<', $next)
->whereNotNull('supplier_name_key')
->where('supplier_name_key', '<>', '')
->distinct()
->pluck('supplier_name_key');
$suppliers = $values
->map(static fn (mixed $value): string => trim((string) $value))
->filter()
->unique()
->values()
->pluck('supplier_name_key')
->all();
$emptySendSuppliers = $baseQuery()
->where('summary_month_key', $month)
->whereRaw($sendTimeEmpty)
->whereNotNull('supplier_name_key')
->where('supplier_name_key', '<>', '')
->distinct()
->pluck('supplier_name_key')
->all();
$legacySuppliers = $baseQuery()
->whereNull('record_date_key')
->where('summary_month_key', $month)
->whereRaw($sendTimePresent)
->whereRaw(
$this->jsonMonthExpression($connection, 'send_time') . ' = ?',
[$month],
)
->whereNotNull('supplier_name_key')
->where('supplier_name_key', '<>', '')
->distinct()
->pluck('supplier_name_key')
->all();
$suppliers = array_values(array_unique(array_merge(
$dateSuppliers,
$emptySendSuppliers,
$legacySuppliers,
)));
$suppliers = array_values(array_filter(array_map(
static fn (mixed $value): string => trim((string) $value),
$suppliers,
), static fn (string $supplier): bool => $supplier !== ''));
sort($suppliers, SORT_NATURAL | SORT_FLAG_CASE);
return $suppliers;