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

/**
 * statistic Class
 *
 * 用户中心统计页面
 *
 * @package         esf_system
 * @subpackage      Libraries
 * @category        Libraries
 * @author          liuhu
 */
class statistic extends MY_Controller {

    /**
    * 用户选择的城市
    *
    * @access private
    * @var string
    */
    protected  $_city = 'nj';

    /**
    * 当前用户ID
    *
    * @access private
    * @var int
    */
    private  $_uid = 0;

    /**
    * 当前页码
    *
    * @access private
    * @var string
    */
    private $_current_page = 1;

    /**
    * 每页条目数
    *
    * @access private
    * @var int
    */
    private $_limit = 10;

    /**
    * 偏移
    *
    * @access private
    * @var int
    */
    private $_offset = 0;

    /**
    * 条目总数
    *
    * @access private
    * @var int
    */
    private $_total_count = 0;


    /**
    * 解析函数
    *
    * @access public
    * @return void
    */
    public function __construct()
    {
        parent::__construct();

        $this->load->helper('url');
    }


    /**
    * 初始化分页参数
    *
    * @access public
    * @param  int  $current_page
    * @param  int  $page_size
    * @return void
    */
    private function _init_pagination($current_page = 1 , $page_size = 0)
    {
        /** 当前页 */
        $this->_current_page = ( $current_page && is_numeric( $current_page ) ) ? intval( $current_page ) : 1;

        /** 每页多少项 */
        $this->_limit = ( $page_size && is_numeric( $page_size ) ) ? intval( $page_size ) : $this->_limit;

        /** 偏移量 */
        $this->_offset = ( $this->_current_page - 1 ) * $this->_limit;

        if($this->_offset < 0)
        {
            redirect(uri_string());
        }
    }

    //默认页面
    public function index()
    {
        $this->opreate_log();
    }
	

    /**
    * 房源操作日志
    *
    * @access public
    * @param  无
    * @return void
    */
    public function operate_log()
    {
       $this->user_id = checklogin();

        if( $this->user_id <= 0)
        {
            jumptotk(USER_CENTER.'login',"请登录后再进行操作！",3000,'tipswaring');
            exit;
        }

        //获取表单数据
        $page = $this->input->post('page',TRUE);
        $count_date = $this->input->post('count_date',TRUE);
        $operate_type = $this->input->post('operate_type',TRUE);
        $operate_type = intval($operate_type);

        if(!$operate_type){
            $operate_type = 1; //默认新增
        }
        //时间限制，最早一个月前，最晚昨天
        $count_date = $count_date ? $count_date : date('Y-m-d',time());

        /** 分页参数 */
        $this->_init_pagination($page);

        $this->_city = GB_CITY;
        $this->_uid = $this->session->userdata('esfuserid');

        //城市参数、或者用户编号参数为空，则跳转到首页
        if( empty($this->_city) || $this->_uid <= 0 )
        {
             redirect(site_url());
        }

        //加载房源操作统计类
        $this->load->model('house_statistic_model');

        //初始化城市和uid参数
        $this->house_statistic_model->set_city($this->_city);
        $this->house_statistic_model->set_uid($this->_uid);

        //符合条件的总行数
        $this->_total_count = $this->house_statistic_model->get_operate_count( $operate_type , $count_date);

        //计算总页数
        $pages  = $this->_total_count > 0 ? ceil( $this->_total_count / $this->_limit ) : 0;

        //获取数据源
        $rowid = 0;
        $statistic_data    = $this->house_statistic_model->get_operate_list( $operate_type , $rowid , $count_date , $this->_offset , $this->_limit );

        //表单类
        $this->load->helper('form');

        //是否显示分页
        $pagination = NULL;
        if( $this->_total_count > $this->_limit)
        {
            $this->load->helper('pagination');
            $pagination   = helper_pagination( $this->_current_page , $pages , $this->_total_count );
        }

        /** 页面初始化 */
        $data['title'] = '统计分析-操作日志';
        $data['conf_where'] = 'statistic';
        $data['show_type'] = 'operate_log';
        $data['operate_data'] = $statistic_data;
        $data['count_date'] = $count_date;
        $data['operate_type'] = $operate_type;
        $data['page'] = $this->_current_page;
        $data['form_attributes'] = array('name' => 'search_form', 'id' => 'search_form');
        $data['operate_arr'] = $this->house_statistic_model->get_operate_type();
//        $data['operate_arr'] = array_merge(array('0'=>'全部'),$data['operate_arr']);
//        $data['operate_arr'][0] = '全部';
        $data['house_type']  = $this->house_statistic_model->get_house_type();
        $data['js'] = "<script type='text/javascript' src='".USER_CENTER."js/My97DatePicker/WdatePicker.js'></script>";
        $data['pagination'] =   $pagination;
		//BI统计 ----- start----->
		 
		$data['isa_website'] = '2,19,1366';
	    $data['isa_city'] = GB_CITY;
	    $data['isa_addones'] = '';
	    $data['isa_url'] = 'isa.house365.com/js/analyze.js?v=2.1';
		//BI统计 ----- ent----->

        //加载模板
        $this->load->view('my_statistic',$data);
    }

	
    /**
    * 用户登录日志
    *
    * @access public
    * @param  无
    * @return void
    */
    public function login_log()
    {
        $this->user_id = checklogin();

        if( $this->user_id <= 0)
        {
            jumptotk(USER_CENTER.'login',"请登录后再进行操作！",3000,'tipswaring');
            exit;
        }
        //获取表单数据
        $page = $this->input->post('page',TRUE);
        $count_date = $this->input->post('count_date',TRUE);
        //时间限制，最早一个月前，最晚昨天
        $count_date = $count_date ? $count_date : date('Y-m-d',time() - 86400 );

        //分页参数
        $this->_init_pagination($page);

        //用户信息
        $this->_city = GB_CITY;
        $this->_uid = $this->session->userdata('esfuserid');


        //城市参数、或者用户编号参数为空，则跳转到首页
        if(empty($this->_city) || $this->_uid <= 0 )
        {
             redirect(site_url());
        }

        //加载房源操作统计类
        $this->load->model('user_statistic_model');

        //初始化城市和uid参数
        $this->user_statistic_model->set_city($this->_city);
        $this->user_statistic_model->set_uid($this->_uid);

        //符合条件的总行数
        $this->_total_count = $this->user_statistic_model->get_login_count( $count_date );

        //计算总页数
        $pages = $this->_total_count > 0 ? ceil( $this->_total_count / $this->_limit ) : 0;

        //获取数据源
        $statistic_data = $this->user_statistic_model->get_login_list( $count_date , $this->_offset , $this->_limit );

        //表单类
        $this->load->helper('form');

        //是否显示分页
        $pagination = NULL;
        if( $this->_total_count > $this->_limit)
        {
            $this->load->helper('pagination');
            $pagination   = helper_pagination( $this->_current_page , $pages , $this->_total_count );
        }

        /** 页面初始化 */
        $data['title'] = '统计分析-登录历史';
        $data['conf_where'] = 'statistic';
        $data['show_type'] = 'login_log';
        $data['operate_data'] = $statistic_data;    //统计数据
        $data['count_date'] = $count_date;
        $data['page'] = $this->_current_page;
        $data['form_attributes'] = array('name' => 'search_form', 'id' => 'search_form');
        $data['js'] = "<script type='text/javascript' src='".USER_CENTER."js/My97DatePicker/WdatePicker.js'></script>";
        $data['pagination'] =   $pagination;
		//BI统计 ----- start----->
		 
		$data['isa_website'] = '2,19,1367';
	    $data['isa_city'] = GB_CITY;
	    $data['isa_addones'] = '';
	    $data['isa_url'] = 'isa.house365.com/js/analyze.js?v=2.1';
		//BI统计 ----- ent----->

        //加载模板
        $this->load->view('my_statistic',$data);
    }


    /**
    * 用户房源点击量统计
    *
    * @access public
    * @param  无
    * @return void
    */
    public function house_click()
    {

        $this->user_id = checklogin();

        if( $this->user_id <= 0)
        {
            jumptotk(USER_CENTER.'login',"请登录后再进行操作！",3000,'tipswaring');
            exit;
        }
        //获取表单数据
        $house_type = $this->input->post( 'house_type' , TRUE);
        $count_date = $this->input->post('count_date',TRUE);

        //查看类型
        $house_type = !empty($house_type)  ? $house_type  : 'all';

        //时间限制，最早一个月前，最晚昨天
        $count_date = $count_date ? $count_date : date('Y-m-d',time() - 86400 );

        //用户信息
        $this->_city = GB_CITY;
        $this->_uid = $this->session->userdata('esfuserid');

        //城市参数、或者用户编号参数为空，则跳转到首页
        if( empty( $this->_city ) || $this->_uid <= 0 )
        {
             redirect(site_url());
        }

        //加载房源操作统计类
        $this->load->model('house_statistic_model');

        //初始化城市和uid参数
        $this->house_statistic_model->set_city($this->_city);
        $this->house_statistic_model->set_uid($this->_uid);

        //获取数据源
        $click_data = $this->house_statistic_model->get_house_click_by_hour( $count_date , $house_type );

        //总点击量
        $total_click = count( $click_data );

        //点击量分组
        $click_group = array();
        if( $total_click > 0 )
        {
            foreach ( $click_data as $key =>$value)
            {
                $click_group[$value['tbl']][$value['hour']][] = $value;
            }
        }

        //循环每小时点击量个数
        $house_click_hour   = array();
        $sell_total_click   = 0;
        $rent_total_click   = 0;
        for( $i = 0 ; $i < 24 ; $i++ )
        {
            //出售、出租房源每个小时的点击量
            $sell_click_hour = isset($click_group['sell'][$i]) ? count($click_group['sell'][$i]) : 0;
            $rent_click_hour = isset($click_group['rent'][$i]) ? count($click_group['rent'][$i]) : 0;

            //出售点击总量
            $sell_total_click += $sell_click_hour;

            //出租点击总量
            $rent_total_click += $rent_click_hour;

            //各小时点击量总和
            if( $house_type == 'all' )
            {
                $house_click_hour[$i]  = $sell_click_hour + $rent_click_hour;
            }
            else if($house_type == 'sell')
            {
                $house_click_hour[$i]  = $sell_click_hour;
            }
            else if($house_type == 'rent')
            {
                $house_click_hour[$i]  = $rent_click_hour;
            }

            //x轴
            $x_axis[$i] = $i;
        }

        //准备显示数据
        $x_axis_json = json_encode( $x_axis );
        $click_num_json = !empty($house_click_hour) ? json_encode( $house_click_hour ) : NULL;

        //表单类
        $this->load->helper('form');

        $house_type_name = array('all'=>'全部','sell'=>'出售','rent'=>'出租');

        /** 页面初始化 */
        $data['title'] = '统计分析-点击统计';
        $data['conf_where'] = 'statistic';
        $data['show_type'] = 'house_click';
        $data['click_num_json'] = $click_num_json;
        $data['x_axis_json'] = $x_axis_json;
        $data['total_click'] = $total_click;
        $data['sell_total_click'] = $sell_total_click;
        $data['rent_total_click'] = $rent_total_click;
        $data['count_date'] = $count_date;
        $data['house_type'] = $house_type;
        $data['house_type_name'] = $house_type_name[$house_type];
        $js = "<script type='text/javascript' src='".USER_CENTER."js/My97DatePicker/WdatePicker.js'></script>";
        $js .= "<script type='text/javascript' src='".USER_CENTER."js/jquery_Highcharts/js/highcharts.js'></script>";
        $data['js'] = $js;
        //$data['pagination'] =   NULL;
		//BI统计 ----- start----->
		 
		$data['isa_website'] = '2,19,1368';
	    $data['isa_city'] = GB_CITY;
	    $data['isa_addones'] = '';
	    $data['isa_url'] = 'isa.house365.com/js/analyze.js?v=2.1';
		//BI统计 ----- ent----->

        //加载模板
        $this->load->view('my_statistic',$data);
    }


    /**
    * 提交登陆操作接口
    *
    * @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;
    }


    /**
    * 提交可刷新次数接口
    *
    * @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");

		if($code != md5('my365house'.$uid))
		{
			exit;
		}

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

}

/* End of file statistic.php */
/* Location: ./center/controllers/statistic.php */