main
Jing Li 2024-05-28 18:56:21 +08:00
parent 731f845bcb
commit 1c5f2ff203
1 changed files with 28 additions and 0 deletions

View File

@ -2,10 +2,14 @@
namespace App\Http\Controllers\Api;
use App\Admin\Filters\LedgerItemFilter;
use App\Models\Keyword;
use App\Models\LedgerItem;
use App\Services\StatisticService;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
class StatisticsController extends Controller
{
@ -111,8 +115,32 @@ class StatisticsController extends Controller
$salesGrowthRate = bcdiv(bcmul($diff, '100'), $beforeLedger['sales'], 2);
}
/** @var \Illuminate\Database\Eloquent\Collection */
$lotteryTypes = Keyword::where('parent_key', 'lottery_type')->oldest('sort')->get();
/** @var \Illuminate\Support\Collection */
$lotteryTypeStatistics = LedgerItem::select([
'ledger_item_type_id',
DB::raw('SUM(sales) as sales'),
DB::raw('SUM(expenditure) as expenditure'),
])
->filter($input, LedgerItemFilter::class)
->whereIn('ledger_item_type_id', $lotteryTypes->pluck('key'))
->groupBy(['ledger_item_type_id'])
->get()
->keyBy('ledger_item_type_id');
return array_merge($ledger, [
'sales_growth_rate' => $salesGrowthRate,
'lottery_types' => $lotteryTypes->map(function ($lotteryType) use ($lotteryTypeStatistics) {
$aggregate = $lotteryTypeStatistics->get($lotteryType->key);
return [
'name' => $lotteryType->name,
'sales' => trim_zeros($aggregate->sales ?? 0),
'expenditure' => trim_zeros($aggregate->expenditure ?? 0),
];
}),
]);
}