Updates to 03-2025

This commit is contained in:
Kevin Adametz 2025-04-01 10:36:47 +02:00
parent bfa3bb1df4
commit 9ae662f63e
243 changed files with 12580 additions and 12018 deletions

View file

@ -0,0 +1,129 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\User;
use App\Services\ShoppingUserService;
use Exception;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB;
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());
}
}
}