|
|
@ -4,12 +4,15 @@ |
|
|
namespace App\Service\v3\Implementations; |
|
|
namespace App\Service\v3\Implementations; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use App\Constants\v3\Goods as GoodsConstants; |
|
|
use App\Model\v3\Category; |
|
|
use App\Model\v3\Category; |
|
|
use App\Model\v3\Goods; |
|
|
use App\Model\v3\Goods; |
|
|
use App\Model\v3\GoodsCategory; |
|
|
use App\Model\v3\GoodsCategory; |
|
|
use App\Model\v3\Store; |
|
|
use App\Model\v3\Store; |
|
|
use App\Service\v3\Interfaces\SearchServiceInterface; |
|
|
use App\Service\v3\Interfaces\SearchServiceInterface; |
|
|
use Hyperf\Paginator\Paginator; |
|
|
use Hyperf\Paginator\Paginator; |
|
|
|
|
|
use Hyperf\Utils\ApplicationContext; |
|
|
|
|
|
use App\Constants\v3\Store as StoreConstants; |
|
|
|
|
|
|
|
|
class SearchService implements SearchServiceInterface |
|
|
class SearchService implements SearchServiceInterface |
|
|
{ |
|
|
{ |
|
|
@ -17,25 +20,34 @@ class SearchService implements SearchServiceInterface |
|
|
public function doForGoods($params) |
|
|
public function doForGoods($params) |
|
|
{ |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
$storeTable = ApplicationContext::getContainer()->get(Store::class)->getTable(); |
|
|
|
|
|
$goodsTable = ApplicationContext::getContainer()->get(Goods::class)->getTable(); |
|
|
|
|
|
|
|
|
$builder = Goods::query() |
|
|
$builder = Goods::query() |
|
|
|
|
|
->join($storeTable,''.$storeTable.'.id', '=', ''.$goodsTable.'.store_id') |
|
|
|
|
|
->where([ |
|
|
|
|
|
''.$storeTable.'.is_open' => StoreConstants::IS_OPEN_YES, |
|
|
|
|
|
''.$storeTable.'.status' => StoreConstants::STATUS_PASS, |
|
|
|
|
|
''.$storeTable.'.is_rest' => StoreConstants::IS_REST_NO |
|
|
|
|
|
]) |
|
|
->with(['store']) |
|
|
->with(['store']) |
|
|
->where(['market_id' => $params['market_id']]) |
|
|
|
|
|
->where(function ($query) { |
|
|
|
|
|
$query->where('inventory', '>', 0)->orWhere('is_infinite', '=', 1); |
|
|
|
|
|
});; |
|
|
|
|
|
|
|
|
->where([''.$goodsTable.'.market_id' => $params['market_id']]) |
|
|
|
|
|
->where(function ($query) use ($goodsTable) { |
|
|
|
|
|
$query->where(''.$goodsTable.'.inventory', '>', 0)->orWhere(''.$goodsTable.'.is_infinite', '=', 1); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
if (isset($params['store_id']) && $params['store_id']) { |
|
|
if (isset($params['store_id']) && $params['store_id']) { |
|
|
$builder->where(['store_id' => $params['store_id']]); |
|
|
|
|
|
|
|
|
$builder->where([''.$goodsTable.'.store_id' => $params['store_id']]); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (isset($params['type_id']) && $params['type_id']) { |
|
|
if (isset($params['type_id']) && $params['type_id']) { |
|
|
$typeIds = explode(',', $params['type_id']); |
|
|
$typeIds = explode(',', $params['type_id']); |
|
|
$builder->whereIn('category_id', $typeIds); |
|
|
|
|
|
|
|
|
$builder->whereIn(''.$goodsTable.'.category_id', $typeIds); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (isset($params['goods_category_ids']) && $params['goods_category_ids']) { |
|
|
if (isset($params['goods_category_ids']) && $params['goods_category_ids']) { |
|
|
$typeIds = explode(',', $params['goods_category_ids']); |
|
|
$typeIds = explode(',', $params['goods_category_ids']); |
|
|
$builder->whereIn('goods_category_id', $typeIds); |
|
|
|
|
|
|
|
|
$builder->whereIn(''.$goodsTable.'.goods_category_id', $typeIds); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (isset($params['keyword']) && $params['keyword']) { |
|
|
if (isset($params['keyword']) && $params['keyword']) { |
|
|
@ -43,10 +55,10 @@ class SearchService implements SearchServiceInterface |
|
|
$categoryIds = Category::query()->where('title', 'like', '%'.$params['keyword'].'%')->pluck('id'); |
|
|
$categoryIds = Category::query()->where('title', 'like', '%'.$params['keyword'].'%')->pluck('id'); |
|
|
// 三级分类
|
|
|
// 三级分类
|
|
|
$goodsCategoryIds = GoodsCategory::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); |
|
|
|
|
|
|
|
|
$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); |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -65,31 +77,41 @@ class SearchService implements SearchServiceInterface |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
$builder->select(['*'])->addSelect(['sales as total_sales']); |
|
|
|
|
|
$paginate = $builder->paginate($params['pagesize']); |
|
|
|
|
|
|
|
|
$builder->select(''.$goodsTable.'.*')->addSelect([''.$goodsTable.'.sales as total_sales']); |
|
|
|
|
|
$paginate = $builder->groupBy(''.$goodsTable.'.id')->paginate($params['pagesize']); |
|
|
$goods = $paginate->toArray(); |
|
|
$goods = $paginate->toArray(); |
|
|
return ['has_more_pages' => $paginate->hasMorePages(), 'goods' => $goods['data']]; |
|
|
return ['has_more_pages' => $paginate->hasMorePages(), 'goods' => $goods['data']]; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public function doForStores($params) |
|
|
public function doForStores($params) |
|
|
{ |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
$storeTable = ApplicationContext::getContainer()->get(Store::class)->getTable(); |
|
|
|
|
|
$goodsTable = ApplicationContext::getContainer()->get(Goods::class)->getTable(); |
|
|
|
|
|
|
|
|
$builder = Store::query() |
|
|
$builder = Store::query() |
|
|
->with(['goods' => function($query) { |
|
|
|
|
|
return $query->limit(5) |
|
|
|
|
|
->select(['*'])->addSelect(['sales as total_sales']); |
|
|
|
|
|
}]) |
|
|
|
|
|
->where(['market_id' => $params['market_id']]); |
|
|
|
|
|
|
|
|
->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); |
|
|
|
|
|
}) |
|
|
|
|
|
// ->with(['goods' => function($query) {
|
|
|
|
|
|
// return $query->select(['*'])->addSelect(['sales as total_sales']);
|
|
|
|
|
|
// }])
|
|
|
|
|
|
->where([''.$storeTable.'.market_id' => $params['market_id']]); |
|
|
|
|
|
|
|
|
if (isset($params['store_id']) && $params['store_id']) { |
|
|
if (isset($params['store_id']) && $params['store_id']) { |
|
|
$builder->where(['store_id' => $params['store_id']]); |
|
|
|
|
|
|
|
|
$builder->where([''.$storeTable.'.store_id' => $params['store_id']]); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (isset($params['type_id']) && $params['type_id']) { |
|
|
if (isset($params['type_id']) && $params['type_id']) { |
|
|
$builder->where(['category_id' => $params['type_id']]); |
|
|
|
|
|
|
|
|
$builder->where([''.$storeTable.'.category_id' => $params['type_id']]); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (isset($params['keyword']) && $params['keyword']) { |
|
|
if (isset($params['keyword']) && $params['keyword']) { |
|
|
$builder->where('name', 'like', '%'.$params['keyword'].'%'); |
|
|
|
|
|
|
|
|
$builder->where(''.$storeTable.'.name', 'like', '%'.$params['keyword'].'%'); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (isset($params['order_by']) && $params['order_by']) { |
|
|
if (isset($params['order_by']) && $params['order_by']) { |
|
|
@ -103,10 +125,13 @@ class SearchService implements SearchServiceInterface |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
$paginate = $builder->groupBy(''.$storeTable.'.id')->paginate($params['pagesize']); |
|
|
|
|
|
$stores = $paginate->map(function ($item, $key) { |
|
|
|
|
|
$item->goods; |
|
|
|
|
|
return $item; |
|
|
|
|
|
})->all(); |
|
|
|
|
|
|
|
|
$paginate = $builder->paginate($params['pagesize']); |
|
|
|
|
|
$stores = $paginate->toArray(); |
|
|
|
|
|
return ['has_more_pages' => $paginate->hasMorePages(), 'stores' => $stores['data']]; |
|
|
|
|
|
|
|
|
return ['has_more_pages' => $paginate->hasMorePages(), 'stores' => $stores]; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public function getHotKeywords($type) |
|
|
public function getHotKeywords($type) |
|
|
|