perf(供应商货款预估): 优化合并明细分页查询
Co-authored-by: 苏木 <594326199@qq.com> Co-committed-by: 苏木 <594326199@qq.com>
This commit is contained in:
parent
414d4fd3ee
commit
c7f011fc93
@ -129,13 +129,7 @@ class SupplierPayableForecastRepository
|
||||
|
||||
$perPage = min(max($perPage, 1), 100);
|
||||
$page = max($page, 1);
|
||||
$sourceRows = (clone $query)
|
||||
->orderByDesc('occurred_at')
|
||||
->orderByDesc('source_rank')
|
||||
->orderByDesc('source_id')
|
||||
->orderByDesc('source_item_id')
|
||||
->forPage($page, $perPage)
|
||||
->get();
|
||||
$sourceRows = $this->pageSourceRows($filters, $page, $perPage);
|
||||
|
||||
$priceMap = $this->latestChangePrices(
|
||||
$sourceRows->pluck('product_code')->filter()->map(static fn (mixed $value): string => trim((string) $value))->unique()->values()->all()
|
||||
@ -285,12 +279,74 @@ class SupplierPayableForecastRepository
|
||||
->fromSub($this->goodsUnionQuery($filters), 'forecast_rows');
|
||||
}
|
||||
|
||||
/**
|
||||
* 页面分页分别读取两条索引有序数据,再在内存中合并;避免每页都对全部联合结果排序。
|
||||
*
|
||||
* @param array{date_from:string,date_to:string,keyword:string} $filters
|
||||
*/
|
||||
private function pageSourceRows(array $filters, int $page, int $perPage): Collection
|
||||
{
|
||||
$limit = $page * $perPage;
|
||||
$rows = $this->procurementSourceQuery($filters)
|
||||
->orderByDesc('h.io_date')
|
||||
->orderByDesc('h.Id')
|
||||
->orderByDesc('i.Id')
|
||||
->limit($limit)
|
||||
->get();
|
||||
|
||||
if ($this->shippingTableAvailable()) {
|
||||
$rows = $rows->concat(
|
||||
$this->shippingSourceQuery($filters)
|
||||
->orderByDesc('s.ship_time')
|
||||
->orderByDesc('s.id')
|
||||
->orderByDesc('s.sku_source_id')
|
||||
->limit($limit)
|
||||
->get(),
|
||||
);
|
||||
}
|
||||
|
||||
return $rows
|
||||
->sort(static function (object $left, object $right): int {
|
||||
$time = strcmp((string) $right->occurred_at, (string) $left->occurred_at);
|
||||
if ($time !== 0) {
|
||||
return $time;
|
||||
}
|
||||
|
||||
$rank = (int) $right->source_rank <=> (int) $left->source_rank;
|
||||
if ($rank !== 0) {
|
||||
return $rank;
|
||||
}
|
||||
|
||||
$source = (int) $right->source_id <=> (int) $left->source_id;
|
||||
|
||||
return $source !== 0
|
||||
? $source
|
||||
: (int) $right->source_item_id <=> (int) $left->source_item_id;
|
||||
})
|
||||
->slice(($page - 1) * $perPage, $perPage)
|
||||
->values();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array{date_from:string,date_to:string,keyword:string} $filters
|
||||
*/
|
||||
private function goodsUnionQuery(array $filters): \Illuminate\Database\Query\Builder
|
||||
{
|
||||
$purchase = $this->filteredProcurementQuery($filters)->select([
|
||||
$purchase = $this->procurementSourceQuery($filters);
|
||||
|
||||
if (! $this->shippingTableAvailable()) {
|
||||
return $purchase;
|
||||
}
|
||||
|
||||
return $purchase->unionAll($this->shippingSourceQuery($filters));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array{date_from:string,date_to:string,keyword:string} $filters
|
||||
*/
|
||||
private function procurementSourceQuery(array $filters): \Illuminate\Database\Query\Builder
|
||||
{
|
||||
return $this->filteredProcurementQuery($filters)->select([
|
||||
DB::raw("'purchase' AS source_type"),
|
||||
DB::raw('1 AS source_rank'),
|
||||
'h.Id AS source_id',
|
||||
@ -312,12 +368,14 @@ class SupplierPayableForecastRepository
|
||||
DB::raw("'' AS express_no"),
|
||||
'h.status AS source_status',
|
||||
]);
|
||||
}
|
||||
|
||||
if (! $this->shippingTableAvailable()) {
|
||||
return $purchase;
|
||||
}
|
||||
|
||||
$shipping = $this->filteredShippingQuery($filters)->select([
|
||||
/**
|
||||
* @param array{date_from:string,date_to:string,keyword:string} $filters
|
||||
*/
|
||||
private function shippingSourceQuery(array $filters): \Illuminate\Database\Query\Builder
|
||||
{
|
||||
return $this->filteredShippingQuery($filters)->select([
|
||||
DB::raw("'shipping' AS source_type"),
|
||||
DB::raw('2 AS source_rank'),
|
||||
's.id AS source_id',
|
||||
@ -339,8 +397,6 @@ class SupplierPayableForecastRepository
|
||||
's.express_no AS express_no',
|
||||
DB::raw('NULL AS source_status'),
|
||||
]);
|
||||
|
||||
return $purchase->unionAll($shipping);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user