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

400 lines
14 KiB

<?php
namespace App\Http\Controllers\Admin;
use App\Exceptions\CustomException;
use App\Models\AdminFloor;
use App\Models\Parking;
use App\Models\ParkingPattern;
use App\Models\ParkingPatternSpace;
use App\Models\ParkingSpace;
use App\Models\ParkingSpaceAttributes;
use App\Models\ParkingSpaceType;
use App\Services\ApiResponseService;
use App\Services\ParkingPatternSpaceService;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
class ParkingPatternSpaceController extends BaseController
{
protected string $menuUri = 'patternSpace';
protected ParkingPatternSpaceService $service;
/**
* 构造函数
* @param ApiResponseService $responseService
* @param ParkingPatternSpaceService $service
*/
public function __construct(
ApiResponseService $responseService,
ParkingPatternSpaceService $service
) {
parent::__construct($responseService);
$this->service = $service;
}
public function index(Request $request): JsonResponse
{
try {
$query = ParkingPatternSpace::query();
$pattern_id = $request->input('pattern_id', '');
if ($pattern_id) {
$query->where('pattern_id', $pattern_id);
} else {
throw new Exception('error');
}
if ($request->has('parking_space_number')) {
$parking_space_number = $request->input('parking_space_number');
if ($parking_space_number) {
$space_id = ParkingSpace::getValueId($parking_space_number);
if ($space_id) {
$query->where('space_id', $space_id);
} else {
$query->where('id', 0);
}
}
}
if ($request->has('parking_space_type')) {
$parking_space_type = $request->input('parking_space_type');
if ($parking_space_type) {
$query->where('space_type_id', $parking_space_type);
}
}
if ($request->has('parking_space_attr')) {
$parking_space_attr = $request->input('parking_space_attr');
if ($parking_space_attr) {
$space_ids = ParkingPatternSpace::query()->where(
'pattern_id',
$pattern_id
)->pluck('space_id');
$ParkingSpaceModel = ParkingSpace::query()->where(
'space_type_id',
$parking_space_attr
);
if ($space_ids) {
$ParkingSpaceModel->whereIn('id', $space_ids);
}
$new_space_ids = $ParkingSpaceModel->pluck('id');
if ($new_space_ids) {
$query->whereIn('space_id', $new_space_ids);
} else {
$query->where('id', 0);
}
}
}
if ($request->has('parking_id')) {
$parking_id = $request->input('parking_id');
if ($parking_id) {
$floor_ids = AdminFloor::query()->where(
'building_floor',
$parking_id
)->pluck('id');
if ($floor_ids) {
$space_ids = ParkingPatternSpace::query()->where(
'pattern_id',
$pattern_id
)->pluck('space_id');
$ParkingSpaceModel = ParkingSpace::query()->whereIn(
'floor_id',
$floor_ids
);
if ($space_ids) {
$ParkingSpaceModel->whereIn('id', $space_ids);
}
$new_space_ids = $ParkingSpaceModel->pluck('id');
if ($new_space_ids) {
$query->whereIn('space_id', $new_space_ids);
} else {
$query->where('id', 0);
}
} else {
$query->where('id', 0);
}
}
}
// 分页
$page = $request->input('page', 1);
$perPage = $request->input('per_page', 10);
$columns = [
'id',
'space_id',
'space_type_id'
];
$total = $query->count();
$items = $query->latest()->forPage($page, $perPage)->select(
$columns
)->get()->each(function ($item) {
$ParkingSpace = ParkingSpace::query()->find($item['space_id']);
$item['parking_space_number'] = '';
$item['parking_space_attr'] = '';
$item['parking'] = '';
if ($ParkingSpace) {
$item['parking_space_number'] = $ParkingSpace['number'];
$item['parking_space_attr'] = ParkingSpaceAttributes::getAttr($ParkingSpace['space_attr_id']);
$parking_id = AdminFloor::query()->where('id', $ParkingSpace['floor_id'])->value('building_floor');
if ($parking_id) {
$item['parking'] = Parking::getName($parking_id);
}
}
$item['parking_space_type'] = ParkingSpaceType::getName(
$item['space_type_id']
);
$item['auxiliary_space_type'] = '';
unset($item['space_id'], $item['space_type_id']);
return $item;
});
$model_name = ParkingPattern::getName($pattern_id);
$parking_space_count = ParkingSpace::getCount();
$model_parking_space_count = ParkingPatternSpace::getCount(
$pattern_id
);
return $this->responseService->success([
'items' => $items,
'total' => $total,
'page' => $page,
'per_page' => $perPage,
'last_page' => ceil($total / $perPage),
'model_name' => $model_name,
'sum_count' => $parking_space_count,
'use_count' => $model_parking_space_count,
'remaining_count' => $parking_space_count
- $model_parking_space_count,
]);
} catch (Exception $e) {
$m_prefix = __('exception.exception_handler.resource');
return $this->responseService->systemError(
$m_prefix . ':' . $e->getMessage()
);
}
}
public function search(): JsonResponse
{
try {
$data = [
'parking_space_type_list' => ParkingSpaceType::getData(),
'parking_space_attr_list' => ParkingSpaceAttributes::getData(),
'parking_list' => Parking::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 create(): JsonResponse
{
try {
$data = [
'parking_space_list' => ParkingSpace::getAll(),
'parking_space_type_list' => ParkingSpaceType::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(
__('exception.parking_pattern.create_failed') . ':'
. $e->getMessage()
);
}
}
/**
* @param array $data
* @param int $id
* @return void
* @throws ValidationException
*/
protected function saveValidator(array $data, int $id = 0): void
{
$rules = [
'parking_space_type' => 'required'
];
$messages = [
'parking_space_type.required' => __(
'validation.parking_pattern.type_empty'
),
];
if ($id) {
$this->validateId($id, ParkingPatternSpace::class);
} else {
$rules['pattern_id'] = 'required';
$rules['parking_space_id'] = 'required';
$messages['pattern_id.required'] = __(
'validation.parking_pattern.space_id_empty'
);
$messages['parking_space_id.required'] = __(
'validation.parking_pattern.id_empty'
);
}
$validator = Validator::make($data, $rules, $messages);
if ($validator->fails()) {
throw new ValidationException($validator);
}
}
public function edit($id): JsonResponse
{
try {
$this->validateId($id, ParkingPatternSpace::class);
$columns = [
'id',
'pattern_id',
'space_id as parking_space_id',
'space_type_id as parking_space_type'
];
$item = ParkingPatternSpace::query()->findOrFail($id, $columns);
$data = [
'model_name' => ParkingPattern::getName($item['pattern_id']),
'parking_space_list' => ParkingSpace::getAll(),
'parking_space_type_list' => ParkingSpaceType::getData()
];
$item['parking_space_number'] = ParkingSpace::getNumber(
$item['parking_space_id']
);
$item['parking_space_type_name'] = ParkingSpaceType::getName(
$item['parking_space_type']
);
$data['item'] = $item;
unset($item['pattern_id']);
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 {
$data = $request->all();
$this->saveValidator($data, $id);
$this->service->updateModel($data, $id);
return $this->responseService->success(
null,
__('admin.update_succeeded')
);
} catch (ValidationException|CustomException $e) {
throw $e;
} catch (Exception $e) {
return $this->responseService->systemError(
__('exception.parking_pattern.update_failed') . ':'
. $e->getMessage()
);
}
}
/**
* @param string $id
* @return JsonResponse
* @throws ValidationException
*/
public function destroy(string $id): JsonResponse
{
try {
$this->validateId($id, ParkingPatternSpace::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_pattern.destroy_failed') . ':'
. $e->getMessage()
);
}
}
// 批量更新车位类型
public function updateType(Request $request): JsonResponse
{
try {
$rules = [
'space_type_id' => 'required|numeric',
'ids' => 'required|array'
];
$messages = [
'space_type_id.required' => __(
'validation.parking_space.type_id_empty'
),
'space_type_id.numeric' => __(
'validation.parking_space.type_id_numeric'
),
'ids.required' => __('validation.id_empty'),
'ids.array' => __('validation.id_empty')
];
$validator = Validator::make($request->all(), $rules, $messages);
if ($validator->fails()) {
throw new ValidationException($validator);
}
$data = $request->all();
$ids = $data['ids'] ?? [];
$space_type_id = $data['space_type_id'];
foreach ($ids as $id) {
$this->service->updateData($id, $space_type_id);
}
return $this->responseService->success(
null,
__('admin.update_succeeded')
);
} catch (Exception $e) {
return $this->responseService->systemError(
__('admin.operation_failed') . ':'
. $e->getMessage()
);
}
}
}