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

410 lines
14 KiB

<?php
namespace App\Http\Controllers\Admin;
use App\Exports\ParkingSpaceExport;
use App\Http\Controllers\Controller;
use App\Models\AdminFloor;
use App\Models\ParkingLicensePlate;
use App\Models\ParkingPatternSpace;
use App\Models\ParkingSpace;
use App\Models\ParkingSpaceAttributes;
use App\Models\ParkingSpaceType;
use App\Services\ApiResponseService;
use App\Services\EventCalendarService;
use App\Services\ParkingSpaceService;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
use Maatwebsite\Excel\Facades\Excel;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
class ParkingSpaceController extends BaseController
{
protected string $menuUri = 'parkingSpace';
/**
* @var ParkingSpaceService
*/
protected ParkingSpaceService $service;
/**
* 构造函数
* @param ApiResponseService $responseService
* @param ParkingSpaceService $service
*/
public function __construct(
ApiResponseService $responseService,
ParkingSpaceService $service
) {
parent::__construct($responseService);
$this->service = $service;
}
/**
* Display a listing of the resource.
*/
public function index(Request $request): JsonResponse
{
try {
$query = ParkingSpace::query();
$pattern_id = EventCalendarService::getTargetModeId();
$spaceIds = ParkingPatternSpace::getParkingSpaceIds($pattern_id);
$query->whereIn('id', $spaceIds);
if ($request->has('license_plate')) {
$license_plate = $request->input('license_plate');
if (!empty($license_plate)) {
$license_plate_id_arr = ParkingLicensePlate::query()->where(
'number',
'like',
"%{$license_plate}%"
)->pluck('id');
$license_plate_id_arr ? $query->whereIn(
'license_plate_id',
$license_plate_id_arr
) : $query->where('id', 0);
}
}
if ($request->has('floor')) {
$floor = $request->input('floor');
if (!empty($floor)) {
$query->where('floor_id', $floor);
}
}
if ($request->has('number')) {
$number = $request->input('number');
if (!empty($number)) {
$query->where('number', 'like', "%{$number}%");
}
}
if ($request->has('space_type')) {
$space_type = $request->input('space_type');
if (!empty($space_type)) {
$query->where('space_type_id', $space_type);
}
}
if ($request->has('space_attr')) {
$space_attr = $request->input('space_attr');
if (!empty($space_attr)) {
$query->where('space_attr_id', $space_attr);
}
}
if ($request->has('status')) {
$status = $request->input('status');
if (!empty($status)) {
$query->where('status', $status);
}
}
// 分页
$page = $request->input('page', 1);
$perPage = $request->input('per_page', 10);
$query->orderBy('id');
$total = $query->count();
$_this = $this;
$items = $query->latest()->forPage($page, $perPage)->get()->each(
function ($item) use ($_this) {
return $_this->service->optionItems($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 = [
'floor_data' => AdminFloor::getData(),
'space_type_data' => ParkingSpaceType::query()->select(
['id', 'name']
)->get()->toArray(),
'space_attr_data' => ParkingSpaceAttributes::query()->select(
['id', 'attributes']
)->get()->toArray(),
'status_data' => get_select_data(
$this->service->getStatusArr()
)
];
return $this->responseService->success($data);
} catch (Exception $e) {
$m_prefix = __('exception.exception_handler.resource');
return $this->responseService->systemError(
$m_prefix . ':' . $e->getMessage()
);
}
}
public function updateType(Request $request): JsonResponse
{
try {
$rules = [
'space_type_id' => 'required|numeric',
'space_ids' => 'required|array'
];
$messages = [
'space_type_id.required' => __(
'validation.parking_space.type_id_empty'
),
'space_type_id.numeric' => __(
'validation.parking_space.type_id_numeric'
),
'space_ids.required' => __(
'validation.parking_space.ids_empty'
),
'space_ids.array' => __(
'validation.parking_space.ids_array'
)
];
$validator = Validator::make($request->all(), $rules, $messages);
if ($validator->fails()) {
throw new ValidationException($validator);
}
$this->service->updateData($request->all());
return $this->responseService->success(
null,
__('admin.update_succeeded')
);
} catch (Exception $e) {
return $this->responseService->systemError(
__('exception.parking_space.update_type_failed') . ':'
. $e->getMessage()
);
}
}
public function updateAttr(Request $request): JsonResponse
{
try {
$rules = [
'space_attr_id' => 'required|numeric',
'space_ids' => 'required|array'
];
$messages = [
'space_attr_id.required' => __(
'validation.parking_space.attr_id_empty'
),
'space_attr_id.numeric' => __(
'validation.parking_space.attr_id_numeric'
),
'space_ids.required' => __(
'validation.parking_space.ids_empty'
),
'space_ids.array' => __(
'validation.parking_space.ids_array'
)
];
$validator = Validator::make($request->all(), $rules, $messages);
if ($validator->fails()) {
throw new ValidationException($validator);
}
$this->service->updateData($request->all(), 'space_attr_id');
return $this->responseService->success(
null,
__('admin.update_succeeded')
);
} catch (Exception $e) {
return $this->responseService->systemError(
__('exception.parking_space.update_attr_failed') . ':'
. $e->getMessage()
);
}
}
public function updateAuxiliaryType(Request $request): JsonResponse
{
try {
$rules = [
'auxiliary_space_type' => 'required|numeric',
'space_ids' => 'required|array'
];
$messages = [
'auxiliary_space_type.required' => __(
'validation.parking_space.attr_id_empty'
),
'auxiliary_space_type.numeric' => __(
'validation.parking_space.attr_id_numeric'
),
'space_ids.required' => __(
'validation.parking_space.ids_empty'
),
'space_ids.array' => __(
'validation.parking_space.ids_array'
)
];
$validator = Validator::make($request->all(), $rules, $messages);
if ($validator->fails()) {
throw new ValidationException($validator);
}
$this->service->updateData($request->all(), 'auxiliary_space_type');
return $this->responseService->success(
null,
__('admin.update_succeeded')
);
} catch (Exception $e) {
return $this->responseService->systemError(
__('exception.parking_space.update_attr_failed') . ':'
. $e->getMessage()
);
}
}
/**
* @return BinaryFileResponse
*/
public function export(): BinaryFileResponse
{
return Excel::download(
new ParkingSpaceExport(),
set_space_underline(__('exports.parking_space.list'))
. get_datetime('date_')
. '.xlsx'
);
}
public function information(Request $request): JsonResponse
{
try {
$data = [];
$parking_space_id = $request->post('parking_space_id', '');
$item = ParkingSpace::query()->where('id', $parking_space_id)
->first();
if (empty($item)) {
throw new Exception(__('controller.parking_space.not_space'));
}
$statusArr = $this->service->getStatusArr();
$data['parking_space_number'] = $item['number'];
$data['license_plate'] = ParkingLicensePlate::getNumber($item['license_plate_id']);
$data['status'] = $statusArr[$item['status']];
$data['parking_space_type'] = ParkingSpaceType::getName($item['space_type_id']);
$data['berthing_time_str'] = '';
if ($item['berthing_time']) {
$data['berthing_time_str'] = get_time_difference_str($item['berthing_time']);
}
$data['recognition_rate'] = $item['recognition'];
$data['recognition'] = $this->service->getRecognition(
$item['recognition']
);
$data['berthing_time'] = $item['berthing_time'];
$data['space_url'] = get_image_url($item['pic_url']);
return $this->responseService->success($data);
} catch (Exception $e) {
$m_prefix = __('exception.exception_handler.resource');
return $this->responseService->systemError(
$m_prefix . ':' . $e->getMessage()
);
}
}
public function updateStatus(Request $request): JsonResponse
{
try {
$data = $request->all();
$statusArr = $this->service->getStatusArrKey();
$statusStr = implode(',', $statusArr);
$rules = [
'status' => 'required|numeric|in:'.$statusStr
];
$messages = [
'status.required' => __(
'validation.parking_space.status_empty'
),
'status.numeric' => __(
'validation.parking_space.status_numeric'
),
'status.in' => __('validation.parking_space.status_in')
];
$validator = Validator::make($data, $rules, $messages);
if ($validator->fails()) {
throw new ValidationException($validator);
}
$parking_space_id = $data['parking_space_id'];
$status = $data['status'] == 1 ? 0 : 1;
$number = $data['license_plate'];
$this->validateId($parking_space_id, ParkingSpace::class);
$this->service->updateStatus($parking_space_id, $status, $number);
return $this->responseService->success('', __('admin.save_succeeded'));
} catch (Exception $e) {
$m_prefix = __('exception.exception_handler.resource');
return $this->responseService->systemError(
$m_prefix . ':' . $e->getMessage()
);
}
}
public function store(Request $request): JsonResponse
{
$data = $request->all();
if (ParkingSpace::query()->where('number', $data['number'])
->exists()
) {
return $this->responseService->systemError('车位号码已存在');
}
$floor_id = AdminFloor::query()->where('name', $data['floor_name'])
->value('id');
if (!$floor_id) {
return $this->responseService->systemError('楼层不存在');
}
$space_attr_id = ParkingSpaceAttributes::query()->where(
'attributes',
$data['attr_name']
)->value('id');
if (!$space_attr_id) {
return $this->responseService->systemError('车位属性不存在');
}
$space_type_id = ParkingSpaceType::getValueId($data['type_name']);
if (!$space_type_id) {
return $this->responseService->systemError('车位类型不存在');
}
$license_plate_id = 0;
$status = 0;
if (isset($data['license_plate'])) {
$license_plate_id = ParkingLicensePlate::getValueId(
$data['license_plate']
);
if ($license_plate_id) {
$status = 1;
} else {
return $this->responseService->systemError('车牌号码不存在');
}
}
$this->service->createData([
'floor_id' => $floor_id,
'number' => $data['number'],
'space_attr_id' => $space_attr_id,
'space_type_id' => $space_type_id,
'license_plate_id' => $license_plate_id,
'status' => $status
]);
return $this->responseService->success('', __('admin.operation_successful'));
}
}