<?php
namespace Site\Model;

use Think\Model;

/**
 * 分类模型 推广员广告模型
 * 鹿文学
 */
class SiteAdvModel extends Model{

    protected $_validate = array(
    );

    protected $_auto = array(
    );

		/**
		 * 构造函数
		 * @param string $name 模型名称
		 * @param string $tablePrefix 表前缀
		 * @param mixed $connection 数据库连接信息
		 */
    public function __construct($name = '', $tablePrefix = '', $connection = '') {
        /* 设置默认的表前缀 */
        $this->tablePrefix ='tab_';
        /* 执行构造方法 */
        parent::__construct($name, $tablePrefix, $connection);
    }
		
		/**
		 * 广告列表
		 */
		public function lists($map=array(),$order='create_time desc') {
			if (PID>0) {
				
				$map['promote_id'] = PID;
				$time = time();
				$map['_string'] = "(start_time=0 or start_time < $time) and (end_time=0 or end_time > $time)"  ;
				
				$data = $this->field('tab_site_adv.*,tab_adv_pos.width,tab_adv_pos.height')->join('tab_adv_pos on tab_adv_pos.id=tab_site_adv.pos_id','right')->where($map)->order($order)->select();
				foreach($data as $k => $v) {
                    if ($v['data']>0) {
                        $cover = get_cover($v['data'],'path');
                        if(strpos($cover,'http')!==false){
                            $cover = $cover;
                        }else{
                            $cover = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$cover;
                        }
                        $data[$k]['data']=$cover;
                    }
				}
			} else {
				$data = null;
			}
			
			return $data;
		}
		
		/**
		 * 广告详情
		 */
		public function detail($id) {
			
			if (!is_numeric($id) || $id <1) {return null;}
			
			if (PID<=0) {return null;}
			
			$map['promote_id'] = PID;
			
			$map['id']= $id;
			
			return $this->where($map)->find();
			
		}

		
		
}