where('parent_id',0) ->field('id,catname,enname') ->order('list_order') ->limit(0,12) ->select(); foreach($catearr as $k=>$v){ $erji_cate = Db::name('category') ->where('parent_id',$v['id']) ->field('id,enname,catname') ->order('list_order') ->limit(0,2) ->select(); $v['son'] = $erji_cate; $xmList = Db::name('jiameng') ->where(['catid|pcatid'=> $v['id'],'status'=> 1]) ->field('id,title') ->order('list_order') ->limit(12) ->select(); $data = $v; $data['xmList'] = $xmList; $v['sonData'][] = $data; // 子类的ID foreach ($erji_cate as $val){ $xmList = Db::name('jiameng') ->where(['catid|pcatid'=> $val['id'],'status'=> 1]) ->field('id,title') ->order('list_order') ->limit(12) ->select(); $data = $val; $data['xmList'] = $xmList; $v['sonData'][] = $data; } $v['son'] = $erji_cate; $catearr[$k] = $v; } if($catearr){ $catearr = $catearr->toArray(); RedisService::set($cacheKey, $catearr, 7 * 24 *3600); } } return $catearr; } /** * 获取主分类行业 * @param int $num 条数 * @return array|\PDOStatement|string|\think\Collection * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public static function getCates($num=30, $pid=0, $field=''){ $field = $field? $field : 'id,catname,enname'; return Db::name('category') ->where(function($query) use ($pid){ if($pid>=0){ $query->where('parent_id', $pid); } }) ->field($field) ->order('list_order') ->limit($num) ->select(); } /** * 获取推荐分类列表以及子类列表 * @param int $num 主分类 * @return $this * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public static function getRecCates($num=6){ return Db::name('category') ->where('parent_id',0) ->field('id,catname,enname') ->order('list_order') ->limit($num) ->select() ->each(function($item, $k){ $id = isset($item['id'])? intval($item['id']) : 0; $item['subList'] = []; if($id){ $subList = CategoryService::getCates(12, $id); $item['subList'] = $subList? $subList->toArray() : []; } return $item; }); } /** * 获取推荐分类列表以及子类列表 * @param int $num 主分类 * @return $this * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public static function getHotCates($num=6){ $catIds = Db::name('jiameng') ->where('status',1) ->order(db()->raw("sum('hits')")) ->limit($num) ->group('catid') ->column('catid'); if(empty($catIds)){ return false; } return Db::name('category') ->whereIn('id',$catIds) ->field('id,catname,enname') ->order('list_order') ->limit($num) ->select(); } }