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.

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