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; } }