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

345 lines
10 KiB

<?php
namespace App\Http\Controllers\Admin;
use App\Exceptions\CustomException;
use App\Exports\ParkingCameraExport;
use App\Models\AdminFloor;
use App\Models\ParkingCamera;
use App\Models\ParkingSpaceAttributes;
use App\Models\ParkingSpaceCamera;
use App\Services\AdminFloorService;
use App\Services\ApiResponseService;
use App\Services\ParkingCameraService;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
use JetBrains\PhpStorm\ArrayShape;
use Maatwebsite\Excel\Facades\Excel;
use Psr\SimpleCache\InvalidArgumentException;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
class ParkingCameraController extends BaseController
{
protected ParkingCameraService $service;
/**
* 构造函数
* @param ApiResponseService $responseService
* @param ParkingCameraService $service
*/
public function __construct(
ApiResponseService $responseService,
ParkingCameraService $service
) {
parent::__construct($responseService);
$this->service = $service;
}
public function index(Request $request): JsonResponse
{
try {
$query = ParkingCamera::query();
if ($request->has('floor_id')) {
$floor_id = $request->input('floor_id');
if ($floor_id) {
$query->where('floor_id', $floor_id);
}
}
if ($request->has('number')) {
$number = $request->input('number');
if ($number) {
$query->where('number', 'like', "%{$number}%");
}
}
if ($request->has('parking_space_number')) {
$parking_space_number = $request->input('parking_space_number');
if ($parking_space_number) {
$cameraIds = ParkingSpaceCamera::getCameraIds(
$parking_space_number
);
if ($cameraIds) {
$query->whereIn('id', $cameraIds);
} else {
$query->where('id', 0);
}
}
}
if ($request->has('camera_ip')) {
$camera_ip = $request->input('camera_ip');
if ($camera_ip) {
$query->where('camera_ip', $camera_ip);
}
}
if ($request->has('status')) {
$status = $request->input('status');
if (is_numeric($status)) {
$query->where('status', $status);
}
}
// 分页
$page = $request->input('page', 1);
$perPage = $request->input('per_page', 10);
$columns = [
'id',
'floor_id',
'number',
'camera_ip',
'is_control_lights',
'type',
'status',
'updated_at'
];
$total = $query->count();
$items = $query->latest()->forPage($page, $perPage)->select(
$columns
)->get()->each(function ($item) {
$item = $this->service->optionItems($item);
unset($item['floor_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()
);
}
}
public function search(): JsonResponse
{
try {
$data = [
'floor_list' => $this->getFloorList(),
'status_list' => get_select_data(
ParkingCameraService::getStatus(),
true
)
];
return $this->responseService->success($data);
} catch (Exception $e) {
$m_prefix = __('exception.exception_handler.resource');
return $this->responseService->systemError(
$m_prefix . ':' . $e->getMessage()
);
}
}
/**
* @return array
*/
private function getFloorList(): array
{
$floor_column = [
'id as floor_id',
'name as floor_name'
];
return AdminFloorService::getSelectList(
1,
$floor_column
);
}
public function create(): JsonResponse
{
try {
$data = $this->getViewData();
return $this->responseService->success($data);
} catch (Exception $e) {
$m_prefix = __('exception.exception_handler.resource');
return $this->responseService->systemError(
$m_prefix . ':' . $e->getMessage()
);
}
}
#[ArrayShape(['floor_list' => "array", 'attr_list' => "array"])]
private function getViewData(): array
{
return [
'floor_list' => $this->getFloorList(),
'attr_list' => ParkingSpaceAttributes::getData()
];
}
/**
* @param Request $request
* @return JsonResponse
* @throws CustomException
* @throws ValidationException
*/
public function store(Request $request): JsonResponse
{
try {
$this->saveValidator($request->all());
$this->service->createModel($request->all());
return $this->responseService->success(
null,
__('admin.save_succeeded')
);
} catch (ValidationException|CustomException $e) {
throw $e;
} catch (Exception $e) {
return $this->responseService->systemError(
__('exception.parking_camera.create_failed') . ':'
. $e->getMessage()
);
}
}
/**
* @param array $data
* @param int $id
* @return void
* @throws ValidationException
*/
protected function saveValidator(array $data, int $id = 0): void
{
$rules = [
'number' => 'required|max:50',
'server_ip' => 'required|max:15',
'camera_ip' => 'required|max:15',
'floor_id' => 'required|numeric',
'parking_space_data' => 'array'
];
$messages = [
'name.required' => __(
'validation.parking_camera.n_empty'
),
'name.max' => __('validation.parking_camera.n_max'),
'server_ip.required' => __(
'validation.parking_camera.s_empty'
),
'server_ip.max' => __('validation.parking_camera.s_max'),
'camera_ip.required' => __(
'validation.parking_camera.c_empty'
),
'camera_ip.max' => __('validation.parking_camera.c_max'),
'floor_id.required' => __('validation.map.f_empty'),
'floor_id.numeric' => __('validation.map.f_number'),
'parking_space_data.array' => __(
'validation.parking_camera.p_array'
)
];
if ($id) {
$this->validateId($id, ParkingCamera::class);
}
$validator = Validator::make($data, $rules, $messages);
if ($validator->fails()) {
throw new ValidationException($validator);
}
}
public function edit(string $id): JsonResponse
{
try {
$this->validateId($id, ParkingCamera::class);
$data = $this->getViewData();
$item = ParkingCamera::query()->findOrFail($id);
$item['parking_space_data'] = ParkingSpaceCamera::getData($id, 1);
$data['item'] = $item;
return $this->responseService->success($data);
} catch (Exception $e) {
return $this->responseService->systemError(
__('exception.get_data_failed') . ':' . $e->getMessage()
);
}
}
/**
* 更新
* @param Request $request
* @param string $id
* @return JsonResponse
* @throws CustomException
* @throws ValidationException
*/
public function update(Request $request, string $id): JsonResponse
{
try {
$this->saveValidator($request->all(), $id);
$this->service->updateModel($request->all(), $id);
return $this->responseService->success(
null,
__('admin.update_succeeded')
);
} catch (ValidationException|CustomException $e) {
throw $e;
} catch (Exception $e) {
return $this->responseService->systemError(
__('exception.parking_camera.update_failed') . ':'
. $e->getMessage()
);
}
}
/**
* @param string $id
* @return JsonResponse
* @throws ValidationException
*/
public function destroy(string $id): JsonResponse
{
try {
$this->validateId($id, ParkingCamera::class);
$this->service->deleteModel($id);
return $this->responseService->success(
null,
__('admin.delete_succeeded')
);
} catch (ValidationException $e) {
throw $e;
} catch (Exception $e) {
return $this->responseService->systemError(
__('exception.parking_camera.destroy_failed') . ':'
. $e->getMessage()
);
}
}
/**
* @return JsonResponse
* @throws InvalidArgumentException
*/
public function rule(): JsonResponse
{
try {
return $this->responseService->success(
$this->methodShow('parkingCamera')
);
} catch (Exception $e) {
return $this->responseService->systemError(
__('exception.get_data_failed') . ':' . $e->getMessage()
);
}
}
/**
* @return BinaryFileResponse
*/
public function export(): BinaryFileResponse
{
return Excel::download(
new ParkingCameraExport(),
__('exports.parking_camera.list') . time() . '.xlsx'
);
}
}