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.
304 lines
9.8 KiB
304 lines
9.8 KiB
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Exceptions\CustomException;
|
|
use App\Models\ParkingPattern;
|
|
use App\Models\ParkingPatternSpace;
|
|
use App\Models\ParkingSpace;
|
|
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;
|
|
use Psr\SimpleCache\InvalidArgumentException;
|
|
|
|
class ParkingPatternSpaceController extends BaseController
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
|
|
// 分页
|
|
$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) {
|
|
$item['parking_space_number'] = ParkingSpace::getNumber(
|
|
$item['space_id']
|
|
);
|
|
$item['parking_space_type'] = ParkingSpaceType::getName(
|
|
$item['space_type_id']
|
|
);
|
|
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()
|
|
];
|
|
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()
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return JsonResponse
|
|
* @throws InvalidArgumentException
|
|
*/
|
|
public function rule(): JsonResponse
|
|
{
|
|
try {
|
|
return $this->responseService->success(
|
|
$this->methodShow('patternSpace')
|
|
);
|
|
} catch (Exception $e) {
|
|
return $this->responseService->systemError(
|
|
__('exception.get_data_failed') . ':' . $e->getMessage()
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|