update 20.10.2025
This commit is contained in:
parent
8c11130b5d
commit
a939cd51ef
616 changed files with 84821 additions and 4121 deletions
136
app/Console/Commands/BusinessClearData.php
Normal file
136
app/Console/Commands/BusinessClearData.php
Normal file
|
|
@ -0,0 +1,136 @@
|
|||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\UserBusinessStructure;
|
||||
use App\Models\UserBusiness;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class BusinessClearData extends Command
|
||||
{
|
||||
/**
|
||||
* php artisan business:clear-data {month} {year}
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'business:clear-data {month} {year} {--force : Force deletion without confirmation}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Clear stored business structure data for a specific month/year';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
try {
|
||||
$month = (int) $this->argument('month');
|
||||
$year = (int) $this->argument('year');
|
||||
|
||||
// Validierung
|
||||
if ($month < 1 || $month > 12) {
|
||||
$this->error('Invalid month. Must be between 1 and 12.');
|
||||
return 1;
|
||||
}
|
||||
|
||||
$currentYear = (int) date('Y');
|
||||
if ($year < 2020 || $year > $currentYear + 1) {
|
||||
$this->error('Invalid year. Must be between 2020 and ' . ($currentYear + 1));
|
||||
return 1;
|
||||
}
|
||||
|
||||
$this->info("Preparing to clear business data for month: {$month} | year: {$year}");
|
||||
|
||||
// Finde bestehende Struktur
|
||||
$existingStructure = UserBusinessStructure::where('year', $year)
|
||||
->where('month', $month)
|
||||
->first();
|
||||
|
||||
if (!$existingStructure) {
|
||||
$this->info('No stored business structure found for the specified month/year');
|
||||
return 0;
|
||||
}
|
||||
|
||||
$structureId = $existingStructure->id;
|
||||
$userBusinessCount = UserBusiness::where('b_structure_id', $structureId)->count();
|
||||
$userCount = is_array($existingStructure->users) ? count($existingStructure->users) : 0;
|
||||
|
||||
$this->info("Found structure ID: {$structureId}");
|
||||
$this->info("- UserBusiness records: {$userBusinessCount}");
|
||||
$this->info("- Users in structure: {$userCount}");
|
||||
$this->info("- Completed: " . ($existingStructure->completed ? 'Yes' : 'No'));
|
||||
|
||||
// Bestätigung (außer bei --force)
|
||||
if (!$this->option('force')) {
|
||||
if (!$this->confirm('Are you sure you want to delete this business structure data?')) {
|
||||
$this->info('Operation cancelled by user');
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
$startTime = microtime(true);
|
||||
|
||||
// Lösche UserBusiness Einträge
|
||||
if ($userBusinessCount > 0) {
|
||||
$this->info("Deleting {$userBusinessCount} UserBusiness records...");
|
||||
UserBusiness::where('b_structure_id', $structureId)->delete();
|
||||
$this->info('✓ UserBusiness records deleted');
|
||||
}
|
||||
|
||||
// Lösche UserBusinessStructure
|
||||
$this->info('Deleting UserBusinessStructure...');
|
||||
$existingStructure->delete();
|
||||
$this->info('✓ UserBusinessStructure deleted');
|
||||
|
||||
// Garbage Collection
|
||||
gc_collect_cycles();
|
||||
|
||||
$endTime = microtime(true);
|
||||
$duration = round(($endTime - $startTime) * 1000, 2);
|
||||
|
||||
$this->info("✅ Successfully cleared all business data in {$duration}ms");
|
||||
$this->logMemoryUsage();
|
||||
|
||||
return 0;
|
||||
} catch (\Exception $e) {
|
||||
$this->error('Error clearing business data: ' . $e->getMessage());
|
||||
$this->error('Stack trace: ' . $e->getTraceAsString());
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loggt aktuelle Memory-Nutzung
|
||||
*/
|
||||
private function logMemoryUsage(): void
|
||||
{
|
||||
$currentMemory = memory_get_usage();
|
||||
$peakMemory = memory_get_peak_usage();
|
||||
|
||||
$currentFormatted = $this->formatBytes($currentMemory);
|
||||
$peakFormatted = $this->formatBytes($peakMemory);
|
||||
|
||||
$this->info("Memory - Current: {$currentFormatted} | Peak: {$peakFormatted}");
|
||||
}
|
||||
|
||||
/**
|
||||
* Formatiert Bytes in lesbare Einheiten
|
||||
*/
|
||||
private function formatBytes(int $bytes, int $precision = 2): string
|
||||
{
|
||||
$units = array('B', 'KB', 'MB', 'GB', 'TB');
|
||||
|
||||
for ($i = 0; $bytes > 1024 && $i < count($units) - 1; $i++) {
|
||||
$bytes /= 1024;
|
||||
}
|
||||
|
||||
return round($bytes, $precision) . ' ' . $units[$i];
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue