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.

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