feat: 完善导入相关
parent
a28b1f034d
commit
fc32752815
|
@ -37,6 +37,7 @@ use PhpOffice\PhpSpreadsheet\IOFactory;
|
|||
use think\exception\ValidateException;
|
||||
use app\admin\validate\goods\GoodsValidate;
|
||||
use think\facade\Db;
|
||||
use think\facade\Log;
|
||||
use think\response\Json;
|
||||
|
||||
/**
|
||||
|
@ -377,9 +378,7 @@ class Goods extends AdminBase
|
|||
$totalCount = 0;
|
||||
|
||||
$batchCode = [];// 同批次商品编码
|
||||
$needInsertGoods = [];// 同批次要插入的商品
|
||||
|
||||
$now = time();
|
||||
// 读取数据并批量导入
|
||||
for ($row = 2; $row <= $highestRow; $row++) {
|
||||
$data = [];
|
||||
|
@ -404,55 +403,27 @@ class Goods extends AdminBase
|
|||
$data['content'] = (string)$worksheet->getCell('J' . $row)->getValue();
|
||||
$data['price'] = (string)$worksheet->getCell('K' . $row)->getValue();
|
||||
$data['stock'] = (string)$worksheet->getCell('L' . $row)->getValue();
|
||||
$data['custom_param'] = (string)$worksheet->getCell('M' . $row)->getValue();
|
||||
$data['custom_params'] = (string)$worksheet->getCell('M' . $row)->getValue();
|
||||
|
||||
$dataAll[$data['code']] = $data;
|
||||
// // 每N条记录进行一次批量插入操作,可以调整batchSize的值
|
||||
// Log::write(sprintf('excel row:%s total:%s', $row, $totalCount));
|
||||
if ($totalCount % $batchSize == 0) {
|
||||
// 已存在的商品编码数组
|
||||
$exists = GoodsModel::whereIn('code', $batchCode)->column('code');
|
||||
|
||||
foreach ($dataAll as $code => $item) {
|
||||
if (!in_array($code, $exists)) {
|
||||
$needInsertGoods[$code] = [
|
||||
'code' => $item['code'],
|
||||
'name' => $item['name'],
|
||||
'shop_id' => $shopId,
|
||||
|
||||
'first_cate' => $item['first_cate'],
|
||||
'second_cate' => $item['second_cate'],
|
||||
'third_cate' => $item['third_cate'],
|
||||
'brand_name' => $item['brand'],
|
||||
'unit_name' => $item['unit'],
|
||||
'images' => $item['images'],
|
||||
'import_status' => 0,//导入状态 0未完成
|
||||
|
||||
'image' => $item['image'],
|
||||
'content' => $item['content'],
|
||||
'spec_type' => 1,//1 统一规格
|
||||
'max_price' => $item['price'],
|
||||
'min_price' => $item['price'],
|
||||
'stock' => $item['stock'],
|
||||
'create_time' => $now,
|
||||
'update_time' => $now,
|
||||
'custom_params' => $item['custom_params'],
|
||||
];
|
||||
}
|
||||
}
|
||||
// 批量插入商品
|
||||
GoodsModel::insertAll($needInsertGoods);
|
||||
GoodsModel::batchInsert($shopId, $dataAll, $batchCode);
|
||||
|
||||
$dataAll = [];//清空该批次
|
||||
$batchCode = [];// 清空该批次商品编码
|
||||
$needInsertGoods = [];// 清空该批次要插入的商品
|
||||
}
|
||||
|
||||
$totalCount++; // 记录已处理的行数
|
||||
}
|
||||
|
||||
// Log::write(sprintf('循环结束 row:%s total:%s', json_encode($needInsertGoods), $totalCount));
|
||||
// 最后一批次导入
|
||||
if (!empty($needInsertGoods)) {
|
||||
GoodsModel::insertAll($needInsertGoods);
|
||||
if (!empty($dataAll)) {
|
||||
// Log::write('最后一批次导入');
|
||||
GoodsModel::batchInsert($shopId, $dataAll, $batchCode);
|
||||
}
|
||||
// 以上 基础商品记录已插入
|
||||
// SKU相关完善 包含SKU、规格、规格值
|
||||
|
@ -472,7 +443,7 @@ class Goods extends AdminBase
|
|||
GoodsModel::handleCategory();//处理商品分类 插入到分类表 未更新商品表中的分类ID
|
||||
GoodsModel::handleSelfInfo();//处理商品自身数据的更新 更新分类ID、品牌ID、单位ID
|
||||
|
||||
return JsonServer::success('导入成功', $dataAll);
|
||||
return JsonServer::success('导入成功');
|
||||
}
|
||||
$shopList = Shop::where('del', 0)->field('id,name')->select();
|
||||
return view('', [
|
||||
|
|
|
@ -22,6 +22,7 @@ namespace app\common\model\goods;
|
|||
|
||||
use app\common\basics\Models;
|
||||
use app\common\model\distribution\DistributionGoods;
|
||||
use think\facade\Db;
|
||||
|
||||
|
||||
/**
|
||||
|
@ -169,6 +170,9 @@ class Goods extends Models
|
|||
'spec_value_str' => '默认',
|
||||
'price' => $item['min_price'],
|
||||
'stock' => $item['stock'],
|
||||
'volume' => 0,
|
||||
'weight' => 0,
|
||||
'bar_code' => 0,
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -235,8 +239,11 @@ class Goods extends Models
|
|||
$insert = [];
|
||||
$now = time();
|
||||
foreach ($needHandleList as $item) {
|
||||
if (!in_array($item['brand_name'], $allBrandName)) {
|
||||
$insert[] = ['name' =>$item['brand_name'], 'create_time'=> $now];
|
||||
if (empty($item)) {
|
||||
continue;
|
||||
}
|
||||
if (!in_array($item, $allBrandName)) {
|
||||
$insert[] = ['name' =>$item, 'create_time'=> $now, 'image' => '', 'initial' => ''];
|
||||
}
|
||||
}
|
||||
GoodsBrand::limit(2000)->insertAll($insert);
|
||||
|
@ -252,8 +259,11 @@ class Goods extends Models
|
|||
$insert = [];
|
||||
$now = time();
|
||||
foreach ($needHandleList as $item) {
|
||||
if (!in_array($item['unit_name'], $allName)) {
|
||||
$insert[] = ['name' =>$item['unit_name'], 'create_time'=> $now];
|
||||
if (empty($item)) {
|
||||
continue;
|
||||
}
|
||||
if (!in_array($item, $allName)) {
|
||||
$insert[] = ['name' =>$item, 'create_time'=> $now];
|
||||
}
|
||||
}
|
||||
GoodsUnit::limit(2000)->insertAll($insert);
|
||||
|
@ -273,18 +283,27 @@ class Goods extends Models
|
|||
|
||||
|
||||
foreach ($needHandleList1 as $item) {
|
||||
if (!in_array($item['first_cate'], $allName)) {
|
||||
$insert[] = ['name' =>$item['first_cate'], 'pid' => 0, 'level' => 1,'create_time'=> $now, 'parent_name' => null];
|
||||
if (empty($item)) {
|
||||
continue;
|
||||
}
|
||||
if (!in_array($item, $allName)) {
|
||||
$insert[] = ['name' =>$item, 'pid' => 0, 'level' => 1,'create_time'=> $now, 'parent_name' => null];
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($needHandleList2 as $item) {
|
||||
if (empty($item)) {
|
||||
continue;
|
||||
}
|
||||
if (!in_array($item['second_cate'], $allName)) {
|
||||
$insert[] = ['name' =>$item['first_cate'], 'pid' => 0, 'level' => 2,'create_time'=> $now, 'parent_name' => $item['first_cate']];
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($needHandleList3 as $item) {
|
||||
if (empty($item)) {
|
||||
continue;
|
||||
}
|
||||
if (!in_array($item['third_cate'], $allName)) {
|
||||
$insert[] = ['name' =>$item['third_cate'], 'pid' => 0, 'level' => 3,'create_time'=> $now, 'parent_name' => $item['second_cate']];
|
||||
}
|
||||
|
@ -308,32 +327,72 @@ class Goods extends Models
|
|||
->join('goods_category c', 'main.first_cate = c.name')
|
||||
->where('main.first_cate_id', 0)
|
||||
->whereNotNull('main.first_cate')
|
||||
->update(['main.first_cate_id' => 'c.id']);
|
||||
->update(['main.first_cate_id' => Db::raw('`c`.id')]);
|
||||
// 更新二级分类ID 二级分类ID=0但分类名不为空的
|
||||
self::alias('main')
|
||||
->join('goods_category c', 'main.first_cate = c.name')
|
||||
->join('goods_category c', 'main.second_cate = c.name')
|
||||
->where('main.second_cate_id', 0)
|
||||
->whereNotNull('main.second_cate')
|
||||
->update(['main.second_cate_id' => 'c.id']);
|
||||
->update(['main.second_cate_id' => Db::raw('`c`.id')]);
|
||||
// 更新三级分类ID 三级分类ID=0但分类名不为空的
|
||||
self::alias('main')
|
||||
->join('goods_category c', 'main.first_cate = c.name')
|
||||
->join('goods_category c', 'main.third_cate = c.name')
|
||||
->where('main.third_cate_id', 0)
|
||||
->whereNotNull('main.third_cate')
|
||||
->update(['main.third_cate_id' => 'c.id']);
|
||||
->update(['main.third_cate_id' => Db::raw('`c`.id')]);
|
||||
|
||||
// 更新品牌ID 品牌ID=0但品牌名不为空的
|
||||
self::alias('main')
|
||||
->join('goods_brand b', 'main.brand_name = c.name')
|
||||
->join('goods_brand c', 'main.brand_name = c.name')
|
||||
->where('main.brand_id', 0)
|
||||
->whereNotNull('main.brand_name')
|
||||
->update(['main.brand_id' => 'c.id']);
|
||||
->update(['main.brand_id' => Db::raw('`c`.id')]);
|
||||
|
||||
// 更新单位ID 单位ID=0但单位名不为空的
|
||||
self::alias('main')
|
||||
->join('goods_unit b', 'main.unit_name = c.name')
|
||||
->join('goods_unit c', 'main.unit_name = c.name')
|
||||
->where('main.unit_id', 0)
|
||||
->whereNotNull('main.unit_name')
|
||||
->update(['main.unit_id' => 'c.id']);
|
||||
->update(['main.unit_id' => Db::raw('`c`.id')]);
|
||||
}
|
||||
|
||||
public static function batchInsert(int $shopId, array $dataAll, array $batchCode)
|
||||
{
|
||||
// 已存在的商品编码数组
|
||||
$exists = self::whereIn('code', $batchCode)->column('code');
|
||||
|
||||
$now = time();
|
||||
$needInsertGoods = [];
|
||||
foreach ($dataAll as $code => $item) {
|
||||
if (!in_array($code, $exists)) {
|
||||
$needInsertGoods[$code] = [
|
||||
'code' => $item['code'],
|
||||
'name' => $item['name'],
|
||||
'shop_id' => $shopId,
|
||||
|
||||
'first_cate' => $item['first_cate'],
|
||||
'second_cate' => $item['second_cate'],
|
||||
'third_cate' => $item['third_cate'],
|
||||
'brand_name' => $item['brand'],
|
||||
'unit_name' => $item['unit'],
|
||||
'images' => $item['images'],
|
||||
'import_status' => 0,//导入状态 0未完成
|
||||
|
||||
'express_money' =>0,
|
||||
|
||||
'image' => $item['image'],
|
||||
'content' => $item['content'],
|
||||
'spec_type' => 1,//1 统一规格
|
||||
'max_price' => $item['price'],
|
||||
'min_price' => $item['price'],
|
||||
'stock' => $item['stock'],
|
||||
'create_time' => $now,
|
||||
'update_time' => $now,
|
||||
'custom_params' => $item['custom_params'],
|
||||
];
|
||||
}
|
||||
}
|
||||
// 批量插入商品
|
||||
self::insertAll($needInsertGoods);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue