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.

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