ExportCommand.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Exceptions\ClientException;
  4. use App\Import\CategoryImport;
  5. use App\Models\Goods\Category;
  6. use EasyWeChat\Kernel\Exceptions\ServiceNotFoundException;
  7. use Guanguans\Notify\Factory;
  8. use http\Exception\RuntimeException;
  9. use Illuminate\Console\Command;
  10. use Illuminate\Support\Facades\DB;
  11. use Maatwebsite\Excel\Facades\Excel;
  12. class ExportCommand extends Command
  13. {
  14. /**
  15. * The name and signature of the console command.
  16. *
  17. * @var string
  18. */
  19. protected $signature = 'export {cmd?}';
  20. /**
  21. * The console command description.
  22. *
  23. * @var string
  24. */
  25. protected $description = 'Command description';
  26. public function handle()
  27. {
  28. $cmd = $this->argument('cmd');
  29. $args = $this->arguments();
  30. unset($args['command'], $args['cmd']);
  31. $params = array_values($args);
  32. if (empty($cmd)) {
  33. echo 'Nothing to do...';
  34. } else {
  35. if (method_exists($this, $cmd)) {
  36. call_user_func_array([$this, $cmd], $params);
  37. } else {
  38. echo 'cmd not exists';
  39. }
  40. }
  41. return;
  42. }
  43. public function category()
  44. {
  45. //从一个表格中获取数据,并放入到一个数组中
  46. $r = (new CategoryImport())->collection(Excel::toCollection(null, storage_path('app/public/category.xls'))[0]);
  47. DB::transaction(function () use ($r) {
  48. foreach ($r as $k => $v) {
  49. if ($k == 0) {
  50. continue;
  51. }
  52. /** @var Category $category */
  53. $category = Category::where("name", $v[1])->first();
  54. $data = [
  55. 'name' => $v[0],
  56. 'parent_id' => optional($category)->id ?? 0,
  57. 'level' => optional($category)->level + 1,
  58. 'thumb' => "https://iph.href.lu/400x200",
  59. 'weight' => $v[2],
  60. 'index_weight' => $v[3],
  61. "category_weight" => $v[5],
  62. "search_weight" => $v[4],
  63. ];
  64. $c = new Category($data);
  65. $c->save();
  66. }
  67. });
  68. }
  69. public function robot()
  70. {
  71. throw new ServiceNotFoundException("找不到");
  72. // dump(1/0);
  73. }
  74. }