| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- <?php
- namespace App\Console\Commands;
- use App\Jobs\Export\ExportYearDataJob;
- use App\Services\Export\ExportYearDataService;
- use Illuminate\Console\Command;
- class ExportYearData extends Command
- {
- protected $signature = 'app:export-year-data
- {year : Год для экспорта данных}
- {--queue : Выполнить в очереди}
- {--user= : ID пользователя для уведомлений (по умолчанию 1)}';
- protected $description = 'Экспорт всех данных CRM за указанный год в ZIP-архив';
- public function handle(): int
- {
- $year = (int) $this->argument('year');
- $userId = (int) ($this->option('user') ?? 1);
- if ($year < 2020 || $year > 2100) {
- $this->error("Некорректный год: {$year}");
- return self::FAILURE;
- }
- $this->info("Экспорт данных за {$year} год");
- if ($this->option('queue')) {
- ExportYearDataJob::dispatch($year, $userId);
- $this->info('Задание добавлено в очередь. Вы получите уведомление по завершении.');
- return self::SUCCESS;
- }
- $this->info('Начинаю экспорт...');
- try {
- $service = new ExportYearDataService($year, $userId);
- $downloadLink = $service->handle();
- $stats = $service->getStats();
- $this->info('Экспорт завершён!');
- $this->newLine();
- $this->info('Статистика:');
- $this->table(
- ['Сущность', 'Количество'],
- collect($stats)->map(fn($count, $name) => [$name, $count])->toArray()
- );
- $this->newLine();
- $this->info("Ссылка на скачивание: {$downloadLink}");
- return self::SUCCESS;
- } catch (\Exception $e) {
- $this->error('Ошибка экспорта: ' . $e->getMessage());
- return self::FAILURE;
- }
- }
- }
|