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.

138 lines
4.5 KiB

5 years ago
5 years ago
6 years ago
5 years ago
6 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. <?php
  2. namespace App\Service\v3\Implementations;
  3. use App\Model\v3\Category;
  4. use App\Model\v3\Goods;
  5. use App\Model\v3\GoodsCategory;
  6. use App\Model\v3\Store;
  7. use App\Service\v3\Interfaces\SearchServiceInterface;
  8. use Hyperf\Paginator\Paginator;
  9. class SearchService implements SearchServiceInterface
  10. {
  11. public function doForGoods($params)
  12. {
  13. $builder = Goods::query()
  14. ->with(['store' => function($query) {
  15. return $query->select(['id', 'logo', 'name']);
  16. }])
  17. ->where(['market_id' => $params['market_id']])
  18. ->where(function ($query) {
  19. $query->where('inventory', '>', 0)->orWhere('is_infinite', '=', 1);
  20. });;
  21. if (isset($params['store_id']) && $params['store_id']) {
  22. $builder->where(['store_id' => $params['store_id']]);
  23. }
  24. if (isset($params['type_id']) && $params['type_id']) {
  25. $typeIds = explode(',', $params['type_id']);
  26. $builder->whereIn('category_id', $typeIds);
  27. }
  28. if (isset($params['goods_category_ids']) && $params['goods_category_ids']) {
  29. $typeIds = explode(',', $params['goods_category_ids']);
  30. $builder->whereIn('goods_category_id', $typeIds);
  31. }
  32. if (isset($params['keyword']) && $params['keyword']) {
  33. // 二级分类
  34. $categoryIds = Category::query()->where('title', 'like', '%'.$params['keyword'].'%')->pluck('id');
  35. // 三级分类
  36. $goodsCategoryIds = GoodsCategory::query()->where('title', 'like', '%'.$params['keyword'].'%')->pluck('id');
  37. $builder->where(function ($query) use ($params, $categoryIds, $goodsCategoryIds) {
  38. $query->where('name', 'like', '%'.$params['keyword'].'%')
  39. ->orWhereIn('category_id', $categoryIds)
  40. ->orWhereIn('goods_category_id', $goodsCategoryIds);
  41. });
  42. }
  43. if (isset($params['order_by']) && $params['order_by']) {
  44. $sort = $params['sort'] ?? 'desc';
  45. switch ($params['order_by']) {
  46. case 'sales':
  47. $builder->orderBySales($sort);
  48. break;
  49. case 'price':
  50. $builder->orderByPrice($sort);
  51. break;
  52. default:
  53. $builder->orderByDefault($sort);
  54. break;
  55. }
  56. }
  57. $builder->select(['*'])->addSelect(['sales as total_sales']);
  58. $paginate = $builder->paginate($params['pagesize']);
  59. $goods = $paginate->toArray();
  60. return ['has_more_pages' => $paginate->hasMorePages(), 'goods' => $goods['data']];
  61. }
  62. public function doForStores($params)
  63. {
  64. $builder = Store::query()
  65. ->with(['goods' => function($query) {
  66. return $query->limit(5)
  67. ->select(['*'])->addSelect(['sales as total_sales']);
  68. }])
  69. ->where(['market_id' => $params['market_id']]);
  70. if (isset($params['store_id']) && $params['store_id']) {
  71. $builder->where(['store_id' => $params['store_id']]);
  72. }
  73. if (isset($params['type_id']) && $params['type_id']) {
  74. $builder->where(['category_id' => $params['type_id']]);
  75. }
  76. if (isset($params['keyword']) && $params['keyword']) {
  77. $builder->where('name', 'like', '%'.$params['keyword'].'%');
  78. }
  79. if (isset($params['order_by']) && $params['order_by']) {
  80. $sort = $params['sort'] ?? 'desc';
  81. switch ($params['order_by']) {
  82. case 'sales':
  83. $builder->orderBySales($sort);
  84. break;
  85. default:
  86. $builder->orderByDefault($sort);
  87. break;
  88. }
  89. }
  90. $builder->select(['id', 'logo', 'name']);
  91. $paginate = $builder->paginate($params['pagesize']);
  92. $stores = $paginate->toArray();
  93. return ['has_more_pages' => $paginate->hasMorePages(), 'stores' => $stores['data']];
  94. }
  95. public function getHotKeywords($type)
  96. {
  97. $keywords = [
  98. 'goods' => ['酱油','油','生蚝'],
  99. 'store' => ['黄姐','王姐','黄姐蔬菜摊'],
  100. ];
  101. return $keywords[$type];
  102. }
  103. public function do()
  104. {
  105. // TODO: Implement do() method.
  106. }
  107. public function check()
  108. {
  109. // TODO: Implement check() method.
  110. }
  111. public function undo()
  112. {
  113. // TODO: Implement undo() method.
  114. }
  115. }