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.
133 lines
4.9 KiB
133 lines
4.9 KiB
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Exports\ParkingBehaviorExport;
|
|
use App\Models\ParkingAccessRecord;
|
|
use App\Models\ParkingInformation;
|
|
use App\Models\ParkingLicensePlate;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Exception;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
|
|
|
class ParkingBehaviorController extends BaseController
|
|
{
|
|
protected string $menuUri = 'parkingBehavior';
|
|
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
try {
|
|
$select = " SELECT DATE_FORMAT(FROM_UNIXTIME(enter_time), "
|
|
. "'%Y-%m-%d') AS time_period, `license_plate_id`, "
|
|
. "COUNT(license_plate_id) AS parking_frequency, SUM(COALESCE("
|
|
. "leave_time, UNIX_TIMESTAMP()) - enter_time) AS parking_duration";
|
|
$from = " FROM `parking_access_record` ";
|
|
$group = " GROUP BY DATE_FORMAT(FROM_UNIXTIME(enter_time),"
|
|
. " '%Y-%m-%d'), `license_plate_id`";
|
|
|
|
$whereArr = [];
|
|
if ($request->has('start_time') && $request->has('end_time')) {
|
|
$start_time = $request->input('start_time');
|
|
$end_time = $request->input('end_time');
|
|
if ($start_time && $end_time) {
|
|
$end_times = strtotime($end_time . ' 23:59:59');
|
|
$start_times = strtotime($start_time . ' 00:00:00');
|
|
if ($start_times && $end_times) {
|
|
$whereArr[]
|
|
= " enter_time BETWEEN {$start_times} and {$end_times}";
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($request->has('license_plate')) {
|
|
$license_plate = $request->input('license_plate');
|
|
if ($license_plate) {
|
|
$license_plate_id = ParkingLicensePlate::getValueId(
|
|
$license_plate
|
|
);
|
|
if ($license_plate_id) {
|
|
$whereArr[] = " license_plate_id = {$license_plate_id}";
|
|
} else {
|
|
$whereArr[] = " id = 0";
|
|
}
|
|
}
|
|
}
|
|
|
|
$where = $whereArr ? ' WHERE ' . implode(' AND ', $whereArr) : '';
|
|
|
|
// 分页
|
|
$page = $request->input('page', 1);
|
|
$perPage = $request->input('per_page', 10);
|
|
|
|
$havingArr = [];
|
|
if ($request->has('min_count') && $request->has('max_count')) {
|
|
$min_count = $request->input('min_count');
|
|
$max_count = $request->input('max_count');
|
|
if (is_numeric($min_count) && is_numeric($max_count)) {
|
|
$havingArr[]
|
|
= "(parking_frequency >= {$min_count} AND parking_frequency <= {$max_count})";
|
|
}
|
|
}
|
|
|
|
if ($request->has('min_times') && $request->has('max_times')) {
|
|
$min_times = $request->input('min_times');
|
|
$max_times = $request->input('max_times');
|
|
if (is_numeric($min_times) && is_numeric($max_times)) {
|
|
$havingArr[]
|
|
= "(parking_duration >= {$min_times} AND parking_duration <= {$max_times})";
|
|
}
|
|
}
|
|
$having = $havingArr ? " HAVING " . implode(' AND ', $havingArr)
|
|
: '';
|
|
|
|
$total = 0;
|
|
$all = DB::select($select . $from . $where . $group . $having);
|
|
|
|
if ($all) {
|
|
$total = count($all);
|
|
}
|
|
$offset = ($page - 1) * $perPage;
|
|
|
|
$limit = " LIMIT {$offset}, {$perPage}";
|
|
$items = DB::select(
|
|
$select . $from . $where . $group . $having . $limit
|
|
);
|
|
|
|
foreach ($items as &$item) {
|
|
$license_plate = ParkingLicensePlate::getNumber(
|
|
$item->license_plate_id
|
|
);
|
|
$item->license_plate = $license_plate;
|
|
unset($item->license_plate_id);
|
|
}
|
|
return $this->responseService->success([
|
|
'items' => $items,
|
|
'total' => $total,
|
|
'page' => $page,
|
|
'per_page' => $perPage,
|
|
'last_page' => ceil($total / $perPage)
|
|
]);
|
|
} catch (Exception $e) {
|
|
$m_prefix = __('exception.exception_handler.resource');
|
|
return $this->responseService->systemError(
|
|
$m_prefix . ':' . $e->getMessage()
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return BinaryFileResponse
|
|
*/
|
|
public function export(): BinaryFileResponse
|
|
{
|
|
return Excel::download(
|
|
new ParkingBehaviorExport(),
|
|
set_space_underline(__('exports.parking_behavior.list'))
|
|
. get_datetime('date_')
|
|
. '.xlsx'
|
|
);
|
|
}
|
|
}
|
|
|