<?php 
namespace Common\Taglib;
class question{
	 public function run($opt){
		$order='id desc';
		$type=trim($opt['type']);
		$field=trim($opt['field']);
		$num=empty($opt['num'])?15:intval($opt['num']);
		$tagid=intval($opt['tagid']);
		$page=intval($opt['page']);
		$key=trim($opt['key']);
		
		
		$category1=intval($opt['category1']);
		$category2=intval($opt['category2']);
		$uid=intval($opt['uid']);
		$map['audit']= array('EGT',0);
		if ($category1>0){
			$map['category1']=$category1;
		}
		if ($category2>0){
			$map['category2']=$category2;
		}
		if ($uid>0){
			$map['uid']=$uid;
		}
		if (!empty($type)){
			switch ($type)
			{
			case 1://0回答
			 $map['answercount']=0;
			  break;
			case 2://高悬赏
			  $map['issolve']=0;
			  $map['reward']=array('gt',0);
			  //$order=' reward desc,id desc';
			  break;
			case 3://已解决
			  $map['issolve']=1;
			  break;
			case 4://未解决
			  $map['issolve']=0;
			  break;
			}
		}
		if (!empty($key))
		{
			$map['title']= array('LIKE','%'.$key.'%');
			$order='';
		}
		if (!empty($tagid)){
			$maptag['tagid']=$tagid;
		}
		$limit=$num;
		$count=0;
		if ($page==1){
			if (empty($tagid))
			{
			$count      = M('Question')->where($map)->count();
			}
			else
			{
			$count      = M('QuestionTag')->where($maptag)->count();
			}
			$Page		= pager($count,$num);
			$Pagestr    = $Page->fshow();
			$limit=$Page->firstRow.','.$Page->listRows;
			if ($count==0)
			{
				return '';
			}
		}
		if(!empty($tagid))
		{
			$tags= D('QuestionTag')->get_tag_tagid($tagid,$limit);	
			if (!empty($tags))
			{
				foreach ($tags as $v){
				$ids[]=$v['qid'];
				}
				unset($map);
				$ids = is_array($ids)?implode(",",$ids):$ids;
				$map['id']=array('IN',$ids);
				
				$result=M('Question')->field($field)->where($map)->order('id desc')->select();
			}
			else
			{
			$result='';
			}
		}
		else
		{
			$result=M('Question')->where($map)->order($order)->limit($limit)->select();
		}
		$list=array();
		if (!empty($result)){
			$cat=uu_get_cache('cat');
			foreach ($result as $key => $v){
				$v['url']=RU('question/view',array('id'=>$v['id']));
				$v['uurl']=D('User')->user_url($v['uid']);//发布人链接
				$v['aurl']=D('User')->user_url($v['answeruid']);//回复人链接
				$v['answertimestr']=daterange($v['answertime']);
				$v['addtimestr']=daterange($v['addtime']);
				$v['cat']=$cat[$v['category2']];//二级分类找不见再找一级分类
				if (empty($v['cat']))
				{
					$v['cat']=$cat[$v['category1']];
				}
				$tagpid[]=$v['id'];//获得标签的数组
				$picpid[]=$v['id'];//获得图片的数组
				$arruid[]=$v['uid'];//获得UID数组
				$arransweruid[]=$v['answeruid'];//获得answeruid数组
				$list[$v['id']]=$v;
			}
		}
		//获取标签
		$maptag['qid']  = array('IN',implode(",",$tagpid));
		if (!empty($tagpid))
		{
			$resulttag=M('QuestionTag')->where($maptag)->select();
			foreach ($resulttag as $v){
					$vtag[$v['qid']][]=array('tag'=>$v['tag'],'tagurl'=>RU('tag/view',array('t'=>$v['tag'])));
			}
		}
		//获取图片
		if (!empty($picpid))
		{
			$mappic['qid']  = array('IN',implode(",",$picpid));
			$resultpic=M('QuestionImg')->where($mappic)->select();
			foreach ($resultpic as $v){
					$vpic[$v['qid']]=1;
			}
		}
		//获取用户名
		//
		$uids=array_unique(array_merge($arruid,$arransweruid));
		if (!empty($uids)){
			$mapuid['uid']  = array('IN',implode(",",$uids));
			$resultuid=D('User')->field('username,uid,avatar,mobile')->where($mapuid)->select();
			foreach ($resultuid as $v){
					$user=D('User')->auto_user_fill($v);
					$vuid[$v['uid']]=$user['username'];
					$avatar[$v['uid']]=$user['avatar'];
			}
		}
		//标签、图片和用户名加入到列表中
		foreach ($list as $key => $v){
					$v['tags']=$vtag[$key];
					$v['qimg']=$vpic[$key];
					$v['uname']=$vuid[$v['uid']];
					$v['avatar']=$avatar[$v['uid']];
					$v['aname']=$vuid[$v['answeruid']];
					$returnlist[$v['id']]=$v;
		}
		//dump($returnlist);
		//print_r($returnlist);
		$relist['list']=$returnlist;
		//dump($returnlist);
		$relist['page']=$Pagestr;
		$relist['count']=$count;
		if ($count==0)
		{
		$relist['count']=count($returnlist);
		}
		return $relist;
	  }
	 
	 
}
?>