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.
137 lines
4.4 KiB
137 lines
4.4 KiB
<?php
|
|
|
|
|
|
namespace App\Service\v3\Implementations;
|
|
|
|
|
|
use App\Model\v3\Category;
|
|
use App\Model\v3\Goods;
|
|
use App\Model\v3\GoodsCategory;
|
|
use App\Model\v3\Store;
|
|
use App\Service\v3\Interfaces\SearchServiceInterface;
|
|
use Hyperf\Paginator\Paginator;
|
|
|
|
class SearchService implements SearchServiceInterface
|
|
{
|
|
|
|
public function doForGoods($params)
|
|
{
|
|
|
|
$builder = Goods::query()
|
|
->with(['store'])
|
|
->where(['market_id' => $params['market_id']])
|
|
->where(function ($query) {
|
|
$query->where('inventory', '>', 0)->orWhere('is_infinite', '=', 1);
|
|
});;
|
|
|
|
if (isset($params['store_id']) && $params['store_id']) {
|
|
$builder->where(['store_id' => $params['store_id']]);
|
|
}
|
|
|
|
if (isset($params['type_id']) && $params['type_id']) {
|
|
$typeIds = explode(',', $params['type_id']);
|
|
$builder->whereIn('category_id', $typeIds);
|
|
}
|
|
|
|
if (isset($params['goods_category_ids']) && $params['goods_category_ids']) {
|
|
$typeIds = explode(',', $params['goods_category_ids']);
|
|
$builder->whereIn('goods_category_id', $typeIds);
|
|
}
|
|
|
|
if (isset($params['keyword']) && $params['keyword']) {
|
|
// 二级分类
|
|
$categoryIds = Category::query()->where('title', 'like', '%'.$params['keyword'].'%')->pluck('id');
|
|
// 三级分类
|
|
$goodsCategoryIds = GoodsCategory::query()->where('title', 'like', '%'.$params['keyword'].'%')->pluck('id');
|
|
$builder->where(function ($query) use ($params, $categoryIds, $goodsCategoryIds) {
|
|
$query->where('name', 'like', '%'.$params['keyword'].'%')
|
|
->orWhereIn('category_id', $categoryIds)
|
|
->orWhereIn('goods_category_id', $goodsCategoryIds);
|
|
});
|
|
}
|
|
|
|
if (isset($params['order_by']) && $params['order_by']) {
|
|
$sort = $params['sort'] ?? 'desc';
|
|
switch ($params['order_by']) {
|
|
case 'sales':
|
|
$builder->orderBySales($sort);
|
|
break;
|
|
case 'price':
|
|
$builder->orderByPrice($sort);
|
|
break;
|
|
default:
|
|
$builder->orderByDefault($sort);
|
|
break;
|
|
}
|
|
}
|
|
|
|
$builder->select(['*'])->addSelect(['sales as total_sales']);
|
|
$paginate = $builder->paginate($params['pagesize']);
|
|
$goods = $paginate->toArray();
|
|
return ['has_more_pages' => $paginate->hasMorePages(), 'goods' => $goods['data']];
|
|
}
|
|
|
|
public function doForStores($params)
|
|
{
|
|
$builder = Store::query()
|
|
->with(['goods' => function($query) {
|
|
return $query->limit(5)
|
|
->select(['*'])->addSelect(['sales as total_sales']);
|
|
}])
|
|
->where(['market_id' => $params['market_id']]);
|
|
|
|
if (isset($params['store_id']) && $params['store_id']) {
|
|
$builder->where(['store_id' => $params['store_id']]);
|
|
}
|
|
|
|
if (isset($params['type_id']) && $params['type_id']) {
|
|
$builder->where(['category_id' => $params['type_id']]);
|
|
}
|
|
|
|
if (isset($params['keyword']) && $params['keyword']) {
|
|
$builder->where('name', 'like', '%'.$params['keyword'].'%');
|
|
}
|
|
|
|
if (isset($params['order_by']) && $params['order_by']) {
|
|
$sort = $params['sort'] ?? 'desc';
|
|
switch ($params['order_by']) {
|
|
case 'sales':
|
|
$builder->orderBySales($sort);
|
|
break;
|
|
default:
|
|
$builder->orderByDefault($sort);
|
|
break;
|
|
}
|
|
}
|
|
|
|
$builder->select(['id', 'logo', 'name']);
|
|
$paginate = $builder->paginate($params['pagesize']);
|
|
$stores = $paginate->toArray();
|
|
return ['has_more_pages' => $paginate->hasMorePages(), 'stores' => $stores['data']];
|
|
}
|
|
|
|
public function getHotKeywords($type)
|
|
{
|
|
$keywords = [
|
|
'goods' => ['酱油','油','生蚝'],
|
|
'store' => ['黄姐','王姐','黄姐蔬菜摊'],
|
|
];
|
|
|
|
return $keywords[$type];
|
|
}
|
|
|
|
public function do()
|
|
{
|
|
// TODO: Implement do() method.
|
|
}
|
|
|
|
public function check()
|
|
{
|
|
// TODO: Implement check() method.
|
|
}
|
|
|
|
public function undo()
|
|
{
|
|
// TODO: Implement undo() method.
|
|
}
|
|
}
|