PageService.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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::whereIsCustom(0)->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. "id" => $attr->id,
  96. "name" => $attr->name,
  97. ];
  98. }),
  99. "is_custom" => $model->is_custom,
  100. ];
  101. });
  102. }
  103. public function categoryGoods($params)
  104. {
  105. $id = $params['id'];
  106. $pageSize = Arr::get($params, "page_size");
  107. $cidArr = $this->childCategoryIdArr($id);
  108. $gidArr = GoodsCategoryMap::where("category_id", $cidArr)->get(['goods_id'])->pluck("goods_id")->unique()->toArray();
  109. $attrIdArr = [];
  110. if ($attrs = Arr::get($params, "attrs", [])) {
  111. $attrIdArr = json_decode($attrs, true);
  112. }
  113. $p = Goods::where("status", Goods::STATUS_OK)->when($params['keyword'], function (Builder $query) use ($params) {
  114. $query->where("name", "like", "%" . $params['keyword'] . "%");
  115. })->when($attrIdArr, function (Builder $query) use ($attrIdArr) {
  116. $d = SpecAttr::whereIn("id", $attrIdArr)->groupBy("spec_id")->get(['spec_id']);
  117. $specIdArr = $d->pluck("spec_id")->toArray();
  118. foreach ($specIdArr as $specId) {
  119. $query->whereJsonContains("spec", $specId);
  120. }
  121. })->orderByDesc("weight")->whereIn("id", $gidArr)->paginate($pageSize);
  122. return [
  123. "total" => $p->total(),
  124. "page_total" => $p->lastPage(),
  125. "list" => array_map(function (Goods $model) {
  126. return [
  127. "id" => $model->id,
  128. "name" => $model->name,
  129. "thumb" => $model->thumb,
  130. "weight" => $model->weight,
  131. ];
  132. }, $p->items()),
  133. ];
  134. }
  135. public function search($params)
  136. {
  137. $attrMap = [];
  138. if ($attrs = Arr::get($params, "attrs", [])) {
  139. $attrMap = json_decode($attrs, true);
  140. }
  141. $pageSize = Arr::get($params, "page_size", 10);
  142. $p = Goods::where("status", Goods::STATUS_OK)->when($attrMap, function (Builder $query) use ($attrMap) {
  143. $mapQuery = GoodsSpecMap::query();
  144. foreach ($attrMap as $attr) {
  145. $mapQuery->orWhere(function (Builder $query) use ($attr) {
  146. $query->where("spec_id", $attr['spec_id'])->Where("value", $attr['value']);
  147. });
  148. }
  149. $goodsIdArr = $mapQuery->groupBy("goods_id")->having(DB::raw("count(`goods_id`)"), count($attrMap))->get("goods_id")->pluck("goods_id")->toArray();
  150. $query->whereIn("id", $goodsIdArr);
  151. })->when($params['keyword'], function (Builder $query) use ($params) {
  152. $query->where("name", "like", "%" . $params['keyword'] . "%");
  153. })->orderByDesc("weight")->paginate($pageSize);
  154. return [
  155. "total" => $p->total(),
  156. "page_total" => $p->lastPage(),
  157. "list" => array_map(function (Goods $model) {
  158. return [
  159. "id" => $model->id,
  160. "name" => $model->name,
  161. "thumb" => $model->thumb,
  162. "weight" => $model->weight,
  163. ];
  164. }, $p->items()),
  165. "category" => $this->searchCategory($params['keyword']),
  166. ];
  167. }
  168. private function searchCategory($keyword)
  169. {
  170. $c = Category::where("name", "like", "%$keyword%")->get();
  171. return $c->map(function (Category $model) {
  172. return [
  173. "id" => $model->id,
  174. "thumb" => $model->thumb,
  175. "name" => $model->name,
  176. ];
  177. });
  178. }
  179. }