diff --git a/app/Http/Controllers/Admin/GuardBoothManagementController.php b/app/Http/Controllers/Admin/GuardBoothManagementController.php new file mode 100644 index 0000000..5639e8c --- /dev/null +++ b/app/Http/Controllers/Admin/GuardBoothManagementController.php @@ -0,0 +1,227 @@ +service = $service; + } + + /** + * @param Request $request + * @return JsonResponse + */ + public function index(Request $request): JsonResponse + { + try { + $query = ParkingGuardBooth::query(); + + if ($request->has('name')) { + $name = $request->input('name'); + if ($name) { + $query->where('name', 'like', "%{$name}%"); + } + } + + if ($request->has('create_start_time') + && $request->has( + 'create_end_time' + ) + ) { + $create_start_time = $request->input('create_start_time'); + $create_end_time = $request->input('create_end_time'); + if ($create_start_time && $create_end_time) { + $query->whereBetween('created_at', [ + $create_start_time . ' 00:00:00', + $create_end_time . ' 23:59:59' + ]); + } + } + + // 分页 + $page = $request->input('page', 1); + $perPage = $request->input('per_page', 10); + $total = $query->count(); + $items = $query->latest()->forPage($page, $perPage)->get()->each( + function ($item) { + $item['name'] + = AdminTranslationService::getTranslationTypeName( + $item['id'], + 7, + $item['name'] + ); + 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' + ]; + $messages = [ + 'name.required' => __( + 'validation.guard_booth_management.n_empty' + ) + ]; + if ($id) { + $this->validateId($id, ParkingGuardBooth::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, ParkingGuardBooth::class); + $data = [ + 'item' => ParkingGuardBooth::query()->find($id) + ]; + $Translation = AdminTranslationService::getTranslation( + $data['item']['id'], + 7 + ); + $data['item']['en_name'] = $Translation['en'] ?? ''; + $data['item']['tw_name'] = $Translation['zh_tw'] ?? ''; + 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 { + $this->saveValidator($request->all(), $id); + $this->service->updateModel($request->all(), $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 CustomException + * @throws ValidationException + */ + public function destroy(string $id): JsonResponse + { + try { + $this->validateId($id, ParkingGuardBooth::class); + $this->service->deleteModel($id); + return $this->responseService->success( + null, + __('admin.delete_succeeded') + ); + } catch (ValidationException|CustomException $e) { + throw $e; + } catch (Exception $e) { + return $this->responseService->systemError( + __('exception.admin_floor.destroy_failed') . ':' + . $e->getMessage() + ); + } + } + +} diff --git a/app/Http/Controllers/Admin/ParkingChannelController.php b/app/Http/Controllers/Admin/ParkingChannelController.php index a8c312e..20c1524 100644 --- a/app/Http/Controllers/Admin/ParkingChannelController.php +++ b/app/Http/Controllers/Admin/ParkingChannelController.php @@ -174,7 +174,7 @@ class ParkingChannelController extends BaseController throw $e; } catch (Exception $e) { return $this->responseService->systemError( - __('exception.admin_floor.create_failed') . ':' + __('admin.operation_failed') . ':' . $e->getMessage() ); } @@ -275,7 +275,7 @@ class ParkingChannelController extends BaseController throw $e; } catch (Exception $e) { return $this->responseService->systemError( - __('exception.admin_floor.update_failed') . ':' + __('admin.operation_failed') . ':' . $e->getMessage() ); } diff --git a/app/Http/Controllers/Admin/RegionalManagementController.php b/app/Http/Controllers/Admin/RegionalManagementController.php index f1c1c8c..8309dd9 100644 --- a/app/Http/Controllers/Admin/RegionalManagementController.php +++ b/app/Http/Controllers/Admin/RegionalManagementController.php @@ -179,7 +179,7 @@ class RegionalManagementController extends BaseController throw $e; } catch (Exception $e) { return $this->responseService->systemError( - __('exception.admin_floor.create_failed') . ':' + __('admin.operation_failed') . ':' . $e->getMessage() ); } @@ -275,7 +275,7 @@ class RegionalManagementController extends BaseController throw $e; } catch (Exception $e) { return $this->responseService->systemError( - __('exception.admin_floor.update_failed') . ':' + __('admin.operation_failed') . ':' . $e->getMessage() ); } diff --git a/app/Services/ParkingGuardBoothService.php b/app/Services/ParkingGuardBoothService.php new file mode 100644 index 0000000..9111f49 --- /dev/null +++ b/app/Services/ParkingGuardBoothService.php @@ -0,0 +1,136 @@ +where($existsWhere)->exists()) { + throw new Exception( + __('service.guard_booth_management.name_exists') + ); + } + + $model = ParkingGuardBooth::query()->create([ + 'name' => $data['name'], + 'remake' => $data['remake'] ?? '', + 'created_at' => get_datetime() + ]); + + $this->logService->logCreated( + $model, + 'guard_booth_management.create' + ); + + AdminTranslationService::saveTranslation( + $data['name'], + $data['en_name'] ?? '', + $data['tw_name'] ?? '', + $model->id, + 7 + ); + + DB::commit(); + return $model; + } catch (Exception $e) { + DB::rollBack(); + throw $e; + } + } + + /** + * @param array $data + * @param int $id + * @throws Exception + */ + public function updateModel(array $data, int $id) + { + try { + DB::beginTransaction(); + + // 验证 + $existsWhere = [ + ['name', '=', $data['name']], + ['id', '<>', $id] + ]; + if (ParkingGuardBooth::query()->where($existsWhere)->exists()) { + throw new Exception( + __('service.guard_booth_management.name_exists') + ); + } + + // 更新 + $model = ParkingGuardBooth::query()->findOrFail($id); + $oldValues = $model->toArray(); + + $model->update([ + 'name' => $data['name'], + 'remake' => $data['remake'] ?? '', + 'updated_at' => get_datetime() + ]); + + $this->logService->logUpdated( + $model, + $oldValues, + 'guard_booth_management.update' + ); + + AdminTranslationService::saveTranslation( + $data['name'], + $data['en_name'] ?? '', + $data['tw_name'] ?? '', + $id, + 7 + ); + + DB::commit(); + return $model; + } catch (Exception $e) { + DB::rollBack(); + throw $e; + } + } + + /** + * @param $id + * @return bool + * @throws Exception + */ + public function deleteModel($id): bool + { + try { + DB::beginTransaction(); + + $model = ParkingGuardBooth::query()->findOrFail($id); + + $this->logService->logDeleted($model, 'guard_booth_management.delete'); + + $model->delete(); + + AdminTranslationService::syncDelete($id, 7); + + DB::commit(); + return true; + } catch (Exception $e) { + DB::rollBack(); + throw $e; + } + } +} diff --git a/resources/lang/en/log.php b/resources/lang/en/log.php index e1bd644..c83d15f 100644 --- a/resources/lang/en/log.php +++ b/resources/lang/en/log.php @@ -106,5 +106,10 @@ return [ 'create' => 'Create channel', 'update' => 'Update channel', 'delete' => 'Delete channel' + ], + 'guard_booth_management' => [ + 'create' => 'Create a booth', + 'update' => 'Update the booth', + 'delete' => 'Delete the booth' ] ]; diff --git a/resources/lang/en/service.php b/resources/lang/en/service.php index 0c943f7..7d65c61 100644 --- a/resources/lang/en/service.php +++ b/resources/lang/en/service.php @@ -104,5 +104,8 @@ return [ 'on-site' => 'on-site', 'off-site' => 'off-site', 'name_exists' => 'The channel name already exists' + ], + 'guard_booth_management' => [ + 'name_exists' => 'The booth name already exists' ] ]; diff --git a/resources/lang/en/validation.php b/resources/lang/en/validation.php index c11404b..b9b9cd5 100644 --- a/resources/lang/en/validation.php +++ b/resources/lang/en/validation.php @@ -143,5 +143,8 @@ return [ 'po_empty' => 'The channel position cannot be empty', 'pa_empty' => 'The affiliated parking lot cannot be empty', 'g_empty' => 'The affiliated booth cannot be empty' + ], + 'guard_booth_management' => [ + 'n_empty' => 'The booth name cannot be empty', ] ]; diff --git a/resources/lang/zh-CN/log.php b/resources/lang/zh-CN/log.php index a71cf69..a912558 100644 --- a/resources/lang/zh-CN/log.php +++ b/resources/lang/zh-CN/log.php @@ -106,5 +106,10 @@ return [ 'create' => '创建通道', 'update' => '更新通道', 'delete' => '删除通道' + ], + 'guard_booth_management' => [ + 'create' => '创建岗亭', + 'update' => '更新岗亭', + 'delete' => '删除岗亭' ] ]; diff --git a/resources/lang/zh-CN/service.php b/resources/lang/zh-CN/service.php index 6b8fbe6..d88c82f 100644 --- a/resources/lang/zh-CN/service.php +++ b/resources/lang/zh-CN/service.php @@ -104,5 +104,8 @@ return [ 'on-site' => '场内', 'off-site' => '场外', 'name_exists' => '通道名称已存在' + ], + 'guard_booth_management' => [ + 'name_exists' => '岗亭名称已存在' ] ]; diff --git a/resources/lang/zh-CN/validation.php b/resources/lang/zh-CN/validation.php index d585014..63d9730 100644 --- a/resources/lang/zh-CN/validation.php +++ b/resources/lang/zh-CN/validation.php @@ -143,5 +143,8 @@ return [ 'po_empty' => '通道位置不能为空', 'pa_empty' => '所属车场不能为空', 'g_empty' => '所属岗亭不能为空' + ], + 'guard_booth_management' => [ + 'n_empty' => '岗亭名称不能为空', ] ]; diff --git a/resources/lang/zh-TW/log.php b/resources/lang/zh-TW/log.php index 8c8b8d3..1dca249 100644 --- a/resources/lang/zh-TW/log.php +++ b/resources/lang/zh-TW/log.php @@ -106,5 +106,10 @@ return [ 'create' => '創建通道', 'update' => '更新通道', 'delete' => '删除通道' + ], + 'guard_booth_management' => [ + 'create' => '創建崗亭', + 'update' => '更新崗亭', + 'delete' => '删除崗亭' ] ]; diff --git a/resources/lang/zh-TW/service.php b/resources/lang/zh-TW/service.php index 901e121..abfbf65 100644 --- a/resources/lang/zh-TW/service.php +++ b/resources/lang/zh-TW/service.php @@ -104,5 +104,8 @@ return [ 'on-site' => '場內', 'off-site' => '場外', 'name_exists' => '通道名稱已存在' + ], + 'guard_booth_management' => [ + 'name_exists' => '崗亭名稱已存在' ] ]; diff --git a/resources/lang/zh-TW/validation.php b/resources/lang/zh-TW/validation.php index a57ce7c..1774c08 100644 --- a/resources/lang/zh-TW/validation.php +++ b/resources/lang/zh-TW/validation.php @@ -143,5 +143,8 @@ return [ 'po_empty' => '通道位置不能為空', 'pa_empty' => '所屬車場不能為空', 'g_empty' => '所屬崗亭不能為空' + ], + 'guard_booth_management' => [ + 'n_empty' => '崗亭名稱不能為空', ] ]; diff --git a/routes/admin/api.php b/routes/admin/api.php index 4f6fa41..ea1c31b 100644 --- a/routes/admin/api.php +++ b/routes/admin/api.php @@ -32,6 +32,7 @@ use App\Http\Controllers\Admin\ParkingManagementListController; use App\Http\Controllers\Admin\ParkingAttendantController; use App\Http\Controllers\Admin\RegionalManagementController; use App\Http\Controllers\Admin\ParkingChannelController; +use App\Http\Controllers\Admin\GuardBoothManagementController; Route::group(['prefix' => 'admin'], function () { @@ -248,6 +249,13 @@ Route::group(['prefix' => 'admin'], function () { Route::delete('/channelManagement/{id}', [ParkingChannelController::class, 'destroy']); Route::get('/channelManagement/rule', [ParkingChannelController::class, 'rule']); Route::get('/channelManagement/search', [ParkingChannelController::class, 'search']); + // 岗亭管理 + Route::get('/guardBoothManagement', [GuardBoothManagementController::class, 'index']); + Route::post('/guardBoothManagement', [GuardBoothManagementController::class, 'store']); + Route::get('/guardBoothManagement/edit/{id}', [GuardBoothManagementController::class, 'edit']); + Route::put('/guardBoothManagement/{id}', [GuardBoothManagementController::class, 'update']); + Route::delete('/guardBoothManagement/{id}', [GuardBoothManagementController::class, 'destroy']); + Route::get('/guardBoothManagement/rule', [GuardBoothManagementController::class, 'rule']); // 角色 Route::get('/roles', [RolesController::class, 'index']); Route::get('/roles/create', [RolesController::class, 'create']);