You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

267 lines
12 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Http\Controllers\Admin;
use App\Models\Visit;
use App\Models\VisitTime;
use Illuminate\Support\Facades\Validator;
use App\Helpers\ResponseCode;
use Rap2hpoutre\FastExcel\FastExcel;
/**
* 配置
*/
class ChartController extends CommonController
{
/**
* @OA\Get(
* path="/api/admin/chart/home",
* tags={"图表"},
* summary="首页统计",
* description="",
* @OA\Parameter(name="start_date", in="query", @OA\Schema(type="string"), required=false, description="开始日期例如2022-01-01"),
* @OA\Parameter(name="end_date", in="query", @OA\Schema(type="string"), required=false, description="结束日期例如2022-12-31"),
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
* description="暂无"
* )
* )
*/
public function home()
{
$all = request()->all();
$startDate = $all['start_date'] ?? null;
$endDate = $all['end_date'] ?? null;
$today = date('Y-m-d');
$list = [
'common_visit' => [
// 总预约数
'total' => Visit::visitHome(1, $startDate, $endDate, [Visit::AUDIT_STATUS_APPROVED, Visit::AUDIT_STATUS_ENTERED, Visit::AUDIT_STATUS_LEFT]),
// 总入场人数
'enter_visit' => Visit::visitHome(1, $startDate, $endDate, [Visit::AUDIT_STATUS_ENTERED, Visit::AUDIT_STATUS_LEFT]),
// 离厂统计人数
'leave_total' => Visit::visitHome(1, $startDate, $endDate, [Visit::AUDIT_STATUS_LEFT]),
// 今日总人数
'today_total' => Visit::visitHome(1, $today, $today, [Visit::AUDIT_STATUS_APPROVED, Visit::AUDIT_STATUS_ENTERED, Visit::AUDIT_STATUS_LEFT]),
// 今日入场人数
'today_enter_visit' => Visit::visitHome(1, $today, $today, [Visit::AUDIT_STATUS_ENTERED, Visit::AUDIT_STATUS_LEFT]),
// 今日离厂
'today_leave_total' => Visit::visitHome(1, $today, $today, [Visit::AUDIT_STATUS_LEFT]),
],
'work_visit' => [
// 总人数
'total' => Visit::visitHome(2, $startDate, $endDate, [Visit::AUDIT_STATUS_APPROVED, Visit::AUDIT_STATUS_ENTERED, Visit::AUDIT_STATUS_LEFT]),
// 总入场人数
'enter_visit' => Visit::visitHome(2, $startDate, $endDate, [Visit::AUDIT_STATUS_ENTERED, Visit::AUDIT_STATUS_LEFT]),
// 离厂统计
'leave_total' => Visit::visitHome(2, $startDate, $endDate, [Visit::AUDIT_STATUS_LEFT]),
// 今日总人数
'today_total' => Visit::visitHome(2, $today, $today, [Visit::AUDIT_STATUS_APPROVED, Visit::AUDIT_STATUS_ENTERED, Visit::AUDIT_STATUS_LEFT]),
// 今日入场人数
'today_enter_visit' => Visit::visitHome(2, $today, $today, [Visit::AUDIT_STATUS_ENTERED, Visit::AUDIT_STATUS_LEFT]),
// 今日离厂
'today_leave_total' => Visit::visitHome(2, $today, $today, [Visit::AUDIT_STATUS_LEFT]),
],
'car_visit' => [
// 总人数
'total' => Visit::visitHome(3, $startDate, $endDate, [Visit::AUDIT_STATUS_APPROVED, Visit::AUDIT_STATUS_ENTERED, Visit::AUDIT_STATUS_LEFT]),
// 总入场人数
'enter_visit' => Visit::visitHome(3, $startDate, $endDate, [Visit::AUDIT_STATUS_ENTERED, Visit::AUDIT_STATUS_LEFT]),
// 离厂统计
'leave_total' => Visit::visitHome(3, $startDate, $endDate, [Visit::AUDIT_STATUS_LEFT]),
// 今日总人数
'today_total' => Visit::visitHome(3, $today, $today, [Visit::AUDIT_STATUS_APPROVED, Visit::AUDIT_STATUS_ENTERED, Visit::AUDIT_STATUS_LEFT]),
// 今日入场人数
'today_enter_visit' => Visit::visitHome(3, $today, $today, [Visit::AUDIT_STATUS_ENTERED, Visit::AUDIT_STATUS_LEFT]),
// 今日离厂
'today_leave_total' => Visit::visitHome(3, $today, $today, [Visit::AUDIT_STATUS_LEFT]),
],
];
// 图表日期范围如果有时间筛选参数使用筛选参数否则使用最近30天
$chartStartDate = $startDate ?: date('Y-m-d', strtotime('-30 day', time()));
$chartEndDate = $endDate ?: date('Y-m-d');
$dateList = getDateFromRange($chartStartDate, $chartEndDate);
$all_date_list = [];
foreach ($dateList as &$item) {
$all_date_list[] = [
'date' => $item,
'common_visit' => Visit::where('type', 1)->where('date', $item)->count(),
'work_visit' => Visit::where('type', 2)->where('date', $item)->count(),
'car_visit' => Visit::where('type', 3)->where('date', $item)->count(),
];
}
return $this->success(compact('list', 'all_date_list'));
}
/**
* @OA\Get(
* path="/api/admin/chart/month",
* tags={"图表"},
* summary="月度统计",
* description="",
* @OA\Parameter(name="year", in="query", @OA\Schema(type="string"), required=true, description="年份例如2022"),
* @OA\Parameter(name="is_export", in="query", @OA\Schema(type="string"), required=false, description="是否导出0否1是默认0"),
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
* description="暂无"
* )
* )
*/
public function month()
{
$all = \request()->all();
$messages = [
'year.required' => '年份必填',
];
$validator = Validator::make($all, [
'year' => 'required'
], $messages);
if ($validator->fails()) {
return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
}
$monthList = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'];
$list = [];
foreach ($monthList as $item) {
$month = $all['year'] . '-' . $item;
$list[] = [
'month' => $month,
'plan_total' => Visit::where('date', 'like', "%{$month}%")->count(),
'enter_total' => Visit::where('date', 'like', "%{$month}%")->whereNotNull('accept_admin_sign')->count(),
'cancel_total' => Visit::where('date', 'like', "%{$month}%")->where('audit_status', 5)->count(),
];
}
if (isset($all['is_export']) && !empty($all['is_export'])) {
return (new FastExcel($list))->download('月度统计' . date('YmdHis') . '.csv', function ($info) {
return [
'月份' => $info['month'],
'预约人数' => $info['plan_total'],
'入场人数' => $info['enter_total'],
'取消人数' => $info['cancel_total'],
'核销比' => $info['enter_total'] / $info['plan_total'] * 100
];
});
} else {
return $this->success($list);
}
}
/**
* @OA\Get(
* path="/api/admin/chart/time",
* tags={"图表"},
* summary="时段统计",
* description="",
* @OA\Parameter(name="date", in="query", @OA\Schema(type="string"), required=true, description="日期例如2022=01=01"),
* @OA\Parameter(name="is_export", in="query", @OA\Schema(type="string"), required=false, description="是否导出0否1是默认0"),
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
* description="暂无"
* )
* )
*/
public function time()
{
$all = \request()->all();
$messages = [
'date.required' => '时段必填',
];
$validator = Validator::make($all, [
'date' => 'required'
], $messages);
if ($validator->fails()) {
return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
}
$timeList = VisitTime::orderBy('start_time', 'asc')->get();
$list = [];
foreach ($timeList as $item) {
$list[] = [
'time' => $item->start_time . '-' . $item->end_time,
'plan_total' => Visit::where('date', $all['date'])->where('visit_time_id', $item->id)->count(),
'enter_total' => Visit::where('date', $all['date'])->where('visit_time_id', $item->id)->whereNotNull('accept_admin_sign')->count(),
'cancel_total' => Visit::where('date', $all['date'])->where('visit_time_id', $item->id)->where('audit_status', 5)->count(),
];
}
if (isset($all['is_export']) && !empty($all['is_export'])) {
return (new FastExcel($list))->download('车辆统计' . date('YmdHis') . '.csv', function ($info) {
return [
'时间段' => $info['time'],
'预约人数' => $info['plan_total'],
'入场人数' => $info['enter_total'],
'取消人数' => $info['cancel_total'],
'核销比' => $info['enter_total'] / $info['plan_total'] * 100
];
});
} else {
return $this->success($list);
}
}
/**
* @OA\Get(
* path="/api/admin/chart/car",
* tags={"图表"},
* summary="车辆统计",
* description="",
* @OA\Parameter(name="year", in="query", @OA\Schema(type="string"), required=true, description="年份例如2022"),
* @OA\Parameter(name="is_export", in="query", @OA\Schema(type="string"), required=false, description="是否导出0否1是默认0"),
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
* description="暂无"
* )
* )
*/
public function car()
{
$all = \request()->all();
$messages = [
'year.required' => '年份必填',
];
$validator = Validator::make($all, [
'year' => 'required'
], $messages);
if ($validator->fails()) {
return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
}
$monthList = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'];
$list = [];
foreach ($monthList as $item) {
$month = $all['year'] . '-' . $item;
$list[] = [
'month' => $month,
'plan_total' => Visit::where('type', 3)->where('date', 'like', "%{$month}%")->count(),
'enter_total' => Visit::where('type', 3)->where('date', 'like', "%{$month}%")->whereNotNull('accept_admin_sign')->count(),
'cancel_total' => Visit::where('type', 3)->where('date', 'like', "%{$month}%")->where('audit_status', 5)->count(),
];
}
if (isset($all['is_export']) && !empty($all['is_export'])) {
return (new FastExcel($list))->download('车辆统计' . date('YmdHis') . '.csv', function ($info) {
return [
'月份' => $info['month'],
'预约车辆数' => $info['plan_total'],
'入场车辆数' => $info['enter_total'],
'取消车辆数' => $info['cancel_total'],
'核销比' => $info['enter_total'] / $info['plan_total'] * 100
];
});
} else {
return $this->success($list);
}
}
}