argument('user_id'); $force = $this->option('force'); $startId = $this->option('start'); try { if ($userId) { $this->syncSingleUser($userId); } else { $this->syncAllUsers($force, $startId); } } catch (Exception $e) { $this->error("Ein Fehler ist aufgetreten: " . $e->getMessage()); Log::error("Shopping User Sync Fehler: ", [ 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return 1; } return 0; } private function syncAllUsers($force, $startId) { $this->info("Starte Synchronisierung für alle User ab ID: {$startId}..."); $count = 0; $errors = []; // Aktiviere SQL Query Logging für Debugging DB::enableQueryLog(); User::where('id', '>=', $startId) ->orderBy('id') ->chunk(10, function($users) use (&$count, &$errors, $force) { foreach($users as $user) { try { $this->info("\nVerarbeite User ID: {$user->id} ({$user->email})"); $this->syncUser($user); $count++; $this->info("✓ User ID {$user->id} erfolgreich synchronisiert"); } catch (Exception $e) { $errorMessage = "Fehler bei User ID {$user->id} ({$user->email}): " . $e->getMessage(); $errors[] = $errorMessage; $this->error($errorMessage); // Log die letzten SQL Queries Log::error("Letzte SQL Queries:", [ 'queries' => DB::getQueryLog() ]); if (!$force) { throw $e; } } } }); $this->newLine(); $this->info("Synchronisierung abgeschlossen!"); $this->info("Gesamt synchronisierte User: {$count}"); if (count($errors) > 0) { $this->warn("Es gab " . count($errors) . " Fehler während der Synchronisierung:"); foreach($errors as $error) { $this->warn("- " . $error); } } } private function syncUser(User $user) { try { $this->output->write(" Setze Faker Mail... "); ShoppingUserService::setFakerMail($user); $this->info("✓"); $this->output->write(" Synchronisiere Numbers... "); ShoppingUserService::syncNumbersByEmail($user); $this->info("✓"); $this->output->write(" Synchronisiere Orders... "); ShoppingUserService::syncOrdersByEmail($user); $this->info("✓"); } catch (Exception $e) { throw new Exception($e->getMessage() . "\n" . $e->getTraceAsString()); } } private function syncSingleUser($userId) { $user = User::find($userId); if (!$user) { throw new Exception("User ID {$userId} nicht gefunden"); } $this->info("Starte Synchronisierung für User ID {$userId}..."); try { $this->syncUser($user); $this->info("✓ Synchronisierung erfolgreich abgeschlossen"); } catch (Exception $e) { throw new Exception("Fehler bei User ID {$userId}: " . $e->getMessage()); } } }