<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * zsb
 *
 * 房源刷新记录模型
 *
 * @package         zsb
 * @author          EllisLab Dev Team
 * @copyright       Copyright (c) 2006 - 2014, HOUSE365, Inc.
 * @link            http://nj.sell.house.com
 * @since           Version 1.0
 */

// ------------------------------------------------------------------------

/**
 * 套餐类型类
 */
class Log_refresh extends MY_Model {
    
    private $_tbl_log_refresh = 'log_refresh';
    
    /**
     * 缓存key
     * @var string 
     */ 
    private $_mem_key ;
    
    //构造函数
    public function __construct() 
    {
        parent::__construct(0,1,2);
        
        $this->_mem_key = GB_CITY.'_log_refresh';
    }
    
    
    /**
     * 根据房源编号和房源类型类型、日期参数获取房源刷新次数
     * (根据上次更新时间来确定是否走缓存)
     *
     * @access	public
     * @param	int $rowid 房源编号
     * @access	string  $tbl 房源类型 sell/rent
     * @param	int $updatetime 房源上次刷新时间
     * @access	string  $day 日期参数格式'2014-07-02'
     * @access	int  $update_cache 是否清除缓存，默认0不清除，1为每次清除
     * @return	int 刷新次数
     */
    public function refresh_num_by_id( $rowid , $tbl = '' , $updatetime = 0 , $day = '',  $update_cache = 0  ) 
    {   
        $refresh_info = array();
        
        $cond_where = "rowid = '".$rowid."'";
        
        //房源类型
        if($tbl != '')
        {
           $cond_where .= " AND tbl = '".$tbl."' "; 
        }
        
        //统计日期
        if($day != '')
        {
           $cond_where .= " AND ymd = '".$day."' "; 
        }
        else
        {
            $cond_where .= " AND ymd = '".date('Y-m-d')."' "; 
        }
        
        $mem_key = $this->_mem_key.'_refresh_num_by_id_'.$rowid.'_'.$tbl.'_'.$day.'_'.$updatetime;
        if( $update_cache == 1 )
        {
            $this->mc->delete($mem_key);
        }
        $cache = $this->mc->get($mem_key);
        
        if(isset($cache['is_ok']) && $cache['is_ok'] == 1 )
        {   
            $refresh_num = $cache['data'];
        }
        else
        { 
            //设置查询条件
            $this->dbback->where($cond_where);
            //查询字段
            $this->dbback->select('num');
            $refresh_info = $this->dbback->get($this->_tbl_log_refresh)->row_array();
            $refresh_num = isset($refresh_info['num']) ? $refresh_info['num'] : 0;
            
            $this->mc->add( $mem_key , array('is_ok'=>1 , 'data'=>$refresh_num) , 300);
        }
        
        return $refresh_num;
    }
    
    
    /**
     * 今日的刷新量
     */
    public function findTodayNumberByBrokerId($brokerId , $update_cache = 0)
    {
        $today  = date('Y-m-d',time());
        $refreshnum = 0;
        
        $mem_key = $this->_mem_key.'_findTodayNumberByBrokerId_'.$brokerId.'_'.$today;
        if( $update_cache == 1 )
        {
            $this->mc->delete($mem_key);
        }
        $cache = $this->mc->get($mem_key);
        
        if(isset($cache['is_ok']) && $cache['is_ok'] == 1 )
        {   
            $refreshnum = $cache['data'];
        }
        else
        { 
            $this->dbback->select("SUM(num) as num");
            $this->dbback->where("uid = '$brokerId' AND  ymd = '$today'");
            $this->dbback->limit(1,0);
            $res = $this->dbback->get($this->_tbl_log_refresh)->row_array();
            $refreshnum = isset($res['num']) ? $res['num'] : 0;

            $this->mc->add( $mem_key , array('is_ok'=> 1 , 'data'=> $refreshnum) , 60);
        }
        
        return $refreshnum;
    }
}
/* End of file log_refresh.php */
/* Location: ./models/log_refresh.php */