Commit 2cfc1e9c authored by luhongguang's avatar luhongguang

update:同步商品数据到es脚本准备

parent ae54a12e
<?php <?php
use Api\PhpUtils\Http\Request;
use App\Base\Cli; use App\Base\Cli;
use App\Exception\custom\InterfaceException;
use App\Models\goods\mysql\Category;
use \App\Models\goods\mysql\GoodsSku; use \App\Models\goods\mysql\GoodsSku;
use \App\Services\goods\GoodsService; use \App\Services\goods\GoodsService;
use \App\Services\goods\ElasticGoodService;
use Api\PhpUtils\Log\DaemonLog; use Api\PhpUtils\Log\DaemonLog;
use Api\PhpServices\JwUser\JwUser; use Api\PhpServices\JwUser\JwUser;
use App\Services\shop\ShopService;
/** /**
* 同步商品数据到es * 同步商品数据到es
...@@ -15,6 +20,8 @@ class GoodsToEsController extends Cli ...@@ -15,6 +20,8 @@ class GoodsToEsController extends Cli
{ {
public function IndexAction() public function IndexAction()
{ {
$this->initEsIndex();
$goodsSkuIds = $this->getGoodsSkuIdList(); $goodsSkuIds = $this->getGoodsSkuIdList();
$this->toEs($goodsSkuIds); $this->toEs($goodsSkuIds);
} }
...@@ -29,8 +36,61 @@ class GoodsToEsController extends Cli ...@@ -29,8 +36,61 @@ class GoodsToEsController extends Cli
if (!empty($goodsSkuIds)) { if (!empty($goodsSkuIds)) {
foreach ($goodsSkuIds as $goodsSkuId) { foreach ($goodsSkuIds as $goodsSkuId) {
echo "当前正在同步的商品sku_id: $goodsSkuId \n"; echo "当前正在同步的商品sku_id: $goodsSkuId \n";
GoodsService::updateGoodsInfoToEs($goodsSkuId); $this->updateGoodsInfoToEs($goodsSkuId);
}
}
}
private function initEsIndex()
{
//删除原index
ElasticGoodService::deleteIndex();
//创建空index
ElasticGoodService::createIndex();
} }
/**
* 写入到es
* @param $goodsSkuId
* @return array|callable
* @throws InterfaceException
*/
public function updateGoodsInfoToEs($goodsSkuId)
{
$goodsSkuInfoList = GoodsSku::getRecordMaster(["goods_sku_id" => $goodsSkuId]);
if (!empty($goodsSkuInfoList)) {
$goodsSkuInfo = $goodsSkuInfoList[0];
$nameList = Category::select("name", ["category_id" => [$goodsSkuInfo["category_1_id"], $goodsSkuInfo["category_2_id"]]]);
//生活号信息
$url = config('interface', 'goods.public.get_life_account_by_id');
if (empty($url)) {
throw new InterfaceException(['cus' => 0]);
}
$lifeAccountRes = (new Request())->get($url, ["life_account_id" => $goodsSkuInfo["life_account_id"]]);
//门店信息
$subShopList = ShopService::getRelationShop($goodsSkuId);
$data = [
"id" => $goodsSkuInfo["goods_sku_id"],
"goods_desc_pic_url" => $goodsSkuInfo["desc_pic_url"],
"goods_name" => $goodsSkuInfo["goods_name"],
"inventory_rest" => $goodsSkuInfo["inventory_rest"],
"total_amount_sold" => $goodsSkuInfo["total_amount_sold"],
"original_price" => $goodsSkuInfo["original_price"] / 100,
"price" => $goodsSkuInfo["price"] / 100,
"price_sort" => (int)$goodsSkuInfo["price"],
"audit_status" => $goodsSkuInfo["audit_status"],
"online_status" => $goodsSkuInfo["online_status"],
"desc" => $goodsSkuInfo["desc"],
"category_1_name" => $nameList[0],
"category_2_name" => $nameList[1],
"life_account_name" => empty($lifeAccountRes["response"]["result"]["life_account_name"]) ? "" : $lifeAccountRes["response"]["result"]["life_account_name"],
"life_account_icon" => empty($lifeAccountRes["response"]["result"]["life_account_icon"]) ? "" : $lifeAccountRes["response"]["result"]["life_account_icon"],
"shop_name" => empty($subShopList[0]["shop_name"]) ? "" : $subShopList[0]["shop_name"],
"shop_longitude" => empty($subShopList[0]["longitude"]) ? "" : $subShopList[0]["longitude"],
"shop_latitude" => empty($subShopList[0]["latitude"]) ? "" : $subShopList[0]["latitude"],
];
return ElasticGoodService::indexDoc($goodsSkuId, $data);
} }
} }
......
...@@ -9,23 +9,60 @@ class ElasticGoodService ...@@ -9,23 +9,60 @@ class ElasticGoodService
{ {
const INDEX = "bp_goods"; const INDEX = "bp_goods";
public static function createIndex($index) public static function createIndex()
{ {
$elasticClient = ElasticUtil::getInstance(); $elasticClient = ElasticUtil::getInstance();
$data = [ $data = [
"settings" => [
"analysis" => [
"analyzer"=>[
"default"=>[
"type"=>"ik_max_word",
]
]
]
],
'mappings' => [ 'mappings' => [
'properties' => [ 'properties' => [
'id' => [ 'goods_name' => [
'type' => 'keyword', 'type' => 'text',
'analyzer' => 'ik_max_word',
],
'desc' => [
'type' => 'text',
'analyzer' => 'ik_max_word',
],
'category_1_name' => [
'type' => 'text',
'analyzer' => 'ik_max_word',
],
'category_2_name' => [
'type' => 'text',
'analyzer' => 'ik_max_word',
], ],
'name' => [ 'life_account_name' => [
'type' => 'text', 'type' => 'text',
'analyzer' => 'ik_max_word',
],
'shop_name' => [
'type' => 'text',
'analyzer' => 'ik_max_word',
], ],
] ]
] ]
]; ];
$res = $elasticClient->createIndex($index, $data); return $elasticClient->createIndex(self::INDEX, $data);
var_export($res); }
/**
* 删除
*/
public static function deleteIndex(): void
{
$elasticClient = ElasticUtil::getInstance();
$res = $elasticClient->deleteIndex(self::INDEX);
var_dump($res);
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment