diff --git a/app/api/controller/GoodsNew.php b/app/api/controller/GoodsNew.php index a157ae04..4b1414a3 100644 --- a/app/api/controller/GoodsNew.php +++ b/app/api/controller/GoodsNew.php @@ -17,7 +17,7 @@ class GoodsNew extends Controller dd($data); } - public function search() + public function getList() { $param = $this->request->param(); $page = $this->request->param('page', 1); @@ -28,6 +28,7 @@ class GoodsNew extends Controller $param['store_id'] = $this->storeId; $goodsService = new GoodsCateEs(); $data = $goodsService->list($param, $page, $pageSize); + return $this->renderSuccess($data); } diff --git a/app/command/SyncGoodsToEs.php b/app/command/SyncGoodsToEs.php index 2cc2fd66..829ef6c0 100644 --- a/app/command/SyncGoodsToEs.php +++ b/app/command/SyncGoodsToEs.php @@ -14,19 +14,30 @@ class SyncGoodsToEs extends Command protected function configure() { // 指令配置 - $this->setName('syncGoodsToEs') - ->setDescription('同步商品数据到ES'); + $this->setName('syncGoodsToEs')->setDescription('同步商品数据到ES'); + $this->addArgument("type"); + $this->addArgument("goods_id"); } protected function execute(Input $input, Output $output) { + $type = $input->getArgument("type"); + $goods_id = $input->getArgument("goods_id"); $goodsService = new GoodsCateEs(); - try { - $goodsService->batchCreateData(); - }catch (\Exception $e) { - $output->writeln($e->getMessage()); + if ($type == "goods") { + $goodsService->batchCreateGoods($goods_id); + } elseif ($type == "goods_cate"){ + $goodsService->batchCreateGoodsCategory($goods_id); } + + // try { + // $goodsService->batchCreateData(); + // }catch (\Exception $e) { + // $output->writeln($e->getMessage()); + // } + //$goodsService->batchCreateGoods(); + } } \ No newline at end of file diff --git a/app/common/library/elasticsearch/Client.php b/app/common/library/elasticsearch/Client.php index 90bde7b5..7a2eb00e 100644 --- a/app/common/library/elasticsearch/Client.php +++ b/app/common/library/elasticsearch/Client.php @@ -400,7 +400,14 @@ class Client { try { $this->parseQueryParams(); - + $this->queryParams['body']['highlight'] = [ + "fields"=>[ + "goods_name" => new \stdClass(), + "goods_no" => new \stdClass(), + ], + "pre_tags"=>"", + "post_tags"=>"" + ]; //dd($this->queryParams); if ($this->debug) { @@ -419,13 +426,7 @@ class Client if (!empty($this->queryParams)) { //高亮 - $this->queryParams['body']['highlight'] = [ - "fields"=>[ - "goods_name" => new \stdClass() - ], - "pre_tags"=>"", - "post_tags"=>"" - ]; + $result = $this->client->search($this->queryParams); $data = [ 'current_page' => 1, @@ -436,9 +437,12 @@ class Client ]; if (isset($result['hits']) && !empty($result['hits'])) { $list = $result['hits']['hits']; + // echo "
";
+                    // print_r($list);
+                    // exit();
                     $arr = [];
                     foreach ($list as $key => &$value) {
-                        $value['_source']['highlight_goods_name'] = $value['highlight']['goods_name'][0] ?? "";
+                        $value['_source']['highlight_goods_name'] = isset($value['highlight']['goods_name'][0]) ? $value['highlight']['goods_name'][0] :  (isset($value['highlight']['goods_no'][0]) ? $value['highlight']['goods_no'][0] : "");
                         $arr[] = $value['_source'];
                     }
                     $data['current_page'] = $this->queryParams['current_page'];
diff --git a/app/common/service/GoodsCateEs.php b/app/common/service/GoodsCateEs.php
index d962b6fc..a7b4f1fd 100644
--- a/app/common/service/GoodsCateEs.php
+++ b/app/common/service/GoodsCateEs.php
@@ -21,20 +21,24 @@ class GoodsCateEs
         $this->esService = Client::setEs($this->index_name);
     }
 
-
     /**
-     * 查询商品列表
-     * @param $params
+     * 商品列表
+     * [list description]
+     * @param  array   $params [description]
+     * @param  integer $page   [description]
+     * @param  integer $limit  [description]
+     * @return [type]          [description]
      */
     public function list(array $params = [],$page = 1, $limit = 10)
     {
 
         $between = $ins = $orlike = $equal = [];
         //商品名称like查询
-        if (isset($params['goods_name']) && $params['goods_name']) {
-            $orlike[] = ['goods_name' => $params['goods_name'] ?? ''];
-            $orlike[] = ['goods_no' => $params['goods_name'] ?? ''];
+        if (isset($params['keyword']) && $params['keyword']) {
+            $orlike[] = ['goods_name' => $params['keyword'] ?? ''];
+            $orlike[] = ['goods_no' => $params['keyword'] ?? ''];
         }
+        $equal[] = ['is_delete' => 0];
         //等于查询
         if (isset($params['status']) && $params['status']) {
             $equal[] = ['status' => $params['status'] ?? 0];
@@ -42,6 +46,9 @@ class GoodsCateEs
         if (isset($params['store_id']) && $params['store_id']) {
             $equal[] = ['store_id' => $params['store_id'] ?? 0];
         }
+        // if (isset($params['categoryId']) && $params['categoryId']) {
+        //     $equal[] = ['store_id' => $params['categoryId'] ?? 0];
+        // }
         //between查询
         if (isset($params['fliter_condition']) && $params['fliter_condition']) {
             $fliter_condition = json_decode($params['fliter_condition'], true);
@@ -59,19 +66,32 @@ class GoodsCateEs
         }
         
         $sort = $this->setQuerySort($params);
-        
-        //dd($sort);
-        return $this->esService
-            ->select($this->field)
-            ->orlike($orlike)
-            ->hasChild($between)
-            ->in($ins)
-            ->equal($equal)
-            ->from($page)
-            ->size($limit)
-            ->order($sort)
-            //->setDebug()
-            ->query();
+        $data = $this->esService
+                    ->select($this->field)
+                    ->orlike($orlike)
+                    ->hasChild($between)
+                    ->in($ins)
+                    ->equal($equal)
+                    ->from($page)
+                    ->size($limit)
+                    ->order($sort)
+                    //->setDebug()
+                    ->query();
+        if ($data['data']) {
+            $model = new Goods;
+            $goods_ids  = array_column($data['data'], "goods_id");
+            $goods_list = array_column($data['data'], null,  "goods_id");
+            $list = $model->getListByIds($goods_ids, 10);
+            if ($list) {
+                foreach ($list as &$value) {
+                    $value['highlight_goods_name'] = $goods_list[$value['goods_id']]['highlight_goods_name'] ?? "";
+                }
+            }
+            
+            $data['data'] = $list;
+
+        }
+        return $data;
     }
     /**
      * 检索排序条件
@@ -97,6 +117,15 @@ class GoodsCateEs
         $sort[] = ["goods_id" => 'desc'];
         return $sort;
     }
+
+    /**
+     * 计算总数
+     * [count description]
+     * @param  array   $params [description]
+     * @param  integer $page   [description]
+     * @param  integer $limit  [description]
+     * @return [type]          [description]
+     */
     public function count($params = [],$page = 1, $limit = 10)
     {
         $between = $ins = [];
@@ -121,7 +150,7 @@ class GoodsCateEs
             unset($params['channels']);
         }
         return $this->esService
-//            ->setDebug()
+            //->setDebug()
             ->like($like)
             ->hasChild($between)
             ->in($ins)
@@ -170,7 +199,7 @@ class GoodsCateEs
             'id' => $value['id'],
             'category_id' => $value['category_id'],
             'cate_store_id' => $value['store_id'],
-//                'cate_create_time' => strtotime($value['create_time']),
+            //'cate_create_time' => strtotime($value['create_time']),
             'cate_create_time' => $value['create_time'],
             'goods_category_field' => [
                 'name' => 'category',
@@ -188,10 +217,10 @@ class GoodsCateEs
      */
     public function batchCreateData()
     {
-        for ($page = 1; $page<=224; $page++)
+        for ($page = 1; $page <= 224; $page++)
         {
             $this->addData($page);
-//            sleep(1);
+            //sleep(1);
         }
     }
 
@@ -200,23 +229,21 @@ class GoodsCateEs
         $goods_cate_list = GoodsCategoryRel::where('category_id', '>', 0)
             ->where('id', '>', 1628858)
             ->where('goods_id', '>', 0)
-//            ->order('goods_id desc')
-                ->page($page)
+            //->order('goods_id desc')
+            ->page($page)
             ->limit(5000)
             ->select()
             ->toArray();
-//        dd($goods_cate_list);
-//
+        //        dd($goods_cate_list);
         $i = 0;
         if (!empty($goods_cate_list)){
-            foreach ($goods_cate_list as $value)
-            {
+            foreach ($goods_cate_list as $value) {
                 $parent_id = 'goods_'.$value['goods_id'];
                 $doc = [
                     'id' => $value['id'],
                     'category_id' => $value['category_id'],
                     'cate_store_id' => $value['store_id'],
-//                'cate_create_time' => strtotime($value['create_time']),
+                    //'cate_create_time' => strtotime($value['create_time']),
                     'cate_create_time' => $value['create_time'],
                     'goods_category_field' => [
                         'name' => 'category',
@@ -224,7 +251,7 @@ class GoodsCateEs
                     ],
 
                 ];
-//            dd($doc);
+                //            dd($doc);
                 $res = $this->esService->addChildDoc('category_'.$value['id'], $parent_id, $doc)->asArray();
                 if($res['result'] == 'created')
                 {
@@ -242,28 +269,27 @@ class GoodsCateEs
      */
     public function batchCreateGoodsData()
     {
-        for ($page = 1; $page<=173; $page++)
+        for ($page = 1; $page <= 173; $page++)
         {
             $this->addGoodsData($page);
-//            sleep(1);
+            //sleep(1);
         }
     }
 
     public function addGoodsData($page)
     {
         $goods_list = Goods::where('goods_id', '>', 0)
-//            ->order('goods_id desc')
+            //->order('goods_id desc')
             ->page($page)
             ->limit(2000)
             ->select()
             ->toArray();
-//        dd($goods_list);
-//
+            // dd($goods_list);
+
         $i = 0;
-        foreach ($goods_list as $value)
-        {
-//            $value['create_time'] = $value['create_time'];
-//            $value['update_time'] = $value['update_time'];
+        foreach ($goods_list as $value) {
+            //$value['create_time'] = $value['create_time'];
+            //$value['update_time'] = $value['update_time'];
             $value['delivery_type'] = implode(',', $value['delivery_type']);
             $value['goods_category_field'] = [
                 'name' => 'goods'
@@ -277,7 +303,81 @@ class GoodsCateEs
 
         echo('同步成功'.$i.'条数据').PHP_EOL;
     }
+    /**
+     * 批量创建商品分类
+     * [batchCreateGoodsCategory description]
+     * @param  integer $goods_cate_id [description]
+     * @return [type]                 [description]
+     */
+    public function batchCreateGoodsCategory($goods_cate_id = 0){
+        while (TRUE) {
+            $goods_cate_list = GoodsCategoryRel::where('id', '>', $goods_cate_id)
+                                ->where('category_id', '>', 0)
+                                ->where('goods_id', '>', 0)
+                                ->order('id desc')
+                                ->limit(1000)
+                                ->select()
+                                ->toArray();
+            if (!$goods_cate_list) {
+                return false;
+            }
+            foreach ($goods_cate_list as $value) {
+                $parent_id = 'goods_'.$value['goods_id'];
+                $doc = [
+                    'id' => $value['id'],
+                    'category_id' => $value['category_id'],
+                    'cate_store_id' => $value['store_id'],
+                    //'cate_create_time' => strtotime($value['create_time']),
+                    'cate_create_time' => $value['create_time'],
+                    'goods_category_field' => [
+                        'name' => 'category',
+                        'parent' => $parent_id
+                    ],
+
+                ];
+                //            dd($doc);
+                $res = $this->esService->addChildDoc('category_'.$value['id'], $parent_id, $doc)->asArray();
+                if($res['result'] == 'created'){
+                    echo('商品分类同步成功'.$value['id'].'条数据').PHP_EOL;
+                }
+            }
+            $goods_cate_id = end($goods_cate_list)['id'];
+        }
+    }
+    /**
+     * 批量创建商品
+     * [batchCreateGoods description]
+     * @param  integer $goods_id [description]
+     * @return [type]            [description]
+     */
+    public function batchCreateGoods($goods_id = 0){
+        while (TRUE) {
+            $goods_list = Goods::where('goods_id', '>', $goods_id)
+                ->order('goods_id asc')
+                ->limit(1000)
+                ->select()
+                ->toArray();
+                // dd($goods_list);
+            if (!$goods_list) {
+                return false;
+            }  
+            $i = 0;
+            foreach ($goods_list as $value) {
+                //$value['create_time'] = $value['create_time'];
+                //$value['update_time'] = $value['update_time'];
+                $value['delivery_type'] = implode(',', $value['delivery_type']);
+                $value['goods_category_field'] = [
+                    'name' => 'goods'
+                ];
+                $res = $this->esService->addDoc('goods_'.$value['goods_id'], $value)->asArray();
+                if($res['result'] == 'created'){
+                    echo('商品同步成功'.$value['goods_id'].'条数据').PHP_EOL;
+                }
+            }
+            $goods_id = end($goods_list)['goods_id'];
 
+        }
+    }
     /**
      * 删除商品
      */
diff --git a/app/common/service/GoodsEs.php b/app/common/service/GoodsEs.php
index 6861720b..4dec986b 100644
--- a/app/common/service/GoodsEs.php
+++ b/app/common/service/GoodsEs.php
@@ -26,7 +26,7 @@ class GoodsEs
     {
         return $this->esService
             ->select($this->field)
-//            ->setDebug()
+            //->setDebug()
             ->like($params)
             ->from($page)
             ->size($limit)
@@ -37,7 +37,7 @@ class GoodsEs
     public function count($params = [],$page = 1, $limit = 10)
     {
         return $this->esService
-//            ->setDebug()
+            //->setDebug()
             ->like($params)
             ->from($page)
             ->size($limit)