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

/**
 * User_Statistic_Model Class
 *
 * 用户操作日志、用户登录日志、用户注册方式、用户行为记录等数据
 *
 * @package         center
 * @subpackage      model
 * @category        model
 * @author          liuhu
 */
class User_Statistic_Model extends MY_Model {

    /**
    * 用户ID
    *
    * @access private
    * @var integer
    */
    private  $_uid    = 0;

    /**
    * 用户操作内容所在的城市
    *
    * @access private
    * @var string
    */
    private $_city  = 'nj';

    /**
    * 用户客户端IP
    *
    * @access private
    * @var string
    */
    private $_ip    = '';

    /**
    *  用户登录日志表
    *
    * @access private
    * @var string
    */
    private  $_log_login_tbl    = "log_login";

    /**
    *  登录来源
    *
    * @access private
    * @var array
    */
    private  $_login_from   = array(
								1 => 'center' ,2 => 'sell' ,3 => 'rent' ,4 => 'block' ,
								5 => 'news', 6 => 'broker',7 => 'ask', 8 => 'haiwai' , 
								9 => 'other', 10 => 'publish', 11 => 'mobile'
							);

    /**
    *  用户登录设备
    *
    * @access private
    * @var array
    */
    private  $_device   = array( 1 => 'pc' , 2 => 'andriod' , 3 => 'iphone' );

    /**
    * Constructor
    */
    public function __construct()
    {
        parent::__construct();
        $this->memkey = 'user_statistic'.GB_CITY;
    }


    /**
    * set_uid
    *
    * 初始化uid参数
    *
    * @access	public
    * @param	int     $uid 用户编号
    * @return	void
    */
    public function set_uid($uid)
    {
        $this->_uid = $uid;
    }


    /**
    * get_uid
    *
    * 初始化uid参数
    *
    * @access	public
    * @return	int     $uid 用户编号
    */
    public function get_uid( )
    {
        return $this->_uid ;
    }

    /**
    * set_city
    *
    * 初始化城市参数
    *
    * @access	public
    * @param	string  $city 城市参数
    * @return	void
    */
    public function set_city($city)
    {
        $this->_city    = $city;
    }


    /**
    * get_city
    *
    * 获得城市
    *
    * @access	public
    * @return	string  $city 城市
    */
    public function get_city()
    {
        return $this->_city ;
    }


    /**
    * set_ip
    *
    * 初始化IP参数
    *
    * @access	public
    * @return	string  $ip IP
    */
    public function set_ip($ip)
    {
       $this->_ip    = $ip;
    }


    /**
    * get_device
    *
    * 获取设备类型
    *
    * @access	public
    * @param	void
    * @return	array   登录设备类型
    */
    public function get_device()
    {
        return $this->_device;
    }


    /**
    * get_login_from
    *
    * 获取登录来源
    *
    * @access	public
    * @param	void
    * @return	array   登录来源数组
    */
    public function get_login_from()
    {
        return $this->_login_from;
    }


    /**
    * sub_login_log
    *
    * 提交用户登录日志
    *
    * @access	public
    * @param	int     $login_from 登录来源
    * @param	int     $device 登录设备 pc
    * @return	array   登录日志记录结果
    */
    public function sub_login_log( $login_from = 'center', $device = 'pc' )
    {
        $arr_from   = array_flip( $this->_login_from );
        $arr_device = array_flip($this->_device);

        //登录来源
        $login_from = ( !empty( $arr_from[$login_from] ) && $arr_from[$login_from] > 0 )
                        ? $arr_from[$login_from] : 0 ;

        //设备类别
        $device = ( !empty( $arr_device[$device] ) && $arr_device[$device] > 0 )
                    ? $arr_device[$device] : 0 ;

        //ip地址
        $ip = empty($this->_ip) ? get_ip() : $this->_ip;

        //提交的日志数据
        $data   = array(
                    'uid'           => $this->_uid,
                    'city'          => $this->_city,
                    'login_from'    => $login_from,
                    'device'        => $device,
                    'datetime'      => date('Y-m-d H:i:s'),
                    'ip'            =>  $ip
                    );

        //插入数据
       $this->set_table( $this->_log_login_tbl );
       $insert_id  = $this->add( $data );

       //返回数据
       $result = array();
       if( $insert_id > 0 )
       {
            $result['is_ok']    = 1;
            $result['insert_id']    = $insert_id;
            $result['msg']    = '登录日志记录成功';
       }
       else
       {
            $result['is_ok']    = 0;
            $result['insert_id']    = 0;
            $result['msg']    = '登录日志记录失败';
       }

       return  $result ;
    }


    /**
    * get_login_count
    *
    * 获取用户登录记录总数
    *
    * @access	public
    * @param	mixed   $datetime 登录日期
    * @param	int     $login_from 登录来源
    * @param	int     $device 登录设备
    * @return	array   登录记录总数
    */
    public function get_login_count( $datetime , $login_from = '' , $device = '')
    {
        $key = $this->memkey.'get_login_count'.$datetime.$login_from.$device.$this->_city.$this->_uid;

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

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

		if(empty($total_num) )
		{
            //base_filter
            $where_cond = "city = '".$this->_city."' AND uid = '".$this->_uid."' ";

            //login_from
            if( !empty( $login_from ) && is_numeric( $login_from ) )
            {
                $where_cond .= " AND login_from = '".$login_from."' ";
            }

            //device
            if( !empty( $device ) && is_numeric( $device ) )
            {
                $where_cond .= " AND device = '".$device."' ";
            }

            //时间参数
            $datetime = !empty( $datetime ) ? $datetime : date( 'Y-m-d' , time() - 86400 );
            //开始时间
            $datetime_start  = date('Y-m-d H:i:s', strtotime($datetime) ) ;
            //结束时间
            $datetime_end  = date('Y-m-d H:i:s', strtotime($datetime) + 86400 ) ;

            $where_cond .= " AND datetime BETWEEN '".$datetime_start."' AND  '".$datetime_end."'";

            $this->db_esfbak->where( $where_cond );
            $this->db_esfbak->from($this->_log_login_tbl);

            $total_num  =  $this->db_esfbak->count_all_results();
            $this->memcached_class->add($key, $total_num,3600);
        }
        return intval( $total_num ) ;
    }


    /**
    * get_login_list
    *
    * 获取用户登录记录
    *
    * @access	public
    * @param	mixed   $datetime 登录日期
    * @param	int     $offset 偏移数
    * @param	int     $limti 每次取的条数
    * @param	int     $login_from 登录来源
    * @param	int     $device 登录设备
    * @param	string  $order 升序降序
    * @return	array   登录记录
    */
    public function get_login_list( $datetime , $offset = 0 , $limit = 10  , $login_from = '' , $device = '' , $order = 'DESC')
    {
        $key = $this->memkey.'get_login_list'.$datetime.$offset.$limit.$login_from.$device.$order.$this->_city.$this->_uid;
		if($this->input->get('update') == 1)
		{
			$this->memcached_class->delete($key);
		}

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

		if(empty($arr_data) )
		{
            $arr_data = array();

            //base_filter
            $where_cond = "city = '".$this->_city."' AND uid = '".$this->_uid."' ";

            //login_from
            if( !empty( $login_from ) && is_numeric( $login_from ) )
            {
                $where_cond .= " AND login_from = '".$login_from."' ";
            }

            //device
            if( !empty( $device ) && is_numeric( $device ) )
            {
                $where_cond .= " AND device = '".$device."' ";
            }

            //增加不传时间，只读取最后一个登陆时间
            if( $datetime != 'notime')
            {
                //时间参数
                $datetime = !empty( $datetime ) ? $datetime : date( 'Y-m-d' , time() - 86400 );
                //开始时间
                $datetime_start  = date('Y-m-d H:i:s', strtotime($datetime) ) ;
                //结束时间
                $datetime_end  = date('Y-m-d H:i:s', strtotime($datetime) + 86400 ) ;

                $where_cond .= " AND datetime BETWEEN '".$datetime_start."' AND  '".$datetime_end."'";
            }

            $this->db_esfbak->where( $where_cond );
            $this->db_esfbak->order_by( 'datetime' , $order );
            $arr_data = $this->db_esfbak->get( $this->_log_login_tbl , $limit , $offset )->result_array();

            $this->memcached_class->add($key, $arr_data,3600);
        }
        return $arr_data ;
    }
}

/* End of file user_statistic_model.php */
/* Location: ./center/model/user_statistic_model.php*/