where('tenant_type', Tenant::TYPE_DEMO_SHOWCASE) ->get(); if ($showcases->isEmpty()) { $this->info('데모 쇼케이스 테넌트가 없습니다.'); return self::SUCCESS; } foreach ($showcases as $tenant) { $this->info("리셋 대상: [{$tenant->id}] {$tenant->company_name}"); if ($this->option('dry-run')) { $this->showStats($tenant); continue; } $this->resetTenantData($tenant); if ($this->option('seed')) { $this->seedSampleData($tenant); } } return self::SUCCESS; } private function showStats(Tenant $tenant): void { foreach (self::RESET_TABLES as $table) { if (! \Schema::hasTable($table)) { continue; } if (! \Schema::hasColumn($table, 'tenant_id')) { continue; } $count = DB::table($table)->where('tenant_id', $tenant->id)->count(); if ($count > 0) { $this->line(" - {$table}: {$count}건"); } } } private function resetTenantData(Tenant $tenant): void { $totalDeleted = 0; DB::beginTransaction(); try { foreach (self::RESET_TABLES as $table) { if (! \Schema::hasTable($table)) { continue; } if (! \Schema::hasColumn($table, 'tenant_id')) { continue; } $deleted = DB::table($table)->where('tenant_id', $tenant->id)->delete(); if ($deleted > 0) { $this->line(" 삭제: {$table} → {$deleted}건"); $totalDeleted += $deleted; } } DB::commit(); $this->info(" 총 {$totalDeleted}건 삭제 완료"); Log::info('데모 쇼케이스 리셋 완료', [ 'tenant_id' => $tenant->id, 'deleted_count' => $totalDeleted, ]); } catch (\Exception $e) { DB::rollBack(); $this->error(" 리셋 실패: {$e->getMessage()}"); Log::error('데모 쇼케이스 리셋 실패', [ 'tenant_id' => $tenant->id, 'error' => $e->getMessage(), ]); return; } } private function seedSampleData(Tenant $tenant): void { $preset = $tenant->getDemoPreset() ?? 'manufacturing'; $this->info(" 샘플 데이터 시드: {$preset}"); try { $seeder = new \Database\Seeders\Demo\ManufacturingPresetSeeder; $seeder->run($tenant->id); $this->info(' 샘플 데이터 시드 완료'); } catch (\Exception $e) { $this->error(" 시드 실패: {$e->getMessage()}"); Log::error('데모 샘플 데이터 시드 실패', [ 'tenant_id' => $tenant->id, 'error' => $e->getMessage(), ]); } } }