generated from liutk/owl-admin-base
182 lines
5.2 KiB
PHP
182 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Services\StatisticService;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Carbon;
|
|
|
|
class StatisticsController extends Controller
|
|
{
|
|
/**
|
|
* 首页统计
|
|
*/
|
|
public function dashboard(Request $request, StatisticService $statisticService): array
|
|
{
|
|
$input = $this->defaultFilterInput($request);
|
|
|
|
// 昨天
|
|
$yesterday = Carbon::yesterday();
|
|
|
|
// 本月上报数据统计
|
|
$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,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 总账统计
|
|
*/
|
|
public function ledger(Request $request, StatisticService $statisticService)
|
|
{
|
|
$request->validate(
|
|
rules: [
|
|
'start_at' => ['bail', 'required', 'date_format:Y-m-d'],
|
|
'end_at' => ['bail', 'required', 'date_format:Y-m-d'],
|
|
'before_start_at' => ['bail', 'required', 'date_format:Y-m-d'],
|
|
'before_end_at' => ['bail', 'required', 'date_format:Y-m-d'],
|
|
],
|
|
attributes: [
|
|
'start_at' => '开始日期',
|
|
'end_at' => '结束日期',
|
|
'before_start_at' => '对比开始日期',
|
|
'before_end_at' => '对比结束日期',
|
|
],
|
|
);
|
|
|
|
$input = $this->defaultFilterInput($request);
|
|
|
|
$ledger = $statisticService->ledger(
|
|
Carbon::parse($request->input('start_at')),
|
|
Carbon::parse($request->input('end_at')),
|
|
$input,
|
|
);
|
|
|
|
$beforeLedger = $statisticService->ledger(
|
|
Carbon::parse($request->input('before_start_at')),
|
|
Carbon::parse($request->input('before_end_at')),
|
|
$input,
|
|
);
|
|
|
|
// 销售涨幅
|
|
$salesGrowthRate = 0;
|
|
|
|
if (bccomp($beforeLedger['sales'], '0', 2) === 0) {
|
|
$salesGrowthRate = '-';
|
|
} else {
|
|
$diff = bcsub($ledger['sales'], $beforeLedger['sales'], 2);
|
|
$salesGrowthRate = bcdiv(bcmul($diff, '100'), $beforeLedger['sales'], 2);
|
|
}
|
|
|
|
return array_merge($ledger, [
|
|
'sales_growth_rate' => $salesGrowthRate,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 门店统计
|
|
*/
|
|
public function stores(Request $request, StatisticService $statisticService)
|
|
{
|
|
$request->validate(
|
|
rules: [
|
|
'start_at' => ['bail', 'required', 'date_format:Y-m-d'],
|
|
'end_at' => ['bail', 'required', 'date_format:Y-m-d'],
|
|
],
|
|
attributes: [
|
|
'start_at' => '开始日期',
|
|
'end_at' => '结束日期',
|
|
],
|
|
);
|
|
|
|
$input = array_merge(
|
|
$this->defaultFilterInput($request),
|
|
$request->only(['start_at', 'end_at']),
|
|
);
|
|
|
|
$sorts = [['sales', 'desc'], ['id', 'asc']];
|
|
|
|
return $statisticService->stores($input, $sorts);
|
|
}
|
|
|
|
/**
|
|
* 销售统计
|
|
*/
|
|
public function sales(Request $request, StatisticService $statisticService): array
|
|
{
|
|
$request->validate(
|
|
rules: [
|
|
'start_at' => ['bail', 'required', 'date_format:Y-m-d'],
|
|
'end_at' => ['bail', 'required', 'date_format:Y-m-d'],
|
|
],
|
|
attributes: [
|
|
'start_at' => '开始日期',
|
|
'end_at' => '结束日期',
|
|
],
|
|
);
|
|
|
|
return $statisticService->sales(
|
|
Carbon::parse($request->input('start_at')),
|
|
Carbon::parse($request->input('end_at')),
|
|
$this->defaultFilterInput($request),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 处理区域和门店过滤条件
|
|
*/
|
|
protected function defaultFilterInput(Request $request): array
|
|
{
|
|
$input = [];
|
|
|
|
if ($request->filled('store_id')) {
|
|
$input['store_id'] = $request->input('store_id');
|
|
} else {
|
|
$region = [];
|
|
|
|
if ($request->filled('province_code')) {
|
|
$region['provinceCode'] = $request->input('province_code');
|
|
}
|
|
|
|
if ($request->filled('city_code')) {
|
|
$region['cityCode'] = $request->input('city_code');
|
|
}
|
|
|
|
$input['region'] = $region;
|
|
}
|
|
|
|
return $input;
|
|
}
|
|
}
|