163 lines
5.9 KiB
PHP
163 lines
5.9 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\Attachment;
|
|
use App\Models\Order;
|
|
use App\Models\OrderArchive;
|
|
use App\Models\OrderArchivesItem;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class EmegrendelesCleanup extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'emegrendeles:cleanup {date : A dátum, ami előtti megrendeléseket törölni kell (YYYY-MM-DD)} {--dry-run : Csak szimuláció, nem töröl semmit}';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Megrendelések törlése egy adott dátum előtt az orders és order_archives táblákból.';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*/
|
|
public function handle(): int
|
|
{
|
|
$date = $this->argument('date');
|
|
|
|
if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $date)) {
|
|
$this->error('Érvénytelen dátum formátum. Használd az YYYY-MM-DD formátumot.');
|
|
return 1;
|
|
}
|
|
|
|
$dryRun = $this->option('dry-run');
|
|
|
|
// Számlálás
|
|
$ordersCount = Order::where('deliveryDate', '<', $date)->count();
|
|
|
|
$archiveQuery = OrderArchive::where('deliveryDate', '<', $date);
|
|
$archivesCount = $archiveQuery->count();
|
|
|
|
$itemsCount = OrderArchivesItem::whereIn('order_archive_id', function ($query) use ($date) {
|
|
$query->select('id')->from('order_archives')->where('deliveryDate', '<', $date);
|
|
})->count();
|
|
|
|
$attachmentsCount = Attachment::whereIn('attachable_id', function ($query) use ($date) {
|
|
$query->select('id')->from('order_archives')->where('deliveryDate', '<', $date);
|
|
})->where('attachable_type', OrderArchive::class)->count();
|
|
|
|
$this->info("Törlendő elemek száma ($date előtt):");
|
|
$this->table(
|
|
['Típus', 'Darabszám'],
|
|
[
|
|
['Megrendelések (orders)', $ordersCount],
|
|
['Archivált megrendelések (order_archives)', $archivesCount],
|
|
['Archivált tételek (order_archives_items)', $itemsCount],
|
|
['Csatolt fájlok (attachments)', $attachmentsCount],
|
|
]
|
|
);
|
|
|
|
// Első megmaradó megrendelés keresése
|
|
$firstRemainingOrder = Order::where('deliveryDate', '>=', $date)
|
|
->orderBy('deliveryDate', 'asc')
|
|
->orderBy('humanId', 'asc')
|
|
->first();
|
|
|
|
$firstRemainingArchive = OrderArchive::where('deliveryDate', '>=', $date)
|
|
->orderBy('deliveryDate', 'asc')
|
|
->orderBy('humanId', 'asc')
|
|
->first();
|
|
|
|
$firstRemaining = null;
|
|
if ($firstRemainingOrder && $firstRemainingArchive) {
|
|
if ($firstRemainingOrder->deliveryDate < $firstRemainingArchive->deliveryDate) {
|
|
$firstRemaining = $firstRemainingOrder;
|
|
} elseif ($firstRemainingOrder->deliveryDate > $firstRemainingArchive->deliveryDate) {
|
|
$firstRemaining = $firstRemainingArchive;
|
|
} else {
|
|
$firstRemaining = ($firstRemainingOrder->humanId <= $firstRemainingArchive->humanId)
|
|
? $firstRemainingOrder
|
|
: $firstRemainingArchive;
|
|
}
|
|
} else {
|
|
$firstRemaining = $firstRemainingOrder ?: $firstRemainingArchive;
|
|
}
|
|
|
|
if ($firstRemaining) {
|
|
$this->info("Az első megmaradó megrendelés:");
|
|
$this->line("- Human ID: " . ($firstRemaining->humanId ?: 'N/A'));
|
|
$this->line("- Dátum: " . $firstRemaining->deliveryDate);
|
|
} else {
|
|
$this->warn("Nincs megmaradó megrendelés a megadott dátumtól kezdődően.");
|
|
}
|
|
|
|
if ($dryRun) {
|
|
$this->info('Dry-run mód: nem történt törlés.');
|
|
return 0;
|
|
}
|
|
|
|
if ($ordersCount === 0 && $archivesCount === 0) {
|
|
$this->info('Nincs törlendő elem.');
|
|
return 0;
|
|
}
|
|
|
|
if (!$this->confirm('Biztosan törölni szeretnéd a megadott elemeket?')) {
|
|
$this->info('Művelet megszakítva.');
|
|
return 0;
|
|
}
|
|
|
|
$this->info('Törlés megkezdése...');
|
|
|
|
// Orders törlése
|
|
Order::where('deliveryDate', '<', $date)->delete();
|
|
$this->info('Megrendelések törölve.');
|
|
|
|
// Archivált adatok törlése optimalizáltan
|
|
$this->info('Archivált adatok törlése...');
|
|
$bar = $this->output->createProgressBar($archivesCount);
|
|
$bar->start();
|
|
|
|
OrderArchive::where('deliveryDate', '<', $date)->chunkById(1000, function ($archives) use ($bar) {
|
|
$archiveIds = $archives->pluck('id')->toArray();
|
|
|
|
// Kapcsolódó tételek törlése tömegesen
|
|
OrderArchivesItem::whereIn('order_archive_id', $archiveIds)->delete();
|
|
|
|
// Csatolmányok és fizikai fájlok kezelése
|
|
$attachments = Attachment::whereIn('attachable_id', $archiveIds)
|
|
->where('attachable_type', OrderArchive::class)
|
|
->get();
|
|
|
|
if ($attachments->isNotEmpty()) {
|
|
$filePaths = $attachments->map(function ($attachment) {
|
|
return $attachment->path . DIRECTORY_SEPARATOR . $attachment->filename;
|
|
})->toArray();
|
|
|
|
// Fizikai fájlok törlése a tárhelyről kötegelve
|
|
Storage::disk('attachments')->delete($filePaths);
|
|
|
|
// Attachment rekordok törlése az adatbázisból
|
|
Attachment::whereIn('id', $attachments->pluck('id'))->delete();
|
|
}
|
|
|
|
// Maguk az archívum rekordok törlése tömegesen
|
|
OrderArchive::whereIn('id', $archiveIds)->delete();
|
|
|
|
$bar->advance($archives->count());
|
|
});
|
|
|
|
$bar->finish();
|
|
$this->info("");
|
|
$this->info('Archivált megrendelések és kapcsolódó elemek törölve.');
|
|
|
|
return 0;
|
|
}
|
|
}
|