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.

121 lines
3.6 KiB

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