From 66a1a3019ff73a0ee3478c080b60565bacad77c3 Mon Sep 17 00:00:00 2001 From: lqmac Date: Tue, 27 Aug 2024 00:15:31 +0800 Subject: [PATCH 1/2] 1 --- app/api/controller/Goods.php | 14 +++- app/command/SyncGoodsToEs.php | 23 ++++-- app/common/library/elasticsearch/Client.php | 32 ++++++-- app/common/service/GoodsCateEs.php | 89 +++++++++++++++------ app/job/service/goods/GoodsDelete.php | 1 + app/job/service/goods/GoodsOffline.php | 1 + app/job/service/goods/GoodsOnline.php | 2 + app/store/model/goods/Import.php | 42 +++++----- config/database.php | 14 ++-- 9 files changed, 146 insertions(+), 72 deletions(-) diff --git a/app/api/controller/Goods.php b/app/api/controller/Goods.php index 1a53f0a8..b78ec95b 100644 --- a/app/api/controller/Goods.php +++ b/app/api/controller/Goods.php @@ -18,7 +18,7 @@ use app\common\enum\goods\GoodsDeliveryTime; use think\facade\Cache; use EasyWeChat\Factory; use app\api\service\Setting as SettingService; - +use app\common\service\GoodsCateEs; /** * 商品控制器 * Class Goods @@ -42,7 +42,17 @@ class Goods extends Controller $param['channels'] = $this->storeInfo['open_channel'] ? array_merge(['zy'], explode(",", $this->storeInfo['open_channel'])) : []; //分类利润-利润率 $param['fliter_condition'] = $this->storeInfo['fliter_condition']; - $list = $model->getList($param, 10); + // if (isset($param['keyword']) && $param['keyword']) { + // $page = $this->request->param('page', 1); + // $pageSize = $this->request->param('pageSize', 10); + // $param['status'] = 10; + // $param['store_id'] = $this->storeId; + // $goodsService = new GoodsCateEs(); + // $list = $goodsService->list($param, $page, $pageSize); + // } else { + $list = $model->getList($param, 10); + //} + return $this->renderSuccess(compact('list')); } diff --git a/app/command/SyncGoodsToEs.php b/app/command/SyncGoodsToEs.php index 3b8a1fea..18e14a08 100644 --- a/app/command/SyncGoodsToEs.php +++ b/app/command/SyncGoodsToEs.php @@ -17,6 +17,7 @@ class SyncGoodsToEs extends Command $this->setName('syncGoodsToEs')->setDescription('同步商品数据到ES'); $this->addArgument("type"); $this->addArgument("goods_id"); + $this->addArgument("store_id"); } @@ -24,18 +25,24 @@ class SyncGoodsToEs extends Command { $type = $input->getArgument("type"); $goods_id = $input->getArgument("goods_id"); - $goodsService = new GoodsCateEs(); + $store_id = $input->getArgument("store_id"); + if ($type == "goods") { - $goodsService->batchCreateGoods($goods_id); + $goodsCateService = new GoodsCateEs(); + //$goodsService = new GoodsEs(); + //$goodsService->createGoodsIndex(); + $goodsCateService->batchCreateGoods($goods_id, $store_id); } elseif ($type == "goods_cate"){ - $goodsService->batchCreateGoodsCategory($goods_id); + $goodsCateService = new GoodsCateEs(); + //$goodsCateService->createGoodsCateIndex(); + $goodsCateService->batchCreateGoodsCategory($goods_id, $store_id); } - try { - $goodsService->batchCreateData(); - }catch (\Exception $e) { - $output->writeln($e->getMessage()); - } + // try { + // $goodsService->batchCreateData(); + // }catch (\Exception $e) { + // $output->writeln($e->getMessage()); + // } } diff --git a/app/common/library/elasticsearch/Client.php b/app/common/library/elasticsearch/Client.php index 7a2eb00e..2ef75778 100644 --- a/app/common/library/elasticsearch/Client.php +++ b/app/common/library/elasticsearch/Client.php @@ -285,7 +285,7 @@ class Client ];*/ $must[] = [ - 'match' => [$filed => $value] + 'match' => [$filed => $value, ''] // 'match_phrase' => [$filed => $value] ]; } @@ -298,9 +298,12 @@ class Client if (!empty($this->queryParams['orlike'])) { foreach ($this->queryParams['orlike'] as $key => $row) { foreach ($row as $filed => $value) { + // $orlike[] = [ + // 'match_phrase' => [$filed => $value] + // ]; $orlike[] = [ - 'match_phrase' => [$filed => $value] - ]; + 'match' => [$filed => ['query' => $value, 'fuzziness' => "AUTO", 'operator' => 'or']] + ]; } } @@ -496,7 +499,8 @@ class Client 'routing' => $parent_id,//与父文档在同一个分区 'body' => $doc ]; - + // var_dump($params); + // exit; return $this->client->index($params); } @@ -529,12 +533,23 @@ class Client 'number_of_replicas' => 0, 'analysis' => [ 'analyzer' => [ - 'default' => [ - 'type' => 'ik_smart' + 'ik' => [ + 'tokenizer' => 'ik_max_word' ] ] ], ]; + // return [ + // 'number_of_shards' => 1, + // 'number_of_replicas' => 0, + // 'analysis' => [ + // 'analyzer' => [ + // 'default' => [ + // 'type' => 'ik_smart' + // ] + // ] + // ], + // ]; } /** @@ -639,12 +654,13 @@ class Client '_source' => [ 'enabled' => true, ], - 'properties' => $mapping + + 'properties' => $mapping['properties'] ] ] ]; -// dd($params); + //dd($params); $result = $this->indices()->create($params); diff --git a/app/common/service/GoodsCateEs.php b/app/common/service/GoodsCateEs.php index a7b4f1fd..a513196c 100644 --- a/app/common/service/GoodsCateEs.php +++ b/app/common/service/GoodsCateEs.php @@ -16,9 +16,10 @@ class GoodsCateEs private string $field = 'goods_name,goods_id,category_id,goods_price_min,profit,profit_rate,status,create_time,goods_no,sort,store_id'; - public function __construct() + public function __construct($index_name = "") { - $this->esService = Client::setEs($this->index_name); + $index_name = $index_name ? $index_name : $this->index_name; + $this->esService = Client::setEs($index_name); } /** @@ -46,9 +47,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]; - // } + if (isset($params['merchantId']) && $params['merchantId']) { + $equal[] = ['merchant_id' => $params['merchantId'] ?? 0]; + } //between查询 if (isset($params['fliter_condition']) && $params['fliter_condition']) { $fliter_condition = json_decode($params['fliter_condition'], true); @@ -81,6 +82,8 @@ class GoodsCateEs $model = new Goods; $goods_ids = array_column($data['data'], "goods_id"); $goods_list = array_column($data['data'], null, "goods_id"); + // var_dump($goods_ids); + // exit(); $list = $model->getListByIds($goods_ids, 10); if ($list) { foreach ($list as &$value) { @@ -110,7 +113,7 @@ class GoodsCateEs if ($params['sortType'] === 'all') { $sort[] = ['sort' => 'asc']; } elseif ($params['sortType'] === 'sales') { - $sort[] = ['goods_sales' => 'desc']; + $sort[] = ['sales_actual' => 'desc']; } elseif ($params['sortType'] === 'price') { $sort[] = $params['sortPrice'] ? ['goods_price_min' => 'desc'] : ['goods_price_min' => 'asc']; } @@ -180,12 +183,12 @@ class GoodsCateEs /** * 创建商品 */ - public function createData($goods_id, $list) + public function createData($goods_id, $goods) { $list['goods_category_field'] = [ 'name' => 'goods' ]; - return $this->esService->addDoc('goods_'.$goods_id, $list)->asArray(); + return $this->esService->addDoc('goods_'.$goods_id, $goods)->asArray(); } /** @@ -243,8 +246,8 @@ 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' => $value['create_time'], + 'cate_create_time' => strtotime($value['create_time']), + //'cate_create_time' => $value['create_time'], 'goods_category_field' => [ 'name' => 'category', 'parent' => $parent_id @@ -309,16 +312,23 @@ class GoodsCateEs * @param integer $goods_cate_id [description] * @return [type] [description] */ - public function batchCreateGoodsCategory($goods_cate_id = 0){ + public function batchCreateGoodsCategory($goods_cate_id = 0, $storeId = 0){ + $where = []; + if ($storeId) { + $where['store_id'] = $storeId; + } while (TRUE) { + echo $goods_cate_id.PHP_EOL; $goods_cate_list = GoodsCategoryRel::where('id', '>', $goods_cate_id) + ->where($where) ->where('category_id', '>', 0) ->where('goods_id', '>', 0) - ->order('id desc') + ->order('id asc') ->limit(1000) ->select() ->toArray(); if (!$goods_cate_list) { + echo "没有商品分类了".PHP_EOL; return false; } foreach ($goods_cate_list as $value) { @@ -337,7 +347,7 @@ class GoodsCateEs ]; // dd($doc); $res = $this->esService->addChildDoc('category_'.$value['id'], $parent_id, $doc)->asArray(); - if($res['result'] == 'created'){ + if($res['result'] == 'created' || $res['result'] == 'updated'){ echo('商品分类同步成功'.$value['id'].'条数据').PHP_EOL; } } @@ -350,27 +360,33 @@ class GoodsCateEs * @param integer $goods_id [description] * @return [type] [description] */ - public function batchCreateGoods($goods_id = 0){ + public function batchCreateGoods($goods_id = 0, $storeId = 0){ + $where = []; + if ($storeId) { + $where['store_id'] = $storeId; + } while (TRUE) { + echo $goods_id.PHP_EOL; $goods_list = Goods::where('goods_id', '>', $goods_id) + ->where($where) + //->field("goods_id,goods_name,goods_no,content,cost_price_min,goods_price_min,sort,sales_actual,profit,profit_rate,channel,store_id,is_delete,status,create_time") ->order('goods_id asc') ->limit(1000) ->select() ->toArray(); - // dd($goods_list); + //dd($goods_list); if (!$goods_list) { + echo "没有商品了".PHP_EOL; 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'){ + //var_dump($res); + if($res['result'] == 'created' || $res['result'] == 'updated'){ echo('商品同步成功'.$value['goods_id'].'条数据').PHP_EOL; } } @@ -378,6 +394,25 @@ class GoodsCateEs } } + /** + * 添加或者更新商品 + * [addOrUpdateGoodsAndGoodsCate description] + * @param [type] $goods_list [description] + */ + public function addOrUpdateGoodsAndGoodsCate($goods_list){ + if (!$goods_list) { + return true; + } + foreach ($goods_list as $goods) { + $this->createData($goods['goods_id'], $goods); + $goods_cate_list = GoodsCategoryRel::where('goods_id', $goods['goods_id'])->select()->toArray(); + foreach ($goods_cate_list as $goods_cate) { + $this->createCateData($goods_cate['id'], $goods_cate); + } + } + return true; + + } /** * 删除商品 */ @@ -410,10 +445,11 @@ class GoodsCateEs ], 'goods_name' => [ 'type' => 'text', - "analyzer" => "ik_smart" + "analyzer" => "ik_max_word", + "search_analyzer" => "ik_smart" ], 'goods_no' => [ - 'type' => 'keyword', + 'type' => 'keyword' ], 'video_id' => [ 'type' => 'integer' @@ -425,8 +461,7 @@ class GoodsCateEs 'type' => 'integer' ], 'selling_point' => [ - 'type' => 'text', - "analyzer" => "ik_smart" + 'type' => 'keyword', ], 'spec_type' => [ 'type' => 'integer' @@ -647,7 +682,8 @@ class GoodsCateEs 'type' => 'integer' ], 'cate_create_time' => [ - 'type' => 'integer' + 'type' => 'date', + 'format' => 'yyyy-MM-dd HH:mm:ss' ], 'goods_category_field' => [ 'type' => 'join', @@ -657,7 +693,8 @@ class GoodsCateEs ] ] ]; - + //var_dump($mapping); + //exit(); return $this->esService->createIndexNew($mapping); } diff --git a/app/job/service/goods/GoodsDelete.php b/app/job/service/goods/GoodsDelete.php index 46b6dae9..a659352a 100644 --- a/app/job/service/goods/GoodsDelete.php +++ b/app/job/service/goods/GoodsDelete.php @@ -42,6 +42,7 @@ class GoodsDelete extends BaseService $goodsIds = array_column($goods_list->toArray(), "goods_id"); GoodsModel::whereIn('origin_goods_id', $goodsIds)->update(['is_delete' => 1, 'update_time' => time()]); + //删除批发商城的商品 return true; } diff --git a/app/job/service/goods/GoodsOffline.php b/app/job/service/goods/GoodsOffline.php index 9ce3338f..8040c556 100644 --- a/app/job/service/goods/GoodsOffline.php +++ b/app/job/service/goods/GoodsOffline.php @@ -46,6 +46,7 @@ class GoodsOffline extends BaseService //GoodsModel::whereIn('origin_goods_id', $goodsIds)->update(['status' => 20, 'is_jd_remove' => 1, 'update_time' => time()]); GoodsModel::whereIn('origin_goods_id', $goodsIds)->update(['is_delete' => 1, 'update_time' => time()]); + //下架批发商城的商品 return true; } diff --git a/app/job/service/goods/GoodsOnline.php b/app/job/service/goods/GoodsOnline.php index ac1056df..0829f403 100644 --- a/app/job/service/goods/GoodsOnline.php +++ b/app/job/service/goods/GoodsOnline.php @@ -43,6 +43,8 @@ class GoodsOnline extends BaseService } $goodsIds = array_column($goods_list->toArray(), "goods_id"); GoodsModel::whereIn('origin_goods_id', $goodsIds)->update(['status' => 10, 'is_jd_remove' => 0, 'update_time' => time(), 'is_delete' => 0]); + + //上架批发商城的商品 return true; } diff --git a/app/store/model/goods/Import.php b/app/store/model/goods/Import.php index f8cce590..018334b2 100644 --- a/app/store/model/goods/Import.php +++ b/app/store/model/goods/Import.php @@ -145,27 +145,27 @@ class Import extends ImportModel } - // $obj = new \app\job\service\goods\GoodsStoreImport(); - // $service = new \app\job\service\goods\Collector(); - // foreach ($execlData as $item) { - // $info = \app\common\model\Goods::where('goods_no', $item['C'])->where('store_id', self::$storeId)->where('merchant_id', self::$merchantId)->where('channel', $item['channel'])->where('is_delete',0)->find(); - // // var_dump($item['C']); - // // var_dump($info); - // if ($info) { - // var_dump($info->goods_id); - // $this->successCount++; - // continue; - // } - // //var_dump($info->goods_id); - // //exit(); - // $data = $obj->createData($item, self::$storeId, self::$merchantId); - // $service->single($item['D'], $data, self::$storeId); - // exit(); - // // // 记录导入成功 - // // $this->successCount++; - // } - // var_dump(\count($execlData)); - // exit(); + $obj = new \app\job\service\goods\GoodsStoreImport(); + $service = new \app\job\service\goods\Collector(); + foreach ($execlData as $item) { + $info = \app\common\model\Goods::where('goods_no', $item['C'])->where('store_id', self::$storeId)->where('merchant_id', self::$merchantId)->where('channel', $item['channel'])->where('is_delete',0)->find(); + // var_dump($item['C']); + // var_dump($info); + if ($info) { + var_dump($info->goods_id); + $this->successCount++; + continue; + } + //var_dump($info->goods_id); + //exit(); + $data = $obj->createData($item, self::$storeId, self::$merchantId); + $service->single($item['D'], $data, self::$storeId); + exit(); + // // 记录导入成功 + // $this->successCount++; + } + var_dump(\count($execlData)); + exit(); diff --git a/config/database.php b/config/database.php index cf68d182..966fe179 100644 --- a/config/database.php +++ b/config/database.php @@ -74,21 +74,21 @@ return [ ], 'dataCenterMysql' => [ // 数据库类型 - 'type' => env('database.type', 'mysql'), + 'type' => env('wdatabase.type', 'mysql'), // 服务器地址 - 'hostname' => env('database.hostname', $config['host']), + 'hostname' => env('wdatabase.hostname', $config['host']), // 数据库名 - 'database' => "data_center", + 'database' => env('wdatabase.database', $config['database']), // 用户名 - 'username' => env('database.username', $config['username']), + 'username' => env('wdatabase.username', $config['username']), // 密码 - 'password' => env('database.password', $config['password']), + 'password' => env('wdatabase.password', $config['password']), // 端口 - 'hostport' => env('database.hostport', $config['hostport']), + 'hostport' => env('wdatabase.hostport', $config['hostport']), // 数据库连接参数 'params' => [], // 数据库编码默认采用utf8 - 'charset' => env('database.charset', 'utf8'), + 'charset' => env('wdatabase.charset', 'utf8'), // 数据库表前缀 'prefix' => "", From 5d32f8d4abb5f994dd66dca089e40d321244faef Mon Sep 17 00:00:00 2001 From: lqmac Date: Tue, 27 Aug 2024 00:30:33 +0800 Subject: [PATCH 2/2] 1 --- app/store/model/goods/Import.php | 42 ++++++++++++++++---------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/app/store/model/goods/Import.php b/app/store/model/goods/Import.php index 018334b2..f8cce590 100644 --- a/app/store/model/goods/Import.php +++ b/app/store/model/goods/Import.php @@ -145,27 +145,27 @@ class Import extends ImportModel } - $obj = new \app\job\service\goods\GoodsStoreImport(); - $service = new \app\job\service\goods\Collector(); - foreach ($execlData as $item) { - $info = \app\common\model\Goods::where('goods_no', $item['C'])->where('store_id', self::$storeId)->where('merchant_id', self::$merchantId)->where('channel', $item['channel'])->where('is_delete',0)->find(); - // var_dump($item['C']); - // var_dump($info); - if ($info) { - var_dump($info->goods_id); - $this->successCount++; - continue; - } - //var_dump($info->goods_id); - //exit(); - $data = $obj->createData($item, self::$storeId, self::$merchantId); - $service->single($item['D'], $data, self::$storeId); - exit(); - // // 记录导入成功 - // $this->successCount++; - } - var_dump(\count($execlData)); - exit(); + // $obj = new \app\job\service\goods\GoodsStoreImport(); + // $service = new \app\job\service\goods\Collector(); + // foreach ($execlData as $item) { + // $info = \app\common\model\Goods::where('goods_no', $item['C'])->where('store_id', self::$storeId)->where('merchant_id', self::$merchantId)->where('channel', $item['channel'])->where('is_delete',0)->find(); + // // var_dump($item['C']); + // // var_dump($info); + // if ($info) { + // var_dump($info->goods_id); + // $this->successCount++; + // continue; + // } + // //var_dump($info->goods_id); + // //exit(); + // $data = $obj->createData($item, self::$storeId, self::$merchantId); + // $service->single($item['D'], $data, self::$storeId); + // exit(); + // // // 记录导入成功 + // // $this->successCount++; + // } + // var_dump(\count($execlData)); + // exit();