PageService.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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" => 1,
  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. $category = Category::find($id);
  125. $p = Goods::where("status", Goods::STATUS_OK)->when($params['keyword'], function (Builder $query) use ($params) {
  126. $query->where("name", "like", "%" . $params['keyword'] . "%");
  127. })->when($attrMap, function (Builder $query) use ($attrMap) {
  128. $mapQuery = GoodsSpecMap::query();
  129. foreach ($attrMap as $attr) {
  130. $mapQuery->orWhere(function (Builder $query) use ($attr) {
  131. $query->where("spec_id", $attr['spec_id'])->where("value", "like", "%{$attr['value']}%");
  132. });
  133. }
  134. $goodsIdArr = $mapQuery->groupBy("goods_id")->having(DB::raw("count(`goods_id`)"), count($attrMap))->get("goods_id")->pluck("goods_id")->toArray();
  135. $query->whereIn("id", $goodsIdArr);
  136. })->orderByDesc("weight")->whereIn("id", $gidArr)->paginate($pageSize);
  137. return [
  138. "total" => $p->total(),
  139. "page_total" => $p->lastPage(),
  140. "list" => array_map(function (Goods $model) {
  141. return [
  142. "id" => $model->id,
  143. "name" => $model->name,
  144. "thumb" => $model->thumb,
  145. "weight" => $model->weight,
  146. ];
  147. }, $p->items()),
  148. "category" => $category->childS->map(function (Category $model) {
  149. return ["id" => $model->id,
  150. "thumb" => $model->thumb,
  151. "name" => $model->name,
  152. ];
  153. }),
  154. ];
  155. }
  156. public function search($params)
  157. {
  158. $attrMap = [];
  159. if ($attrs = Arr::get($params, "attrs", [])) {
  160. if (is_array($attrs)) {
  161. $attrMap = $attrs;
  162. } else {
  163. $attrMap = json_decode($attrs, true);
  164. }
  165. }
  166. $pageSize = Arr::get($params, "page_size", 10);
  167. $p = Goods::where("status", Goods::STATUS_OK)->when($attrMap, function (Builder $query) use ($attrMap) {
  168. $mapQuery = GoodsSpecMap::query();
  169. foreach ($attrMap as $attr) {
  170. $mapQuery->orWhere(function (Builder $query) use ($attr) {
  171. $query->where("spec_id", $attr['spec_id'])->where("value", "like", "%{$attr['value']}%");
  172. });
  173. }
  174. $goodsIdArr = $mapQuery->groupBy("goods_id")->having(DB::raw("count(`goods_id`)"), count($attrMap))->get("goods_id")->pluck("goods_id")->toArray();
  175. $query->whereIn("id", $goodsIdArr);
  176. })->when($params['keyword'], function (Builder $query) use ($params) {
  177. $query->where("name", "like", "%" . $params['keyword'] . "%");
  178. })->orderByDesc("weight")->paginate($pageSize);
  179. return [
  180. "total" => $p->total(),
  181. "page_total" => $p->lastPage(),
  182. "list" => array_map(function (Goods $model) {
  183. return [
  184. "id" => $model->id,
  185. "name" => $model->name,
  186. "thumb" => $model->thumb,
  187. "weight" => $model->weight,
  188. ];
  189. }, $p->items()),
  190. "category" => [],
  191. ];
  192. }
  193. private function searchCategory($keyword)
  194. {
  195. $c = Category::where("name", "like", "%$keyword%")->orderByDesc("search_weight")->get();
  196. return $c->map(function (Category $model) {
  197. return [
  198. "id" => $model->id,
  199. "thumb" => $model->thumb,
  200. "name" => $model->name,
  201. ];
  202. });
  203. }
  204. }