From f013e73d611662f8c341b62e400ee71dc9bc6f74 Mon Sep 17 00:00:00 2001 From: panliang <1163816051@qq.com> Date: Wed, 28 Sep 2022 15:22:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=95=86=E5=93=81=E5=AF=BC=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Form/Goods/ImportForm.php | 102 ++++++++++++++++++++++++++++++++-- src/Models/Goods.php | 2 +- 2 files changed, 98 insertions(+), 6 deletions(-) diff --git a/src/Form/Goods/ImportForm.php b/src/Form/Goods/ImportForm.php index 6f8841a..80d192a 100644 --- a/src/Form/Goods/ImportForm.php +++ b/src/Form/Goods/ImportForm.php @@ -6,6 +6,11 @@ use Dcat\Admin\Widgets\Form; use Dcat\EasyExcel\Excel; use Dcat\EasyExcel\Support\SheetCollection; use Illuminate\Support\Facades\Storage; +use Illuminate\Support\Str; +use Peidikeji\Goods\Models\Goods; +use Peidikeji\Goods\Models\GoodsBrand; +use Peidikeji\Goods\Models\GoodsCategory; +use Peidikeji\Goods\Models\GoodsType; class ImportForm extends Form { @@ -14,16 +19,103 @@ class ImportForm extends Form public function handle(array $input) { $disk = Storage::disk('public'); - Excel::import($disk->path($input['file']))->headings(false)->first()->chunk(500, function (SheetCollection $collection) { - $rows = $collection->toArray(); - // dd($rows); - }); - return $this->response()->success('导入成功'); + Excel::import($disk->path($input['file']))->headings(false)->first()->chunk(500, fn(SheetCollection $collection) => $this->mapGoods($collection->toArray())); + + return $this->response()->success('导入成功')->refresh(); } public function form() { $this->file('file')->autoUpload()->uniqueName()->move('goods/import')->accept('xlsx,xls')->disk('public'); } + + protected function mapGoods($rows) + { + array_shift($rows); + foreach ($rows as $row) { + $index = 0; + $sn = $row[$index]; + $category = $row[++$index]; + $brand = $row[++$index]; + $type = $row[++$index]; + $name = $row[++$index]; + $description = $row[++$index]; + $cover = $row[++$index]; + $images = $row[++$index]; + $content = $row[++$index]; + $price = $row[++$index]; + $stock = $row[++$index]; + $attr = $row[++$index]; + $spec = $row[++$index]; + $part = $row[++$index]; + + if (!$sn) { + throw new \Exception('编号必填'); + } + if (!$name) { + throw new \Exception('名称必填'); + } + if (!$category) { + throw new \Exception('分类必填'); + } + $category = GoodsCategory::firstOrCreate(['name' => $category]); + if ($brand) { + $brand = GoodsBrand::firstOrCreate(['name' => $brand]); + } + if ($type) { + $type = GoodsType::firstOrCreate(['name' => $type]); + } + $attributes = [ + 'category_id' => $category->id, + 'brand_id' => $brand?->id, + 'type_id' => $type?->id, + 'name' => $name, + 'description' => $description, + 'cover_image' => $cover, + 'images' => $images, + 'content' => $content, + 'price' => $price, + 'stock' => $stock, + 'attr' => $this->parseAttr($attr), + 'spec' => $this->parseAttr($spec), + 'part' => $this->parseAttr($part), + ]; + + $goods = Goods::where('goods_sn', $sn)->first(); + if (!$goods) { + $goods = new Goods(); + } + $goods->updateQuietly($attributes); + } + } + + protected function parseAttr($str = '') + { + if (!$str) { + return null; + } + $attr = []; + if (Str::contains($str, '\\n')) { + preg_match_all('/(.*)\n(.*)/', $str, $matches); + array_shift($matches); + } else { + $matches = [$str]; + } + foreach($matches as $item) { + preg_match_all('/(.*)\[(.*)\]/', $item[0], $itemMatch); + $name = data_get($itemMatch, '1.0'); + $values = []; + foreach(explode(',', data_get($itemMatch, '2.0')) as $k) { + $i = explode(':', $k); + array_push($values, ['name' => data_get($i, '0'), 'value' => data_get($i, '1')]); + } + array_push($attr, [ + 'name' => $name, + 'values' => $values, + ]); + } + + return $attr; + } } diff --git a/src/Models/Goods.php b/src/Models/Goods.php index ed93e34..a8655c2 100644 --- a/src/Models/Goods.php +++ b/src/Models/Goods.php @@ -17,7 +17,7 @@ class Goods extends Model protected $fillable = [ 'category_id', 'type_id', 'brand_id', - 'goods_sn', 'name', 'cover_image', 'description', 'content', + 'goods_sn', 'name', 'cover_image', 'images', 'description', 'content', 'price', 'on_sale', 'is_recommend', 'sold_count', 'stock', 'attr', 'part', 'spec', ];