LicensePlateService = $LicensePlateService; } /** * Display a listing of the resource. */ public function index(Request $request): JsonResponse { try { $query = ParkingLicensePlate::query(); // 车位类型搜索 $space_type_id = $request->input('space_type_id'); $query->where('space_type_id', '=', $space_type_id); // 分页 $page = $request->input('page', 1); $perPage = $request->input('per_page', 10); $total = $query->count(); $items = $query->latest()->forPage($page, $perPage)->select( ['id', 'number'] )->get(); 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() ); } } /** * Store a newly created resource in storage. */ public function store(Request $request) { try { $this->saveValidator($request->all()); $this->LicensePlateService->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.license_plate.create_failed') . ':' . $e->getMessage() ); } } /** * @param array $data * @param int $id * @return void * @throws ValidationException */ protected function saveValidator(array $data, int $id = 0): void { $rules = [ 'number' => 'required', ]; $messages = [ 'number.required' => __( 'validation.license_plate.n_empty' ) ]; if ($id) { $this->validateId($id, ParkingLicensePlate::class); } else { $rules['space_type_id'] = 'required'; $messages['space_type_id.required'] = __( 'validation.license_plate.type_empty' ); } $validator = Validator::make($data, $rules, $messages); if ($validator->fails()) { throw new ValidationException($validator); } } /** * @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->LicensePlateService->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( __('exception.license_plate.update_failed') . ':' . $e->getMessage() ); } } /** * @param string $id * @return JsonResponse * @throws CustomException * @throws ValidationException */ public function destroy(string $id): JsonResponse { try { $this->validateId($id, ParkingLicensePlate::class); $this->LicensePlateService->deleteModel($id); return $this->responseService->success( null, __('admin.delete_succeeded') ); } catch (ValidationException|CustomException $e) { throw $e; } catch (Exception $e) { return $this->responseService->systemError( __('exception.license_plate.destroy_failed') . ':' . $e->getMessage() ); } } /** * @param $id * @return JsonResponse * @throws CustomException * @throws ValidationException */ public function clear($id): JsonResponse { try { $this->validateId($id, ParkingLicensePlate::class); $this->LicensePlateService->clearModel($id); return $this->responseService->success( null, __('controller.license_plate.clear_success') ); } catch (ValidationException|CustomException $e) { throw $e; } catch (Exception $e) { return $this->responseService->systemError( __('exception.license_plate.clear_failed') . ':' . $e->getMessage() ); } } /** * @return BinaryFileResponse */ public function importTemplate(): BinaryFileResponse { return Excel::download( new ParkingLicensePlateImportTemplateExport(), __('exports.license_plate.import_template') . time() .'.xlsx' ); } /** * @param Request $request * @return JsonResponse * @throws ValidationException */ public function import(Request $request): JsonResponse { try { // 1. 验证上传的文件 $request->validate([ 'file' => 'required|mimes:xlsx,xls,csv|max:2048' // 限制文件类型和大小 ]); $validator = Validator::make($request->all(), [ 'file' => 'required|mimes:xlsx,xls,csv|max:2048' ], [ 'file.required' => __('validation.admin_list_vip.file_empty'), 'file.mimes' => __('validation.admin_list_vip.file_mimes'), 'file.max' => __('validation.admin_list_vip.file_max'), ]); if ($validator->fails()) { throw new ValidationException($validator); } // 2. 获取上传的文件 $file = $request->file('file'); // 3. 正确的做法:先存储文件,再获取绝对路径进行导入 // 将文件存储到 storage/app/imports 目录下 $path = $file->store('imports'); // 4. 执行导入(使用存储后的绝对路径) // storage_path('app') 获取 storage/app 的绝对路径 Excel::import( new ParkingLicensePlateImport(), storage_path('app/' . $path) ); // 5. (可选)导入完成后删除临时文件 Storage::delete($path); return $this->responseService->success( __('controller.import.success') ); } catch (ValidationException $e) { throw $e; } catch (Exception $e) { return $this->responseService->systemError( __('exception.admin_vip_list.import_failed') . ':' . $e->getMessage() ); } } /** * @return JsonResponse * @throws InvalidArgumentException */ public function rule(): JsonResponse { try { return $this->responseService->success( $this->methodShow('licensePlate') ); } catch (Exception $e) { return $this->responseService->systemError( __('exception.get_data_failed') . ':' . $e->getMessage() ); } } }