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.

197 lines
7.5 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
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\Service\v3\Implementations;
  3. use App\Constants\v3\Goods as GoodsConstants;
  4. use App\Model\v3\Category;
  5. use App\Model\v3\Goods;
  6. use App\Model\v3\GoodsCategory;
  7. use App\Model\v3\Store;
  8. use App\Service\v3\Interfaces\SearchServiceInterface;
  9. use Hyperf\Paginator\Paginator;
  10. use Hyperf\Utils\ApplicationContext;
  11. use App\Constants\v3\Store as StoreConstants;
  12. class SearchService implements SearchServiceInterface
  13. {
  14. public function doForGoods($params)
  15. {
  16. $params['pagesize'] = $params['pagesize'] ?: 10;
  17. $storeTable = ApplicationContext::getContainer()->get(Store::class)->getTable();
  18. $goodsTable = ApplicationContext::getContainer()->get(Goods::class)->getTable();
  19. $builder = Goods::query()
  20. ->join($storeTable,''.$storeTable.'.id', '=', ''.$goodsTable.'.store_id')
  21. ->where([
  22. ''.$storeTable.'.is_open' => StoreConstants::IS_OPEN_YES,
  23. ''.$storeTable.'.status' => StoreConstants::STATUS_PASS,
  24. ]);
  25. if(!isset($params['frompage']) || $params['frompage'] != 'zh_cjdianc/pages/takeout/takeoutindex'){
  26. $builder->where(''.$storeTable.'.is_rest',StoreConstants::IS_REST_NO);
  27. }
  28. $builder->where([
  29. ''.$goodsTable.'.market_id' => $params['market_id'],
  30. ''.$goodsTable.'.on_sale' => GoodsConstants::ON_SALE_YES
  31. ])
  32. ->whereRaw(''.$goodsTable.'.deleted_at IS NULL')
  33. ->with(['store'])
  34. ->where(function ($query) use ($goodsTable) {
  35. $query->where(''.$goodsTable.'.inventory', '>', 0)->orWhere(''.$goodsTable.'.is_infinite', '=', 1);
  36. })
  37. /*->where('time1', '<=', date('H:i'))
  38. ->where(function ($query) {
  39. $query->where('time2', '>=', date('H:i'))
  40. ->orWhere('time4', '>=', date('H:i'));
  41. })*/;
  42. if (isset($params['store_id']) && $params['store_id']) {
  43. $builder->where([''.$goodsTable.'.store_id' => $params['store_id']]);
  44. }
  45. if (isset($params['type_id']) && $params['type_id']) {
  46. $typeIds = explode(',', $params['type_id']);
  47. $builder->whereIn(''.$goodsTable.'.category_id', $typeIds);
  48. }
  49. if (isset($params['goods_category_ids']) && $params['goods_category_ids']) {
  50. $typeIds = explode(',', $params['goods_category_ids']);
  51. $builder->where(function ($query) use ($goodsTable, $typeIds) {
  52. $query->whereIn(''.$goodsTable.'.goods_category_id', $typeIds)
  53. ->orWhere(''.$goodsTable.'.goods_category_id', 0);
  54. });
  55. }
  56. if (isset($params['keyword']) && $params['keyword']) {
  57. // 二级分类
  58. $categoryIds = Category::query()->where('title', 'like', '%'.$params['keyword'].'%')->pluck('id');
  59. // 三级分类
  60. $goodsCategoryIds = GoodsCategory::query()->where('title', 'like', '%'.$params['keyword'].'%')->pluck('id');
  61. $builder->where(function ($query) use ($params, $categoryIds, $goodsCategoryIds, $goodsTable) {
  62. $query->where(''.$goodsTable.'.name', 'like', '%'.$params['keyword'].'%')
  63. ->orWhereIn(''.$goodsTable.'.category_id', $categoryIds)
  64. ->orWhereIn(''.$goodsTable.'.goods_category_id', $goodsCategoryIds);
  65. });
  66. }
  67. if (isset($params['order_by']) && $params['order_by']) {
  68. $sort = $params['sort'] ?? 'desc';
  69. switch ($params['order_by']) {
  70. case 'sales':
  71. $builder->orderBySales($sort);
  72. break;
  73. case 'price':
  74. $builder->orderByPrice($sort);
  75. break;
  76. default:
  77. $builder->orderByDefault($sort);
  78. break;
  79. }
  80. }
  81. $builder->select(''.$goodsTable.'.*')->addSelect([''.$goodsTable.'.sales as total_sales']);
  82. // Feature/搜索商品也展示出三级分类
  83. $allGoodsCategoryIds = $builder->groupBy(''.$goodsTable.'.goods_category_id')->pluck('goods_category_id');
  84. $goodsCategories = GoodsCategory::query()
  85. ->whereIn('id', $allGoodsCategoryIds)
  86. ->get()
  87. ->toArray();
  88. $goodsCategoryIds = implode(',', array_values(array_column($goodsCategories, 'id')));
  89. $paginate = $builder->groupBy(''.$goodsTable.'.id')->inRandomOrder()->paginate($params['pagesize']);
  90. $goods = $paginate->toArray();
  91. return [
  92. 'has_more_pages' => $paginate->hasMorePages(),
  93. 'goods' => $goods['data'],
  94. 'category' => ['goods_category' => $goodsCategories, 'goods_category_ids' => $goodsCategoryIds]
  95. ];
  96. }
  97. public function doForStores($params)
  98. {
  99. $params['pagesize'] = $params['pagesize'] ?: 10;
  100. $storeTable = ApplicationContext::getContainer()->get(Store::class)->getTable();
  101. $goodsTable = ApplicationContext::getContainer()->get(Goods::class)->getTable();
  102. $builder = Store::query()
  103. ->select(''.$storeTable.'.*')
  104. ->join($goodsTable,''.$storeTable.'.id', '=', ''.$goodsTable.'.store_id')
  105. ->where([''.$goodsTable.'.on_sale' => GoodsConstants::ON_SALE_YES])
  106. ->where([''.$goodsTable.'.market_id' => $params['market_id']])
  107. ->where(function ($query) use ($goodsTable) {
  108. $query->where(''.$goodsTable.'.inventory', '>', 0)->orWhere(''.$goodsTable.'.is_infinite', '=', 1);
  109. })
  110. ->whereRaw(''.$goodsTable.'.deleted_at IS NULL')
  111. ->where([''.$storeTable.'.market_id' => $params['market_id']])
  112. ->orderBy($storeTable.'.is_rest','asc');
  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. if (isset($params['store_id']) && $params['store_id']) {
  119. $builder->where([''.$storeTable.'.store_id' => $params['store_id']]);
  120. }
  121. if (isset($params['type_id']) && $params['type_id']) {
  122. $builder->where([''.$storeTable.'.category_id' => $params['type_id']]);
  123. }
  124. if (isset($params['keyword']) && $params['keyword']) {
  125. $builder->where(''.$storeTable.'.name', 'like', '%'.$params['keyword'].'%');
  126. }
  127. if (isset($params['order_by']) && $params['order_by']) {
  128. $sort = $params['sort'] ?? 'desc';
  129. switch ($params['order_by']) {
  130. case 'sales':
  131. $builder->orderBySales($sort);
  132. break;
  133. default:
  134. $builder->orderByDefault($sort);
  135. break;
  136. }
  137. }
  138. $paginate = $builder->groupBy(''.$storeTable.'.id')->inRandomOrder()->paginate($params['pagesize']);
  139. $stores = $paginate->map(function ($item, $key) {
  140. $item->goods;
  141. return $item;
  142. })->all();
  143. return ['has_more_pages' => $paginate->hasMorePages(), 'stores' => $stores];
  144. }
  145. public function getHotKeywords($type)
  146. {
  147. $keywords = [
  148. 'goods' => ['酱油','油','生蚝'],
  149. 'store' => ['黄姐','王姐','黄姐蔬菜摊'],
  150. ];
  151. return $keywords[$type];
  152. }
  153. public function do()
  154. {
  155. // TODO: Implement do() method.
  156. }
  157. public function check()
  158. {
  159. // TODO: Implement check() method.
  160. }
  161. public function undo()
  162. {
  163. // TODO: Implement undo() method.
  164. }
  165. }