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.
212 lines
8.1 KiB
212 lines
8.1 KiB
<?php
|
|
|
|
|
|
namespace App\Service\v3\Implementations;
|
|
|
|
|
|
use App\Constants\v3\Goods as GoodsConstants;
|
|
use App\Model\v3\Category;
|
|
use App\Model\v3\Goods;
|
|
use App\Model\v3\GoodsCategory;
|
|
use App\Model\v3\Market;
|
|
use App\Model\v3\Store;
|
|
use App\Service\v3\Interfaces\SearchServiceInterface;
|
|
use Hyperf\Paginator\Paginator;
|
|
use Hyperf\Utils\ApplicationContext;
|
|
use App\Constants\v3\Store as StoreConstants;
|
|
|
|
class SearchService implements SearchServiceInterface
|
|
{
|
|
|
|
public function doForGoods($params)
|
|
{
|
|
|
|
$params['pagesize'] = $params['pagesize'] ?: 10;
|
|
|
|
$storeTable = ApplicationContext::getContainer()->get(Store::class)->getTable();
|
|
$goodsTable = ApplicationContext::getContainer()->get(Goods::class)->getTable();
|
|
|
|
$builder = Goods::query()
|
|
->join($storeTable,''.$storeTable.'.id', '=', ''.$goodsTable.'.store_id')
|
|
->where([
|
|
''.$storeTable.'.is_open' => StoreConstants::IS_OPEN_YES,
|
|
''.$storeTable.'.status' => StoreConstants::STATUS_PASS,
|
|
]);
|
|
if(!isset($params['frompage']) || $params['frompage'] != 'zh_cjdianc/pages/takeout/takeoutindex'){
|
|
$builder->where(''.$storeTable.'.is_rest',StoreConstants::IS_REST_NO);
|
|
}
|
|
$builder->where([
|
|
''.$goodsTable.'.market_id' => $params['market_id'],
|
|
''.$goodsTable.'.on_sale' => GoodsConstants::ON_SALE_YES
|
|
])
|
|
->whereRaw(''.$goodsTable.'.deleted_at IS NULL')
|
|
->with(['store'])
|
|
->where(function ($query) use ($goodsTable) {
|
|
$query->where(''.$goodsTable.'.inventory', '>', 0)->orWhere(''.$goodsTable.'.is_infinite', '=', 1);
|
|
})
|
|
/*->where('time1', '<=', date('H:i'))
|
|
->where(function ($query) {
|
|
$query->where('time2', '>=', date('H:i'))
|
|
->orWhere('time4', '>=', date('H:i'));
|
|
})*/;
|
|
|
|
if (isset($params['store_id']) && $params['store_id']) {
|
|
$builder->where([''.$goodsTable.'.store_id' => $params['store_id']]);
|
|
}
|
|
|
|
if (isset($params['type_id']) && $params['type_id']) {
|
|
$typeIds = explode(',', $params['type_id']);
|
|
$builder->whereIn(''.$goodsTable.'.category_id', $typeIds);
|
|
}
|
|
|
|
if (isset($params['goods_category_ids']) && $params['goods_category_ids']) {
|
|
$typeIds = explode(',', $params['goods_category_ids']);
|
|
$builder->where(function ($query) use ($goodsTable, $typeIds) {
|
|
$query->whereIn(''.$goodsTable.'.goods_category_id', $typeIds)
|
|
->orWhere(''.$goodsTable.'.goods_category_id', 0);
|
|
});
|
|
}
|
|
|
|
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, $goodsTable) {
|
|
$query->where(''.$goodsTable.'.name', 'like', '%'.$params['keyword'].'%')
|
|
->orWhereIn(''.$goodsTable.'.category_id', $categoryIds)
|
|
->orWhereIn(''.$goodsTable.'.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(''.$goodsTable.'.*')->addSelect([''.$goodsTable.'.sales as total_sales']);
|
|
|
|
// Feature/搜索商品也展示出三级分类
|
|
$allGoodsCategoryIds = $builder->groupBy(''.$goodsTable.'.goods_category_id')->pluck('goods_category_id');
|
|
$goodsCategories = GoodsCategory::query()
|
|
->whereIn('id', $allGoodsCategoryIds)
|
|
->get()
|
|
->toArray();
|
|
$goodsCategoryIds = implode(',', array_values(array_column($goodsCategories, 'id')));
|
|
|
|
$paginate = $builder->groupBy(''.$goodsTable.'.id')->inRandomOrder()->paginate($params['pagesize']);
|
|
$goods = $paginate->toArray();
|
|
return [
|
|
'has_more_pages' => $paginate->hasMorePages(),
|
|
'goods' => $goods['data'],
|
|
'category' => ['goods_category' => $goodsCategories, 'goods_category_ids' => $goodsCategoryIds]
|
|
];
|
|
}
|
|
|
|
public function doForStores($params)
|
|
{
|
|
|
|
$params['pagesize'] = $params['pagesize'] ?: 10;
|
|
|
|
$storeTable = ApplicationContext::getContainer()->get(Store::class)->getTable();
|
|
$goodsTable = ApplicationContext::getContainer()->get(Goods::class)->getTable();
|
|
|
|
$builder = Store::query()
|
|
->select(''.$storeTable.'.*')
|
|
->join($goodsTable,''.$storeTable.'.id', '=', ''.$goodsTable.'.store_id')
|
|
->where([''.$goodsTable.'.on_sale' => GoodsConstants::ON_SALE_YES])
|
|
->where([''.$goodsTable.'.market_id' => $params['market_id']])
|
|
->where(function ($query) use ($goodsTable) {
|
|
$query->where(''.$goodsTable.'.inventory', '>', 0)->orWhere(''.$goodsTable.'.is_infinite', '=', 1);
|
|
})
|
|
->whereRaw(''.$goodsTable.'.deleted_at IS NULL')
|
|
->where([''.$storeTable.'.market_id' => $params['market_id']])
|
|
->orderBy($storeTable.'.is_rest','asc');
|
|
/*->where('time1', '<=', date('H:i'))
|
|
->where(function ($query) {
|
|
$query->where('time2', '>=', date('H:i'))
|
|
->orWhere('time4', '>=', date('H:i'));
|
|
})*/
|
|
|
|
if (isset($params['store_id']) && $params['store_id']) {
|
|
$builder->where([''.$storeTable.'.store_id' => $params['store_id']]);
|
|
}
|
|
|
|
if (isset($params['type_id']) && $params['type_id']) {
|
|
$builder->where([''.$storeTable.'.category_id' => $params['type_id']]);
|
|
}
|
|
|
|
if (isset($params['keyword']) && $params['keyword']) {
|
|
$builder->where(''.$storeTable.'.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;
|
|
}
|
|
|
|
}
|
|
|
|
$paginate = $builder->groupBy(''.$storeTable.'.id')->inRandomOrder()->paginate($params['pagesize']);
|
|
$stores = $paginate->map(function ($item, $key) {
|
|
$item->goods;
|
|
return $item;
|
|
})->all();
|
|
|
|
return ['has_more_pages' => $paginate->hasMorePages(), 'stores' => $stores];
|
|
}
|
|
|
|
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.
|
|
}
|
|
|
|
public function getStoresForPersonnel($marketId, $keywords, $page = 1, $pagesize = 10)
|
|
{
|
|
$market = Market::query()->withoutGlobalScope('normal')->find($marketId);
|
|
$builder = Store::query()->where('market_id',$marketId);
|
|
if(!is_null($keywords)){
|
|
$builder->where('name','like','%'.$keywords.'%');
|
|
}
|
|
$paginate = $builder->orderBy('is_rest', 'asc')->paginate($pagesize);
|
|
$stores = $paginate->toArray();
|
|
$market->stores = $stores['data'];
|
|
return ['has_more_pages' => $paginate->hasMorePages(), 'market' => $market];
|
|
}
|
|
}
|