调整排序

This commit is contained in:
mkm 2023-11-25 14:33:05 +08:00
parent cf42b7e1d6
commit cab9448700
3 changed files with 268 additions and 227 deletions

View File

@ -2521,196 +2521,222 @@ class StoreOrderRepository extends BaseRepository
if ($typeCode == Merchant::TypeCode['TypeSupplyChain']) {
$product_type = 98; //供应链
}
foreach ($data as $datum) {
try {
foreach ($data as $datum) {
$find = Db::name('store_product')->where('mer_id', $merId)->where('store_name', $datum['value']['store_name'])->find();
$attr_one = explode('|', $datum['value']['attr_one']);
$attr_two = explode('|', $datum['value']['attr_two']);
$attr = [];
$arr = [];
foreach ($attr_one as $key => $value) {
$attr[$value] = $attr_two[$key];
$arr[] = [
'value' => $value,
'detail' => [$attr_two[$key] ?? []],
'inputVisible' => false
];
}
$array_values = array_values($attr);
if ($find) {
$store_product_attr_value = Db::name('store_product_attr_value')->where('product_id', $find['product_id'])->where('bar_code', $datum['value']['bar_code'])->find();
if ($store_product_attr_value) {
Log::error('xls导入商品:已经导入过该规格了');
continue;
$find = Db::name('store_product')->where('mer_id', $merId)->where('store_name', $datum['value']['store_name'])->find();
$attr_one = explode('|', $datum['value']['attr_one']);
$attr_two = explode('|', $datum['value']['attr_two']);
$attr = [];
$arr = [];
foreach ($attr_one as $key => $value) {
$attr[$value] = $attr_two[$key];
$arr[] = [
'value' => $value,
'detail' => [$attr_two[$key] ?? []],
'inputVisible' => false
];
}
$is_update = false;
foreach ($attr as $kk => $vv) {
$array_values = array_values($attr);
if ($find) {
$store_product_attr_value = Db::name('store_product_attr_value')->where('product_id', $find['product_id'])->where('bar_code', $datum['value']['bar_code'])->find();
if ($store_product_attr_value) {
$data['product_id'] = $find['product_id'];
$data['mer_id'] = $merId;
$data['store_name'] = $$datum['value']['store_name'];
$data['content'] = 'xls导入商品:已经导入过该规格了';
$this->create_product_import_log($data,0);
continue;
}
$is_update = false;
foreach ($attr as $kk => $vv) {
/**查询当前规格是否存在 */
$attr_values_find = Db::name('store_product_attr')->where('product_id', $find['product_id'])->where('attr_name', $kk)
->find();
if ($attr_values_find) {
$attr_values = $attr_values_find['attr_values'];
$attr_values = explode('-!-', $attr_values);
if (!in_array($vv, $attr_values)) {
$attr_values[] = $vv;
$attr_values = implode('-!-', $attr_values);
Db::name('store_product_attr')->where('product_id', $find['product_id'])->where('attr_name', $kk)->update(['attr_values' => $attr_values]);
$this->create_product_attr_value($find, $datum, $product_type, $attr, $merId, implode(',', $array_values));
/**查询当前规格是否存在 */
$attr_values_find = Db::name('store_product_attr')->where('product_id', $find['product_id'])->where('attr_name', $kk)
->find();
if ($attr_values_find) {
$attr_values = $attr_values_find['attr_values'];
$attr_values = explode('-!-', $attr_values);
if (!in_array($vv, $attr_values)) {
$attr_values[] = $vv;
$attr_values = implode('-!-', $attr_values);
Db::name('store_product_attr')->where('product_id', $find['product_id'])->where('attr_name', $kk)->update(['attr_values' => $attr_values]);
$this->create_product_attr_value($find, $datum, $product_type, $attr, $merId, implode(',', $array_values));
}
$is_update = true;
} else {
$datas = [
'product_id' => $find['product_id'],
'attr_name' => $kk, 'attr_values' => $vv,
];
Db::name('store_product_attr')->insert($datas);
}
$is_update = true;
} else {
$datas = [
'product_id' => $find['product_id'],
'attr_name' => $kk, 'attr_values' => $vv,
];
Db::name('store_product_attr')->insert($datas);
}
}
if ($is_update == false) {
$this->create_product_attr_value($find, $datum, $product_type, $attr, $merId, implode(',', $array_values));
}
} else {
//默认其他分类
$cate_id = 1726;
$spec_type = 1;
$detail = $attr;
if ($is_update == false) {
$this->create_product_attr_value($find, $datum, $product_type, $attr, $merId, implode(',', $array_values));
}
} else {
//默认其他分类
$cate_id = 1726;
$spec_type = 1;
$detail = $attr;
/**查询平台分类 */
$cate_id_one = Db::name('store_category')->where('mer_id', 0)->where('cate_name', $datum['value']['cate_id_one'])->value('store_category_id');
if ($cate_id_one) {
$cate_id_two = Db::name('store_category')->where('mer_id', 0)->where('cate_name', $datum['value']['cate_id_two'])->value('store_category_id');
if ($cate_id_two) {
$cate_id = $cate_id_two;
/**查询平台分类 */
$cate_id_one = Db::name('store_category')->where('mer_id', 0)->where('cate_name', $datum['value']['cate_id_one'])->value('store_category_id');
if ($cate_id_one) {
$cate_id_two = Db::name('store_category')->where('mer_id', 0)->where('cate_name', $datum['value']['cate_id_two'])->value('store_category_id');
if ($cate_id_two) {
$cate_id = $cate_id_two;
}
}
}
/**查询商户分类 */
$mer_cate_id=0;
if( $datum['value']['cate_id_one_mer'] !=''&& $datum['value']['cate_id_one_two']!=''){
$cate_id_one_mer = Db::name('store_category')->where('level',0)->where('mer_id', $merId)->where('cate_name', $datum['value']['cate_id_one_mer'])->value('store_category_id');
/**有一级分类 */
if($cate_id_one_mer){
$cate_id_one_two = Db::name('store_category')->where('pid',$cate_id_one_mer)->where('level',1)->where('mer_id', $merId)->where('cate_name', $datum['value']['cate_id_one_two'])->value('store_category_id');
/**有二级分类 */
if($cate_id_one_two){
$mer_cate_id =$cate_id_one_two;
}else{
$mer_cate = ['level'=>1,'pid' => $cate_id_one_mer, 'cate_name' => $datum['value']['cate_id_one_mer'], 'path' => '/' . $cate_id_one_mer . '/', 'mer_id' => $merId, 'sort' => 0, 'is_show' => 1, 'create_time' => date('Y-m-d H:i:s')];
/**查询商户分类 */
$mer_cate_id = 0;
if ($datum['value']['cate_id_one_mer'] != '' && $datum['value']['cate_id_one_two'] != '') {
$cate_id_one_mer = Db::name('store_category')->where('level', 0)->where('mer_id', $merId)->where('cate_name', $datum['value']['cate_id_one_mer'])->value('store_category_id');
/**有一级分类 */
if ($cate_id_one_mer) {
$cate_id_one_two = Db::name('store_category')->where('pid', $cate_id_one_mer)->where('level', 1)->where('mer_id', $merId)->where('cate_name', $datum['value']['cate_id_one_two'])->value('store_category_id');
/**有二级分类 */
if ($cate_id_one_two) {
$mer_cate_id = $cate_id_one_two;
} else {
$mer_cate = ['level' => 1, 'pid' => $cate_id_one_mer, 'cate_name' => $datum['value']['cate_id_one_mer'], 'path' => '/' . $cate_id_one_mer . '/', 'mer_id' => $merId, 'sort' => 0, 'is_show' => 1, 'create_time' => date('Y-m-d H:i:s')];
$mer_cate_id = Db::name('store_category')->insertGetId($mer_cate);
}
} else {
$cate_id_one_mer_arr = ['pid' => 0, 'cate_name' => $datum['value']['cate_id_one_mer'], 'path' => '/', 'mer_id' => $merId, 'sort' => 0, 'is_show' => 1, 'create_time' => date('Y-m-d H:i:s')];
$cate_id_one_mer = Db::name('store_category')->insertGetId($cate_id_one_mer_arr);
$mer_cate = ['level' => 1, 'pid' => $cate_id_one_mer, 'cate_name' => $datum['value']['cate_id_one_mer'], 'path' => '/' . $cate_id_one_mer . '/', 'mer_id' => $merId, 'sort' => 0, 'is_show' => 1, 'create_time' => date('Y-m-d H:i:s')];
$mer_cate_id = Db::name('store_category')->insertGetId($mer_cate);
}
}else{
$cate_id_one_mer_arr = ['pid' => 0, 'cate_name' => $datum['value']['cate_id_one_mer'], 'path' => '/', 'mer_id' => $merId, 'sort' => 0, 'is_show' => 1, 'create_time' => date('Y-m-d H:i:s')];
$cate_id_one_mer = Db::name('store_category')->insertGetId($cate_id_one_mer_arr);
$mer_cate = ['level'=>1,'pid' => $cate_id_one_mer, 'cate_name' => $datum['value']['cate_id_one_mer'], 'path' => '/' . $cate_id_one_mer . '/', 'mer_id' => $merId, 'sort' => 0, 'is_show' => 1, 'create_time' => date('Y-m-d H:i:s')];
$mer_cate_id = Db::name('store_category')->insertGetId($mer_cate);
}
}
$bar_code = (int) $datum['value']['bar_code'];
$bar_code_where = ['is_used' => 1, 'status' => 1, 'is_del' => 0];
$bar_code_field = 'id,store_name,bar_code,manu_address,price,stock,image,slider_image,spec,trademark,manu_name,manu_address,note';
$library=null;
if ($bar_code) {
$library = Db::name('product_library')->where('bar_code', $bar_code)->where($bar_code_where)->field($bar_code_field)
->withAttr('slider_image', function ($value, $data) {
return $value ? explode(',', $value) : [];
})
->find();
if (!$library) {
$ProductLibrary = app()->make(ProductLibrary::class);
try {
$bar_code = (int) $datum['value']['bar_code'];
$bar_code_where = ['is_used' => 1, 'status' => 1, 'is_del' => 0];
$bar_code_field = 'id,store_name,bar_code,manu_address,price,stock,image,slider_image,spec,trademark,manu_name,manu_address,note';
$library = null;
if ($bar_code) {
$library = Db::name('product_library')->where('bar_code', $bar_code)->where($bar_code_where)->field($bar_code_field)
->withAttr('slider_image', function ($value, $data) {
return $value ? explode(',', $value) : [];
})
->find();
if (!$library) {
$ProductLibrary = app()->make(ProductLibrary::class);
$ProductLibrary->caiji($bar_code);
$library = Db::name('product_library')->where('bar_code', $bar_code)->where($bar_code_where)->field($bar_code_field)
->withAttr('slider_image', function ($value, $data) {
return $value ? explode(',', $value) : [];
})
->find();
} catch (Exception $e) {
Log::error('导入采集编码错误:' . $bar_code . '。' . $e->getMessage());
}
}
}
if($library){
$image=$library['image'];
$slider_image=$library['slider_image'];
}else{
$image='https://lihai001.oss-cn-chengdu.aliyuncs.com/public/kk/luzhou/static4/oa_app/23565656.png';
$slider_image=[
0 => "https://lihai001.oss-cn-chengdu.aliyuncs.com/public/kk/luzhou/static4/oa_app/23565656.png",
];
}
$datas = [
"image" =>$image,
"slider_image" =>$slider_image,
"store_name" => $datum['value']['store_name'],
"store_info" => $datum['value']['store_name'],
"keyword" => '',
"bar_code" => $bar_code,
"guarantee_template_id" => "",
"cate_id" => $cate_id,
"mer_cate_id" => [
0 => $mer_cate_id
],
'product_type' => $product_type,
"unit_name" => $datum['value']['unit_name'],
"sort" => 0,
"is_show" => "",
"is_good" => 0,
"is_gift_bag" => 0,
"integral_rate" => -1,
"video_link" => "",
"temp_id" => "",
"content" => "",
"spec_type" => $spec_type,
"extension_type" => 0,
"attr" => $arr,
"mer_labels" => [],
"delivery_way" => [
0 => "2",
1 => "1"
],
"delivery_free" => 0,
"param_temp_id" => [],
"extend" => [],
"brand_id" => "",
"once_max_count" => 0,
"once_min_count" => 0,
"pay_limit" => 0,
"attrValue" => [
[
"image" => $image,
"price" => $datum['value']['price'],
"cost" => $datum['value']['cost'],
"ot_price" => $datum['value']['price'],
"svip_price" => null,
"procure_price" => $datum['value']['procure_price']??0,
"stock" => 1,
"bar_code" => (int)$datum['value']['bar_code'],
"weight" => 0,
"volume" => 0,
"detail" => $detail,
if ($library) {
$image = $library['image'];
$slider_image = $library['slider_image'];
} else {
$image = 'https://lihai001.oss-cn-chengdu.aliyuncs.com/public/kk/luzhou/static4/oa_app/23565656.png';
$slider_image = [
0 => "https://lihai001.oss-cn-chengdu.aliyuncs.com/public/kk/luzhou/static4/oa_app/23565656.png",
];
}
$datas = [
"image" => $image,
"slider_image" => $slider_image,
"store_name" => $datum['value']['store_name'],
"store_info" => $datum['value']['store_name'],
"keyword" => '',
"bar_code" => $bar_code,
"guarantee_template_id" => "",
"cate_id" => $cate_id,
"mer_cate_id" => [
0 => $mer_cate_id
],
],
"give_coupon_ids" => [],
"type" => 0,
"svip_price" => 0,
"svip_price_type" => 0,
"params" => [],
"mer_id" => $merId,
"status" => 1,
"mer_status" => 1,
"rate" => 3,
];
// $data_list = [
// 'data' => $datas,
// 'product_type' => $product_type,
// ];
$make = app()->make(ProductRepository::class);
$make->create($datas, $product_type);
// Queue::push(ProductImportJob::class, $data_list);
'product_type' => $product_type,
"unit_name" => $datum['value']['unit_name'],
"sort" => 0,
"is_show" => "",
"is_good" => 0,
"is_gift_bag" => 0,
"integral_rate" => -1,
"video_link" => "",
"temp_id" => "",
"content" => "",
"spec_type" => $spec_type,
"extension_type" => 0,
"attr" => $arr,
"mer_labels" => [],
"delivery_way" => [
0 => "2",
1 => "1"
],
"delivery_free" => 0,
"param_temp_id" => [],
"extend" => [],
"brand_id" => "",
"once_max_count" => 0,
"once_min_count" => 0,
"pay_limit" => 0,
"attrValue" => [
[
"image" => $image,
"price" => $datum['value']['price'],
"cost" => $datum['value']['cost'],
"ot_price" => $datum['value']['price'],
"svip_price" => null,
"procure_price" => $datum['value']['procure_price'] ?? 0,
"stock" => 1,
"bar_code" => (int)$datum['value']['bar_code'],
"weight" => 0,
"volume" => 0,
"detail" => $detail,
],
],
"give_coupon_ids" => [],
"type" => 0,
"svip_price" => 0,
"svip_price_type" => 0,
"params" => [],
"mer_id" => $merId,
"status" => 1,
"mer_status" => 1,
"rate" => 3,
];
// $data_list = [
// 'data' => $datas,
// 'product_type' => $product_type,
// ];
$make = app()->make(ProductRepository::class);
$product_id = $make->create($datas, $product_type);
if ($product_id) {
$data['product_id'] = $product_id;
$data['mer_id'] = $merId;
$data['store_name'] = $datum['value']['store_name'];
$this->create_product_import_log($data);
}
// Queue::push(ProductImportJob::class, $data_list);
}
}
} catch (Exception $e) {
$data['product_id'] = $product_id;
$data['mer_id'] = $merId;
$data['store_name'] = $$datum['value']['store_name'];
$data['content'] = $datum['value']['store_name'];
$this->create_product_import_log($data, 0);
}
return true;
}
public function create_product_import_log($data, $status = 1)
{
$data = [
'product_id' => $data['product_id'],
'mer_id' => $data['mer_id'],
'name' => $data['store_name'],
'content' => $data['content'] ?? '',
'status' => $status,
'create_time' => date('Y-m-d H:i:s'),
];
Db::name('store_product_import')->insert($data);
}
public function create_product_attr_value($find, $datum, $product_type, $attr_values, $merId, $sku)
{

View File

@ -184,6 +184,7 @@ class StoreImport extends BaseController
Queue::push(ImportPicJob::class,['mer_id'=>$mer_id,'path'=>$destination_path]);
return app('json')->success('开始导入数据,请稍后在列表中查看!,如果未导入请检查格式');
}
public function getXlsList($type_id,$path){
if($type_id==12){
$check =[

View File

@ -13,7 +13,7 @@ class ImportPicJob implements JobInterface
{
public function fire($job, $data)
{
Log::error('开始导入商品图片:'.$job->attempts());
Log::error('开始导入商品图片:' . $job->attempts());
if ($job->attempts() > 3) {
$job->delete();
$this->failed($data);
@ -23,61 +23,59 @@ class ImportPicJob implements JobInterface
$files = scandir($directory);
$dir = 'def/' . date('Y-m-d');
$upload = UploadService::create();
/**循环目录 */
foreach ($files as $file) {
if ($file === '.' || $file === '..' ||$file ==='__MACOSX') {
continue;
}
if(!is_dir($directory . '/' . $file)){
continue;
}
$files_two = scandir($directory . '/' . $file);
$image_extensions = array('jpg', 'jpeg', 'png');
$image = '';
$slider_image = [];
$details = [];
$sku_arr = [];
/**清洗图片 */
foreach ($files_two as $file_two) {
if ($file_two === '.' || $file_two === '..' ||$file_two ==='__MACOSX') {
try {
/**循环目录 */
foreach ($files as $file) {
if ($file === '.' || $file === '..' || $file === '__MACOSX') {
continue;
}
$arr = explode('.', $file_two);
if (in_array($arr[1], $image_extensions)) {
/**首图 */
$images[] = $file_two;
if ($image == '' && is_numeric($arr[0])) {
$image = $directory . '/' . $file . '/' . $file_two;
continue;
}
/**轮播图 */
if (is_numeric($arr[0]) && count($slider_image) < 4) {
$slider_image[] = $directory . '/' . $file . '/' . $file_two;
continue;
}
/**详情图 */
if (is_numeric($arr[0])) {
$details[] = $directory . '/' . $file_two;
continue;
}
/**sku图 */
$sku = explode('==', $arr[0]);
if ($sku) {
$sku = implode(',', $sku);
$sku_arr[$sku] = $directory . '/' . $file . '/' . $file_two;
if (!is_dir($directory . '/' . $file)) {
continue;
}
$files_two = scandir($directory . '/' . $file);
$image_extensions = array('jpg', 'jpeg', 'png');
$image = '';
$slider_image = [];
$details = [];
$sku_arr = [];
/**清洗图片 */
foreach ($files_two as $file_two) {
if ($file_two === '.' || $file_two === '..' || $file_two === '__MACOSX') {
continue;
}
$arr = explode('.', $file_two);
if (in_array($arr[1], $image_extensions)) {
/**首图 */
$images[] = $file_two;
if ($image == '' && is_numeric($arr[0])) {
$image = $directory . '/' . $file . '/' . $file_two;
continue;
}
/**轮播图 */
if (is_numeric($arr[0]) && count($slider_image) < 4) {
$slider_image[] = $directory . '/' . $file . '/' . $file_two;
continue;
}
/**详情图 */
if (is_numeric($arr[0])) {
$details[] = $directory . '/' . $file_two;
continue;
}
/**sku图 */
$sku = explode('==', $arr[0]);
if ($sku) {
$sku = implode(',', $sku);
$sku_arr[$sku] = $directory . '/' . $file . '/' . $file_two;
}
}
}
}
$where = ['mer_id' => $mer_id, 'is_del' => 0];
$update = [];
$update_content['title'] = '';
$update_content['image'] = [];
$find = Db::name('store_product')->where($where)->where('store_name',$file)->find();
if ($find) {
try {
$where = ['mer_id' => $mer_id, 'is_del' => 0];
$update = [];
$update_content['title'] = '';
$update_content['image'] = [];
$find = Db::name('store_product')->where($where)->where('store_name', $file)->find();
if ($find) {
/**更新商品图片 */
$image = $upload->to($dir)->stream(file_get_contents($image));
$update['image'] = $image->filePath;
@ -85,7 +83,7 @@ class ImportPicJob implements JobInterface
$oss = $upload->to($dir)->stream(file_get_contents($v));
$update['slider_image'][] = $oss->filePath;
}
if(isset($update['slider_image'])){
if (isset($update['slider_image'])) {
$update['slider_image'] = implode(',', $update['slider_image']);
}
Db::name('store_product')->where('product_id', $find['product_id'])->update($update);
@ -106,29 +104,45 @@ class ImportPicJob implements JobInterface
$update_content['image'][] = $oss->filePath;
}
if ($store_product_content) {
if(isset($update_content['image']) && !empty($update_content['image'])){
if (isset($update_content['image']) && !empty($update_content['image'])) {
Db::name('store_product_content')
->where(['product_id' => $find['product_id']])
->update(['content' => json_encode($update_content)]);
->where(['product_id' => $find['product_id']])
->update(['content' => json_encode($update_content)]);
}
} else {
$update_content['product_id'] = $find['product_id'];
Db::name('store_product_content')
->insert(['product_id'=>$find['product_id'],'type'=>1,'content'=>json_encode($update_content)]);
->insert(['product_id' => $find['product_id'], 'type' => 1, 'content' => json_encode($update_content)]);
}
} catch (Exception $e) {
halt($e->getMessage(), $e->getLine());
}
}
} catch (Exception $e) {
$data['product_id'] = $find['product_id'];
$data['mer_id'] = $data['mer_id'];
$data['store_name'] = $find['store_name'];
$data['content'] = $e->getMessage();
$this->create_product_import_log($data, 0);
}
$job->delete();
}
public function failed($data)
{
Log::error('导入商品图片失败:'.json_encode($data));
Log::error('导入商品图片失败:' . json_encode($data));
// TODO: Implement failed() method.
}
public function create_product_import_log($data, $status = 1)
{
$data = [
'product_id' => $data['product_id'],
'mer_id' => $data['mer_id'],
'name' => $data['store_name'],
'content' => $data['content'] ?? '',
'status' => $status,
'create_time' => date('Y-m-d H:i:s'),
];
Db::name('store_product_import')->insert($data);
}
}