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.

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