ImportJob.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace App\Jobs\Import;
  3. use App\Events\SendWebSocketMessageEvent;
  4. use App\Models\Import;
  5. use App\Services\Import\ImportCommonCatalogService;
  6. use App\Services\Import\ImportMafOrdersService;
  7. use App\Services\Import\ImportSparePartOrdersService;
  8. use App\Services\Import\ImportStockOrdersService;
  9. use App\Services\ImportCatalogService;
  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. use Illuminate\Support\Facades\Storage;
  18. class ImportJob implements ShouldQueue
  19. {
  20. use Queueable;
  21. /**
  22. * Create a new job instance.
  23. */
  24. public function __construct(
  25. private readonly Import $import,
  26. private readonly int $userId,
  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 'stock_orders':
  51. app(ImportStockOrdersService::class, [
  52. 'import' => $this->import,
  53. 'userId' => $this->userId,
  54. ])->handle();
  55. break;
  56. case 'spare_part_orders':
  57. $this->handleSparePartOrdersImport();
  58. break;
  59. case 'maf_orders':
  60. $this->handleMafOrdersImport();
  61. break;
  62. }
  63. Log::info('Import '.$this->import->type.' job done!');
  64. event(new SendWebSocketMessageEvent('Импорт завершён!', $this->userId, ['path' => $this->import->filename, 'type' => $this->import->type]));
  65. } catch (Exception $e) {
  66. Log::info('Import '.$this->import->type.' job failed! ERROR: '.$e->getMessage());
  67. event(new SendWebSocketMessageEvent('Ошибка импорта! '.$e->getMessage(), $this->userId, ['error' => $e->getMessage()]));
  68. }
  69. }
  70. private function handleSparePartOrdersImport(): void
  71. {
  72. $filePath = Storage::disk('upload')->path($this->import->filename);
  73. $service = new ImportSparePartOrdersService($filePath, $this->userId);
  74. $result = $service->handle();
  75. // Записываем логи в Import
  76. foreach ($service->getLogs() as $logLine) {
  77. $this->import->log($logLine);
  78. }
  79. $this->import->status = $result['success'] ? 'DONE' : 'ERROR';
  80. $this->import->save();
  81. }
  82. private function handleMafOrdersImport(): void
  83. {
  84. $filePath = Storage::disk('upload')->path($this->import->filename);
  85. $service = new ImportMafOrdersService($filePath, $this->userId, $this->import->year);
  86. $result = $service->handle();
  87. // Записываем логи в Import
  88. foreach ($service->getLogs() as $logLine) {
  89. $this->import->log($logLine);
  90. }
  91. $this->import->status = $result['success'] ? 'DONE' : 'ERROR';
  92. $this->import->save();
  93. }
  94. }