CategoryService.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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,2)
  35. ->select();
  36. $v['son'] = $erji_cate;
  37. $xmList = Db::name('jiameng')
  38. ->where(['catid|pcatid'=> $v['id'],'status'=> 1])
  39. ->field('id,title')
  40. ->order('list_order')
  41. ->limit(12)
  42. ->select();
  43. $data = $v;
  44. $data['xmList'] = $xmList;
  45. $v['sonData'][] = $data;
  46. // 子类的ID
  47. foreach ($erji_cate as $val){
  48. $xmList = Db::name('jiameng')
  49. ->where(['catid|pcatid'=> $val['id'],'status'=> 1])
  50. ->field('id,title')
  51. ->order('list_order')
  52. ->limit(12)
  53. ->select();
  54. $data = $val;
  55. $data['xmList'] = $xmList;
  56. $v['sonData'][] = $data;
  57. }
  58. $v['son'] = $erji_cate;
  59. $catearr[$k] = $v;
  60. }
  61. if($catearr){
  62. $catearr = $catearr->toArray();
  63. RedisService::set($cacheKey, $catearr, 7 * 24 *3600);
  64. }
  65. }
  66. return $catearr;
  67. }
  68. /**
  69. * 获取主分类行业
  70. * @param int $num 条数
  71. * @return array|\PDOStatement|string|\think\Collection
  72. * @throws \think\db\exception\DataNotFoundException
  73. * @throws \think\db\exception\ModelNotFoundException
  74. * @throws \think\exception\DbException
  75. */
  76. public static function getCates($num=30, $pid=0, $field=''){
  77. $field = $field? $field : 'id,catname,enname';
  78. return Db::name('category')
  79. ->where(function($query) use ($pid){
  80. if($pid>=0){
  81. $query->where('parent_id', $pid);
  82. }
  83. })
  84. ->field($field)
  85. ->order('list_order')
  86. ->limit($num)
  87. ->select();
  88. }
  89. /**
  90. * 获取推荐分类列表以及子类列表
  91. * @param int $num 主分类
  92. * @return $this
  93. * @throws \think\db\exception\DataNotFoundException
  94. * @throws \think\db\exception\ModelNotFoundException
  95. * @throws \think\exception\DbException
  96. */
  97. public static function getRecCates($num=6){
  98. return Db::name('category')
  99. ->where('parent_id',0)
  100. ->field('id,catname,enname')
  101. ->order('list_order')
  102. ->limit($num)
  103. ->select()
  104. ->each(function($item, $k){
  105. $id = isset($item['id'])? intval($item['id']) : 0;
  106. $item['subList'] = [];
  107. if($id){
  108. $subList = CategoryService::getCates(12, $id);
  109. $item['subList'] = $subList? $subList->toArray() : [];
  110. }
  111. return $item;
  112. });
  113. }
  114. /**
  115. * 获取推荐分类列表以及子类列表
  116. * @param int $num 主分类
  117. * @return $this
  118. * @throws \think\db\exception\DataNotFoundException
  119. * @throws \think\db\exception\ModelNotFoundException
  120. * @throws \think\exception\DbException
  121. */
  122. public static function getHotCates($num=6){
  123. $catIds = Db::name('jiameng')
  124. ->where('status',1)
  125. ->order(db()->raw("sum('hits')"))
  126. ->limit($num)
  127. ->group('catid')
  128. ->column('catid');
  129. if(empty($catIds)){
  130. return false;
  131. }
  132. return Db::name('category')
  133. ->whereIn('id',$catIds)
  134. ->field('id,catname,enname')
  135. ->order('list_order')
  136. ->limit($num)
  137. ->select();
  138. }
  139. }