<?php
class adapiControl extends skymvc{
	
	public function __construct(){
		parent::__construct();
		$this->loadModel(array("ad","ad_tags"));
	}
	
	public function adlist($tag_id,$limit=4){
		$data=$this->ad->select(array(
			"where"=>" starttime<".time()." AND endtime>".time()." AND status=2 AND tag_id_2nd=".intval($tag_id)." ",
			"order"=>" orderindex asc",
			"start"=>0,
			"limit"=>$limit
		));
		if($data){
			foreach($data as $k=>$v){
				$v['info']=$v['info']?$v['info']:$v['title'];
				$v['link2']=$v['link2']?$v['link2']:$v['link1'];
				$data[$k]=$v;
			}
		}
		
		return $data;
	}
	
	public function listbyno($no,$limit=4){
		$tag_id=$this->ad_tags->selectOne(array("where"=>"  tagno='".$no."' ","fields"=>"tag_id"));
		$data=$this->ad->select(array(
			"where"=>" starttime<".time()." AND endtime>".time()." AND status=2 AND tag_id_2nd=".intval($tag_id)." ",
			"order"=>" orderindex asc",
			"start"=>0,
			"limit"=>$limit
		));
		if($data){
			foreach($data as $k=>$v){
				$v['info']=$v['info']?$v['info']:$v['title'];
				$v['link2']=$v['link2']?$v['link2']:$v['link1'];
				$data[$k]=$v;
			}
		}
		
		return $data;
	}
	
	/*产品广告*/
	public function productByNo($no,$limit=4){
		$tag_id=$this->ad_tags->selectOne(array("where"=>"  tagno='".$no."' ","fields"=>"tag_id"));
		$t_c=$this->ad->select(array(
			"where"=>" starttime<".time()." AND endtime>".time()." AND status=2 AND tag_id_2nd=".intval($tag_id)." ",
			"order"=>" orderindex asc",
			"start"=>0,
			"limit"=>$limit,
		));
		if($t_c){
			foreach($t_c as $k=>$v){
				$ids[]=$v['object_id'];
				$tc[$v['object_id']]=$v;
			}
		}
		if($ids){
			$this->loadControl("productapi");
			$data=$this->productapiControl->select(array("where"=>" id in("._implode($ids).")"));
			if($data){
				foreach($data as $k=>$v){
					unset($tc[$v['id']]['id']);
					$v=array_merge($v,$tc[$v['id']]);
					$data[$k]=$v;
				}
			}
			return $data;
		}
		
	}
}

?>