CategoryService.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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='', $orderBy = 'list_order'){
  104. $cacheKey = "cache:cates:list_byparent_".$pid.'_n'.$num.($field? ':'.md5($field) : '');
  105. $dataList = RedisService::get($cacheKey);
  106. if ($dataList) {
  107. return $dataList;
  108. }
  109. $field = $field? $field : 'id,catname,enname';
  110. $dataList = Db::name('category')
  111. ->where(function($query) use ($pid){
  112. if($pid>=0){
  113. $query->where('parent_id', $pid);
  114. }
  115. })
  116. ->field($field)
  117. ->order($orderBy)
  118. ->limit($num)
  119. ->select();
  120. $dataList = $dataList ? $dataList->toArray() : [];
  121. if ($dataList) {
  122. RedisService::set($cacheKey, $dataList, 6 * 3600);
  123. }
  124. return $dataList;
  125. }
  126. /**
  127. * 获取推荐分类列表以及子类列表
  128. * @param int $num 主分类
  129. * @return $this
  130. * @throws \think\db\exception\DataNotFoundException
  131. * @throws \think\db\exception\ModelNotFoundException
  132. * @throws \think\exception\DbException
  133. */
  134. public static function getRecCates($num=6){
  135. $cacheKey = "cache:cates:recommend_".$num;
  136. $dataList = RedisService::get($cacheKey);
  137. if ($dataList) {
  138. return $dataList;
  139. }
  140. $dataList = Db::name('category')
  141. ->where('parent_id',0)
  142. ->field('id,catname,enname')
  143. ->order('list_order')
  144. ->limit($num)
  145. ->select()
  146. ->each(function($item, $k){
  147. $id = isset($item['id'])? intval($item['id']) : 0;
  148. $item['subList'] = [];
  149. if($id){
  150. $item['subList'] = CategoryService::getCates(12, $id);
  151. }
  152. return $item;
  153. });
  154. $dataList = $dataList ? $dataList->toArray() : [];
  155. if ($dataList) {
  156. RedisService::set($cacheKey, $dataList, 3 * 3600);
  157. }
  158. return $dataList;
  159. }
  160. /**
  161. * 获取推荐分类列表以及子类列表
  162. * @param int $num 主分类
  163. * @return $this
  164. * @throws \think\db\exception\DataNotFoundException
  165. * @throws \think\db\exception\ModelNotFoundException
  166. * @throws \think\exception\DbException
  167. */
  168. public static function getHotCates($num=6){
  169. $cacheKey = "cache:cates:hots_".$num;
  170. $dataList = RedisService::get($cacheKey);
  171. if ($dataList) {
  172. return $dataList;
  173. }
  174. $catIds = Db::name('jiameng')
  175. ->where('status',1)
  176. ->order(db()->raw("sum('hits')"))
  177. ->limit($num)
  178. ->group('catid')
  179. ->column('catid');
  180. if(empty($catIds)){
  181. return false;
  182. }
  183. $dataList = Db::name('category')
  184. ->whereIn('id',$catIds)
  185. ->field('id,catname,enname')
  186. ->order('list_order')
  187. ->limit($num)
  188. ->select();
  189. $dataList = $dataList ? $dataList->toArray() : [];
  190. if ($dataList) {
  191. RedisService::set($cacheKey, $dataList, 3 * 3600);
  192. }
  193. return $dataList;
  194. }
  195. }