ExportYearData.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Jobs\Export\ExportYearDataJob;
  4. use App\Services\Export\ExportYearDataService;
  5. use Illuminate\Console\Command;
  6. class ExportYearData extends Command
  7. {
  8. protected $signature = 'app:export-year-data
  9. {year : Год для экспорта данных}
  10. {--queue : Выполнить в очереди}
  11. {--user= : ID пользователя для уведомлений (по умолчанию 1)}';
  12. protected $description = 'Экспорт всех данных CRM за указанный год в ZIP-архив';
  13. public function handle(): int
  14. {
  15. $year = (int) $this->argument('year');
  16. $userId = (int) ($this->option('user') ?? 1);
  17. if ($year < 2020 || $year > 2100) {
  18. $this->error("Некорректный год: {$year}");
  19. return self::FAILURE;
  20. }
  21. $this->info("Экспорт данных за {$year} год");
  22. if ($this->option('queue')) {
  23. ExportYearDataJob::dispatch($year, $userId);
  24. $this->info('Задание добавлено в очередь. Вы получите уведомление по завершении.');
  25. return self::SUCCESS;
  26. }
  27. $this->info('Начинаю экспорт...');
  28. try {
  29. $service = new ExportYearDataService($year, $userId);
  30. $downloadLink = $service->handle();
  31. $stats = $service->getStats();
  32. $this->info('Экспорт завершён!');
  33. $this->newLine();
  34. $this->info('Статистика:');
  35. $this->table(
  36. ['Сущность', 'Количество'],
  37. collect($stats)->map(fn($count, $name) => [$name, $count])->toArray()
  38. );
  39. $this->newLine();
  40. $this->info("Ссылка на скачивание: {$downloadLink}");
  41. return self::SUCCESS;
  42. } catch (\Exception $e) {
  43. $this->error('Ошибка экспорта: ' . $e->getMessage());
  44. return self::FAILURE;
  45. }
  46. }
  47. }