停车场管理系统
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.
 
 

153 lines
4.5 KiB

<?php
namespace App\Http\Controllers\Admin;
use App\Models\ParkingGateControl;
use App\Models\ParkingLicensePlate;
use App\Models\ParkingSpaceType;
use App\Services\ApiResponseService;
use App\Services\ParkingGateControlService;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class ParkingGateControlController extends BaseController
{
protected string $menuUri = 'gateControl';
/**
* @var ParkingGateControlService
*/
protected ParkingGateControlService $service;
/**
* 构造函数
* @param ApiResponseService $responseService
* @param ParkingGateControlService $service
*/
public function __construct(
ApiResponseService $responseService,
ParkingGateControlService $service
) {
parent::__construct($responseService);
$this->service = $service;
}
/**
* @param Request $request
* @return JsonResponse
*/
public function index(Request $request): JsonResponse
{
try {
$query = ParkingGateControl::query();
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) {
$query->where('license_plate_id', $license_plate_id);
} else {
$query->where('id', 0);
}
}
}
if ($request->has('member_type')) {
$member_type = $request->input('member_type');
if ($member_type) {
$query->where('member_type', $member_type);
}
}
if ($request->has('enter_at')) {
$enter_at = $request->input('enter_at');
if ($enter_at) {
$query->where('enter_at', '>=', $enter_at);
}
}
if ($request->has('leave_at')) {
$leave_at = $request->input('leave_at');
if ($leave_at) {
$query->where('leave_at', '<=', $leave_at);
}
}
if ($request->has('customer_id')) {
$customer_id = $request->input('customer_id');
if ($customer_id) {
$query->where('customer_id', $customer_id);
}
}
if ($request->has('member_id')) {
$member_id = $request->input('member_id');
if ($member_id) {
$query->where('member_id', $member_id);
}
}
// 分页
$page = $request->input('page', 1);
$perPage = $request->input('per_page', 10);
$total = $query->count();
$items = $query->latest()->forPage($page, $perPage)->get()->each(
function ($item) {
return $this->service->getItem($item);
}
);
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 JsonResponse
*/
public function search(): JsonResponse
{
try {
$data = [
'member_type_list' => ParkingSpaceType::getData()
];
return $this->responseService->success($data);
} catch (Exception $e) {
$m_prefix = __('exception.exception_handler.resource');
return $this->responseService->systemError(
$m_prefix . ':' . $e->getMessage()
);
}
}
public function show(string $id): JsonResponse
{
try {
$data = ParkingGateControl::query()->find($id);
return $this->responseService->success($this->service->getItem($data));
} catch (Exception $e) {
return $this->responseService->systemError(
__('exception.get_data_failed') . ':' . $e->getMessage()
);
}
}
}