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.
 
 

124 lines
3.7 KiB

<?php
namespace App\Service\v3\Implementations;
use App\Model\v3\Goods;
use App\Model\v3\Store;
use Hyperf\Paginator\Paginator;
class SearchService implements \App\Service\v3\Interfaces\SearchServiceInterface
{
public function doForGoods($params)
{
$builder = Goods::query()
->with(['store' => function($query) {
return $query->select(['id', 'logo', 'name']);
}])
->where(['market_id' => $params['market_id']])
->where(function ($query) {
$query->where('inventory', '>', 0)->orWhere('is_infinite', '=', 1);
});;
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']) {
$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;
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.
}
}