From ad57a6f9a5371ea9335d5b9f84e9abf32b584cd3 Mon Sep 17 00:00:00 2001 From: lewis <604446095@qq.com> Date: Mon, 23 Dec 2024 17:02:50 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E5=95=86=E5=93=81=E5=88=86?= =?UTF-8?q?=E7=B1=BB=E7=9A=84=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/controller/LocalController.php | 89 ++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 app/admin/controller/LocalController.php diff --git a/app/admin/controller/LocalController.php b/app/admin/controller/LocalController.php new file mode 100644 index 000000000..6a592345d --- /dev/null +++ b/app/admin/controller/LocalController.php @@ -0,0 +1,89 @@ +request->file('file'); + $reader = IOFactory::createReader('Xlsx'); + $spreadsheet = $reader->load($file->getRealPath()); + $data = $spreadsheet->getActiveSheet()->toArray(); + $updateCount = 0; + $finishCount = Redis::get('finishCount'); + $finishCount = empty($finishCount) ? 0 : $finishCount; + if ($finishCount >= count($data)) { + echo '数据已更新完成'; + return; + } + $max = $finishCount + 100; + foreach ($data as $k => $row) { + if ($k < (1 + $finishCount) || empty($row[0])) { + continue; + } + if ($k > $max) { + break; + } + $product = StoreProduct::where('id', $row[0])->field('id,store_name,top_cate_id,two_cate_id,cate_id')->findOrEmpty()->toArray(); + if (empty($product)) { + continue; + } + $result = $this->updateProduct($product, $row); + if ($result) { + $updateCount++; + } + } + Redis::set('finishCount', 100 + $finishCount); + echo '更新成功:' . $updateCount . '条'; + } + + public function updateProduct($product, $row) + { + $topCateName = rtrim($row[1], '类'); + $secondCateName = rtrim($row[2], '类'); + $cateName = rtrim($row[3], '类'); + $topCate = StoreCategory::where('name', $topCateName)->value('id'); + $updateData = []; + if (!empty($topCate) && $topCate != $product['top_cate_id']) { + $updateData['top_cate_id'] = $topCate; + } + $secondCateId = StoreCategory::where('pid', $topCate)->where('name', $secondCateName)->value('id'); + if (empty($secondCateId)) { + $secondCate = new StoreCategory(); + $secondCate->name = $secondCateName; + $secondCate->pid = $topCate; + $secondCate->save(); + $secondCateId = $secondCate->id; + } + if ($secondCateId != $product['two_cate_id']) { + $updateData['two_cate_id'] = $secondCateId; + } + $cateId = StoreCategory::where('pid', $secondCateId)->where('name', $cateName)->value('id'); + if (empty($cateId)) { + $cate = new StoreCategory(); + $cate->name = $cateName; + $cate->pid = $secondCateId; + $cate->save(); + $cateId = $cate->id; + } + if ($cateId != $product['cate_id']) { + $updateData['cate_id'] = $cateId; + } + if (!empty($updateData)) { + StoreProduct::where('id', $row[0])->update($updateData); + echo '更新成功ID:' . $row[0] . PHP_EOL; + return true; + } + return false; + } + +} \ No newline at end of file