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.

146 lines
5.9 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 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. switch ($tab) {
  55. case Tabs::APPLET_INDEX_RECOMMEND:
  56. $builder = $builder->orderBy(''.$goodsTable.'.sales', 'desc');
  57. break;
  58. case Tabs::APPLET_INDEX_NEW:
  59. $builder = $builder->orderBy(''.$goodsTable.'.created_at', 'desc');
  60. break;
  61. case Tabs::APPLET_INDEX_FRESH:
  62. $builder = $builder->orderBy(''.$goodsTable.'.price', 'asc');
  63. break;
  64. case Tabs::APPLET_INDEX_OFFICE:
  65. $categoryIds = [97,98];
  66. // 查询出三级分类
  67. $goodsCategoryIds = GoodsCategory::query()->whereIn('category_id', $categoryIds)->pluck('id');
  68. $builder = $builder->where(function ($query) use ($categoryIds, $goodsCategoryIds, $goodsTable) {
  69. $query->whereIn(''.$goodsTable.'.category_id', $categoryIds)
  70. ->orWhereIn(''.$goodsTable.'.goods_category_id', $goodsCategoryIds);
  71. });
  72. break;
  73. }
  74. $builder->select(''.$goodsTable.'.*')->addSelect([''.$goodsTable.'.sales as total_sales']);
  75. $paginate = $builder->groupBy(''.$goodsTable.'.id')->paginate($pagesize);
  76. $goods = $paginate->toArray();
  77. return $this->success(['has_more_pages' => $paginate->hasMorePages(), 'tab_data' => $goods['data']]);
  78. }
  79. /**
  80. * 获取推荐商品列表
  81. * 1、前端上传标识
  82. * recommend_search_goods
  83. * recommend_search_stores
  84. * recommend_user_index
  85. * 2、根据标识从Elasticsearch中获取商品IDs
  86. * 3、根据IDs获取商品数据
  87. * 4、返回数据,id、封面图、名称、原价、现价、库存、月销、tag、规格、购物车相关、商户id、商户avatar、商户名
  88. */
  89. public function getByTab()
  90. {
  91. $marketId = $this->request->input('market_id', 0);
  92. $tab = $this->request->input('tab', '');
  93. $storeTable = ApplicationContext::getContainer()->get(Store::class)->getTable();
  94. $goodsTable = ApplicationContext::getContainer()->get(Goods::class)->getTable();
  95. $builder = Goods::query()
  96. ->select(''.$goodsTable.'.*')
  97. ->addSelect(''.$goodsTable.'.sales as total_sales')
  98. ->join($storeTable,''.$storeTable.'.id', '=', ''.$goodsTable.'.store_id')
  99. ->where([
  100. ''.$storeTable.'.is_open' => StoreConstants::IS_OPEN_YES,
  101. ''.$storeTable.'.status' => StoreConstants::STATUS_PASS,
  102. ''.$storeTable.'.is_rest' => StoreConstants::IS_REST_NO
  103. ])
  104. ->with(['store'])
  105. ->where([
  106. ''.$goodsTable.'.market_id' => $marketId,
  107. ''.$goodsTable.'.on_sale' => GoodsConstants::ON_SALE_YES
  108. ])
  109. ->whereRaw(''.$goodsTable.'.deleted_at IS NULL')
  110. ->where(function ($query) use ($goodsTable) {
  111. $query->where(''.$goodsTable.'.inventory', '>', 0)->orWhere(''.$goodsTable.'.is_infinite', '=', 1);
  112. })
  113. // ->where('time1', '<=', date('H:i'))
  114. // ->where(function ($query) {
  115. // $query->where('time2', '>=', date('H:i'))
  116. // ->orWhere('time4', '>=', date('H:i'));
  117. // })
  118. ->inRandomOrder();
  119. switch ($tab) {
  120. case Tabs::RECOMMEND_FOR_GOODS_DETAIL:
  121. $builder = $builder->limit(10);
  122. break;
  123. default:
  124. $builder = $builder->limit(20);
  125. break;
  126. }
  127. $goods = $builder->get()->toArray();
  128. return $this->success(['has_more_pages' => false, 'tab_data' => $goods]);
  129. }
  130. }