generated from liutk/owl-admin-base
Update
parent
dde3b2e72c
commit
c23691613f
|
|
@ -50,33 +50,26 @@ class StoreStatisticController extends AdminController
|
|||
|
||||
protected function getStoreStatistics(Request $request): array
|
||||
{
|
||||
$stores = Store::filter($request->only(['region']), StoreFilter::class)
|
||||
->get(['id', 'title']);
|
||||
|
||||
/** @var \Illuminate\Database\Eloquent\Collection */
|
||||
$statistics = Ledger::select(['store_id', DB::raw('SUM(sales) as sales'), DB::raw('SUM(expenditure) as expenditure')])
|
||||
$stats = Ledger::with(['store'])
|
||||
->select(['store_id', DB::raw('SUM(sales) as sales'), DB::raw('SUM(expenditure) as expenditure')])
|
||||
->filter($request->input(), LedgerFilter::class)
|
||||
->groupBy('store_id')
|
||||
->get()
|
||||
->keyBy('store_id');
|
||||
->get();
|
||||
|
||||
// 排序规则
|
||||
$sortBy = [
|
||||
[$request->input('orderBy') ?: 'sales', $request->input('orderDir') ?: 'desc'],
|
||||
];
|
||||
|
||||
return $stores->map(function ($store) use ($statistics) {
|
||||
$statistic = $statistics->get($store->id);
|
||||
|
||||
return [
|
||||
'title' => $store->title,
|
||||
'sales' => trim_zeros($statistic->sales ?? '0'),
|
||||
'expenditure' => trim_zeros($statistic->expenditure ?? '0'),
|
||||
];
|
||||
})
|
||||
return $stats->map(fn ($item) => [
|
||||
'title' => $item->store->title,
|
||||
'sales' => trim_zeros($item->sales ?? '0'),
|
||||
'expenditure' => trim_zeros($item->expenditure ?? '0'),
|
||||
])
|
||||
->sortBy($sortBy)
|
||||
->values()
|
||||
->map(fn ($statistic, $key) => array_merge($statistic, ['ranking' => $key + 1]))
|
||||
->map(fn ($item, $key) => array_merge($item, ['ranking' => $key + 1]))
|
||||
->all();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue