generated from liutk/owl-admin-base
彩种销售趋势
parent
cc6e4e817f
commit
691e0a04ad
|
|
@ -4,7 +4,9 @@ namespace App\Admin\Controllers;
|
|||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Employee;
|
||||
use App\Models\Keyword;
|
||||
use App\Models\Ledger;
|
||||
use App\Models\LedgerItem;
|
||||
use App\Models\Store;
|
||||
use App\Models\TaskPerformance;
|
||||
use Illuminate\Http\Request;
|
||||
|
|
@ -41,10 +43,10 @@ class CockpitController extends Controller
|
|||
],
|
||||
);
|
||||
|
||||
$data = collect();
|
||||
|
||||
$last = $request->input('last');
|
||||
|
||||
$data = collect();
|
||||
|
||||
if (in_array($last, ['7days', '30days'])) {
|
||||
// 按天
|
||||
$days = match ($last) {
|
||||
|
|
@ -104,7 +106,7 @@ class CockpitController extends Controller
|
|||
$ledger = $ledgers->get($month);
|
||||
|
||||
$data->push([
|
||||
'date' => $month,
|
||||
'month' => $month,
|
||||
'sales' => trim_zeros($ledger->sales ?? 0),
|
||||
]);
|
||||
|
||||
|
|
@ -120,7 +122,119 @@ class CockpitController extends Controller
|
|||
*/
|
||||
public function lotterySalesTrend(Request $request): array
|
||||
{
|
||||
return [];
|
||||
$request->validate(
|
||||
rules: [
|
||||
'last' => ['bail', 'required', Rule::in(['7days', '30days', '180days', '365days'])],
|
||||
],
|
||||
);
|
||||
|
||||
$last = $request->input('last');
|
||||
|
||||
$data = collect();
|
||||
|
||||
/** @var \Illuminate\Database\Eloquent\Collection */
|
||||
$lotteryTypes = Keyword::where('parent_key', 'lottery_type')->get();
|
||||
|
||||
if (in_array($last, ['7days', '30days'])) {
|
||||
// 按天
|
||||
$days = match ($last) {
|
||||
'7days' => 7,
|
||||
'30days' => 30,
|
||||
};
|
||||
|
||||
// 今天
|
||||
$today = Carbon::today();
|
||||
// 开始时间
|
||||
$startAt = $today->copy()->subDays($days);
|
||||
// 结束时间
|
||||
$endAt = $today->copy()->subDay();
|
||||
|
||||
/** @var \Illuminate\Support\Collection */
|
||||
$lotteryTypeLedgers = LedgerItem::select([
|
||||
'date',
|
||||
'ledger_item_type_id',
|
||||
DB::raw('SUM(sales) as sales'),
|
||||
])
|
||||
->whereBetween('date', [$startAt->toDateString(), $endAt->toDateString()])
|
||||
->groupBy(['date', 'ledger_item_type_id'])
|
||||
->get()
|
||||
->groupBy('date');
|
||||
|
||||
while ($startAt->lte($endAt)) {
|
||||
$date = $startAt->toDateString();
|
||||
|
||||
$lotteryTypeLedgerItems = $lotteryTypeLedgers->get($date, collect())->keyBy('ledger_item_type_id');
|
||||
|
||||
$data->push([
|
||||
'date' => $date,
|
||||
'data' => $lotteryTypes->map(function ($lotteryType) use ($lotteryTypeLedgerItems) {
|
||||
$lotteryTypeLedgerItem = $lotteryTypeLedgerItems->get($lotteryType->key);
|
||||
return [
|
||||
'id' => $lotteryType->key,
|
||||
'name' => $lotteryType->name,
|
||||
'sales' => trim_zeros($lotteryTypeLedgerItem->sales ?? 0),
|
||||
];
|
||||
}),
|
||||
]);
|
||||
|
||||
$startAt->addDay();
|
||||
}
|
||||
}
|
||||
elseif (in_array($last, ['180days', '365days'])) {
|
||||
// 按月
|
||||
$months = match ($last) {
|
||||
'180days' => 6, // 6个月
|
||||
'365days' => 12, // 12个月
|
||||
};
|
||||
|
||||
// 今天
|
||||
$today = Carbon::today();
|
||||
// 开始时间
|
||||
$startAt = $today->copy()->startOfMonth()->subMonths($months);
|
||||
// 结束时间
|
||||
$endAt = $today->copy()->startOfMonth()->subMonth()->endOfMonth();
|
||||
|
||||
/** @var \Illuminate\Support\Collection */
|
||||
$lotteryTypeLedgers = LedgerItem::select([
|
||||
DB::raw("DATE_FORMAT(`date`, '%Y-%m') as month"),
|
||||
'ledger_item_type_id',
|
||||
DB::raw('SUM(sales) as sales'),
|
||||
])
|
||||
->whereBetween('date', [$startAt->toDateString(), $endAt->toDateString()])
|
||||
->groupBy(['month', 'ledger_item_type_id'])
|
||||
->get()
|
||||
->keyBy('month');
|
||||
|
||||
for ($i=0; $i < $months; $i++) {
|
||||
$month = $startAt->format('Y-m');
|
||||
|
||||
$lotteryTypeLedgerItems = $lotteryTypeLedgers->get($month, collect())->keyBy('ledger_item_type_id');
|
||||
|
||||
$data->push([
|
||||
'month' => $month,
|
||||
'data' => $lotteryTypes->map(function ($lotteryType) use ($lotteryTypeLedgerItems) {
|
||||
$lotteryTypeLedgerItem = $lotteryTypeLedgerItems->get($lotteryType->key);
|
||||
return [
|
||||
'id' => $lotteryType->key,
|
||||
'name' => $lotteryType->name,
|
||||
'sales' => trim_zeros($lotteryTypeLedgerItem->sales ?? 0),
|
||||
];
|
||||
}),
|
||||
]);
|
||||
|
||||
$startAt->addMonth();
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'lottery_types' => $lotteryTypes->map(function ($lotteryType) {
|
||||
return [
|
||||
'id' => $lotteryType->id,
|
||||
'name' => $lotteryType->name,
|
||||
];
|
||||
}),
|
||||
'data' => $data->all(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in New Issue