PageService.php 8.7 KB

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