227 lines
11 KiB
PHP
227 lines
11 KiB
PHP
<?php
|
||
|
||
namespace app\admin\controller;
|
||
|
||
use app\common\model\store_category\StoreCategory;
|
||
use app\common\model\store_product\StoreProduct;
|
||
use app\common\model\store_product_price\StoreProductPrice;
|
||
use PhpOffice\PhpSpreadsheet\IOFactory;
|
||
use support\Redis;
|
||
use think\facade\Db;
|
||
|
||
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 = [];
|
||
$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;
|
||
}
|
||
$products = StoreProduct::whereIn('id', $productIds)->field('id,store_name')->select()->toArray();
|
||
$products = reset_index($products, 'id');
|
||
$lastPurchasePrices = StoreProductPrice::whereIn('product_id', $productIds)->where('status', 1)->order('id desc')->select()->toArray();
|
||
$lastPurchasePrices = reset_index($lastPurchasePrices, 'product_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;
|
||
}
|
||
$lastPurchasePrice = $lastPurchasePrices[$productId] ?? [];
|
||
if (empty($lastPurchasePrice)) {
|
||
continue;
|
||
}
|
||
$storeRate = rtrim($row[4] ?? 0, '%');
|
||
$row[5] = rtrim($row[5] ?? 0, '%');
|
||
$row[7] = rtrim($row[7] ?? 0, '%');
|
||
$row[9] = rtrim($row[9] ?? 0, '%');
|
||
$row[11] = rtrim($row[11] ?? 0, '%');
|
||
$row[13] = rtrim($row[13] ?? 0, '%');
|
||
$row[15] = rtrim($row[15] ?? 0, '%');
|
||
$row[17] = rtrim($row[17] ?? 0, '%');
|
||
$row[19] = rtrim($row[19] ?? 0, '%');
|
||
$row[21] = rtrim($row[21] ?? 0, '%');
|
||
$row[23] = rtrim($row[23] ?? 0, '%');
|
||
$row[25] = rtrim($row[25] ?? 0, '%');
|
||
$row[27] = rtrim($row[27] ?? 0, '%');
|
||
$row[29] = rtrim($row[29] ?? 0, '%');
|
||
$row[31] = rtrim($row[31] ?? 0, '%');
|
||
$row[33] = rtrim($row[33] ?? 0, '%');
|
||
$row[35] = rtrim($row[35] ?? 0, '%');
|
||
$row[37] = rtrim($row[37] ?? 0, '%');
|
||
$row[39] = rtrim($row[39] ?? 0, '%');
|
||
$row[41] = rtrim($row[41] ?? 0, '%');
|
||
$storePrice = $lastPurchasePrice['purchase_price'] * (1 + $storeRate / 100);
|
||
$item = [
|
||
'product_id' => $product['id'],
|
||
'product_name' => $product['store_name'],
|
||
'store_price' => $storePrice,
|
||
'price_set' => json_encode([
|
||
['user_ship' => 23, 'rate' => $row[5], 'price' => bcmul($storePrice, (1 + $row[5] / 100), 2)],
|
||
['user_ship' => 24, 'rate' => $row[7], 'price' => bcmul($storePrice, (1 + $row[7] / 100), 2)],
|
||
['user_ship' => 25, 'rate' => $row[9], 'price' => bcmul($storePrice, (1 + $row[9] / 100), 2)],
|
||
['user_ship' => 26, 'rate' => $row[11], 'price' => bcmul($storePrice, (1 + $row[11] / 100), 2)],
|
||
['user_ship' => 27, 'rate' => $row[13], 'price' => bcmul($storePrice, (1 + $row[13] / 100), 2)],
|
||
['user_ship' => 28, 'rate' => $row[15], 'price' => bcmul($storePrice, (1 + $row[15] / 100), 2)],
|
||
['user_ship' => 29, 'rate' => $row[17], 'price' => bcmul($storePrice, (1 + $row[17] / 100), 2)],
|
||
['user_ship' => 30, 'rate' => $row[19], 'price' => bcmul($storePrice, (1 + $row[19] / 100), 2)],
|
||
['user_ship' => 31, 'rate' => $row[21], 'price' => bcmul($storePrice, (1 + $row[21] / 100), 2)],
|
||
['user_ship' => 32, 'rate' => $row[23], 'price' => bcmul($storePrice, (1 + $row[23] / 100), 2)],
|
||
['user_ship' => 33, 'rate' => $row[25], 'price' => bcmul($storePrice, (1 + $row[25] / 100), 2)],
|
||
['user_ship' => 34, 'rate' => $row[27], 'price' => bcmul($storePrice, (1 + $row[27] / 100), 2)],
|
||
['user_ship' => 35, 'rate' => $row[29], 'price' => bcmul($storePrice, (1 + $row[29] / 100), 2)],
|
||
['user_ship' => 36, 'rate' => $row[31], 'price' => bcmul($storePrice, (1 + $row[31] / 100), 2)],
|
||
['user_ship' => 37, 'rate' => $row[33], 'price' => bcmul($storePrice, (1 + $row[33] / 100), 2)],
|
||
['user_ship' => 38, 'rate' => $row[35], 'price' => bcmul($storePrice, (1 + $row[35] / 100), 2)],
|
||
['user_ship' => 39, 'rate' => $row[37], 'price' => bcmul($storePrice, (1 + $row[37] / 100), 2)],
|
||
['user_ship' => 40, 'rate' => $row[39], 'price' => bcmul($storePrice, (1 + $row[39] / 100), 2)],
|
||
['user_ship' => 41, 'rate' => $row[41], 'price' => bcmul($storePrice, (1 + $row[41] / 100), 2)],
|
||
], JSON_UNESCAPED_UNICODE),
|
||
];
|
||
$insert[] = $item;
|
||
}
|
||
$count = Db::name('product_price_set')->insertAll($insert);
|
||
return $this->success('插入成功:' . $count . '条');
|
||
}
|
||
|
||
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);
|
||
}
|
||
|
||
public function index()
|
||
{
|
||
$file = $this->request->file('file');
|
||
$reader = IOFactory::createReader('Xlsx');
|
||
$spreadsheet = $reader->load($file->getRealPath());
|
||
$data = $spreadsheet->getActiveSheet()->toArray();
|
||
$updateCount = 0;
|
||
$finishCount = Redis::get('updateFinishCount');
|
||
$finishCount = empty($finishCount) ? 0 : $finishCount;
|
||
if ($finishCount >= count($data)) {
|
||
return $this->success('数据已更新完成');
|
||
}
|
||
$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('updateFinishCount', 100 + $finishCount);
|
||
return $this->success('更新成功:' . $updateCount . '条');
|
||
}
|
||
|
||
public function updateProduct($product, $row)
|
||
{
|
||
$topCateName = $row[1];
|
||
$secondCateName = $row[2];
|
||
$cateName = $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;
|
||
}
|
||
|
||
} |