PageService.php 8.7 KB

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