perf(供应商对账): 使用日期索引优化退货厂家月度筛选 (#1712)
退货厂家按月筛选改用已有日期索引,并保留异常数据 JSON 回退,生产对照名单一致。 Co-authored-by: 苏木 <594326199@qq.com> Co-committed-by: 苏木 <594326199@qq.com>
This commit is contained in:
parent
aadf5682e2
commit
db28cf4f0f
@ -743,6 +743,84 @@ class SupplierPayableOverrideStore
|
||||
return $suppliers;
|
||||
}
|
||||
|
||||
/**
|
||||
* 退货数据优先按发货时间归月;MySQL 下使用已有日期索引列,避免逐行 JSON 函数计算。
|
||||
*
|
||||
* @return list<string>
|
||||
*/
|
||||
public function distinctSuppliersForSendTimeMonth(
|
||||
string $dataset,
|
||||
int $companyId,
|
||||
string $month,
|
||||
string $reconciler = '',
|
||||
): array {
|
||||
if (preg_match('/^\d{4}-(0[1-9]|1[0-2])$/', $month) !== 1) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$connection = $this->connection();
|
||||
$schema = Schema::connection($connection->getName());
|
||||
if ($connection->getDriverName() !== 'mysql'
|
||||
|| ! $schema->hasColumn('supplier_payable_overrides', 'summary_month_key')
|
||||
|| ! $schema->hasColumn('supplier_payable_overrides', 'record_date_key')
|
||||
|| ! $schema->hasColumn('supplier_payable_overrides', 'supplier_name_key')) {
|
||||
return $this->distinctSuppliersForFilters(
|
||||
$dataset,
|
||||
$companyId,
|
||||
$month,
|
||||
$reconciler,
|
||||
true,
|
||||
);
|
||||
}
|
||||
|
||||
$start = $month . '-01';
|
||||
$next = \Illuminate\Support\Carbon::createFromFormat('!Y-m', $month, 'Asia/Shanghai')
|
||||
->addMonthNoOverflow()
|
||||
->toDateString();
|
||||
$sendTime = $this->jsonStringExpression($connection, 'send_time');
|
||||
$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],
|
||||
);
|
||||
});
|
||||
})
|
||||
->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()
|
||||
->all();
|
||||
sort($suppliers, SORT_NATURAL | SORT_FLAG_CASE);
|
||||
|
||||
return $suppliers;
|
||||
}
|
||||
|
||||
/**
|
||||
* 只返回指定账期涉及的供应商,供月度催办统计使用。
|
||||
*
|
||||
|
||||
@ -87,12 +87,11 @@ class SupplierReconciliationPageData
|
||||
) as $name) {
|
||||
$suppliers[$name] = true;
|
||||
}
|
||||
foreach ($this->store->distinctSuppliersForFilters(
|
||||
foreach ($this->store->distinctSuppliersForSendTimeMonth(
|
||||
$returnDataset,
|
||||
$companyId,
|
||||
$month,
|
||||
$reconciler,
|
||||
true,
|
||||
) as $name) {
|
||||
$suppliers[$name] = true;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user