diff --git a/database/seeders/AiTokenUsageMenuSeeder.php b/database/seeders/AiTokenUsageMenuSeeder.php index 9f84b824..e5f39563 100644 --- a/database/seeders/AiTokenUsageMenuSeeder.php +++ b/database/seeders/AiTokenUsageMenuSeeder.php @@ -11,16 +11,16 @@ public function run(): void { $tenantId = 1; - // AI 관리 부모 그룹 찾기 또는 생성 - $parentMenu = Menu::withoutGlobalScopes() + // 1. AI 관리 부모 그룹 찾기 또는 생성 + $aiGroup = Menu::withoutGlobalScopes() ->where('tenant_id', $tenantId) ->where('name', 'AI 관리') ->whereNull('parent_id') ->whereNull('deleted_at') ->first(); - if (! $parentMenu) { - $parentMenu = Menu::withoutGlobalScopes()->create([ + if (! $aiGroup) { + $aiGroup = Menu::withoutGlobalScopes()->create([ 'tenant_id' => $tenantId, 'parent_id' => null, 'name' => 'AI 관리', @@ -29,42 +29,63 @@ public function run(): void 'sort_order' => 12, 'is_active' => true, ]); - $this->command->info("AI 관리 부모 그룹 생성 완료 (id: {$parentMenu->id})"); + $this->command->info("AI 관리 부모 그룹 생성 완료 (id: {$aiGroup->id})"); + } else { + $this->command->info("AI 관리 부모 그룹 이미 존재 (id: {$aiGroup->id})"); } - // AI 토큰 사용량 메뉴 존재 확인 - $existingMenu = Menu::withoutGlobalScopes() + // 2. AI 설정 메뉴를 AI 관리 그룹으로 이동 (다른 그룹에 있는 경우) + $aiConfig = Menu::withoutGlobalScopes() ->where('tenant_id', $tenantId) - ->where('name', 'AI 토큰 사용량') - ->where('parent_id', $parentMenu->id) + ->where('name', 'AI 설정') ->whereNull('deleted_at') ->first(); - if ($existingMenu) { - $this->command->info('AI 토큰 사용량 메뉴가 이미 존재합니다.'); + if ($aiConfig && $aiConfig->parent_id !== $aiGroup->id) { + $aiConfig->update([ + 'parent_id' => $aiGroup->id, + 'sort_order' => 1, + ]); + $this->command->info("AI 설정 메뉴를 AI 관리 그룹으로 이동 완료"); + } elseif (! $aiConfig) { + $this->command->warn("AI 설정 메뉴가 존재하지 않습니다. (건너뜀)"); } else { - $maxSort = Menu::withoutGlobalScopes() - ->where('parent_id', $parentMenu->id) - ->whereNull('deleted_at') - ->max('sort_order') ?? 0; + $this->command->info("AI 설정 메뉴가 이미 AI 관리 그룹에 있습니다."); + } + // 3. AI 토큰 사용량 메뉴 생성 또는 이동 + $aiToken = Menu::withoutGlobalScopes() + ->where('tenant_id', $tenantId) + ->where('name', 'AI 토큰 사용량') + ->whereNull('deleted_at') + ->first(); + + if ($aiToken && $aiToken->parent_id !== $aiGroup->id) { + $aiToken->update([ + 'parent_id' => $aiGroup->id, + 'sort_order' => 2, + ]); + $this->command->info("AI 토큰 사용량 메뉴를 AI 관리 그룹으로 이동 완료"); + } elseif (! $aiToken) { Menu::withoutGlobalScopes()->create([ 'tenant_id' => $tenantId, - 'parent_id' => $parentMenu->id, + 'parent_id' => $aiGroup->id, 'name' => 'AI 토큰 사용량', 'url' => '/system/ai-token-usage', 'icon' => 'brain-circuit', - 'sort_order' => $maxSort + 1, + 'sort_order' => 2, 'is_active' => true, ]); - $this->command->info('AI 토큰 사용량 메뉴 생성 완료'); + $this->command->info("AI 토큰 사용량 메뉴 생성 완료"); + } else { + $this->command->info("AI 토큰 사용량 메뉴가 이미 AI 관리 그룹에 있습니다."); } // 결과 출력 $this->command->info(''); $this->command->info('=== AI 관리 하위 메뉴 ==='); $children = Menu::withoutGlobalScopes() - ->where('parent_id', $parentMenu->id) + ->where('parent_id', $aiGroup->id) ->whereNull('deleted_at') ->orderBy('sort_order') ->get(['name', 'url', 'sort_order']);