<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: huajie <banhuajie@163.com>
// +----------------------------------------------------------------------

namespace Qrcode\Model;
use Think\Model;

/**
 * 文档基础模型
 */
class AdvModel extends Model{



    /* 自动验证规则 */
    protected $_validate = array(

    );

    /* 自动完成规则 */
    protected $_auto = array(
        array('start_time',  'strtotime', self::MODEL_INSERT, 'function'),
        array('end_time',  'strtotime', self::MODEL_INSERT, 'function'),
    );

    /**
     * 构造函数
     * @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 adv_lists($name,$sort,$limit){

        $map['tab_adv.status'] = 1;
        $map['tab_adv_pos.name'] = $name;
        $map['start_time'] = array(array('lt',time()),array('eq',0),'or');
        $map['end_time'] = array(array('gt',time()),array('eq',0),'or');
        $data = $this->field("tab_adv.*,tab_adv_pos.name")
            ->join("tab_adv_pos on tab_adv.pos_id = tab_adv_pos.id")
            ->where($map)
            ->limit($limit)
            ->order($sort)
            ->select();
        return $data;

    }

    public function adv_lists2($name,$sort,$limit){
        $map['tab_adv.status'] = 1;
        $map['tab_adv_pos.name'] = $name;/*array('in',array('7','8'))*/
        $data = $this->field("tab_adv.*,tab_adv_pos.name")
            ->join("tab_adv_pos on tab_adv.pos_id = tab_adv_pos.id")
            ->where($map)
            ->limit($limit)
            ->order('tab_adv.sort desc')
            ->select();
        return $data;

    }
    
    
    public function lists($name,$limit=20,$sort='a.sort desc',$field='a.title,a.data,a.url,a.target') {
        
        if (empty($name) || !is_string($name)) {return '';}
        
        $map['a.status']=1;
        $map['p.name']=$name;
        
        $map['start_time']=array(array('eq',0),array('egt',time()),'or');
        $map['end_time']=array(array('eq',0),array('egt',time()),'or');
        
        $data = $this->alias('a')->field($field)
        
          ->join('tab_adv_pos as p on a.pos_id = p.id')
        
          ->where($map)
          
          ->limit($limit)
          
          ->order($sort?$sort:'sort desc')
          
          ->select();
          
        return $data;  
    }
}