zhanglinxin 1 年之前
父节点
当前提交
d19e161513

+ 0 - 2
application/api/controller/Goodscategory.php

@@ -27,12 +27,10 @@ class Goodscategory extends Base
      * @Apidoc\Returned("id", type="integer", desc="分类ID")
      * @Apidoc\Returned("pid", type="integer", desc="父ID")
      * @Apidoc\Returned("name", type="string", desc="分类名称")
-     * @Apidoc\Returned("weigh", type="integer", desc="权重")
      * @Apidoc\Returned("childlist", type="array", desc="子分类列表",
      *     @Apidoc\Returned("id", type="integer", desc="分类ID"),
      *     @Apidoc\Returned("pid", type="integer", desc="父ID"),
      *     @Apidoc\Returned("name", type="string", desc="分类名称"),
-     *     @Apidoc\Returned("weigh", type="integer", desc="权重")
      * )
      */
     public function get_list()

+ 63 - 44
application/common/service/ApproveInfoService.php

@@ -297,8 +297,8 @@ class ApproveInfoService
         }
 
         $goods_list_new = [];
-        $category_first_data = []; // 导入的一级分类数组
-        $category_data = []; // 导入的二级分类数组
+        $category_first_data = [];
+        $category_data = [];
         $goods_ids = [];
         $goods_ids_new = [];
         $data = [];
@@ -322,14 +322,14 @@ class ApproveInfoService
         foreach ($params as $value) {
             if ($value['flag'] == 2) {
                 // 批量导入
-                $category_first_data[$value['goods_category_first']] = $value['goods_category_first'];
-                $category_data[$value['goods_category_id']] = $value['goods_category_id'];
+                $category_first_data[$value['goods_category_first']][$value['goods_category_id']] = $value['goods_category_id'];
             }
             if ($value['flag'] == 3) {
                 // 商品库选择
                 $goods_ids[$value['goods_id']] = $value;
             }
         }
+
         // 商品库选择
         if ($goods_ids) {
             $goods_list = Goods::field('status,is_deleted,create_at', true)
@@ -403,17 +403,46 @@ class ApproveInfoService
 
         // 批量导入
         if ($category_first_data) {
-            $category_first_list = GoodsCategory::where('name', 'in', $category_first_data)
-                ->where('pid', 0)
-                ->where('is_deleted', CommonConstant::IS_DELETED_0)
-                ->column('id,name');
-            $category_list = GoodsCategory::where('name', 'in', $category_data)
-                ->where('pid', 'gt', 0)
-                ->where('is_deleted', CommonConstant::IS_DELETED_0)
-                ->column('id,name');
-            $category_first_list_flip = array_flip($category_first_list); // 交换数组中的键和值
-            $category_list_flip = array_flip($category_list);
+            $category_first_list = GoodsCategoryService::get_list([['name', 'in', array_keys($category_first_data)]]);
+            $category_first_object = $category_first_list ? array_column($category_first_list->toArray(), null, 'name') : [];
+            foreach ($category_first_data as $key=>$value){
+                if(array_key_exists($key,$category_first_object)){
+                    // 一级里有该商品分类
+                    $category_first_info = $category_first_object[$key];
+                    $goods_category_first_id = $category_first_info['id'];
+                    $category_second_object = $category_first_info['childlist'] ? array_column($category_first_info['childlist'], null, 'name') : [];
+                } else{
+                    // 一级里没有该商品分类 创建
+                    $goods_category_first = GoodsCategory::create(['name'=>$key]);
+                    $goods_category_first_id = $goods_category_first->id;
+                    $category_second_object = [];
+                }
+
+                $childlist = [];
+                foreach ($value as $kk=>$vv){
+                    if(array_key_exists($kk,$category_second_object)){
+                        // 二级里有该商品分类
+                        $category_second_info = $category_second_object[$kk];
+                        $goods_category_id = $category_second_info['id'];
+                    } else {
+                        // 二级里没有该商品分类 创建
+                        $goods_category = GoodsCategory::create(['pid'=>$goods_category_first_id,'name'=>$kk]);
+                        $goods_category_id = $goods_category->id;
+                    }
+                    $childlist[] = [
+                        'id' => $goods_category_id,
+                        'name' => $kk,
+                    ];
+                }
+                $category_data[] = [
+                    'id'=>$goods_category_first_id,
+                    'name'=>$key,
+                    'childlist'=>$childlist
+                ];
+            }
         }
+        $category_object = $category_data ? array_column($category_data, null, 'name') : [];
+
         foreach ($params as $key=>$value) {
             if ($value['flag'] == 1) {
                 // 添加新商品
@@ -433,37 +462,27 @@ class ApproveInfoService
             }
             if ($value['flag'] == 2) {
                 // 批量导入
-                if(!array_key_exists($value['goods_category_first'],$category_first_list_flip)) {
-                    // 一级里没有该商品分类 创建
-                    $goods_category_first = GoodsCategory::create(['name'=>$value['goods_category_first']]);
-                    $goods_category_first_id = $goods_category_first->id;
-                } else{
-                    // 一级里有该商品分类
-                    $goods_category_first_id = $category_first_list_flip[$value['goods_category_first']];
-                }
-                if(!array_key_exists($value['goods_category_id'], $category_list_flip)){
-                    // 二级里没有该商品分类 创建
-                    $goods_category = GoodsCategory::create(['pid'=>$goods_category_first_id,'name'=>$value['goods_category_id']]);
-                    $goods_category_id = $goods_category->id;
-                } else{
-                    // 二级里有该商品分类
-                    $goods_category_id = $category_list_flip[$value['goods_category_id']];
-                }
+                if(array_key_exists($value['goods_category_first'],$category_object)) {
+                    $category_info = $category_object[$value['goods_category_first']];
+                    $goods_category_first_id = $category_info['id'];
+                    $category_info_object = array_column($category_info['childlist'], null, 'name');
+                    $goods_category_id = $category_info_object[$value['goods_category_id']];
 
-                $goods_ids_new[$key] = $value;
-                $goods_ids_new[$key]['goods_category_first'] = $goods_category_first_id;
-                $goods_ids_new[$key]['goods_category_id'] = $goods_category_id;
-                $goods_info = Goods::field('status,is_deleted,create_at', true)
-                    ->where('goods_category_first', $goods_category_first_id)
-                    ->where('goods_category_id', $goods_category_id)
-                    ->where('goods_name', $value['goods_name'])
-                    ->where('is_deleted', CommonConstant::IS_DELETED_0)
-                    ->with([
-                        'goodsStock',
-                    ])
-                    ->find();
-                if ($goods_info) {
-                    $goods_list_new[] = $goods_info->toArray();
+                    $goods_ids_new[$key] = $value;
+                    $goods_ids_new[$key]['goods_category_first'] = $goods_category_first_id;
+                    $goods_ids_new[$key]['goods_category_id'] = $goods_category_id;
+                    $goods_info = Goods::field('status,is_deleted,create_at', true)
+                        ->where('goods_category_first', $goods_category_first_id)
+                        ->where('goods_category_id', $goods_category_id)
+                        ->where('goods_name', $value['goods_name'])
+                        ->where('is_deleted', CommonConstant::IS_DELETED_0)
+                        ->with([
+                            'goodsStock',
+                        ])
+                        ->find();
+                    if ($goods_info) {
+                        $goods_list_new[] = $goods_info->toArray();
+                    }
                 }
             }
         }

+ 7 - 3
application/common/service/GoodsCategoryService.php

@@ -13,13 +13,17 @@ class GoodsCategoryService
 
     /**
      * 商品分类列表
+     *
+     * @param array $where
      **/
-    public static function get_list()
+    public static function get_list($where = [])
     {
-        $field = 'id,pid,name,weigh';
+
+        $field = 'id,pid,name';
         $list = GoodsCategory::field($field)
-            ->where('pid', 0)
             ->where('is_deleted', CommonConstant::IS_DELETED_0)
+            ->where('pid', 0)
+            ->where($where)
             ->with([
                 'childlist' => function ($query) use ($field) {
                     $query->field($field);