<?php
/**
 * UserBehavior Class
 *
 * 记录用户行为数据
 *
 * @package         core
 * @subpackage      lib
 * @author          liuhu
 */

class UserBehavior
{
    /**
    * 用户ID
    *
    * @access private
    * @var integer
    */
    private  $_uid = 0;
    
    /**
    * 记录ID
    *
    * @access private
    * @var integer
    */
    private  $_id = 0;
    
   /**
    * 操作手机
    *
    * @access private
    * @var integer
    */
    private  $_telno = '';
    
    /**
    * 操作设备类型
    *
    * @access private
    * @var integer
    */
    private  $_device = 0;
    
    /**
    * 区属
    *
    * @access private
    * @var integer
    */
    private  $_district = 0;
    
    /**
    * 板块
    *
    * @access private
    * @var integer
    */
    private  $_street = 0;
    
    /**
    * 来源
    *
    * @access private
    * @var integer
    */
    private  $_infofrom = 0;
    
    /**
    * 房源类型
    *
    * @access private
    * @var integer
    */
    private  $_infotype = 0;
    
    /**
    * 房源租赁方式
    *
    * @access private
    * @var integer
    */
    private  $_renttype = 0;
    
    /**
    * 装修程度
    *
    * @access private
    * @var integer
    */
    private  $_fitment = 0;
    
    /**
    * 房源价格下限
    *
    * @access private
    * @var integer
    */
    private  $_price1 = 0;
    
    /**
    * 房源价格上限
    *
    * @access private
    * @var integer
    */
    private  $_price2 = 0;
    
    /**
    * 房源均价下限
    *
    * @access private
    * @var integer
    */
    private  $_averprice1 = 0;
    
    /**
    * 房源均价上限
    *
    * @access private
    * @var integer
    */
    private  $_averprice2 = 0;
    
    /**
    * 房源面积下限
    *
    * @access private
    * @var integer
    */
    private  $_buildarea1 = 0;
    
    /**
    * 房源面积上限
    *
    * @access private
    * @var integer
    */
    private  $_buildarea2 = 0;
    
    /**
    * 房源户型下限
    *
    * @access private
    * @var integer
    */
    private  $_room1 = 0;
    
    /**
    * 房源户型上限
    *
    * @access private
    * @var integer
    */
    private  $_room2 = 0;
    
    /**
    * 房源楼层下限
    *
    * @access private
    * @var integer
    */
    private  $_floor1 = 0;
    
    /**
    * 房源楼层上限
    *
    * @access private
    * @var integer
    */
    private  $_floor2 = 0;
    
    /**
    * 问题类型
    *
    * @access private
    * @var integer
    */
    private $_qa_type = 0;
    
    /**
    * 问题子类型
    *
    * @access private
    * @var integer
    */
    private $_qa_class = 0;
    
    /**
    * 问题是否有最佳答案
    *
    * @access private
    * @var integer
    */
    private $_is_best = 0;

	/**
    * 学校类型
    *
    * @access private
    * @var integer
    */
	private $_category_sh = 0;
	
	/**
    * 学校等级
    *
    * @access private
    * @var integer
    */
	private $_rank = 0;
    
    /**
    * 关键词/小区名称
    *
    * @access private
    * @var string
    */
    private  $_keyword = '';
    
    /**
    * 用户操作内容所在的城市
    *
    * @access private
    * @var int
    */
    private $_city = '';

    /**
    * 站点城市字典(可扩展其它城市)
    *
    * @access private
    * @var array
    */
    private $_city_arr = array( 1 => 'nj' , 2 => 'wx' , 3 => 'sz', 4 => 'hf', 5 => 'hz', 6 => 'xa', 7 => 'cz', 8 => 'ks', 9 => 'wh', 10 => 'cq');


    /**
    * 用户客户端IP
    *
    * @access private
    * @var string
    */
    private $_ip = '';

    /**
    * 用户行为记录表
    *
    * @access private
    * @var string
    */
    private  $_log_behavior_tbl = "log_user_behavior";
    
    /**
    * 用户行为记录表(sell)
    *
    * @access private
    * @var string
    */
    private  $_log_behavior_sell_tbl = "behavior_sell";
    
    /**
    *  用户行为记录表(rent)
    *
    * @access private
    * @var string
    */
    private  $_log_behavior_rent_tbl = "behavior_rent";
    
    /**
    *  用户行为记录表(block)
    *
    * @access private
    * @var string
    */
    private  $_log_behavior_block_tbl = "behavior_block";

	/**
    *  用户行为记录表(block)
    *
    * @access private
    * @var string
    */
    private  $_log_behavior_school_tbl = "behavior_school";
	
    /**
    *  用户行为记录表(ask)
    *
    * @access private
    * @var string
    */
    private  $_log_behavior_ask_tbl = "behavior_ask";

    /**
    *  用户行为记录节点表
    *
    * @access private
    * @var string
    */
    private  $_behavior_nodes_tbl   = "behavior_nodes";

    /**
    *  操作频道
    *
    * @access private
    * @var string
    */
    private  $_website    = 0;

    /**
    *  频道(可向后扩展，不能中间插入类型)
    *
    * @access private
    * @var array
    */
    private  $_website_arr   = array( 1 => '二手房' ,2 => '租房' ,3 => '小区' ,
				4 => '问答' , 5 => '会员中心', 6 => '发布', 7 =>'学区');

    /**
    *  用户操作位置
    *
    * @access private
    * @var string
    */
    private  $_position = 0;

    /**
    *  用户操作位置数组
    *
    * @access private
    * @var array
    */
    private  $_position_arr   = array( 1 => '首页' , 2 => '列表页' , 3 => '详情页', 4 => '发布页' );

    /**
    *  用户操作类型
    *
    * @access private
    * @var string
    */
    private  $_opreatetype    = 0;

    /**
    *  用户操作类型数组
    *
    * @access private
    * @var array
    */
    private  $_opreatetype_arr   = array(1 => '搜索' , 2 => '查看' , 3 => '加入收藏夹' ,
                                        4=>'保存至桌面' , 5 => '关注找房条件' ,
					6 => '预约看房' , 7 => '发送到手机' , 8 => '关注房源' ,
                                        9 => '求购求租发布'  , 11 => '关注小区' );
    
    /**
    * 操作设备类型
    *
    * @access private
    * @var array
    */
    private  $_device_arr   = array( 1 => 'pc', 2 => 'andriod' , 3 => 'iphone');

    /**
    *  数据库主库
    *
    * @access private
    * @var object
    */
    private $_db_user = NULL;

    /**
    *  数据库从库
    *
    * @access private
    * @var object
    */
    private $_dbback_user = NULL;

    /*Constructor*/
    public function __construct( $db_user , $dbback_user = NULL , $city = 'nj' )
    {
        $this->_db_user	= $db_user;
        $this->_dbback_user = $dbback_user;
        $this->set_city(strip_tags($city));
    }


    /**
    * set_uid
    *
    * 初始化uid参数
    *
    * @access	public
    * @param	int     $uid 用户编号
    * @return	void
    */
    public function set_uid( $uid )
    {
        $this->_uid = $uid;
    }
    
    
    /**
    * set_id
    *
    * 初始化参数
    *
    * @access	public
    * @param	int $id 记录编号
    * @return	void
    */
    public function set_id( $id )
    {
        $this->_id = $id;
    }
    
    
    /**
    * set_telno
    *
    * 初始化参数
    *
    * @access	public
    * @param	int $telno 手机号码
    * @return	void
    */
    public function set_telno( $telno )
    {
        $this->_telno = strip_tags($telno);
    }
    
    
    /**
    * get_uid
    *
    * 初始化uid参数
    *
    * @access	public
    * @return	int     $uid 用户编号
    */
    public function get_uid( )
    {
        return $this->_uid ;
    }

    /**
    * set_city
    *
    * 初始化城市参数
    *
    * @access	public
    * @param	string  $city 城市参数
    * @return	void
    */
    public function set_city($city)
    {
        $city_arr_flip	=  array_flip($this->_city_arr);
        $city_id	= $city_arr_flip[$city];
        
        $this->_city    = $city_id;
    }


    /**
    * get_city
    *
    * 获得城市
    *
    * @access	public
    * @return	void
    */
    public function get_city()
    {
        return $this->_city ;
    }
    
    
    /**
    * set_district
    *
    * 设置区属变量
    *
    * @access	public
    * @param	int  $district 区属参数
    * @return	void
    */
    public function set_district($district)
    {
        $this->_district = intval($district);
    }
    
    
    /**
    * set_street
    *
    * 设置区属变量
    *
    * @access	public
    * @param	int  $street 板块参数
    * @return	void
    */
    public function set_street($street)
    {
        $this->_street = intval($street);
    }
    
    
    /**
    * set_street
    *
    * 设置区属变量
    *
    * @access	public
    * @param	int  $averprice1 均价1
    * @param	int  $averprice2 均价2
    * @return	void
    */
    public function set_averprice( $averprice1 , $averprice2 )
    {
        $this->_averprice1 = intval($averprice1);
        $this->_averprice2 = intval($averprice2);
    }

	
	/**
    * set_category
    *
    * 设置学校类别
    *
    * @access	public
    * @param	int  $category 学校类型
    * @return	void
    */
	public function set_category($category)
	{
		$this->_category_sh = intval($category);
	}


	/**
    * set_rank
    *
    * 设置学校级别
    *
    * @access	public
    * @param	int  $category 学校级别
    * @return	void
    */
	public function set_rank($rank)
	{
		$this->_rank = intval($rank);
	}


    /**
    * set_ip
    *
    * 初始化IP参数
    *
    * @access	public
	* @param	string  $ip IP
    * @return	void
    */
    public function set_ip($ip)
    {
       $this->_ip = strip_tags($ip);
    }


    /**
    * set_website
    *
    * 初始化website参数
    *
    * @access	public
    * @param	int  $website
    * @return	void
    */
    public function set_website( $website )
    {
        $this->_website	= intval($website);
    }


    /**
    * get_website_arr
    *
    * 获取get_website_arr数组
    *
    * @access	public
    * @param	none
    * @return	array $website
    */
    public function get_website_arr()
    {
        return $this->_website_arr;
    }


    /**
    * set_position
    *
    * 初始化position参数
    *
    * @access	public
    * @param	int  $postion
    * @return	void
    */
    public function set_position( $position )
    {
        $this->_position = intval($position);
    }


    /**
    * get_position_arr
    *
    * 获取position_arr数组
    *
    * @access	public
    * @param	none
    * @return	array $_position_arr
    */
    public function get_position_arr()
    {
        return $this->_position_arr;
    }


    /**
    * set_opreatetype
    *
    * 初始化opreatetype参数
    *
    * @access	public
    * @param	int  opreatetype
    * @return	void
    */
    public function set_opreatetype( $opreatetype )
    {
       $this->_operate_type    = intval($opreatetype);
    }


    /**
    * get_opreatetype_arr
    *
    * 获取$_opreatetype_arr数组
    *
    * @access	public
    * @param	none
    * @return	array $_opreatetype_arr
    */
    public function get_opreatetype_arr()
    {
        return $this->_opreatetype_arr;
    }
    
    
    /**
    * set_device
    *
    * 初始化device参数
    *
    * @access	public
    * @param	int  $device
    * @return	void
    */
    public function set_device( $device )
    {
       $this->_device   = intval($device);
    }
    
    /**
    * get_device_arr
    *
    * 获取_device_arr数组
    *
    * @access	public
    * @param	none
    * @return	array $_device_arr
    */
    public function get_device_arr()
    {
        return $this->_device_arr;
    }
    
    
    /**
    * set_infofrom
    *
    * 初始化infofrom参数
    *
    * @access	public
    * @param	int  $infofrom
    * @return	void
    */
    public function set_infofrom( $infofrom )
    {
       $this->_infofrom   = intval($infofrom);
    }
    
    
    /**
    * set_infotype
    *
    * 初始化infotype参数
    *
    * @access	public
    * @param	int  $infotype
    * @return	void
    */
    public function set_infotype( $infotype )
    {
       $this->_infotype   = intval($infotype);
    }
    
    
    /**
    * set_renttype
    *
    * 初始化renttype参数
    *
    * @access	public
    * @param	int  $renttype
    * @return	void
    */
    public function set_renttype( $renttype )
    {
       $this->_renttype  = intval($renttype);
    }
    

    /**
    * set_fitment
    *
    * 初始化fitment参数
    *
    * @access	public
    * @param	int  $fitment
    * @return	void
    */
    public function set_fitment( $fitment )
    {
       $this->_fitment   = intval($fitment);
    }
    
    
    /**
    * set_price
    *
    * 初始化价格参数
    *
    * @access	public
    * @param	int  $price1
    * @param	int  $price2
    * @return	void
    */
    public function set_price( $price1  , $price2 )
    {
       $this->_price1   = intval($price1);
       $this->_price2   = intval($price2);
    }
    
    /**
    * set_buildarea
    *
    * 初始化面积参数
    *
    * @access	public
    * @param	int  $buildarea1
    * @param	int  $buildarea2
    * @return	void
    */
    public function set_buildarea( $buildarea1  , $buildarea2 )
    {
       $this->_buildarea1   = intval($buildarea1);
       $this->_buildarea2   = intval($buildarea2);
    }
    
    /**
    * set_floor
    *
    * 初始化楼层参数
    *
    * @access	public
    * @param	int  $floor1
    * @param	int  $floor2
    * @return	void
    */
    public function set_floor( $floor1  , $floor2 )
    {
       $this->_floor1   = intval($floor1);
       $this->_floor2   = intval($floor2);
    }
    
    
    /**
    * set_room
    *
    * 初始化户型参数
    *
    * @access	public
    * @param	int  $floor1
    * @param	int  $floor2
    * @return	void
    */
    public function set_room( $room1  , $room2 )
    {
       $this->_room1   = intval($room1);
       $this->_room2   = intval($room2);
    }
    
    
    /**
    * set_keyword
    *
    * 初始化关键词/小区名称参数
    *
    * @access	public
    * @param	string  keyword
    * @return	void
    */
    public function set_keyword( $keyword )
    {
       $this->_keyword  = strip_tags($keyword);
    }
    
    /**
    * set_qa_type
    *
    * 初始化问答类型参数
    *
    * @access	public
    * @param	intval  $qa_type
    * @return	void
    */
    public function set_qa_type( $qa_type )
    {
       $this->_qa_type  = intval($qa_type);
    }
    
    
    /**
    * set_qa_class
    *
    * 初始化问答类型参数
    *
    * @access	public
    * @param	string  keyword
    * @return	void
    */
    public function set_qa_class( $qa_class )
    {
       $this->_qa_class  = intval($qa_class);
    }
    
    /**
    * set_is_best
    *
    * 初始化最佳答案参数
    *
    * @access	public
    * @param	int  $is_best
    * @return	void
    */
    public function set_is_best( $is_best )
    {
       $this->_is_best  = intval($is_best);
    }
    
    /**
    * get_device_by_id
    *
    * 获取$_opreatetype_arr数组
    *
    * @access	public
    * @param	none
    * @return	array $_opreatetype_arr
    */
    public function get_device_by_id( $device_id )
    {
        $device_name = $this->_device_arr[$device_id];
        return $device_name;
    }


    /**
    * _tbl_exists
    *
    * 判断数据库表是否存在
    *
    * @access	private
    * @param	int $tbl_name   表名称
    * @return	boolean  true/false
    */
    private function _tbl_exists($tbl_name)
    {
        $sql = "SHOW TABLES LIKE '".$tbl_name."'";
        $tbl_info = $this->_db_user->GetAll($sql);
   
        if( is_array($tbl_info) && !empty($tbl_info) )
        {
            return true;
        }
        else
        {
            return false;
        }
    }


    /**
    * _get_tblname_by_date
    *
    * 根据日期生成表字段
    *
    * @access	private
    * @param	string	$date   日期(eg:20140313)
    * @param	string	$tbl_name  基础表名称
    * @return	string	$tblname 表名称
    */
    private function _get_tblname_by_date($date , $base_tblname = '')
    {
        $base_tblname = $base_tblname == '' ?
            $this->_log_behavior_tbl : strip_tags($base_tblname);

        $tblname = $base_tblname . $date;

        return $tblname;
    }


    /**
    * _creat_tbl_by_copy
    *
    * 根据$tblname 生成新表 $tblname_new
    *
    * @access	private
    * @param	string	$tblname_new  新表名称
    * @param	string	$tblname	  被复制的表的名称
    * @return	boolean true/false
    */
    private function _creat_tbl_by_copy ( $tblname_new , $tblname )
    {
        if( trim($tblname_new) == trim($tblname) )
        {
            return false;
        }

        //5.0之前用(CREATE TABLE 新表 SELECT * FROM 旧表 WHERE 1=2)
        // MYSQL5.0以上才支持
        $sql = " CREATE TABLE $tblname_new LIKE $tblname ";
        $result = $this->_db_user->Execute($sql);

        if($result)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    
    /**
    * get_cityname_by_id
    *
    * 根据城市ID返回城市建成字符串
    *
    * @access	public
    * @param	int $city_id 城市编号
    * @return	string	$city_name 城市简写字符串
    */
    public function get_cityname_by_id( $city_id )
    {
        $city_name = $this->_city_arr[$city_id];

        if( $city_name != '')
        {
            return $city_name;
        }
        else
        {
            return $city_name;
        }
    }


    /**
    * get_websitename_by_id
    *
    * 根据频道编号返回频道字符串
    *
    * @access	public
    * @param	int $website_id 频道编号
    * @return	string	$website_name 频道字符串
    */
    public function get_websitename_by_id( $website_id )
    {
        $website_name = $this->_website_arr[$website_id];

        return $website_name;
    }


    /**
    * get_positionname_by_id
    *
    * 根据位置ID返回位置字符串
    *
    * @access	public
    * @param	int $postion_id 位置编号
    * @return	string	$postion_name 位置字符串
    */
    public function get_positionname_by_id( $position_id )
    {
        $position_name = $this->_position_arr[$position_id];
        return $position_name;
    }


    /**
    * get_operate_by_id
    *
    * 根据操作类型编号获取操作类型
    *
    * @access	public
    * @param	int $operate_id 操作类型编号
    * @return	string	$operate_name 操作类型
    */
    public function get_operate_by_id( $operate_id )
    {
        $operate_name = $this->_opreatetype_arr[$operate_id];
        return $operate_name;
    }


    /**
    * get_node_by_id
    *
    * 根据操作内容节点ID，获取操作内容字符串
    *
    * @access	public
    * @param	int $node_id 操作内容编号
    * @return	string	$node_str 操作内容
    */
    public function get_node_by_id( $node_id )
    {
        /*where*/
        $cond_where = " WHERE  id = '".$node_id."' ";

        /*sql*/
        $sql = " SELECT node FROM $this->_behavior_nodes_tbl ";
        $sql .= $cond_where;

        $node_str = $this->_dbback_user->GetOne($sql);

        return $node_str;
    }


    /**
    * get_node_by_ids
    *
    * 根据多个操作内容节点ID，获取操作内容字符串
    *
    * @access	public
    * @param	array	$node_id_arr 操作内容编号数组
    * @return	array	$node_str 操作内容
    */
    public function get_node_by_ids( $node_id_arr )
    {
        $node_arr = array();

        if( is_array($node_id_arr) && !empty($node_id_arr) )
        {
            $ids = implode(',',$node_id_arr);

            /*where*/
            $cond_where = " WHERE id IN (".$ids.") ";

            /*sql*/
            $sql = " SELECT id,node FROM $this->_behavior_nodes_tbl ";
            $sql .= $cond_where;

            $result = $this->_dbback_user->GetAll($sql);

            for( $i = 0 ; $i < count( $result ) ; $i++)
            {
                $node_arr[$result[$i]['id']] = $result[$i]['node'];
            }
        }

        return $node_arr;
    }


    /**
    * array_pluck
    *
    * 取出二维数组中键的值并组成的数组
    *
    * @access	public
    * @param	string	$key   要查的key
    * @param	string	$array 目标数组
    * @return	array   键值对数组
    */
    public function array_pluck($key, $array)
    {
        if ( is_array( $key ) || !is_array( $array ) )
        {
            return array();
        }

        $funct	= create_function('$e', 'return is_array($e)
                                && array_key_exists("'.$key.'",$e) ? $e["'. $key .'"] : null;');

        return array_unique(array_map($funct, $array));
    }
    
    
    /**
    * _get_where_condtion
    *
    * 获取用户的查询条件
    *
    * @access	public
    * @param	void
    * @return	string   用户的查询条件
    */
    private function _get_where_condtion()
    {   
        $cond_where = '';
        
        /*city_condtion*/
        if( $this->_city > 0 )
        {
            $cond_where	.= "WHERE city = '".$this->_city."' " ;
        }
        
        /*uid_condtion*/
        if( $this->_uid > 0 )
        {
            $cond_where	.= " AND uid = '".$this->_uid."' ";
        }

        /*website_condtion*/
        if( $this->_website > 0 )
        {
            $cond_where	.= " AND website = '".$this->_website."' " ;
        }

        /*position_condtion*/
        if( $this->_position > 0 )
        {
            $cond_where	.= " AND position = '".$this->_position."' " ;
        }

        /*operate_type_condtion*/
        if( $this->_operate_type > 0 )
        {
            $cond_where	.= " AND operate_type = '".$this->_operate_type."' ";
        }
        
        /*telno_condtion*/
        if( $this->_telno != '' )
        {
            $cond_where	.= " AND telno = '".$this->_telno."' ";
        }
        
        /*id_condtion*/
        if( $this->_id > 0 )
        {
            $cond_where	.= " AND id = '".$this->_id."' ";
        }
        
        /*device_condtion*/
        if( $this->_device > 0 )
        {
            $cond_where	.= " AND device = '".$this->_device."' ";
        }
        
        return $cond_where;
    }
    
    
    /**
    * _get_house_where_condtion
    *
    * 获取用户查询条件（house）
    *
    * @access	public
    * @param	void
    * @return	string   用户的查询条件
    */
    private function _get_house_where_condtion()
    {   
        $cond_where = '';
        
        /*infofrom_condtion*/
        if( $this->_infofrom > 0 )
        {
            $cond_where	.= " AND infofrom = '".$this->_infofrom."' ";
        }
        
        /*infotpe_condtion*/
        if( $this->_infotype > 0 )
        {
            $cond_where	.= " AND infotype = '".$this->_infotype."' ";
        }
        
        /*district_condtion*/
        if( $this->_district > 0 )
        {
            $cond_where	.= " AND district = '".$this->_district."' ";
        }
        
        /*street_condtion*/
        if( $this->_street > 0 )
        {
            $cond_where	.= " AND streetid = '".$this->_street."' ";
        }
        
        /*renttype_condtion*/
        if( $this->_renttype > 0 )
        {
            $cond_where	.= " AND renttype = '".$this->_renttype."' ";
        }
         
        
        /*fitment_condtion*/
        if( $this->_fitment > 0 )
        {
            $cond_where	.= " AND fitment = '".$this->_fitment."' ";
        }

        /*price_condtion*/
        if( $this->_price1 > 0 ||  $this->_price2 > 0 )
        {   
            if($this->_price1 > 0  && $this->_price2 > 0)
            {
                $cond_where .= " AND price1 >= '".$this->_price1."' AND price2 <= '".$this->_price2."' AND price2 >= price1";
            }
            else if( $this->_price1 > 0)
            {
                $cond_where .= " AND price1 >= '".$this->_price1."'";
            }
            else
            {
                $cond_where .= " AND price2 <= '".$this->_price2."' AND price2 > 0";
            }
        }
        
        /*averprice_condtion*/
        if( $this->_averprice1 > 0 ||  $this->_averprice2 > 0 )
        {   
            if($this->_averprice1 > 0  && $this->_averprice2 > 0)
            {
                $cond_where .= " AND averprice1 >= '".$this->_averprice1."' AND averprice2 <= '".$this->_averprice2."' AND averprice2 >= averprice1";
            }
            else if( $this->_averprice1 > 0)
            {
                $cond_where .= " AND averprice1 >= '".$this->_averprice1."'";
            }
            else
            {
                $cond_where .= " AND averprice2 <= '".$this->_averprice2."' AND averprice2 > 0";
            }
        }
        
        /*buildarea_condtion*/
        if( $this->_buildarea1 > 0 ||  $this->_buildarea2 > 0 )
        {   
            if($this->_buildarea1 > 0  && $this->_buildarea2 > 0)
            {
                $cond_where .= " AND buildarea1 >= '".$this->_buildarea1."' AND buildarea2 <= '".$this->_buildarea2."' AND buildarea2 >= buildarea1";
            }
            else if( $this->_buildarea1 > 0)
            {
                $cond_where .= " AND buildarea1 >= '".$this->_buildarea1."'";
            }
            else
            {
                $cond_where .= " AND buildarea2 <= '".$this->_buildarea2."' AND buildarea2 > 0";
            }
        }
        
        /*floor_condtion*/
        if( $this->_floor1 > 0 ||  $this->_floor2 > 0 )
        {   
            if($this->_floor1 > 0  && $this->_floor2 > 0)
            {
                $cond_where .= " AND floor1 >= '".$this->_floor1."' AND floor2 <= '".$this->_floor2."' AND floor2 >= floor1";
            }
            else if( $this->_floor1 > 0)
            {
                $cond_where .= " AND floor1 >= '".$this->_floor1."'";
            }
            else
            {
                $cond_where .= " AND floor2 <= '".$this->_floor2."' AND floor2 > 0";
            }
        }
        
        /*room_condtion*/
        if( $this->_room1 > 0 ||  $this->_room2 > 0 )
        {   
            $cond_where .= " AND room >= '".$this->_room1."' AND room <= '".$this->_room2."'";
        }
        
        /*keyword_condtion*/
        if( $this->_keyword != '' )
        {
            $cond_where	.= " AND keyword LIKE '".$this->_keyword."%' ";
        }
        
        return $cond_where;
    }
    
    
    /**
    * _get_ask_where_condtion
    *
    * 获取用户查询条件（ask）
    *
    * @access	public
    * @param	void
    * @return	string   用户的查询条件
    */
    private function _get_ask_where_condtion()
    {   
        $cond_where = '';
        
        /*type_condtion*/
        if( $this->_qa_type > 0 )
        {
            $cond_where	.= " AND type = '".$this->_qa_type."' ";
        }
        
        /*class_condtion*/
        if( $this->_qa_class > 0 )
        {
            $cond_where	.= " AND class = '".$this->_qa_class."' ";
        }
        
        /*is_best_condtion*/
        if( $this->_is_best > 0 )
        {
            $cond_where	.= " AND is_best = '".$this->_is_best."' ";
        }
        

        /*keyword_condtion*/
        if( $this->_keyword != '' )
        {
            $cond_where	.= " AND keyword LIKE '".$this->_keyword."%' ";
        }
        
        return $cond_where;
    }



	/**
    * _get_school_where_condtion
    *
    * 获取用户查询条件（ask）
    *
    * @access	public
    * @param	void
    * @return	string   用户的查询条件
    */
    private function _get_school_where_condtion()
    {   
        $cond_where = '';
        
        /*district_condtion*/
        if( $this->_district > 0 )
        {
            $cond_where	.= " AND district = '".$this->_district."' ";
        }
        
        /*street_condtion*/
        if( $this->_street > 0 )
        {
            $cond_where	.= " AND streetid = '".$this->_street."' ";
        }
        
        /*rank_condtion*/
        if( $this->_rank > 0 )
        {
            $cond_where	.= " AND rank = '".$this->_rank."' ";
        }

		/*rank_condtion*/
        if( $this->_category_sh > 0 )
        {
            $cond_where	.= " AND category_sh = '".$this->_category_sh."' ";
        }
		
        /*keyword_condtion*/
        if( $this->_keyword != '' )
        {
            $cond_where	.= " AND keyword LIKE '".$this->_keyword."%' ";
        }
        
        return $cond_where;
    }
    
    
    /**
    * get_behavior_logs_count
    *
    * 获取用户行为记录节总数
    *
    * @access	public
    * @param	string	$day 日期（eg:20140313）
    * @return	int   登录日志记录结果数
    */
    public function get_behavior_logs_count( $day )
    {
        //公用查询条件
        $cond_where = $this->_get_where_condtion();

        $day = $day != '' ? $day : date('Ymd');
        $tbl_name = $this->_get_tblname_by_date($day);
        
        /*sql_select*/
        $sql = " SELECT COUNT(*) FROM $tbl_name ";
        $sql .=  $cond_where ;
        
        //查询结果
        $result = $this->_dbback_user->GetOne($sql);

        return $result;
    }
    
    
    /**
    * get_behavior_logs
    *
    * 获取用户行为记录行为
    *
    * @access	public
    * @param	string	$day 日期（eg:20140313）
    * @param	int $offset 网站编号
    * @param	int $limit 网站位置标识
    * @param	int $order_key 操作类型
    * @param	string	$sort 操作内容
    * @return	array   登录日志记录结果
    */
    public function get_behavior_logs( $day , $offset = 0 , $limit = 10 , $order_key = 'datetime' , $sort = 'ASC')
    {   
        //公用查询条件
        $cond_where = $this->_get_where_condtion();
        
        /*order_query*/
        $cond_order = " ORDER BY $order_key  $sort ";

        /*limit_query*/
        $cond_limit = " LIMIT $offset , $limit ";

        $day = $day != '' ? $day : date('Ymd');
        $tbl_name = $this->_get_tblname_by_date($day);
        
        /*sql_select*/
        $sql = " SELECT * FROM $tbl_name ";
        $sql .=  $cond_where . $cond_order . $cond_limit;

        //判断表是不是存在
        $is_exist = $this->_tbl_exists($tbl_name);

        if( $is_exist )
        {
            $result = $this->_dbback_user->GetAll($sql); //查询结果
        }
        else
        {
            $result = array();
        }

        return $result;
    }


    /**
    * get_behavior_sell_logs_count
    *
    * 获取用户行为记录节总数
    *
    * @access	public
    * @param	string	$day 日期（eg:20140313）
    * @param	string	$day2 日期（eg:20140313）
    * @return	int   登录日志记录结果数
    */
    public function get_behavior_sell_logs_count( $day , $day2 )
    {   
        //公用查询条件
        $cond_where = $this->_get_where_condtion();
        
        //SELL频道查询条件
        $cond_where .= $this->_get_house_where_condtion();
        
        /*time_query*/
        $day = $day != '' ? $day : date('Ymd');
        $day2 = $day2 != '' ? $day2 : date('Ymd');
        $start_time = strtotime($day);
        $end_time = strtotime($day2) + 86399;

        $cond_where .= " AND datetime >= '".$start_time."' AND datetime < '".$end_time."'";
                
        /*sql_select*/
        $sql = " SELECT COUNT(*) FROM $this->_log_behavior_sell_tbl ";
        $sql .=  $cond_where ;
        
        //查询结果
        $result = $this->_dbback_user->GetOne($sql);
        
        return $result;
    }
    
    
    /**
    * get_behavior_sell_logs
    *
    * 获取二手房用户行为记录行为
    *
    * @access	public
    * @param	string	$day 日期（eg:20140313）
    * @param	string	$day2 日期（eg:20140313）
    * @param	int $offset 网站编号
    * @param	int $limit 网站位置标识
    * @param	int $order_key 操作类型
    * @param	string	$sort 操作内容
    * @return	array   登录日志记录结果
    */
    public function get_behavior_sell_logs( $day , $day2 ,$offset = 0 , $limit = 10 , $order_key = 'datetime' , $sort = 'ASC')
    {   
        //公用查询条件
        $cond_where = $this->_get_where_condtion();
        
        //SELL频道查询条件
        $cond_where .= $this->_get_house_where_condtion();
        
        /*order_query*/
        $cond_order = " ORDER BY $order_key  $sort ";

        /*limit_query*/
        $cond_limit = " LIMIT $offset , $limit ";

        $day = $day != '' ? $day : date('Ymd');
        $day2 = $day2 != '' ? $day2 : date('Ymd');
        $start_time = strtotime($day);
        $end_time   = strtotime($day2) + 86399;

        $cond_where .= " AND datetime >= '".$start_time."' AND datetime < '".$end_time."'";
        
        /*sql_select*/
        $sql = " SELECT * FROM $this->_log_behavior_sell_tbl ";
        $sql .=  $cond_where . $cond_order . $cond_limit;
        $result = $this->_dbback_user->GetAll($sql); //查询结果

        return $result;
    }
    
    
    /**
    * get_behavior_block_logs_count
    *
    * 获取用户行为记录节总数
    *
    * @access	public
    * @param	string	$day 日期（eg:20140313）
    * @param	string	$day2 日期（eg:20140313）
    * @return	int   登录日志记录结果数
    */
    public function get_behavior_block_logs_count( $day , $day2 )
    {   
        //公用查询条件
        $cond_where = $this->_get_where_condtion();
        
        //SELL频道查询条件
        $cond_where .= $this->_get_house_where_condtion();
        
        /*time_query*/
        $day = $day != '' ? $day : date('Ymd');
        $day2 = $day2 != '' ? $day2 : date('Ymd');
        $start_time = strtotime($day);
        $end_time = strtotime($day2) + 86399;

        $cond_where .= " AND datetime >= '".$start_time."' AND datetime < '".$end_time."'";
                
        /*sql_select*/
        $sql = " SELECT COUNT(*) FROM $this->_log_behavior_block_tbl ";
        $sql .=  $cond_where ;
        
        //查询结果
        $result = $this->_dbback_user->GetOne($sql);
        
        return $result;
    }
    
    
    /**
    * get_behavior_block_logs
    *
    * 提交用户操作数据节点
    *
    * @access	public
    * @param	string	$day 日期（eg:20140313）
    * @param	string	$day2 日期（eg:20140313）
    * @param	int $offset 网站编号
    * @param	int $limit 网站位置标识
    * @param	int $order_key 操作类型
    * @param	string	$sort 操作内容
    * @return	array   登录日志记录结果
    */
    public function get_behavior_block_logs( $day , $day2 ,$offset = 0 , $limit = 10 , $order_key = 'datetime' , $sort = 'ASC')
    {   
        //公用查询条件
        $cond_where = $this->_get_where_condtion();
        
        //SELL频道查询条件
        $cond_where .= $this->_get_house_where_condtion();
        
        /*order_query*/
        $cond_order = " ORDER BY $order_key  $sort ";

        /*limit_query*/
        $cond_limit = " LIMIT $offset , $limit ";

        $day = $day != '' ? $day : date('Ymd');
        $day2 = $day2 != '' ? $day2 : date('Ymd');
        $start_time = strtotime($day);
        $end_time   = strtotime($day2) + 86399;

        $cond_where .= " AND datetime >= '".$start_time."' AND datetime < '".$end_time."'";
        
        /*sql_select*/
        $sql = " SELECT * FROM $this->_log_behavior_block_tbl ";
        $sql .=  $cond_where . $cond_order . $cond_limit;
        $result = $this->_dbback_user->GetAll($sql); //查询结果
        
        return $result;
    }
    
    
    /**
    * get_behavior_rent_logs_count
    *
    * 获取用户行为记录节总数
    *
    * @access	public
    * @param	string	$day 日期（eg:20140313）
    * @param	string	$day2 日期（eg:20140313）
    * @return	int   登录日志记录结果数
    */
    public function get_behavior_rent_logs_count( $day , $day2 )
    {   
        //公用查询条件
        $cond_where = $this->_get_where_condtion();
        
        //SELL频道查询条件
        $cond_where .= $this->_get_house_where_condtion();
        
        /*time_query*/
        $day = $day != '' ? $day : date('Ymd');
        $day2 = $day2 != '' ? $day2 : date('Ymd');
        $start_time = strtotime($day);
        $end_time = strtotime($day2) + 86399;

        $cond_where .= " AND datetime >= '".$start_time."' AND datetime < '".$end_time."'";
                
        /*sql_select*/
        $sql = " SELECT COUNT(*) FROM $this->_log_behavior_rent_tbl ";
        $sql .=  $cond_where ;
        
        //查询结果
        $result = $this->_dbback_user->GetOne($sql);
        
        return $result;
    }
    
    
    /**
    * get_behavior_rent_logs
    *
    * 获取租房频道用户操作记录
    *
    * @access	public
    * @param	string	$day 日期（eg:20140313）
    * @param	string	$day2 日期（eg:20140313）
    * @param	int $offset 网站编号
    * @param	int $limit 网站位置标识
    * @param	int $order_key 操作类型
    * @param	string	$sort 操作内容
    * @return	array   日志记录结果
    */
    public function get_behavior_rent_logs( $day , $day2 ,$offset = 0 , $limit = 10 , $order_key = 'datetime' , $sort = 'ASC')
    {   
        //公用查询条件
        $cond_where = $this->_get_where_condtion();
        
        //SELL频道查询条件
        $cond_where .= $this->_get_house_where_condtion();
        
        /*order_query*/
        $cond_order = " ORDER BY $order_key  $sort ";

        /*limit_query*/
        $cond_limit = " LIMIT $offset , $limit ";

        $day = $day != '' ? $day : date('Ymd');
        $day2 = $day2 != '' ? $day2 : date('Ymd');
        $start_time = strtotime($day);
        $end_time   = strtotime($day2) + 86399;

        $cond_where .= " AND datetime >= '".$start_time."' AND datetime < '".$end_time."'";
        
        /*sql_select*/
        $sql = " SELECT * FROM $this->_log_behavior_rent_tbl ";
        $sql .=  $cond_where . $cond_order . $cond_limit;
        $result = $this->_dbback_user->GetAll($sql); //查询结果
        
        return $result;
    }
    
    
    /**
    * get_behavior_ask_logs_count
    *
    * 获取用户行为记录节总数
    *
    * @access	public
    * @param	string	$day 日期（eg:20140313）
    * @param	string	$day2 日期（eg:20140313）
    * @return	int   登录日志记录结果数
    */
    public function get_behavior_ask_logs_count( $day , $day2 )
    {   
        //公用查询条件
        $cond_where = $this->_get_where_condtion();
        
        //SELL频道查询条件
        $cond_where .= $this->_get_ask_where_condtion();
        
        /*time_query*/
        $day = $day != '' ? $day : date('Ymd');
        $day2 = $day2 != '' ? $day2 : date('Ymd');
        $start_time = strtotime($day);
        $end_time = strtotime($day2) + 86399;

        $cond_where .= " AND datetime >= '".$start_time."' AND datetime < '".$end_time."'";
                
        /*sql_select*/
        $sql = " SELECT COUNT(*) FROM $this->_log_behavior_ask_tbl ";
        $sql .=  $cond_where ;
        
        //查询结果
        $result = $this->_dbback_user->GetOne($sql);
        
        return $result;
    }
    
    
    /**
    * get_behavior_ask_logs
    *
    * 获取问答频道用户操作记录
    *
    * @access	public
    * @param	string	$day 日期（eg:20140313）
    * @param	string	$day2 日期（eg:20140313）
    * @param	int $offset 网站编号
    * @param	int $limit 网站位置标识
    * @param	int $order_key 操作类型
    * @param	string	$sort 操作内容
    * @return	array   日志记录结果
    */
    public function get_behavior_ask_logs( $day , $day2 ,$offset = 0 , $limit = 10 , $order_key = 'datetime' , $sort = 'ASC')
    {   
        //公用查询条件
        $cond_where = $this->_get_where_condtion();
        
        //SELL频道查询条件
        $cond_where .= $this->_get_ask_where_condtion();
        
        /*order_query*/
        $cond_order = " ORDER BY $order_key  $sort ";

        /*limit_query*/
        $cond_limit = " LIMIT $offset , $limit ";

        $day = $day != '' ? $day : date('Ymd');
        $day2 = $day2 != '' ? $day2 : date('Ymd');
        $start_time = strtotime($day);
        $end_time   = strtotime($day2) + 86399;

        $cond_where .= " AND datetime >= '".$start_time."' AND datetime < '".$end_time."'";
        
        /*sql_select*/
        $sql = " SELECT * FROM $this->_log_behavior_ask_tbl ";
        $sql .=  $cond_where . $cond_order . $cond_limit;
        $result = $this->_dbback_user->GetAll($sql); //查询结果
        
        return $result;
    }


	/**
    * get_behavior_school_logs_count
    *
    * 获取用户行为记录节总数
    *
    * @access	public
    * @param	string	$day 日期（eg:20140313）
    * @param	string	$day2 日期（eg:20140313）
    * @return	int  日志总数
    */
    public function get_behavior_school_logs_count( $day , $day2 )
    {   
        //公用查询条件
        $cond_where = $this->_get_where_condtion();
        
        //SELL频道查询条件
        $cond_where .= $this->_get_house_where_condtion();
        
        /*time_query*/
        $day = $day != '' ? $day : date('Ymd');
        $day2 = $day2 != '' ? $day2 : date('Ymd');
        $start_time = strtotime($day);
        $end_time = strtotime($day2) + 86399;

        $cond_where .= " AND datetime >= '".$start_time."' AND datetime < '".$end_time."'";
                
        /*sql_select*/
        $sql = " SELECT COUNT(*) FROM $this->_log_behavior_school_tbl ";
        $sql .=  $cond_where ;
        
        //查询结果
        $result = $this->_dbback_user->GetOne($sql);
        
        return $result;
    }


	/**
    * get_behavior_school_logs
    *
    * 获取学区频道用户操作记录
    *
    * @access	public
    * @param	string	$day 日期（eg:20140313）
    * @param	string	$day2 日期（eg:20140313）
    * @param	int $offset 网站编号
    * @param	int $limit 网站位置标识
    * @param	int $order_key 操作类型
    * @param	string	$sort 操作内容
    * @return	array   日志记录结果
    */
    public function get_behavior_school_logs( $day , $day2 ,$offset = 0 , $limit = 10 , $order_key = 'datetime' , $sort = 'ASC')
    {   
        //公用查询条件
        $cond_where = $this->_get_where_condtion();
        
        //学区频道查询条件
        $cond_where .= $this->_get_school_where_condtion();
        
        /*order_query*/
        $cond_order = " ORDER BY $order_key  $sort ";

        /*limit_query*/
        $cond_limit = " LIMIT $offset , $limit ";

        $day = $day != '' ? $day : date('Ymd');
        $day2 = $day2 != '' ? $day2 : date('Ymd');
        $start_time = strtotime($day);
        $end_time   = strtotime($day2) + 86399;

        $cond_where .= " AND datetime >= '".$start_time."' AND datetime < '".$end_time."'";
        
        /*sql_select*/
        $sql = " SELECT * FROM $this->_log_behavior_school_tbl ";
        $sql .=  $cond_where . $cond_order . $cond_limit;
        $result = $this->_dbback_user->GetAll($sql); //查询结果

        return $result;
    }
}
/* End of file UserBehavior.class.php */
/* Location: ../core/lib/UserBehavior.class.php*/