You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
6.1 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. namespace App\Controller\v3;
  3. use App\Constants\v3\Goods as GoodsConstants;
  4. use App\Constants\v3\Store as StoreConstants;
  5. use App\Constants\v3\Tabs;
  6. use App\Controller\BaseController;
  7. use App\Model\v3\Goods;
  8. use App\Model\v3\GoodsCategory;
  9. use App\Model\v3\Store;
  10. use Hyperf\Utils\ApplicationContext;
  11. /**
  12. * 推荐商品相关
  13. * Class GoodsRecommend
  14. * @package App\Controller\v3
  15. */
  16. class GoodsRecommendController extends BaseController
  17. {
  18. /**
  19. * 获取首页tabs推荐商品列表
  20. * 1、前端上传tab标识
  21. * 2、根据tab标识从Elasticsearch中获取商品IDs
  22. * 3、根据IDs获取商品数据
  23. * 4、返回数据,id、封面图、名称、原价、现价、库存、月销、tag、规格、购物车相关、商户id、商户avatar、商户名
  24. */
  25. public function getByTabsForAppletIndex()
  26. {
  27. $tab = $this->request->input('tab', '');
  28. $marketId = $this->request->input('market_id', 0);
  29. $page = $this->request->input('page', 1);
  30. $pagesize = $this->request->input('pagesize', 10);
  31. $storeTable = ApplicationContext::getContainer()->get(Store::class)->getTable();
  32. $goodsTable = ApplicationContext::getContainer()->get(Goods::class)->getTable();
  33. $builder = Goods::query()
  34. ->join($storeTable,''.$storeTable.'.id', '=', ''.$goodsTable.'.store_id')
  35. ->where([
  36. ''.$storeTable.'.is_open' => StoreConstants::IS_OPEN_YES,
  37. ''.$storeTable.'.status' => StoreConstants::STATUS_PASS,
  38. ''.$storeTable.'.is_rest' => StoreConstants::IS_REST_NO
  39. ])
  40. ->with(['store'])
  41. ->where([
  42. ''.$goodsTable.'.market_id' => $marketId,
  43. ''.$goodsTable.'.on_sale' => GoodsConstants::ON_SALE_YES
  44. ])
  45. ->whereRaw(''.$goodsTable.'.deleted_at IS NULL')
  46. ->where(function ($query) use ($goodsTable) {
  47. $query->where(''.$goodsTable.'.inventory', '>', 0)->orWhere(''.$goodsTable.'.is_infinite', '=', 1);
  48. })
  49. ->where('time1', '<=', date('H:i'))
  50. ->where(function ($query) {
  51. $query->where('time2', '>=', date('H:i'))
  52. ->orWhere('time4', '>=', date('H:i'));
  53. });
  54. // $builder = Goods::query()->with('store')
  55. // ->where('market_id', $marketId)
  56. // ->where(function ($query) {
  57. // $query->where('inventory', '>', 0)->orWhere('is_infinite', '=', 1);
  58. // });
  59. switch ($tab) {
  60. case Tabs::APPLET_INDEX_RECOMMEND:
  61. $builder = $builder->orderBy(''.$goodsTable.'.sales', 'desc');
  62. break;
  63. case Tabs::APPLET_INDEX_NEW:
  64. $builder = $builder->orderBy(''.$goodsTable.'.created_at', 'desc');
  65. break;
  66. case Tabs::APPLET_INDEX_FRESH:
  67. $builder = $builder->orderBy(''.$goodsTable.'.price', 'asc');
  68. break;
  69. case Tabs::APPLET_INDEX_OFFICE:
  70. $categoryIds = [97,98];
  71. // 查询出三级分类
  72. $goodsCategoryIds = GoodsCategory::query()->whereIn('category_id', $categoryIds)->pluck('id');
  73. $builder = $builder->where(function ($query) use ($categoryIds, $goodsCategoryIds, $goodsTable) {
  74. $query->whereIn(''.$goodsTable.'.category_id', $categoryIds)
  75. ->orWhereIn(''.$goodsTable.'.goods_category_id', $goodsCategoryIds);
  76. });
  77. break;
  78. }
  79. $builder->select(''.$goodsTable.'.*')->addSelect([''.$goodsTable.'.sales as total_sales']);
  80. $paginate = $builder->groupBy(''.$goodsTable.'.id')->paginate($pagesize);
  81. $goods = $paginate->toArray();
  82. return $this->success(['has_more_pages' => $paginate->hasMorePages(), 'tab_data' => $goods['data']]);
  83. }
  84. /**
  85. * 获取推荐商品列表
  86. * 1、前端上传标识
  87. * recommend_search_goods
  88. * recommend_search_stores
  89. * recommend_user_index
  90. * 2、根据标识从Elasticsearch中获取商品IDs
  91. * 3、根据IDs获取商品数据
  92. * 4、返回数据,id、封面图、名称、原价、现价、库存、月销、tag、规格、购物车相关、商户id、商户avatar、商户名
  93. */
  94. public function getByTab()
  95. {
  96. $marketId = $this->request->input('market_id', 0);
  97. $storeTable = ApplicationContext::getContainer()->get(Store::class)->getTable();
  98. $goodsTable = ApplicationContext::getContainer()->get(Goods::class)->getTable();
  99. $goods = Goods::query()
  100. ->select(''.$goodsTable.'.*')
  101. ->addSelect(''.$goodsTable.'.sales as total_sales')
  102. ->join($storeTable,''.$storeTable.'.id', '=', ''.$goodsTable.'.store_id')
  103. ->where([
  104. ''.$storeTable.'.is_open' => StoreConstants::IS_OPEN_YES,
  105. ''.$storeTable.'.status' => StoreConstants::STATUS_PASS,
  106. ''.$storeTable.'.is_rest' => StoreConstants::IS_REST_NO
  107. ])
  108. ->with(['store'])
  109. ->where([
  110. ''.$goodsTable.'.market_id' => $marketId,
  111. ''.$goodsTable.'.on_sale' => GoodsConstants::ON_SALE_YES
  112. ])
  113. ->whereRaw(''.$goodsTable.'.deleted_at IS NULL')
  114. ->where(function ($query) use ($goodsTable) {
  115. $query->where(''.$goodsTable.'.inventory', '>', 0)->orWhere(''.$goodsTable.'.is_infinite', '=', 1);
  116. })
  117. ->where('time1', '<=', date('H:i'))
  118. ->where(function ($query) {
  119. $query->where('time2', '>=', date('H:i'))
  120. ->orWhere('time4', '>=', date('H:i'));
  121. })
  122. ->inRandomOrder()
  123. ->limit(20)
  124. ->get()->toArray();
  125. // $goods = Goods::query()
  126. // ->with(['store'])
  127. // ->where('market_id', $marketId)
  128. // ->where(function ($query) {
  129. // $query->where('inventory', '>', 0)->orWhere('is_infinite', '=', 1);
  130. // })
  131. // ->inRandomOrder()
  132. // ->limit(20)
  133. // ->get()->toArray();
  134. return $this->success(['has_more_pages' => false, 'tab_data' => $goods]);
  135. }
  136. }