PageService.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. $p = Goods::where("status", Goods::STATUS_OK)->orderByDesc("weight")->whereIn("id", $gidArr)->paginate($pageSize);
  89. return [
  90. "total" => $p->total(),
  91. "page_total" => $p->lastPage(),
  92. "list" => array_map(function (Goods $model) {
  93. return [
  94. "id" => $model->id,
  95. "name" => $model->name,
  96. "thumb" => $model->thumb,
  97. "weight" => $model->weight,
  98. ];
  99. }, $p->items()),
  100. ];
  101. }
  102. }