ImportJob.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace App\Jobs\Import;
  3. use App\Events\SendWebSocketMessageEvent;
  4. use App\Models\Import;
  5. use App\Services\Import\ImportMafOrdersService;
  6. use App\Services\Import\ImportSparePartOrdersService;
  7. use App\Services\ImportCatalogService;
  8. use Illuminate\Support\Facades\Storage;
  9. use App\Services\ImportMafsService;
  10. use App\Services\ImportOrdersService;
  11. use App\Services\ImportReclamationsService;
  12. use Exception;
  13. use Illuminate\Contracts\Queue\ShouldQueue;
  14. use Illuminate\Foundation\Queue\Queueable;
  15. use Illuminate\Support\Facades\Log;
  16. class ImportJob implements ShouldQueue
  17. {
  18. use Queueable;
  19. /**
  20. * Create a new job instance.
  21. */
  22. public function __construct(
  23. private readonly Import $import,
  24. private readonly int $userId,
  25. )
  26. {}
  27. /**
  28. * Execute the job.
  29. */
  30. public function handle(): void
  31. {
  32. try {
  33. switch($this->import->type) {
  34. case 'orders':
  35. (new ImportOrdersService($this->import))->handle();
  36. break;
  37. case 'mafs':
  38. (new ImportMafsService($this->import))->handle();
  39. break;
  40. case 'reclamations':
  41. (new ImportReclamationsService($this->import))->handle();
  42. break;
  43. case 'catalog':
  44. (new ImportCatalogService($this->import, $this->import->year))->handle();
  45. break;
  46. case 'spare_part_orders':
  47. $this->handleSparePartOrdersImport();
  48. break;
  49. case 'maf_orders':
  50. $this->handleMafOrdersImport();
  51. break;
  52. }
  53. Log::info('Import ' . $this->import->type. ' job done!');
  54. event(new SendWebSocketMessageEvent('Импорт завершён!', $this->userId, ['path' => $this->import->filename, 'type' => $this->import->type]));
  55. } catch (Exception $e) {
  56. Log::info('Import ' . $this->import->type. ' job failed! ERROR: ' . $e->getMessage());
  57. event(new SendWebSocketMessageEvent('Ошибка импорта! ' . $e->getMessage(), $this->userId, ['error' => $e->getMessage()]));
  58. }
  59. }
  60. private function handleSparePartOrdersImport(): void
  61. {
  62. $filePath = Storage::disk('upload')->path($this->import->filename);
  63. $service = new ImportSparePartOrdersService($filePath, $this->userId);
  64. $result = $service->handle();
  65. // Записываем логи в Import
  66. foreach ($service->getLogs() as $logLine) {
  67. $this->import->log($logLine);
  68. }
  69. $this->import->status = $result['success'] ? 'DONE' : 'ERROR';
  70. $this->import->save();
  71. }
  72. private function handleMafOrdersImport(): void
  73. {
  74. $filePath = Storage::disk('upload')->path($this->import->filename);
  75. $service = new ImportMafOrdersService($filePath, $this->userId, $this->import->year);
  76. $result = $service->handle();
  77. // Записываем логи в Import
  78. foreach ($service->getLogs() as $logLine) {
  79. $this->import->log($logLine);
  80. }
  81. $this->import->status = $result['success'] ? 'DONE' : 'ERROR';
  82. $this->import->save();
  83. }
  84. }