PageService.php 7.0 KB

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