PageService.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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($source = "")
  31. {
  32. if ($source == "home") {
  33. $categoryS = Category::with("childS")->where("parent_id", 0)->where("index_weight", ">", 0)->orderByDesc("index_weight")->get(["id", "name", "thumb", "parent_id", "level", "weight"]);
  34. } else {
  35. $categoryS = Category::with("childS")->where("parent_id", 0)->where("index_weight", ">", 0)->orderByDesc("category_weight")->get(["id", "name", "thumb", "parent_id", "level", "weight"]);
  36. }
  37. return $categoryS->map(function (Category $category) {
  38. return [
  39. "id" => $category->id,
  40. "name" => $category->name,
  41. "thumb" => $category->thumb,
  42. "child" => $this->childTree($category),
  43. ];
  44. });
  45. }
  46. protected function childTree(Category $category)
  47. {
  48. if ($category->childS->isEmpty()) {
  49. return [];
  50. }
  51. return $category->childS->map(function (Category $category) {
  52. return [
  53. "id" => $category->id,
  54. "name" => $category->name,
  55. "thumb" => $category->thumb,
  56. "child" => $this->childTree($category),
  57. ];
  58. });
  59. }
  60. public function hotGoods($num = 10)
  61. {
  62. $goods = Goods::where("status", Goods::STATUS_OK)->orderByDesc("view_total")->limit($num)->get();
  63. return $goods->map(function (Goods $g) {
  64. return [
  65. "id" => $g->id,
  66. "name" => $g->name,
  67. "thumb" => $g->thumb,
  68. "view_total" => $g->view_total,
  69. ];
  70. });
  71. }
  72. public function recommendedGoods($num = 10)
  73. {
  74. $goods = Goods::where("status", Goods::STATUS_OK)->orderBy(DB::raw("rand() "))->limit($num)->get();
  75. return $goods->map(function (Goods $g) {
  76. return [
  77. "id" => $g->id,
  78. "name" => $g->name,
  79. "thumb" => $g->thumb,
  80. ];
  81. });
  82. }
  83. public function childCategoryIdArr($id)
  84. {
  85. $cids = Category::where("parent_id", $id)->get(["id"])->pluck("id")->toArray();
  86. $cidss = Category::whereIn("parent_id", $cids)->get(['id'])->pluck("id")->toArray();
  87. return array_merge($cids, $cidss, [$id]);
  88. }
  89. public function filterConfig($params)
  90. {
  91. if ($params['type'] == "category") {
  92. $spec = Spec::with(["attrs"])->orderByDesc("category_weight")->get();
  93. } else {
  94. $spec = Spec::with(["attrs"])->orderByDesc("search_weight")->get();
  95. }
  96. return $spec->map(function (Spec $model) {
  97. return [
  98. "id" => $model->id,
  99. "name" => $model->name,
  100. "type" => "attr",
  101. "child" => $model->attrs->map(function (SpecAttr $attr) {
  102. return [
  103. "name" => $attr->name,
  104. ];
  105. }),
  106. "is_custom" => $model->is_custom,
  107. ];
  108. });
  109. }
  110. public function categoryGoods($params)
  111. {
  112. $id = $params['id'];
  113. $pageSize = Arr::get($params, "page_size");
  114. $cidArr = $this->childCategoryIdArr($id);
  115. $gidArr = GoodsCategoryMap::where("category_id", $cidArr)->get(['goods_id'])->pluck("goods_id")->unique()->toArray();
  116. $attrMap = [];
  117. if ($attrs = Arr::get($params, "attrs", [])) {
  118. if (is_array($attrs)) {
  119. $attrMap = $attrs;
  120. } else {
  121. $attrMap = json_decode($attrs, true);
  122. }
  123. }
  124. $p = Goods::where("status", Goods::STATUS_OK)->when($params['keyword'], function (Builder $query) use ($params) {
  125. $query->where("name", "like", "%" . $params['keyword'] . "%");
  126. })->when($attrMap, function (Builder $query) use ($attrMap) {
  127. $mapQuery = GoodsSpecMap::query();
  128. foreach ($attrMap as $attr) {
  129. $mapQuery->orWhere(function (Builder $query) use ($attr) {
  130. $query->where("spec_id", $attr['spec_id'])->where("value", "like", "%{$attr['value']}%");
  131. });
  132. }
  133. $goodsIdArr = $mapQuery->groupBy("goods_id")->having(DB::raw("count(`goods_id`)"), count($attrMap))->get("goods_id")->pluck("goods_id")->toArray();
  134. $query->whereIn("id", $goodsIdArr);
  135. })->orderByDesc("weight")->whereIn("id", $gidArr)->paginate($pageSize);
  136. return [
  137. "total" => $p->total(),
  138. "page_total" => $p->lastPage(),
  139. "list" => array_map(function (Goods $model) {
  140. return [
  141. "id" => $model->id,
  142. "name" => $model->name,
  143. "thumb" => $model->thumb,
  144. "weight" => $model->weight,
  145. ];
  146. }, $p->items()),
  147. ];
  148. }
  149. public function search($params)
  150. {
  151. $attrMap = [];
  152. if ($attrs = Arr::get($params, "attrs", [])) {
  153. if (is_array($attrs)) {
  154. $attrMap = $attrs;
  155. } else {
  156. $attrMap = json_decode($attrs, true);
  157. }
  158. }
  159. $pageSize = Arr::get($params, "page_size", 10);
  160. $p = Goods::where("status", Goods::STATUS_OK)->when($attrMap, function (Builder $query) use ($attrMap) {
  161. $mapQuery = GoodsSpecMap::query();
  162. foreach ($attrMap as $attr) {
  163. $mapQuery->orWhere(function (Builder $query) use ($attr) {
  164. $query->where("spec_id", $attr['spec_id'])->where("value", "like", "%{$attr['value']}%");
  165. });
  166. }
  167. $goodsIdArr = $mapQuery->groupBy("goods_id")->having(DB::raw("count(`goods_id`)"), count($attrMap))->get("goods_id")->pluck("goods_id")->toArray();
  168. $query->whereIn("id", $goodsIdArr);
  169. })->when($params['keyword'], function (Builder $query) use ($params) {
  170. $query->where("name", "like", "%" . $params['keyword'] . "%");
  171. })->orderByDesc("weight")->paginate($pageSize);
  172. return [
  173. "total" => $p->total(),
  174. "page_total" => $p->lastPage(),
  175. "list" => array_map(function (Goods $model) {
  176. return [
  177. "id" => $model->id,
  178. "name" => $model->name,
  179. "thumb" => $model->thumb,
  180. "weight" => $model->weight,
  181. ];
  182. }, $p->items()),
  183. "category" => [],
  184. ];
  185. }
  186. private function searchCategory($keyword)
  187. {
  188. $c = Category::where("name", "like", "%$keyword%")->orderByDesc("search_weight")->get();
  189. return $c->map(function (Category $model) {
  190. return [
  191. "id" => $model->id,
  192. "thumb" => $model->thumb,
  193. "name" => $model->name,
  194. ];
  195. });
  196. }
  197. }