Commit 2cfc1e9c authored by luhongguang's avatar luhongguang

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

parent ae54a12e
<?php
use Api\PhpUtils\Http\Request;
use App\Base\Cli;
use App\Exception\custom\InterfaceException;
use App\Models\goods\mysql\Category;
use \App\Models\goods\mysql\GoodsSku;
use \App\Services\goods\GoodsService;
use \App\Services\goods\ElasticGoodService;
use Api\PhpUtils\Log\DaemonLog;
use Api\PhpServices\JwUser\JwUser;
use App\Services\shop\ShopService;
/**
* 同步商品数据到es
......@@ -15,6 +20,8 @@ class GoodsToEsController extends Cli
{
public function IndexAction()
{
$this->initEsIndex();
$goodsSkuIds = $this->getGoodsSkuIdList();
$this->toEs($goodsSkuIds);
}
......@@ -29,8 +36,61 @@ class GoodsToEsController extends Cli
if (!empty($goodsSkuIds)) {
foreach ($goodsSkuIds as $goodsSkuId) {
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
{
const INDEX = "bp_goods";
public static function createIndex($index)
public static function createIndex()
{
$elasticClient = ElasticUtil::getInstance();
$data = [
"settings" => [
"analysis" => [
"analyzer"=>[
"default"=>[
"type"=>"ik_max_word",
]
]
]
],
'mappings' => [
'properties' => [
'id' => [
'type' => 'keyword',
'goods_name' => [
'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',
'analyzer' => 'ik_max_word',
],
'shop_name' => [
'type' => 'text',
'analyzer' => 'ik_max_word',
],
]
]
];
$res = $elasticClient->createIndex($index, $data);
var_export($res);
return $elasticClient->createIndex(self::INDEX, $data);
}
/**
* 删除
*/
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