From 22a0f5ebcd72e46f6684797db8f4b7087172a006 Mon Sep 17 00:00:00 2001 From: lqmac Date: Mon, 13 May 2024 15:10:06 +0800 Subject: [PATCH] 1 --- app/admin/controller/Goods.php | 3 +- app/job/service/goods/AdminImport.php | 4 ++ app/job/service/goods/GoodsStoreImport.php | 9 ++- app/store/model/Goods.php | 79 ++++++++++++++++++++++ 4 files changed, 91 insertions(+), 4 deletions(-) diff --git a/app/admin/controller/Goods.php b/app/admin/controller/Goods.php index 947c6001..aa057294 100644 --- a/app/admin/controller/Goods.php +++ b/app/admin/controller/Goods.php @@ -144,7 +144,7 @@ class Goods extends Controller return $this->renderSuccess('更新成功'); } $goods_sku = GoodsModel::where('goods_id', $goodsId)->where('store_id', 0)->find(); - + $goods_data = [ 'goods_name' => $goods_sku->name, 'content' => $goods_sku->content, @@ -228,6 +228,7 @@ class Goods extends Controller $model = new GoodsModel; foreach ($goodsIds as $key => $goodsId) { + $categoryIds = $model->dealCategory($categoryIds); \app\store\model\GoodsCategoryRel::updates($goodsId, $categoryIds); } $model->whereIn('goods_id', $goodsIds)->update(['cate_status'=>1]); diff --git a/app/job/service/goods/AdminImport.php b/app/job/service/goods/AdminImport.php index 41e674c6..a4c769bc 100644 --- a/app/job/service/goods/AdminImport.php +++ b/app/job/service/goods/AdminImport.php @@ -259,6 +259,10 @@ class AdminImport extends BaseService // exit(); // 过滤不存在的ID集数据 $data['categoryIds'] = CategoryModel::filterCategoryIds($data['categoryIds'], $storeId); + + //处理分类,设置最后一级一直往上查找 + $model = new \app\store\model\Goods(); + $data['categoryIds'] = $model->dealCategory($data['categoryIds']); return $data; } diff --git a/app/job/service/goods/GoodsStoreImport.php b/app/job/service/goods/GoodsStoreImport.php index 54c205b7..7c2b922b 100644 --- a/app/job/service/goods/GoodsStoreImport.php +++ b/app/job/service/goods/GoodsStoreImport.php @@ -257,11 +257,14 @@ class GoodsStoreImport extends BaseService 'region_text' => $region_text ? json_encode(array_values($region_text)) : "", 'import' => 1,//是否是导入采集 ]; - // echo "
";
-        // print_r($data);
-        // exit();
+        
         // 过滤不存在的ID集数据
         $data['categoryIds'] = CategoryModel::filterCategoryIds($data['categoryIds'], $storeId);
+
+        //处理分类,设置最后一级一直往上查找
+        $model = new \app\store\model\Goods();
+        $data['categoryIds'] = $model->dealCategory($data['categoryIds']);
+
         return $data;
     }
 
diff --git a/app/store/model/Goods.php b/app/store/model/Goods.php
index ee053e55..c6086968 100644
--- a/app/store/model/Goods.php
+++ b/app/store/model/Goods.php
@@ -20,6 +20,7 @@ use app\store\model\GoodsImage as GoodsImageModel;
 use app\store\model\GoodsSpecRel as GoodsSpecRelModel;
 use app\store\model\goods\ServiceRel as GoodsServiceRelModel;
 use app\store\model\GoodsCategoryRel as GoodsCategoryRelModel;
+use app\store\model\Category as CategoryRelModel;
 use app\store\service\Goods as GoodsService;
 use app\common\enum\goods\SpecType as GoodsSpecTypeEnum;
 use app\common\enum\goods\Status as GoodsStatusEnum;
@@ -140,6 +141,9 @@ class Goods extends GoodsModel
 
         // 创建商品数据
         $data = $this->createData($data);
+        // echo "
";
+        // print_r($data);
+        // exit();
         // 事务处理
         $ret = $this->transaction(function () use ($data, $goodsModel) {
             
@@ -317,6 +321,81 @@ class Goods extends GoodsModel
         }
         
         $data['alone_grade_equity'] = $aloneGradeEquity;
+
+        $data['categoryIds'] = $this->dealCategory($data['categoryIds']);
+
         return $data;
     }
+
+    public function dealCategory($category){
+        $arr = [];
+        // var_dump($category);
+        foreach ($category as $key => $value) {
+            //一级
+            $cate = CategoryRelModel::where('category_id', $value)->find();
+            if (!$cate) {
+                continue;
+            }
+            $arr[] = $cate['category_id'];
+            if ($cate['parent_id'] == 0) {
+                continue;
+            }
+            //二级
+            $cate1 = CategoryRelModel::where('category_id', $cate['parent_id'])->find();
+            if (!$cate1) {
+                continue;
+            }
+            $arr[] = $cate1['category_id'];
+            if ($cate1['parent_id'] == 0) {
+                continue;
+            }
+            //三级
+            $cate2 = CategoryRelModel::where('category_id', $cate1['parent_id'])->find();
+            if (!$cate2) {
+                continue;
+            }
+            $arr[] = $cate2['category_id'];
+            if ($cate2['parent_id'] == 0) {
+                continue;
+            }
+            //四级
+            $cate3 = CategoryRelModel::where('category_id', $cate2['parent_id'])->find();
+            if (!$cate3) {
+                continue;
+            }
+            $arr[] = $cate3['category_id'];
+            if ($cate3['parent_id'] == 0) {
+                continue;
+            }
+            //五级
+            $cate4 = CategoryRelModel::where('category_id', $cate3['parent_id'])->find();
+            if (!$cate4) {
+                continue;
+            }
+            $arr[] = $cate4['category_id'];
+            if ($cate4['parent_id'] == 0) {
+                continue;
+            }
+            //五级
+            $cate5 = CategoryRelModel::where('category_id', $cate4['parent_id'])->find();
+            if (!$cate5) {
+                continue;
+            }
+            $arr[] = $cate5['category_id'];
+            if ($cate5['parent_id'] == 0) {
+                continue;
+            }
+        }
+        return array_unique($arr);
+    }
+
+
+
+
+
+
+
+
+
+
 }