<?php if (!defined('BASEPATH'))  exit('No direct script access allowed');


class UserTest_model extends MY_Model
{

	function __construct()
	{
		parent::__construct();
		$this->memkey = 'usercent'.GB_CITY;
        $this->load->library('session');
        $this->load->model("house_model");
        $this->load->model("block_model");
		//移到config/constants.php文件中
		define('UCENTER_CHECK_PHONE','http://api.house365.com/sh/usercenter/ucenter_checkphone.php');//检测全站用户中心是否有此电话
		define('UCENTER_SEND_AUTH_CODE','http://api.house365.com/sh/usercenter/ucenter_sendauthcode.php');//获取全站用户中心验证码
		define('UCENTER_REISTER','http://api.house365.com/sh/usercenter/ucenter_register.php');//注册到全站用户中心
		define('UCENTER_LOGIN','http://api.house365.com/sh/usercenter/ucenter_login.php');//全站用户中心登录
		define('UCENTER_FORGET_PWD','http://api.house365.com/sh/usercenter/ucenter_forgetpwd.php');//全站用户中心重置密码
		define('UCENTER_CHECK_LOGIN','http://api.house365.com/sh/usercenter/check_login.php');//全站用户中心检测用户是否登录
		define('UCENTER_SSO_TOKEN','http://passport.house365.com/');
	}

	 /**
	 * 查询验证码是否有效
	 * @param type $telno 手机号码 $validcode 验证码
	 * @return string
	 */
	function yzusersms($validcode,$telno,$kind=3)
	{
		$vtime = time();
        //短信有效时间为30分钟
		$telno_isset =  $this->get_data(array('form_name' => 'personal_sms','limit'=>1,'where'=>array('telno'=>$telno,'validcode'=>$validcode,'dateline  >'=>($vtime-60*30),'kind'=>$kind,'esta'=>0),'select'=>array('telno')),'esfbak');
		if(!empty($telno_isset[0]['telno']))
		{
			$this->updatesms($telno,$validcode);
			return 1;
		}else{
			return 0;
		}
	}
	
	/**
	 * 查询验证码今天是否已经发送,如果已经发送,那就发送上次的验证码
	 * @param type $telno 手机号码
	 * @return string
	 */
	function check_yzm_today($telno)
	{
		$time_start =strtotime(date('Y-m-d 00:00:00'));
		
		$time_end =strtotime(date('Y-m-d 23:59:59'));

		$telno_isset =  $this->get_data(array('form_name' => 'personal_sms','limit'=>1,'where'=>array('telno'=>$telno,'dateline  >'=>($time_start),'dateline  <'=>($time_end),'kind'=>1,'esta'=>0),'select'=>array('validcode')),'esfbak');
		if(!empty($telno_isset[0]['validcode']))
		{
			return $telno_isset[0]['validcode'];
		}else{
			return 0;
		}
		
	}

	function check_yzm_today_orderby_id($telno)
	{
		$time_start =strtotime(date('Y-m-d 00:00:00'));
		
		$time_end =strtotime(date('Y-m-d 23:59:59'));

		$telno_isset =  $this->get_data(array('form_name' => 'personal_sms','limit'=>1,'where'=>array('telno'=>$telno,'dateline  >'=>($time_start),'dateline  <'=>($time_end),'kind'=>1),'select'=>array('validcode','esta','dateline'),'order_by'=>'id desc'),'esfbak');
		
		if($telno_isset[0]['esta']=='0')
		{
			return $telno_isset[0]['validcode'];
		}else{
			return 0;
		}
	}

	function check_yzm_today_orderby_by_kind_four($telno)
	{
		$time_start =strtotime(date('Y-m-d 00:00:00'));
		
		$time_end =strtotime(date('Y-m-d 23:59:59'));

		$telno_isset =  $this->get_data(array('form_name' => 'personal_sms','limit'=>1,'where'=>array('telno'=>$telno,'dateline  >'=>($time_start),'dateline  <'=>($time_end),'kind'=>4),'select'=>array('validcode','esta','dateline'),'order_by'=>'id desc'),'esfbak');
		
		if($telno_isset[0]['esta']=='0')
		{
			return $telno_isset[0]['validcode'];
		}else{
			return 0;
		}
	}


		 /**
	 * 查询验证码是否有效
	 * @param type $telno 手机号码 $validcode 验证码
	 * @return string
	 */
	function yzsms($telno,$kind=3)
	{
		  
		$telno_isset =  $this->get_data(array('form_name' => 'personal_sms','limit'=>1,'where'=>array('telno'=>$telno,'kind'=>$kind),'select'=>array
('telno','esta','validcode'),'order_by'=>'id desc'),'esfbak');
		if(empty($telno_isset[0]['validcode'])||$telno_isset[0]['esta']==1)
		{
			
			return 0;//
		}elseif(!empty($telno_isset[0]['validcode'])&&$telno_isset[0]['esta']==0){
			return $telno_isset[0]['validcode'];
		}

	}





    /**
	 * 查询验证码是否有效
	 * @param type $telno 手机号码 $kind 验证码种类 $sec 时间间隔
	 * @return string
	 */
	function yzusersmstime( $sec , $telno , $kind = 3 )
	{
		$vtime = time();
		$limit_time = $vtime - $sec;

 		$telno_isset =  $this->get_data(array('form_name' => 'personal_sms','limit'=>1,'where'=>array('telno'=>$telno,'dateline  >='=>$limit_time,'kind'=>$kind,'esta'=>0),'select'=>array('telno')),'esf');
		if(!empty($telno_isset[0]['telno']))
		{
			return 1;
		}else{
			return 0;
		}
	}

    /**
	 * 查询验证码是否有效
	 * @param type $telno 手机号码 $validcode 验证码
	 * @return string
	 */
	function yzcollect($searchcond,$kind,$uid,$city=GB_CITY)
	{
       $collect_isset =  $this->get_data(array('form_name' => 'personal_collection','limit'=>1,'where'=>array('collect_value'=>$searchcond,'kind'=>$kind,'uid'=>$uid,'city_name'=>$city),'select'=>array('id')),'esfbak');

		if(!empty($collect_isset[0]['id']))
		{
			return 1;
		}else{
			return 0;
		}
	}

	/**
	 *APP发送短信
	 */
	function addSendSms($tt)
	 {
		$this->set_table('api_sms');
		$result = $this->add($tt,'esf');		
		return $result;
	 
	 }

	 /**
	 * 验证服务密码登陆
	 * @param type $telno 手机号码 $password 服务密码
	 * @return string
	 */
	function yzloginfw($telno,$password)
	{
      if(empty($password) || $telno=='')
       {
        return 0;
       }
	  $user_id =  $this->get_data(array('form_name' => 'personal_user','limit'=>1,'where'=>array('telno'=>$telno,'password'=>md5($password),'is_use'=>1),'select'=>array('id')),'esfbak');
	  $user_id1 =  $this->get_data(array('form_name' => 'personal_user','limit'=>1,'where'=>array('telno'=>$telno,'password1'=>md5($password),'is_use'=>1),'select'=>array('id')),'esfbak');
		if(!empty($user_id[0]['id']) || !empty($user_id1[0]['id']))
		{
			return $user_id[0]['id']?$user_id[0]['id']:$user_id1[0]['id'];
		}else{
			return 0;
		}
	}

	 /**
	 * 验证动态密码登陆
	 * @param type $telno 手机号码 $validcode 验证码
	 * @return string
	 */
	function yzlogindt($telno,$validcode)
	{

		$vtime = time();
		//验证短信
		$sms_id = $this->get_data(array('form_name' => 'personal_sms','limit'=>1,'where'=>array('telno'=>$telno,'validcode'=>$validcode,'kind'=>1,'dateline  >'=>($vtime-60*10)),'select'=>array('id')),'esfbak');

		if(!empty($sms_id[0]['id']))
		{
			$user_id =  $this->get_data(array('form_name' => 'personal_user','limit'=>1,'where'=>array('telno'=>$telno,'is_use'=>1),'select'=>array('id','password')),'esfbak');

			if(!empty($user_id[0]['id']))
			{
				$this->updatesms($telno,$validcode);
				return $user_id[0];
			}
		}
		else
		{
			################2016-7-12防止暴力破解start#################
			$this->stopForceCrack($telno);
			################2016-7-12防止暴力破解end###################
			return 0;
		}
	}

	/**
	 *防止暴力破解
	 *
	 */
	function stopForceCrack($telno)
	{
		$vtime = time()-600;
		$dateline =date('Y-m-d');
		$nj_mysql_obj = mysql_connect('njesfdbmastervip.house365.com','root','idontcare') or die('error');
		mysql_query('set names gbk');
		mysql_select_db('user');
		mysql_query('set autocommit=0');
	
		$sql = "select id,num from personal_sms_log where telno='".$telno."' && dateline='".$dateline."' for update";
		$query = mysql_query($sql);
		
		$publish_house_concurrent_log_data = array();
		while($info = mysql_fetch_assoc($query))
		{
			$publish_house_concurrent_log_data = $info;

			if($info['num'] >= '10')
			{
				$sql = "update personal_sms set esta ='1' where telno='$telno' and dateline >'$vtime' and kind =1";
				mysql_query($sql);
				break;
			}
			else
			{
				$info['num'] += 1;
				$sql = "update personal_sms_log set num='".$info['num']."' where id='".$info['id']."'";
				mysql_query($sql);

				$sql = "delete from personal_sms_log where telno='$telno'";
				mysql_query($sql);
				break;
			}
		}

		if(empty($publish_house_concurrent_log_data))
		{
			$sql = "insert into personal_sms_log(id,telno,num,dateline) values('','".$telno."','1','".$dateline."')";
			mysql_query($sql);
		}
	
		//mysql_query('unlock table');
		mysql_query('commit');
		mysql_close($nj_mysql_obj);
	
	}


	 /**
	 * 验证用户名或者手机登陆
	 * @param type $name 手机号码或者用户名 $password 服务密码
	 * @return string
	 */
	function yzlogin($name,$password)
	{
        if(empty($password) || empty($name))
        {
            return 0;
        }
		//验证是否是手机登陆
        $sql = "select id from personal_user where telno='".$name."' and is_use=1 and (password='".md5($password)."' or password1='".md5($password)."')";
		$query = $this->db_esfbak->query($sql);
        $tel_id = $query->result_array();

        //验证是否是用户名登陆
        $sql = "select id from personal_user where username='".$name."' and is_use=1 and (password='".md5($password)."' or password1='".md5($password)."')";
		$query = $this->db_esfbak->query($sql);
        $user_id = $query->result_array();

		if(!empty($tel_id[0]['id']))
		{
			$this->updatelogintime($tel_id[0]['id']);
    		return $tel_id[0]['id'];

		}elseif(!empty($user_id[0]['id'])){
		 	$this->updatelogintime($user_id[0]['id']);
    		return $user_id[0]['id'];

	    }else{
            return 0;

        }
	}


	/**
	 * 短信使用后，对短信状态进行更新
	 * @param type $telno 手机号码 $validcode 验证码
	 * @return string
	 */
	function updatesms($telno,$validcode)
	{
	   if(empty($telno) || empty($validcode))
	   {
	     return 0;exit;
	   }

		$arr=array('telno'=>$telno,'esta'=>0,'validcode'=>$validcode);
		$up= array('esta'=>1);

		$this->set_table('personal_sms');
		$result = $this->save($arr,$up,'esf');

		return $result;
	}

	/**
	 * 消息点击更新状态
	 * @param type $id 消息id
	 * @return string
	 */
	function updateannounce($id)
	{
		if(empty($id))
	   {
	     return 0;exit;
	   }
		$arr = array('id'=>$id);
		$up = array('new'=>1);

		$this->set_table('personal_announceitem');
		$result = $this->save($arr,$up,'esf');

		return $result;
	}


	/**
	 * 消息点击更新状态
	 * @param type $uid 用户id
	 * @return string
	 */
	function updateannounceclick($uid)
	{
		if(empty($uid))
	   {
	     return 0;exit;
	   }
		$arr = array('uid'=>$uid);
		$up = array('is_click'=>1);

		$this->set_table('personal_announceitem');
		$result = $this->save($arr,$up,'esf');

		return $result;
	}

	 /**
	 * 登陆后对登陆时间进行修改
	 * @param type $uid 用户id
	 * @return string
	 */
	function updatelogintime($uid)
	{
	   if(empty($uid))
	   {
	     return 0;exit;
	   }
		$arr = array('id'=>$uid);
		$up = array('lastlogintime'=>time());

		$this->set_table('personal_user');
		$result = $this->save($arr,$up,'esf');

		return $result;
	}

	/**
	 * 登陆后对登陆时间进行修改
	 * @param type $uid 用户id
	 * @return string
	 */
	function updateheadpic($uid,$pic)
	{
	   if(empty($uid)|| empty($pic))
	   {
	     return 0;exit;
	   }
		$arr=array('id'=>$uid);
		$up= array('head_pic'=>$pic);

		$this->set_table('personal_user');
		$result = $this->save($arr,$up,'esf');

		return $result;
	}


	/**
	* 修改登录密码
	* @param type $telno 手机号码 $password 新密码
	* @return string
	*/
	function updatepassowrd($telno,$password)
	{
	   if(empty($telno)|| empty($password))
	   {
	     return 0;exit;
	   }
		$arr = array('telno'=>$telno);
		$up	= array('password'=>md5($password),'password1'=>'');

		$this->set_table('personal_user');
        if(!empty($telno))
		{
		$result = $this->save($arr,$up,'esf');
		if($result>0)
		{
		 get_api("http://mtapi.house365.com/?method=user.modifypwd&phone=".$telno."&newpassword=".$password);
		}
		}else{
		$result = 0;
		}
		return $result;
	}


	/**
	* 修改手机号码
	* @param type $uid 手机号码 $telno 新密码
	* @return string
	*/
	function updatetelnobyuid($uid,$telno)
	{
	   if(empty($uid)|| empty($telno))
	   {
	     return 0;exit;
	   }
		$arr =array('id'=>$uid);
		$up = array('telno'=>$telno);

		$this->set_table('personal_user');
		$result = $this->save($arr,$up,'esf');
        if($result>0)
        {
          //隐藏手机号部分数字
          $pattern = "/(1\d{1,2})\d\d(\d{0,3})/";
          $replacement = "\$1****\$3";
          $telno2 = preg_replace($pattern, $replacement, trim($telno));
          $showname = $telno2 ? $telno2 : $name ;
          setcoandsess('esfusertelno',$telno);
          setcoandsess('esfshowname',$showname);
        }
		return $result;
	}


	/**
	* 修改登录密码by uid
	* @param type $uid 用户id $password 新密码
	* @return string
	*/
	function updatepassowrdbyuid($uid,$password)
	{
        if( empty($uid) || empty($password))
        {
            return 0;
        }

        $arr = array('id'=>$uid);
        $up = array( 'password' => md5($password) , 'password1' => '');

        $this->set_table('personal_user');
        $result = $this->save( $arr , $up , 'esf' );

        if( $result > 0 )
        {
            $user_array = $this->getuserdetail($uid);
            $telno = $user_array['telno'];

            //验证手机号码是否为空，不为空才提交
            if( isset($telno) && strlen($telno) == 11 )
            {
                $return_array = get_api("http://mtapi.house365.com/?method=user.modifypwd&phone=".$telno."&newpassword=".$password);
            }
        }

        return $result;
	}


	/**
	* 验证用户中心登录判断
	* @return string
	*/
	function check_user($esfuserid,$esfpassword)
	{
		if( $esfuserid <> '' )
		{

			if( isset($_SESSION['esfuserid']) && $_SESSION['esfuserid'] > 0 )
			{
				return $_SESSION['esfuserid'];
			}
			else
			{
				//用户退出后清除本地session
				unset($_SESSION['esfuserid']);
				$check_login = $this->yzloginuser($esfuserid,$esfpassword);

				if( $check_login > 0 )
				{
					$_SESSION['esfuserid']=$check_login;
                    $user = $this->getuserdetail($check_login);

                    //赋值SESSION
                    $array = array( 'esfuserid'=>$user['id'] , 'esfpassword'=>$user['password'] ,'esfusername'=>$user['username'] , 'esfusertelno'=>$user['telno'] ,'esfusercity'=>GB_CITY,'esfshowname'=>$_COOKIE['esfshowname']);
                    $this->session->set_userdata($array);

                	return $_SESSION['esfuserid'];
				}
				else
				{
					return 0;
				}
			}
		}
		else
		{
			return 0;
		}
	}


	/**
	* 验证用户id登陆
	* @param type $uid 用户uid $password 加密后的服务密码
	* @return string
	*/
	function yzloginuser($uid,$password)
	{
        if(empty($password) || empty($uid))
        {
            return 0;
        }

		$user_id =  $this->get_data(array('form_name' => 'personal_user','limit'=>1,'where'=>array('id'=>$uid,'password'=>$password,'is_use'=>1),'select'=>array('id')),'esfbak');
		$user_id1 =  $this->get_data(array('form_name' => 'personal_user','limit'=>1,'where'=>array('id'=>$uid,'password1'=>$password,'is_use'=>1),'select'=>array('id')),'esfbak');

		if(!empty($user_id[0]['id']) || !empty($user_id1[0]['id']))
		{
			return $user_id[0]['id']?$user_id[0]['id'] : $user_id1[0]['id'];
		}else{
			return 0;
		}
	}

    	/**
	* 验证用户id登陆
	* @param type $telno 用户  $password 加密后的服务密码
	* @return string
	*/
	function yzlogintelno($telno,$password)
	{
        if(empty($password) || $telno=='')
        {
         return 0;
        }
		$user_id =  $this->get_data(array('form_name' => 'personal_user','limit'=>1,'where'=>array('telno'=>$telno,'password'=>$password,'is_use'=>1),'select'=>array('id')),'esfbak');
		$user_id1 =  $this->get_data(array('form_name' => 'personal_user','limit'=>1,'where'=>array('telno'=>$telno,'password1'=>$password,'is_use'=>1),'select'=>array('id')),'esfbak');
		if(!empty($user_id[0]['id']) || !empty($user_id1[0]['id']))
		{
			return $user_id[0]['id']?$user_id[0]['id']:$user_id1[0]['id'];
		}else{
			return 0;
		}
	}


	/**
	* 验证是否有该用户
	* @param type $uid
	* @return string
	*/
	function yzuser($uid)
	{
		$user_id =  $this->get_data(array('form_name' => 'personal_user','limit'=>1,'where'=>array('id'=>$uid,'is_use'=>1),'select'=>array('id')),'esfbak');

		if( !empty($user_id[0]['id']) )
		{
			return $user_id[0]['id'];
		}else{
			return 0;
		}
	}


	/**
	* 验证是否该用户已注册过手机号
	* @param type $uid
	* @return string
	*/
	function yztelno($telno)
	{
		$user_id =  $this->get_data(array('form_name' => 'personal_user','limit'=>1,'where'=>array('telno'=>$telno,'is_use'=>1),'select'=>array('id')),'esf');

		if(!empty($user_id[0]['id']))
		{
			return $user_id[0]['id'];
		}else{
			return 0;
		}
	}


    /**
	* 验证是否该用户已注册过手机号,论坛用户绑定手机专用
	* @param type $uid
	* @return string
	*/
	function yztelnolt($telno)
	{
		$user_id =  $this->get_data(array('form_name' => 'personal_user','limit'=>1,'where'=>array('telno'=>$telno,'is_use'=>1),'select'=>array('id','infofrom')),'esfbak');

		if(!empty($user_id))
		{
			return $user_id;
		}else{
			return 0;
		}
	}


	/**
	 * 验证是否该用户已有此用户名
	 * @param type $username
	 * @return string
	 */
	function yzusername($username)
	{
		$user_id =  $this->get_data(array('form_name' => 'personal_user','limit'=>1,'where'=>array('username'=>$username,'is_use'=>1),'select'=>array('id')),'esfbak');

		if(!empty($user_id[0]['id']))
		{
			return $user_id[0]['id'];
		}else{
			return 0;
		}
	}


	 /**
	 * 获得用户收藏总量
	 * @param type $uid
	 * @return string
	 */
	function getusercollect($uid,$kind,$city=GB_CITY,$jmz=0)
	{
	  if($jmz==1){
		   $uidnum =  $this->get_data(array('form_name' => 'personal_collection','where'=>array('passport_uid'=>$uid,'kind'=>$kind,'city_name'=>$city),'select'=>array('count(*) as num')),'esfbak');	  
	  }else{
		   $uidnum =  $this->get_data(array('form_name' => 'personal_collection','where'=>array('uid'=>$uid,'kind'=>$kind,'city_name'=>$city),'select'=>array('count(*) as num')),'esfbak');
	  
	  }
	 

		if(!empty($uidnum[0]))
		{
			return $uidnum[0]['num'];
		}else{
			return 0;
		}
	}


	 /**
	 * 查询是否已关注过此房源
	 * @param type $uid
	 * @return string
	 */
	function getusercollectid($uid,$houseid,$city=GB_CITY,$kind="")
	{
        if($kind){
			$return =  $this->get_data(array('form_name' => 'personal_collection','where'=>array('uid'=>$uid,'collect_id'=>$houseid,'city_name'=>$city,'kind'=>$kind),'select'=>array('id')),'esf');

		}else{
			$return =  $this->get_data(array('form_name' => 'personal_collection','where'=>array('uid'=>$uid,'collect_id'=>$houseid,'city_name'=>$city),'select'=>array('id')),'esf');
		}
		

		if(!empty($return[0]))
		{
			return $return[0];
		}else{
			return 0;
		}
	}
	
	 /**
	 * 查询是否已关注过此房源
	 * @param type $uid
	 * @return string
	 */
	function getcollectbyuid($uid,$kind,$city=GB_CITY,$sendtime='')
	{
        if(!empty($sendtime)){
	    $return =  $this->get_data(array('form_name' => 'personal_collection','where'=>array('uid'=>$uid,'kind'=>$kind,'city_name'=>$city,'collect_time >'=>$sendtime),'limit'=>'1','select'=>array('id','collect_id')),'esfbak');
        }else{
	    $return =  $this->get_data(array('form_name' => 'personal_collection','where'=>array('uid'=>$uid,'kind'=>$kind,'city_name'=>$city),'limit'=>'1','select'=>array('id','collect_id')),'esfbak');
        }

		if(!empty($return[0]))
		{
			return $return[0];
		}else{
			return 0;
		}
	}

	/**
	 * 查询用户关注房源的房源id
	 * @param type $uid
	 * @return string
	 */
	function getCollectHouseId($uid,$kind,$city=GB_CITY){
		$return =  $this->get_data(array('form_name' => 'personal_collection','where'=>array('uid'=>$uid,'kind'=>$kind,'city_name'=>$city),'select'=>array('collect_id')),'esfbak');
		if(!empty($return)){
			return $return;
		}else{
			return array();
		}
	}


	/**
	* 查询是否有此用户
	* @param type $uid
	* @return string
	*/
	function getuserdetail($uid)
	{
		$return =  $this->get_data(array('form_name' => 'personal_user','where'=>array('id'=>$uid,'is_use'=>1),'select'=>array('id','lastlogintime','username','password','telno')),'esfbak');

		if(!empty($return[0]))
		{
			return $return[0];
		}else{
			return 0;
		}
	}

	
	/**
	* 添加关注
	* @param type $uid
	* @return string
	*/
	function addcollect($uid,$collect_id,$kind,$collect_value,$city=GB_CITY,$infofrom=1,$passport_uid=0)
	{
		$arr	= array('uid'=>$uid,'collect_id'=>$collect_id,'kind'=>$kind,'collect_value'=>$collect_value,'collect_time'=>time(),'city_name'=>$city,'infofrom'=>$infofrom,'passport_uid'=>$passport_uid);
		//设置表
		$this->user_model->set_table('personal_collection');
		//过滤表结构
		$result = $this->user_model->add($arr,'esf');

		return $result;
	}

    /**
	* 推广情况
	* @param type $uid
	* @return string
	*/
	function addusercharge($id, $paytype , $payday,$uid,$tbl='sell',$city=GB_CITY)
	{
		$arr	= array('uid'=>$uid,'paytype'=>$paytype,'payday'=>$payday,'rowid'=>$id,'tbl'=>$tbl,'createtime'=>time(),'city_name'=>$city);
		//设置表
		$this->user_model->set_table('personal_order');
		//过滤表结构
		$result = $this->user_model->add($arr,'esf');

		return $result;
	}


	/**
	* 修改会员资料
	* @param type $uid
	* @return string
	*/
	function updateuser($uid,$up)
	{
	    if( empty($uid) )
		{
		    return 0;
		}

		$arr = array( 'id' => $uid );

		$this->set_table('personal_user');
		$result = $this->save($arr,$up,'esfbak');

		return $result;
	}


	/**
	* 获取关注信息
	* @param type $uid
	* @return string
	*/
	function getcollect($kind,$uid,$offset=0,$pagesize=0,$city=GB_CITY)
	{
		$key = $this->memkey.'getcollect'.$uid.$kind.$offset.$pagesize.$city;
		if($this->input->get('update') == 1)
		{
			$this->memcached_class->delete($key);
		}

		$collect_array = $this->memcached_class->get($key);

		if(empty($collect_array) )
		{
			$collect_array =  $this->get_data(array('form_name' => 'personal_collection','where'=>array('uid'=>(int)$uid,'kind'=>$kind,'city_name'=>$city),'limit'=>20,'offset'=>$pagesize),'esfbak');

			$this->memcached_class->add($key, $collect_array,1);
		}

		return $collect_array;
	}

	/**
	* 获取关注信息加盟站专用
	* @param type $uid
	* @return string
	*/
	function getcollectjmz($kind,$uid,$offset=0,$pagesize=0,$city=GB_CITY)
	{
		$key = $this->memkey.'getcollect'.$uid.$kind.$offset.$pagesize.$city;
		if($this->input->get('update') == 1)
		{
			$this->memcached_class->delete($key);
		}

		$collect_array = $this->memcached_class->get($key);

		if(empty($collect_array) )
		{
			$collect_array =  $this->get_data(array('form_name' => 'personal_collection','where'=>array('passport_uid'=>$uid,'kind'=>$kind,'city_name'=>$city),'limit'=>$offset,'offset'=>$pagesize,'order_by'=>'collect_time desc'),'esfbak');

			$this->memcached_class->add($key, $collect_array,1);
		}

		return $collect_array;
	}


    /**
	* 获取关注信息详情
	* @param type $uid
	* @return string
	*/
	function getcollectdetail($kind,$uid,$collect_id,$city=GB_CITY)
	{
		$key = $this->memkey.'getcollect'.$uid.$kind.$collect_id.$city;
		//if($this->input->get('update') == 1)
		///{
			$this->memcached_class->delete($key);
		//}

		$collect_array = $this->memcached_class->get($key);

		if(empty($collect_array) )
		{

			$collect_array =  $this->get_data(array('form_name' => 'personal_collection','where'=>array('uid'=>$uid,'kind'=>$kind,'city_name'=>$city,'collect_id'=>$collect_id),'select'=>array('id')),'esf');
			$this->memcached_class->add($key, $collect_array,10);
		}

		return !empty($collect_array[0]['id'])?$collect_array[0]['id']:0;
	}


	/**
	* getSeachcondStr 转化找房条件
	*
	* @param array $sarr
	* @return array
	*/
	function getSeachcondStr($sarr)
	{
	   global $house;

		$key = $this->memkey.'getSeachcondStr'.serialize($sarr);
		if($this->input->get('update') == 1)
		{
			$this->memcached_class->delete($key);
		}
		$this->memcached_class->delete($key);
		$return = $this->memcached_class->get($key);

		if(empty($return) )
		{
		if(empty($sarr))
		{
			return array();
		}

		$return = array();
		foreach($sarr as $key=>$value)
		{
			$str = '';
		
			$search = unserialize($value['collect_value']);

			
			$str .= $search['keyword'] != '' ? '关键词：'.$search['keyword'].'<em class="t-line">|</em>' : '';

			if(!empty($search['infofrom']))
			{
                if($search['infofrom'] == 6)
                  {
                    $str .='中介<em class="t-line">|</em>';
                  }else{
				    $str .= $house["infofrom"][$search['infofrom']] != '' ? $house["infofrom"][$search['infofrom']].'<em class="t-line">|</em>' : '';
                   }
			}

			if(!empty($search['infotype']))
			{
				$str .= $house['infotype'][$search['infotype']] != '' ? $house['infotype'][$search['infotype']].'<em class="t-line">|</em>' : '';
			}

			if(!empty($search['renttype']))
			{
				$str .= $house['renttype_num'][$search['renttype']] != '' ? $house['renttype_num'][$search['renttype']].'<em class="t-line">|</em>' : '';
			}

			if(!empty($search['roomtype']))
			{
				$str .= $house['roomtype'][$search['roomtype']] != '' ? $house['roomtype'][$search['roomtype']].'<em class="t-line">|</em>' : '';
			}

			if(!empty($search['price_price1']))
			{
				$price1 = intval($search['price_price1']);
				$s_price1 = intval($search['price_price2']);
			}

			if(!empty($search['price_price2']))
			{
				$price2 = intval($search['price_price2']);
				$s_price2 = intval($search['price_price1']);
			}

		   if($search['price_sell'] > 0 && $search['price_sell']!=99 && empty($search['price_office']) && empty($search['price_stores']))
			{
				$str .= $house['price_sell'][$search['price_sell']] != '' ? $house['price_sell'][$search['price_sell']].'<em class="t-line">|</em>' : '';
			}
			elseif($search['price_rent'] > 0 && $search['price_rent']!=99 && empty($search['price_office']) && empty($search['price_stores']))
			{
				$str .= $house['price_rent'][$search['price_rent']] != '' ? $house['price_rent'][$search['price_rent']].'<em class="t-line">|</em>' : '';
			}else{

				if(!empty($search['price_price1']) > 0 || !empty($search['price_price2']) > 0)
				{
					if($price1 > 0 && $s_price2 == "")
					{
						$str .= '大于'.$price1.'万元<em class="t-line">|</em>';
					}
					elseif($price2 > 0 && $s_price1 == "")
					{
						$str .= '0－'.$price2.'万元<em class="t-line">|</em>';
					}
					elseif($price1 < $price2)
					{
						$str .= $price1.'－'.$price2.'万元<em class="t-line">|</em>';
					}
					elseif($price1 == $price2)
					{
						$str .= $price1.'万元<em class="t-line">|</em>';
					}
				}elseif(!empty($search['price1']) > 0 || !empty($search['price2']) > 0){
                    if($search['price1'] > 0 && $search['price2'] == "")
					{
						$str .= '大于'.$search['price1'].'元<em class="t-line">|</em>';
					}
					elseif($search['price2'] > 0 && $search['price1'] == "")
					{
						$str .= '0－'.$search['price2'].'元<em class="t-line">|</em>';
					}
					elseif($search['price1'] < $search['price2'])
					{
						$str .= $search['price1'].'－'.$search['price2'].'元<em class="t-line">|</em>';
					}
					elseif($search['price1'] == $search['price2'])
					{
						$str .= $search['price1'].'元<em class="t-line">|</em>';
					}
                }
			}

			if(!empty($search['buildarea1']))
			{
				$buildarea1 = intval($search['buildarea1']);
				$s_buildarea1 = trim($search['buildarea2']);
			}

			if(!empty($search['buildarea2']))
			{
				$buildarea2 = intval($search['buildarea2']);
				$s_buildarea2 = trim($search['buildarea1']);
			}

			if( isset( $search['buildarea'] ) && $search['buildarea'] > 0 && $search['buildarea']!=99)
			{
				$str .= isset($house['buildarea'][$search['buildarea']]) ? $house['buildarea'][$search['buildarea']].'<em class="t-line">|</em>' : '';
			}
			else
			{
				if(!empty($search['buildarea1']) || !empty($search['buildarea2']))
				{
					if($buildarea1 > 0 && $s_buildarea2 == "")
					{
						$str .= '大于'.$buildarea1.'平米<em class="t-line">|</em>';
					}
					elseif($buildarea2 > 0 && $s_buildarea1 == "")
					{
						$str .= '0－'.$buildarea2.'平米<em class="t-line">|</em>';
					}
					elseif($buildarea1 < $buildarea2)
					{
						$str .= $buildarea1.'－'.$buildarea2.'平米<em class="t-line">|</em>';
					}
					elseif($buildarea1 == $buildarea2)
					{
						$str .= $buildarea1.'平米<em class="t-line">|</em>';
					}
				}
			}

			$floor1 = intval($search['floor1']);
			$s_floor1 = trim($search['floor1']);
			$floor2 = intval($search['floor2']);
			$s_floor2 = trim($search['floor2']);

			if($floor1 > 0 || $floor2 > 0)
			{
				if($floor1 > 0 && $s_floor2 == "")
				{
					$str .= '大于'.$price1.'层<em class="t-line">|</em>';
				}
				elseif($floor2 > 0 && $s_floor1 == "")
				{
					$str .= '0－'.$floor2.'层<em class="t-line">|</em>';
				}
				elseif($floor1 < $floor2)
				{
					$str .= $floor1.'－'.$floor2.'层<em class="t-line">|</em>';
				}
				elseif($floor1 == $floor2)
				{
					$str .= $floor1.'层<em class="t-line">|</em>';
				}
			}

			if(!empty($search['totalf']))
			{
				$str .= $house['totalf'][$search['totalf']] != '' ? $house['totalf'][$search['totalf']].'<em class="t-line">|</em>' : '';
			}

			if(!empty($search['shoptype']))
			{
				$str .= $house['shoptype'][$search['shoptype']] != '' ? $house['shoptype'][$search['shoptype']].'<em class="t-line">|</em>' : '';
			}

			if(!empty($search['priceterm']))
			{
				$str .= $house['priceterm'][$search['priceterm']] != '' ? $house['priceterm'][$search['priceterm']].'<em class="t-line">|</em>' : '';
			}

			if(!empty($search['officetype']))
			{
				$str .= $house['officetype'][$search['officetype']] != '' ? $house['officetype'][$search['officetype']].'<em class="t-line">|</em>' : '';
			}

			if(!empty($search['price_office']))
			{
				$str .= $house['price_office'][$search['price_office']] != '' ? $house['price_office'][$search['price_office']].'<em class="t-line">|</em>' : '';
			}

			if(!empty($search['buildarea_office']))
			{
				$str .= $house['buildarea_office'][$search['buildarea_office']] != '' ? $house['buildarea_office'][$search['buildarea_office']].'<em class="t-line">|</em>' : '';
			}

			if(!empty($search['price_stores']))
			{
				$str .= $house['price_stores'][$search['price_stores']] != '' ? $house['price_stores'][$search['price_stores']].'<em class="t-line">|</em>' : '';
			}

			if(!empty($search['buildarea_stores']))
			{
				$str .= $house['buildarea_stores'][$search['buildarea_stores']] != '' ? $house['buildarea_stores'][$search['buildarea_stores']].'<em class="t-line">|</em>' : '';
			}

			if(!empty($search['hangye']))
			{
				$str .= $house['hangye'][$search['hangye']] != '' ? $house['hangye'][$search['hangye']].'<em class="t-line">|</em>' : '';
			}

			if(!empty($search['shop_staus']))
			{
				$str .= $house['shop_staus'][$search['shop_staus']] != '' ? $house['shop_staus'][$search['shop_staus']].'<em class="t-line">|</em>' : '';
			}

			if(!empty($search['forward']))
			{
				$str .= $search['forward'] != '' ? $search['forward'].'<em class="t-line">|</em>' : '';
			}
			//2015-4-30 产权属性显示
			if(!empty($search['mright']))
			{
				
				if($search['mright']=='1')
				{
					$search['mright']=='产权房';
					$str .= $search['mright'] != '' ? '产权房'.'<em class="t-line">|</em>' : '';
				}
				elseif($search['mright']=='2')
				{
					
					$search['mright']=='使用权房';
					$str .= $search['mright'] != '' ? '使用权房'.'<em class="t-line">|</em>' : '';
				}
				
				
			}

			if(!empty($search['fitment']) && $search['fitment']!='任意')
			{

			 $str .= $search['fitment'] != '' ? $search['fitment'].'<em class="t-line">|</em>' : '';
			}
			
			
			if(!empty($search['floor']))
			{
				if($search['floor']=='4')
				{
					$search['floor']=='13层以上';
					$str .= $search['floor'] != '' ? '13层以上'.'<em class="t-line">|</em>' : '';
				}
				else
				{
					$str .= $house['floortypesell'][$search['floor']] != '' ? $house['floortypesell'][$search['floor']].'<em class="t-line">|</em>' : '';
				}
			}
			elseif(!empty($search['floor']))
			{
				$str .= $house['floortyperent'][$search['floor']] != '' ? $house['floortyperent'][$search['floor']].'<em class="t-line">|</em>' : '';
			}

			$roomstr = $search['room'] > 0 ? $search['room'].'室' : '';
			$roomstr .= $search['hall'] > 0 ? $search['hall'].'厅' : '';
			$roomstr .= $search['toilet'] > 0 ? $search['toilet'].'卫' : '';
			$roomstr .= $search['kitchen'] > 0 ? $search['kitchen'].'厨' : '';
			$str .= $roomstr != '' ? $roomstr.'<em class="t-line">|</em>' : '';

			if($search['district'])
			{
				$str .= $house['district'][$search['district']] != '' ? $house['district'][$search['district']].'<em class="t-line">|</em>' : '';
			}

			$streetstr = $search['streetid'] != '' ? $this->block_model->getstreetname($search['streetid']) : '';

			if(!empty($streetstr['streetname']))
			{
				$str .= $streetstr['streetname'] != '' ? $streetstr['streetname'].'<em class="t-line">|</em>' : '';
			}

			if(!empty($search['line']))
			{
				$str .= $house['railway'][$search['line']] != '' ? $house['railway'][$search['line']].'<em class="t-line">|</em>' : '';
			}
			
			if(!empty($search['buildyear']))
			{	
				$house['buildyear'] = array ('1'=>'0-2年','2'=>'3-5年','3'=>'6-10年','4'=>'10年以上','5'=>'2年以内', '6'=>'5年以内', '7'=>'10年以内');
				$str .= $house['buildyear'][$search['buildyear']] != '' ? $house['buildyear'][$search['buildyear']].'<em class="t-line">|</em>' : '';
			}

			$ststr = $search['st'] != '' ? $this->block_model->getstname($search['st']) : '';
			$str .= $ststr != '' ? $ststr.'<em class="t-line">|</em>' : '';

            $arr_line=$this->block_model->getline();
			$ststr = $search['bl'] != '' ? $arr_line[$search['bl']] : '';
			$str .= $ststr != '' ? $ststr.'<em class="t-line">|</em>' : '';

			$return[$key]['searchcond'] = $str;
			$return[$key]['id'] = $value['id'];
		}
		$this->memcached_class->add($key, $return,1800);
	}

		return $return;
	}


	/**
	* getSeachcondUrl 转化找房条件
	*
	* @param array $sarr
	* @param str $tbl
	* @return str
	*/
	function getSeachcondUrl( $sarr , $kind)
	{
		GLOBAL $house;

		$return = array();

		if(empty($sarr))
		{
			return '';
		}
		
		foreach( $sarr as $key => $value )
		{	
			$search = unserialize($value['collect_value']);

			$type = $search['type'];

			if( GB_CITY == 'nj' )
			{
				$url = $kind == 1 ? GB_SELL_URL."/selllist_sell.php" : GB_RENT_URL."/rentlist_rent.php";

				if($type == 4)
				{
					$url = GB_SELL_URL."/school_houselist.php";
				}
			}
			else
			{
				$url = $kind == 1 ? GB_SELL_URL."/selllist_rewrite.php" : GB_RENT_URL."/rentlist_rent.php";
			}

			$search['fitment'] = $search['fitment'] == '任意' ? '' : $search['fitment'];

			if( isset($search['renttype']) )
			{
				$search['renttype'] = $search['renttype'] == '任意' ? '' : $search['renttype'];
			}
			else
			{
				$search['renttype'] = '';
			}


			$param = '?';
			if( is_array($search) && !empty($search) )
			{
				foreach ($search  as $key_cond =>$value_cond)
				{
					if( $key_cond !='' && !empty($value_cond))
					{
						if($key_cond=='st')
						{
							$key_cond='sn';
						}
						else if($kind!=1 && ($key_cond=='price_stores' || $key_cond=='price_office'))
						{
							$key_cond='price';
						}
						else if($kind!=1 && ($key_cond=='officetype' || $key_cond=='shoptype'))
						{
							$key_cond='ostype';
						}
						else if($kind != 1 && ($key_cond == 'buildarea_stores' || $key_cond =='buildarea_office'))
						{
							$key_cond='osbuildarea';
						}
						elseif($kind!=1 && $key_cond=='price_rent')
						{
							$key_cond = 'price';
						}

						if($key_cond == 'forward')
						{
							$value_cond = $house['uri_forward'][$value_cond];
						}

						if($key_cond == 'bl' && !empty($value_cond))
						{
							$st_change = 1;
						}
						if($key_cond == 'district' && !empty($value_cond))
						{
							$uri_array = array("");
		                    $uri_district  = array_merge($uri_array,array_keys($house['district']));
		                    $uri_district_asc = array_flip($uri_district);
							$value_cond = $uri_district_asc[$value_cond];
						}

						$param .= $param != '?' ? '&'.$key_cond.'='.$value_cond : $key_cond.'='.$value_cond;
					}
				}
				if($st_change==1){
				$param = str_replace("&sn", "&st", $param);
				}
			}
			$return[$key] = $url.$param;
		}

		return $return;
	}


	 /**
	 * 根据小区id获取房源信息
	 * @param type $blockid
	 * @return array
	 */
	function getblockhouse($blockid)
	{
		$key = $this->memkey.'gethouse'.$blockid;
		if($this->input->get('update') == 1)
		 {
		  $this->memcached_class->delete($key);
		 }
		 $housedetail = $this->memcached_class->get($key);
		if(empty($housedetail) )
		{
		  $sql = "select id,district,streetid,blockshowname,room,hall,buildarea,updatetime,forward,fitment,subfloor,floor,totalfloor,buildyear from sell where blockid=".$blockid." order by updatetime desc  limit 0,1 luc";

		  $selldetail = $this->server_sell->query($sql);
          if(!empty($selldetail))
            {
		  $housedetail = $selldetail['Record'][0];
		  $housedetail['time'] = time_tran($housedetail['UPDATETIME']);
		  $streetname = $this->block_model->getstreetname($housedetail['STREETID']);
		  $housedetail['streetname'] = $streetname['streetname'];
            }else{
            $housedetail=array();
            }
		  $this->memcached_class->add($key, $housedetail,10);
		}

		return $housedetail;
	}


	 /**
	 * 根据搜索条件获得最新房源
	 * @param type $blockid
	 * @return array
	 */
	function getconditionhouse($searcharr , $tbl = 'sell' , $datatime = '')
	{
		$key = $this->memkey.'_gethouse_guanzhu_'.serialize($searcharr);
	
		if($this->input->get('update') == 1)
		{
			$this->memcached_class->delete($key);
		}

		$housedetail = $this->memcached_class->get($key);

		if( empty($housedetail) )
		{
			$wherecond = $this->getsearchcond($searcharr);

			if(!empty($datatime))
			{
				$wherecond .= " AND creattime > $datatime ";
			}

			if($tbl=='sell')
			{
				$sql = "SELECT id,district,streetid,blockshowname,room,hall,buildarea,updatetime,floor,subfloor,totalfloor,fitment,forward,buildyear FROM sell WHERE $wherecond ORDER BY updatetime DESC LIMIT 0,1 luc";if($_GET['test']==1){return $sql;}
				$selldetail = $this->server_sell->query($sql);
			}
			else
			{
				$sql = "SELECT id,district,streetid,blockshowname,room,hall,buildarea,updatetime,floor,subfloor,totalfloor,fitment,forward,buildyear FROM rent WHERE $wherecond ORDER BY updatetime DESC LIMIT 0,1 luc";
				$selldetail = $this->server_rent->query($sql);
			}

			if(isset($selldetail['Record'][0]))
			{
				$housedetail = $selldetail['Record'][0];
				$housedetail['time'] = time_tran($housedetail['UPDATETIME']);
				$streetname = $this->block_model->getstreetname($housedetail['STREETID']);
				$housedetail['streetname'] = $streetname['streetname'];
			}

			$this->memcached_class->add($key, $housedetail,120);
		}

		return $housedetail;
	}


	/**
	* 根据搜索条件获得sql
	* @param type $searcharr
	* @return string
	*/
	function getsearchcond($searcharr)
	{
		GLOBAL $house;

        $housedetail = array();

		//SQL搜索条件
		$wherecond	= " esta = 1 ";

		//关键字条件
		if( $searcharr['keyword'] != '' && $searcharr['keyword'] != '请输入关键字..' )
		{
			//如果搜索关键字是1开头，11位数字默认按照手机号搜索；
			//如果5-10位的数字，则按照房源编号查询
			if(preg_match('/^1[0-9]{10}$/', $searcharr['keyword']))
			{
				$wherecond	.= " AND telno = '".$searcharr['keyword']."' ";
			}
			else if(preg_match('/^\d{5,10}$/', $searcharr['keyword']) )
			{
				$wherecond  .= " AND id = '".intval($searcharr['keyword'])."' ";
			}
			else
			{
				$wherecond	.= " AND (address LIKE '%".$searcharr['keyword']."%' OR blockshowname LIKE '%".$searcharr['keyword']."%')";
			}
		}

		//区属条件
		if( $searcharr['district'] != '' && $searcharr['district'] != '0')
		{
			$wherecond	.= " AND district = '".$searcharr['district']."' ";
		}

		//板块条件
		if( $searcharr['streetid'] != '' && $searcharr['streetid'] != '0')
		{
			$wherecond .= " AND streetid = '".$searcharr['streetid']."' ";
		}

		//房源类型条件
		$searcharr['infotype'] = intval($searcharr['infotype']);

		switch ($searcharr['infotype']) {
			case 1:
				$wherecond	.= " AND infotype IN ('1','2') ";
				break;
			case 2:
				$wherecond	.= " AND infotype = '2' ";
				break;
			case 3:
				$wherecond	.= " AND infotype = '3' ";
				break;
			case 4:
				$wherecond	.= " AND infotype = '4' ";
				break;
			case 5:
				$wherecond	.= " AND infotype = '5' ";
				break;
			case 6:
				$wherecond	.= " AND infotype = '6' ";
				break;
		}

		//写字楼分类搜索条件
		if( $searcharr['infotype'] == 3 && $searcharr['officetype'] != '')
		{
			$wherecond .= " AND tradeclass = '".$house['officetype'][$searcharr['officetype']]."' ";
		}

		//商铺分类搜索条件
		if( $searcharr['infotype'] == 4 && $searcharr['shoptype'] != '')
		{
			$wherecond	.= " AND housekind = '".$house['shoptype'][$searcharr['shoptype']]."' ";
		}

		//价格搜素条件
		$searcharr['price_sell'] = empty($searcharr['price_sell']) ? '' : $searcharr['price_sell'];
		if( $searcharr['price_sell'] == 99 && isset($searcharr['price_price1']) && isset($searcharr['price_price2']) && ($searcharr['price_price1'] != "" || $searcharr['price_price2'] != ""))
		{
			$price_price1 = $searcharr['price_price1'];
			$price_price2 = $searcharr['price_price2'];
			//自定义价格条件
			if($price_price1 > 0 && $price_price2 == '')
			{
				$wherecond	.= " AND price >= '".$price_price1."'";
			}
			else if($price_price2 > 0 && $price_price1 == '')
			{
				$wherecond	.= " AND price >= '0' AND price <= '".$price_price2."'";
			}
			else if($price_price1 > 0 && $price_price2 >0 && $price_price1 == $price_price2)
			{
				$wherecond	.= " AND price >= '".$price_price1."' AND price <= '".$price_price1."'";
			}
			else if($price_price1 > 0 && $price_price2 >0 && $price_price1 < $price_price2)
			{
				$wherecond	.= " AND price >= '".$price_price1."' AND price <= '".$price_price2."'";
			}
			else if($price_price1 > 0 && $price_price2 >0 && $price_price1 > $price_price2)
			{
				$wherecond	.= " AND price >= '".$price_price2."' AND price <= '".$price_price1."'";
			}
		}
		else if($searcharr['price_sell'] > 0 )
		{
			//搜索面板条件
			$where_price_sell = empty($house['price_sell'][$searcharr['price_sell']])? '' : $house['price_sell'][$searcharr['price_sell']];

			if(!empty($where_price_sell))
			{
				$where_price_sell = preg_replace("#[^0-9-]#",'',$where_price_sell);

				$where_price_sell = explode('-',$where_price_sell);
				if(count($where_price_sell) == 2)
				{
					$wherecond	.= " AND price >= '".$where_price_sell[0]."' AND price <= '".$where_price_sell[1]."'";
				}
				else
				{
					if( isset($_REQUEST['price_sell']) && $_REQUEST['price_sell'] == 1)
					{
						$wherecond	.= " AND price < '".$where_price_sell[0]."' ";
					}
					else
					{
						$wherecond	.= " AND price > '".$where_price_sell[0]."' ";
					}
				}
				unset($where_price_sell);
			}
		}
        	if(!empty($searcharr['price1']) or !empty($searcharr['price2'])){

			if($searcharr['price1']>0&&$searcharr['price2']=='')	 $wherecond.=" and price>='".$searcharr['price1']."'";
			if($searcharr['price2']>0&&$searcharr['price1']=='')	 $wherecond.=" and price>='0' and price<='".$searcharr['price2']."'";
			if($searcharr['price1']>0&&$searcharr['price2']>0&&$searcharr['price1']==$searcharr['price2'])	 $wherecond.=" and price>='".$searcharr['price1']."' and price<='".$searcharr['price1']."'";
			if($searcharr['price1']>0&&$searcharr['price2']>0&&$searcharr['price1']<$searcharr['price2'])	 $wherecond.=" and price>='".$searcharr['price1']."' and price<='".$searcharr['price2']."'";
			if($searcharr['price1']>0&&$searcharr['price2']>0&&$searcharr['price1']>$searcharr['price2'])	 $wherecond.=" and price>='".$searcharr['price2']."' and price<='".$searcharr['price1']."'";
            }

		//面积搜索条件
		$searcharr['buildarea'] = empty($searcharr['buildarea'])? '' : $searcharr['buildarea'];

		if( $searcharr['buildarea'] == 99 && ($searcharr['buildarea1'] != "" ||  $searcharr['buildarea2'] != "") )
		{
			$buildarea1 = $searcharr['buildarea1'];
			$buildarea2 = $searcharr['buildarea2'];
			//自定义建筑面积条件
			if( $buildarea1 > 0 && $buildarea2 == '')
			{
				$wherecond	.= " AND buildarea >= '".$buildarea1."'";
			}
			else if( $buildarea2 > 0 && $buildarea1 == '')
			{
				$wherecond	.= " AND buildarea >= '0' AND buildarea <= '".$buildarea2."' ";
			}
			else if($buildarea1 > 0 && $buildarea2 > 0 && $buildarea1 == $buildarea2)
			{
				$wherecond	.= " AND buildarea >= '".$buildarea1."' AND buildarea <= '".$buildarea1."' ";
			}
			else if($buildarea1 > 0 && $buildarea2 > 0 && $buildarea2 > $buildarea1)
			{
				$wherecond	.= " AND buildarea >= '".$buildarea1."' AND buildarea <= '".$buildarea2."' ";
			}
			else if($buildarea1 > 0 && $buildarea2 > 0 && $buildarea2 < $buildarea1)
			{
				$wherecond	.= " AND buildarea >= '".$buildarea2."' AND buildarea <= '".$buildarea1."' ";
			}
		}
		else if( $searcharr['buildarea'] > 0)
		{
			//搜索面板条件
			$where_buildarea_sell = empty($house['buildarea'][$searcharr['buildarea']])? '' : $house['buildarea'][$searcharr['buildarea']];

			if(!empty($where_buildarea_sell))
			{
				$where_buildarea_sell = preg_replace("#[^0-9-]#",'',$where_buildarea_sell);
				$where_buildarea_sell = explode('-',$where_buildarea_sell);

				if(count($where_buildarea_sell) == 2)
				{
					$wherecond	.= " AND buildarea >= '".$where_buildarea_sell[0]."' AND buildarea <= '".$where_buildarea_sell[1]."'";
				}
				else
				{
					if($_REQUEST['buildarea'] == 1){
						$wherecond	.= " AND buildarea < '".$where_buildarea_sell[0]."' ";
					}
					else
					{
						$wherecond	.= " AND buildarea > '".$where_buildarea_sell[0]."' ";
					}
				}

				unset($where_buildarea_sell);
			}
		}

		//房型搜索条件
		switch ($searcharr['roomtype']) {
			case 1:
				$wherecond	.= " AND room = '1'";
				break;
			case 2:
				$wherecond	.= " AND room = '2'";
				break;
			case 3:
				$wherecond	.= " AND room = '3'";
				break;
			case 4:
				$wherecond	.= " AND room = '4'";
				break;
			case 5:
				$wherecond	.= " AND room = '5'";
				break;
		}
        //处理南京和分站不一致的情况
        if(GB_CITY=='nj'){
          $house['renttype_num']=array(1=>'整租',2=>'合租',3=>'日租');
        }

		if(isset($searcharr['renttype']) && $searcharr['renttype'])
		{
			$wherecond	.= " AND renttype = '".$house['renttype_num'][$searcharr['renttype']]."'";
		}

		//自定义房型条件
		if($searcharr['room'] != "" ||  $searcharr['hall'] != "" || $searcharr['toilet'] != "" || $searcharr['kitchen'] != "")
		{
			if( $searcharr['room'] >= 0 )
			{
				$wherecond	.= " AND room = '".$searcharr['room']."'";//房间个数
			}

			if($searcharr['hall'] >= 0 )
			{
				$wherecond	.= " AND hall = '".$searcharr['hall']."'";//客厅个数
			}

			if($searcharr['toilet'] >=0 )
			{
				$wherecond	.= " AND toilet = '".$searcharr['toilet']."'";//卫生间个数
			}

			if($searcharr['kitchen'] >=0 )
			{
				$wherecond	.= " AND kitchen = '".$searcharr['kitchen']."'";//厨房个数
			}
		}

		if( $searcharr['floor'] > 0)
		{
			//楼层条件
			switch ($searcharr['floor']) {
				case 1:
					$wherecond	.= " AND floor = '1'";
					break;
				case 2:
					$wherecond	.= " AND floor >= '2' AND floor <= '5'";
					break;
				case 3:
					$wherecond	.= " AND floor >= '6' AND floor <= '12'";
					break;
				case 4:
					$wherecond	.= " AND floor >= '13'";
					break;
			}
		}
		else if( $searcharr['floor1'] > 0 || $searcharr['floor2'] > 0 )
		{
			$floor1 = $searcharr['floor1'];
			$floor2 = $searcharr['floor2'];
			//自定义楼层条件
			if($floor1 > 0 && $floor2 == '')
			{
				$wherecond	.= " AND floor >= '".$floor1."' ";
			}
			elseif($floor2 > 0 && $floor1 == '')
			{
				$wherecond	.= " AND floor >= '0' AND floor <= '".$floor2."' ";
			}
			elseif($floor1 > 0 && $floor2 > 0 && $floor1 == $floor2)
			{
				$wherecond	.= " AND floor >= '".$floor1."' AND floor <= '".$floor1."' ";
			}
			elseif( $floor1 > 0 && $floor2 > 0 &&  $floor2 > $floor1 )
			{
				$wherecond	.= " AND floor >= '".$floor1."' AND floor <= '".$floor2."' ";
			}
			elseif( $floor1 > 0 && $floor2 > 0 &&  $floor2 < $floor1 )
			{
				$wherecond	.= " AND floor >= '".$floor2."' AND floor <= '".$floor1."' ";
			}
		}

		//价格条件
		if( isset( $searcharr['priceterm'] ) && $searcharr['priceterm'] == 1)
		{
			$wherecond	.= " AND priceterm = '".$searcharr['priceterm']."'" ;
		}

		//营业税条件
		if( isset($searcharr['pricetype']) && $searcharr['pricetype'] == 1)
		{
			$wherecond .= " AND pricetype = '".$searcharr['pricetype']."'";
		}

		//装修条件
		if( $searcharr['fitment'] && $searcharr['fitment']!='任意' ) $wherecond	.= " AND fitment = '".$searcharr['fitment']."'";

		//房龄条件
		if( isset($searcharr['buildyear']) && $searcharr['buildyear'] )
		{
			$current_year = date('Y');

			switch($searcharr['buildyear']) {
				case 1:	//0-2年
					$year_start	= $current_year - 2;
					$year_end	= $current_year;
					$wherecond	.= " AND buildyear BETWEEN '".$year_start."' AND  '".$year_end."' ";
					break;
				case 2://3-5年
					$year_start	= $current_year - 5;
					$year_end	= $current_year - 3;
					$wherecond	.= " AND buildyear BETWEEN '".$year_start."' AND  '".$year_end."' ";
					break;
				case 3://6-10年
					$year_start	= $current_year - 10;
					$year_end	= $current_year - 6;
					$wherecond	.= " AND buildyear BETWEEN '".$year_start."' AND  '".$year_end."' ";
					break;
				case 4://10年以上
					$year_start	= 1970;
					$year_end	= $current_year - 11;
					$wherecond	.= " AND buildyear BETWEEN '".$year_start."' AND  '".$year_end."' ";
					break;
			}
		}

		//来源条件
		$searcharr['infofrom'] = intval($searcharr['infofrom']);
		switch ($searcharr['infofrom']) {
			case 1://个人房源
				$wherecond	.= ' AND infofrom = 1 ';
				break;
			case 6://中介房源
				$wherecond	.= ' AND infofrom = 5 ';
				break;
            case 5://中介房源
				$wherecond	.= ' AND infofrom = 5 ';
				break;
		}

		//个人付费房源
		if( isset($searcharr['kind']) && $searcharr['kind'] == 6 && $searcharr['infofrom'] != 6)
		{
			$wherecond	.= ' AND ischarge = 1 ';
		}

		//多图条件(中介房源才有多图条件)
		if(isset($searcharr['star']) && $searcharr['star'] == 1 && $searcharr['infofrom'] != 1)
		{
			$wherecond	.= ' AND isstar = 3 ';
		}

		//REAL房源条件
		if( isset($searcharr['isreal']) && $searcharr['isreal'] == 1 && $searcharr['infofrom'] != 1)
		{
			$wherecond	.= ' AND isreal = 1 ';
		}

		//新房免佣
		if( isset($searcharr['config_nofee']) && $searcharr['config_nofee'] == 1 && $searcharr['nofee'] == 1 && $searcharr['infofrom'] != 1)
		{
			$wherecond	.= ' AND nofee = 1 ';
		}

		//急推房源条件
		if( isset($searcharr['istag']) && $searcharr['istag'] == 1 && $searcharr['infofrom'] != 1)
		{
			$wherecond	.= ' AND istag = 1 ';
		}

		if( isset($searcharr['buildyear'])){
		//房源朝向条件
		switch ($searcharr['forward']) {
			case '南':
				$wherecond	.= " AND forward ='南'";
				break;
			case '西南':
				$wherecond	.= " AND forward='西南'";
				break;
			case '东':
				$wherecond	.= " AND forward='东'";
				break;
			case '东南':
				$wherecond	.= " AND forward='东南'";
				break;
			case  '北':
				$wherecond	.= " AND forward ='北' ";
				break;
			case  '东北':
				$wherecond	.= " AND forward ='东北' ";
				break;
			case  '西':
				$wherecond	.= " AND forward ='西' ";
				break;
			case  '西北':
				$wherecond	.= " AND forward ='西北' ";
				break;
			case  '南北':
				$wherecond	.= " AND forward ='南北' ";
				break;
		}
        }


		//2015-4-30 zhou 添加产权过滤查询条件
		if( isset($searcharr['mright'])){
		//房源朝向条件
		switch ($searcharr['mright']) {
			case '1':
				$wherecond	.= " AND mright ='产权房'";
				break;
			case '2':
				$wherecond	.= " AND mright='使用权房'";
				break;
			
		}
     }

    if( isset($searcharr['price_rent']) && empty($searcharr['price_office']) && empty($searcharr['price_stores'])){
        	switch ($searcharr['price_rent']) {
		case 1:
			$wherecond.=" and price<='800'";
			break;
		case 2:
			$wherecond.=" and price>='800' and price<='1200'";
			break;
		case 3:
			$wherecond.=" and price>='1200' and price<='2000'";
			break;
		case 4:
			$wherecond.=" and price>='2000' and  price<='3000'";
			break;
		case 5:
			$wherecond.=" and price>='3000' and  price<='5000'";
			break;
		case 6:
			$wherecond.=" and price>='5000' and price<='10000'";
			break;
		case 7:
			$wherecond.=" and price>='10000' ";
			break;
	}
    }

    if($searcharr['infotype']==3){
    switch ($searcharr['price_office']) {
		case 1:
			$wherecond.=" and price<='5000'";
			break;
		case 2:
			$wherecond.=" and price>='5000' and price<='8000'";
			break;
		case 3:
			$wherecond.=" and price>='8000' and price<='10000'";
			break;
		case 4:
			$wherecond.=" and price>='10000' and  price<='15000'";
			break;
		case 5:
			$wherecond.=" and price>='15000' and  price<='20000'";
			break;
		case 6:
			$wherecond.=" and price>='20000' and price<='50000'";
			break;
		case 7:
			$wherecond.=" and price>='50000' ";
			break;
	}

	switch ($searcharr['buildarea_office']) {
		case 1:
			$wherecond.=" and buildarea <='100'";
			break;
		case 2:
			$wherecond.=" and buildarea >='100' and buildarea <='150' ";
			break;
		case 3:
			$wherecond.=" and buildarea >='150' and buildarea <='200' ";
			break;
		case 4:
			$wherecond.=" and buildarea >='200' and buildarea <='300'";
			break;
		case 5:
			$wherecond.=" and buildarea >='300' and buildarea <='500' ";
			break;
		case 6:
			$wherecond.=" and buildarea >='500' and buildarea <='1000' ";
			break;
		case 7:
			$wherecond.=" and buildarea >='1000'  ";
			break;
	}
}

if($searcharr['infotype']==4){
    switch ($searcharr['price_stores']) {
		case 1:
			$wherecond.=" and price<='5000'";
			break;
		case 2:
			$wherecond.=" and price>='5000' and price<='10000'";
			break;
		case 3:
			$wherecond.=" and price>='10000' and price<='20000'";
			break;
		case 4:
			$wherecond.=" and price>='20000' and price<='30000'";
			break;
		case 5:
			$wherecond.=" and price>='30000' and  price<='50000'";
			break;
		case 6:
			$wherecond.=" and price>='50000' and  price<='70000'";
			break;
		case 7:
			$wherecond.=" and price>='70000' and price<='100000'";
			break;
		case 8:
			$wherecond.=" and price>='100000' and price<='200000'";
			break;
		case 9:
			$wherecond.=" and price>='200000' ";
			break;
	}
	switch ($searcharr['buildarea_stores']) {
		case 1:
			$wherecond.=" and buildarea <='20'";
			break;
		case 2:
			$wherecond.=" and buildarea >='20' and buildarea <='50' ";
			break;
		case 3:
			$wherecond.=" and buildarea >='50' and buildarea <='100' ";
			break;
		case 4:
			$wherecond.=" and buildarea >='100' and buildarea <='150'";
			break;
		case 5:
			$wherecond.=" and buildarea >='150' and buildarea <='200' ";
			break;
		case 6:
			$wherecond.=" and buildarea >='200' and buildarea <='500' ";
			break;
		case 7:
			$wherecond.=" and buildarea >='500' and buildarea <='1000' ";
			break;
		case 8:
			$wherecond.=" and buildarea >='1000'  ";
			break;
	}
}



        if(!empty($searcharr['st'])){
         $sql = "SELECT blockid FROM `traffic_block` WHERE `bid` = '".$searcharr['st']."'";

		 $query = $this->db_esfhousebak->query($sql);
	     $result = $query->result_array();
       //  if($_COOKIE['esfusertelno']=='18652058969'){
       //  var_dump($sql);exit;
       //  }
		  $i = 0;
		  $blockid = '';
		  if(!empty($result)){
			  foreach($result as $key => $val)
			  {
				 $arr_bid[] = $val['blockid'];
			  }

			  $arr_bid =  array_unique($arr_bid);
			if(!empty($arr_bid))
			{
				foreach($arr_bid as $key => $val)
				{
					if( $i == 0)
					{
						$blockid .= "'".$val."'";
					}
					else
					{
						$blockid .= ",'".$val."'";
					}
					$i++;
				}
			}
         }}


       // var_dump($searcharr['railway_blockid']);
		//地铁TAB页搜素条件
		if( isset ($searcharr['type']) && ($searcharr['type'] == 1 || $searcharr['type'] == 2)  && isset($searcharr['st']) && !empty($blockid)){
		   $wherecond	.= " AND blockid IN (".$blockid.") ";
		}

		return $wherecond;
	}

	 /**
	 * 根据搜索条件获得用户详情
	 * @param type $searcharr
	 * @return string
	 */
	function get_userdetail($uid,$select)
	{
	   $key = $this->memkey.'getuserdetail'.$uid.$select;
	   if($this->input->get('update') == 1)
		 {
		  $this->memcached_class->delete($key);
		 }
		 $uid_array = $this->memcached_class->get($key);
		if(empty($collect_array) )
		{
		  $uid_array =  $this->get_data(array('form_name' => 'personal_user','where'=>array('id'=>$uid,'is_use'=>1),'select'=>$select),'esfbak');

		  $this->memcached_class->add($key, $uid_array,10);
		}

		return $uid_array;
	}

	 /**
	 * 根据用户id获得消息总数
	 * @param type $uid
	 * @return string
	 */
	function getmessagenumbyuid($uid,$city=GB_CITY)
	{
	 $query = $this->db_esfbak->query("select count(*) as count_num from personal_announceitem as a left join personal_announce as b on a.announceid=b.id where a.uid = ".$uid." and a.cityname='".$city."'");
	 $num = $query->result_array();
	 return $num[0]['count_num'];
	}

	 /**
	 * 根据用户id获得未点击的新消息总数(头部未被点击)
	 * @param type $uid
	 * @return string
	 */
	function getmessagenumbyuidwd($uid,$city=GB_CITY)
	{
	 $query = $this->db_esfbak->query("select count(*) as count_num from personal_announceitem as a left join personal_announce as b on a.announceid=b.id where a.uid = ".$uid." and a.cityname='".$city."' and a.is_click = 0");
	 $num = $query->result_array();
	 return $num[0]['count_num'];
	}

	 function getmessagebyuid($uid,$limit,$offset,$city=GB_CITY)
	{
	 $query = $this->db_esfbak->query("select a.announceid,a.id , b.subject, b.operator, a.new, b.creattime,b.message from personal_announceitem as a left join personal_announce as b on a.announceid  = b.id where a.uid = $uid and a.cityname='".$city."'  order by b.creattime desc limit ".$limit.",".$offset."");
	 //echo '<!--'."select a.announceid,a.id , b.subject, b.operator, a.new, b.creattime,b.message from personal_announceitem as a left join personal_announce as b on a.announceid  = b.id where a.uid = $uid and a.cityname='".$city."'  order by b.creattime desc limit ".$limit.",".$offset."".'-->';
	 $message = $query->result_array();
	 return $message;

	 }


	 /**
	 * 添加消息
	 * @param type $arr
	 * @return string
	 */
	function addannounce($arr)
	{
	  //设置表
	  $this->user_model->set_table('personal_announce');
	  //过滤表结构
	  $result = $this->user_model->add($arr,'esf');
	  return $result;
	}


	 /**
	 * 添加用户消息
	 * @param type $arr
	 * @return string
	 */
	function addannounceitem($arr)
	{
	  //设置表
	  $this->user_model->set_table('personal_announceitem');
	  //过滤表结构
	  $result = $this->user_model->add($arr,'esf');
	  return $result;
	}


	 /**
	 * 删除用户消息
	 * @param type $arr
	 * @return string
	 */
	function delmessage($arr)
	{
	  //设置表
	  $this->user_model->set_table('personal_announceitem');
	  //过滤表结构
	  $result = $this->user_model->del_mul($arr,'id','esf');
	  return $result;
	}

	 /**
	 * 删除关注
	 * @param type $arr
	 * @return string
	 */
	function delcollect($arr)
	{
	  //设置表
	  $this->user_model->set_table('personal_collection');
	  //过滤表结构
	  $result = $this->user_model->del_mul($arr,'id','esf');
	  return $result;
	}


	/**
	* 获取有更新的小区
	* @param type $arr
	* @return string
	*/
	function getnewblockhouse($uid,$lastlogintime)
	{
		$ids_array = $this->getcollect(2,$uid);

		foreach($ids_array as $key=>$value)
		{
			$sql = "select blockshowname from sell where blockid=".$value['collect_id']." and creattime > $lastlogintime order by updatetime desc  limit 0,1 luc";
			$selldetail = $this->server_sell->query($sql);

			$sql = "select blockshowname from rent where blockid=".$value['collect_id']." and creattime > $lastlogintime order by updatetime desc  limit 0,1 luc";
			$rentdetail = $this->server_rent->query($sql);

			if(isset($selldetail['Record']) or isset($rentdetail['Record']))
			{
				$array[]= $selldetail['Record'][0]['BLOCKSHOWNAME']?$selldetail['Record'][0]['BLOCKSHOWNAME']:$rentdetail['Record'][0]['BLOCKSHOWNAME'];
			}
		}

		return !empty($array)?$array:'';
	}

	/**
	* 获取有更新的找房条件
	* @param type $arr
	* @return string
	*/
	function getnewtjhouse($uid,$lastlogintime)
	{
		$array_sell = $this->getcollect(1,$uid);
		$array_rent = $this->getcollect(5,$uid);

		$mark = 0;
		foreach($array_sell as $key=>$value)
		{
			$house_detail = $this->getconditionhouse(unserialize($value['collect_value']),'sell',$lastlogintime);

			if(isset($house_detail) && !empty($house_detail))
			{
				$mark = 1;
				break;
			}
		}

		foreach($array_rent as $key=>$value)
		{
			$house_detail = $this->getconditionhouse(unserialize($value['collect_value']),'rent',$lastlogintime);
			if(isset($house_detail) && !empty($house_detail))
			{
				$mark = 1;
				break;
			}
		}

		return  $mark;
	}


	/**
	* 自动注册发送动态密码
	* @author cxf
	*/
	public function getyzmpassword($telno)
	{
		//验证是否已经通过验证
		$validcode = random(6);
		$tt = array();
		$tt['validcode'] = $validcode;
		$tt['telno'] = $telno;
		$tt['kind'] = 3;
		$tt['dateline'] = time();

		//设置表
		$this->user_model->set_table('personal_sms');
		//过滤表结构
		$tt = $this->user_model->create($tt,'db_esf');
		$result = $this->user_model->add($tt,'db_esf');

		if($result) {
			$msg   = '【365网】您在365网已经成功注册并认证该手机，可以使用该手机登录，登录密码为'.$validcode.'，为保证账户安全，登陆后尽快修改密码。';

			sendsms($tt['telno'],$msg,'sx');
			return $validcode;
		}
	}


	


	/**
	* 生成用户账号
	* @author cxf
	*/
	public function adduser($telno,$password,$city=GB_CITY,$infofrom=1,$passport_uid=0,$is_md5=0,$register_from=0)
	{
		
		if($is_md5==1){
		   $password = $password;
		}else{
		   $password = md5($password);
		}

		$user = array(
		'telno' => $telno,
		'password' => $password,
        'infofrom' => $infofrom,
		'registertime' => time(),
		'lastlogintime' => time(),
        'cityname' => $city,
		'passport_uid'=>$passport_uid,
	    'infofrom'=>$register_from
		);

		//设置表
		$this->user_model->set_table('personal_user');
		//过滤表结构
		$result = $this->user_model->add($user,'db_esf');

		return $result;
	}

    /**
    * 获取用户的求购数量
    * @author cxf
    */
    function get_buynum_by_uid($uid,$is_esta=0)
    {
        if($is_esta==0)
        {
        $uidnum =  $this->get_data(array('form_name' => 'buy','where'=>array('uid'=>$uid),'select'=>array('COUNT(*) AS num')),'esfhousebak');
        }else{
        $uidnum =  $this->get_data(array('form_name' => 'buy','where'=>array('uid'=>$uid,'esta'=>1),'select'=>array('COUNT(*) AS num')),'esfhousebak');     }
        if(!empty($uidnum[0]))
        {
            return $uidnum[0]['num'];
        }else{
            return 0;
        }
    }


   /**
    * 获取用户的求购
    * @author cxf
    */
    function get_buy_by_uid($uid,$offset=0,$pagesize=0,$sendtime='')
    {
	    if(!empty($sendtime)){
        $buy_array =  $this->get_data(array('form_name' => 'buy','where'=>array('uid'=>$uid,'esta'=>1,'creattime >'=>$sendtime),'limit'=>$offset,'offset'=>$pagesize,'order_by'=>'id DESC','select'=>array('matched_num','id','infotype','infofrom','address','buildyear','buildyear1','buildyear2','buildarea1','buildarea2','room','price1','price2','remark','allow_match','creattime','esta','telno')),'esfhousebak');
        }else{
        $buy_array =  $this->get_data(array('form_name' => 'buy','where'=>array('uid'=>$uid,'esta'=>1),'limit'=>$offset,'offset'=>$pagesize,'order_by'=>'id DESC','select'=>array('matched_num','id','infotype','infofrom','address','buildyear','buildyear1','buildyear2','buildarea1','buildarea2','room','price1','price2','remark','allow_match','creattime','esta','telno')),'esfhousebak');
        }
        if(is_array($buy_array) && !empty($buy_array) && GB_CITY=='nj')
        {
        foreach($buy_array as $key=>$value){
           $buy_array[$key]['new_matched_num'] = $this->get_match_num_by_uid('buy',$value['id']);
        }
        }

        return $buy_array;
    }


    /**
    * 获取用户的求购数量
    * @author cxf
    */
    function get_rent_wantnum_by_uid($uid,$is_esta=0)
    {
        if($is_esta==0)
        {
        $uidnum =  $this->get_data(array('form_name' => 'rentwanted','where'=>array('uid'=>$uid),'select'=>array('count(*) as num')),'esfhousebak');
        }else{
         $uidnum =  $this->get_data(array('form_name' => 'rentwanted','where'=>array('uid'=>$uid,'esta'=>1),'select'=>array('count(*) as num')),'esfhousebak');
        }
        if(!empty($uidnum[0]))
        {
            return $uidnum[0]['num'];
        }else{
            return 0;
        }
    }

    /**
    * 根据求租求购id获取用户的已匹配未查看的数量
    * @author cxf
    */
    function get_match_num_by_uid($want_type,$want_id)
    {
        $tbl=$want_type=='buy'?'buy_match':'rentwanted_match';

        $wantnum =  $this->get_data(array('form_name' => $tbl,'where'=>array('want_id'=>$want_id,'isread'=>0),'select'=>array('count(*) as num')),'esfhousebak');

        if(!empty($wantnum[0]))
        {
            return $wantnum[0]['num'];
        }else{
            return 0;
        }
    }

    /**
    * 根据用户获取所有已匹配未查看的数量
    * @author cxf
    */
    function get_match_num_byuid($uid)
    {
	    $all_num = 0;
        $buy_array = $this->get_buy_by_uid($uid);
        if(is_array($buy_array) && !empty($buy_array))
        {
          foreach($buy_array as $value)
           {
             $num = 0;
             if($value['esta']==1)
               {
                $num = $this->get_match_num_by_uid('buy',$value['id']);
               
               }
               $all_num += $num;
           }
        }
       
        $rentwant_array = $this->get_rentwant_by_uid($uid);
        if(is_array($rentwant_array) && !empty($rentwant_array))
        {
          foreach($rentwant_array as $value)
           {
             $num = 0;
             if($value['esta']==1)
               {
                $num = $this->get_match_num_by_uid('rentwanted',$value['id']);
               }
               $all_num += $num;
           }
        }
         return $all_num;
    }


    /**
    * 获取用户的求购
    * @author cxf
    */
    function get_rentwant_by_uid($uid,$offset=0,$pagesize=0,$sendtime='')
    {
        if(!empty($sendtime)){
        $rentwant_array =  $this->get_data(array('form_name' => 'rentwanted','where'=>array('uid'=>$uid,'esta'=>1,'creattime >'=>$sendtime),'limit'=>$offset,'offset'=>$pagesize,'order_by'=>'id DESC','select'=>array('id','infotype','infofrom','address','buildyear','room','price1','price2','remark','allow_match','creattime','buildarea1','buildarea2','matched_num','renttype','esta','telno')),'esfhousebak');        
        }else{
        $rentwant_array =  $this->get_data(array('form_name' => 'rentwanted','where'=>array('uid'=>$uid,'esta'=>1),'limit'=>$offset,'offset'=>$pagesize,'order_by'=>'id DESC','select'=>array('id','infotype','infofrom','address','buildyear','room','price1','price2','remark','allow_match','creattime','buildarea1','buildarea2','matched_num','renttype','esta','telno')),'esfhousebak');
        }
		
       if(is_array($rentwant_array) && !empty($rentwant_array) && GB_CITY=='nj')
        {
        foreach($rentwant_array as $key=>$value){
           $rentwant_array[$key]['new_matched_num'] = $this->get_match_num_by_uid('rentwant',$value['id']);
        }
        }
        return $rentwant_array;
    }


    /**
    * get_want_matched_by_id
    *
    * 获取求购、求租已匹配房源信息
    *
    * @access	public
    * @param	int     $want_id 求租、求购编号
    * @param	int     $offset 偏移数
    * @param	int     $pagesize 每次取的条数
    * @param	string  $order 升序降序
    * @return	array   求租求购记录
    */
    function get_want_matched_by_id ($want_id , $offset = 0 , $pagesize = 0 , $order = 'DESC')
    {
        $want_id = intval($want_id);

        $tbl_house = 'sell';
        $tbl_match = 'buy_match';

        //经纪人自己的房源数组
        $MatchedHouseInfo = array();

        //匹配该求租求购的所有房源编号
        $cond_where = " WHERE want_id = '".$want_id."' ";
        $cond_order = " ORDER BY creattime DESC ";
        $sql = " SELECT match_id FROM $tbl_match ";
        $sql_sub = $sql . $cond_where . $cond_order;

        $where_cond = ' id IN ('.$sql_sub.') ';
        $house_fields_arr = array('id','address','infotype','streetid','buildarea','price','buildyear');
        $this->db_esfhousebak->select( $house_fields_arr );
        $this->db_esfhousebak->where( $where_cond );
        $MatchedHouseInfo = $this->db_esfhousebak->get($tbl_house)->result_array();

        return $MatchedHouseInfo;
    }


    /**
    * get_want_matchednum_by_id
    *
    * 获取求购、求租已匹配房源个数
    *
    * @access	public
    * @param	int   $want_id 求租、求购编号
    * @return	int   求购求租匹配房源个数
    */
    function get_want_matchednum_by_id ($want_id)
    {
        $want_id = intval($want_id);

        $tbl_house = 'sell';
        $tbl_match = 'buy_match';

        //经纪人自己的房源数组
        $MatchedNum = 0;

        //匹配该求租求购的所有房源编号
        $cond_where = " WHERE want_id = '".$want_id."' ";
        $sql = " SELECT match_id FROM $tbl_match ";
        $sql_sub = $sql . $cond_where . $cond_order;

        $where_cond = ' id IN ('.$sql_sub.')';
        $this->db_esfhousebak->where( $where_cond );
        $MatchedHouseInfo = $this->db_esfhousebak->get( $tbl_house , $pagesize , $offset )->result_array();

        return $MatchedHouseInfo;
    }

	/**
	* 获取用户的最新发布的房源
	* @author cxf
	*/
	function newhouse($uid , $num)
	{
        $data['sell_array'] = $this->house_model->gethousebywhere(array('uid'=>$uid,'infofrom'=>'1','esta'=>1,'ischarge'=>'1'),'sell',array('id','address','buildarea','price','updatetime','creattime','esta','ischarge','pic1','district','streetid','blockshowname','infotype','room','hall','fitment','buildarea','ischarge','telno','floor ','subfloor','totalfloor'),0,$num);

        $data['rent_array'] = $this->house_model->gethousebywhere(array('uid'=>$uid,'infofrom'=>'1','esta'=>1,'ischarge'=>'1'),'rent',array('id','address','buildarea','price','updatetime','creattime','esta','ischarge','pic1','district','streetid','blockshowname','infotype','room','hall','fitment','buildarea','priceunit','ischarge','telno','floor ','subfloor','totalfloor'),0,$num);

        if(!empty($data['rent_array']) && !empty($data['sell_array']))
        {
            $house_array = array_merge($data['rent_array'],$data['sell_array']);
        }
        else if(!empty($data['rent_array']))
        {
            $house_array = $data['rent_array'];
        }
        else
        {
            $house_array = $data['sell_array'];
        }

		if(is_array($house_array) && !empty($house_array))
		{
			foreach($house_array as $key=>$value)
			{
				$return[$key] = $value;
				$return[$key]['tbl'] =! empty($value['priceunit']) ? 'rent' : 'sell' ;
				$updatetime[$key] = $value['updatetime'];
			}

			if(is_array($updatetime) && !empty($updatetime))
			{
				array_multisort( $updatetime,SORT_NUMERIC,SORT_DESC,$return);
			}
		}

        //如果付费房源不足2套，补齐非付费房源
        if(count($house_array)<2)
        {
        $data['sell_array1'] = $this->house_model->gethousebywhere(array('uid'=>$uid,'infofrom'=>'1','esta !='=>4,'ischarge !='=>'1'),'sell',array('id','address','buildarea','price','updatetime','creattime','esta','ischarge','pic1','district','streetid','blockshowname','infotype','room','hall','fitment','buildarea','ischarge','telno','floor ','subfloor','totalfloor'),0,$num-count($house_array));

        $data['rent_array1'] = $this->house_model->gethousebywhere(array('uid'=>$uid,'infofrom'=>'1','esta !='=>4,'ischarge !='=>'1'),'rent',array('id','address','buildarea','price','updatetime','creattime','esta','ischarge','pic1','district','streetid','blockshowname','infotype','room','hall','fitment','buildarea','priceunit','ischarge','telno','floor ','subfloor','totalfloor'),0,$num-count($house_array));

        if(!empty($data['rent_array1']) && !empty($data['sell_array1']))
        {
            $house_array1 = array_merge($data['rent_array1'],$data['sell_array1']);
        }
        elseif(!empty($data['rent_array1']))
        {
            $house_array1 = $data['rent_array1'];
        }
        else
        {
            $house_array1 = $data['sell_array1'];
        }
 

           if(is_array($house_array1) && !empty($house_array1))
            {
                foreach($house_array1 as $key=>$value)
                {
                    $return1[$key] = $value;
                    $return1[$key]['tbl'] =! empty($value['priceunit']) ? 'rent' : 'sell' ;
                    $updatetime1[$key] = $value['updatetime'];
                }

				if(is_array($updatetime1) && !empty($updatetime1))
				{
					array_multisort( $updatetime1,SORT_NUMERIC,SORT_DESC,$return1);
				}
            }
            
			if(!empty($return) &&  !empty($return1))
			{
				$return = array_merge($return,$return1);
			}
			elseif(empty($return) &&  !empty($return1))
			{
				$return = $return1;
			}
        }

        if( !empty($return) && is_array($return) )
        {
            $return = array_slice( $return, 0,2);
            return $return;
        }
    }
	



	/**
    * 根据规则获取用户的可能感兴趣的信息
	* 当$tbl变量为sell时，$sell_house_activate 为激活的二手房信息，$sell_collect_house 为网友关注的二手房信息
	* 当$tbl变量问rent时，$sell_house_activate 为激活的租房信息，  $sell_collect_house 为网友关注的租房信息
	* @since 2015-4-29
    * @author zhou
    */
	function getlikehousebyrule($active_num,$active_sell_num,$newhouse,$sell_collect_house,$sell_collect_block, $uid , $tbl , $num ,$city = GB_CITY)
	{
		$key = $this->memkey.'_getlikehousebyrule_'.$uid.'_'.$tbl.'_'.$num.'_'.$city;
		$this->memcached_class->delete($key);
		$housedetail = $this->memcached_class->get($key);
		
		//if(1) //测试数据，等开发完成后，删除
		if(empty($housedetail))
        {	
			
			if($active_num==0)
			{
				
				$housedetail =$this->getlikehousebynotactive($active_sell_num,$newhouse,$sell_collect_house,$sell_collect_block, $uid , $tbl , $num ,$city = GB_CITY);
			}
			else
			{
				//获取通过审核的数据
				
				$housedetail =$this->getlikehousebyactive($active_sell_num,$newhouse,$sell_collect_house,$sell_collect_block, $uid , $tbl , $num ,$city = GB_CITY);
			}

			$this->memcached_class->add($key, $housedetail,600);
			return $housedetail;	
			
		}
		else
		{
			
			return $housedetail ;
		}
	
	}

	
	 /**
	* 当用户审核未通过时, 获取用户的可能感兴趣的信息
	* 当$tbl变量为sell时，$sell_house_activate 为激活的二手房信息，$sell_collect_house 为网友关注的二手房信息
	* 当$tbl变量问rent时，$sell_house_activate 为激活的租房信息，  $sell_collect_house 为网友关注的租房信息
	* @since 2015-4-29
    * @author zhou
    */

	function getlikehousebynotactive($active_sell_num,$newhouse,$sell_collect_house,$sell_collect_block, $uid , $tbl , $num ,$city = GB_CITY)
	{
		//获取对应信息
		if($tbl == 'sell')
		{
			
			$housedetail =$this->getlikehousebynotactive_sell($active_sell_num,$newhouse,$sell_collect_house,$sell_collect_block, $uid , $tbl , $num ,$city = GB_CITY);
		}


		if($tbl == 'rent')
		{

			$housedetail =$this->getlikehousebynotactive_rent($active_sell_num,$newhouse,$sell_collect_house,$sell_collect_block, $uid , $tbl , $num ,$city = GB_CITY);
				 
		}


		if($tbl =='block')
		{
			
			$housedetail =$this->getlikehousebynotactive_block($active_sell_num,$newhouse,$sell_collect_house,$sell_collect_block, $uid , $tbl , $num ,$city = GB_CITY);
			
		}

		return $housedetail ;
	
	}

		/**
    *获取未激活的二手房数据
	*@author zhou
	*/
	function getlikehousebynotactive_sell($active_sell_num,$newhouse,$sell_collect_house,$sell_collect_block, $uid , $tbl , $num ,$city = GB_CITY)
	{
		
		//先推送网友关注的房源中对应的小区
		for($y =0;$y<count($sell_collect_house);$y++)
		{
			$collect_id[] =$sell_collect_house[$y]['collect_id'];
		}
		$collect_id = array_unique($collect_id);
			
		$block_id   =$this->getblockbysell($collect_id,'sell');
			
		$datainfo_2 =$this->getsellbyblock($block_id,4,$tbl);  //网友关注的房源中对应的小区的房源
	
		$housedetail1		=$datainfo_2['housedetail1'];

		$housedetail1num    =$datainfo_2['housedetail1num'];

		//$datainfo_2 =array(); //测试数据,等测试完成后删除
		if(is_array($datainfo_2['housedetail1']) && !empty($datainfo_2['housedetail1']))
		{
			
			$housedetail =$datainfo_2['housedetail1'];         //关注的房源中对应的小区的房源
		}
		else
		{	 
		
			//推送网友关注的小区的房源
			for($y =0;$y<count($sell_collect_block);$y++)
			{
				$collect_block_id[] =$sell_collect_block[$y]['collect_id'];
			}
			$collect_block_id = array_unique($collect_block_id);
			
			//$housedetail1num =3; //测试数据,等测试完成后删除

			$datainfo_3 =$this->getsellbyblock($collect_block_id,4-$housedetail1num,$tbl);  //网友关注的小区房源信息
			
			//$datainfo_3 =array(); //测试数据,等测试完成后删除
			
			if(is_array($datainfo_3['housedetail1']) && !empty($datainfo_3['housedetail1']))
			{

				if(!empty($housedetail1))
				{
					$housedetail =array_merge($housedetail1,$datainfo_3['housedetail1']); //关注房源的数据+关注的小区房源信息
				}
				else
				{
					$housedetail =$datainfo_3['housedetail1'];
				}
				
			}
			else
			{
				
				//获取网友游览过的数据
				$data_cookie =$this->getblockbycookie('sellinfo_blockid');
				
				$datainfo_4  =$this->getsellbyblock($data_cookie,4-$housedetail1num,$tbl);    //网友游览记录的房源信息
				//$datainfo_4 =array();
				//$datainfo_4 =array(); //测试数据,等测试完成后删除
				if(is_array($datainfo_4['housedetail1']) && !empty($datainfo_4['housedetail1']))
				{
					
					if(!empty($housedetail1))
					{
						$housedetail =array_merge($housedetail1,$datainfo_4['housedetail1']);	//关注房源的数据+网友游览记录的房源信息
					}
					else
					{
						$housedetail =$datainfo_4['housedetail1'];

						if(count($housedetail)<4)
						{
							$datainfo_5 =$this->getlateinfobysell(4-count($housedetail));
							$housedetail =array_merge($housedetail,$datainfo_5['housedetail1']);

						}
					}
					
				}
				else
				{
					
					//最新发布房源的数据
					$datainfo_5 =$this->getlateinfobysell(4-$housedetail1num);
					if(!empty($housedetail1))
					{
						$housedetail =array_merge($housedetail1,$datainfo_5['housedetail1']); //审核的数据+最新发布房源的数据
					}
					else
					{
						$housedetail =$datainfo_5['housedetail1'];
					}
					
				}
					
			}
				
		}
		
		return $housedetail ;
	
	}


	/**
    *获取激活的租房数据
	*@author zhou
	*/
	function getlikehousebynotactive_rent($active_sell_num,$newhouse,$sell_collect_house,$sell_collect_block, $uid , $tbl , $num ,$city = GB_CITY)
	{
		
		//先推送网友关注的房源中对应的小区
		for($y =0;$y<count($sell_collect_house);$y++)
		{
			$collect_id[] =$sell_collect_house[$y]['collect_id'];
		}
		
		$collect_id = array_unique($collect_id);
		
		$block_id   =$this->getblockbysell($collect_id,'rent');

		$datainfo_2 =$this->getrentbyblock($block_id,4,$tbl);  //网友关注的房源中对应的小区的房源
		
		$housedetail1		=$datainfo_2['housedetail1'];

		$housedetail1num    =$datainfo_2['housedetail1num'];

		//$datainfo_2 =array(); //测试数据,等测试完成后删除
		
		if(is_array($datainfo_2['housedetail1']) && !empty($datainfo_2['housedetail1']))
		{
			$housedetail =$housedetail1; //关注的房源中对应的小区的房源
		}
		else
		{
			//$housedetail1num =3;//测试数据,等测试完成后删除

			//推送网友关注的小区的房源
			for($y =0;$y<count($sell_collect_block);$y++)
			{
				$collect_block_id[] =$sell_collect_block[$y]['collect_id'];
			}

			$collect_block_id = array_unique($collect_block_id);

			$datainfo_3 =$this->getrentbyblock($collect_block_id,4-$housedetail1num,$tbl);		//网友关注的小区房源信息
			
			//$datainfo_3 =array(); //测试数据,等测试完成后删除
			
			if(is_array($datainfo_3['housedetail1']) && !empty($datainfo_3['housedetail1']))
			{
				if(!empty($housedetail1))
				{
					$housedetail =array_merge($housedetail1,$datainfo_3['housedetail1']); //审核的数据+关注的小区房源信息
				}
				else
				{
					$housedetail =$datainfo_3['housedetail1'];
				}
			}
			else
			{
				//获取网友游览过的数据
				$data_cookie =$this->getblockbycookie('rentinfo_blockid');
									
				$datainfo_4  =$this->getrentbyblock($data_cookie,4-$housedetail1num,$tbl);               //网友游览记录的房源信息
				
				//$datainfo_4 =array(); //测试数据,等测试完成后删除
	
				if(is_array($datainfo_4['housedetail1']) && !empty($datainfo_4['housedetail1']))
				{	
					if(!empty($housedetail1))
					{
						
						$housedetail =array_merge($housedetail1,$datainfo_4['housedetail1']); //数据+网友游览记录的房源信息
					}
					else
					{
						$housedetail =$datainfo_4['housedetail1'];
						if(count($housedetail)<4)
						{
							$datainfo_5 =$this->getlateinfobyrent(4-count($housedetail));
							$housedetail =array_merge($housedetail,$datainfo_5['housedetail1']);

						}
					}
					
				}
				else
				{
					//最新发布房源的数据
					$datainfo_5 =$this->getlateinfobyrent(4-$housedetail1num);
					if(!empty($housedetail1))
					{
						$housedetail =array_merge($housedetail1,$datainfo_5['housedetail1']); //审核的数据+最新发布房源的数据
					}
					else
					{
						$housedetail =$datainfo_5['housedetail1'];
					}
					
				}
						
			
			}
		}
			
		
		
		return $housedetail ;
	}



	/**
    *获取未激活的小区数据
	*@author zhou
	*/

	function getlikehousebynotactive_block($active_sell_num,$newhouse,$sell_collect_house,$sell_collect_block, $uid , $tbl , $num ,$city = GB_CITY)
	{

		//先推送网友关注的房源中对应的小区
		for($y =0;$y<count($sell_collect_house);$y++)
		{
			$collect_id[] =$sell_collect_house[$y]['collect_id'];
		}
		
		$collect_id = array_unique($collect_id);

		$block_id   =$this->getblockbysell($collect_id,'sell');
		
	
		$datainfo_2 =$this->getblockbyblock($block_id,4,$tbl);      //网友关注的房源中对应的小区数据
		
		$housedetail1		=$datainfo_2['housedetail1'];
		$housedetail1num    =$datainfo_2['housedetail1num'];

		//$datainfo_2 =array(); //测试数据,等测试完成后删除
		if(is_array($datainfo_2['housedetail1']) && !empty($datainfo_2['housedetail1']))
		{
			$housedetail =$housedetail1;  //关注的房源中对应的小区的房源
		}
		else
		{
			
			//$housedetail1num =3;     //测试数据,等测试完成后删除
			//推送网友关注的小区房源
			for($y =0;$y<count($sell_collect_block);$y++)
			{
				$collect_block_id[] =$sell_collect_block[$y]['collect_id'];
			}
			$collect_block_id = array_unique($collect_block_id);
			
			$datainfo_3 =$this->getblockbyblock($collect_block_id,4-$housedetail1num,$tbl);  //网友关注的小区房源信息
			
			//$datainfo_3 =array(); //测试数据,等测试完成后删除

			if(is_array($datainfo_3['housedetail1']) && !empty($datainfo_3['housedetail1']))
			{
				if(!empty($housedetail1))
				{
					$housedetail =array_merge($housedetail1,$datainfo_3['housedetail1']); //审核的数据+关注的小区房源信息
				}
				else
				{
					$housedetail =$datainfo_3['housedetail1'];
				}
			}
			else
			{
				
				//获取网友游览过的数据
				$data_cookie =$this->getblockbycookie('blockinfo_blockid');
				
				$datainfo_4  =$this->getblockbyblock($data_cookie,4-$housedetail1num,$tbl);           //网友游览记录的房源信息
				
				//$datainfo_4 =array();//测试数据,等测试完成后删除
				if(is_array($datainfo_4['housedetail1']) && !empty($datainfo_4['housedetail1']))
				{
					if(!empty($housedetail1))
					{
						$housedetail =array_merge($housedetail1,$datainfo_4['housedetail1']); //审核的数据+网友游览记录的房源信息
					}
					else
					{
						$housedetail =$datainfo_4['housedetail1'];

						if(count($housedetail)<4)
						{
							$datainfo_5 =$this->getlateinfobysell(4-count($housedetail));
							$housedetail =array_merge($housedetail,$datainfo_5['housedetail1']);

						}
					}
				}
				else
				{
					
					//最新发布房源的数据
					$datainfo_5 =$this->getlateinfobysell(4-$housedetail1num);
				
					foreach($datainfo_5['housedetail1'] as $value_5)
					{
						$sell_5[] = $value_5['ID'];
					}
					
					$block_5   =$this->getblockbysell($sell_5,'sell');

					$block_id_5 = array_unique($block_5);

					$datainfo_5 =$this->getblockbyblock($block_id_5,4-$housedetail1num,$tbl);  //网友关注的小区房源信息
							
					if(!empty($housedetail1))
					{
						$housedetail =array_merge($housedetail1,$datainfo_5['housedetail1']); //审核的数据+最新发布房源的数据
					}
					else
					{
						$housedetail =$datainfo_5['housedetail1'];
					}

		
					
				}
			}

				
		}
			
		return $housedetail ;
		
	}









    /**
	* 当用户审核通过时,获取用户的可能感兴趣的信息
	* 当$tbl变量为sell时，$sell_house_activate 为激活的二手房信息，$sell_collect_house 为网友关注的二手房信息
	* 当$tbl变量问rent时，$sell_house_activate 为激活的租房信息，  $sell_collect_house 为网友关注的租房信息
	* @since 2015-4-29
    * @author zhou
    */
	function getlikehousebyactive($active_sell_num,$newhouse,$sell_collect_house,$sell_collect_block, $uid ,$tbl , $num ,$city = GB_CITY)
	{
		
		if($tbl == 'sell')
		{
			$housedetail =$this->getlikehousebyactive_sell($active_sell_num,$newhouse,$sell_collect_house,$sell_collect_block, $uid , $tbl , $num ,$city = GB_CITY);
		}
		if($tbl == 'rent')
		{
			
			$housedetail =$this->getlikehousebyactive_rent($active_sell_num,$newhouse,$sell_collect_house,$sell_collect_block, $uid , $tbl , $num ,$city = GB_CITY);
			
		}

		if($tbl =='block')
		{
		
			$housedetail =$this->getlikehousebyactive_block($active_sell_num,$newhouse,$sell_collect_house,$sell_collect_block, $uid , $tbl , $num ,$city = GB_CITY);
		}
		

		return $housedetail ;
	
	}


	/**
    *获取激活的二手房数据
	*@author zhou
	*/
	function getlikehousebyactive_sell($active_sell_num,$newhouse,$sell_collect_house,$sell_collect_block, $uid , $tbl , $num ,$city = GB_CITY)
	{
		
		//获取对应两条激活房源对应的小区的二手房
		if(is_array($newhouse) && !empty($newhouse))
		{
			foreach($newhouse as $value)
			{
				$sell[] = $value['id'];
			}
		}

		if($active_sell_num)
		{
			$block   =$this->getblockbysell($sell,'sell');
		}	
		else
		{
			
			$block   =$this->getblockbysell($sell,'rent');
		}
		
		
		$block_id = array_unique($block);
		
		$datainfo_1 =$this->getsellbyblock($block_id,4,$tbl);  //通过审核的数据

		
		$housedetail1num =$datainfo_1['housedetail1num'];
		$housedetail1	 =$datainfo_1['housedetail1'];
	
		//$housedetail1num =3;//测试数据,等测试完成后删除
		
		if($housedetail1num < 4)
		{
			//如果数量不足4条，则先推送网友关注的房源中对应的小区
			for($y =0;$y<count($sell_collect_house);$y++)
			{
				$collect_id[] =$sell_collect_house[$y]['collect_id'];
			}
			$collect_id = array_unique($collect_id);
			
			$block_id   =$this->getblockbysell($collect_id,'sell');
			
			$datainfo_2 =$this->getsellbyblock($block_id,4-$housedetail1num,$tbl);  //网友关注的房源中对应的小区的房源

			
			
			//$datainfo_2 =array(); //测试数据,等测试完成后删除
			if(is_array($datainfo_2['housedetail1']) && !empty($datainfo_2['housedetail1']))
			{
				if(!empty($datainfo_1['housedetail1']))
				{
					$housedetail =array_merge($datainfo_1['housedetail1'],$datainfo_2['housedetail1']); //审核的数据+关注的房源中对应的小区的房源
				}
				else
				{
					$housedetail =$datainfo_2['housedetail1']; 
				}
			}
			else
			{
				//推送网友关注的小区的房源
				for($y =0;$y<count($sell_collect_block);$y++)
				{
					$collect_block_id[] =$sell_collect_block[$y]['collect_id'];
				}
				$collect_block_id = array_unique($collect_block_id);
				
				$datainfo_3 =$this->getsellbyblock($collect_block_id,4-$housedetail1num,$tbl);  //网友关注的小区房源信息
				
				//$datainfo_3 =array(); //测试数据,等测试完成后删除

				if(is_array($datainfo_3['housedetail1']) && !empty($datainfo_3['housedetail1']))
				{
					if(!empty($datainfo_3['housedetail1']))
					{
						$housedetail =array_merge($datainfo_1['housedetail1'],$datainfo_3['housedetail1']); //审核的数据+关注的小区房源信息
					}
					else
					{
						$housedetail =$datainfo_3['housedetail1'];
					}
				}
				else
				{
					//获取网友游览过的数据
					$data_cookie =$this->getblockbycookie('sellinfo_blockid');

					$datainfo_4  =$this->getsellbyblock($data_cookie,4-$housedetail1num,$tbl);               //网友游览记录的房源信息

					
					
					//$datainfo_4 =array(); //测试数据,等测试完成后删除
					if(is_array($datainfo_4['housedetail1']) && !empty($datainfo_4['housedetail1']))
					{
						if(!empty($datainfo_3['housedetail1']))
						{
							$housedetail =array_merge($datainfo_1['housedetail1'],$datainfo_4['housedetail1']); //审核的数据+网友游览记录的房源信息
						}
						else
						{
							$housedetail =$datainfo_4['housedetail1'];
						}
					}
					else
					{
						//最新发布房源的数据
						$datainfo_5 =$this->getlateinfobysell(4-$housedetail1num);

						$housedetail =array_merge($datainfo_1['housedetail1'],$datainfo_5['housedetail1']); //审核的数据+最新发布房源的数据

						
					}
					
				}
				
			}
		}
		else
		{
			$housedetail = $housedetail1;
		}

		
		return $housedetail ;
	
	}

	/**
    *获取激活的租房数据
	*@author zhou
	*/
	function getlikehousebyactive_rent($active_sell_num,$newhouse,$sell_collect_house,$sell_collect_block, $uid , $tbl , $num ,$city = GB_CITY)
	{
		
		if(is_array($newhouse) && !empty($newhouse))
		{
			foreach($newhouse as $value)
			{
				$sell[] = $value['id'];
			}
		}
		
		if($active_sell_num)
		{
			$block   =$this->getblockbysell($sell,'sell');
		}	
		else
		{
			
			$block   =$this->getblockbysell($sell,'rent');
		}
		$block_id = array_unique($block);
	
		$datainfo_1 =$this->getrentbyblock($block_id,4,$tbl);      //通过审核的房源数据
		
		
		$housedetail1num =$datainfo_1['housedetail1num'];
		$housedetail1	 =$datainfo_1['housedetail1'];

		//$housedetail1num =3; //测试数据,开发结束删除
		if($housedetail1num<4)
		{
			//如果数量不足4条，则先推送网友关注的房源中对应的小区
			for($y =0;$y<count($sell_collect_house);$y++)
			{
				$collect_id[] =$sell_collect_house[$y]['collect_id'];
			}
			
			$collect_id = array_unique($collect_id);
			
			$block_id   =$this->getblockbysell($collect_id,'rent');

			$datainfo_2 =$this->getrentbyblock($block_id,4-$housedetail1num,$tbl);  //网友关注的房源中对应的小区的房源

			//$datainfo_2 =array(); //测试数据,等测试完成后删除
			
			if(is_array($datainfo_2['housedetail1']) && !empty($datainfo_2['housedetail1']))
			{
				if($datainfo_1['housedetail1'])
				{
					$housedetail =array_merge($datainfo_1['housedetail1'],$datainfo_2['housedetail1']); //审核的数据+关注的房源中对应的小区的房源
				}
				else
				{
					$housedetail =$datainfo_2['housedetail1'];
				}
			}
			else
			{
				//推送网友关注的小区的房源
				for($y =0;$y<count($sell_collect_block);$y++)
				{
					$collect_block_id[] =$sell_collect_block[$y]['collect_id'];
				}

				$collect_block_id = array_unique($collect_block_id);

				$datainfo_3 =$this->getrentbyblock($collect_block_id,4-$housedetail1num,$tbl);		//网友关注的小区房源信息

				//$datainfo_3 =array(); //测试数据,等测试完成后删除
				
				if(is_array($datainfo_3['housedetail1']) && !empty($datainfo_3['housedetail1']))
				{
					if($datainfo_1['housedetail1'])
					{
						$housedetail =array_merge($datainfo_1['housedetail1'],$datainfo_3['housedetail1']); //审核的数据+关注的小区房源信息
					}
					else
					{
						$housedetail =$datainfo_3['housedetail1']; 
					}
				}
				else
				{
					//获取网友游览过的数据
					$data_cookie =$this->getblockbycookie('rentinfo_blockid');
										
					$datainfo_4  =$this->getrentbyblock($data_cookie,4-$housedetail1num,$tbl);               //网友游览记录的房源信息

					//$datainfo_4 =array(); //测试数据,等测试完成后删除
		
					if(is_array($datainfo_4['housedetail1']) && !empty($datainfo_4['housedetail1']))
					{
						if($datainfo_1['housedetail1'])
						{
							$housedetail =array_merge($datainfo_1['housedetail1'],$datainfo_4['housedetail1']); //审核的数据+网友游览记录的房源信息
						}
						else
						{
							$housedetail =$datainfo_4['housedetail1'];
						}
					}
					else
					{
						//最新发布房源的数据
						$datainfo_5 =$this->getlateinfobyrent(4-$housedetail1num);
						
						if($datainfo_1['housedetail1'])
						{
							$housedetail =array_merge($datainfo_1['housedetail1'],$datainfo_5['housedetail1']); //审核的数据+最新发布房源的数据
						}
						else
						{
							$housedetail =$datainfo_5['housedetail1']; //审核的数据+最新发布房源的数据
						}
						
					}
							
				
				}
			}
			
		}
		else
		{
			$housedetail = $housedetail1;
		}
		return $housedetail ;
	}

	/**
    *获取激活的小区数据
	*@author zhou
	*/

	function getlikehousebyactive_block($active_sell_num,$newhouse,$sell_collect_house,$sell_collect_block, $uid , $tbl , $num ,$city = GB_CITY)
	{

		if(is_array($newhouse) && !empty($newhouse))
		{
			foreach($newhouse as $value)
			{
				$sell[] = $value['id'];
			}
		}
		
		if($active_sell_num)
		{
			$block   =$this->getblockbysell($sell,'sell');
		}	
		else
		{
			
			$block   =$this->getblockbysell($sell,'rent');
		}

		$block_id = array_unique($block);

		$datainfo_1 =$this->getblockbyblock($block_id,4,$tbl);      //通过审核的房源数据

		$housedetail1num =$datainfo_1['housedetail1num'];
		$housedetail1	 =$datainfo_1['housedetail1'];
		

		//$housedetail1num =3; //测试数据,开发结束删除
		
		if($housedetail1num<4)
		{
			//如果数量不足4条，则先推送网友关注的房源中对应的小区
			for($y =0;$y<count($sell_collect_house);$y++)
			{
				$collect_id[] =$sell_collect_house[$y]['collect_id'];
			}
			
			$collect_id = array_unique($collect_id);

			$block_id   =$this->getblockbysell($collect_id,'sell');

			$datainfo_2 =$this->getblockbyblock($block_id,4-$housedetail1num,$tbl);      //网友关注的房源中对应的小区数据
			
			//$datainfo_2 =array(); //测试数据,等测试完成后删除
			if(is_array($datainfo_2) && !empty($datainfo_2))
			{
				
				$housedetail =array_merge($datainfo_1['housedetail1'],$datainfo_2['housedetail1']); //审核的数据+关注的房源中对应的小区的房源
	
			}
			else
			{
				

				//推送网友关注的小区房源
				for($y =0;$y<count($sell_collect_block);$y++)
				{
					$collect_block_id[] =$sell_collect_block[$y]['collect_id'];
				}
				$collect_block_id = array_unique($collect_block_id);

				$datainfo_3 =$this->getblockbyblock($collect_block_id,4-$housedetail1num,$tbl);  //网友关注的小区房源信息

				//$datainfo_3 =array(); //测试数据,等测试完成后删除

				if(is_array($datainfo_3) && !empty($datainfo_3))
				{
					$housedetail =array_merge($datainfo_1['housedetail1'],$datainfo_3['housedetail1']); //审核的数据+关注的小区房源信息
				}
				else
				{
					
					//获取网友游览过的数据
					$data_cookie =$this->getblockbycookie('blockinfo_blockid');
					
					$datainfo_4  =$this->getblockbyblock($data_cookie,4-$housedetail1num,$tbl);           //网友游览记录的房源信息
					
					
					if(is_array($datainfo_4['housedetail1']) && !empty($datainfo_4['housedetail1']))
					{
						
						$housedetail =array_merge($datainfo_1['housedetail1'],$datainfo_4['housedetail1']); //审核的数据+网友游览记录的房源信息
					}
					else
					{
						//最新发布房源的数据
						$datainfo_5 =$this->getlateinfobysell(4-$housedetail1num);
						
						foreach($datainfo_5['housedetail1'] as $value_5)
						{
							$sell_5[] = $value_5['ID'];
						}
						
						$block_5   =$this->getblockbysell($sell_5,'sell');

						$block_id_5 = array_unique($block_5);

						$datainfo_5 =$this->getblockbyblock($block_id_5,4-$housedetail1num,$tbl);  //网友关注的小区房源信息

						$housedetail =array_merge($datainfo_1['housedetail1'],$datainfo_5['housedetail1']); //审核的数据+最新发布房源的数据

						
					}
				}

				
			}
			
			
			
		}

		else
		{
			$housedetail = $housedetail1;
		}

		return $housedetail ;
		
	}


   /**
	*获取小区的数据
	*@author zhou
	*/
	function getblockbyblock($block_id,$num,$tbl)
	{
		if( is_array($block_id) && !empty($block_id) )
		{
			$block_id = implode(',',$block_id);
			
			$sql ="SELECT averprice,streetid FROM block  "
							. "WHERE id in (".$block_id.")";
		    $temp_averprice_streetid = $this->get_data(array('form_name' => 'block','where'=>array('esta'=>2),'where_in'=>array('id',$block_id),'select'=>array('averprice,streetid')),'esfhousebak');
       
			
		
			$averprice		 = isset($temp_averprice_streetid[0]['averprice']) ? $temp_averprice_streetid[0]['averprice'] :0 ;
			$averprice_start =0.9*$averprice;
			$averprice_end   =1.1*$averprice;
			
			
		    if(isset($temp_averprice_streetid[0]['streetid'])){
			    $street_id = $temp_averprice_streetid[0]['streetid'];
				$sql = "SELECT id,blockname,district,b_img,streetid,address,averprice FROM block WHERE esta=2 and streetid = $street_id and averprice >=$averprice_start and averprice<=$averprice_end ORDER BY updatetime DESC LIMIT 0,$num";			
			}else{			
			    $sql = "SELECT id,blockname,district,b_img,streetid,address,averprice FROM block WHERE esta=2 and  averprice >=$averprice_start and averprice<=$averprice_end ORDER BY updatetime DESC LIMIT 0,$num";
			}
			
		
			$query = $this->db_esfhousebak->query($sql);
            $temp = $query->result_array();
		
			$housedetail1 = isset($temp) ? $temp : array() ;
		}

		else
		{
			$housedetail1num = 0;
		}
		$datainfo =array('housedetail1'=>$housedetail1,'housedetail1num'=>count($housedetail1));
		
		return $datainfo;
		
	
	}

	/**
    *获取最新租房发布数据
	*@author zhou
	*/

	function getlateinfobyrent($num)
	{
		$sql = "select id,district,streetid,blockshowname,room,hall,"
				. "buildarea,updatetime,floor,subfloor,totalfloor,fitment,"
				. "forward,buildyear,pic1,address,price from rent "
				. "where esta=1 and infofrom=1 and pic1 like 'http%' order by updatetime desc"
				. "  limit 0,$num"." luc"; //个人房源
		
		$temp =  $this->server_rent->query($sql);

		$housedetail1 = isset($temp['Record']) ? $temp['Record'] : array();

		if(empty($housedetail1))
		{
			$sql = "select id,district,streetid,blockshowname,room,hall,"
				. "buildarea,updatetime,floor,subfloor,totalfloor,fitment,"
				. "forward,buildyear,pic1,address,price from rent "
				. "where esta=1 and infofrom=2  and pic1 like 'http%' order by updatetime desc"
				. "  limit 0,$num"." luc"; //中介房源
		
			$temp =  $this->server_rent->query($sql);

			$housedetail1 = isset($temp['Record']) ? $temp['Record'] : array();

			if(empty($housedetail1))
			{
				$housedetail1num = 0;
			}
			else
			{
				$housedetail1num = count($housedetail1);
			}
		}
		else
		{
			$housedetail1num = count($housedetail1);
		}
		
		$datainfo =array('housedetail1'=>$housedetail1,'housedetail1num'=>count($housedetail1));

		return $datainfo;
	
	}

   /**
    *获取最新二手房发布数据
	*@author zhou
	*/

	function getlateinfobysell($num)
	{
		$sql = "select id,district,streetid,blockshowname,room,hall,"
				. "buildarea,updatetime,floor,subfloor,totalfloor,fitment,"
				. "forward,buildyear,pic1,address,price from sell "
				. "where esta=1 and infofrom =1  order by updatetime desc"
				. "  limit 0,$num"." luc";
		
		$temp =  $this->server_sell->query($sql);

		$housedetail1 = isset($temp['Record']) ? $temp['Record'] : array();
		
		if(empty($housedetail1))
		{
			
			$sql_agency = "select id,district,streetid,blockshowname,room,hall,"
				. "buildarea,updatetime,floor,subfloor,totalfloor,fitment,"
				. "forward,buildyear,pic1,address,price from sell "
				. "where esta=1 and infofrom =2  order by updatetime desc"
				. "  limit 0,$num"." luc";  //最新中介房源
			
			$temp_agency =  $this->server_sell->query($sql_agency);
			
			$housedetail1 = isset($temp_agency['Record']) ? $temp_agency['Record'] : array() ;

			if(empty($housedetail1))
			{
				$housedetail1num = 0;
			}
			else
			{
				$housedetail1num = count($housedetail1);
			}
		}
		else
		{
			$housedetail1num = count($housedetail1);
		}

		$datainfo =array('housedetail1'=>$housedetail1,'housedetail1num'=>count($housedetail1));

		return $datainfo;
	
	}


   /**
	*对应的房源转化为小区
	*
	*/
	function getblockbysell($sell,$tbl)
	{
		$sell_id = implode(',',$sell);

		$sql = "SELECT blockid FROM $tbl WHERE id IN (".$sell_id.")";
		$sql = "SELECT blockid FROM sell WHERE id IN (".$sell_id.")";
	
		
		$selldetail = $this->server_sell->query($sql);
	
		if( count($selldetail['Record']) > 0 && is_array($selldetail['Record']) &&  isset($selldetail['Record']))
		{
			
			foreach($selldetail['Record'] as $value)
			{
				$block[] = isset($value['BLOCKID']) ? $value['BLOCKID'] :  $value['blockid'] ;
			}
		}
	

		return $block;
	
	}


	
	/**
	 *对应小区转化为房源
	 *
	 */
   
    function getrentbyblock($block_id,$num,$tbl)
	{
		
		if( is_array($block_id) && !empty($block_id) )
		{
			$block_id = implode(',',$block_id);

			$sql = "SELECT id,district,streetid,blockshowname,room,hall,"
			. "buildarea,updatetime,floor,subfloor,totalfloor,fitment,"
			. "forward,buildyear,pic1,address,price,priceunit FROM $tbl "
			. "WHERE blockid in (".$block_id.") AND esta = 1 and infofrom=1 "
			. "ORDER BY updatetime DESC LIMIT 0,$num luc";  //个人租房房源
			
			
			$temp =  $this->server_rent->query($sql);
			
			
			$housedetail1 = isset($temp['Record']) ? $temp['Record'] : array() ;

			if(empty($housedetail1))
			{

				$sql = "SELECT id,district,streetid,blockshowname,room,hall,"
				. "buildarea,updatetime,floor,subfloor,totalfloor,fitment,"
				. "forward,buildyear,pic1,address,price,priceunit FROM $tbl "
				. "WHERE blockid in (".$block_id.") AND esta = 1 and infofrom=2 "
				. "ORDER BY updatetime DESC LIMIT 0,$num luc";  //中介租房房源
				
				
				$temp =  $this->server_rent->query($sql);
				
				
				$housedetail1 = isset($temp['Record']) ? $temp['Record'] : array() ;

				if(empty($housedetail1))
				{
					$housedetail1num = 0;
				}
				else
				{
					$housedetail1num = count($housedetail1);
				}
			}
			else
			{
				$housedetail1num = count($housedetail1);
			}

			
		}

		else
		{
			$housedetail1num = 0;
		}
		$datainfo =array('housedetail1'=>$housedetail1,'housedetail1num'=>$housedetail1num);
		return $datainfo;
	
	
	}
	

	
	/**
	 *对应小区转化为房源
	 *
	 */
   
    function getsellbyblock($block_id,$num,$tbl)
	{
		if( is_array($block_id) && !empty($block_id) )
		{
			$block_id = implode(',',$block_id);

			/*$sql = "SELECT id,esta,district,streetid,blockshowname,room,hall,"
			. "buildarea,updatetime,floor,subfloor,totalfloor,fitment,"
			. "forward,buildyear,pic1,address,price FROM $tbl "
			. "WHERE blockid in (".$block_id.") AND esta = 1 and infofrom=1 "
			. "ORDER BY updatetime DESC LIMIT 0,$num luc";	*/	//最近个人房源

			$sql = "SELECT id,esta,district,streetid,blockshowname,room,hall,"
			. "buildarea,updatetime,floor,subfloor,totalfloor,fitment,"
			. "forward,buildyear,pic1,address,price FROM sell "
			. "WHERE blockid in (".$block_id.") AND esta = 1 and infofrom=1 "
			. "ORDER BY updatetime DESC LIMIT 0,$num luc";		//最近个人房源
			
			$temp =  $this->server_sell->query($sql);
			
			$housedetail1 = isset($temp['Record']) ? $temp['Record'] : array() ;

			

			if(empty($housedetail1))
			{
				
				/*$sql_agency = "SELECT id,district,streetid,blockshowname,room,hall,"
				. "buildarea,updatetime,floor,subfloor,totalfloor,fitment,"
				. "forward,buildyear,pic1,address,price FROM $tbl "
				. "WHERE blockid in (".$block_id.") AND esta = 1 and infofrom=2 "
				. "ORDER BY updatetime DESC LIMIT 0,$num luc";  *///最新中介房源

				$sql_agency = "SELECT id,district,streetid,blockshowname,room,hall,"
				. "buildarea,updatetime,floor,subfloor,totalfloor,fitment,"
				. "forward,buildyear,pic1,address,price FROM sell "
				. "WHERE blockid in (".$block_id.") AND esta = 1 and infofrom=2 "
				. "ORDER BY updatetime DESC LIMIT 0,$num luc";  //最新中介房源
				
				$temp_agency =  $this->server_sell->query($sql_agency);
				
				$housedetail1 = isset($temp_agency['Record']) ? $temp_agency['Record'] : array() ;

				if(empty($housedetail1))
				{
					$housedetail1num = 0;
				}
				else
				{
					$housedetail1num = count($housedetail1);
				}
			}
			else
			{
				$housedetail1num = count($housedetail1);
			}

			
		}
		else
		{
			$housedetail1num = 0;
		}
		$datainfo =array('housedetail1'=>$housedetail1,'housedetail1num'=>$housedetail1num);
		return $datainfo;
	
	
	}
   /**
    *获取网友游览记录中最新游览过房源对应的小区
	*@author zhou
	*/

	function getblockbycookie($key)
	{
		
		if(GB_CITY == 'nj')
		{
			
			$sellinfo_blockid = isset($_COOKIE[$key]) ? $_COOKIE[$key] : '';
		}
		else
		{
			$sellinfo_blockid = isset($_COOKIE[$key.GB_CITY]) ? $_COOKIE[$key.GB_CITY] : ''; 
		}
		
		//获取cookie记录
		if( !empty($sellinfo_blockid) )
		{
			$array_sellinfo_blockid = explode(',',$sellinfo_blockid);
			//if(0)//测试数据写好后，注释掉，打开下一行
			if(count( $array_sellinfo_blockid ) > 4)
			{
				$block_id = array_slice( $array_sellinfo_blockid ,0,4);
			}
			else if( count( $array_sellinfo_blockid ) == 0 )
			{
				$block_id = array();
			}
			else
			{
				$block_id = $array_sellinfo_blockid; 
			}
		}

		$block = array_unique($block_id);
		
		return $block;
	
	}



   /**
    * 获取关注的房源对应的小区
    * @author zhou
    */
	function getcollectblockbyhouse( $uid , $city = GB_CITY,$tbl)
	{
		$key = $this->memkey.'_'.$tbl.'_getcollectblockbyhouse_'.$uid;
		
		if($this->input->get('update') == 1)
		{
			$this->memcached_class->delete($key);
		}

		if( empty($block) )
		{
			if($tbl=='block')
			{
				$block_array =  $this->get_data(array('form_name' => 'personal_collection','where'=>array('uid'=>$uid,'city_name'=>$city,'collect_id <>'=>0,'kind'=>2),'select'=>array('collect_id')),'esfbak');

				if(is_array($block_array) && !empty($block_array))
				{
					foreach($block_array as $value)
					{
						$block[] = $value['collect_id'];
					}
				}
			}
			if($tbl=='sell')
			{
				$sell_array =  $this->get_data(array('form_name' => 'personal_collection','where'=>array('uid'=>$uid,'city_name'=>$city,'collect_id <>'=>0,'kind'=>3),'select'=>array('collect_id')),'esfbak');

				if(is_array($sell_array) && !empty($sell_array))
				{
					foreach($sell_array as $value)
					{
						$temp_array[] = $value['collect_id'];
					}

					$sell_id = implode(',',$temp_array);
					$sql = "SELECT blockid FROM sell WHERE id IN (".$sell_id.") luc";

					$selldetail = $this->server_sell->query($sql);
					if( $selldetail['TOTAL'] > 0 && is_array($selldetail['Record']) &&  isset($selldetail['Record']))
					{
						foreach($selldetail['Record'] as $value)
						{
							$block[] = $value['BLOCKID'];
						}
					}
				}
			}
			if($tbl=='rent')
			{
				$rent_array =  $this->get_data(array('form_name' => 'personal_collection','where'=>array('uid'=>$uid,'city_name'=>$city,'collect_id <>'=>0,'kind'=>4),'select'=>array('collect_id')),'esfbak');

				if(is_array($rent_array) && !empty($rent_array))
				{
					foreach($rent_array as $value)
					{
						$temp_array[] = $value['collect_id'];
					}

					$rent_id = implode(',',$temp_array);
					$sql = "SELECT blockid FROM rent WHERE id IN (".$rent_id.") luc";

					$rentdetail = $this->server_rent->query($sql);

					if(!empty($rentdetail['Record']))
					{
						foreach($rentdetail['Record'] as $value)
						{

							$block[] = $value['BLOCKID'];
						}

					}
				}
			}
            if( is_array($block) && !empty($block))
            {
                $block = array_unique($block);
            }
            else
            {
                $block = array();
            }
			
			$this->memcached_class->add($key, $block,10);
		}

		return $block;

	
	}


   /**
    * 获取关注的小区
    * @author zhou
    */
    function getcollectblockbytype( $uid , $city = GB_CITY,$tbl)
    {
    	$key = $this->memkey.'_'.$tbl.'_getcollectblock_'.$uid;
		
		if($this->input->get('update') == 1)
		{
			$this->memcached_class->delete($key);
		}

		$block = $this->memcached_class->get($key);
		
		if( empty($block) )
		{
			if($tbl=='block')
			{
				$block_array =  $this->get_data(array('form_name' => 'personal_collection','where'=>array('uid'=>$uid,'city_name'=>$city,'collect_id <>'=>0,'kind'=>2),'select'=>array('collect_id')),'esfbak');

				if(is_array($block_array) && !empty($block_array))
				{
					foreach($block_array as $value)
					{
						$block[] = $value['collect_id'];
					}
				}
			}
			if($tbl=='sell')
			{
				$sell_array =  $this->get_data(array('form_name' => 'personal_collection','where'=>array('uid'=>$uid,'city_name'=>$city,'collect_id <>'=>0,'kind'=>3),'select'=>array('collect_id')),'esfbak');

				if(is_array($sell_array) && !empty($sell_array))
				{
					foreach($sell_array as $value)
					{
						$block[] = $value['collect_id'];
					}

					
				}
			}
			if($tbl=='rent')
			{
				$rent_array =  $this->get_data(array('form_name' => 'personal_collection','where'=>array('uid'=>$uid,'city_name'=>$city,'collect_id <>'=>0,'kind'=>4),'select'=>array('collect_id')),'esfbak');

				if(is_array($rent_array) && !empty($rent_array))
				{
					foreach($rent_array as $value)
					{
						$block[] = $value['collect_id'];
					}

					
				}
			}
            if( is_array($block) && !empty($block))
            {
                $block = array_unique($block);
            }
            else
            {
                $block = array();
            }
			
			$this->memcached_class->add($key, $block,10);
		}

		return $block;
    }



    /**
    * 获取用户的可能感兴趣的
    * @author cxf
    */
    function getlikehouse( $uid , $tbl , $num ,$city = GB_CITY)
    {
        $key = $this->memkey.'_getlikehouse'.$uid.'_'.$tbl.'_'.$num.'_'.$city;

        if($this->input->get('update') == 1)
        {
            $this->memcached_class->delete($key);
        }
        
        $housedetail = $this->memcached_class->get($key);

        if(empty($housedetail) )
        {
            //获取关注的所有小区
            $block_id = $this->getcollectblock($uid,$city);

            //获取对应信息
            if($tbl == 'sell')
            {
				 if(GB_CITY == 'nj')
				 {
					$sellinfo_blockid = isset($_COOKIE['sellinfo_blockid']) ? $_COOKIE['sellinfo_blockid'] : '';
				 }
				 else
				{
					$sellinfo_blockid = isset($_COOKIE['sellinfo_blockid_'.GB_CITY]) ? $_COOKIE['sellinfo_blockid_'.GB_CITY] : ''; 
				}

                //获取cookie记录
                if( !empty($sellinfo_blockid) )
                {
                    $array_sellinfo_blockid = explode(',',$sellinfo_blockid);

                    if(count( $block_id ) < 4 && count( $block_id ) > 0 && count( $array_sellinfo_blockid ) > 0 )
                    {
                        $block_id = array_merge( $block_id , $array_sellinfo_blockid );
                    }
                    else if( count( $block_id ) == 0 )
                    {
                        $block_id = $array_sellinfo_blockid;
                    }
                }

                $block_id = array_unique($block_id);
   
                if( is_array($block_id) && !empty($block_id) )
                {
                    foreach($block_id as $value)
                    {
                        $sql = "SELECT id,district,streetid,blockshowname,room,hall,"
                                . "buildarea,updatetime,floor,subfloor,totalfloor,fitment,"
                                . "forward,buildyear,pic1,address,price FROM sell "
                                . "WHERE blockid = '".$value."' AND esta = 1 "
                                . "AND pic1 LIKE 'http%' ORDER BY updatetime DESC LIMIT 0,1 luc";
                        $temp =  $this->server_sell->query($sql);
						$housedetail1[] = isset($temp['Record'][0]) ? $temp['Record'][0] : array() ;

                    }
                    if(empty($housedetail1))
                    {
                    $housedetail1num = 0;
                    }else{
                    $housedetail1num = count($housedetail1);
                    }
                }
                else
                {
                    $housedetail1num = 0;
                }

                //补齐最新房源
                if($housedetail1num < 4)
                {
                    $sql = "select id,district,streetid,blockshowname,room,hall,"
                            . "buildarea,updatetime,floor,subfloor,totalfloor,fitment,"
                            . "forward,buildyear,pic1,address,price from sell "
                            . "where esta=1 and pic1 like 'http%' order by updatetime desc"
                            . "  limit 0,".(4-$housedetail1num)." luc";
                    $temp =  $this->server_sell->query($sql);

                    $housedetail2 = isset($temp['Record']) ? $temp['Record'] : array();

                    if($housedetail1num>=1 && !empty($housedetail2))
                    {
                        $housedetail = array_merge($housedetail1,$housedetail2);
                    }
                    else
                    {
                        $housedetail = $housedetail2;
                    }
                }
                else
                {
                    $housedetail = $housedetail1;
                }

        }
        elseif( $tbl == 'rent' )
        {
                $rentinfo_blockid = GB_CITY=='nj'?$_COOKIE['rentinfo_blockid']:$_COOKIE['rentinfo_blockid_'.GB_CITY];
                if(!empty($rentinfo_blockid)){
                $array_rentinfo_blockid = explode(',',$rentinfo_blockid);
                if(count($block_id)<4 && count($block_id)>0 && count($array_rentinfo_blockid)>0)
                {
                   $block_id = array_merge($block_id,$array_rentinfo_blockid);
                }
                elseif(count($block_id)==0)
                {
                   $block_id = $array_rentinfo_blockid;
                }
                }

               $block_id = array_unique($block_id);
               if(is_array($block_id) && !empty($block_id))
               {
                    foreach($block_id as $value)
                    {
                        $sql = "select id,district,streetid,blockshowname,room,hall,buildarea,updatetime,floor,subfloor,"
                                . "totalfloor,fitment,forward,buildyear,pic1,address,price,priceunit from rent "
                                . "where blockid = $value and esta=1 and pic1 like 'http%' order by updatetime desc  limit 0,1 luc";
                        $temp = $this->server_rent->query($sql);

                        $housedetail1[] = !empty($temp['Record'][0])?$temp['Record'][0]:'';
                    }
                    if(empty($housedetail1))
                    {
                    $housedetail1num = 0;
                    }else{
                    $housedetail1num = count($housedetail1);
                    }
               }else{
                    $housedetail1num =0;
               }


               //补齐最新房源
               if($housedetail1num<4)
               {
               $sql = "select id,district,streetid,blockshowname,room,hall,buildarea,updatetime,floor,subfloor,totalfloor,fitment,forward,buildyear,pic1,address,price,priceunit from rent where esta=1 and pic1 like 'http%' order by updatetime desc  limit 0,".(4-$housedetail1num)." luc";
               $temp  = $this->server_rent->query($sql);
               $housedetail2 = $temp['Record'];

                   if($housedetail1num>=1 && !empty($housedetail2))
                   {
                    $housedetail = array_merge($housedetail1,$housedetail2);
                   }else{
                    $housedetail = $housedetail2;
                   }
               }
               else
               {
                   $housedetail = $housedetail1;
               }
        }
        else if( $tbl == 'block' )
        {
            //获取关注的所有小区
            $gz_bid_arr = $this->getcollectblock($uid,$city);
            $gz_num = count($gz_bid_arr);

            //缓存的小区编号
            $cookie_bid_arr = array();

            //关注小区不够数量的情况下，依顺序查询小区、二手房、租房浏览COOKIE
            if( $gz_num < $num )
            {
                $limit_num = $num - $gz_num;
                $blockinfo_blockid = GB_CITY=='nj'?$_COOKIE['blockinfo_blockid']:$_COOKIE['blockinfo_blockid_'.GB_CITY];
                $sellinfo_blockid = GB_CITY=='nj'?$_COOKIE['sellinfo_blockid']:$_COOKIE['sellinfo_blockid_'.GB_CITY];
                $rentinfo_blockid = GB_CITY=='nj'?$_COOKIE['rentinfo_blockid']:$_COOKIE['rentinfo_blockid_'.GB_CITY];
                //浏览的相关小区
                if( $limit_num > 0 && !empty($blockinfo_blockid))
                {
                     $array_blockinfo_blockid = explode( ',' , $blockinfo_blockid);

                     foreach( $array_blockinfo_blockid as $value)
                     {
                        if( $limit_num <= 0 )
                        {
                            break;
                        }

                        if(!in_array($value , $gz_bid_arr))
                        {
                            $cookie_bid_arr[] = $value;
                            $limit_num -- ;
                        }
                     }
                }

                //浏览的二手房相关小区
                if( $limit_num > 0 && !empty($sellinfo_blockid))
                {
                    $array_sellinfo_blockid = explode( ',' , $sellinfo_blockid );

                    foreach( $array_sellinfo_blockid as $value )
                    {
                        if( $limit_num <= 0 )
                        {
                            break;
                        }

                        if(!in_array($value , $gz_bid_arr))
                        {
                            $cookie_bid_arr[] = $value;
                            $limit_num -- ;
                        }
                    }
                }

                //浏览的租房相关小区
                if( $limit_num > 0 && !empty($rentinfo_blockid) )
                {
                    $array_rentinfo_blockid = explode( ',' , $rentinfo_blockid);

                    foreach( $array_rentinfo_blockid as $value)
                    {
                        if( $limit_num <= 0 )
                        {
                            break;
                        }

                        if(!in_array($value , $gz_bid_arr))
                        {
                            $cookie_bid_arr[] = $value;
                            $limit_num -- ;
                        }
                    }
                }
            }

            //合并关注小区和浏览的小区
           $search_bid_arr  = array();
           if( is_array($gz_bid_arr) && !empty($gz_bid_arr) && is_array($cookie_bid_arr) && !empty($cookie_bid_arr) )
           {
                $search_bid_arr = array_merge($gz_bid_arr,$cookie_bid_arr);
           }
           else if(is_array($gz_bid_arr) && !empty($gz_bid_arr))
           {
                $search_bid_arr = $gz_bid_arr;
           }
           else if(is_array($cookie_bid_arr) && !empty($cookie_bid_arr))
           {
               $search_bid_arr = $cookie_bid_arr;
           }

           //应该搜索的小区编号数组
           $block_id =  '';
           if(is_array($search_bid_arr) && !empty($search_bid_arr))
           {
                //随机取一个小区编号
                $rand_key = array_rand( $search_bid_arr);
                $rand_bid = $search_bid_arr[$rand_key];

                //根据随机的小区编号获取周边小区（去除关注的小区编号）
                $block_id = $this->block_model->getsearchBlockNearly( $rand_bid , $gz_bid_arr , $num );
           }

            //随机取4个小区
            $blockids = !empty($block_id) ?  explode( ',' , $block_id) : array();

            //获取小区详细信息
            $blockdetail = array();
            if( is_array($blockids) && !empty($blockids) )
            {
                $blockdetail = $this->block_model->getarrayblockdetail( $blockids , array('id','blockname','district','b_img','streetid','address','averprice') );
            }

            //补齐二手房数量最多的小区
            $like_block_num =  count($blockdetail);
            
            if( $like_block_num < 4)
            {
      
                $extend_block_num = $num - $like_block_num;
                $block =  $this->get_data(array('form_name' => 'block','order_by'=>'sellcount desc','where'=>array('esta'=>2,'b_img !='=>''),'select'=>array('id','blockname','district','b_img','streetid','address','averprice'),'limit'=>($extend_block_num)),'esfhousebak');
                if( $like_block_num >= 1 )
                {
                    if( is_array($block) && !empty($block) )
                    {
                        $housedetail = array_merge( $blockdetail , $block );
                    }
                    else
                    {
                        $housedetail = $blockdetail;
                    }
                }
                else
                {
                    $housedetail = $block;
                }
            }
            else
            {
                $housedetail = $blockdetail;
            }
        }

            $this->memcached_class->add($key, $housedetail,600);
        }

        return $housedetail;
    }

    /**
    * 获取用户关注信息转化成小区
    * @author cxf
    */
    function getcollectblock( $uid , $city = GB_CITY)
    {
    	$key = $this->memkey.'_getcollectblock_'.$uid;

		if($this->input->get('update') == 1)
		{
			$this->memcached_class->delete($key);
		}

		$block = $this->memcached_class->get($key);

		if( empty($block) )
		{
			$block_array =  $this->get_data(array('form_name' => 'personal_collection','where'=>array('uid'=>$uid,'city_name'=>$city,'collect_id <>'=>0,'kind'=>2),'select'=>array('collect_id')),'esfbak');

            if(is_array($block_array) && !empty($block_array))
            {
                foreach($block_array as $value)
                {
                    $block[] = $value['collect_id'];
                }
            }

            $sell_array =  $this->get_data(array('form_name' => 'personal_collection','where'=>array('uid'=>$uid,'city_name'=>$city,'collect_id <>'=>0,'kind'=>3),'select'=>array('collect_id')),'esfbak');

            if(is_array($sell_array) && !empty($sell_array))
            {
                foreach($sell_array as $value)
                {
                    $temp_array[] = $value['collect_id'];
                }

                $sell_id = implode(',',$temp_array);
                $sql = "SELECT blockid FROM sell WHERE id IN (".$sell_id.") luc";

                $selldetail = $this->server_sell->query($sql);
                if( $selldetail['TOTAL'] > 0 && is_array($selldetail['Record']) &&  isset($selldetail['Record']))
                {
                    foreach($selldetail['Record'] as $value)
                    {
                        $block[] = $value['BLOCKID'];
                    }
                }
            }

            $rent_array =  $this->get_data(array('form_name' => 'personal_collection','where'=>array('uid'=>$uid,'city_name'=>$city,'collect_id <>'=>0,'kind'=>4),'select'=>array('collect_id')),'esfbak');

            if(is_array($rent_array) && !empty($rent_array))
            {
                foreach($rent_array as $value)
                {
                    $temp_array[] = $value['collect_id'];
                }

                $rent_id = implode(',',$temp_array);
                $sql = "SELECT blockid FROM rent WHERE id IN (".$rent_id.") luc";

                $rentdetail = $this->server_rent->query($sql);

                if(!empty($rentdetail['Record']))
                {
                    foreach($rentdetail['Record'] as $value)
                {

                $block[] = $value['BLOCKID'];
            }

            }
            }

            if( is_array($block) && !empty($block))
            {
                $block = array_unique($block);
            }
            else
            {
                $block = array();
            }

			$this->memcached_class->add($key, $block,10);
		}

	return $block;
    }

        /**
    * 获取房源可刷新次数
    * @author cxf
    */
	function getRefreshByid($type,$id){
	    //验证刷新可用次数
		$num = $this->house_model->yzrefrshnum($this->user_id,$id,$type);

		//获取今日已刷新次数
		$this->house_statistic_model->set_uid($this->user_id);
		$this->house_statistic_model->set_city(GB_CITY);
		$this->house_statistic_model->set_nocache(1);
		$arr = $this->house_statistic_model->get_operate_list(2,$id,date("Y-m-d"));
        $num = $num - count($arr);
		return $num>0?$num:0;
	}

        /**
    * 更新求租求购状态
    * @return string
    */
    function updatewant($id,$tbl,$up)
    {
        if( $id > 0)
        {
			//2015-11-25 zhou 删除状态修改  --------- start--------->
			if($tbl=='buy')
			{
				$sql = "UPDATE buy_message_match set esta=0 WHERE buy_id = '".$id."'";
				$this->db_esfhouse->query($sql);
			}
			if($tbl=='rentwanted')
			{
				$sql = "UPDATE rentwanted_message_match set esta=0 WHERE rentwanted_id = '".$id."'";
				$this->db_esfhouse->query($sql);
			}

			//2015-11-25 zhou 删除状态修改  ---------- end --------->
            $arr = array( 'id' => $id );
            $this->set_table($tbl);
            $result = $this->save($arr,$up,'esfhouse');
        }

        return $result;
    }

    /**
    * 更新求租求购的匹配条数
    * @return string
    */
    function updatematched_num($id,$tbl)
    {
        if( $id > 0)
        {
         $sql = "UPDATE $tbl set matched_num=matched_num-1 WHERE `id` = '".$id."'";
		 $result = $this->db_esfhouse->query($sql);
        }

        return $result;
    }
 

    /**
    * 获取用户的看房信息
    * @author cxf
    */
    function get_seehouse_by_telno($telno,$offset=0,$pagesize=0,$sendtime='')
    {
        $key = $this->memkey.'_get_seehouse_by_telno_'.$telno.$offset.$pagesize.$sendtime;

		if($this->input->get('update') == 1)
		{
			$this->memcached_class->delete($key);
		}

		$seehouse_array = $this->memcached_class->get($key);

		if( empty($seehouse_array) )
		{
        if(!empty($sendtime)){
        $seehouse_array =  $this->get_data(array('form_name' => 'see_house','where'=>array('telno'=>$telno,'esta'=>1,'dateline >'=>$sendtime),'limit'=>$offset,'offset'=>$pagesize,'order_by'=>'dateline DESC','select'=>array('name','id','telno','uid','rowid','tbl')),'esfhousebak');
        }else{
        $seehouse_array =  $this->get_data(array('form_name' => 'see_house','where'=>array('telno'=>$telno,'esta'=>1),'limit'=>$offset,'offset'=>$pagesize,'order_by'=>'dateline DESC','select'=>array('name','id','telno','uid','rowid','tbl')),'esfhousebak');
        }
		$this->memcached_class->add($key, $seehouse_array,3600);
		}
        return $seehouse_array;
    }
	
	/**
    * 获取用户的发送到手机信息
    * @author cxf
    */
    function get_smshouse_by_telno($telno,$offset=0,$pagesize=0,$sendtime='')
    {
         $key = $this->memkey.'_get_smshouse_by_telno_'.$telno.$offset.$pagesize.$sendtime;

		if($this->input->get('update') == 1)
		{
			$this->memcached_class->delete($key);
		}

		$smshouse_array = $this->memcached_class->get($key);

		if( empty($smshouse_array) )
		{        
            if(!empty($sendtime)){
               $smshouse_array =  $this->get_data(array('form_name' => 'user_sms_house','where'=>array('telno'=>$telno,' 	dateline >'=>$sendtime),'limit'=>$offset,'offset'=>$pagesize,'order_by'=>'dateline DESC','select'=>array('rowid','tbl','id')),'esfhousebak');
            }else{
               $smshouse_array =  $this->get_data(array('form_name' => 'user_sms_house','where'=>array('telno'=>$telno),'limit'=>$offset,'offset'=>$pagesize,'order_by'=>'dateline DESC','select'=>array('rowid','tbl','id')),'esfhousebak');            
            }
		$this->memcached_class->add($key, $smshouse_array,3600);
		}
        return $smshouse_array[0];
    }

    /**
    * 获取用户的关注房源信息
    * @author cxf
    */
    function get_collecthouse_by_uid($uid,$city=GB_CITY,$sendtime=''){
	    $key = $this->memkey.'_get_collecthouse_by_uid_'.$uid.$city.$sendtime;

		if($this->input->get('update') == 1)
		{
			$this->memcached_class->delete($key);
		}

		$collect_id = $this->memcached_class->get($key);

		if( empty($collect_id) )
		{  
        if(!empty($sendtime)){
        $sql = "select collect_id,id,kind from personal_collection where uid='".($uid)."' and city_name ='".($city)."' and (kind=3 or kind=4) and collect_time>'".$sendtime."' order by collect_time desc limit 0,1";
		}else{
        $sql = "select collect_id,id,kind from personal_collection where uid='".($uid)."' and city_name ='".($city)."' and (kind=3 or kind=4) order by collect_time desc limit 0,1";        
        }
		$query = $this->db_esfbak->query($sql);
        $collect_id = $query->result_array();
		$this->memcached_class->add($key, $collect_id,3600);
		}
		return $collect_id[0];
    }
	
    /**
    * 根据用户行为更新用户城市
    * @author cxf
    */
    function Updateusercity($uid,$city=GB_CITY){
	        if(empty($uid)){
			return 0;
			}
	        $arr = array( 'id' => $uid,'is_use'=>1);
			$up = array( 'cityname' => $city );
            $this->set_table('personal_user');
            $result = $this->save($arr,$up,'esf');
			return $result;
	}

	/**
	*感兴趣的学区房（model下面的）
	*@param int $uid 
	*@return array
	*/
	public function getLikeSchoolHouse($uid){

		$key = $this->memkey.'get_lise_school_house_'.$uid;
		if($this->input->get('update') == 1)
		{
		  $this->memcached_class->delete($key);
		}
		
		$house = $this->memcached_class->get($key);
		
		if(empty($house))
		{
			$houseNum = 0;

			//获取关注的学校
			$block = array();		
			$school = $this->getCollectSchool($uid , GB_CITY);		
			if(is_array($school)&&!empty($school))
			{
				foreach($school as $value)
				{
					$tempBlock = $this->getSchoolBlock($value);
					if(!empty($tempBlock)&&is_array($tempBlock))
					{
						$block = array_merge($tempBlock,$block); 
					}else{
						$block = $tempBlock;
					}
					 
				}
				$block = (is_array($block) && !empty($block)) ?  array_unique($block) : array();
				if(is_array($block)&&!empty($block))
				{
					$blockStr = implode(",",$block);
					$blockStr = rtrim($blockStr,',');
					$sql = "SELECT id,district,streetid,blockshowname,room,hall,"
										. "buildarea,updatetime,floor,subfloor,totalfloor,fitment,"
										. "forward,buildyear,pic1,address,price FROM sell "
										. "WHERE blockid  IN (".$blockStr.") AND esta = 1 AND infotype=1 "
										. "ORDER BY creattime DESC LIMIT 0,4 luc";							
					$info = $this->server_sell->query($sql);
					$house = $info['Record'];
					$houseNum = count($house);
				}
			
			}    
			
			if($houseNum < 4)
			{   //根据游览记录
				if(GB_CITY == 'nj')
				{
					$sellinfoBlock = isset($_COOKIE['sellinfo_blockid']) ? $_COOKIE['sellinfo_blockid'] : '';
				}else{
					$sellinfoBlock = isset($_COOKIE['sellinfo_blockid_'.GB_CITY]) ? $_COOKIE['sellinfo_blockid_'.GB_CITY] : ''; 
				}

				//获取cookie记录获取最新创建的房源
				if(!empty($sellinfoBlock))
				{
					$remainNum = 4-$houseNum;               				
					$blockStr = rtrim($sellinfoBlock,',');
					$sql = "SELECT id,district,streetid,blockshowname,room,hall,"
									. "buildarea,updatetime,floor,subfloor,totalfloor,fitment,"
									. "forward,buildyear,pic1,address,price FROM sell "
									. "WHERE blockid  IN (".$blockStr.") AND esta = 1 AND infotype=1 "
									. "ORDER BY creattime DESC LIMIT 0,$remainNum luc";				
				   
					$info= $this->server_sell->query($sql);
					$tempHouse = $info['Record'];
					if($remainNum==4)
					{
						$house = $tempHouse;
					}else{
						$house = array_merge($tempHouse,$house);
					}
					$houseNum += count($house);

				}
			}
			if($houseNum < 4)
			{
				$remainNum = 4-$houseNum;               				
				$blockStr = "学区";
				$sql = "SELECT id,district,streetid,blockshowname,room,hall,"
								. "buildarea,updatetime,floor,subfloor,totalfloor,fitment,"
								. "forward,buildyear,pic1,address,price FROM sell "
								. "WHERE address  like '%".$blockStr."%' AND esta = 1 AND infotype=1 "
								. "ORDER BY creattime DESC LIMIT 0,$remainNum luc";
				
				$info = $this->server_sell->query($sql);
				$tempHouse = $info['Record'];
				 
				if($remainNum==4)
				{
					$house = $tempHouse;
				}else{
					$house = array_merge($tempHouse,$house);
				}
				$houseNum += count($house);
			
			}

			$this->memcached_class->add($key, $house,1000);
		}
		return $house;
		
	}




	



	
	/**
	*获取关注的学校（model下面的）
	*@param int $uid 
	*@param string $city 城市
	*@param int $limit 获取学校的个数
	*@return array
	*/
	public function getCollectSchool($uid,$city=GB_CITY,$limit=10)
	{

		$school_array =  $this->get_data(array('form_name' => 'personal_collection','where'=>array('uid'=>$uid,'city_name'=>$city,'collect_id <>'=>0,'kind'=>7),'select'=>array('collect_id'),'order_by'=>'id DESC',),'esfbak');
		if(is_array($school_array) && !empty($school_array))
		{
			 foreach($school_array as $key=>$value)
             {
				 if($key<$limit)//限制获取的个数
			     {					
					 $school[] = $value['collect_id'];
				 }
                    
             }
        }
		return $school;

	}

	
	/**
	*获取学校下面的小区（model下面的）
	*@param int schoolId	
	*@return array
	*/
	public function getSchoolBlock($schoolId)
	{	
		$key = $this->memkey.'get_school_block_arr_'.$schoolId;
		if($this->input->get('update') == 1)
		{
			$this->memcached_class->delete($key);
		}
		$block = $this->memcached_class->get($key);
		if(empty($block) )
		{
			$schoolBlock = $this->get_data(array('form_name' => 'school_block','where'=>array('school_id'=>$schoolId),'select'=>array('block_id')),'esfhousebak');
			if(is_array($schoolBlock) && !empty($schoolBlock))
			{
				 foreach($schoolBlock as $value)
				 {
					 $block[] = $value['block_id'];
				 }
			}
			$this->memcached_class->add($key, $block,3600);
		}
		return $block;
	}  
	



	 /**
	 * 根据学校id获取本学区最新房源信息
	 * @param int $school
	 * @return array
	 */
	public function getSchoolLastestHouse($school)
	{
		$key = $this->memkey.'get_school_house_lastest_new_'.$school;
		if($this->input->get('update') == 1)
		 {
		  $this->memcached_class->delete($key);
		 }
		$housedetail = $this->memcached_class->get($key);
		if(empty($housedetail) )
		{
		  $block = $this->getSchoolBlock($school);
		  $selldetail = array();
		  if(is_array($block)&&!empty($block))
		  {
			  $blockStr = implode(",",$block);
		      $blockStr = rtrim($blockStr,',');		  
		      $sql = "select id,district,streetid,blockshowname,room,hall,buildarea,updatetime,forward,fitment,subfloor,floor,totalfloor,buildyear,price from sell where blockid in (".$blockStr.") order by updatetime desc  limit 0,1 luc";
		      $selldetail = $this->server_sell->query($sql);
		  }	
          if(!empty($selldetail))
          {
			  $housedetail = $selldetail['Record'][0];
			  $housedetail['time'] = time_tran($housedetail['UPDATETIME']);
			  $streetname = $this->block_model->getstreetname($housedetail['STREETID']);
			  $housedetail['streetname'] = $streetname['streetname'];
          }else{
            $housedetail=array();
          }
		  $this->memcached_class->add($key, $housedetail,60);
		}

		return $housedetail;
	}



	
	/**
	 * 获取关注学区信息
	 * @param int $kind 关注类别
	 * @param int $uid 用户id
	 * @param int $offset
	 * @param int $pagesize
	 * @return array
	 */
	public function getCollectSchoolList($kind,$uid,$offset,$pagesize)
	{	
		global $school_type,$school_pap,$school_rank;
		$collectSchool = $this->getcollect($kind,$uid,$offset,$pagesize);//修改user_model
        if(is_array($collectSchool)&&!empty($collectSchool))
		{
			foreach($collectSchool as $key=>$value){
				//$value['collect_id'];//学校id	
				$collectValue = unserialize($value['collect_value']);
				$collectSchool[$key]['feature'] = $this->getSchoolFeature($collectValue['features']);
				$blockArr = $this->getSchoolBlock($value['collect_id']);
				$collectSchool[$key]['blockNum'] = count($blockArr);
                $collectSchool[$key]['sellNum'] = $this->getSchoolHouseNum($value['collect_id'],$blockArr,'sell');
				$collectSchool[$key]['rentNum'] = $this->getSchoolHouseNum($value['collect_id'],$blockArr,'rent');
				$collectSchool[$key]['lastestHouse'] = $this->getSchoolLastestHouse($value['collect_id']);//最新房源
				$collectSchool[$key]['price'] = $this->block_model->getBlockPrice($value['collect_value'],$blockArr);//改调用block_model下的
				$collectSchool[$key]['schoolName'] = $collectValue['schoolname'];
				$collectSchool[$key]['address'] = $collectValue['address'];
				$collectSchool[$key]['streetname'] = $collectValue['streetname']['streetname'];
				$collectSchool[$key]['district'] = $collectValue['district'];
				$collectSchool[$key]['telno'] = $collectValue['telno'];
				$collectSchool[$key]['schoolType'] = isset($collectValue['school_type']) ? $school_type[$collectValue['school_type']]:"";
				$collectSchool[$key]['schoolPap'] = isset($collectValue['ispublic']) ? $school_pap[$collectValue['ispublic']]:"";//性质
				$collectSchool[$key]['schoolRank'] = isset($collectValue['rank']) ? $school_rank[$collectValue['rank']] : "";//级别
				$collectSchool[$key]['coverphoto'] = isset($collectValue['coverphoto'])?$collectValue['coverphoto']:"";

			}
		}

		return $collectSchool;
		
	}


	


   
   /**
	 * 获取房源数量
	 * @param int $school 学校id
	 * @param array $blockArr 小区数组
	 * @param string $type 二手房或租房 	
	 * @return array
	 */
   public function getSchoolHouseNum($school,$blockArr,$type='sell')
   {
	   $key = $this->memkey.'get_school_house_num_'.$type."_".$school;
	   if($this->input->get('update') == 1)
	   {
		  $this->memcached_class->delete($key);
	   }
	   $houseNum = $this->memcached_class->get($key);
       if(empty($houseNum))
	   {
		   if(is_array($blockArr)&&!empty($blockArr))
	       {
			  $blockStr = implode(",",$blockArr);
			  $blockStr = rtrim($blockStr,',');		  
			  $sql = "select id,district,streetid,blockshowname,room,hall,buildarea,updatetime,forward,fitment,subfloor,floor,totalfloor,buildyear from ".$type." where blockid in (".$blockStr.") order by updatetime desc  limit 0,1 luc";
			  if($type=='sell')
			  {
				  $detail = $this->server_sell->query($sql);
			  }elseif($type=='rent'){
			      $detail = $this->server_rent->query($sql);
			  }
			  
			  $houseNum = $detail['TOTAL'];
			  $this->memcached_class->add($key, $houseNum,3600);
	      }	

	   }
	   return $houseNum;	   

   }



   //处理学校特色
   public function getSchoolFeature($features)
   {
	   global $school_features;	
	   if(isset($features))
       {	     
		   $match_num = 0;
		   $school_feature = array();
		   //#2#,#9#
		   $match_num = preg_match_all('/[0-9]+/',$features,$school_feature);
		   $node['features'] = $match_num > 0 ? $school_feature[0] : array();		
		   foreach($school_feature[0] as $key=>$value)
		   {	if(isset($school_features[$value]))
			   {
			      $s_shchool_feature[] .= $school_features[$value];
		       }
				
		   }
	    }
		return $s_shchool_feature;
   }



   /**
	 * 通过全站个人会员中心uid查找二手房个人会员中心uid
	 * @param int $passportId 全站uid
	 * @return int $userId 二手房uid
	 */
	function getUidByPassportId($passportId){
		$key = $this->memkey.'getUidByPassportID'.$passportId;
		if($this->input->get('update') == 1)
		{
			$this->memcached_class->delete($key);
		}
        
		$userId = $this->memcached_class->get($key);
		
		if(!$userId){
			$userId = 0;
		    if((int)$passportId>0 ){
				$userIdArr =  $this->get_data(array('form_name' => 'personal_user','limit'=>1,'where'=>array('passport_uid'=>$passportId,'is_use'=>1),'select'=>array('id')),'esfbak');  
			    $userId = $userIdArr[0]['id'];
				$this->memcached_class->add($key, $userId,1000);
		    }
		
		}
		
		return $userId;		
		
	}
    
	/**
	 * 检测全站个人会员中心是否有此号码
	 * @param int $telno
	 * @return bool
	 */
	function ucenterCheckTelno($telno){
         $return = get_api(UCENTER_CHECK_PHONE."?phone=".$telno);
		// echo UCENTER_CHECK_PHONE."?phone=".$telno;
		 $newReturn = json_decode($return,true);		 
		 return $newReturn['result'] == 1 ? False : True; 
	}

    /**
	 * 全站个人会员中心发送验证码
	 * @param int $telno
	 * @return bool
	 */
	function ucenterAuthCode($telno){
		 $return = get_api(UCENTER_SEND_AUTH_CODE."?phone=".$telno."&auth_resource=6");
		// echo UCENTER_SEND_AUTH_CODE."?phone=".$telno."&auth_resource = 6";echo "<br>";
		// var_dump($return);exit();
		 $newReturn = json_decode($return,true);
		 return $newReturn['result'] == 1 ? True : False; 
	}

	 /**
	 * 全站个人会员中心注册
	 * @param int $telno
	 * @param string $password
	 * @param string $city
	 * @param string $authcode
	 * @return bool
	 */
     function ucenterReister($telno,$password,$city,$authcode){
		$return = get_api(UCENTER_REISTER."?phone=".$telno."&password=".$password."&city=".$city."&authcode=".$authcode."&registerfrom=12");
		//echo UCENTER_REISTER."?phone=".$telno."&password=".$password."&city=".$city;exit();
		$newReturn = json_decode($return,true);
        return $newReturn;
     }

	/**
	 * 全站个人会员验证修改密码
	 * @param int $telno
	 * @param string $password
	 * @param string $city
	 * @param string $authcode
	 * @return bool
	 */
	 function ucenterForgetpwd($phone,$password,$authcode){
		$return = get_api(UCENTER_FORGET_PWD."?phone=".$phone."&password=".$password."&authcode=".$authcode);
		//echo UCENTER_FORGET_PWD."?phone=".$phone."&password=".$password."&authcode=".$authcode;exit();
		$newReturn = json_decode($return,true);
        return $newReturn;
		
	 }





	  /**
	 * 验证动态密码登陆
	 * @param type $telno 手机号码 $validcode 验证码
	 * @return string
	 */
	function yzlogindtnew($telno,$validcode)
	{
		$vtime = time();
		//验证短信
		$sms_id = $this->get_data(array('form_name' => 'personal_sms','limit'=>1,'where'=>array('telno'=>$telno,'validcode'=>$validcode,'kind'=>1,'dateline  >'=>($vtime-120*30*30)),'select'=>array('id')),'esfbak');

		if(!empty($sms_id[0]['id']))
		{
			//干掉是否是二手房用户（前面已经验证过）
			//$user_id =  $this->get_data(array('form_name' => 'personal_user','limit'=>1,'where'=>array('telno'=>$telno,'is_use'=>1),'select'=>array('id','password')),'esfbak');

			if(!empty($user_id[0]['id']))
			{
				$this->updatesms($telno,$validcode);
				return $user_id[0];
			}
		}else{
			return 0;
		}
	}

	/**
	 * 个人会员中心登录
	 * @param type $telno 手机号码 $password 密码
	 * @return string
	 */

	 function ucenterLogin($telno,$password){
		 $return = get_api(UCENTER_LOGIN."?phone=".$telno."&password=".$password);
		 //echo UCENTER_LOGIN."?phone=".$telno."&password=".$password;exit;
		 $newReturn = json_decode($return,true);
         return $newReturn;		 
	 }


    /**
	 * 个人会员中心导入二手房用户数据
	 * @param type $telno 手机号码 $validcode 验证码
	 * @return string
	 */
	 function addUserFromUcenter($passport_uid,$password,$telno,$city,$email="",$username="",$nickname="",$avatar="",$ip=""){
	   if(empty($passport_uid) || empty($telno) || empty($password)||empty($city))
	   {
	     return 0;exit;
	   }

		$user = array(
		'passport_uid'=>$passport_uid,
		'username'=>$username,
		'nickname'=>$nickname,
		'telno' => $telno,
		'password' => $password,
        'infofrom' => $infofrom,
		'registertime' => time(),
		'lastlogintime' => time(),
		'email'=>$email,
		'head_pic'=>$avatar,
        'cityname' => $city,
		'infofrom'=>5,
		'is_use'=>1
		);
		//var_dump($arr);exit();
        //设置表
		$this->user_model->set_table('personal_user');
		//过滤表结构
		$result = $this->user_model->add($user,'esf');

		return $result;
	 }





	/**
	* 只修改二手房登录密码
	* @param type $telno 手机号码 $password 新密码
	* @return string
	*/
	function updateEsfPassowrd($telno,$password)
	{
	   if(empty($telno)|| empty($password))
	   {
	     return 0;exit;
	   }
		$arr = array('telno'=>$telno);
		$up	= array('password'=>md5($password),'password1'=>'');

		$this->set_table('personal_user');
        if(!empty($telno))
		{
			$result = $this->save($arr,$up,'esf');
			
			return $result;
	    }
	}


	/**
	* 查出二手房登录md5密码
	* @param type $telno 手机号码 $password 新密码
	* @return string
	*/
    function getEsfPassword($telno){
		$user_password =  $this->get_data(array('form_name' => 'personal_user','limit'=>1,'where'=>array('id'=>$telno),'select'=>array('password')),'esfbak');

		if( !empty($user_password[0]['password']) )
		{
			return $user_password[0]['password'];
		}else{
			return 0;
		}
		
    }


   /**
	* 更新全站用户中心用户密码
	* @param type $telno 手机号码 $password 新密码
	* @return string
	*/
	function ucenterUpdatePassword($telno,$password){
		$return = get_api(UCENTER_CHECK_LOGIN."?phone=".$telno."&password=".$password);
		$newReturn = json_decode($return,true);
        return $newReturn;		
		
		
	}




     /**
	 * $id为会员id
	 * 获取会员信息
	 *
	 */
    function get_user_data($id = 0)
	{
		$result = array();

		if(empty($id))
		{
			return $result;
		}

		$data =  $this->get_data(array('form_name' => 'personal_user','where'=>array('id'=>$id,'is_use'=>1),'select'=>array('*')),'esfbak');

		if(!empty($data[0]))
		{
			$result = $data[0];
		}


		return $result;

	}

     /**
	 * $telno为会员手机
	 * $start_date活动开始时间
	 * $end_date活动结束时间
	 * 验证是否可以领取小礼品
	 * 
	 */
	function check_is_win($user_id = 0,$telno = 0,$start_date = 0,$end_date = 0)
	{
		$result = false;

		if(empty($telno) || empty($start_date) || empty($end_date))
		{
			return $result;	
		}

	    //$data =  $this->get_data(array('form_name' => 'hd_ffy','limit'=>1,'where'=>array('userid' => $user_id,'createtime >='=>$start_date,'createtime <='=>$end_date,'is_active' => '1','disabled' => '0'),'select'=>array('id','createtime')),'esfhousebak');

		$data = $this->get_data(array('form_name' => 'hd_ffy','limit'=>1,'where'=>array('userid' => $user_id,'is_prize_messagebox'=>1),'select'=>array('id','createtime','is_active','disabled')),'esfhousebak');

		if(!empty($data) && $data[0]['is_active'] == '1' && $data[0]['disabled'] == '0' && (time()-$data[0]['createtime']) < 604800)
		{
			$result = true;
		}

		return array('data' => $data,'status' => $result);
	}

	 /**
	 * $telno为会员手机
	 * $start_date活动开始时间
	 * $end_date活动结束时间
	 * 验证是否可以领取小礼品
	 * 
	 */
	function check_is_wins($user_id = 0,$telno = 0,$start_date = 0,$end_date = 0)
	{
		$result = false;

		if(empty($telno) || empty($start_date) || empty($end_date))
		{
			return $result;	
		}

	    //$data =  $this->get_data(array('form_name' => 'hd_ffy','limit'=>1,'where'=>array('userid' => $user_id,'createtime >='=>$start_date,'createtime <='=>$end_date,'is_active' => '1','disabled' => '0'),'select'=>array('id','createtime')),'esfhousebak');

		$data = $this->get_data(array('form_name' => 'hd_ffy','limit'=>1,'where'=>array('userid' => $user_id,'is_prize_messagebox'=>1),'select'=>array('id','createtime','is_active','disabled')),'esfhousebak');

		if(!empty($data) && $data[0]['is_active'] == '1' && $data[0]['disabled'] == '0' && (time()-$data[0]['createtime']) < 604800)
		{
			$result = true;
		}

		return array('data' => $data,'status' => $result);
	}


	 /**
	 * $telno为会员手机
	 * 获取每天用户验证码的数量
	 * 
	 */
	function getNumPersonalUserStatistic($telno)
	{
		$start_date =strtotime(date("Y-m-d 00:00:00"));
		$end_date =strtotime(date("Y-m-d 23:59:59"));
		
		
		$data =  $this->get_data(array('form_name' => 'personal_user_statistic','where'=>array('telno'=>$telno,'ctime >='=>$start_date,'ctime <='=>$end_date),'select'=>array('*')),'esfbak');
		if(is_array($data) &&  empty($data))
		{
			$num=0;
		}
		else
		{
			$num =count($data);
		}
		return $num;
	}

	/**
	 * $telno为会员手机
	 * 获取一分钟之内验证码的数量
	 * 
	 */
	function getMinuteNumPersonalUserStatistic($telno)
	{
		$start_date =time()-60;
		
		$data =  $this->get_data(array('form_name' => 'personal_user_statistic','where'=>array('telno'=>$telno,'ctime >'=>$start_date),'select'=>array('*')),'esfbak');
		
		if(is_array($data) &&  empty($data))
		{
			$num=0;
		}
		else
		{
			$num =1;
		}
		return $num;
	}


	 /**
	 * $telno为会员手机
	 * 添加记录
	 * 
	 */
	function addRecordPersonalUserStatistic($telno)
	{
		$ctime  =time();
		
		$arr	= array('telno'=>$telno,'ctime'=>$ctime);
		//设置表
		$this->user_model->set_table('personal_user_statistic');
		//过滤表结构
		$result = $this->user_model->add($arr,'esf');
		
		return $result;
		
		
	}


	
	 /**
	 * 2015-11-10 二手房详情页面金融短信验证
	 *
	 * 
	 */

	 function yzjrsmstime( $sec , $telno , $type = 1 )
	 {

		
		$vtime = time();
		$limit_time = $vtime - $sec;
		
 		$data =  $this->get_data(array('form_name' => 'jr_sms','limit'=>1,'where'=>array('telno'=>$telno,'ctime  >='=>$limit_time,'type'=>$type,'status'=>0),'select'=>array('telno')),'esf');
		
		if(!empty($data[0]['telno']))
		{
			return 1;
		}else{
			
			return 0;
		}
	 }

	/**
	 * 2015-11-10 金融短信验证
	 * 未验证通过的，发送之前验证码
	 * 
	 */

	 function check_jr_yzm_today_orderby_id($telno,$type)
	 {
		
		$time_start =strtotime(date('Y-m-d 00:00:00'));
		
		$time_end =strtotime(date('Y-m-d 23:59:59'));

		$data =  $this->get_data(array('form_name' => 'jr_sms','limit'=>1,'where'=>array('telno'=>$telno,'ctime  >'=>($time_start),'ctime  <'=>($time_end),'type'=>$type),'select'=>array('validcode','status','ctime'),'order_by'=>'id desc'),'esfbak');
		
		if($data[0]['status']=='0')
		{
			
			return $data[0]['validcode'];
		}else{
			return 0;
		}
	 }

	/**
	 * 2015-11-10 验证金融短信验证
	 * 
	 * 
	 */
	function check_jr_yzm_today_by_validcode($telno,$validcode)
	{
		$time_start =strtotime(date('Y-m-d 00:00:00'));
		
		$time_end =strtotime(date('Y-m-d 23:59:59'));

		$data =  $this->get_data(array('form_name' => 'jr_sms','limit'=>1,'where'=>array('telno'=>$telno,'ctime  >'=>($time_start),'ctime  <'=>($time_end),'validcode'=>$validcode),'select'=>array('validcode','status','ctime'),'order_by'=>'id desc'),'esfbak');
		if(!empty($data))
		{
			return $data[0]['validcode'];
		}
		else
		{
			return 0;
		}
		
	}

	/**
	 * 金融短信使用后，对短信状态进行更新
	 * @param type $telno 手机号码 $validcode 验证码
	 * @return string
	 */
	function jrupdatesms($telno,$validcode)
	{
		
	   if(empty($telno) || empty($validcode))
	   {
	     return 0;exit;
	   }
	   $sql = "UPDATE jr_sms set status=1 WHERE telno = '".$telno."' and validcode='$validcode'";
	   //echo $sql;die;
	   $result = $this->db_esf->query($sql);

	   
	}


	//查看是否选择提醒
	function isCheckPriceRemind($uid,$houseid){
		//var_dump($uid);
		//var_dump($houseid);exit();
        $data =  $this->get_data(array('form_name' => 'price_remind','where'=>array('uid'=>$uid,'houseid '=>$houseid),'select'=>array('houseid,status')),'esfhousebak');
		return $data;

		
	}

	//根据房源编号查看来源
	function get_infofrom_by_id($collect_id){
		//var_dump($uid);
		//var_dump($houseid);exit();
        $data =  $this->get_data(array('form_name' => 'sell','where'=>array('id'=>$collect_id),'select'=>array('infofrom')),'esfhousebak');
		
		return $data;

		
	}

	//查看价格变动用户选择数量
	function getPriceRemindNum($uid){
		
		$data =  $this->get_data(array('form_name' => 'price_remind','where'=>array('uid'=>$uid,'status'=>'1'),'select'=>array('count(*) as num')),'esfhousebak');
		if(!empty($data[0]))
		{
			return $data[0]['num'];
		}else{
			return 0;
		}
		
	}

	

	//添加价格提醒
    function addPriceRemind($houseid,$uid){
		$addtime  =time();		
		$arr = array('type'=>'sell','houseid'=>$houseid,'uid'=>$uid,'addtime'=>$addtime,'status'=>1);
		
		//设置表
		$this->user_model->set_table('price_remind');
		//过滤表结构
		$result = $this->user_model->add($arr,'esfhouse');
		
		return $result;
		
    }

	

	//修改价格提醒
	function updatePrcieRemind($houseid,$uid,$status){
		$result = 0;
		if( $houseid > 0&&$uid>0)
        {
         $sql = "UPDATE price_remind set status=$status WHERE   uid=$uid and `houseid` = $houseid ";
		// echo $sql;exit();
		 $result = $this->db_esfhouse->query($sql);
        }

        return $result;
		
	}
	 




	

 }