diff --git a/app/Http/Controllers/Admin/FloorController.php b/app/Http/Controllers/Admin/FloorController.php new file mode 100644 index 0000000..6e635a7 --- /dev/null +++ b/app/Http/Controllers/Admin/FloorController.php @@ -0,0 +1,197 @@ +responseService = $responseService; + $this->AdminFloorService = $AdminFloorService; + } + + /** + * @param Request $request + * @return JsonResponse + */ + public function index(Request $request): JsonResponse + { + try { + $query = AdminFloor::query(); + + // 分页 + $page = $request->input('page', 1); + $perPage = $request->input('per_page', 10); + + $total = $query->count(); + $items = $query->latest()->forPage($page, $perPage)->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.get_user_info_list_failed'); + 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->AdminFloorService->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.admin_floor.create_failed') . ':' . $e->getMessage( + ) + ); + } + } + + /** + * @param array $data + * @param int $id + * @return void + * @throws ValidationException + */ + public function saveValidator(array $data, int $id = 0): void + { + $rules = [ + 'name' => 'required|max:50', + 'region_data' => 'array' + ]; + $messages = [ + 'name.required' => __('validation.admin_floor.n_empty'), + 'name.max' => __('validation.admin_floor.n_max'), + 'region_data.array' => __('validation.admin_floor.r_array') + ]; + if ($id) { + $this->validateId($id, AdminFloor::class); + } + $validator = Validator::make($data, $rules, $messages); + + if ($validator->fails()) { + throw new ValidationException($validator); + } + } + + /** + * Show the form for editing the specified resource. + */ + public function edit(string $id): JsonResponse + { + try { + $this->validateId($id, AdminFloor::class); + $data = [ + 'region_list' => AdminFloorRegion::getFloorRegion($id), + 'item' => AdminFloor::query() + ->where('id', $id) + ->get() + ->toArray() + ]; + 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->AdminFloorService->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.admin_floor.update_failed') . ':' . $e->getMessage( + ) + ); + } + } + + /** + * @param string $id + * @return JsonResponse + * @throws CustomException + * @throws ValidationException + */ + public function destroy(string $id): JsonResponse + { + try { + $this->validateId($id, AdminFloor::class); + $this->AdminFloorService->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/Models/AdminFloor.php b/app/Models/AdminFloor.php new file mode 100644 index 0000000..1c4e122 --- /dev/null +++ b/app/Models/AdminFloor.php @@ -0,0 +1,22 @@ +where('floor_id', $floor_id)->select($columns) + ->get()->toArray(); + } + + public static function existsRoleMenu($floor_id, $nameArr): bool + { + $count = count($nameArr); + $existsCount = self::query()->where('floor_id', $floor_id) + ->whereIn('name', $nameArr)->count(); + return $count != $existsCount; + } + +} diff --git a/app/Services/AdminFloorService.php b/app/Services/AdminFloorService.php new file mode 100644 index 0000000..0b4f520 --- /dev/null +++ b/app/Services/AdminFloorService.php @@ -0,0 +1,173 @@ +logService = $logService; + } + + /** + * 创建角色 + * @param array $data + * @return Model|Builder + * @throws Exception + */ + public function createModel(array $data): Model|Builder + { + try { + DB::beginTransaction(); + $region_data = $data['region_data']; + + if (AdminFloor::query()->where('name', $data['name'])->exists()) { + throw new Exception(__('service.admin_floor.name_exists')); + } + + $model = AdminFloor::query()->create([ + 'name' => $data['name'], + 'image_url' => $data['image_url'], + 'created_at' => get_datetime() + ]); + + $this->logService->logCreated($model, '创建楼层'); + + $this->addAdminFloorRegion($model->id, $region_data); + + DB::commit(); + return $model; + } catch (Exception $e) { + DB::rollBack(); + throw $e; + } + } + + /** + * 同步添加区域关联数据 + * @param int $floor_id + * @param array $regionData + * @return void + */ + private function addAdminFloorRegion(int $floor_id, array $regionData): void + { + $regionSaveData = []; + foreach ($regionData as $value) { + $regionSaveData[] = [ + 'floor_id' => $floor_id, + 'name' => $value['name'], + 'created_at' => get_datetime() + ]; + } + AdminFloorRegion::query()->insert($regionSaveData); + $AdminRoleMenuData = AdminFloorRegion::getFloorRegion($floor_id); + $this->logService->logCreatedData( + new AdminFloorRegion(), + '创建楼层关联区域', + $AdminRoleMenuData + ); + } + + /** + * @param array $data + * @param int $id + * @return Model|Builder + * @throws Exception + */ + public function updateModel(array $data, int $id): Model|Builder + { + try { + DB::beginTransaction(); + $region_data = $data['region_data']; + + // 验证 + $existsWhere = [ + ['name', '=', $data['name']], + ['id', '<>', $id] + ]; + if (AdminFloor::query()->where($existsWhere)->exists()) { + throw new Exception(__('service.admin_role.name_exists')); + } + + // 更新 + $model = AdminFloor::query()->findOrFail($id); + $oldValues = $model->toArray(); + + $model->update([ + 'name' => $data['name'], + 'image_url' => $data['image_url'], + 'updated_at' => get_datetime() + ]); + + $this->logService->logUpdated($model, $oldValues, '更新楼层'); + + // 删除再创建关联 + $nameArr = array_column($region_data, 'name'); + if (AdminFloorRegion::existsRoleMenu($id, $nameArr)) { + $this->delAdminFloorRegion($id); + $this->addAdminFloorRegion($id, $region_data); + } + + DB::commit(); + return $model; + } catch (Exception $e) { + DB::rollBack(); + throw $e; + } + } + + protected function delAdminFloorRegion($floor_id): void + { + $oldData = AdminFloorRegion::query()->where('floor_id', $floor_id) + ->select()->get()->toArray(); + $this->logService->logDeletedData( + new AdminFloorRegion(), + '删除角色关联菜单', + $oldData + ); + AdminFloorRegion::query()->where('floor_id', $floor_id)->delete(); + } + + /** + * @param $id + * @return bool + * @throws Exception + */ + public function deleteModel($id): bool + { + try { + DB::beginTransaction(); + + $model = AdminFloor::query()->findOrFail($id); + + $this->logService->logDeleted($model, '删除楼层'); + + $model->delete(); + + $this->delAdminFloorRegion($id); + + DB::commit(); + return true; + } catch (Exception $e) { + DB::rollBack(); + throw $e; + } + } +}