From 9878a1fc5c25bf6c69ee292b83137b1de1e95b5a Mon Sep 17 00:00:00 2001 From: Jing Li Date: Tue, 16 Apr 2024 15:05:32 +0800 Subject: [PATCH] Update --- .../Controllers/Api/StatisticsController.php | 121 ++---------------- app/Services/StatisticService.php | 109 ++++++++++++++++ 2 files changed, 119 insertions(+), 111 deletions(-) diff --git a/app/Http/Controllers/Api/StatisticsController.php b/app/Http/Controllers/Api/StatisticsController.php index 8cb454c..844176c 100644 --- a/app/Http/Controllers/Api/StatisticsController.php +++ b/app/Http/Controllers/Api/StatisticsController.php @@ -3,12 +3,8 @@ namespace App\Http\Controllers\Api; use App\Admin\Filters\LedgerFilter; -use App\Admin\Filters\LedgerItemFilter; -use App\Models\Keyword; use App\Models\Ledger; -use App\Models\LedgerItem; use App\Services\StatisticService; -use Illuminate\Database\Eloquent\Collection; use Illuminate\Http\Request; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\DB; @@ -18,7 +14,7 @@ class StatisticsController extends Controller /** * 首页统计 */ - public function dashboard(Request $request): array + public function dashboard(Request $request, StatisticService $statisticService): array { $query = Ledger::filter( $this->defaultFilterInput($request), LedgerFilter::class @@ -40,34 +36,6 @@ class StatisticsController extends Controller //-------------------------------------------------------------------------- $yesterdayLedger = (clone $query)->where('date', $yesterday->format('Y-m-d'))->first(); - //-------------------------------------------------------------------------- - // 近 30 天趋势数据 - //-------------------------------------------------------------------------- - $startAt = $yesterday->copy()->subDays(29); - $endAt = $yesterday->copy(); - - /** @var \Illuminate\Database\Eloquent\Collection */ - $ledgers30days = (clone $query) - ->whereBetween('date', [$startAt, $endAt]) - ->get(['date', 'sales', 'expenditure']) - ->keyBy('date'); - - $trend = collect(); - - while ($startAt->lte($endAt)) { - $ledger = $ledgers30days->get( - $date = $startAt->format('Y-m-d') - ); - - $trend->push([ - 'date' => $date, - 'sales' => trim_zeros($ledger->sales ?? 0), - 'expenditure' => trim_zeros($ledger->expenditure ?? 0), - ]); - - $startAt->addDay(); - } - return [ // 本月总账录入 'current_month_ledger' => [ @@ -83,7 +51,9 @@ class StatisticsController extends Controller 'expenditure' => trim_zeros($yesterdayLedger->expenditure ?? 0), ], // 近30天趋势数据 - 'trend_data_of_30days' => $trend, + 'trend_data_of_30days' => $statisticService->ledgerTrends( + $yesterday->copy()->subDays(29), $yesterday->copy() + ), ]; } @@ -116,7 +86,7 @@ class StatisticsController extends Controller /** * 销售统计 */ - public function sales(Request $request): array + public function sales(Request $request, StatisticService $statisticService): array { $request->validate( rules: [ @@ -129,82 +99,11 @@ class StatisticsController extends Controller ], ); - // 开始日期 - $startAt = Carbon::parse($request->input('start_at')); - // 结束日期 - $endAt = Carbon::parse($request->input('end_at')); - - $input = $this->defaultFilterInput($request); - - /** @var \Illuminate\Database\Eloquent\Collection */ - $lotteryTypes = Keyword::where('parent_key', 'lottery_type')->get(); - - /** @var \Illuminate\Support\Collection */ - $ledgerStatistics = Ledger::select([ - 'date', - DB::raw('SUM(new_customers) as new_customers'), - DB::raw('SUM(sales) as sales'), - DB::raw('SUM(expenditure) as expenditure') - ]) - ->filter($input, LedgerFilter::class) - ->whereBetween('date', [$startAt->format('Y-m-d'), $endAt->format('Y-m-d')]) - ->groupBy(['date']) - ->get() - ->keyBy('date'); - - /** @var \Illuminate\Support\Collection */ - $ledgerItemStatistics = LedgerItem::select([ - 'date', - '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')) - ->whereBetween('date', [$startAt->format('Y-m-d'), $endAt->format('Y-m-d')]) - ->groupBy(['date', 'ledger_item_type_id']) - ->get() - ->groupBy('date'); - - $data = collect(); - $date = $endAt->copy(); - - while ($date->gte($startAt)) { - $_date = $date->format('Y-m-d'); - $ledgerStatistic = $ledgerStatistics->get($_date); - /** @var \Illuminate\Support\Collection */ - $lotteryTypeStatistics = $ledgerItemStatistics->get($_date, collect())->keyBy('ledger_item_type_id'); - - $lotteryTypes->map(function ($lotteryType) use ($lotteryTypeStatistics) { - $lotteryTypeStatistic = $lotteryTypeStatistics->get($lotteryType->key); - return [ - 'name' => $lotteryType->name, - 'sales' => trim_zeros($lotteryTypeStatistic->sales ?? 0), - 'expenditure' => trim_zeros($lotteryTypeStatistic->expenditure ?? 0), - ]; - }); - - $data->push([ - 'date' => $_date, - 'ledger' => [ - 'new_customers' => $ledgerStatistic->new_customers ?? 0, - 'sales' => trim_zeros($ledgerStatistic->sales ?? 0), - 'expenditure' => trim_zeros($ledgerStatistic->expenditure ?? 0), - ], - 'lottery_types' => $lotteryTypes->map(function ($lotteryType) use ($lotteryTypeStatistics) { - $lotteryTypeStatistic = $lotteryTypeStatistics->get($lotteryType->key); - return [ - 'name' => $lotteryType->name, - 'sales' => trim_zeros($lotteryTypeStatistic->sales ?? 0), - 'expenditure' => trim_zeros($lotteryTypeStatistic->expenditure ?? 0), - ]; - }), - ]); - - $date->subDay(); - } - - return $data->all(); + return $statisticService->sales( + Carbon::parse($request->input('start_at')), + Carbon::parse($request->input('end_at')), + $this->defaultFilterInput($request), + ); } /** diff --git a/app/Services/StatisticService.php b/app/Services/StatisticService.php index 734c7c3..54e6424 100644 --- a/app/Services/StatisticService.php +++ b/app/Services/StatisticService.php @@ -3,14 +3,47 @@ namespace App\Services; use App\Admin\Filters\LedgerFilter; +use App\Admin\Filters\LedgerItemFilter; use App\Admin\Filters\StoreFilter; +use App\Models\Keyword; use App\Models\Ledger; +use App\Models\LedgerItem; use App\Models\Store; use Illuminate\Support\Arr; +use Illuminate\Support\Carbon; use Illuminate\Support\Facades\DB; class StatisticService { + /** + * 总账趋势 + */ + public function ledgerTrends(Carbon $start, Carbon $end, array $input = []): array + { + $ledgers = Ledger::filter($input, LedgerFilter::class) + ->whereBetween('date', [$start->format('Y-m-d'), $end->format('Y-m-d')]) + ->get(['date', 'sales', 'expenditure']) + ->keyBy('date'); + + $data = collect(); + + while ($start->lte($end)) { + $ledger = $ledgers->get( + $date = $start->format('Y-m-d') + ); + + $data->push([ + 'date' => $date, + 'sales' => trim_zeros($ledger->sales ?? 0), + 'expenditure' => trim_zeros($ledger->expenditure ?? 0), + ]); + + $start->addDay(); + } + + return $data->all(); + } + /** * 门店统计 */ @@ -41,4 +74,80 @@ class StatisticService ]; })->all(); } + + /** + * 销售统计 + */ + public function sales(Carbon $start, Carbon $end, array $input = []): array + { + /** @var \Illuminate\Database\Eloquent\Collection */ + $lotteryTypes = Keyword::where('parent_key', 'lottery_type')->get(); + + /** @var \Illuminate\Support\Collection */ + $ledgerStatistics = Ledger::select([ + 'date', + DB::raw('SUM(new_customers) as new_customers'), + DB::raw('SUM(sales) as sales'), + DB::raw('SUM(expenditure) as expenditure') + ]) + ->filter($input, LedgerFilter::class) + ->whereBetween('date', [$start->format('Y-m-d'), $end->format('Y-m-d')]) + ->groupBy(['date']) + ->get() + ->keyBy('date'); + + /** @var \Illuminate\Support\Collection */ + $ledgerItemStatistics = LedgerItem::select([ + 'date', + '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')) + ->whereBetween('date', [$start->format('Y-m-d'), $end->format('Y-m-d')]) + ->groupBy(['date', 'ledger_item_type_id']) + ->get() + ->groupBy('date'); + + $data = collect(); + + while ($end->gte($start)) { + $date = $end->format('Y-m-d'); + + $ledgerStatistic = $ledgerStatistics->get($date); + /** @var \Illuminate\Support\Collection */ + $lotteryTypeStatistics = $ledgerItemStatistics->get($date, collect())->keyBy('ledger_item_type_id'); + + $lotteryTypes->map(function ($lotteryType) use ($lotteryTypeStatistics) { + $lotteryTypeStatistic = $lotteryTypeStatistics->get($lotteryType->key); + return [ + 'name' => $lotteryType->name, + 'sales' => trim_zeros($lotteryTypeStatistic->sales ?? 0), + 'expenditure' => trim_zeros($lotteryTypeStatistic->expenditure ?? 0), + ]; + }); + + $data->push([ + 'date' => $date, + 'ledger' => [ + 'new_customers' => $ledgerStatistic->new_customers ?? 0, + 'sales' => trim_zeros($ledgerStatistic->sales ?? 0), + 'expenditure' => trim_zeros($ledgerStatistic->expenditure ?? 0), + ], + 'lottery_types' => $lotteryTypes->map(function ($lotteryType) use ($lotteryTypeStatistics) { + $lotteryTypeStatistic = $lotteryTypeStatistics->get($lotteryType->key); + return [ + 'name' => $lotteryType->name, + 'sales' => trim_zeros($lotteryTypeStatistic->sales ?? 0), + 'expenditure' => trim_zeros($lotteryTypeStatistic->expenditure ?? 0), + ]; + }), + ]); + + $end->subDay(); + } + + return $data->all(); + } }