ImportJob.php 4.1 KB

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