diff --git a/app/Http/Controllers/Api/StatisticsController.php b/app/Http/Controllers/Api/StatisticsController.php index 161c242..4b07090 100644 --- a/app/Http/Controllers/Api/StatisticsController.php +++ b/app/Http/Controllers/Api/StatisticsController.php @@ -2,12 +2,9 @@ namespace App\Http\Controllers\Api; -use App\Admin\Filters\LedgerFilter; -use App\Models\Ledger; use App\Services\StatisticService; use Illuminate\Http\Request; use Illuminate\Support\Carbon; -use Illuminate\Support\Facades\DB; class StatisticsController extends Controller { @@ -18,44 +15,43 @@ class StatisticsController extends Controller { $input = $this->defaultFilterInput($request); - $query = Ledger::filter($input, LedgerFilter::class); - // 昨天 $yesterday = Carbon::yesterday(); - //-------------------------------------------------------------------------- - // 本月总账录入 - //-------------------------------------------------------------------------- - $currentMonthLedger = (clone $query) - ->select([DB::raw('SUM(sales) as sales'), DB::raw('SUM(expenditure) as expenditure')]) - ->whereBetween('date', [$yesterday->copy()->startOfMonth()->format('Y-m-d'), $yesterday->format('Y-m-d')]) - ->first(); - - //-------------------------------------------------------------------------- - // 昨日总账录入 - //-------------------------------------------------------------------------- - $yesterdayLedger = (clone $query)->where('date', $yesterday->format('Y-m-d'))->first(); - - return [ - // 本月总账录入 - 'current_month_ledger' => [ - // 截止日期 - 'deadline' => $yesterday->format('Y-m-d'), - 'sales' => trim_zeros($currentMonthLedger->sales ?? 0), - 'expenditure' => trim_zeros($currentMonthLedger->expenditure ?? 0), - ], - // 昨日累计金额 - 'yesterday_ledger' => [ - 'date' => $yesterday->format('Y-m-d'), - 'sales' => trim_zeros($yesterdayLedger->sales ?? 0), - 'expenditure' => trim_zeros($yesterdayLedger->expenditure ?? 0), - ], - // 近30天趋势数据 - 'trend_data_of_30days' => $statisticService->ledgerTrends( - $yesterday->copy()->subDays(29), + // 本月上报数据统计 + $currentMonthLedger = array_merge( + ['deadline' => $yesterday->format('Y-m-d')], + $statisticService->ledger( + $yesterday->copy()->startOfMonth(), $yesterday->copy(), $input, ), + ); + + // 昨日总账录入 + $yesterdayLedger = array_merge( + ['date' => $yesterday->format('Y-m-d')], + $statisticService->ledger( + $yesterday->copy(), + $yesterday->copy(), + $input, + ), + ); + + // 近30天趋势数据 + $trendsOf30days = $statisticService->ledgerTrends( + $yesterday->copy()->subDays(29), + $yesterday->copy(), + $input, + ); + + return [ + // 本月总账录入 + 'current_month_ledger' => $currentMonthLedger, + // 昨日累计金额 + 'yesterday_ledger' => $yesterdayLedger, + // 近30天趋势数据 + 'trends_of_30days' => $trendsOf30days, ]; } @@ -108,6 +104,27 @@ class StatisticsController extends Controller ); } + public function sales2(Request $request, StatisticService $statisticService): array + { + $request->validate( + rules: [ + 'start_at' => ['bail', 'required', 'date_format:Y-m-d'], + ], + attributes: [ + 'start_at' => '开始日期', + ], + ); + + // yesterday + // + + 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 54e6424..16f04bb 100644 --- a/app/Services/StatisticService.php +++ b/app/Services/StatisticService.php @@ -16,7 +16,23 @@ use Illuminate\Support\Facades\DB; class StatisticService { /** - * 总账趋势 + * 上报数据统计 + */ + public function ledger(Carbon $start, Carbon $end, array $input = []): array + { + $ledger = Ledger::filter($input, LedgerFilter::class) + ->select([DB::raw('SUM(sales) as sales'), DB::raw('SUM(expenditure) as expenditure')]) + ->whereBetween('date', [$start->format('Y-m-d'), $end->format('Y-m-d')]) + ->first(); + + return [ + 'sales' => trim_zeros($ledger->sales ?? 0), + 'expenditure' => trim_zeros($ledger->expenditure ?? 0), + ]; + } + + /** + * 上报数据趋势 */ public function ledgerTrends(Carbon $start, Carbon $end, array $input = []): array {