<?php
class productApiControl extends skymvc{
	
	public function __construct(){
		parent::__construct();
		$this->loadModel(array("product","category"));
	}
	
	public function onDefault(){
		print_r($this->GetByWeek());
	}
	
	public function onTop($catid=0,$limit=10){
		return $this->onList($catid,$limit," grade DESC");
	}
	
	public function GetByWeek(){
		$ids=$this->category->id_family(1);
		$where=" status>0 AND status<98";
		if($ids){
			$where.=" AND catid in("._implode($ids).") ";
		}else{
			$where.=" AND 1=2 ";
		}
		 
		$t_d=$this->select(array("where"=>$where));
		$week=getweek();
		foreach($t_d as $v){
			for($i=1;$i<=7;$i++){
				if($v['week'.$i]){
					$data['week'.$i][]=$v;
				}
			}
			
		}
		$data;
		if(!empty($data)){
			$i=0;
			foreach($data as $k=>$v){
				
				if($k=='week'.$week){
					break;
				}
				$i++;
			}
			$j=0;
			foreach($data as $k=>$v){			
				if($j>=$i){
					$t_data[$k]=$v;
				}				
				$j++;
			}
			
			$j=0;
			foreach($data as $k=>$v){			
				if($j<$i){
					$t_data[$k]=$v;
				}				
				$j++;
			}
		}
		
		return $t_data;
	}
	
	public function select($option=array(),$rscount=false){
		$this->loadModel(array("user"));
		$data=$this->product->select($option,$rscount);
		if($data){
			foreach($data as $k=>$v){
				$uids[]=$v['userid'];
				$t_ids[]=$v['catid'];
			}
			$us=$this->user->getUserByIds($uids);
			$t_ids && $t_c=$this->category->cat_list(" id in("._implode($t_ids).")");
			foreach($data as $k=>$v){
				if(!isset($us[$v['userid']])){
					$u=array(
						"nickname"=>"管理员",
						"user_head"=>"/images/user_head.jpg",
					);
				}else{
					$u=$us[$v['userid']];
				}
				$v['cname']=$t_c[$v['catid']];
				$v['nickname']=$u['nickname'];
				$v['user_head']=$u['user_head'];
				$data[$k]=$v;
			}
		}
		return $data;
	}
	
	public function onList($catid=0,$limit=10,$orderby=" id DESC"){
		$w=" status>0 AND status<10 ";
		if($catid){
			$this->loadConfig("c_category_children");
			$category_children=$this->config_item('category_children');
			$cids=$category_children[$catid];
			if($cids){
				$w.=" AND catid in(".implode(",",$cids).") ";
			}else{
				$w.=" AND 1=2 ";
			}
		}
		$rscount=false;
		$list=$this->select(array(
			"where"=>$w,
			"start"=>$start,
			"limit"=>$limit,
			"order"=>$orderby
			
		),$rscount);
		return $list;
	}
	
	public function listByUser($userid,$limit=10,$orderby=" id DESC"){
		$rscount=true;
		$userid=intval($userid);
		$w=" status>0 AND status<10 AND userid=$userid ";
		$list=$this->select(array(
			"where"=>$w,
			"start"=>$start,
			"limit"=>$limit,
			"order"=>$orderby
			
		));
		return $list;
	}
	
	
	
	public function hot($catid=0,$limit=10){
		$where=" status=1 ";
		if($catid){
			$ids=$this->category->id_family($catid);
			if($ids){
				$where.=" AND catid in("._implode($ids).")";
			}else{
				$where .=" AND 1=2 ";
			}
		}
		$option=array(
			"where"=>$where,
			"limit"=>$limit,
			"order"=>" grade DESC"
		);
		$data=$this->product->select($option);
		return $data;
	}
	
	public function recommend($catid=0,$limit=10){
		$where=" status=1 ";
		if($catid){
			$ids=$this->category->id_family($catid);
			if($ids){
				$where.=" AND catid in("._implode($ids).")";
			}else{
				$where .=" AND 1=2 ";
			}
		}
		 
		$option=array(
			"where"=>$where,
			"limit"=>$limit,
			"order"=>" grade DESC"
		);
		$data=$this->product->select($option);
		return $data;
	}
	
	
	public function hotBySite($limit=99){
		$option=array(
			"where"=>"",
			"limit"=>$limit,
			"order"=>" grade DESC"
		);
		$this->loadModel("shop");
		$data=$this->product->select($option);
		if($data){
			foreach($data as $k=>$v){
				$ids[]=$v['shopid'];
			}
			
			$ids && $ss=$this->shop->id_shop(array("where"=>" shopid in("._implode($ids).")"));
			foreach($data as $k=>$v){
				if(isset($ss[$v['shopid']])){
					$v['shopname']=$ss[$v['shopid']]['shopname'];
					$v['shoplogo']=$ss[$v['shopid']]['logo'];
				}
				$data[$k]=$v;
			}
		}
		return $data;
	}
	
}
?>