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

class Esf_center_api_wang extends MY_Controller {

    function __construct()
    {
	  parent::__construct();

	  $this->load->model("user_model");
	  $this->load->model("ask_model");
	  $this->load->model("house_model");
	  $this->load->library("user_class");
	  $this->load->helper("template_helper");
	  $this->load->library("apply_class");
	  $this->load->model("user_statistic_model");
	  $this->load->model("push_model");
	  $this->city = '';
	  $this->user_id = 0;
    }

	
	/**
	* 验证验证码接口
	* @author cxf
	*/
	public function api_yzcode()
	{
		$validcode = intval($this->input->get('validcode'));
		$telno = intval($this->input->get('telno'));
		$kind = intval($this->input->get('kind'));

		//验证验证码
		$valid_isuse = $this->user_model->yzusersms($validcode,$telno,$kind);

		if($valid_isuse)
		{	
			//验证成功
			//$this->user_model->updatesms($validcode,$telno);
			echo 1;
		}
		else
		{
			echo 0;
		}
		
		exit;
	}


    /**
    * 关注房源操作接口
    * @author cxf
    */
    public function api_savehouse()
    {
         $uid = $this->input->get('uid');
         $kind = $this->input->get('kind');
         $rowid = $this->input->get('rowid');
         $tbl = $kind == 3 ? 'sell' : 'rent';
         $city = $this->input->get('city');
         $city = $city ? $city : 'nj';
         $result = $this->user_model->getusercollectid($uid,$rowid,$city);

         //已关注此房源
         if( $result > 0 )
         {
            echo "2";
            exit;
         }

         if($tbl == 'sell')
         {
            $houseinfo =  $this->house_model->get_data(array('form_name' => $tbl,'where'=>array('id'=>$rowid,'esta'=>'1'),'select'=>array('infofrom','id','district','address','streetid','blockshowname','room','hall','buildarea','forward','fitment','buildyear','floor','totalfloor','subfloor','price','uid','telno','pic1','contactor','uid'),'limit'=>1),'esfhousebak');
         }
         else
         {
            $houseinfo =  $this->house_model->get_data(array('form_name' => $tbl,'where'=>array('id'=>$rowid,'esta'=>'1'),'select'=>array('infofrom','id','district','address','streetid','blockshowname','room','hall','buildarea','forward','fitment','buildyear','floor','totalfloor','subfloor','price','uid','telno','pic1','contactor','uid','priceunit','renttype'),'limit'=>1),'esfhousebak');
         }

         //判断经纪人房源多查一次经纪人姓名
         if( $houseinfo[0]['infofrom'] == '5' && $houseinfo[0]['uid'] > 0 && $houseinfo[0]['contactor'] == '')
         {
            $broker_data = $this->house_model->get_data(array('form_name'=>'broker','where'=>array('uid'=>$houseinfo[0]['uid']),'select'=>array('uid','truename'),'limit'=>1),'esfhousebak');

            //查询经纪人姓名
            $houseinfo[0]['contactor'] = $broker_data[0]['truename'];
         }

         $searchcond = serialize($houseinfo[0]);
         $this->user_model->addcollect( $uid , $rowid , $kind , $searchcond,$city,1);

         echo "1";
         exit;
    }





    /**
     * 关注房源操作接口，移动专用
     * @author cxf
    */
    public function api_savehousemobile()
    {
     $return ='';
     $uid = $this->input->get('uid');
     if(empty($uid))
      {
        $telno = $this->input->get('telno');
        $uid = $this->user_model->yztelno($telno);
      }
      $flag = $this->input->get('flag');
     if(empty($flag))
      {
        $flag = 2;
      }
     if($uid==0)
     {
     $return = 'N_USER';
     }
     $kind = $this->input->get('kind');
     $rowid = $this->input->get('rowid');
     $city = $this->input->get('city');
     $city = $city?$city:'nj';
     $tbl=$kind==3?'sell':'rent';
     $result = $this->user_model->getusercollectid($uid,$rowid,$city);
     //已关注此房源
     if($result['id']>0)
     {
      $return = 'N_COL';
     }
     if($tbl == 'sell'){
     $houseinfo =  $this->house_model->get_data(array('form_name' => $tbl,'where'=>array('id'=>$rowid,'esta'=>'1'),'select'=>array('infofrom','id','district','address','streetid','blockshowname','room','hall','buildarea','forward','fitment','buildyear','floor','totalfloor','subfloor','price','uid','telno','pic1','contactor','uid'),'limit'=>1),'esfhousebak');
     }else{
      $houseinfo =  $this->house_model->get_data(array('form_name' => $tbl,'where'=>array('id'=>$rowid,'esta'=>'1'),'select'=>array('infofrom','id','district','address','streetid','blockshowname','room','hall','buildarea','forward','fitment','buildyear','floor','totalfloor','subfloor','price','uid','telno','pic1','contactor','uid','priceunit','renttype'),'limit'=>1),'esfhousebak');
     }
        if(empty($houseinfo[0]) && $city=='nj')
        {
         $return = 'N_HOU';
        }elseif($return != 'N_COL'){
            if(!empty($houseinfo[0]))
            {
             $searchcond = serialize($houseinfo[0]);
            }else{
            $searchcond = '';
            }
          $return = $this->user_model->addcollect($uid,$rowid,$kind,$searchcond,$city,$flag);
        }


     if($return == 'N_COL'){
          $result = 2;
          $msg = '已收藏此房源';
     }elseif($return == 'N_HOU'){
          $result = 3;
          $msg = '无此房源';
     }elseif($return == 'N_USER'){
          $result = 4;
          $msg = '无此用户';
     }elseif($return > 0){
          $result = 1;
          $msg = '收藏成功';
		   //验证不在白名单，不在推送库，不在推送历史库
			$result2 = $this->push_model->yzpush($telno);
			if($result2<=0  && $city=='nj'){
				//加入备选库
				//$this->push_model->addalternatives($uid,$telno);
			}
     }elseif($return == 0){
          $result = 0;
          $msg = '收藏失败';
     }

     $return_array = array('result'=>$result,'msg'=>$msg);
     echo serialize($return_array);
    }



	 /**
     * 关注房源操作接口，触屏专用
     * @author cxf
    */
    public function api_savehousetouch()
    {
      $return ='';
      $passport_uid = $this->input->get('passport_uid');
     
       //根据passport_uid获取uid
      $uid = $this->user_model->getUidByPassportId($passport_uid);
      
      $flag = $this->input->get('flag');
     if(empty($flag))
      {
        $flag = 2;
      }
     if($uid==0)
     {
     $return = 'N_USER';
     }
     $kind = $this->input->get('kind');
     $rowid = $this->input->get('rowid');
     $city = $this->input->get('city');
     $city = $city?$city:'nj';
     $tbl=$kind==3?'sell':'rent';
     $result = $this->user_model->getusercollectid($uid,$rowid,$city);
     //已关注此房源
     if($result['id']>0)
     {
      $return = 'N_COL';
     }
     if($tbl == 'sell'){
     $houseinfo =  $this->house_model->get_data(array('form_name' => $tbl,'where'=>array('id'=>$rowid,'esta'=>'1'),'select'=>array('infofrom','id','district','address','streetid','blockshowname','room','hall','buildarea','forward','fitment','buildyear','floor','totalfloor','subfloor','price','uid','telno','pic1','contactor','uid'),'limit'=>1),'esfhousebak');
     }else{
      $houseinfo =  $this->house_model->get_data(array('form_name' => $tbl,'where'=>array('id'=>$rowid,'esta'=>'1'),'select'=>array('infofrom','id','district','address','streetid','blockshowname','room','hall','buildarea','forward','fitment','buildyear','floor','totalfloor','subfloor','price','uid','telno','pic1','contactor','uid','priceunit','renttype'),'limit'=>1),'esfhousebak');
     }
        if(empty($houseinfo[0]) && $city=='nj')
        {
         $return = 'N_HOU';
        }elseif($return != 'N_COL'){
            if(!empty($houseinfo[0]))
            {
             $searchcond = serialize($houseinfo[0]);
            }else{
            $searchcond = '';
            }
          $return = $this->user_model->addcollect($uid,$rowid,$kind,$searchcond,$city,$flag,$passport_uid);
        }


     if($return == 'N_COL'){
          $result = 2;
          $msg = '已收藏此房源';
     }elseif($return == 'N_HOU'){
          $result = 3;
          $msg = '无此房源';
     }elseif($return == 'N_USER'){
          $result = 4;
          $msg = '无此用户';
     }elseif($return > 0){
          $result = 1;
          $msg = '收藏成功';
		   //验证不在白名单，不在推送库，不在推送历史库
			$result2 = $this->push_model->yzpush($telno);
			if($result2<=0  && $city=='nj'){
				//加入备选库
				//$this->push_model->addalternatives($uid,$telno);
			}
     }elseif($return == 0){
          $result = 0;
          $msg = '收藏失败';
     }

     $return_array = array('result'=>$result,'msg'=>$msg);
     echo serialize($return_array);
    }




	/**
     * 关注房源操作接口，加盟站专用
     * @author cxf
    */
    public function api_savehouse_jmz()
    {


		
     $return ='';
     $uid = $this->input->get('uid');
     if(empty($uid))
      {
        $telno = $this->input->get('telno');
        $uid = $this->user_model->yztelno($telno);
      }
      $flag = $this->input->get('flag');
     if(empty($flag))
      {
        $flag = 2;
      }
     if($uid==0)
     {
     $return = 'N_USER';
     }
     $kind = $this->input->get('kind');
     $rowid = $this->input->get('rowid');
     $city = $this->input->get('city');
     $city = $city?$city:'nj';
     $tbl=$kind==3?'sell':'rent';
     $result = $this->user_model->getusercollectid($uid,$rowid,$city);
     //已关注此房源
     if($result['id']>0)
     {
      $return = 'N_COL';
     }
     if($tbl == 'sell'){
     $houseinfo =  $this->house_model->get_data(array('form_name' => $tbl,'where'=>array('id'=>$rowid,'esta'=>'1'),'select'=>array('infofrom','id','district','address','streetid','blockshowname','room','hall','buildarea','forward','fitment','buildyear','floor','totalfloor','subfloor','price','uid','telno','pic1','contactor','uid'),'limit'=>1),'esfhousebak');
     }else{
      $houseinfo =  $this->house_model->get_data(array('form_name' => $tbl,'where'=>array('id'=>$rowid,'esta'=>'1'),'select'=>array('infofrom','id','district','address','streetid','blockshowname','room','hall','buildarea','forward','fitment','buildyear','floor','totalfloor','subfloor','price','uid','telno','pic1','contactor','uid','priceunit','renttype'),'limit'=>1),'esfhousebak');
     }
        if(empty($houseinfo[0]) && $city=='nj')
        {
         $return = 'N_HOU';
        }elseif($return != 'N_COL'){
            if(!empty($houseinfo[0]))
            {
             $searchcond = serialize($houseinfo[0]);
            }else{
            $searchcond = '';
            }

		  //获取淘房用户的用户中心ID passport_uid
          $passport_uid = getJiaUserID($uid);

          $return = $this->user_model->addcollect($uid,$rowid,$kind,$searchcond,$city,$flag,$passport_uid);
        }


     if($return == 'N_COL'){
          $result = 2;
          $msg = '已收藏此房源';
     }elseif($return == 'N_HOU'){
          $result = 3;
          $msg = '无此房源';
     }elseif($return == 'N_USER'){
          $result = 4;
          $msg = '无此用户';
     }elseif($return > 0){
          $result = 1;
          $msg = '收藏成功';
		   //验证不在白名单，不在推送库，不在推送历史库
			$result2 = $this->push_model->yzpush($telno);
			if($result2<=0  && $city=='nj'){
				//加入备选库
				//$this->push_model->addalternatives($uid,$telno);
			}
     }elseif($return == 0){
          $result = 0;
          $msg = '收藏失败';
     }

     $return_array = array('result'=>$result,'msg'=>$msg);
     echo serialize($return_array);
    }


	/**
     * 关注房源操作接口，加盟站专用
     * @author cxf
    */
    public function api_savehouse_jmz_new()
    {


		
     $return ='';
     $passport_uid = $this->input->get('uid');
    
      $flag = $this->input->get('flag');
     if(empty($flag))
      {
        $flag = 2;
      }
     if($uid==0)
     {
     $return = 'N_USER';
     }
     $kind = $this->input->get('kind');
     $rowid = $this->input->get('rowid');
     $city = $this->input->get('city');
     $city = $city?$city:'nj';
     $tbl=$kind==3?'sell':'rent';
	 $esfuid = $this->user_model->getUidByPassportId($passport_uid);
     $result = $this->user_model->getusercollectid($esfuid,$rowid,$city);
     //已关注此房源
     if($result['id']>0)
     {
      $return = 'N_COL';
     }
     if($tbl == 'sell'){
     $houseinfo =  $this->house_model->get_data(array('form_name' => $tbl,'where'=>array('id'=>$rowid,'esta'=>'1'),'select'=>array('infofrom','id','district','address','streetid','blockshowname','room','hall','buildarea','forward','fitment','buildyear','floor','totalfloor','subfloor','price','uid','telno','pic1','contactor','uid'),'limit'=>1),'esfhousebak');
     }else{
      $houseinfo =  $this->house_model->get_data(array('form_name' => $tbl,'where'=>array('id'=>$rowid,'esta'=>'1'),'select'=>array('infofrom','id','district','address','streetid','blockshowname','room','hall','buildarea','forward','fitment','buildyear','floor','totalfloor','subfloor','price','uid','telno','pic1','contactor','uid','priceunit','renttype'),'limit'=>1),'esfhousebak');
     }
        if(empty($houseinfo[0]) && $city=='nj')
        {
         $return = 'N_HOU';
        }elseif($return != 'N_COL'){
            if(!empty($houseinfo[0]))
            {
             $searchcond = serialize($houseinfo[0]);
            }else{
            $searchcond = '';
            }

		 

          $return = $this->user_model->addcollect($esfuid,$rowid,$kind,$searchcond,$city,$flag,$passport_uid);
        }


     if($return == 'N_COL'){
          $result = 2;
          $msg = '已收藏此房源';
     }elseif($return == 'N_HOU'){
          $result = 3;
          $msg = '无此房源';
     }elseif($return == 'N_USER'){
          $result = 4;
          $msg = '无此用户';
     }elseif($return > 0){
          $result = 1;
          $msg = '收藏成功';
		   //验证不在白名单，不在推送库，不在推送历史库
			$result2 = $this->push_model->yzpush($telno);
			if($result2<=0  && $city=='nj'){
				//加入备选库
				//$this->push_model->addalternatives($uid,$telno);
			}
     }elseif($return == 0){
          $result = 0;
          $msg = '收藏失败';
     }

     $return_array = array('result'=>$result,'msg'=>$msg);
     echo serialize($return_array);
    }





     /**
     * 查询关注房源操作接口
     * @author cxf
    */
    public function api_yzsavehouse()
    {
     $return ='';
     $telno = $this->input->get('telno');
     $uid = $this->user_model->yztelno($telno);
     if($uid==0)
     {
     $return = 'N_USER';
     }
     $kind = $this->input->get('kind');
     $rowid = $this->input->get('rowid');
     $city = $this->input->get('city');
     $city = $city?$city:'nj';

	 if($uid){
		 $result = $this->user_model->getusercollectid($uid,$rowid);
	 }else{
		$result['id']=0;
	 }

     echo $result['id']>0?'1':'0';
    }


	   /**
     * 查询关注房源操作接口
     * @author cxf
    */
    public function api_yzsavehouse_uid()
    {
     $return ='';

     $passport_uid = $this->input->get('passport_uid');     
     //根据passport_uid获取uid
     $uid = $this->user_model->getUidByPassportId($passport_uid);
     if($uid==0)
     {
     $return = 'N_USER';
     }
     $kind = $this->input->get('kind');
     $rowid = $this->input->get('rowid');
     $city = $this->input->get('city');
     $city = $city?$city:'nj';

	 if($uid){
		 $result = $this->user_model->getusercollectid($uid,$rowid);
	 }else{
		$result['id']=0;
	 }

     echo $result['id']>0?'1':'0';
    }

	/**
     * 查询我的已经关注房源uid接口
     * @author bsw
    */
    public function getCollectHouseId(){
		 $return ='';
         $uid = $this->input->get('uid');
         if(empty($uid))
         {
          $telno = $this->input->get('telno');
          $uid = $this->user_model->yztelno($telno);
         }
		 if($uid==0){
			return json_encode(array());;
		 }
		 $kind = $this->input->get('kind');
		 $city = $this->input->get('city');
		 $city = $city?$city:'nj';
		 $result = $this->user_model->getCollectHouseId($uid,$kind,$city);
		 echo json_encode($result);
    }
	

	/**
	* 查询关注房源操作接口
	* @author cxf
	*/
	public function api_user_login()
	{
		$uid = $this->input->get('uid');
		$code = $this->input->get('code');
		$md5_code = md5(md5('myhouse365'.$uid.date("Y-m-d",time()).$_COOKIE['interface_msg']));

		if( $code == $md5_code )
		{
			$user_array = $this->user_model->getuserdetail($uid);

			if(is_array($user_array) && !empty($user_array) )
			{
				$this->user_class->user_login($uid,$user_array['password'],$user_array['telno'],$user_array['username']);
				jumpto(USER_CENTER.'user');
			}
			else
			{
				echo '用户不存在或已失效';
			}
		}
		else
		{
			echo '来源不正确或请再试一次!';
		}
	}

     /**
     * 取消关注房源操作接口
     * @author cxf
    */
    public function api_delsavehouse()
    {
     $telno = $this->input->get('telno');
     $uid = $this->user_model->yztelno($telno);
     if($uid==0)
     {
     $return = 'N_USER';
     }
     $kind = $this->input->get('kind');
     $rowid = $this->input->get('rowid');
     $city = $this->input->get('city');
     $tbl=$kind==3?'sell':'rent';
     $id = $this->user_model->getcollectdetail($kind,$uid,$rowid,$city);

     if($id==0)
     {
     $return = 'N_COL';
     }else{
     $return = $this->user_model->delcollect($id);

     }
         if($return == 'N_COL'){
          $result = 2;
          $msg = '无此收藏';
        }elseif($return == 'N_USER'){
          $result = 3;
          $msg = '无此用户';
        }elseif($return > 0){
          $result = 1;
          $msg = '取消收藏';
        }elseif($return == 0){
          $result = 0;
          $msg = '取消失败';
        }
        $return_array = array('result'=>$result,'msg'=>$msg);
        echo serialize($return_array);
    }




   /**
     * 根据用户中心id取消关注房源操作接口
     * @author cxf
    */
    public function api_delsavehouse_uid()
    {
     $passport_uid = $this->input->get('passport_uid');

     
     //根据passport_uid获取uid
     $uid = $this->user_model->getUidByPassportId($passport_uid);
   
     if($uid==0)
     {
     $return = 'N_USER';
     }
     $kind = $this->input->get('kind');
     $rowid = $this->input->get('rowid');
     $city = $this->input->get('city');
     $tbl=$kind==3?'sell':'rent';
     $id = $this->user_model->getcollectdetail($kind,$uid,$rowid,$city);

     if($id==0)
     {
     $return = 'N_COL';
     }else{
     $return = $this->user_model->delcollect($id);

     }
         if($return == 'N_COL'){
          $result = 2;
          $msg = '无此收藏';
        }elseif($return == 'N_USER'){
          $result = 3;
          $msg = '无此用户';
        }elseif($return > 0){
          $result = 1;
          $msg = '取消收藏';
        }elseif($return == 0){
          $result = 0;
          $msg = '取消失败';
        }
        $return_array = array('result'=>$result,'msg'=>$msg);
        echo serialize($return_array);
    }



	/**
	* 获取用户关注房源操作接口
	* @author cxf
	*/
	public function api_getsavehouse()
	{
		$telno = $this->input->get('telno');
		$uid = $this->user_model->yztelno($telno);

		if($uid==0)
		{
			echo serialize(array());exit;
		}

		$kind = $this->input->get('kind');
		$page = $this->input->get('page');
		$pagesize = $this->input->get('pagesize');
		$city = $this->input->get('city');
		$page = $page ? $page:1;
		$pagesize = $pagesize?$pagesize : 20;
		$offset = $pagesize * ($page-1);

		$collect_array = $this->user_model->getcollect($kind,$uid,$offset,$pagesize,$city);
		if(!empty($collect_array) && is_array($collect_array))
		{
			foreach($collect_array as $key=>$value)
			{
				$arr[$key]['id'] = $value['collect_id'];
			}
		}
		else
		{
			$arr=array();
		}

		echo serialize($arr);
	}
	

	/**
	* 根据用户中心获取用户关注房源操作接口
	* @author cxf
	*/
	public function api_getsavehouse_uid()
	{
		$passport_uid = $this->input->get('passport_uid');     
	
        //根据passport_uid获取uid
        $uid = $this->user_model->getUidByPassportId($passport_uid);
       	//var_dump($uid);exit();
		if($uid==0)
		{
			echo serialize(array());exit;
		}

		$kind = $this->input->get('kind');
		$page = $this->input->get('page');
		$pagesize = $this->input->get('pagesize');
		$city = $this->input->get('city');
		$page = $page ? $page:1;
		$pagesize = $pagesize?$pagesize : 20;
		$offset = $pagesize * ($page-1);

		$collect_array = $this->user_model->getcollect($kind,$uid,$offset,$pagesize,$city);
		if(!empty($collect_array) && is_array($collect_array))
		{
			foreach($collect_array as $key=>$value)
			{
				$arr[$key]['id'] = $value['collect_id'];
			}
		}
		else
		{
			$arr=array();
		}

		echo serialize($arr);
	}


	/**
	* 获取用户关注房源操作接口
	* @author cxf
	*/
	public function api_getsavehouse_data()
	{
		
		$telno = $this->input->get('telno');
		$uid = $this->user_model->yztelno($telno);

		if($uid==0)
		{
			echo serialize(array());exit;
		}
		
		$kind = $this->input->get('kind');
		$page = $this->input->get('page');
		$pagesize = $this->input->get('pagesize');
		$city = $this->input->get('city');
		$page = $page ? $page:1;
		$pagesize = $pagesize?$pagesize : 20;
		$offset = $pagesize * ($page-1);

		$this->load->model("userTest_model");
		
		$collect_array = $this->userTest_model->getcollect($kind,$uid,$offset,$pagesize,$city);

		
		if(!empty($collect_array) && is_array($collect_array))
		{
			foreach($collect_array as $key=>$value)
			{
				$arr[$key]['value'] = $value['collect_value'];
			}
		}
		else
		{
			$arr=array();
		}
		
		echo serialize($arr);
	}
	

	/**
	* 获得用户id接口
	* @author cxf
	*/
	public function api_getuidbytelno()
	{
		$telno = $this->input->get('telno');
		$userinfo =  $this->user_model->get_data(array('form_name' => 'personal_user','where'=>array('telno'=>$telno,'is_use'=>'1'),'select'=>array('id'),'limit'=>1),'esfbak');
		echo isset($userinfo[0]['id']) ? $userinfo[0]['id'] : 0 ;
		exit;
	}

	/**
	* 获得用户id接口
	* @author cxf
	*/
	public function api_get_userinfo_by_telno()
	{
		$telno = $this->input->get('telno');
		$userinfo =  $this->user_model->get_data(array('form_name' => 'personal_user','where'=>array('telno'=>$telno,'is_use'=>'1'),'select'=>array('id','username','cityname','infofrom'),'limit'=>1),'esfbak');
		//print_r($userinfo[0]);
		echo isset($userinfo[0]) ? serialize($userinfo[0]) : serialize(array());
		exit;
	}
	

	/**
	* 关注小区操作接口
	* @author cxf
	*/
    public function api_saveblock()
    {
		$uid = $this->input->get('uid');
		$kind = $this->input->get('kind');
		$blockid = $this->input->get('id');
		$city = $this->input->get('city');
		$city = $city ? $city : 'nj';
		$result = $this->user_model->getusercollectid($uid,$blockid,$city);

		//已关注此房源
		if( $result > 0 )
		{
			echo "2";
			exit;
		}

		$blockinfo =  $this->user_model->get_data(array('form_name' => 'block','where'=>array('id'=>$blockid,'esta'=>'2'),'limit'=>1),'esfhousebak');
		$searchcond = serialize($blockinfo[0]);
		$this->user_model->addcollect($uid,$blockid,$kind,$searchcond,$city);

		echo "1";
		exit;
    }

	/**
	* 关注小区操作接口加盟站专用
	* @author xue
	*/
    public function api_saveblock_jmz()
    {
		$uid = $this->input->get('uid');
		$kind = $this->input->get('kind');
		$blockid = $this->input->get('id');
		$city = $this->input->get('city');
		$city = $city ? $city : 'nj';
		$result = $this->user_model->getusercollectid($uid,$blockid,$city);

		//已关注此房源
		if( $result > 0 )
		{
			echo "2";
			exit;
		}

		$blockinfo =  $this->user_model->get_data(array('form_name' => 'block','where'=>array('id'=>$blockid,'esta'=>'2'),'limit'=>1),'esfhousebak');
		$searchcond = serialize($blockinfo[0]);
		//获取淘房用户的用户中心ID passport_uid
        $passport_uid = getJiaUserID($uid);
		$this->user_model->addcollect($uid,$blockid,$kind,$searchcond,$city,1,$passport_uid);

		echo "1";
		exit;
    }


	/**
	* 关注小区操作接口淘房专用
	* @author xue
	*
	*/
    public function api_saveblock_tf()
    {
		$passport_uid = $this->input->get('uid');
		$kind = $this->input->get('kind');
		$blockid = $this->input->get('id');
		$city = $this->input->get('city');
		$city = $city ? $city : 'nj';
		$kind = $kind ? $kind : 2;
        //找出二手房id
		$esfUid = $this->user_model->getUidByPassportId($passport_uid);

        //查看是否保存
		$result = $this->user_model->getusercollectid($esfUid,$blockid,$city,$kind);

		//已关注此房源
		if( $result > 0 )
		{
			echo "2";
			exit;
		}
        //获取小区信息
		$blockinfo =  $this->user_model->get_data(array('form_name' => 'block','where'=>array('id'=>$blockid,'esta'=>'2'),'limit'=>1),'esfhousebak');

		
		if(is_array($blockinfo[0])&&!empty($blockinfo[0])){
			$searchcond = serialize($blockinfo[0]);
			//获取淘房用户的用户中心ID passport_uid
			//$passport_uid = getJiaUserID($uid);
			$this->user_model->addcollect($esfUid,$blockid,$kind,$searchcond,$city,1,$passport_uid);
			echo "1";
			exit;
		
		}else{
		  //无此小区
		  echo "3";
		  exit();
		}
		
    }



    /**
     * 关注小区操作接口(手机端提交)
     * @author bsw
    */
    public function apiAddCollectBlock()
	{
		 $city = $this->input->get('city');
		 $city = $city ? $city : 'nj';
		 $telno = $this->input->get('telno');
		 $uid = $this->user_model->yztelno($telno);
		 if(!$uid)
		 {
			echo 3;
			die;
		 }
		 $blockid = $this->input->get('id');
		 $result = $this->user_model->getusercollectid($uid,$blockid);

		 //已关注此房源
		 if($result>0)
		 {
			echo "2";
			exit;
		 }
		 $blockinfo =  $this->user_model->get_data(array('form_name' => 'block','where'=>array('id'=>$blockid,'esta'=>'2'),'limit'=>1),'esfhousebak');
		 $searchcond = serialize($blockinfo[0]);
		 $this->user_model->addcollect($uid,$blockid,2,$searchcond,$city,2);
		 //验证不在白名单，不在推送库，不在推送历史库
        $result = $this->push_model->yzpush($uid);

        if($result<=0)
		{
			//加入备选库
			//$result = $this->push_model->addalternatives($uid,$telno);
        }

		echo "1";
		exit;
    }


	/**
     * 关注学校操作接口
     * @author xue
    */
    public function api_save_school()
    { 
        $uid = $this->input->get('uid');
		$kind = $this->input->get('kind');
		$schoolid = $this->input->get('id');
		$city = $this->input->get('city');
		$city = $city ? $city : 'nj';
		$result = $this->user_model->getusercollectid($uid,$schoolid,$city,$kind);	
		//var_dump($result);exit;
		//已关注此房源
		if($result>0)
		{
			echo "2";
			exit;
		}
  
		$schoolinfo =  $this->user_model->get_data(array('form_name' => 'school','where'=>array('id'=>$schoolid),'select'=>array('id','schoolname','district','streetid','schooltype','blockid','features','address','schooltype','telno','ispublic','rank','coverphoto'),'limit'=>1),'esfhousebak');		
		$this->load->model("block_model");
		$schoolinfo[0]['streetname'] = $this->block_model->getstreetname($schoolinfo[0]['streetid']);		
		$searchcond = serialize($schoolinfo[0]);
		
		$this->user_model->addcollect($uid,$schoolid,$kind,$searchcond,$city);
		echo "1";exit;
    }



    /**
    * 获取验证码
    * @author cxf
    */
    public function api_getyzm()
    {
        //验证是否已经通过验证
        $validcode = random(6);
        $tt = array();
        $tt['validcode'] = $validcode;
        $tt['telno'] = $this->input->get('telno');
        $tt['kind'] = $this->input->get('kind');
        $tt['dateline'] = time();

		$is_send_validcode = $this->user_model->yzsms($tt['telno'],$tt['kind']);
		
		if($is_send_validcode!=0){
		   $tt['validcode'] = $is_send_validcode;
		}

		//if($_GET['test']==1){
		//   var_dump($tt);exit();
		//}
        //验证此号码55秒内是否已经发送过验证码,防止刷短信
        $result=$this->user_model->yzusersmstime(55,$tt['telno'],$tt['kind']);
		if($result>0)
		{
			echo '7';
			exit;
		}
        //设置表
        $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网友，您操作的验证码为'.$tt['validcode'].'，请在收到后两分钟内提交。';
           // sendsms($tt['telno'],$msg,'sx');
            sendsms($tt['telno'],$msg,'sx',$tt['validcode']);
            echo '1';
        }else{
            echo '2';//发送失败
        }

    }

    /**
    * 问答有回复提醒
    * @author cxf
    */
    public function api_ask_sms()
    {
       $telno = $this->input->get('telno');
       $msg   = '你提的问题有新答案，请登录365个人会员中心进行查看。';
       sendsms($telno,$msg,'tz');
    }


	/**
	* 发送站内信接口
	* @author cxf
	*/
	public function api_sendmessage()
	{
		$uid = $this->input->get('uid');
		$msg = $this->input->get('msg');
		$subject = $this->input->get('subject');
		$city = $this->input->get('city');
		$kind = $this->input->get('kind');
		$kind = !empty($kind)?$kind:'1';
		$msg = urldecode($msg);
		$subject = urldecode($subject);

		if(empty($city))
		{
			$city = GB_CITY;
		}

		$result = $this->apply_class->send_systemmsg($uid,$subject,$msg,$kind,$city);
		echo $result;
	}
	

    /**
    * 用户添加接口
    * @author cxf
    */
    public function api_adduser()
    {
		$telno = $this->input->get('telno');
		$paasword = $this->input->get('paasword');
		$city = $this->input->get('city');
		$infofrom = $this->input->get('infofrom');

		if (!preg_match('/^[_0-9a-z]{6,16}$/i',$paasword))
		{
			//密码格式不对
			$return = 'N_PASS';
		}
       

	    $url = "api.house365.com/esf/web/api_4001.php?&stc=".urlencode($telno);
					//ECHO $url;
		curl_get_contents($url);
		//验证此号码是否已经注册
		$result = $this->user_model->yztelno($telno);
		if( $result > 0 )
		{
			//已经注册
			$return = 'N_REG';
		}
		else if($return != 'N_PASS')
		{
			$result = $this->user_model->adduser($telno,$paasword,$city,$infofrom);
			$return = $result;
		}

		//密码格式不对
		if($return == 'N_PASS')
		{
			$result = 2;
			$msg = '密码格式不对';
		}
		else if($return == 'N_REG')
		{
			$result = 3;
			$msg = '号码已被注册';
		}
		else if($return > 0)
		{
			$result = 1;
			$msg = '注册成功';
		}
		else if($return == 0)
		{
			$result = 0;
			$msg = '注册失败';
		}

		$return_array = array('result'=>$result , 'msg'=>$msg);

		echo serialize($return_array);
    }




	/**
    * 用户添加接口APP专用
    * @author xue
    */
    public function api_adduser_mobile()
    {
		$telno = $this->input->get('telno');
		$paasword = $this->input->get('paasword');
		$city = $this->input->get('city');
		$infofrom = $this->input->get('infofrom');
		$passport_uid = $this->input->get('passport_uid');

		$is_md5 = $this->input->get('is_md5');
        $register_from = $this->input->get('register_from');

		if($register_from=="" or $register_from=0){
		   $register_from  = 4;
		}

	   $url = "api.house365.com/esf/web/api_4002.php?&stc=".urlencode($telno);
					//ECHO $url;
		curl_get_contents($url);

		//if (!preg_match('/^[_0-9a-z]{6,16}$/i',$paasword))
		//{
			//密码格式不对
		//	$return = 'N_PASS';
		//}

		//验证此号码是否已经注册
		$result = $this->user_model->yztelno($telno);
		if( $result > 0 )
		{   //修改用户中心ID
            $this->user_model->updateuser($result,array('passport_uid'=>$passport_uid));
			//已经注册
			$return = 'N_REG';
		}
		else if($return != 'N_PASS')
		{
			$result = $this->user_model->adduser($telno,$paasword,$city,$infofrom,$passport_uid,$is_md5,$register_from);
			$return = $result;
		}

		//密码格式不对
		if($return == 'N_PASS')
		{
			$result = 2;
			$msg = '密码格式不对';
		}
		else if($return == 'N_REG')
		{
			$result = 3;
			$msg = '号码已被注册';
		}
		else if($return > 0)
		{
			$result = 1;
			$msg = '注册成功';
		}
		else if($return == 0)
		{
			$result = 0;
			$msg = '注册失败';
		}

		$return_array = array('result'=>$result , 'msg'=>$msg);

		echo serialize($return_array);
    }

    /**
    * 用户验证接口
    * @author cxf
    */
    public function api_yztelno()
    {
		$telno = $this->input->get('telno');
		$result = $this->user_model->yztelno($telno);
		echo $result;
    }

    /**
    * 验证用户是否可以登陆接口
    * @author cxf
    */
    public function api_yzlogin()
    {
      $telno = $this->input->get('telno');
      $password = $this->input->get('password');
      $result = $this->user_model->yzlogintelno($telno,$password);

      echo $result;
    }

    /**
    * 验证用户是否可以登陆接口移动专用
    * @author cxf
    */
    public function api_yzloginmobile()
    {
      $telno = $this->input->get('telno');
      $password = $this->input->get('password');
      $return = $this->user_model->yzlogintelno($telno,md5($password));

      if($return > 0){
          $result = 1;
          $msg = '登陆成功';
        }elseif($return == 0){
          $result = 0;
          $msg = '登陆失败';
        }
      $return_array = array('result'=>$result,'msg'=>$msg);
      echo serialize($return_array);
    }

	 /**
    * 提交登陆信息
    * @author cxf
    */
    public function api_sub_login_log_mobile()
    {
		$device_array = $this->user_statistic_model->get_device();

		$telno = $this->input->get('telno');
		$uid = $this->user_model->yztelno($telno);
		$city = $this->input->get('city');
		$device = $this->input->get('device');
		//初始化用户信息
		$this->user_statistic_model->set_uid($uid);
		$this->user_statistic_model->set_city($city);
		//提交登录信息
		$result = $this->user_statistic_model->sub_login_log('mobile',$device_array[$device]);
		echo serialize($result);
	}

        /**
    * 提交登陆操作接口
    *
    * @access public
    * @param  无
    * @return void
    */
    function api_sub_login_log()
    {
		$this->load->model('user_statistic_model');
		$login_from = $this->input->get('login_from');
		$device = $this->input->get('device');
		$uid = $this->input->get('uid');
		$code = $this->input->get('code');
		$city = $this->input->get('city');
		if( $code != md5('my365house'.$uid) )
		{
			exit;
		}

 		$this->user_statistic_model->set_city($city);
		$this->user_statistic_model->set_uid($uid);
		$result = $this->user_statistic_model->sub_login_log($login_from,$device);
        echo 1;
    }

    /**
    * 验证用户是否可以登陆接口
    * @author cxf
    */
    public function api_editpassword()
    {
      $telno = $this->input->get('telno');
      $password = $this->input->get('password');
      $return = '';
      if (!preg_match('/^[_0-9a-z]{6,16}$/i',$password)){
       //密码格式不对
       $return = 'N_PASS';
      }

      $result = $this->user_model->yztelno($telno);
      if($result==0)
        {
        //无此用户
        $return = 'N_REG';
        }elseif($return != 'N_PASS'){
         $result = $this->user_model->updatepassowrd($telno,$password);
         $return = $result;
        }

        //密码格式不对
        if($return == 'N_PASS')
        {
          $result = 2;
          $msg = '密码格式不正确';
        }elseif($return == 'N_REG')
        {
          $result = 3;
          $msg = '号码未被注册';
        }elseif($return > 0){
          $result = 1;
          $msg = '修改密码成功';
        }elseif($return == 0){
          $result = 0;
          $msg = '修改密码失败';
        }
        $return_array = array('result'=>$result,'msg'=>$msg);
        echo serialize($return_array);
    }

    //判断房源标签类型
    public function getState($istag, $isreal, $nofee) 
	{
        if ($istag == 1) {
            $state = 1;
        } else if ($isreal == 1) {
            $state = 2;
        } else if ($nofee == 1) {
            $state = 3;
        } else {
            $state = 0;
        }
        return $state;
    }

   /**
    * 提交房源操作接口
    *
    * @access public
    * @param  无
    * @return void
    */
    public function api_sub_operate_log()
    {
		$this->load->model('house_statistic_model');
		$tbl = $this->input->get('tbl');
		$rowid = $this->input->get('rowid');
		$operate_type = $this->input->get('operate_type');
		$uid = $this->input->get('uid');
		$city = $this->input->get('city');
        $device = $this->input->get('device');
		$code = $this->input->get('code');

        //验证
		if( $code != md5('my365house'.$uid) )
		{
		    $result = array ('is_ok' => 1 , 'msg' => '参数不正确');
		}
        else
        {
            $this->house_statistic_model->set_city($city);
		    $this->house_statistic_model->set_uid( $uid );

		    $result = $this->house_statistic_model->sub_operate_log( $tbl , $rowid , $operate_type , $device );
        }

        echo serialize($result);
    }


	/**
    * 提交房源点击量接口
    *
    * @access public
    * @param  无
    * @return void
    */
	public function api_sub_house_click()
	{
		$result = array();

		//GET参数
		$city	= $this->input->get('city');
		$uid	= $this->input->get('uid');
		$tbl	= $this->input->get('tbl');
		$rowid	= $this->input->get('rowid');
		$district	= $this->input->get('district');
		$blockid	= $this->input->get('blockid');
		$streetid	= $this->input->get('streetid');
		$room		= $this->input->get('room');
		$price		= $this->input->get('price');
		$buildarea	= $this->input->get('buildarea');
		$client_ip	= $this->input->get('client_ip');
        $device = $this->input->get('device');
		$device = isset($device) ? $device : '1' ;
		$sub_key = $this->input->get('sub_key');

		if(  in_array($tbl,array('sell','rent')) && $rowid > 0 && $uid > 0 )
		{
			//验证秘钥
			if( $sub_key != md5('house_click_'.$uid.'_'.$tbl.'_'.$rowid) )
			{
				 $result = array ('is_ok' => 1 , 'msg' => '参数不正确');
			}
            else
            {
                $this->load->model('house_statistic_model');
			    $this->house_statistic_model->set_city( $city );
			    $this->house_statistic_model->set_uid( $uid );
			    $this->house_statistic_model->set_ip( $client_ip );

			    $result = $this->house_statistic_model->sub_house_click( $tbl , $rowid , $district , $blockid , $streetid , $room , $price , $buildarea , $device );
            }
		}
        else
        {
           $result = array ('is_ok' => 1 , 'msg' => '参数不正确');
        }

		echo serialize($result);
	}

	/**
    * 获取消息数接口
    *
    * @access public
    * @param  无
    * @return void
    */
    public function api_getmessagenumbyuid()
	{
		//GET参数
		$city	= $this->input->get('city');
		$uid	= $this->input->get('uid');

        $return = $this->user_model->getmessagenumbyuidwd($uid,$city);

         echo $return;
    }

    /**
    * 获取未读匹配数接口
    *
    * @access public
    * @param  无
    * @return void
    */
    public function api_getmatchnumbyuid()
	{
		//GET参数
		$uid	= $this->input->get('uid');

        $return = $this->user_model->get_match_num_byuid($uid);

         echo $return;
    }
	
	
	/**
    * 获取未读回答数接口
    *
    * @access public
    * @param  无
    * @return void
    */
    public function api_getnewanswerbyuid()
	{
		//GET参数
		$uid	= $this->input->get('uid');

        $return = $this->ask_model->getnewanswerbyuid($uid);

         echo $return;
    }
	
	
    /**
    * 提交可刷新次数接口
    *
    * @access public
    * @param  无
    * @return void
    */
    function api_add_refresh_num()
    {
        $this->load->model('house_model');
        $uid = $this->input->get("uid");
        $id = $this->input->get("id");
        $tbl = $this->input->get("tbl");
        $num = $this->input->get("num");
        $city_name = $this->input->get("city");
        $code = $this->input->get("code");
        $flag = $this->input->get("flag");
        if(empty($flag)){
          $flag=1;
        }
		if($code != md5('my365house'.$uid))
		{
			exit;
		}

        $this->house_model->addrefrshnum($uid,$id,$tbl,$num,$city_name,$flag);
        echo 1;
		exit;
    }
	

	/**
    * 获取个人出售&出租房源总刷新次数&已经刷新次数
    * @author bsw
    */
	function getRefreshHousePerson($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($this->city);
		$this->house_statistic_model->set_nocache(1);
		$arr = $this->house_statistic_model->get_operate_list(2,$id,date("Y-m-d"));
		return array('total'=>$num,'haveRef'=>count($arr));
	}

	/**
    * 批量获取个人出售&出租房源总刷新次数&已经刷新次数
    * @author bsw
    */
	function getRefreshHousePersonBatch(){
        $type = $this->input->get_post('type');
		$this->user_id = $this->input->get_post('uid');
		$this->city = $this->input->get_post('city');
		$tmp = $arr_ids = $this->input->get_post('arr_ids');
		$arr_ids = json_decode($arr_ids,1);
		
		$result = array();
        if(is_array($arr_ids) && $arr_ids){
			foreach($arr_ids as $val){
				$result[$val] = $this->getRefreshHousePerson($type,$val);
			}
		}

	    echo json_encode($result);
	}



	/**
    * 刷新个人出售房源
    * @author bsw
    */
	function refreshSellPerson(){
		$id = $this->input->get_post('id');
		$telno = $this->input->get_post('telno');
		$version = $this->input->get_post('version');
	
		$this->user_id = $this->user_model->yztelno($telno);
		$this->city = $this->input->get_post('city');

		$device = $this->input->get_post('device');
		  
		if(!$id || !$this->city || !$this->user_id){
			//参数错误
			$arr1['res'] = 0;
			$arr1['num'] = 0;
			echo json_encode($arr1);exit;
		}
        
		//获得实际可刷新次数
        $refreshInfo = $this->getRefreshHousePerson('sell',$id);
		if($version>='6.0.4')
		{
			if($refreshInfo['total']<9)
			{
				$num = $refreshInfo['total'] - $refreshInfo['haveRef']+1;
			}
			else
			{
				$num = $refreshInfo['total'] - $refreshInfo['haveRef'];
			}
			//$num = $refreshInfo['total'] - $refreshInfo['haveRef'];
		}
		else
		{
			$num = $refreshInfo['total'] - $refreshInfo['haveRef'];
		}
		if($num>9)
		{
			$num =9;
		}
		//echo $num;die;
		if($num>0){
			//更新房源刷新时间
			$result = $this->house_model->updatehouse($id,'sell',array('updatetime'=>time()));
			//记录刷新操作
			$this->house_statistic_model->set_uid($this->user_id);
			$this->house_statistic_model->set_city($this->city);
			$result = $this->house_statistic_model->sub_operate_log('sell' , $id , 2 ,$device);
			if($result['is_ok'] == 1){
				 //发送消息
				 $subject='您的房源刷新成功';
				 $msg='您编号为"'.$id.'"房源刷新成功。如有疑问，请联系客服。';
				 $this->apply_class->send_systemmsg($this->user_id,$subject,$msg,1,GB_CITY);
				 //刷新成功
				 $arr1['res'] = 1;
				// echo $num;die;
				 $arr1['num'] = $num-1;
				 echo json_encode($arr1);exit;
			}else{
				 //刷新失败
				 $arr1['res'] = 2;
				 $arr1['num'] = $num;
				 echo json_encode($arr1);exit;
			}
		}else{
			//刷新次数不足
			$arr1['res'] = 3;
			$arr1['num'] = $num;
			echo json_encode($arr1);exit;
		}
    }

	/**
    * 刷新个人出租房源
    * @author bsw
    */
	function refreshRentPerson(){
		$id = $this->input->get_post('id');
		$telno = $this->input->get_post('telno');
		$version = $this->input->get_post('version');
		$this->user_id = $this->user_model->yztelno($telno);
		$this->city = $this->input->get_post('city');

		$device = $this->input->get_post('device');
		  
		if(!$id || !$this->city || !$this->user_id){
			//参数错误
			$arr1['res'] = 0;
			$arr1['num'] = 0;
			echo json_encode($arr1);exit;
		}
        
		//获得实际可刷新次数
        $refreshInfo = $this->getRefreshHousePerson('rent',$id);
		if($version>='6.0.4')
		{
			if($refreshInfo['total']<9)
			{
				$num = $refreshInfo['total'] - $refreshInfo['haveRef']+1;
			}
			else
			{
				$num = $refreshInfo['total'] - $refreshInfo['haveRef'];
			}
		}
		else
		{
			$num = $refreshInfo['total'] - $refreshInfo['haveRef'];
		}
		if($num>9)
		{
			$num=9;
		}
		if($num>0){
			//更新房源刷新时间
			$result = $this->house_model->updatehouse($id,'rent',array('updatetime'=>time()));
			//记录刷新操作
			$this->house_statistic_model->set_uid($this->user_id);
			$this->house_statistic_model->set_city($this->city);
			$result = $this->house_statistic_model->sub_operate_log('rent' , $id , 2 ,$device);
			if($result['is_ok'] == 1){
				 //发送消息
				 $subject='您的房源刷新成功';
				 $msg='您编号为"'.$id.'"房源刷新成功。如有疑问，请联系客服。';
				 $this->apply_class->send_systemmsg($this->user_id,$subject,$msg,1,GB_CITY);
				 //刷新成功
				 $arr1['res'] = 1;
				 $arr1['num'] = $num-1;
				 echo json_encode($arr1);exit;
			}else{
				 //刷新失败
				 $arr1['res'] = 2;
				 $arr1['num'] = $num;
				 echo json_encode($arr1);exit;
			}
		}else{
			//刷新次数不足
			$arr1['res'] = 3;
			$arr1['num'] = $num;
			echo json_encode($arr1);exit;
		}
    }

	public function get_operate_list_api() {
		$id = $this->input->get_post('id');
		$telno = intval($this->input->get_post('telno'));
	    
	    $city = $this->input->get('city');
		$this->load->model('house_statistic_model');
		$this->house_statistic_model->set_uid($uid);
		$this->house_statistic_model->set_city($city);

		echo serialize($this->house_statistic_model->get_operate_list(5 , $id,date('Y-m-d',time()))) ;
	}

	
	/**
    * 添加个人的推送信息接口
    *
    * @access public
    * @param  无
    * @return void
    */
    public function addpush() {

	//记录日志
	$msg = "************************************************ \n";
	$msg .= date('Y-m-d H:i:s')." \n";
	$log = dirname(__FILE__)."/addpush.log";
	file_put_contents($log,$msg,FILE_APPEND );


    $telno = $this->input->get_post('telno');
    $uid = $this->user_model->yztelno($telno);
    $city = $this->input->get_post('city');
	$city = isset($city)?$city:'nj';
	$code = $this->input->get_post('code');
	if($code != md5('my365house'.$telno) || empty($telno))
	{    
		echo 'N_CHECK';exit;
	}
        //$msg = "*********************************************** \n";
		//$msg .= date('y-m-d H:i:s')."::telno-".$telno." \n";
		//$log = 'log/addpushlog'.date('Y-m-d');
		//@file_put_contents($log,$msg,FILE_APPEND );

	//验证不在白名单，不在推送库，不在推送历史库
	$result = $this->push_model->yzpush($telno);
    
	    //added by bianshiwu 临时日志
		//$data['sendtime'] = date('Y-m-d H:i:s');
		//$data['telno'] = $telno;
		//$data['desc'] = $result;
		//$this->push_model->setPushLogTemp($data);

		//$msg = date('y-m-d H:i:s')."::result-".$result." \n";
		//$log = 'log/addpushlog'.date('Y-m-d');
		//@file_put_contents($log,$msg,FILE_APPEND );

    if($result>0){
	 echo 'N_CHECK_Y';exit;
	}
    //如果有推送历史，只推送上次推送之后一个月的行为
    $sendtime = $this->push_model->getpushlogbytelno($telno);
	$sendtime = 86400*30;

    //读取求购
	$result = $this->push_model->addbuy($uid,$city,$sendtime);
	if($result>0){
     //加入成功，删除备选库
     $this->push_model->delalternatives($telno);
	 echo 'Y';exit;
	}
    //读取求租
	$result = $this->push_model->addrentwanted($uid,$city,$sendtime);
    if($result>0){
     //加入成功，删除备选库
     $this->push_model->delalternatives($telno);
	 echo 'Y';exit;
	}
    //预约看房
	/*
	$result = $this->push_model->addseehouse($uid,$telno,$sendtime);
    if($result>0){
     //加入成功，删除备选库
     $this->push_model->delalternatives($telno);
	 echo 'Y';exit;
	}
    

    //关注房源
    $result = $this->push_model->addcollecthouse($uid,$telno,$sendtime);
    if($result>0){
     //加入成功，删除备选库
     $this->push_model->delalternatives($telno);
	 echo 'Y';exit;
	}
	

    //发送到手机
    $result = $this->push_model->addsmshouse($uid,$telno,$sendtime);
    if($result>0){
     //加入成功，删除备选库
     $this->push_model->delalternatives($telno);
	 echo 'Y';exit;
	}	
	
    //关注小区
    $result = $this->push_model->addcollectblock($uid,$telno,$sendtime);
    if($result>0){
     //加入成功，删除备选库
     $this->push_model->delalternatives($telno);
	 echo 'Y';exit;
	}	
	*/

    echo 'N';exit;
    }


    /**
    * 添加个人的进入备选库接口
    *
    * @access public
    * @param  无
    * @return void
    */
    public function addalternatives() {
        $telno = $this->input->get_post('telno');
        $sendkind = $this->input->get_post('sendkind');
        $tbl = $this->input->get_post('tbl');
        $uid = $this->user_model->yztelno($telno);
        $code = $this->input->get_post('code');
        $city = $this->input->get_post('city');
        $city = !empty($city)?$city:GB_CITY; 
        if($code != md5('my365house'.$telno) or empty($uid) or empty($telno))
        {
        echo 'N_CHECK';exit;
        }
        //验证不在白名单，不在推送库，不在推送历史库最近一个月
        $result = $this->push_model->yzpush($telno);
        if($result>0){
         echo 'N_CHECK_Y';exit;
        }
        //加入备选库
        $result = $this->push_model->addalternatives($uid,$telno,$city,$sendkind,$tbl);
        if($result>0){
         echo 'Y';exit;
        }	
	
        echo 'N';exit;
    }
	
    /**
    * 获取推送信息接口
    *
    * @access public
    * @param  无
    * @return void
    */
    public function Getpush()
	{ 
		
		//记录日志
		$msg = "************************************************ \n";
		$msg .= date('Y-m-d H:i:s')." \n";
		$log = dirname(__FILE__)."/addpush.log";
		file_put_contents($log,$msg,FILE_APPEND );

		//当前方法执行会锁定该方法，防止重复执行
		$key  = "Job::Getpush";
		$check = $this->memcached_class->get($key);
		if($check !== false){
			echo json_encode('N_LOCKED');exit;
		}
		$this->memcached_class->add($key,true,1200);
 
		$time1=strtotime(date("Y-m-d "."8:30",time()));
		$time2=strtotime(date("Y-m-d "."21:30",time()));
		if(time()<$time1 || time()>$time2){
			echo json_encode('N_TIMEOUT');exit;
		}
		$code = $this->input->get_post('code');
		if($code != md5("my365house::ADMINPUSH")){
			echo json_encode('N_CHECK');exit;
		}
		$remainNum = $this->input->get_post('remainNum');
		if(intval($remainNum) <= 0){
			echo json_encode('N_REMAINNUM');exit;
		}
		$result = $this->push_model->getpusharray($remainNum);
		echo json_encode($result);
	}


   /**
    * 获取推送信息接口
    *
    * @access public
    * @param  无
    * @return void
    */
    public function Getpushtest()
	{ 
		
		//记录日志
		$msg = "************************************************ \n";
		$msg .= date('Y-m-d H:i:s')." \n";
		$log = dirname(__FILE__)."/addpushtest.log";
		file_put_contents($log,$msg,FILE_APPEND );

		//当前方法执行会锁定该方法，防止重复执行
		$key  = "Job::Getpush";
		$check = $this->memcached_class->get($key);
		if($check !== false){
			//echo json_encode('N_LOCKED');exit;
		}
		$this->memcached_class->add($key,true,1200);
 
		$time1=strtotime(date("Y-m-d "."8:30",time()));
		$time2=strtotime(date("Y-m-d "."21:30",time()));
		if(time()<$time1 || time()>$time2){
			echo json_encode('N_TIMEOUT');exit;
		}
		$code = $this->input->get_post('code');
		if($code != md5("my365house::ADMINPUSH")){
			//echo json_encode('N_CHECK');exit;
		}
		$remainNum = $this->input->get_post('remainNum');
		if(intval($remainNum) <= 0){
			//echo json_encode('N_REMAINNUM');exit;
		}
		$result = $this->push_model->getpusharraytest($remainNum);
		echo json_encode($result);
	}
	
	/**
    * 根据经纪人id获取未抢完用户推送列表
    *
    * @param  ctime 开始时间，$is_buy是否购买
    * @access public
    * @return void
    */
    public function Getpushbybrokerid()
	{
	$brokerid = $this->input->get_post('brokerid');
	if(empty($brokerid)){echo json_encode(0);exit;}
	$ctime = $this->input->get_post('ctime');
    $ctime = $ctime>(time()-60*60*24*30)?$ctime:(time()-60*60*24*30);
    $is_buy = $this->input->get_post('is_buy');
    $is_buy= !empty($is_buy)?$is_buy:0;
	$page = $this->input->get_post('page');
	$pagesize = $this->input->get_post('pagesize');
    $code = $this->input->get_post('code');
	if($code != md5('my365house'.$brokerid))
	{
	echo 'N_CHECK';exit;
	}
	$result = $this->push_model->Getpushnumbybrokerid($brokerid,$ctime,$is_buy,$page,$pagesize);
    echo json_encode($result);
	}

    /**
    * 根据经纪人id获取已经推送的数量
    *
    * @param  ctime 开始时间，$is_buy是否购买
    * @access public
    * @return void
    */
    public function Getpushnumbybrokerid()
	{
	$brokerid = $this->input->get_post('brokerid');
	if(empty($brokerid)){echo json_encode(0);exit;}
	$ctime = $this->input->get_post('ctime');
    $ctime = $ctime>(time()-60*60*24*30)?$ctime:(time()-60*60*24*30);
    $is_buy = $this->input->get_post('is_buy');
    $is_buy= !empty($is_buy)?$is_buy:0;
    $code = $this->input->get_post('code');
	if($code != md5('my365house'.$brokerid))
	{
	echo 'N_CHECK';exit;
	}
   
	$result = $this->push_model->Getpushnumbybroker($brokerid,$ctime,$is_buy);
    echo json_encode($result);
	}
	
    /**
    * 获取推送信息的详细信息
    *
    * @access public
    * @param  telno
    * @return void
    */
    public function Getsellnumbypush()
	{
		$telno = $this->input->get_post('telno');
		$sendid = $this->input->get_post('sendid');
		$sendkind = $this->input->get_post('sendkind');
		if(empty($telno)){echo json_encode(0);exit;}
		$code = $this->input->get_post('code');

		if($code != md5('my365house'.$telno))
		{
			echo 'N_CHECK';exit;
		}

		$result = $this->push_model->Getsellnumbypush($telno,$sendid,$sendkind);
		echo json_encode($result);
	}
	
	/**
    * 修改经纪人出售或者查看
    *
    * @access public
    * @param  $type 1,更新是否电话，2更新是否购买
    * @return void
    */
    public function Updatebrokerpush()
	{
	$brokerid = $this->input->get_post('brokerid');
	$pushid = $this->input->get_post('pushid');
	if(empty($brokerid)){echo json_encode(0);exit;}
    $type = $this->input->get_post('type');
    $type= !empty($type)?$type:1;
    $code = $this->input->get_post('code');
    $code_yz = $type==1?md5('my365house'.'::'.$brokerid):md5('my365house'.'::'.$brokerid.'::'.$pushid);
	if($code != $code_yz)
	{
	echo 'N_CHECK';exit;
	}	
	$result = $this->push_model->Updatebrokerpush($brokerid,$type,$pushid);
    echo json_encode($result);
	}
	

	/**
	* 根据用户行为修改用户的城市
	*
	* @access public
	* @param  $city,$telno,$uid
	* @return void
	*/
	public function Updateusercity()
	{
		$uid = $this->input->get_post('uid');

		if(empty($uid))
		{
			$telno = $this->input->get_post('telno');
			$uid = $this->user_model->yztelno($telno);
		}

		if(empty($uid))
		{
			return 0;
		}

		$city = $this->input->get_post('city');
		$city = !empty($city)?$city:GB_CITY;
		$result = $this->user_model->Updateusercity($uid,$city);

		echo json_encode($result);
	}

    //获取微信二维码
	function getWxPic(){
        $house = intval($_GET['id']);
		$tbl = $_GET['tbl'];
		if($tbl=='sell'){
		    $wxapi = "http://api.house365.com/esf/web/ewm/?url=http://nj.sell.house365.com/s_".$house.".html";
		}else{
		    $wxapi = "http://api.house365.com/esf/web/ewm/?url=http://nj.rent.house365.com/r_".$house.".html";
		}
		
		echo curl_get_contents($wxapi); 
	}

	 //获取合肥微信二维码
	function getHfWxPic(){
        $house = intval($_GET['id']);
		$tbl = $_GET['tbl'];
		if($tbl=='sell'){
		    $wxapi = "http://api.house365.com/esf/web/ewm/?url=http://hf.sell.house365.com/s_".$house.".html";
		}else{
		    $wxapi = "http://api.house365.com/esf/web/ewm/?url=http://hf.rent.house365.com/r_".$house.".html";
		}
		
		echo curl_get_contents($wxapi); 
	}


	function get_house_clicknum_by_id()
	{
		$this->load->model('house_statistic_model');
		$rowid = $this->input->get('rowid');
	    $tbl = $this->input->get('tbl');
		$uid = $this->input->get('uid');
		$city = $this->input->get('city');

        $this->house_statistic_model->set_uid( $uid );
        $click_num = $this->house_statistic_model->get_house_clicknum_by_id($rowid,$tbl,"",$city);
		
		echo $click_num;
		exit();
		
	}


	function sms_check_state()
	{
		$this->load->model('house_statistic_model');
		$telno = $this->input->get('telno');
		$status = $this->house_statistic_model->get_telno_state($telno);
		echo $status;exit;
		
	}


	function check_validcode_state()
	{
		$this->load->model('house_statistic_model');
		$telno = $this->input->get('telno');
		$validcode = $this->input->get('validcode');
		$status = $this->house_statistic_model->check_validcode_state($telno,$validcode);
	
		echo $status;exit;
	}

	function update_validcode_state()
	{
		$this->load->model('house_statistic_model');
		$telno = $this->input->get('telno');
		$validcode = $this->input->get('validcode');
		$status = $this->house_statistic_model->update_validcode_state($telno,$validcode);
		echo $status;exit;
	}

	/**
    * refreshSellPerson
    * 刷新APP个人房源（出售）
    */
	function refreshSellPersonApp()
	{
		$id = $this->input->get_post('id');
		$telno = $this->input->get_post('telno');
		$version = $this->input->get_post('version');
	
		$this->user_id = $this->user_model->yztelno($telno);
		$this->city = $this->input->get_post('city');

		$device = $this->input->get_post('device');
		  
		if(!$id || !$this->city || !$this->user_id){
			//参数错误
			$arr1['res'] = 0;
			$arr1['num'] = 0;
			echo json_encode($arr1);exit;
		}
        
		//获得实际可刷新次数
        $refreshInfo = $this->getRefreshHousePerson('sell',$id);
		if($version>='6.0.4')
		{
			if($refreshInfo['total']<9)
			{
				$num = $refreshInfo['total'] - $refreshInfo['haveRef']+1;
			}
			else
			{
				$num = $refreshInfo['total'] - $refreshInfo['haveRef'];
			}
			//$num = $refreshInfo['total'] - $refreshInfo['haveRef'];
		}
		else
		{
			$num = $refreshInfo['total'] - $refreshInfo['haveRef'];
		}
		if($num>9)
		{
			$num =9;
		}
		if($num>0){
			//更新房源刷新时间
			$result = $this->house_model->updatehouse($id,'sell',array('updatetime'=>time()));
			//记录刷新操作
			$this->house_statistic_model->set_uid($this->user_id);
			$this->house_statistic_model->set_city($this->city);
			$result = $this->house_statistic_model->sub_operate_log('sell' , $id , 2 ,$device);
			if($result['is_ok'] == 1){
				 //发送消息
				 $subject='您的房源刷新成功';
				 $msg='您编号为"'.$id.'"房源刷新成功。如有疑问，请联系客服。';
				 $this->apply_class->send_systemmsg($this->user_id,$subject,$msg,1,GB_CITY);
				 //刷新成功
				 $arr1['res'] = 1;
				// echo $num;die;
				 $arr1['num'] = $num-1;
				 echo json_encode($arr1);exit;
			}else{
				 //刷新失败
				 $arr1['res'] = 2;
				 $arr1['num'] = $num;
				 echo json_encode($arr1);exit;
			}
		}else{
			//刷新次数不足
			$arr1['res'] = 3;
			$arr1['num'] = $num;
			echo json_encode($arr1);exit;
		}
		
	}

	/**
    * 刷新APP端个人出租房源
    * @author bsw
    */
	function refreshRentPersonApp(){
		
		$id = $this->input->get_post('id');
		$telno = $this->input->get_post('telno');
		$version = $this->input->get_post('version');
		$this->user_id = $this->user_model->yztelno($telno);
		$this->city = $this->input->get_post('city');

		$device = $this->input->get_post('device');
		  
		if(!$id || !$this->city || !$this->user_id){
			//参数错误
			$arr1['res'] = 0;
			$arr1['num'] = 0;
			echo json_encode($arr1);exit;
		}
        
		//获得实际可刷新次数
        $refreshInfo = $this->getRefreshHousePerson('rent',$id);
		if($version>='6.0.4')
		{
			if($refreshInfo['total']<9)
			{
				$num = $refreshInfo['total'] - $refreshInfo['haveRef']+1;
			}
			else
			{
				$num = $refreshInfo['total'] - $refreshInfo['haveRef'];
			}
		}
		else
		{
			$num = $refreshInfo['total'] - $refreshInfo['haveRef'];
		}
		if($num>9)
		{
			$num=9;
		}

		if($num>0){
			//更新房源刷新时间
			$result = $this->house_model->updatehouse($id,'rent',array('updatetime'=>time()));
			//记录刷新操作
			$this->house_statistic_model->set_uid($this->user_id);
			$this->house_statistic_model->set_city($this->city);
			$result = $this->house_statistic_model->sub_operate_log('rent' , $id , 2 ,$device);
			if($result['is_ok'] == 1){
				 //发送消息
				 $subject='您的房源刷新成功';
				 $msg='您编号为"'.$id.'"房源刷新成功。如有疑问，请联系客服。';
				 $this->apply_class->send_systemmsg($this->user_id,$subject,$msg,1,GB_CITY);
				 //刷新成功
				 $arr1['res'] = 1;
				 $arr1['num'] = $num-1;
				 echo json_encode($arr1);exit;
			}else{
				 //刷新失败
				 $arr1['res'] = 2;
				 $arr1['num'] = $num;
				 echo json_encode($arr1);exit;
			}
		}else{
			//刷新次数不足
			$arr1['res'] = 3;
			$arr1['num'] = $num;
			echo json_encode($arr1);exit;
		}
        
		
    }


	//通过全站用户中心passport_uid获取二手房uid
	function getEsfUidByPassportUid(){
		$passport_uid = $this->input->get('passport_uid');
		//根据passport_uid获取uid
		$uid = $this->user_model->getUidByPassportId($passport_uid);
		if($uid>0){
			$arr1['res'] = 1;
			$arr1['msg'] = iconv('gbk','utf-8',"获取用户uid成功");
		    $arr1['data'] = array('uid'=>$uid);
		
		}else{
			$arr1['res'] = 0;
			$arr1['msg'] = iconv('gbk','utf-8',"获取用户uid失败");
		    $arr1['data'] = array();		
		}
	    echo json_encode($arr1);exit;

		
	}

	//获取求购求租匹配数据
	public function get_want_match(){
		//error_reporting(E_ALL);
          //  ini_set("display_errors",TRUE);

		 global $house;
        //获取表单数据
        $want_id = $this->input->get('want_id',TRUE);
        $want_type = strip_tags($this->input->get('want_type',TRUE));
        //$page = $this->input->get('page',TRUE);
        $data['priceunit'] = $house['priceunit'];

		$this->_limit = 20;

        if( $want_id > 0 && in_array( $want_type , array('buy','rentwant') ))
        {
            //加载房源操作统计类
            $this->load->model('want_house_model');

            $want_arr = array ('buy'=> array('house_tbl'=>'sell','match_tbl'=>'buy_match'),'rentwant'=> array('house_tbl'=>'rent','match_tbl'=>'rentwanted_match'));

            //设置房源表
            $this->want_house_model->set_tbl_house($want_arr[$want_type]['house_tbl']);

            //设置匹配表
            $this->want_house_model->set_tbl_match($want_arr[$want_type]['match_tbl']);
            if($want_type=='buy')
            {
            $fields_base = array('id','district','streetid','blockshowname','infotype','infofrom','buildarea','price','room','hall','fitment','floor','subfloor','totalfloor','address');
            }else{
            $fields_base = array('id','district','streetid','blockshowname','infotype','infofrom','buildarea','price','room','hall','fitment','floor','subfloor','totalfloor','priceunit','address');           
            }
            $fields_t['buy'] = array('buildyear');
            $fields_t['rentwant'] = array('renttype');
            $fields = array_merge( $fields_base , $fields_t[$want_type]);
            $this->want_house_model->set_house_fields($fields);

            //符合条件的总行数
            $this->_total_count = $this->want_house_model->get_matched_num_by_id( $want_id );
            //计算总页数
            $pages  = $this->_total_count > 0 ? ceil( $this->_total_count / 6 ) : 0;

            if($page>$pages)
            {
             $page=$pages;
            }

            //获取数据源

            $statistic_data = $this->want_house_model->get_matched_by_id( $want_id , $this->_offset , $this->_limit );

			if(is_array($statistic_data)&&!empty($statistic_data)){
				foreach($statistic_data as $key=>$value){
				    $statistic_data[$key]['url'] = $want_type == 'buy' ? get_sellinfo_url($value['id']) : get_rentinfo_url($value['id']);
                    $statistic_data[$key]['price'] = $want_type == 'buy' ? $value['price']."万元" : $value['price'].$house['priceunit'][$value['priceunit']];
					$statistic_data[$key]['title'] = $value['address'];
					unset($statistic_data[$key]['address']);
				}
			
			}
        }
        else
        {
            $this->_total_count = 0;
            $pages  = 0;
            $statistic_data = array();
        }
      

        $return_arr = array('total'=>$this->_total_count,'value'=>strIconv($statistic_data));
		echo json_encode(array('result'=>'1','msg'=>'success','data'=>$return_arr));
    
        
     
		
	}




	//获取求购求租匹配数据
	public function get_want_match_num(){
		//error_reporting(E_ALL);
          //  ini_set("display_errors",TRUE);

		
        //获取表单数据
        $want_id = $this->input->get('want_id',TRUE);
        $want_type = strip_tags($this->input->get('want_type',TRUE));


        if( $want_id > 0 && in_array( $want_type , array('buy','rentwant') ))
        {
            //加载房源操作统计类
            $this->load->model('want_house_model');

            $want_arr = array ('buy'=> array('house_tbl'=>'sell','match_tbl'=>'buy_match'),'rentwant'=> array('house_tbl'=>'rent','match_tbl'=>'rentwanted_match'));
            //设置房源表
            $this->want_house_model->set_tbl_house($want_arr[$want_type]['house_tbl']);
            //设置匹配表
            $this->want_house_model->set_tbl_match($want_arr[$want_type]['match_tbl']);  

            //符合条件的总行数
            $this->_total_count = $this->want_house_model->get_matched_num_by_id( $want_id );
        }
        else
        {
            $this->_total_count = 0;
            $pages  = 0;
            $statistic_data = array();
        }
      

        $return_arr = array('total'=>$this->_total_count);
		echo json_encode(array('result'=>'1','msg'=>'success','data'=>$return_arr));
    
        
     
		
	}






}