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.

134 lines
4.3 KiB

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