Browse Source

商品过滤店铺休息的,商户过滤没有商品的

master
weigang 5 years ago
parent
commit
cc5fdabd41
  1. 5
      app/Constants/v3/Store.php
  2. 8
      app/Model/v3/Goods.php
  3. 2
      app/Model/v3/GoodsActivity.php
  4. 8
      app/Model/v3/Store.php
  5. 73
      app/Service/v3/Implementations/SearchService.php

5
app/Constants/v3/Store.php

@ -41,4 +41,9 @@ class Store extends AbstractConstants
*/
const STATUS_EXPIRED = 4;
/**
* @Message("商家已休息")
*/
const IS_REST_NO = 0;
}

8
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)

2
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]);
});
}

8
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()

73
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)

Loading…
Cancel
Save