<?php

/**
 * statistics
 * 默认接受字符gbk,页面字符gbk,只支持get方式传输$day格式date("Ymd")
 * @package classes
 * @author alang
 * @copyright www.house365.com
 * @version 2010
 * @access public
 */
class statistics {

    public $appName;
    public $appName_bak;
    public $method;
    public $channel;
    public $sourceCharSet;
    public $inChar;
    public $outChar;
    private $mc;

    /**
     * statistics::statistics()
     * 
     * @param mixed $appName 应用名称
     * @param string $sourceCharSet 编码
     * @return
     */
    public function __construct() 
	{
        $ci = & get_instance();
        $configStat = $ci->config->item('statistics');
        $configStatNew = $configStat[2]['new'];
        $configStatOld = $configStat[2]['old'];
        if (!$configStatNew || !$configStatOld) {
            error_log('统计类配置错误');
            show_404();
        }
        $this->appName = $configStatOld['appName'];
        $this->appName_bak = $configStatNew['appName'];
        $this->city = $configStatNew['city'];
        $this->sourceCharSet = strtolower($configStatNew['charSet']);
        $ci->load->library('My_memcached',null,'memcached');
        if (!$ci->memcached) {
            show_404();
        }
        $this->mc = $ci->memcached;
    }

    /*** 读取数据 ***/
    
    /**
     * 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;
            // 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;

        $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;

        $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;

        $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;

        $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;

        $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;

        $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;
        
        $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;
        
        $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')
    {
        $mem_key = GB_CITY.'_statistics_get_dayclickcount2_'.$day.'_'.$app.'_'.$uid.'v3';
        if($_COOKIE['update']){
	        $this->mc->delete($mem_key);
        }
        $cache = $this->mc->get($mem_key);

        $click_info = array();
        if($cache['is_ok'] == 1)
        {
            $click_info_new = $cache['data'];
        }
        else
        {
            $url = NewStatisticsUrl."/click?type=getClickUIDHourCount&city=".GB_CITY."&day=".$day."&app=".$app."&uid=".$uid;
            $strData = $this->curl_get_contents($url);
            $click_info = $this->json_decode($strData);
            $click_info_new =array();

            foreach ($click_info as $key =>$rs)
            {
                $click_info_new[$rs['hour']] =$rs;
            }


            $this->mc->add($mem_key, array('is_ok'=>1 , 'data' => $click_info_new ), 3600);
        }

        return $click_info_new;
    }


    /**
     * 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;
        }
        else
        {
            $url = "http://".$this->appName."/getclickcount?"
                    . "day=".$day."&uid=".$uid."&app=".$app.$strSql;
        }

        $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;
        }
        
        $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' )
    {
        $mem_key = GB_CITY.'_nj_statistics_get_fidclickcount_'.$day.'_'.$app.'_'.$fid;
		//echo  $mem_key;
		$this->mc->delete($mem_key);
        $cache = $this->mc->get($mem_key);
        
        if($cache['isok'] == 1)
        {
            $str = $cache['data'];
        }
        else
        {
             $url = "http://".$this->appName."/getclickcount?type=getfidcount&"
                    . "day=".$day."&fid=".$fid."&app=".$app."&city=".GB_CITY;
			if(GB_CITY=='hf')
			{
				//echo $url;die;
			}
            $strData = $this->curl_get_contents($url);
            $str = $this->strIconv( $strData , 'gbk' , $this->sourceCharSet);
            $str = abs($str);
            $this->mc->add($mem_key, array( 'isok'=>1 , 'data' => $str ),  3600);
        }

        return $str;
    }


	 /**
      * statistics::getfidsclickcountnew()
      * 获取多条房源某日点击量
      * @param mixed $day（昨日及以前）
      * @param string $fids
      * @param string $app
      * @return string
      */
    function getfidsclickcountnew( $day , $fids , $app = 'sell' )
    {
        $url = NewStatisticsUrl."/click?type=getClickFidsCount".'&city='.GB_CITY.
            "&day=".$day."&app=".$app."&fids=".$fids;
        //echo $url ;die;
        $strData = $this->curl_get_contents($url);

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

        $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;

        $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;

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


    /**
     * 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;
        
        $strData = $this->curl_get_contents($url);
        return $this->strIconv($strData,"gbk",$this->sourceCharSet);
    }


	//临时用的
    function get_searchcount_bak($day,$app,$array=null)
	{
        $strSql = $this->tourl($array);
        $mem_key = md5(GB_CITY.'_get_searchcount_bak3_'.$day.'_'.$app.'_'.$strSql);
        //$cache = $this->mc->delete($mem_key);
        $cache = $this->mc->get($mem_key);

        $data = array();

        if(isset($cache['isok']) && $cache['isok'] == 1)
        {
            $data = $cache['data'];
        }
        else
        {
            $url = NewStatisticsUrl."/search?type=getStreetHourCount&city=".GB_CITY."&day=".$day."&app=".$app.$strSql;
            $strData = $this->curl_get_contents($url);
            $data_array = $this->json_decode($strData);
            foreach ($data_array as $key =>$rs)
            {
                $data[$rs['hour']] =$rs['count'];
            }
            $this->mc->add( $mem_key, array('isok'=>1,'data'=>$data) , 3600);
        }

        return $data;
	}

	//临时用的
    function get_searchcount_bak_test($day,$app,$array=null)
	{
		$strSql = $this->tourl($array);
		$mem_key = md5('nj_get_searchcount_bak_'.$day.'_'.$app.'_'.$strSql);
        $cache = $this->mc->get($mem_key);
		//$cache = $this->mc->delete($mem_key);
		$data = array();

        if(isset($cache['isok']) && $cache['isok'] == 1 && false)
        {
            $data = $cache['data'];
        }
        else
        {
			$url = "http://".$this->appName_bak."/getsearchcount?type=searchcount&day=".$day."&app=".$app.$strSql;
			$strData = $this->curl_get_contents($url);
			$data =  $this->strIconv($strData,"gbk",$this->sourceCharSet);
			$this->mc->add( $mem_key, array('isok'=>1,'data'=>$data) , 3600);
		}
		
		return $data;
	}

	
    
    
    function get_searchhourcount($day,$app,$array=null)
    {
        $strSql = $this->tourl($array);
        $url = "http://".$this->appName."/getsearchcount?"
                . "type=searchhourcount&day=".$day."&app=".$app.$strSql;
        
        $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;
        
        $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;

        $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;

        $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;

        $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;

        $strData = $this->curl_get_contents($url);
        return $this->strIconv($strData,"gbk",$this->sourceCharSet);
    }
    
    
    function get_dayoperatecount($day,$uid,$operatetype='1',$app='sell')
    {
        $url = "http://".$this->appName."/getoperatecount?"
            . "type=getdayoperate&uid=".$uid."&day=".$day."&operatetype=".$operatetype."&app=".$app;
        
        $strData = $this->curl_get_contents($url);
        return $this->strIconv($strData,"gbk",$this->sourceCharSet);
    }
    
    
    //临时用的
    function get_dayoperatecount_bak($day,$uid,$operatetype='1',$app='sell')
    {
        $url = "http://".$this->appName_new."/getoperatecount?type=getdayoperate&"
                . "uid=".$uid."&day=".$day."&operatetype=".$operatetype."&app=".$app;

        $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;

        $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;
        }
        else
        {
            $url = "http://".$this->appName."/getclickcount?"
                    . "type=getorderbyagentcode&day=".$day."&limit=".$limit."&app=".$app;
        }
            
        $strData = $this->curl_get_contents($url);
        $strData = $this->strIconv($strData,"gbk",$this->sourceCharSet);
        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;
        
        $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;
        
        $strData = $this->curl_get_contents($url);
        $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;
        }
        else
        {
            $url = "http://".$this->appName."/getclickcount?"
                    . "type=getorderbyuid&day=".$day."&limit=".$limit."&app=".$app;
        }
            
        $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
     * @return bool
     */
    function sub_clickcount($fid,$uid,$clicksource='',$ip='',$agentcode='B000',$app='sell',$grade='')
    {
        $clicksource = $clicksource == '' ? $_SERVER["HTTP_REFERER"] : $clicksource;
        $clicksource = $clicksource == '' ? 'unknown' : urlencode($clicksource);
        $ip = $ip == '' ? $this->GetIP() : $ip;
        
        $url = "http://".$this->appName."/getclickcount?"
                . "type=submit&fid=".$fid."&uid=".$uid."&clicksource=".$clicksource."&"
                . "ip=".$ip."&agentcode=".$agentcode."&app=".$app."&grade=".$grade;

        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;

        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;

        $url ="http://172.17.1.177:8089/xxx-sell-submit-api/submit?type=login&city=".$this->city. "&uid=".$uid."&agentcode=".$agentcode."&ip=".$ip;
        //echo $url;die;

        //$this->curl_get_contents_nocache("http://" .$this->appName_bak. "/getlogincount?type=submit&uid=" . $uid . "&agentcode=" . $agentcode . "&ip=" . $ip);
        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' , $domain = '', $company = '')
    {
        if(!in_array($operatetype,array('0','1','2','3','4','5'))) 
        {
            return ;
        }
        
        $ip = $ip == '' ? $this->GetIP() : $ip;

        //$url_old = "http://".$this->appName."/getoperatecount?type=submit&uid=".$uid."&". "agentcode=".$agentcode."&operatetype=".$operatetype."&fid=".$fid."&". "ip=".urlencode($ip)."&app=".$app."&domain=".$domain."&company=".$company;

        $url_old = "http://172.17.1.177:8089/xxx-sell-submit-api/submit?type=operate&city=".$this->city."&uid=".$uid."&". "agentcode=".$agentcode."&operatetype=".$operatetype."&fid=".$fid."&". "ip=".urlencode($ip)."&app=".$app."&domain=".$domain."&company=".$company;

        //$this->curl_get_contents_nocache($url_new);
        return $this->curl_get_contents_nocache($url_old);
    }


    /**
     * 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;

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

    /**
     * statistics::GetIP()
     * 获得客户端ip
     * @return string
     */
    function GetIP()
    {
        $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::json_decode()
     * php4json_decode替代方法
     * @param mixed $data
     * @return array
     */
    function json_decode($data) 
    {
        return json_decode($data,true);
    }
    
    
    function curl_get_contents($str,$t_url="")
    {
        $memkey = md5($str);
	    if($_COOKIE['update']){
		    $this->mc->delete($memkey);
	    }
	    $cache = $this->mc->get($memkey);

	    //$this->mc->delete($memkey);
        if($cache['isok'] == 1)
        {
            $str = $cache['data'];
        }
        else
        {

			$startTime = time();//开始时间 

            $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_TIMEOUT, 3);//
            curl_setopt($ch, CURLOPT_HEADER, 0);//
            curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);

            $str = curl_exec($ch);
            curl_close($ch);
			
			$endTime = time();//结束时间
			
			if($endTime-$startTime>3){
			   //日志记录
				$time = date('Y-m-d H:i',time());
				$content = $time." ".$t_url." ".print_r($str,true)." \r\n";
				$fp1 = fopen('./applications/libraries/log_zjj_'.date('Ymd').'.txt','a');
				fwrite($fp1,"$content");
				fclose($fp1);
				
			}

            //日志记录
            $errorArr = array('error');
            foreach($errorArr as $value){
				if(strpos($str,$value)!== FALSE){
					$content = $time." ".$t_url." ".print_r($str,true)." \r\n";
					$fp1 = fopen('./applications/libraries/log_zjj2_'.date('Ymd').'.txt','a');
					fwrite($fp1,"$content");
					fclose($fp1);
				
				}
			
			}		
             


            $this->mc->add( $memkey ,  array('isok'=>1,'data'=>$str) , 3600);
        }

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

		$startTime = time();//开始时间 new add 
        $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_TIMEOUT, 3);//
        curl_setopt($ch, CURLOPT_HEADER, 0);//
        curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);

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

		$endTime = time();//结束时间 new add
			
		if($endTime-$startTime>3){
		   //日志记录
			$time = date('Y-m-d H:i',time());
			$content = $time." ".$t_url." ".print_r($str,true)." \r\n";
			$fp1 = fopen('./applications/libraries/log_zjj_'.date('Ymd').'.txt','a');
			fwrite($fp1,"$content");
			fclose($fp1);
			
		}

		//日志记录
		$errorArr = array('error');
		foreach($errorArr as $value){
			if(strpos($str,$value)!== FALSE){
				$time = date('Y-m-d H:i',time());
				$content = $time." ".$t_url." ".print_r($str,true)." \r\n";
				$fp1 = fopen('./applications/libraries/log_zjj2_'.date('Ymd').'.txt','a');
				fwrite($fp1,"$content");
				fclose($fp1);
			
			}
		
		}	
		


        return $str;
    }
}
?>