From cc5fdabd410950a36a91073dc49b466d4c001756 Mon Sep 17 00:00:00 2001 From: weigang Date: Mon, 14 Sep 2020 18:10:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=95=86=E5=93=81=E8=BF=87=E6=BB=A4=E5=BA=97?= =?UTF-8?q?=E9=93=BA=E4=BC=91=E6=81=AF=E7=9A=84=EF=BC=8C=E5=95=86=E6=88=B7?= =?UTF-8?q?=E8=BF=87=E6=BB=A4=E6=B2=A1=E6=9C=89=E5=95=86=E5=93=81=E7=9A=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Constants/v3/Store.php | 5 ++ app/Model/v3/Goods.php | 8 +- app/Model/v3/GoodsActivity.php | 2 +- app/Model/v3/Store.php | 8 +- .../v3/Implementations/SearchService.php | 73 +++++++++++++------ 5 files changed, 63 insertions(+), 33 deletions(-) diff --git a/app/Constants/v3/Store.php b/app/Constants/v3/Store.php index 50b585b..62bf11d 100644 --- a/app/Constants/v3/Store.php +++ b/app/Constants/v3/Store.php @@ -41,4 +41,9 @@ class Store extends AbstractConstants */ const STATUS_EXPIRED = 4; + /** + * @Message("商家已休息") + */ + const IS_REST_NO = 0; + } \ No newline at end of file diff --git a/app/Model/v3/Goods.php b/app/Model/v3/Goods.php index f4aa2aa..d6a31bc 100644 --- a/app/Model/v3/Goods.php +++ b/app/Model/v3/Goods.php @@ -69,23 +69,23 @@ class Goods extends Model { parent::boot(); self::addGlobalScope('normal', function (Builder $builder) { - $builder->where(['on_sale' => GoodsConstants::ON_SALE_YES]); + $builder->where([$this->getTable().'.on_sale' => GoodsConstants::ON_SALE_YES]); }); } public function scopeOrderByDefault($query, $sort) { - return $query->orderBy('sort', $sort)->orderBy('id', $sort); + return $query->orderBy($this->getTable().'.sort', $sort)->orderBy($this->getTable().'.id', $sort); } public function scopeOrderBySales($query, $sort) { - return $query->orderBy('sales', $sort); + return $query->orderBy($this->getTable().'.sales', $sort); } public function scopeOrderByPrice($query, $sort) { - return $query->orderBy('price', $sort); + return $query->orderBy($this->getTable().'.price', $sort); } public function getCoverImgAttribute($value) diff --git a/app/Model/v3/GoodsActivity.php b/app/Model/v3/GoodsActivity.php index f341712..3257026 100644 --- a/app/Model/v3/GoodsActivity.php +++ b/app/Model/v3/GoodsActivity.php @@ -57,7 +57,7 @@ class GoodsActivity extends Model { parent::boot(); self::addGlobalScope('normal', function (Builder $builder) { - $builder->where(['on_sale' => GoodsConstants::ON_SALE_YES]); + $builder->where([$this->getTable().'.on_sale' => GoodsConstants::ON_SALE_YES]); }); } diff --git a/app/Model/v3/Store.php b/app/Model/v3/Store.php index cbda028..93d5dad 100644 --- a/app/Model/v3/Store.php +++ b/app/Model/v3/Store.php @@ -44,18 +44,18 @@ class Store extends Model { parent::boot(); self::addGlobalScope('normal', function (Builder $builder) { - $builder->where(['is_open' => StoreConstants::IS_OPEN_YES, 'status' => StoreConstants::STATUS_PASS]); + $builder->where([$this->getTable().'.is_open' => StoreConstants::IS_OPEN_YES, $this->getTable().'.status' => StoreConstants::STATUS_PASS]); }); } public function scopeOrderByDefault($query, $sort) { - return $query->orderBy('sort', $sort)->orderBy('id', $sort); + return $query->orderBy($this->getTable().'.sort', $sort)->orderBy($this->getTable().'.id', $sort); } public function scopeOrderBySales($query, $sort) { - return $query->orderBy('sales', $sort); + return $query->orderBy($this->getTable().'.sales', $sort); } public function getMonthSalesAttribute() { @@ -65,7 +65,7 @@ class Store extends Model public function goods() { - return $this->hasMany(Goods::class, 'store_id', 'id'); + return $this->hasMany(Goods::class, 'store_id', 'id')->limit(5); } public function shoppingCart() diff --git a/app/Service/v3/Implementations/SearchService.php b/app/Service/v3/Implementations/SearchService.php index 6127c98..7b260f3 100644 --- a/app/Service/v3/Implementations/SearchService.php +++ b/app/Service/v3/Implementations/SearchService.php @@ -4,12 +4,15 @@ 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 { @@ -17,25 +20,34 @@ 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(['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']) { - $builder->where(['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('category_id', $typeIds); + $builder->whereIn(''.$goodsTable.'.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); + $builder->whereIn(''.$goodsTable.'.goods_category_id', $typeIds); } if (isset($params['keyword']) && $params['keyword']) { @@ -43,10 +55,10 @@ class SearchService implements SearchServiceInterface $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); + $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(); 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() - ->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']) { - $builder->where(['store_id' => $params['store_id']]); + $builder->where([''.$storeTable.'.store_id' => $params['store_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']) { - $builder->where('name', 'like', '%'.$params['keyword'].'%'); + $builder->where(''.$storeTable.'.name', 'like', '%'.$params['keyword'].'%'); } if (isset($params['order_by']) && $params['order_by']) { @@ -103,10 +125,13 @@ class SearchService implements SearchServiceInterface 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)