<?php
class CatchAction extends Action{
	public function rCatchGroup($limit=8)
	{
		//后期向缓存过度
		$group=M('topic_group');
		if($limit==0)
			$rs=$group->order('id desc')->select();
		else
			$rs=$group->limit($limit)->order('id desc')->select();
		$msg="";
		foreach($rs as $value)
			$msg.="<li><a href='".U("blog/gname/id/".urlencode($value['id'])."")."'>".$value['groupname']."</a></li>";
		return $msg;
	}
	public function rCatchTag($limit=8)
	{
		$tag=M('topic_tag');
		if($limit)
			$rs=$tag->limit($limit)->order('id desc')->select();
		else
			$rs=$tag->order('id desc')->select();
		return $rs;
	}
	public function rGetAllGroup()
	{
		$group=M('topic_group');
		$list=$group->order('sort desc')->select();
		return $list;
	}
	public function rGetAllTag()
	{
		$group=M('topic_tag');
		$list=$group->order('id desc')->select();
		return $list;
	}
	//回复数最多的微博
	public function rNewTopic($limit=6)
	{
		$topic=M('topic');
		$rs=FALSE;
		$arrcount=array();
		if($limit)
			$rs=$topic->field('parentid')->where('parentid>0')->limit($limit)->group('parentid')->select();
		else
			$rs=$topic->field('parentid')->where('parentid>0')->group('parentid')->select();
		if($rs)
		{
			for($i=0;$i<count($rs);$i++)
			{
				$info=$topic->find($rs[$i]['parentid']);
				$count=$topic->where('parentid='.$rs[$i]['parentid'])->count();
				$rs[$i]['count']=$count;
				$rs[$i]['title']=$info['title'];
				$rs[$i]['tid']=$info['id'];
			}
		}
		if($rs)
		{
			return list_sort_by($rs,"count","desc");
		}
	}
	public function rUserTop($limit=6)
	{
		$topic=M('topic');
		$user=M('user');
		$rs=FALSE;
		$arrcount=array();
		if($limit)
			$rs=$topic->field('id,userid,count(*) as num')->group('userid')->limit($limit)->order('num desc')->select();
		else
			$rs=$topic->field('id,userid,count(*) as num')->order('num desc')->group('userid')->select();
		if($rs)
		{
			return $rs;
		}
	}
	public function rTagTop($limit=8)
	{
		$topic=M('topic');
		$tag=M('topic_tag');
		$rs=FALSE;
		$arrcount=array();
		if($limit)
			$rs=$topic->field('tagid')->where('tagid>0')->group("tagid")->limit($limit)->select();
		else
			$rs=$topic->field('tagid')->where('tagid>0')->group("tagid")->select();
		if($rs)
		{
			for($i=0;$i<count($rs);$i++)
			{
				$id=$rs[$i]['tagid'];
				$count=$topic->where('tagid='.$id)->count();
				$info=$tag->field('title')->find($id);
				if($info)
				{
					$rs[$i]['count']=$count;
					$rs[$i]['title']=$info['title'];	
				}
			}
		}
		if($rs)
		{
			$rs=list_sort_by($rs,"count","desc");
			return $rs;
		}
	}
	function rUserAttention($limit=8)
	{
		$att=M('attention');
		$list=$att->field("*,count(*) as num")->limit($limit)->group('objuser')->order('num desc')->select();
		return $list;
	}
}
?>