service = $service; } /** * @param Request $request * @return JsonResponse */ public function index(Request $request): JsonResponse { try { $query = AdminFloorRegion::query(); if ($request->has('floor_id')) { $floor_id = $request->input('floor_id'); if ($floor_id) { $query->where('floor_id', $floor_id); } } 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); $statusArr = AdminFloorRegionService::getStatus(); $total = $query->count(); $items = $query->latest()->forPage($page, $perPage)->get()->each( function ($item) use ($statusArr) { $item['name'] = AdminTranslationService::getTranslationTypeName( $item['id'], 5, $item['name'] ); $item['floor'] = AdminFloor::getName($item['floor_id']); $item['status_str'] = $statusArr[$item['status']]; $item['parking_space_count'] = ParkingSpace::getFloorCount( $item['floor_id'] ); $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']; 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() ); } } /** * 列表搜索数据 * @return JsonResponse */ public function search(): JsonResponse { try { $data = [ 'floor_list' => AdminFloor::getData() ]; return $this->responseService->success($data); } catch (Exception $e) { $m_prefix = __('exception.exception_handler.resource'); return $this->responseService->systemError( $m_prefix . ':' . $e->getMessage() ); } } /** * @return JsonResponse */ public function create(): JsonResponse { try { $data = [ 'building_floor_list' => get_select_data( AdminFloorService::getBuildingFloor() ), 'floor_list' => AdminFloor::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( __('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|max:50', 'open_time' => 'required|size:5', 'close_time' => 'required|size:5', ]; $messages = [ 'name.required' => __('validation.admin_floor_region.n_empty'), 'name.max' => __('validation.admin_floor_region.n_max'), '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' ) ]; if ($id) { $this->validateId($id, AdminFloorRegion::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, AdminFloorRegion::class); $data = [ 'building_floor_list' => get_select_data( AdminFloorService::getBuildingFloor() ), 'floor_list' => AdminFloor::getData(), 'item' => AdminFloorRegion::query()->find($id) ]; $AdminFloor = AdminFloor::getFirst($data['item']['floor_id']); $data['item']['building_floor'] = $AdminFloor['building_floor'] ?? ''; $Translation = AdminTranslationService::getTranslation( $data['item']['id'], 5 ); $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, AdminFloorRegion::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( __('admin.operation_failed') . ':' . $e->getMessage() ); } } }