|
|
<?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\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) {
$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, ''.$storeTable.'.is_rest' => StoreConstants::IS_REST_NO ]) ->with(['store']) ->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']) { $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->whereIn(''.$goodsTable.'.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, $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']); $paginate = $builder->groupBy(''.$goodsTable.'.id')->paginate($params['pagesize']); $goods = $paginate->toArray(); return ['has_more_pages' => $paginate->hasMorePages(), 'goods' => $goods['data']]; }
public function doForStores($params) {
$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); }) // ->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']) { $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')->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.
}}
|