<?php
/**统计类
 * statistics
 * 默认接受字符gbk,页面字符gbk,只支持get方式传输$day格式date("Ymd")
 * @package classes
 * @author alang
 * @copyright www.house365.com
 * @version 2010
 * @access public
 */
class statistics
{
    var $appName;
    var $sourceCharSet;
    var $inChar;
    var $outChar;
    var $city;

    /**
     * statistics::statistics()
     *
     * @param mixed $appName 应用名称
     * @param string $sourceCharSet 编码
     * @return
     */
    function statistics($appName,$city,$sourceCharSet="gbk")
    {

        $this->appName=$appName;
        $this->city=$city;
        $this->sourceCharSet=strtolower($sourceCharSet);
    }


	/**
	  *读取数据
	  */
	/**
	 * statistics::get_housecount()
	 * 获取房源总量
	 * @param mixed $day 日期
	 * @param array $array 传入的数组
     * array('infofrom'=>'','infotype'=>'','uid'=>'','agentcode'=>'','partition'=>'','counttype'=>'')
	 * @param string $app sell:二手房 rent:租房 默认为sell
	 * @return string
	 */
	function get_housecount($day,$array=null,$app='sell'){
		$strSql = $this->tourl($array);
		$url = "http://".$this->appName."/gethousecount?day=".$day."&app=".$app.$strSql."&city=".$this->city;
		// return $url;
		$strData = $this->curl_get_contents($url);
		return $this->strIconv($strData,"gbk",$this->sourceCharSet);
	}


	/**
	 * statistics::get_graderhouse()
	 * 获取某经纪人下星级房源量
	 * @param mixed $day
	 * @param mixed $uid 用户uid
	 * @param string $app
	 * @return string
	 */
	function get_graderhouse($day,$uid,$app='sell'){
		$url = "http://".$this->appName."/gethousecount?type=gradehouse&day=".$day."&uid=".$uid."&app=".$app."&city=".$this->city;
		// return $url;
		$strData = $this->curl_get_contents($url);
		return $this->strIconv($strData,"gbk",$this->sourceCharSet);
	}


	/**
	 * statistics::get_pricecount()
	 * 获取均价统计
	 * @param mixed $day
	 * @param array $array
     * array('infofrom'=>'','infotype'=>'','uid'=>'','agentcode'=>'','partition'=>'','counttype'=>'')
	 * @param string $app
	 * @return string
	 */
	function get_pricecount($day,$array=null,$app='sell'){
		$strSql = $this->tourl($array);
	    $url = "http://".$this->appName."/getpricecount?day=".$day."&app=".$app.$strSql."&city=".$this->city;
		// return $url;
		$strData = $this->curl_get_contents($url);
		return $this->strIconv($strData,"gbk",$this->sourceCharSet);
	}
	
	function get_clickcountbygrade($day,$uid='',$app='sell'){
		$strSql = $this->tourl($array);
		if($uid){
			$url = "http://".$this->appName."/getclickcount?type=getcountbygrade&day=".$day."&uid=".$uid."&app=".$app."&city=".$this->city;
		}
		$strData = $this->curl_get_contents($url);
		return $this->strIconv($strData,"gbk",$this->sourceCharSet);
	}

	/**
	 * statistics::get_aver()
	 * 获取某条件下多少天的均价走势
	 * @param mixed $startday
	 * @param array $array array('streetid'='') or array('blockid'='')
            * @param array $limit
	 * @param string $app
	 * @return string
	 */

	function get_aver($startday,$array,$limit,$app='sell'){
		$strSql = $this->tourl($array);
	    $url = "http://".$this->appName."/getpricecount?type=getaver&startday=".$startday."&limit=".$limit."&app=".$app.$strSql."&city=".$this->city;
		// return $url;
		$strData = $this->curl_get_contents($url);
		$strData = $this->strIconv($strData,"gbk",$this->sourceCharSet);
		return $this->json_decode($strData);
	}


	/**
	 * statistics::getcityaver()
	 * 昨日全市挂牌均价
	 * @param mixed $startday
	 * @param array $array array('streetid'='') or array('blockid'='')
         * @param array $limit
	 * @param string $app
	 * @return array
	 */

	function getcityaver($day,$limit,$app='sell'){
	    $url = "http://".$this->appName."/getpricecount?type=getcityaver&day=".$day."&limit=".$limit."&app=".$app."&city=".$this->city;
		// return $url;
		$strData = $this->curl_get_contents($url);
		$strData = $this->strIconv($strData,"gbk",$this->sourceCharSet);
		return $this->json_decode($strData);
	}

	/**
	 * statistics::getaverbyuid()
	 * 根据uid获取某条件下多少天的均价走势
	 * @param mixed $startday
	 * @param int $uid
	 * @param array $array array('streetid'='') or array('blockid'='')
     * @param array $limit
	 * @param string $app
	 * @return string
	 */

	function getaverbyuid($startday,$uid,$array,$limit,$app='sell'){
		$strSql = $this->tourl($array);
	    $url = "http://".$this->appName."/getpricecount?type=getaverbyuid&startday=".$startday."&uid=".$uid."&limit=".$limit."&app=".$app.$strSql."&city=".$this->city;
		//echo $url;
		$strData = $this->curl_get_contents($url);
		$strData = $this->strIconv($strData,"gbk",$this->sourceCharSet);
		return $this->json_decode($strData);
	}


	/**
	 * statistics::getaverbyinfo()
	 * 根据infofrom获取某条件下多少天的均价走势
	 * @param mixed $startday
	 * @param int $infofrom
	 * @param array $array array('streetid'='') or array('blockid'='')
     * @param array $limit
	 * @param string $app
	 * @return string
	 */


	function getaverbyinfo($startday,$infofrom,$array,$limit,$app='sell'){
		$strSql = $this->tourl($array);
	    $url = "http://".$this->appName."/getpricecount?type=getaverbyinfo&startday=".$startday."&infofrom=".$infofrom."&limit=".$limit."&app=".$app.$strSql."&city=".$this->city;
		//echo $url;
		$strData = $this->curl_get_contents($url);
		$strData = $this->strIconv($strData,"gbk",$this->sourceCharSet);
		return $this->json_decode($strData);
	}


	/**
	 * statistics::getuidaver()
	 * 昨日某经济人发布的房源均价
	 * @param mixed $day
	 * @param int $uid
     * @param array $limit
	 * @param string $app
	 * @return array
	 */

	function getuidaver($uid,$day,$limit,$app='sell'){
		$strSql = $this->tourl($array);
	    $url = "http://".$this->appName."/getpricecount?type=getuidaver&uid=".$uid."&day=".$day."&limit=".$limit."&app=".$app."&city=".$this->city;
		//echo $url;
		$strData = $this->curl_get_contents($url);
		$strData = $this->strIconv($strData,"gbk",$this->sourceCharSet);
		return $this->json_decode($strData);
	}


	/**
	 * statistics::get_housencount()
	 * 获取新增房源量
	 * @param mixed $day
	 * @param array $array
     * array('infofrom'=>'','infotype'=>'','uid'=>'','agentcode'=>'','partition'=>'','counttype'=>'')
	 * @param string $app
	 * @return string
	 */
	function get_housencount($day,$array=null,$app='sell'){
		$strSql = $this->tourl($array);
		$url = "http://".$this->appName."/gethousencount?day=".$day."&app=".$app.$strSql."&city=".$this->city;
		// return $url;
		$strData = $this->curl_get_contents($url);
		return $this->strIconv($strData,"gbk",$this->sourceCharSet);
	}

	/**
	 * statistics::get_dayclickcount()
	 * 获取经纪人房源某天点击量
	 * @param mixed $day
	 * @param mixed $uid
	 * @param string $app
	 * @return array
	 */
	function get_dayclickcount($day,$uid,$app='sell'){
		$url = "http://".$this->appName."/getclickcount?type=dayclickcount&day=".$day."&uid=".$uid."&app=".$app."&city=".$this->city;
		$strData = $this->curl_get_contents($url);
		$strData = $this->strIconv($strData,"gbk",$this->sourceCharSet);
		//return $strData;
		return $this->json_decode($strData);
	}

	function get_dayclickcount_test($day,$uid,$app='sell'){
		$url = "http://".$this->appName."/getclickcount?type=dayclickcount&day=".$day."&uid=".$uid."&app=".$app."&city=".$this->city;
		return $url;
	}
	/**
	 * statistics::get_clickcount()
	 * 获取实时点击量
	 * @param mixed $day
	 * @param string $uid
	 * @param array $array
     * array('hour'=>'','grade'=>'');
	 * @param string $app
	 * @return string
	 */
	function get_clickcount($day,$uid='',$array=null,$app='sell'){
		$strSql = $this->tourl($array);
		if($uid == ''){
			$url = "http://".$this->appName."/getclickcount?day=".$day."&app=".$app.$strSql."&city=".$this->city;
		}else{
			$url = "http://".$this->appName."/getclickcount?day=".$day."&uid=".$uid."&app=".$app.$strSql."&city=".$this->city;
		}
		//return $url;
		$strData = $this->curl_get_contents($url);
		return $this->strIconv($strData,"gbk",$this->sourceCharSet);
	}


	function get_clickcount_test($day,$uid='',$array=null,$app='sell'){
		$strSql = $this->tourl($array);
		if($uid == ''){
			$url = "http://".$this->appName."/getclickcount?day=".$day."&app=".$app.$strSql."&city=".$this->city;
		}else{
			$url = "http://".$this->appName."/getclickcount?day=".$day."&uid=".$uid."&app=".$app.$strSql."&city=".$this->city;
		}
		$strData = $this->curl_get_contents($url);
		return $this->strIconv($strData,"gbk",$this->sourceCharSet);
	}

	/**
	 * statistics::get_clickcount()
	 * 获取莫条房源实时点击量
	 * @param mixed $day
	 * @param string $fid
	 * @param string $app
	 * @return string
	 */
	function get_fidclickcount($day,$fid,$app='sell'){
		$url = "http://".$this->appName."/getclickcount?day=".$day."&fid=".$fid."&app=".$app."&city=".$this->city;
		//return $url;
		$strData = $this->curl_get_contents($url);
		$num = $this->strIconv($strData,"gbk",$this->sourceCharSet);
		return $num < 0 ? 0 : $num;
	}

	/**
	 * statistics::get_clickcount()
	 * 获取莫条房源实时点击量
	 * @param mixed $day
	 * @param string $fid
	 * @param string $app
	 * @return string
	 */
	function get_fidclickcount_test($day,$fid,$app='sell'){
		$url = "http://".$this->appName."/getclickcount?day=".$day."&fid=".$fid."&app=".$app."&city=".$this->city;
		return $url;
		$strData = $this->curl_get_contents($url);
		$num = $this->strIconv($strData,"gbk",$this->sourceCharSet);
		return $num < 0 ? 0 : $num;
	}

	/**
	 * statistics::get_dayclickcountbyuid()
	 * 获取往日点击量(快)
	 * @param mixed $day
	 * @param string $uid
	 * @param string $app
	 * @return array
	 */
	function get_dayclickcountbyuid($day,$uid='',$app='sell'){
	    $url = "http://".$this->appName."/getclickcount?type=dayclickcountbyuid&day=".$day."&app=".$app."&city=".$this->city;
		if($uid != '')
			$url = "http://".$this->appName."/getclickcount?type=dayclickcountbyuid&day=".$day."&app=".$app."&uid=".$uid."&city=".$this->city;

		$strData = $this->curl_get_contents($url);
		$strData = $this->strIconv($strData,"gbk",$this->sourceCharSet);
		return $this->json_decode($strData);
	}


	/**
	 * statistics::getcountbylimit()
	 * 获取前几天的点击量
	 * @param string $uid
	 * @param array $array
     * array('hour'=>'','grade'=>'');
	 * @param string $app
	 * @return string
	 */
	function getcountbylimit($uid='',$limit='',$array=null,$app='sell'){
		$strSql = $this->tourl($array);
	    $url = "http://".$this->appName."/getclickcount?type=getcountbylimit&uid=".$uid."&limit=".$limit."&app=".$app.$strSql."&city=".$this->city;
		// return $url;
		$strData = $this->curl_get_contents($url);
		$strData = $this->strIconv($strData,"gbk",$this->sourceCharSet);
        return $this->json_decode($strData);
	}

	/**
	 * statistics::get_csclickcount()
	 * 获取某天某经济人各来源的点击量
	 * @param mixed $day
	 * @param mixed $uid
	 * @param string $limit 条数
	 * @param string $app
	 * @return array
	 */
	function get_csclickcount($day,$uid,$limit='1',$app='sell'){
		$url = "http://".$this->appName."/getclickcount?type=csclickcount&day=".$day."&uid=".$uid."&limit=".$limit."&app=".$app."&city=".$this->city;
		// return $url;
		$strData = $this->curl_get_contents($url);
		$strData = $this->strIconv($strData,"gbk",$this->sourceCharSet);
		return $this->json_decode($strData);
	}


	/**
	 * statistics::get_cshourclickcount()
	 * 获取某天某经济人某来源下各时段的点击量
	 * @param mixed $day
	 * @param mixed $uid
	 * @param mixed $clicksource 来源网址
	 * @param string $app
	 * @return array
	 */
	function get_cshourclickcount($day,$uid,$clicksource,$app='sell'){
	$url ="http://".$this->appName."/getclickcount?type=cshourclickcount&day=".$day."&uid=".$uid."&clicksource=".urlencode($clicksource)."&app=".$app."&city=".$this->city;
		$strData = $this->curl_get_contents($url);
		$strData = $this->strIconv($strData,"gbk",$this->sourceCharSet);
		return $this->json_decode($strData);
	}
        
	/*
	*statistics::get_dayoperatecount()
	*获取某天各时段的刷新量
	*/
	function get_dayoperatecount($day,$uid,$operatetype='1',$app='sell'){
	    $url = "http://".$this->appName."/getoperatecount?type=getdayoperate&uid=".$uid."&day=".$day."&operatetype=".$operatetype."&app=".$app."&city=".$this->city;
		$strData = $this->curl_get_contents($url);
		return $this->strIconv($strData,"gbk",$this->sourceCharSet);
	}


	/**
	 * statistics::get_searchcount()
	 * 获取某条件的搜索次数
	 * @param mixed $day
	 * @param mixed $app
	 * @param array $array
     * array('district'=>'','streetid'=>'','address'=>'','price'=>'','buildarea'=>'','room'=>'','floor'=>'','infotype'=>'','ip'=>'','hour'=>'');
	 * @return string
	 */
	function get_searchcount($day,$app,$array=null){
		$strSql = $this->tourl($array);
	    $url = "http://".$this->appName."/getsearchcount?type=searchcount&day=".$day."&app=".$app.$strSql."&city=".$this->city;
		$strData = $this->curl_get_contents($url);
		return $this->strIconv($strData,"gbk",$this->sourceCharSet);
	}


	/**
	 * statistics::get_searchrank()
	 * 获取某天关键词排名
	 * @param mixed $day
	 * @param string $searchtype 搜索字段
	 * @param string $limit
	 * @param string $app
	 * @return array
	 */
	function get_searchrank($day,$searchtype='address',$limit='100',$app='sell'){
	    $url = "http://".$this->appName."/getsearchcount?type=searchrank&day=".$day."&searchtype=".$searchtype."&limit=".$limit."&app=".$app."&city=".$this->city;
		// return $url;
		$strData = $this->curl_get_contents($url);
		$strData = $this->strIconv($strData,"gbk",$this->sourceCharSet);
		return $this->json_decode($strData);
	}


	/**
	 * statistics::get_logincount()
	 * 获取登陆次数
	 * @param mixed $day
	 * @param mixed $uid
	 * @param string $hour 小时(0-23)
	 * @param string $agentcode 中介编号
	 * @return string
	 */
	function get_logincount($day,$uid,$hour='',$agentcode=''){
	    $url = "http://".$this->appName."/getlogincount?type=getlogincount&day=".$day."&uid=".$uid."&hour=".$hour."&agentcode=".$agentcode."&city=".$this->city;
		//return $url;
		$strData = $this->curl_get_contents($url);
		return $this->strIconv($strData,"gbk",$this->sourceCharSet);

	}


	/**
	 * statistics::get_countrank()
	 * 获取小区挂牌量排行
	 * @param mixed $day
	 * @param string $limit
	 * @param string $app
	 * @return array
	 */
	function get_countrank($day,$limit='100',$app='sell'){
	    $url = "http://".$this->appName."/getrank?type=getcountrank&day=".$day."&limit=".$limit."&app=".$app."&city=".$this->city;
		// return $url;
		$strData = $this->curl_get_contents($url);
		$strData = $this->strIconv($strData,"gbk",$this->sourceCharSet);
		return $this->json_decode($strData);
	}


	/**
	 * statistics::get_pricerank()
	 * 获取小区挂牌价排行
	 * @param mixed $day
	 * @param string $limit
	 * @param string $app
	 * @return array
	 */
	function get_pricerank($day,$limit='100',$app='sell'){
	    $url = "http://".$this->appName."/getrank?type=getpricerank&day=".$day."&limit=".$limit."&app=".$app."&city=".$this->city;
		// return $url;
		$strData = $this->curl_get_contents($url);
		$strData = $this->strIconv($strData,"gbk",$this->sourceCharSet);
		return $this->json_decode($strData);
	}


	/**
	 * statistics::get_operatecount()
	 * 通过操作日志获取经纪人或者门店某日新增、刷新、删除房源量
	 * @param mixed $day
	 * @param array $array
     * array('uid'=>'','hour'=>'') or array('agentcode'=>'','hour'=>'')
	 * @param string $operatetype
     * 1:新增 2刷新 3删除
	 * @param string $app
	 * @return string
	 */
	function get_operatecount($day,$array,$operatetype='1',$app='sell'){
		$strSql = $this->tourl($array);
	    $url = "http://".$this->appName."/getoperatecount?type=getcount&day=".$day."&operatetype=".$operatetype."&app=".$app.$strSql."&city=".$this->city;
		$strData = $this->curl_get_contents($url);
		return $this->strIconv($strData,"gbk",$this->sourceCharSet);
	}


	/**
	 * statistics::get_credit()
	 * 获取积分事件详细信息
	 * @param mixed $day
	 * @param array $array
     * array('uid'=>'','agentcode'=>'','event'=>'1','op'=>'','hour'=>'','start'=>'','limit'=>'','app'=>'sell')
	 * @param string $app
	 * @return array
	 */
	function get_credit($day,$array,$app='sell'){
		$strSql = $this->tourl($array);
	    $url = "http://".$this->appName."/getcredit?type=getdetail&day=".$day."&app=".$app.$strSql."&city=".$this->city;
		// return $url;
		$strData = $this->curl_get_contents($url);
		$strData = $this->strIconv($strData,"gbk",$this->sourceCharSet);
		return $this->json_decode($strData);
	}


	/**
	 * statistics::get_orderbyagentcode()
	 * 获取某日房源点击量门店的排行
	 * Author:马达
	 * @param mixed $day 日期(格式如:20101219)
	 * @param mixed $limit 显示几条
	 * @param string $app (值为sell或者rent) 区别出租还是出售
	 * @param mixed $grade 区别多图房源(暂时值为3的话就是多图房源，如果为空的话就是所有房源)
	 * @return array
	 */
	 function get_orderbyagentcode($day,$limit='2',$app='sell',$grade=''){
		if($grade!=''){
			$url = "http://".$this->appName."/getclickcount?type=getorderbyagentcode&day=".$day."&limit=".$limit."&app=".$app."&grade=".$grade."&city=".$this->city;
		 }else{
			 $url = "http://".$this->appName."/getclickcount?type=getorderbyagentcode&day=".$day."&limit=".$limit."&app=".$app."&city=".$this->city;
		 }
		$strData = $this->curl_get_contents($url);
		//return $strData;
		$strData = $this->strIconv($strData,"gbk",$this->sourceCharSet);
		return $this->json_decode($strData);
	 }


	 /**
	 * statistics::get_orderbyuid()
	 * 获取某日房源点击量经纪人的排行
	 * Author:马达
	 * @param mixed $day 日期(格式如:20101219)
	 * @param mixed $limit 显示几条
	 * @param string $app (值为sell或者rent) 区别出租还是出售
	 * @param mixed $grade 区别多图房源(暂时值为3的话就是多图房源，如果为空的话就是所有房源)
	 * @return array
	 */
	 function get_orderbyuid($day,$limit='2',$app='sell',$grade=''){
		if($grade!=''){
			 $url = "http://".$this->appName."/getclickcount?type=getorderbyuid&day=".$day."&limit=".$limit."&app=".$app."&grade=".$grade."&city=".$this->city;
		 }else{
			 $url = "http://".$this->appName."/getclickcount?type=getorderbyuid&day=".$day."&limit=".$limit."&app=".$app."&city=".$this->city;
		 }
		$strData = $this->curl_get_contents($url);
		$strData = $this->strIconv($strData,"gbk",$this->sourceCharSet);
		return $this->json_decode($strData);
	 }



	/**
	  *提交数据
	  */
	/**
	 * statistics::sub_clickcount()
	 * 点击量数据提交接口
	 * @param mixed $fid 房源id
	 * @param mixed $uid
	 * @param string $clicksource
	 * @param string $ip
	 * @param string $app
	 * @param string $grade
	 * @param string $blockid
	 * @param string $istag
	 * @return bool
	 */
	function sub_clickcount($fid,$uid,$clicksource='',$ip='',$agentcode='B000',$app='sell',$grade='',$blockid='',$streetid='',$district='',$domain='',$company,$istag='',$room='',$price=''){
		$clicksource = $clicksource==''? $_SERVER["HTTP_REFERER"]:$clicksource;
		$clicksource = $clicksource==''? 'unknown':urlencode($clicksource);
		$ip = $ip==''?$this->GetIP():$ip;
		$company = trim($company);
		$url = "http://".$this->appName."/getclickcount?type=submit&fid=".$fid."&uid=".$uid."&clicksource=".$clicksource."&ip=".urlencode($ip)."&agentcode=".urlencode($agentcode)."&app=".$app."&grade=".$grade."&streetid=".$streetid."&blockid=".$blockid."&district=".$district."&domain=".$domain."&company=".$company."&istag=".$istag."&room=".$room."&price=".$price."&city=".$this->city;

		return $this->curl_get_contents_nocache($url);
	}


	/**
	 * statistics::sub_searchcount()
	 * 提交搜索数据
	 * @param array $array
     * array('district'=>'','streetid'=>'','address'=>'','price'=>'','buildarea'=>'','room'=>'','floor'=>'','infotype'=>'');
	 * @param string $ip
	 * @param string $app
	 * @return bool
	 */
	function sub_searchcount($array,$ip='',$app='sell'){
		$ip = $ip==''?$this->GetIP():$ip;
		$strSql = $this->tourl($array);
		$url = "http://".$this->appName."/getsearchcount?type=submit&ip=".$ip."&app=".$app.$strSql."&city=".$this->city;
		// return $url;
		return $this->curl_get_contents_nocache($url);
	}


	/**
	 * statistics::sub_logincount()
	 * 登录日志提交接口
	 * @param mixed $uid
	 * @param mixed $agentcode
	 * @param string $ip
	 * @return bool
	 */
	function sub_logincount($uid,$agentcode,$ip=''){
		$ip = $ip==''?$this->GetIP():$ip;
		$url = "http://".$this->appName."/getlogincount?type=submit&uid=".$uid."&agentcode=".$agentcode."&ip=".$ip."&city=".$this->city;
		$this->curl_get_contents_nocache("http://".NS_STACTICS_BAK."/getlogincount?type=submit&uid=".$uid."&agentcode=".$agentcode."&ip=".$ip."&city=".$this->city);
		//return $url;
		return $this->curl_get_contents_nocache($url);
	}


	/**
	 * statistics::sub_operatecount()
	 * 操作日志提交接口
	 * @param mixed $uid
	 * @param mixed $agentcode
	 * @param mixed $operatetype
	 * @param mixed $fid
	 * @param string $ip
	 * @param string $app
	 * @return bool
	 */
	function sub_operatecount($uid,$agentcode,$operatetype,$fid,$ip='',$app='sell'){
		
		//1:新增 2刷新 3删除 4失效 5再发布
		if(!in_array($operatetype,array('1','2','3','4','5'))) return ;

		$ip = $ip==''?$this->GetIP():$ip;
		
		$url = "http://".$this->appName."/getoperatecount?type=submit&uid=".$uid."&agentcode=".$agentcode."&operatetype=".$operatetype."&fid=".$fid."&ip=".urlencode($ip)."&app=".$app."&city=".$this->city;

		$this->curl_get_contents_nocache("http://".NS_STACTICS_BAK."/getoperatecount?type=submit&uid=".$uid."&agentcode=".$agentcode."&operatetype=".$operatetype."&fid=".$fid."&ip=".urlencode($ip)."&app=".$app."&city=".$this->city);
		return $this->curl_get_contents_nocache($url);
	}


	/**
	 * statistics::sub_credit()
	 * 积分操作提交接口
	 * @param mixed $uid
	 * @param mixed $agentcode
	 * @param mixed $op
	 * @param mixed $credit  积分值
	 * @param string $ip
	 * @param string $event  积分事件 默认为1
	 * @param string $app
	 * @return bool
	 */
	function sub_credit($uid,$agentcode,$op,$credit,$ip='',$event='1',$app='sell'){
		$ip = $ip==''?$this->GetIP():$ip;
		$url = "http://".$this->appName."/getcredit?type=submit&uid=".$uid."&agentcode=".$agentcode."&event=".$event."&op=".$op."&credit=".$credit."&ip=".$ip."&app=".$app."&city=".$this->city;
		// return $url;
		return $this->curl_get_contents_nocache($url);
	}

	/**
	 * statistics::tourl()
	 * 格式化数组参数
	 * @param array $array
	 * @return string
	 */
	function tourl($array){
		if(is_array($array) && !empty($array)){
			foreach($array as $k=>$v){
				$ss.="&".$k."=".@urlencode(trim($v));
			}
		}
		return $ss;
	}

   /**
    * statistics::GetIP()
    * 获得客户端ip
    * @return string
    */
   function GetIP(){
	   /*
	   if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")){
			   $ip = getenv("HTTP_CLIENT_IP");
	   }else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")){
			   $ip = getenv("HTTP_X_FORWARDED_FOR");
			   $arr_ip=explode(",",$ip);
		       $ip=$arr_ip[count($arr_ip)-1];
	   }else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")){
			   $ip = getenv("REMOTE_ADDR");
	   }else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")){
			   $ip = $_SERVER['REMOTE_ADDR'];
	   }else
			   $ip = "unknown";
	   return(trim($ip));
	   */
	   	   	$ip = $_SERVER["REMOTE_ADDR"];
			if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])){
				$tip=split(",",$_SERVER["HTTP_X_FORWARDED_FOR"]);
				$ip=$tip[0];
			}
		   return(trim($ip));
	   
	}

    /**
     * statistics::aryIconv()
     * 输入数组并返回数组
     * @param array $array 输入的数组
     * @param mixed $inChar 输入数组的编码
     * @param mixed $outChar 返回数组的编码
     * @return array
     */
    function aryIconv($array,$inChar,$outChar)
    {
        $this->inChar=$inChar;
        $this->outChar=$outChar;
        return $this->arraymyicov($array);
    }

    /**
     * statistics::arraymyicov()
     * 内部方法,循环数组
     * @param array $array
     * @return array
     */
    function arraymyicov($array)
    {
        foreach ((array)$array as $key=>$value)
        {
            $key=$this->strIconv($key);
            if (!is_array($value))
            {
                $value=$this->strIconv($value);
            }
            else
            {
                $value=$this->arraymyicov($value);
            }
            $temparray[$key]=$value;
        }
        return $temparray;
    }

    /**
     * statistics::strIconv()
     * 替换数组编码
     * @param mixed $strInput
     * @param string $inChar
     * @param string $outChar
     * @return string
     */
    function strIconv($strInput,$inChar="",$outChar="")
    {
        if($inChar)
            $this->inChar = $inChar;
        if($outChar)
            $this->outChar = $outChar;
        $strOutput = iconv($this->inChar,$this->outChar,$strInput);
        if($strOutput !== false)
            return $strOutput;
        else
            return $strInput;
    }

		/**
	 * statistics::get_totalcount()
	 * 获取总登陆次数
	 * @param mixed $day
	 * @param mixed $uid
	 * @param string $hour 小时(0-23)
	 * @param string $agentcode 中介编号
	 * @return string
	 */
	function get_totalcount($day,$uid,$hour='',$agentcode=''){
		 $url = "http://".$this->appName."/getlogincount?type=gettotalcount&day=".$day."&uid=".$uid;
		if(strval($hour) != ''){
			$url.="&hour=".$hour;
		}
		if($agentcode != ''){
			$url.="&agentcode=".$agentcode;
		}
		//return $url;
		$strData = $this->curl_get_contents($url);
		//return $strData;
		return $this->strIconv($strData,"gbk",$this->sourceCharSet);
	}

	
	/**
	 * statistics::get_detail()
	 * 获取登录详细信息
	 * @param mixed $day
	 * @param mixed $uid
	 * @param string $hour 小时(0-23)
	 * @param string $agentcode 中介编号
	 * @param string $start
	 * @param string $limit
	 * @return string
	 */
	function get_detail($day='',$uid='',$agentcode='',$hour='',$start='0',$limit='5'){
		 $url = "http://".$this->appName."/getlogincount?type=getdetail&start=".$start."&limit=".$limit;
		if($day != ''){
			$url.="&day=".$day;
		}
		if($uid != ''){
			$url.="&uid=".$uid;
		}
		if(strval($hour) != ''){
			$url.="&hour=".$hour;
		}
		if($agentcode != ''){
			$url.="&agentcode=".$agentcode;
		}
		//return $url;
		$strData = $this->curl_get_contents($url);
		//return $strData;
		$strData = $this->strIconv($strData,"gbk",$this->sourceCharSet);
		//return $strData;
		return $this->json_decode($strData);

	}

	/**
	 * 获取公司id下的经纪人刷新记录
	 * @param type getdayoperatebycid
	 * @param startDay 开始时间YYYYMMDD
	 * @param endDay 结束时间YYYYMMDD
	 * @param pid qtype=1时，pid为经纪人id,qtype=2时，pid为公司id
	 * @param operatetype 操作类型2为刷新操作
	 * @param app sell为二手房，rent为租房
	 * @param qType 1表示经纪人，2表示公司
	 */

	 function getdayoperatebycid($startDay,$endDay, $pid, $operatetype, $qType){
		$url = "http://".$this->appName."/getoperatecount?type=getdayoperatebycid&startDay=".$startDay."&endDay=".$endDay
			 ."&pid=".$pid."&operatetype=".$operatetype ."&qType=".$qType."&city=".$this->city;
		$strData = $this->curl_get_contents($url);
		$strData = $this->strIconv($strData,"gbk",$this->sourceCharSet);
		return $this->json_decode($strData);
	 }

	//公司统计刷新接口
	 function getdayoperatebyaid($startDay,$endDay, $pid, $operatetype, $qType){
		$url = "http://".$this->appName."/getoperatecount?type=getdayoperatebyaid&startDay=".$startDay."&endDay=".$endDay
			 ."&pid=".$pid."&operatetype=".$operatetype ."&qType=".$qType."&city=".$this->city;
		$strData = $this->curl_get_contents($url);
		$strData = $this->strIconv($strData,"gbk",$this->sourceCharSet);
		return $this->json_decode($strData);
	 }
	/**
	 * statistics::json_decode()
	 * php4 json_decode替代方法
	 * @param mixed $data
	 * @return array
	 */

	function json_decode($data) {
		return json_decode($data,true);
		/*
		 if (is_string($data)) {
			$data = trim($data);
			if ($data{0}!='{' && $data{0}!='[') return array();

			$data = str_replace(array('{','[',']','}',':','null','$'), array('array(','array(','),','),','=>','NULL',''), $data);
			$data = "array(".substr($data, 0, -1).")";
			$data = preg_replace_callback('/#\d+/',__FUNCTION__,$data);
			$data = str_replace(", ,",",",$data);
			@eval("\$data = $data;");
			return $data;
		}else{
			return array();
		}
	   */
	}

	function curl_get_contents($str,$t_url="")
	{
		global $mc;

		//return false;

		$memkey = md5($str);
		$cache = $mc->get($memkey);
		if($cache['isok'] == 1)
		{
			$str = $cache['data'];
		}
		else
		{
			$ch = curl_init();
			$t_url = $t_url ? $t_url : "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
			curl_setopt($ch, CURLOPT_URL, $str);
			curl_setopt($ch, CURLOPT_REFERER, $t_url);
			curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);

			$str = curl_exec($ch);
			curl_close($ch);

			$mc->add($memkey, array('isok'=>1,'data'=>$str), false, 600);
		}
		return $str;
	}

	function curl_get_contents_nocache($str,$t_url="")
	{
		//return false;

		$ch = curl_init();
		$t_url = $t_url ? $t_url : "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		curl_setopt($ch, CURLOPT_URL, $str);
		curl_setopt($ch, CURLOPT_REFERER, $t_url);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);

		$str = curl_exec($ch);
		curl_close($ch);

		return $str;
	}
}

?>