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'], 3 ); $item['name'] = $tr_name ?: $item['name']; $open_time_res = option_time( $item['open_time'] ); $item['open_time'] = $open_time_res['time']; $item['open_time_str'] = $open_time_res['str']; $close_time_res = option_time( $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); $Translation = AdminTranslationService::getTranslation( $item['id'], 3 ); $item['en_name'] = $Translation['en'] ?? ''; $item['tw_name'] = $Translation['zh_tw'] ?? ''; $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() ); } } }