mivita/app/Console/Commands/SyncShoppingUserData.php
Kevin 316a9b76d6 Pint: Formatierung des bisherigen Stands, ohne inhaltliche Aenderung
Die Dateien lagen im Arbeitsbaum formatiert und inhaltlich geaendert
zusammen. Dieser Commit enthaelt nur die Pint-Formatierung des alten
Stands, damit die folgenden Commits nur noch Inhalt zeigen.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-23 15:39:17 +00:00

130 lines
4 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Services\ShoppingUserService;
use App\User;
use Exception;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class SyncShoppingUserData extends Command
{
// aufruf: php artisan shopping:sync-user-data --force --start=4
protected $signature = 'shopping:sync-user-data {user_id?} {--force} {--start=1}';
protected $description = 'Synchronisiere Shopping User Daten für einen oder alle User';
public function handle()
{
$userId = $this->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());
}
}
}