diff --git a/app/Http/Controllers/Admin/ChartController.php b/app/Http/Controllers/Admin/ChartController.php index c9635a4..744b5c9 100644 --- a/app/Http/Controllers/Admin/ChartController.php +++ b/app/Http/Controllers/Admin/ChartController.php @@ -6,6 +6,7 @@ use App\Models\Visit; use App\Models\VisitTime; use Illuminate\Support\Facades\Validator; use App\Helpers\ResponseCode; +use Rap2hpoutre\FastExcel\FastExcel; /** @@ -21,6 +22,7 @@ class ChartController extends CommonController * 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", @@ -51,7 +53,20 @@ class ChartController extends CommonController 'cancel_total' => Visit::where('date', 'like', "%{$month}%")->where('audit_status', 5)->count(), ]; } - return $this->success($list); + + 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); + } } @@ -62,6 +77,7 @@ class ChartController extends CommonController * 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", @@ -81,17 +97,29 @@ class ChartController extends CommonController if ($validator->fails()) { return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]); } - $timeList = VisitTime::orderBy('start_time','asc')->get(); + $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(), + '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(), ]; } - return $this->success($list); + 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); + } } /** @@ -101,6 +129,7 @@ class ChartController extends CommonController * 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", @@ -126,12 +155,24 @@ class ChartController extends CommonController $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(), + '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(), ]; } - return $this->success($list); + 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); + } } }