You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
yanzong/app/common/service/GoodsEs.php

381 lines
10 KiB

6 months ago
<?php
namespace app\common\service;
use app\common\library\elasticsearch\Client;
class GoodsEs
{
private Client $esService;
private string $index_name = 'goods_list';
6 months ago
6 months ago
public function __construct()
{
$this->esService = Client::setEs($this->index_name);
6 months ago
}
6 months ago
6 months ago
/**
* 查询商品列表
* @param $params
* @return
*/
public function list($params = [],$page = 1, $limit = 10)
6 months ago
{
return $this->esService
6 months ago
// ->setDebug()
->like($params)
->from($page)
->size($limit)
->order(['goods_id' => 'desc'])
6 months ago
->query();
6 months ago
}
/**
* 更新商品
*/
public function updateData($goods_id, $data)
6 months ago
{
return $this->esService->update($goods_id, $data);
6 months ago
}
6 months ago
/**
* 查询商品详情
*/
public function detail($goods_id)
{
return $this->esService->getDoc($goods_id);
6 months ago
}
6 months ago
/**
* 创建商品
*/
public function createData($goods_id, $goods_list)
{
$goods_list['create_time'] = $goods_list['create_time'] ? strtotime($goods_list['create_time']) * 1000 : time() * 1000;
$goods_list['update_time'] = $goods_list['update_time'] ? strtotime($goods_list['update_time']) * 1000 : time() * 1000;
$goods_list['delivery_type'] = implode(',', $goods_list['delivery_type']);
return $this->esService->addDoc($goods_id, $goods_list)->asArray();
}
/**
* 批量创建商品
6 months ago
* @return
*/
public function batchCreateData()
6 months ago
{
for ($page = 1; $page<=134; $page++)
{
$this->addData($page);
sleep(1);
}
}
public function addData($page)
{
$goods_list = \app\common\model\Goods::where('goods_id', '>', 368257)
// ->order('goods_id desc')
->page($page)
->limit(2000)
->select()
->toArray();
//
$i = 0;
foreach ($goods_list as $value)
{
$value['create_time'] = strtotime($value['create_time']) * 1000;
$value['update_time'] = strtotime($value['update_time']) * 1000;
$value['delivery_type'] = implode(',', $value['delivery_type']);
$res = $this->esService->addDoc($value['goods_id'], $value)->asArray();
if($res['result'] == 'created')
{
$i++;
}
}
echo('同步成功'.$i.'条数据').PHP_EOL;
6 months ago
}
/**
* 删除商品
*/
6 months ago
public function delete($goods_id)
6 months ago
{
return $this->esService->deleteDoc($goods_id);
}
/**
* 删除索引
*/
public function deleteIndex()
{
return $this->esService->deleteIndexNew();
6 months ago
}
/**
* 创建商品Index
* @return void
* @throws Exception
*/
function createGoodsIndex()
{
$mapping = [
'properties' => [
6 months ago
'goods_id' => [
'type' => 'keyword'
6 months ago
],
6 months ago
'goods_type' => [
'type' => 'keyword'
6 months ago
],
'goods_name' => [
6 months ago
'type' => 'text',
"analyzer" => "ik_smart"
],
'goods_no' => [
'type' => 'keyword',
],
'video_id' => [
'type' => 'integer'
],
'brand_id' => [
'type' => 'integer'
],
'video_cover_id' => [
'type' => 'integer'
],
'selling_point' => [
6 months ago
'type' => 'text',
"analyzer" => "ik_smart"
],
'spec_type' => [
'type' => 'integer'
],
'cost_price_min' => [
'type' => 'float'
],
'goods_price_min' => [
'type' => 'float'
],
'goods_price_max' => [
'type' => 'float'
],
'line_price_min' => [
'type' => 'float'
],
'line_price_max' => [
'type' => 'float'
],
'stock_total' => [
'type' => 'integer'
],
'deduct_stock_type' => [
'type' => 'integer'
],
'is_restrict' => [
'type' => 'integer'
],
'restrict_total' => [
'type' => 'integer'
],
'restrict_single' => [
'type' => 'integer'
],
'content' => [
'type' => 'text',
],
'sales_initial' => [
'type' => 'integer'
],
'delivery_id' => [
'type' => 'integer'
],
'is_points_gift' => [
'type' => 'integer'
],
'is_points_discount' => [
'type' => 'integer'
],
'is_alone_points_discount' => [
'type' => 'integer'
],
'points_discount_config' => [
'type' => 'text'
],
'is_enable_grade' => [
'type' => 'integer'
],
'is_alone_grade' => [
'type' => 'integer'
],
'alone_grade_equity' => [
'type' => 'text'
],
'is_ind_dealer' => [
'type' => 'integer'
],
'dealer_money_type' => [
'type' => 'integer'
],
'first_money' => [
'type' => 'float'
],
'second_money' => [
'type' => 'float'
],
'third_money' => [
'type' => 'float'
],
'is_ind_delivery_type' => [
'type' => 'integer'
],
'delivery_type' => [
6 months ago
'type' => 'keyword'
],
'status' => [
'type' => 'integer'
],
'sort' => [
'type' => 'integer'
],
'store_id' => [
'type' => 'keyword'
],
'merchant_id' => [
'type' => 'keyword'
],
'create_time' => [
'type' => 'date',
'format' => 'yyyy-MM-dd HH:mm:ss'
],
'update_time' => [
'type' => 'date',
'format' => 'yyyy-MM-dd HH:mm:ss'
],
'is_brand' => [
'type' => 'integer'
],
'is_new' => [
'type' => 'integer'
],
'paihang' => [
'type' => 'integer'
],
'remaizhishu' => [
'type' => 'integer'
],
'spu_id' => [
'type' => 'integer'
],
'channel' => [
'type' => 'keyword'
],
'unicode' => [
'type' => 'keyword'
],
'is_in_store' => [
'type' => 'integer'
],
'profit' => [
'type' => 'float'
],
'profit_rate' => [
'type' => 'float'
],
'cmmdty_model'=> [
'type' => 'keyword'
],
'remark' => [
'type' => 'text'
],
'sale_areas' => [
'type' => 'text'
],
'sale_areas_id' => [
'type' => 'keyword'
],
'data_type' => [
'type' => 'integer'
],
'is_pool' => [
'type' => 'integer'
],
'is_self' => [
'type' => 'integer'
],
'link' => [
6 months ago
'type' => 'keyword'
],
'origin_goods_id' => [
'type' => 'integer'
],
'link_other' => [
'type' => 'text'
],
'goods_no_other' => [
6 months ago
'type' => 'keyword'
],
'region' => [
'type' => 'text'
],
'region_text' => [
'type' => 'text'
],
'specific_value' => [
6 months ago
'type' => 'keyword'
],
'cate_status' => [
'type' => 'integer'
],
'is_check' => [
'type' => 'integer'
],
'goods_source' => [
6 months ago
'type' => 'keyword'
],
'delivery_time' => [
'type' => 'integer'
],
'distribute_price' => [
'type' => 'float'
],
'shop_price' => [
'type' => 'float'
],
'is_has_banner' => [
'type' => 'integer'
],
'is_has_detail' => [
'type' => 'integer'
],
'is_jingpin' => [
'type' => 'integer'
],
'sale_time' => [
'type' => 'integer'
],
'markup_rate' => [
'type' => 'integer'
]
6 months ago
]
];
return $this->esService->createIndexNew($mapping);
}
/**
* 设置分词字段
*/
public function analyze()
{
return $this->esService->analyze('goods_name');
6 months ago
}
6 months ago
}