From 474a0fffb714bf5fa62251f43e1aa5c787971262 Mon Sep 17 00:00:00 2001 From: lqmac Date: Sun, 1 Sep 2024 16:50:54 +0800 Subject: [PATCH] 1 --- app/admin/controller/Goods.php | 4 +- app/admin/controller/Store.php | 16 ++++- app/api/controller/Goods.php | 18 ++--- app/command/SyncGoodsToEs.php | 14 ++-- app/command/SyncGoodsToPerMinute.php | 52 ++++++++++++++ app/common/library/elasticsearch/Client.php | 8 ++- app/common/service/GoodsCateEs.php | 71 +++++++++++--------- app/job/controller/goods/GoodsRealDelete.php | 40 +++++++++++ app/job/service/goods/GoodsRealDelete.php | 41 +++++++++++ config/console.php | 1 + 10 files changed, 212 insertions(+), 53 deletions(-) create mode 100644 app/command/SyncGoodsToPerMinute.php create mode 100644 app/job/controller/goods/GoodsRealDelete.php create mode 100644 app/job/service/goods/GoodsRealDelete.php diff --git a/app/admin/controller/Goods.php b/app/admin/controller/Goods.php index 8b412ceb..ea1b7d9a 100644 --- a/app/admin/controller/Goods.php +++ b/app/admin/controller/Goods.php @@ -197,8 +197,10 @@ class Goods extends Controller 'update_time' => time(), ]; - GoodsSku::whereIn('goods_id', array_column($list->toArray(), "goods_id"))->update($goods_sku_data); + + //同步到批发商城 + return $this->renderSuccess('更新成功'); } return $this->renderError($model->getError() ?: '更新失败'); diff --git a/app/admin/controller/Store.php b/app/admin/controller/Store.php index d0285f5f..b3bc4829 100644 --- a/app/admin/controller/Store.php +++ b/app/admin/controller/Store.php @@ -18,7 +18,8 @@ use app\store\model\GoodsSku; use app\store\model\GoodsImage; use app\store\model\Category; use app\store\model\GoodsSpecRel; - +use app\job\controller\goods\GoodsRealDelete as GoodsRealDeleteJob; + /** * 商城管理 * Class Store @@ -401,6 +402,19 @@ class Store extends Controller break; } $goods_ids = array_column($goods_list->toArray(), "goods_id"); + // 分批每次导入20条 + $limit = 200; + // 根据商品总数量计算需要的队列任务数量 + $jobCount = \count($goods_ids) / $limit; + // 逐次发布队列任务 + for ($i = 0; $i < $jobCount; $i++) { + $data = array_slice($goods_ids, $i * $limit, $limit); + GoodsRealDeleteJob::dispatch([ + 'list' => $data, + ]); + + } + //删除商品sku GoodsSku::whereIn('goods_id', $goods_ids)->delete(); //删除商品图片 diff --git a/app/api/controller/Goods.php b/app/api/controller/Goods.php index b78ec95b..23d8c387 100644 --- a/app/api/controller/Goods.php +++ b/app/api/controller/Goods.php @@ -42,16 +42,16 @@ 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']; - // 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 { + 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 18e14a08..5bc5aa7c 100644 --- a/app/command/SyncGoodsToEs.php +++ b/app/command/SyncGoodsToEs.php @@ -26,23 +26,17 @@ class SyncGoodsToEs extends Command $type = $input->getArgument("type"); $goods_id = $input->getArgument("goods_id"); $store_id = $input->getArgument("store_id"); - + $goodsCateService = new GoodsCateEs(); + // $goodsCateService->batchDelete([345865,1040929]); + // exit(); if ($type == "goods") { - $goodsCateService = new GoodsCateEs(); - //$goodsService = new GoodsEs(); - //$goodsService->createGoodsIndex(); $goodsCateService->batchCreateGoods($goods_id, $store_id); } elseif ($type == "goods_cate"){ - $goodsCateService = new GoodsCateEs(); + //$goodsCateService->createGoodsCateIndex(); $goodsCateService->batchCreateGoodsCategory($goods_id, $store_id); } - // try { - // $goodsService->batchCreateData(); - // }catch (\Exception $e) { - // $output->writeln($e->getMessage()); - // } } diff --git a/app/command/SyncGoodsToPerMinute.php b/app/command/SyncGoodsToPerMinute.php new file mode 100644 index 00000000..028f3e97 --- /dev/null +++ b/app/command/SyncGoodsToPerMinute.php @@ -0,0 +1,52 @@ +setName('SyncGoodsToPerMinute')->setDescription('同步商品数据到ES'); + $this->addArgument("goods_id"); + $this->addArgument("div"); + $this->addArgument("mod"); + $this->addArgument("minute"); + } + + + protected function execute(Input $input, Output $output) + { + ini_set('memory_limit', '1024M'); + $goods_id = $input->getArgument("goods_id"); + $div = $input->getArgument("div"); + $mod = $input->getArgument("mod"); + $minute = $input->getArgument("minute"); + + $min_time = time() - $minute * 60; + $limit = 100; + while (TRUE) { + $sql = "SELECT * FROM `yoshop_goods` WHERE goods_id > " . $goods_id . " AND goods_id % ". $div . " = ". $mod ." AND update_time > ".$min_time." order by update_time asc LIMIT ".$limit; + $list = Db::query($sql); + if (!$list) { + return true; + } + $obj = new GoodsCateEs(); + $res = $obj->addOrUpdateGoodsAndGoodsCate($list); + var_dump($res); + + + $goods_id = end($list)['goods_id']; + } + } + +} \ No newline at end of file diff --git a/app/common/library/elasticsearch/Client.php b/app/common/library/elasticsearch/Client.php index 2ef75778..31b52f82 100644 --- a/app/common/library/elasticsearch/Client.php +++ b/app/common/library/elasticsearch/Client.php @@ -171,7 +171,13 @@ class Client return $this->client->deleteByQuery($params); } - + public function batchDelete($bulkBody = []){ + $params = [ + 'index' => $this->index, + 'body' => $bulkBody, + ]; + return $this->client->bulk($params); + } public function existsIndex(String $index_name) { $params = [ 'index' => $index_name diff --git a/app/common/service/GoodsCateEs.php b/app/common/service/GoodsCateEs.php index a513196c..6153d976 100644 --- a/app/common/service/GoodsCateEs.php +++ b/app/common/service/GoodsCateEs.php @@ -109,7 +109,7 @@ class GoodsCateEs ]); //var_dump($params); // 排序规则 - $sort = []; + $sort[] = ["_score" => 'desc']; if ($params['sortType'] === 'all') { $sort[] = ['sort' => 'asc']; } elseif ($params['sortType'] === 'sales') { @@ -210,7 +210,7 @@ class GoodsCateEs ], ]; - return $this->esService->addDoc('category_'.$id, $doc)->asArray(); + return $this->esService->addChildDoc('category_'.$id, $parent_id, $doc)->asArray(); } @@ -331,24 +331,10 @@ class GoodsCateEs echo "没有商品分类了".PHP_EOL; 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(); + foreach ($goods_cate_list as $goods_cate) { + $res = $this->createCateData($goods_cate['id'], $goods_cate); if($res['result'] == 'created' || $res['result'] == 'updated'){ - echo('商品分类同步成功'.$value['id'].'条数据').PHP_EOL; + echo('商品分类同步成功'.$goods_cate['id'].'条数据').PHP_EOL; } } $goods_cate_id = end($goods_cate_list)['id']; @@ -369,7 +355,6 @@ class GoodsCateEs 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() @@ -379,15 +364,10 @@ class GoodsCateEs echo "没有商品了".PHP_EOL; return false; } - foreach ($goods_list as $value) { - - $value['goods_category_field'] = [ - 'name' => 'goods' - ]; - $res = $this->esService->addDoc('goods_'.$value['goods_id'], $value)->asArray(); - //var_dump($res); + foreach ($goods_list as $goods) { + $res = $this->createData($goods['goods_id'], $goods); if($res['result'] == 'created' || $res['result'] == 'updated'){ - echo('商品同步成功'.$value['goods_id'].'条数据').PHP_EOL; + echo('商品同步成功'.$goods['goods_id'].'条数据').PHP_EOL; } } $goods_id = end($goods_list)['goods_id']; @@ -403,16 +383,45 @@ class GoodsCateEs if (!$goods_list) { return true; } - foreach ($goods_list as $goods) { + $goods_cate_list = GoodsCategoryRel::whereIn('goods_id', array_column($goods_list, "goods_id"))->select()->toArray(); + $arr = []; + foreach ($goods_cate_list as $key => $value) { + $arr[$value['goods_id']][] = $value; + } + foreach ($goods_list as &$goods) { + $goods['create_time'] = date("Y-m-d H:i:s", $goods['create_time']); + $goods['update_time'] = date("Y-m-d H:i:s", $goods['update_time']); $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) { + $goods_cate_items = $arr[$goods['goods_id']] ?? []; + if (!$goods_cate_items) { + continue; + } + foreach ($goods_cate_items as $goods_cate) { $this->createCateData($goods_cate['id'], $goods_cate); } } return true; } + /** + * 批量删除父子关系的数据 + * [batchDelete description] + * @param [type] $parentIds [description] + * @return [type] [description] + */ + public function batchDelete($parentIds){ + $bulkBody = []; + foreach ($parentIds as $parentId) { + $bulkBody[] = [ + 'delete' => [ + '_index' => $this->index_name, + //'_type' => '_doc', // 在 Elasticsearch 7.x 之前的版本中,类型字段是必需的 + '_id' => 'goods_'.$parentId + ] + ]; + } + return $this->esService->batchDelete($bulkBody); + } /** * 删除商品 */ diff --git a/app/job/controller/goods/GoodsRealDelete.php b/app/job/controller/goods/GoodsRealDelete.php new file mode 100644 index 00000000..27dfc192 --- /dev/null +++ b/app/job/controller/goods/GoodsRealDelete.php @@ -0,0 +1,40 @@ +batch($data['list']); + } +} \ No newline at end of file diff --git a/app/job/service/goods/GoodsRealDelete.php b/app/job/service/goods/GoodsRealDelete.php new file mode 100644 index 00000000..76e33ef3 --- /dev/null +++ b/app/job/service/goods/GoodsRealDelete.php @@ -0,0 +1,41 @@ +batchDelete($goodsIds); + return true; + } + + +} \ No newline at end of file diff --git a/config/console.php b/config/console.php index 80efc546..03074cf0 100644 --- a/config/console.php +++ b/config/console.php @@ -17,5 +17,6 @@ return [ 'ProfitSharingResult' => 'app\command\ProfitSharingResult', 'SyncCategory' => 'app\command\SyncCategory', 'HomeLocation' => 'app\command\HomeLocation', + 'SyncGoodsToPerMinute' => 'app\command\SyncGoodsToPerMinute', ], ];