ImportJob.php 3.5 KB

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