CategoryService.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * 品牌分类
  4. * @author wesmielr
  5. */
  6. namespace app\index\service;
  7. use think\Db;
  8. class CategoryService
  9. {
  10. /**
  11. * 获取首页分类导航列表
  12. * @param bool $refresh
  13. * @return array|bool|\PDOStatement|string|\think\Collection
  14. * @throws \think\db\exception\DataNotFoundException
  15. * @throws \think\db\exception\ModelNotFoundException
  16. * @throws \think\exception\DbException
  17. */
  18. public static function getCateList($refresh = false){
  19. $cacheKey ="cache:index:catearr";
  20. $catearr = RedisService::get($cacheKey);
  21. if(empty($catearr) || $refresh){
  22. //一级分类+二级分类
  23. $catearr = Db::name('category')
  24. ->where('parent_id',0)
  25. ->field('id,catname,enname')
  26. ->order('list_order')
  27. ->limit(0,12)
  28. ->select();
  29. foreach($catearr as $k=>$v){
  30. $erji_cate = Db::name('category')
  31. ->where('parent_id',$v['id'])
  32. ->field('id,enname,catname')
  33. ->order('list_order')
  34. ->limit(0,3)
  35. ->select();
  36. $erji_cate = $erji_cate? $erji_cate->toArray() : [];
  37. // 子类的ID
  38. foreach ($erji_cate as $val){
  39. $xmList = Db::name('jiameng')
  40. ->where(['catid'=> $val['id'],'status'=> 1])
  41. ->field('id,title')
  42. ->order('list_order')
  43. ->limit(12)
  44. ->select();
  45. $data = $val;
  46. $data['xmList'] = $xmList;
  47. $v['sonData'][] = $data;
  48. }
  49. $v['son'] = $erji_cate? array_slice($erji_cate, 0, 2) : [];
  50. $catearr[$k] = $v;
  51. }
  52. if($catearr){
  53. $catearr = $catearr->toArray();
  54. RedisService::set($cacheKey, $catearr, 7 * 24 *3600);
  55. }
  56. }
  57. return $catearr;
  58. }
  59. /**
  60. * 获取首页分类导航列表
  61. * @param bool $refresh
  62. * @return array|bool|\PDOStatement|string|\think\Collection
  63. * @throws \think\db\exception\DataNotFoundException
  64. * @throws \think\db\exception\ModelNotFoundException
  65. * @throws \think\exception\DbException
  66. */
  67. public static function getNavCateList($refresh = false){
  68. $cacheKey ="cache:index:nav_catearr";
  69. $catearr = RedisService::get($cacheKey);
  70. if(empty($catearr) || $refresh){
  71. //一级分类+二级分类
  72. $catearr = Db::name('category')
  73. ->where('parent_id',0)
  74. ->field('id,catname,enname')
  75. ->order('list_order')
  76. ->limit(0,12)
  77. ->select();
  78. foreach($catearr as $k=>$v){
  79. $erji_cate = Db::name('category')
  80. ->where('parent_id',$v['id'])
  81. ->field('id,enname,catname')
  82. ->order('list_order')
  83. ->select();
  84. $erji_cate = $erji_cate? $erji_cate->toArray() : [];
  85. $v['son'] = $erji_cate;
  86. $catearr[$k] = $v;
  87. }
  88. if($catearr){
  89. $catearr = $catearr->toArray();
  90. RedisService::set($cacheKey, $catearr, 7 * 24 *3600);
  91. }
  92. }
  93. return $catearr;
  94. }
  95. /**
  96. * 获取主分类行业
  97. * @param int $num 条数
  98. * @return array|\PDOStatement|string|\think\Collection
  99. * @throws \think\db\exception\DataNotFoundException
  100. * @throws \think\db\exception\ModelNotFoundException
  101. * @throws \think\exception\DbException
  102. */
  103. public static function getCates($num=30, $pid=0, $field=''){
  104. $field = $field? $field : 'id,catname,enname';
  105. return Db::name('category')
  106. ->where(function($query) use ($pid){
  107. if($pid>=0){
  108. $query->where('parent_id', $pid);
  109. }
  110. })
  111. ->field($field)
  112. ->order('list_order')
  113. ->limit($num)
  114. ->select();
  115. }
  116. /**
  117. * 获取推荐分类列表以及子类列表
  118. * @param int $num 主分类
  119. * @return $this
  120. * @throws \think\db\exception\DataNotFoundException
  121. * @throws \think\db\exception\ModelNotFoundException
  122. * @throws \think\exception\DbException
  123. */
  124. public static function getRecCates($num=6){
  125. return Db::name('category')
  126. ->where('parent_id',0)
  127. ->field('id,catname,enname')
  128. ->order('list_order')
  129. ->limit($num)
  130. ->select()
  131. ->each(function($item, $k){
  132. $id = isset($item['id'])? intval($item['id']) : 0;
  133. $item['subList'] = [];
  134. if($id){
  135. $subList = CategoryService::getCates(12, $id);
  136. $item['subList'] = $subList? $subList->toArray() : [];
  137. }
  138. return $item;
  139. });
  140. }
  141. /**
  142. * 获取推荐分类列表以及子类列表
  143. * @param int $num 主分类
  144. * @return $this
  145. * @throws \think\db\exception\DataNotFoundException
  146. * @throws \think\db\exception\ModelNotFoundException
  147. * @throws \think\exception\DbException
  148. */
  149. public static function getHotCates($num=6){
  150. $catIds = Db::name('jiameng')
  151. ->where('status',1)
  152. ->order(db()->raw("sum('hits')"))
  153. ->limit($num)
  154. ->group('catid')
  155. ->column('catid');
  156. if(empty($catIds)){
  157. return false;
  158. }
  159. return Db::name('category')
  160. ->whereIn('id',$catIds)
  161. ->field('id,catname,enname')
  162. ->order('list_order')
  163. ->limit($num)
  164. ->select();
  165. }
  166. }