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

641 lines
21 KiB

<?php
namespace App\Http\Controllers\Admin;
use App\Exceptions\CustomException;
use App\Exports\ParkingSpaceExport;
use App\Exports\ParkingSpaceTemplateExport;
use App\Imports\ParkingSpaceImport;
use App\Models\AdminFloor;
use App\Models\AdminFloorRegion;
use App\Models\Parking;
use App\Models\ParkingLicensePlate;
use App\Models\ParkingPatternSpace;
use App\Models\ParkingSpace;
use App\Models\ParkingSpaceAttributes;
use App\Models\ParkingSpaceType;
use App\Services\AdminFloorService;
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\Storage;
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()
);
}
}
/**
* @return JsonResponse
*/
public function create(): JsonResponse
{
try {
$data = [
'parking_list' => Parking::getData(),
'space_attr_list' => ParkingSpaceAttributes::getData()
];
return $this->responseService->success($data);
} catch (Exception $e) {
return $this->responseService->systemError(
__('exception.get_data_failed') . ':' . $e->getMessage()
);
}
}
/**
* @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(
__('admin.operation_failed') . ':'
. $e->getMessage()
);
}
}
/**
* @param array $data
* @param int $id
* @return void
* @throws ValidationException
*/
protected function saveValidator(array $data, int $id = 0): void
{
$rules = [
'parking_id' => 'required',
'floor_id' => 'required',
'region_id' => 'required',
'space_attr_id' => 'required',
'number' => 'required'
];
$messages = [
'parking_id.required' => __validation('parking_space.pi_empty'),
'floor_id.required' => __validation('parking_space.fi_empty'),
'region_id.required' => __validation('parking_space.ri_empty'),
'space_attr_id.required' => __validation('parking_space.a_empty'),
'number.required' => __validation('parking_space.n_empty')
];
if ($id) {
$this->validateId($id, ParkingSpace::class);
}
$validator = Validator::make($data, $rules, $messages);
if ($validator->fails()) {
throw new ValidationException($validator);
}
}
/**
* @param string $id
* @return JsonResponse
*/
public function edit(string $id): JsonResponse
{
try {
$this->validateId($id, ParkingSpace::class);
$item = ParkingSpace::query()->find($id, [
'id',
'floor_id',
'number',
'region_id',
'space_attr_id'
]);
$item['parking_id'] = AdminFloor::query()->where(
'id',
$item['floor_id']
)->value('building_floor');
$data = [
'parking_list' => Parking::getData(),
'floor_list' => AdminFloorService::getSelectList(
$item['parking_id'],
[
'id as floor_id',
'name as floor_name'
]
),
'floor_region_list' => AdminFloorRegion::getFloorRegion(
$item['floor_id']
),
'space_attr_list' => ParkingSpaceAttributes::getData(),
'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 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 $e) {
throw $e;
} catch (Exception $e) {
return $this->responseService->systemError(
__('admin.operation_failed') . ':'
. $e->getMessage()
);
}
}
/**
* @param string $id
* @return JsonResponse
* @throws ValidationException
*/
public function destroy(string $id): JsonResponse
{
try {
$this->validateId($id, ParkingSpace::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(
__('admin.operation_failed') . ':'
. $e->getMessage()
);
}
}
/**
* @param Request $request
* @return JsonResponse
* @throws CustomException
* @throws ValidationException
*/
public function batchDelete(Request $request): JsonResponse
{
try {
$data = $request->all();
$rules = [
'ids' => 'required|array',
];
$messages = [
'ids.required' => __validation(
'parking_repair_list.ids_empty'
),
'ids.array' => __validation(
'parking_repair_list.ids_array'
),
];
$validator = Validator::make($data, $rules, $messages);
if ($validator->fails()) {
throw new ValidationException($validator);
}
$ids = $data['ids'];
$this->service->batchDeleteModel($ids);
return $this->responseService->success(
null,
__('admin.delete_succeeded')
);
} catch (ValidationException|CustomException $e) {
throw $e;
} catch (Exception $e) {
return $this->responseService->systemError(
__('admin.operation_failed') . ':'
. $e->getMessage()
);
}
}
/**
* @return BinaryFileResponse
*/
public function importTemplate(): BinaryFileResponse
{
return Excel::download(
new ParkingSpaceTemplateExport(),
set_space_underline(__('exports.parking_space.list_export'))
. get_datetime('date_')
. '.xlsx'
);
}
/**
* 创建导入
* @param Request $request
* @return JsonResponse
* @throws ValidationException
*/
public function import(Request $request): JsonResponse
{
try {
// 1. 验证上传的文件
$data = $request->all();
$validator = Validator::make($data, [
'file' => 'required|mimes:xlsx,xls,csv,txt|max:2048'
], [
'file.required' => __('validation.admin_list_vip.file_empty'),
'file.mimes' => __('validation.admin_list_vip.file_mimes'),
'file.max' => __('validation.admin_list_vip.file_max')
]);
if ($validator->fails()) {
throw new ValidationException($validator);
}
// 2. 获取上传的文件
$file = $request->file('file');
// 3. 正确的做法:先存储文件,再获取绝对路径进行导入
$path = $file->store('imports');
// 4. 执行导入(使用存储后的绝对路径)
// storage_path('app') 获取 storage/app 的绝对路径
Excel::import(
new ParkingSpaceImport($this->adminUserId),
storage_path('app/' . $path)
);
// 5. (可选)导入完成后删除临时文件
Storage::delete($path);
return $this->responseService->success(
__('controller.import.success')
);
} catch (ValidationException $e) {
throw $e;
} catch (Exception $e) {
return $this->responseService->systemError(
__('exception.admin_vip_list.import_failed') . ':'
. $e->getMessage()
);
}
}
}