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

240 lines
7.5 KiB

<?php
namespace App\Http\Controllers\Admin;
use App\Exceptions\CustomException;
use App\Models\Parking;
use App\Services\AdminTranslationService;
use App\Services\ApiResponseService;
use App\Services\ParkingManagementService;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
class ParkingManagementController extends BaseController
{
protected string $menuUri = 'parkingManagement';
protected ParkingManagementService $service;
/**
* 构造函数
* @param ApiResponseService $responseService
* @param ParkingManagementService $service
*/
public function __construct(
ApiResponseService $responseService,
ParkingManagementService $service
) {
parent::__construct($responseService);
$this->service = $service;
}
public function index(Request $request): JsonResponse
{
try {
$query = Parking::query();
// 分页
$page = $request->input('page', 1);
$perPage = $request->input('per_page', 10);
$statusArr = ParkingManagementService::getStatus();
$total = $query->count();
$items = $query->latest()->forPage($page, $perPage)->select()
->get()->each(function ($item) use ($statusArr) {
$tr_name = AdminTranslationService::getTranslationName(
$item['id'],
1
);
$item['name'] = $tr_name ?: $item['name'];
$open_time_res = $this->service->optionTime(
$item['open_time']
);
$item['open_time'] = $open_time_res['time'];
$item['open_time_str'] = $open_time_res['str'];
$close_time_res = $this->service->optionTime(
$item['close_time']
);
$item['close_time'] = $close_time_res['time'];
$item['close_time_str'] = $close_time_res['str'];
$item['parking_space_count']
= $this->service->getParkingCount($item['id']);
$item['status_str'] = $statusArr[$item['status']];
return $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()
);
}
}
/**
* @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 = [
'name' => 'required',
'open_time' => 'required|size:5',
'close_time' => 'required|size:5',
'address' => 'required',
];
$messages = [
'name.required' => __(
'validation.parking_management.n_empty'
),
'open_time.required' => __(
'validation.parking_management.o_empty'
),
'open_time.size' => __(
'validation.parking_management.o_length'
),
'close_time.required' => __(
'validation.parking_management.c_empty'
),
'close_time.size' => __(
'validation.parking_management.c_length'
),
'address.length' => __(
'validation.parking_management.a_empty'
),
];
$model = Parking::query();
if ($id) {
$this->validateId($id, Parking::class);
$model->where([
['name', '=', $data['name']],
['id', '<>', $id]
]);
} else {
$model->where('name', $data['name']);
}
if ($model->exists()) {
throw new CustomException(
__('validation.parking_management.name_exists')
);
}
$validator = Validator::make($data, $rules, $messages);
if ($validator->fails()) {
throw new ValidationException($validator);
}
$open_times = strtotime($data['open_time']);
$close_time = strtotime($data['close_time']);
if ($open_times >= $close_time) {
throw new CustomException(
__('validation.parking_management.error_time')
);
}
}
public function edit($id): JsonResponse
{
try {
$this->validateId($id, Parking::class);
$item = Parking::query()->findOrFail($id);
$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 {
$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(
__('admin.operation_failed') . ':'
. $e->getMessage()
);
}
}
/**
* @param string $id
* @return JsonResponse
* @throws ValidationException
*/
public function destroy(string $id): JsonResponse
{
try {
$this->validateId($id, Parking::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()
);
}
}
}