LoadExcels.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Handlers\LoadExcelHandler;
  4. use App\Import;
  5. use Illuminate\Console\Command;
  6. use Illuminate\Support\Facades\Artisan;
  7. class LoadExcels extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'excel:load {id}';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = '读取excel';
  21. /**
  22. * Create a new command instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return mixed
  34. */
  35. public function handle()
  36. {
  37. $info = Import::where('id', $this->argument('id'))->first();
  38. $data = (new LoadExcelHandler)->load(public_path('uploads') . '/' . $info->path);
  39. $category = collect(Import::$CATEGORY)->where('id', $info->category)->first();
  40. unset($data[0]);
  41. $data = collect($data)->map(function ($v) use ($category) {
  42. return array_combine(array_column($category['fields'], 'field'), array_only($v, array_column($category['fields'], 'column')));
  43. })->toArray();
  44. $info->is_load = 'T';
  45. $info->data = serialize($data);
  46. $info->save();
  47. if ($info->is_import === 'T') {
  48. Artisan::queue('database:import', ['id' => $info->id]);
  49. }
  50. }
  51. }