From 2f0c441af2b5f0efdabfe9cd4f29ff12b13ec35a Mon Sep 17 00:00:00 2001 From: liapples Date: Mon, 25 Oct 2021 17:05:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=BC=E5=85=A5=E5=88=B0=E6=89=80=E6=9C=89?= =?UTF-8?q?=E4=BE=9B=E5=BA=94=E5=95=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Commands/Collector.php | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/app/Console/Commands/Collector.php b/app/Console/Commands/Collector.php index 21bdccc..e6f557a 100644 --- a/app/Console/Commands/Collector.php +++ b/app/Console/Commands/Collector.php @@ -4,6 +4,7 @@ namespace App\Console\Commands; use App\Models\CollectProduct; use App\Models\Product; +use App\Models\Supplier; use Illuminate\Console\Command; use Illuminate\Support\Facades\Http; @@ -21,7 +22,7 @@ class Collector extends Command * * @var string */ - protected $description = '驴妈妈产品采集'; + protected $description = '马蜂窝产品采集'; /** * 如果已经存在是否要更新,true更新,false不更新 @@ -48,9 +49,7 @@ class Collector extends Command { $supplier_id = $this->option('import'); if (!empty($supplier_id)) { - if (ctype_digit($supplier_id)) { - $this->import($supplier_id); - } + $this->import($supplier_id); $this->line("供应商 $supplier_id 导入完毕"); } else { // $this->exists_update = false; @@ -65,11 +64,11 @@ class Collector extends Command private function import($supplier_id) { $limit = $this->option('limit'); + $cpModel = CollectProduct::query()->orderBy('id', 'desc'); if (!empty($limit)) { $arr = explode(',', $limit); $count = count($arr); - $cpModel = CollectProduct::query()->orderBy('id', 'desc'); if ($count == 1) { $import_data = $cpModel->limit($arr[0])->get()->toArray(); } else if ($count == 2) { @@ -77,13 +76,27 @@ class Collector extends Command } else { $import_data = $cpModel->get()->toArray(); } + } else { + $import_data = $cpModel->get()->toArray(); + } + if (ctype_digit($supplier_id)) { array_walk($import_data, function ($v) use ($supplier_id) { $v['supplier_id'] = $supplier_id; unset($v['unique_id'], $v['site']); Product::query()->updateOrCreate(['supplier_id' => $supplier_id, 'title' => $v['title']], $v); }); + } else if ($supplier_id == 'all') { + $ids = Supplier::query()->pluck('id'); + foreach ($ids as $supplier_id) { + array_walk($import_data, function ($v) use ($supplier_id) { + $v['supplier_id'] = $supplier_id; + unset($v['unique_id'], $v['site']); + + Product::query()->updateOrCreate(['supplier_id' => $supplier_id, 'title' => $v['title']], $v); + }); + } } }