Browse Source

商品过滤空商户

master
weigang 5 years ago
parent
commit
2aee6f8996
  1. 68
      app/Controller/v3/GoodsRecommendController.php

68
app/Controller/v3/GoodsRecommendController.php

@ -2,10 +2,13 @@
namespace App\Controller\v3; namespace App\Controller\v3;
use App\Constants\v3\Store as StoreConstants;
use App\Constants\v3\Tabs; use App\Constants\v3\Tabs;
use App\Controller\BaseController; use App\Controller\BaseController;
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 Hyperf\Utils\ApplicationContext;
/** /**
* 推荐商品相关 * 推荐商品相关
@ -29,34 +32,51 @@ class GoodsRecommendController extends BaseController
$page = $this->request->input('page', 1); $page = $this->request->input('page', 1);
$pagesize = $this->request->input('pagesize', 10); $pagesize = $this->request->input('pagesize', 10);
$builder = Goods::query()->with('store')
->where('market_id', $marketId)
->where(function ($query) {
$query->where('inventory', '>', 0)->orWhere('is_infinite', '=', 1);
$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' => $marketId])
->where(function ($query) use ($goodsTable) {
$query->where(''.$goodsTable.'.inventory', '>', 0)->orWhere(''.$goodsTable.'.is_infinite', '=', 1);
}); });
// $builder = Goods::query()->with('store')
// ->where('market_id', $marketId)
// ->where(function ($query) {
// $query->where('inventory', '>', 0)->orWhere('is_infinite', '=', 1);
// });
switch ($tab) { switch ($tab) {
case Tabs::APPLET_INDEX_RECOMMEND: case Tabs::APPLET_INDEX_RECOMMEND:
$builder = $builder->orderBy('sales', 'desc');
$builder = $builder->orderBy(''.$goodsTable.'.sales', 'desc');
break; break;
case Tabs::APPLET_INDEX_NEW: case Tabs::APPLET_INDEX_NEW:
$builder = $builder->orderBy('created_at', 'desc');
$builder = $builder->orderBy(''.$goodsTable.'.created_at', 'desc');
break; break;
case Tabs::APPLET_INDEX_FRESH: case Tabs::APPLET_INDEX_FRESH:
$builder = $builder->orderBy('price', 'asc');
$builder = $builder->orderBy(''.$goodsTable.'.price', 'asc');
break; break;
case Tabs::APPLET_INDEX_OFFICE: case Tabs::APPLET_INDEX_OFFICE:
$categoryIds = [97,98]; $categoryIds = [97,98];
// 查询出三级分类 // 查询出三级分类
$goodsCategoryIds = GoodsCategory::query()->whereIn('category_id', $categoryIds)->pluck('id'); $goodsCategoryIds = GoodsCategory::query()->whereIn('category_id', $categoryIds)->pluck('id');
$builder = $builder->where(function ($query) use ($categoryIds, $goodsCategoryIds) {
$query->whereIn('category_id', $categoryIds)
->orWhereIn('goods_category_id', $goodsCategoryIds);
$builder = $builder->where(function ($query) use ($categoryIds, $goodsCategoryIds, $goodsTable) {
$query->whereIn(''.$goodsTable.'.category_id', $categoryIds)
->orWhereIn(''.$goodsTable.'.goods_category_id', $goodsCategoryIds);
}); });
break; break;
} }
$paginate = $builder->paginate($pagesize);
$builder->select(''.$goodsTable.'.*')->addSelect([''.$goodsTable.'.sales as total_sales']);
$paginate = $builder->groupBy(''.$goodsTable.'.id')->paginate($pagesize);
$goods = $paginate->toArray(); $goods = $paginate->toArray();
return $this->success(['has_more_pages' => $paginate->hasMorePages(), 'tab_data' => $goods['data']]); return $this->success(['has_more_pages' => $paginate->hasMorePages(), 'tab_data' => $goods['data']]);
@ -75,15 +95,35 @@ class GoodsRecommendController extends BaseController
public function getByTab() public function getByTab()
{ {
$marketId = $this->request->input('market_id', 0); $marketId = $this->request->input('market_id', 0);
$storeTable = ApplicationContext::getContainer()->get(Store::class)->getTable();
$goodsTable = ApplicationContext::getContainer()->get(Goods::class)->getTable();
$goods = Goods::query() $goods = 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', $marketId)
->where(function ($query) {
$query->where('inventory', '>', 0)->orWhere('is_infinite', '=', 1);
->where([''.$goodsTable.'.market_id' => $marketId])
->where(function ($query) use ($goodsTable) {
$query->where(''.$goodsTable.'.inventory', '>', 0)->orWhere(''.$goodsTable.'.is_infinite', '=', 1);
}) })
->inRandomOrder() ->inRandomOrder()
->limit(20) ->limit(20)
->get()->toArray(); ->get()->toArray();
// $goods = Goods::query()
// ->with(['store'])
// ->where('market_id', $marketId)
// ->where(function ($query) {
// $query->where('inventory', '>', 0)->orWhere('is_infinite', '=', 1);
// })
// ->inRandomOrder()
// ->limit(20)
// ->get()->toArray();
return $this->success(['has_more_pages' => false, 'tab_data' => $goods]); return $this->success(['has_more_pages' => false, 'tab_data' => $goods]);
} }

Loading…
Cancel
Save