ImportOrder.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Imports\chatOrder;
  4. use App\Imports\Order\vaccineOrder;
  5. use Illuminate\Console\Command;
  6. use Maatwebsite\Excel\Facades\Excel;
  7. class ImportOrder extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'importOrder {type} {filepath}';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = '订单数据导入';
  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. $type = $this->argument('type');
  38. $file_path = $this->argument('filepath');
  39. //路径相对于项目根目录即 /public
  40. if(empty($type) || empty($file_path)){
  41. $this->output->error('请输入完整参数');
  42. }
  43. //图文咨询订单导入
  44. if($type == 'chat'){
  45. $this->makeChat($file_path);
  46. }
  47. if($type == 'vaccine'){
  48. $this->makeVaccine($file_path);
  49. }
  50. }
  51. public function makeChat($file_path)
  52. {
  53. $filePath = './public/import/'.$file_path.'.xls';
  54. if(!file_exists($filePath)){
  55. $this->output->error('文件不存在');
  56. exit;
  57. }
  58. Excel::import(new chatOrder(), $filePath);
  59. $this->output->success('Import successful');
  60. }
  61. public function makeVaccine($file_path)
  62. {
  63. $filePath = './public/import/'.$file_path.'.xls';
  64. if(!file_exists($filePath)){
  65. $this->output->error('文件不存在');
  66. exit;
  67. }
  68. Excel::import(new vaccineOrder(), $filePath);
  69. $this->output->success('Import successful');
  70. }
  71. }