PageService.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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\Goods\GoodsSpecMap;
  8. use App\Models\Goods\Spec;
  9. use App\Models\Goods\SpecAttr;
  10. use App\Models\Setting;
  11. use Illuminate\Database\Eloquent\Builder;
  12. use Illuminate\Support\Arr;
  13. use Illuminate\Support\Facades\DB;
  14. class PageService extends BaseService
  15. {
  16. public function banner()
  17. {
  18. $banner = Setting::query()->where("type", Setting::TYPE_BANNER)->get();
  19. return $banner->filter(function (Setting $setting) {
  20. return $setting->value['is_use'];
  21. })->sortByDesc(function (Setting $setting) {
  22. return $setting->value['sort'];
  23. })->map(function (Setting $setting) {
  24. return [
  25. "id" => $setting->id,
  26. "imageUrl" => $setting->value['imageUrl'],
  27. ];
  28. })->values();
  29. }
  30. public function categoryTree()
  31. {
  32. $categoryS = Category::with("childS")->where("parent_id", 0)->where("index_weight", ">", 0)->get(["id", "name", "thumb", "parent_id", "level", "weight"]);
  33. return $categoryS->map(function (Category $category) {
  34. return [
  35. "id" => $category->id,
  36. "name" => $category->name,
  37. "thumb" => $category->thumb,
  38. "child" => $this->childTree($category),
  39. ];
  40. });
  41. }
  42. protected function childTree(Category $category)
  43. {
  44. if ($category->childS->isEmpty()) {
  45. return [];
  46. }
  47. return $category->childS->map(function (Category $category) {
  48. return [
  49. "id" => $category->id,
  50. "name" => $category->name,
  51. "thumb" => $category->thumb,
  52. "child" => $this->childTree($category),
  53. ];
  54. });
  55. }
  56. public function hotGoods($num = 10)
  57. {
  58. $goods = Goods::where("status", Goods::STATUS_OK)->orderByDesc("view_total")->limit($num)->get();
  59. return $goods->map(function (Goods $g) {
  60. return [
  61. "id" => $g->id,
  62. "name" => $g->name,
  63. "thumb" => $g->thumb,
  64. "view_total" => $g->view_total,
  65. ];
  66. });
  67. }
  68. public function recommendedGoods($num = 10)
  69. {
  70. $goods = Goods::where("status", Goods::STATUS_OK)->orderBy(DB::raw("rand() "))->limit($num)->get();
  71. return $goods->map(function (Goods $g) {
  72. return [
  73. "id" => $g->id,
  74. "name" => $g->name,
  75. "thumb" => $g->thumb,
  76. ];
  77. });
  78. }
  79. public function childCategoryIdArr($id)
  80. {
  81. $cids = Category::where("parent_id", $id)->get(["id"])->pluck("id")->toArray();
  82. $cidss = Category::whereIn("parent_id", $cids)->get(['id'])->pluck("id")->toArray();
  83. return array_merge($cids, $cidss, [$id]);
  84. }
  85. public function filterConfig($params)
  86. {
  87. $spec = Spec::get();
  88. return $spec->map(function (Spec $model) {
  89. return [
  90. "id" => $model->id,
  91. "name" => $model->name,
  92. "type" => "attr",
  93. "child" => $model->attrs->map(function (SpecAttr $attr) {
  94. return [
  95. "name" => $attr->name,
  96. ];
  97. }),
  98. "is_custom" => $model->is_custom,
  99. ];
  100. });
  101. }
  102. public function categoryGoods($params)
  103. {
  104. $id = $params['id'];
  105. $pageSize = Arr::get($params, "page_size");
  106. $cidArr = $this->childCategoryIdArr($id);
  107. $gidArr = GoodsCategoryMap::where("category_id", $cidArr)->get(['goods_id'])->pluck("goods_id")->unique()->toArray();
  108. $attrIdArr = [];
  109. if ($attrs = Arr::get($params, "attrs", [])) {
  110. $attrIdArr = json_decode($attrs, true);
  111. }
  112. $p = Goods::where("status", Goods::STATUS_OK)->when($params['keyword'], function (Builder $query) use ($params) {
  113. $query->where("name", "like", "%" . $params['keyword'] . "%");
  114. })->when($attrIdArr, function (Builder $query) use ($attrIdArr) {
  115. $d = SpecAttr::whereIn("id", $attrIdArr)->groupBy("spec_id")->get(['spec_id']);
  116. $specIdArr = $d->pluck("spec_id")->toArray();
  117. foreach ($specIdArr as $specId) {
  118. $query->whereJsonContains("spec", $specId);
  119. }
  120. })->orderByDesc("weight")->whereIn("id", $gidArr)->paginate($pageSize);
  121. return [
  122. "total" => $p->total(),
  123. "page_total" => $p->lastPage(),
  124. "list" => array_map(function (Goods $model) {
  125. return [
  126. "id" => $model->id,
  127. "name" => $model->name,
  128. "thumb" => $model->thumb,
  129. "weight" => $model->weight,
  130. ];
  131. }, $p->items()),
  132. ];
  133. }
  134. public function search($params)
  135. {
  136. $attrMap = [];
  137. if ($attrs = Arr::get($params, "attrs", [])) {
  138. $attrMap = json_decode($attrs, true);
  139. }
  140. $pageSize = Arr::get($params, "page_size", 10);
  141. $p = Goods::where("status", Goods::STATUS_OK)->when($attrMap, function (Builder $query) use ($attrMap) {
  142. $mapQuery = GoodsSpecMap::query();
  143. foreach ($attrMap as $attr) {
  144. $mapQuery->orWhere(function (Builder $query) use ($attr) {
  145. $query->where("spec_id", $attr['spec_id'])->Where("value", $attr['value']);
  146. });
  147. }
  148. $goodsIdArr = $mapQuery->groupBy("goods_id")->having(DB::raw("count(`goods_id`)"), count($attrMap))->get("goods_id")->pluck("goods_id")->toArray();
  149. $query->whereIn("id", $goodsIdArr);
  150. })->when($params['keyword'], function (Builder $query) use ($params) {
  151. $query->where("name", "like", "%" . $params['keyword'] . "%");
  152. })->orderByDesc("weight")->paginate($pageSize);
  153. return [
  154. "total" => $p->total(),
  155. "page_total" => $p->lastPage(),
  156. "list" => array_map(function (Goods $model) {
  157. return [
  158. "id" => $model->id,
  159. "name" => $model->name,
  160. "thumb" => $model->thumb,
  161. "weight" => $model->weight,
  162. ];
  163. }, $p->items()),
  164. "category" => $this->searchCategory($params['keyword']),
  165. ];
  166. }
  167. private function searchCategory($keyword)
  168. {
  169. $c = Category::where("name", "like", "%$keyword%")->get();
  170. return $c->map(function (Category $model) {
  171. return [
  172. "id" => $model->id,
  173. "thumb" => $model->thumb,
  174. "name" => $model->name,
  175. ];
  176. });
  177. }
  178. }