PageService.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace App\Modules\Mini\Services;
  3. use App\Base\BaseService;
  4. use App\Models\Goods\Category;
  5. use App\Models\Goods\Goods;
  6. use App\Models\Goods\GoodsCategoryMap;
  7. use App\Models\Setting;
  8. use Illuminate\Database\Eloquent\Builder;
  9. use Illuminate\Support\Arr;
  10. use Illuminate\Support\Facades\DB;
  11. class PageService extends BaseService
  12. {
  13. public function banner()
  14. {
  15. $banner = Setting::query()->where("type", Setting::TYPE_BANNER)->get();
  16. return $banner->filter(function (Setting $setting) {
  17. return $setting->value['is_use'];
  18. })->sortByDesc(function (Setting $setting) {
  19. return $setting->value['sort'];
  20. })->map(function (Setting $setting) {
  21. return [
  22. "id" => $setting->id,
  23. "imageUrl" => $setting->value['imageUrl'],
  24. ];
  25. })->values();
  26. }
  27. public function categoryTree()
  28. {
  29. $categoryS = Category::with("childS")->where("parent_id", 0)->where("index_weight", ">", 0)->get(["id", "name", "thumb", "parent_id", "level", "weight"]);
  30. return $categoryS->map(function (Category $category) {
  31. return [
  32. "id" => $category->id,
  33. "name" => $category->name,
  34. "thumb" => $category->thumb,
  35. "child" => $this->childTree($category),
  36. ];
  37. });
  38. }
  39. protected function childTree(Category $category)
  40. {
  41. if ($category->childS->isEmpty()) {
  42. return [];
  43. }
  44. return $category->childS->map(function (Category $category) {
  45. return [
  46. "id" => $category->id,
  47. "name" => $category->name,
  48. "thumb" => $category->thumb,
  49. "child" => $this->childTree($category),
  50. ];
  51. });
  52. }
  53. public function hotGoods($num = 10)
  54. {
  55. $goods = Goods::orderByDesc("view_total")->limit($num)->get();
  56. return $goods->map(function (Goods $g) {
  57. return [
  58. "id" => $g->id,
  59. "name" => $g->name,
  60. "thumb" => $g->thumb,
  61. "view_total" => $g->view_total,
  62. ];
  63. });
  64. }
  65. public function recommendedGoods($num = 10)
  66. {
  67. $goods = Goods::orderBy(DB::raw("rand() "))->limit($num)->get();
  68. return $goods->map(function (Goods $g) {
  69. return [
  70. "id" => $g->id,
  71. "name" => $g->name,
  72. "thumb" => $g->thumb,
  73. ];
  74. });
  75. }
  76. public function childCategoryIdArr($id)
  77. {
  78. $cids = Category::where("parent_id", $id)->get(["id"])->pluck("id")->toArray();
  79. $cidss = Category::whereIn("parent_id", $cids)->get(['id'])->pluck("id")->toArray();
  80. return array_merge($cids, $cidss, [$id]);
  81. }
  82. public function categoryGoods($params)
  83. {
  84. $id = $params['id'];
  85. $pageSize = Arr::get($params, "page_size");
  86. $cidArr = $this->childCategoryIdArr($id);
  87. $gidArr = GoodsCategoryMap::where("category_id", $cidArr)->get(['goods_id'])->pluck("goods_id")->unique()->toArray();
  88. if ($attrs = Arr::get($params, "attr", [])) {
  89. }
  90. $p = Goods::where("status", Goods::STATUS_OK)->when($params['keyword'], function (Builder $query) use ($params) {
  91. $query->where("name", "like", "%" . $params['keyword'] . "%");
  92. })->orderByDesc("weight")->whereIn("id", $gidArr)->paginate($pageSize);
  93. return [
  94. "total" => $p->total(),
  95. "page_total" => $p->lastPage(),
  96. "list" => array_map(function (Goods $model) {
  97. return [
  98. "id" => $model->id,
  99. "name" => $model->name,
  100. "thumb" => $model->thumb,
  101. "weight" => $model->weight,
  102. ];
  103. }, $p->items()),
  104. ];
  105. }
  106. public function search($params)
  107. {
  108. $pageSize = Arr::get($params, "page_size", 10);
  109. $p = Goods::where("status", Goods::STATUS_OK)->when($params['keyword'], function (Builder $query) use ($params) {
  110. $query->where("name", "like", "%" . $params['keyword'] . "%");
  111. })->orderByDesc("weight")->paginate($pageSize);
  112. return [
  113. "total" => $p->total(),
  114. "page_total" => $p->lastPage(),
  115. "list" => array_map(function (Goods $model) {
  116. return [
  117. "id" => $model->id,
  118. "name" => $model->name,
  119. "thumb" => $model->thumb,
  120. "weight" => $model->weight,
  121. ];
  122. }, $p->items()),
  123. "category" => $this->searchCategory($params['keyword']),
  124. ];
  125. }
  126. private function searchCategory($keyword)
  127. {
  128. $c = Category::where("name", "like", "%$keyword%")->get();
  129. return $c->map(function (Category $model) {
  130. return [
  131. "id" => $model->id,
  132. "thumb" => $model->thumb,
  133. "name" => $model->name,
  134. ];
  135. });
  136. }
  137. }