multi-store/app/admin/controller/LocalController.php

236 lines
9.5 KiB
PHP
Raw Normal View History

2024-12-23 17:03:51 +08:00
<?php
namespace app\admin\controller;
use app\common\model\store_category\StoreCategory;
use app\common\model\store_product\StoreProduct;
2024-12-30 17:57:27 +08:00
use app\common\model\store_product_group_price\StoreProductGroupPrice;
2024-12-23 17:03:51 +08:00
use PhpOffice\PhpSpreadsheet\IOFactory;
use support\Redis;
class LocalController extends BaseAdminController
{
public $notNeedLogin = ['setPrice'];
public function setPrice()
{
$file = $this->request->file('file');
$reader = IOFactory::createReader('Xlsx');
$spreadsheet = $reader->load($file->getRealPath());
$data = $spreadsheet->getActiveSheet()->toArray();
$insert = [];
2024-12-30 17:57:27 +08:00
$update = [];
$productIds = [];
foreach ($data as $k => $row) {
if ($k < 2) {
continue;
}
if (empty($row[0])) {
continue;
}
$productId = intval($row[0]);
if (empty($productId)) {
continue;
}
$productIds[] = $productId;
}
2024-12-30 17:57:27 +08:00
$products = StoreProduct::whereIn('id', $productIds)->field('id,store_name,purchase')->select()->toArray();
$products = reset_index($products, 'id');
foreach ($data as $k => $row) {
if ($k < 2) {
continue;
}
if (empty($row[0])) {
continue;
}
$productId = intval($row[0]);
if (empty($productId)) {
continue;
}
$product = $products[$productId] ?? [];
if (empty($product)) {
continue;
}
2024-12-30 17:57:27 +08:00
$groupIds = [
5 => 23,
7 => 24,
9 => 25,
11 => 26,
13 => 27,
15 => 28,
17 => 29,
19 => 30,
21 => 31,
23 => 32,
25 => 33,
27 => 34,
29 => 35,
31 => 36,
33 => 37,
35 => 1,
37 => 18,
39 => 22,
];
2024-12-30 17:57:27 +08:00
for ($i = 5; $i < 35; $i = $i + 2) {
$rate = intval(rtrim($row[$i] ?? 0, '%'));
if (empty($rate)) {
continue;
}
$price = $row[$i + 1] ?? 0;
if (empty($price)) {
$price = $product['purchase'] * (1 + $rate / 100);
}
$item = [
'product_id' => $productId,
'group_id' => $groupIds[$i],
'price_type' => 3,
'base_rate' => 100 + $rate,
'price' => $price
];
$insert[] = $item;
}
$productGroupPrices = StoreProductGroupPrice::where('product_id', $productId)->whereIn('group_id', [1, 18, 22])->select()->toArray();
$groupIds = array_flip($groupIds);
foreach ($productGroupPrices as $productGroupPrice) {
$cellId = $groupIds[$productGroupPrice['group_id']] ?? 0;
$rate = intval(rtrim($row[$cellId] ?? 0, '%'));
if (empty($rate)) {
continue;
}
$price = $row[$cellId + 1] ?? 0;
if (empty($price)) {
$price = $product['purchase'] * (1 + $rate / 100);
}
if ($price != $productGroupPrice['price']) {
$update[] = [
'id' => $productGroupPrice['id'],
'base_rate' => $rate,
'price' => $price,
];
}
}
}
if (count($insert) > 0) {
StoreProductGroupPrice::insertAll($insert);
}
if (count($update) > 0) {
(new StoreProductGroupPrice())->saveAll($update);
}
2024-12-31 11:29:25 +08:00
return $this->success('插入成功:' . count($insert) . '条,更新成功:' . count($update) . '条');
}
2024-12-25 14:10:28 +08:00
public function fixCategory()
{
$topCate = StoreCategory::where('pid', 0)->field('id,name,pid')->order('name')->select()->toArray();
$topCate = reset_index($topCate, 'name');
$sql = [];
$time = time();
foreach ($topCate as $item) {
if (isset($topCate[$item['name'] . '类'])) {
$target = $topCate[$item['name'] . '类'];
$sql[] = "##原分类id{$item['id']},原分类名:{$item['name']}目标分类id{$target['id']},目标分类名:{$target['name']}";
$sql[] = "update la_store_product set top_cate_id={$target['id']} where top_cate_id={$item['id']};";
$sql[] = "update la_store_product set two_cate_id={$target['id']} where two_cate_id={$item['id']};";
$sql[] = "update la_store_category set delete_time=$time where id={$item['id']};";
}
$secondCate = StoreCategory::where('pid', $item['id'])->field('id,name,pid')->order('name')->select()->toArray();
$secondCate = reset_index($secondCate, 'name');
foreach ($secondCate as $item2) {
if (isset($secondCate[$item2['name'] . '类'])) {
$target = $secondCate[$item2['name'] . '类'];
$sql[] = "##原分类id{$item2['id']},原分类名:{$item2['name']}目标分类id{$target['id']},目标分类名:{$target['name']}";
$sql[] = "update la_store_product set two_cate_id={$target['id']} where two_cate_id={$item2['id']};";
$sql[] = "update la_store_product set cate_id={$target['id']} where cate_id={$item2['id']};";
$sql[] = "update la_store_category set delete_time=$time where id={$item2['id']};";
}
$thirdCate = StoreCategory::where('pid', $item2['id'])->field('id,name,pid')->order('name')->select()->toArray();
$thirdCate = reset_index($thirdCate, 'name');
foreach ($thirdCate as $item3) {
if (isset($thirdCate[$item3['name'] . '类'])) {
$target = $thirdCate[$item3['name'] . '类'];
$sql[] = "##原分类id{$item3['id']},原分类名:{$item3['name']}目标分类id{$target['id']},目标分类名:{$target['name']}";
$sql[] = "update la_store_product set cate_id={$target['id']} where cate_id={$item3['id']};";
$sql[] = "update la_store_category set delete_time=$time where id={$item3['id']};";
}
}
}
}
file_put_contents(public_path() . '/update.sql', implode(PHP_EOL, $sql));
return $this->success('数据已更新完成', $sql);
}
2024-12-23 17:03:51 +08:00
public function index()
{
$file = $this->request->file('file');
$reader = IOFactory::createReader('Xlsx');
$spreadsheet = $reader->load($file->getRealPath());
$data = $spreadsheet->getActiveSheet()->toArray();
$updateCount = 0;
2024-12-23 17:42:48 +08:00
$finishCount = Redis::get('updateFinishCount');
2024-12-23 17:03:51 +08:00
$finishCount = empty($finishCount) ? 0 : $finishCount;
if ($finishCount >= count($data)) {
2024-12-23 17:42:48 +08:00
return $this->success('数据已更新完成');
2024-12-23 17:03:51 +08:00
}
$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++;
}
}
2024-12-23 17:42:48 +08:00
Redis::set('updateFinishCount', 100 + $finishCount);
return $this->success('更新成功:' . $updateCount . '条');
2024-12-23 17:03:51 +08:00
}
public function updateProduct($product, $row)
{
2024-12-23 17:42:48 +08:00
$topCateName = $row[1];
$secondCateName = $row[2];
$cateName = $row[3];
2024-12-23 17:03:51 +08:00
$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;
}
}