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

229 lines
7.4 KiB

<?php
namespace App\Http\Controllers\Admin;
use App\Exceptions\CustomException;
use App\Models\AdminFloorRegion;
use App\Models\ParkingInformation;
use App\Models\ParkingLicensePlate;
use App\Models\ParkingSpace;
use App\Models\ParkingSpaceType;
use App\Services\ApiResponseService;
use App\Services\ParkingInformationService;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
use Psr\SimpleCache\InvalidArgumentException;
class ParkingInformationController extends BaseController
{
/**
* @var ParkingInformationService
*/
protected ParkingInformationService $service;
/**
* 构造函数
* @param ApiResponseService $responseService
* @param ParkingInformationService $service
*/
public function __construct(
ApiResponseService $responseService,
ParkingInformationService $service
) {
parent::__construct($responseService);
$this->service = $service;
}
/**
* Display a listing of the resource.
*/
public function index(Request $request): JsonResponse
{
try {
$query = ParkingInformation::query();
// 车位号码搜索
if ($request->has('parking_space_number')) {
$parking_space_number = $request->input('parking_space_number');
if ($parking_space_number) {
$space_ids = ParkingSpace::getId($parking_space_number);
if ($space_ids) {
$query->whereIn('space_id', $space_ids);
} else {
$query->where('id', 0);
}
}
}
if ($request->has('license_plate')) {
$license_plate = $request->input('license_plate');
if ($license_plate) {
$license_plate_ids = ParkingLicensePlate::getId(
$license_plate
);
if ($license_plate_ids) {
$query->whereIn('license_plate_id', $license_plate_ids);
} else {
$query->where('id', 0);
}
}
}
if ($request->has('entry_start_date')) {
$entry_start_date = $request->input('entry_start_date');
if ($entry_start_date && strtotime($entry_start_date)) {
$query->where('entry_time', '>=', $entry_start_date);
}
}
if ($request->has('entry_end_date')) {
$entry_end_date = $request->input('entry_end_date');
if ($entry_end_date && strtotime($entry_end_date)) {
$query->where('entry_time', '<=', $entry_end_date);
}
}
if ($request->has('parking_space_type')) {
$type = $request->input('parking_space_type');
if ($type) {
$query->where('space_type_id', '=', $type);
}
}
// 分页
$page = $request->input('page', 1);
$perPage = $request->input('per_page', 10);
$total = $query->count();
$items = $query->latest()->forPage($page, $perPage)->select()
->get()->each(function ($item) {
$item['floor_region_name'] = AdminFloorRegion::getName(
$item['floor_region_id']
);
$item['parking_space'] = ParkingSpace::getNumber(
$item['space_id']
);
$item['license_plate'] = ParkingLicensePlate::getNumber(
$item['license_plate_id']
);
$item['parking_space_type'] = ParkingSpaceType::getName(
$item['space_type_id']
);
unset($item['floor_region_id'], $item['space_id'], $item['license_plate_id'], $item['space_type_id']);
return $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 = [
'parking_space_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 store(Request $request): JsonResponse
{
try {
$data = $request->all();
$rules = [
'license_plate' => 'required',
];
$messages = [
'license_plate.required' => __(
'validation.license_plate.n_empty'
)
];
$validator = Validator::make($data, $rules, $messages);
if ($validator->fails()) {
throw new ValidationException($validator);
}
$this->service->createModel($data);
return $this->responseService->success(
null,
__('admin.save_succeeded')
);
} catch (ValidationException|CustomException $e) {
throw $e;
} catch (Exception $e) {
return $this->responseService->systemError(
__('exception.parking_information.create_failed') . ':'
. $e->getMessage()
);
}
}
/**
* @param string $id
* @return JsonResponse
* @throws CustomException
* @throws ValidationException
*/
public function destroy(string $id): JsonResponse
{
try {
$this->validateId($id, ParkingInformation::class);
$this->service->deleteModel($id);
return $this->responseService->success(
null,
__('admin.delete_succeeded')
);
} catch (ValidationException|CustomException $e) {
throw $e;
} catch (Exception $e) {
return $this->responseService->systemError(
__('exception.license_plate.destroy_failed') . ':'
. $e->getMessage()
);
}
}
/**
* @return JsonResponse
* @throws InvalidArgumentException
*/
public function rule(): JsonResponse
{
try {
return $this->responseService->success(
$this->methodShow('information')
);
} catch (Exception $e) {
return $this->responseService->systemError(
__('exception.get_data_failed') . ':' . $e->getMessage()
);
}
}
}