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.

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