<?php

class Viewlogs_model extends CI_Model
{
	var $top_num=false;
	var $l_user_key=false;
	var $l_user_ip=false;
	var $l_page_auther=false;
	var $l_page_url=false;
	var $l_is_first_page=false;
	var $l_sitecode=false;
	var $order_by=false;
	var $have_order=false;
	var $datetime=false;
	
    function __construct()
    {
        parent::__construct();
        $this->IPDB = $this->load->database('IPDB', TRUE);
        $this->HT = $this->load->database('HT', TRUE);
    }
	
	function init()
    {
		$this->top_num=false;
		$this->l_user_key=false;
		$this->l_user_ip=false;
		$this->l_page_auther=false;
		$this->l_page_url=false;
		$this->l_is_first_page=false;
		$this->l_sitecode=false;
		$this->order_by=false;
		$this->have_order=false;		
		$this->datetime=false;		
    }

	function get_list()
	{
		$this->top_num ? $sql = "SELECT TOP " . $this->top_num : $sql = "SELECT ";
		$sql .= "	v.l_id, \n"
			 . "	v.l_user_key, \n"
			 . "	v.l_user_ip, \n"
			 . "	v.l_page_title, \n"
			 . "	v.l_page_url, \n"
			 . "	v.l_page_referer, \n"
			 . "	v.l_page_auther, \n"
			 . "	v.l_is_first_page, \n"
			 . "	v.l_sitecode, \n"
			 . "	v.l_ht_order_id, \n"
			 . "	v.l_ht_order_type, \n"
			 . "	v.l_datetime \n"
			 . "FROM \n"
			 . "	viewlogs v WHERE 1=1 ";
		$this->l_user_key ? $sql.=$this->l_user_key : false;
		$this->l_user_ip ? $sql.=$this->l_user_ip : false;
		$this->l_sitecode ? $sql.=$this->l_sitecode : false;
		$this->have_order ? $sql.=$this->have_order : false;
		$this->l_is_first_page ? $sql.=$this->l_is_first_page : false;
		$this->l_page_auther ? $sql.=$this->l_page_auther : false;
		$this->l_page_url ? $sql.=$this->l_page_url : false;
		$this->datetime ? $sql.=$this->datetime : false;
		$this->order_by ? $sql.=$this->order_by : false;
		$query=$this->IPDB->query($sql);
		if($this->top_num==1)
		{
			if ($query->num_rows() > 0)
			{
				$row = $query->row();
				return $row;
			}
			else
			{
				return FALSE;
			}
		}
		else
		{
			return $query->result();
		}
	}
	
    //写入浏览日志
    function add($l_user_key,$l_user_ip,$l_page_title,$l_page_url,$l_page_referer,$l_page_auther,$l_is_first_page,$l_sitecode)
    {
		$sql = "INSERT INTO viewlogs \n"
			 . "  ( \n"
			 . "    l_user_key, \n"
			 . "    l_user_ip, \n"
			 . "    l_page_title, \n"
			 . "    l_page_url, \n"
			 . "    l_page_referer, \n"
			 . "    l_page_auther, \n"
			 . "    l_is_first_page, \n"
			 . "    l_sitecode, \n"
			 . "    l_datetime \n"
			 . "  ) \n"
			 . "VALUES \n"
			 . "  ( ?,?,?,?,?,?,?,?,getdate() )";
        return $this->IPDB->query($sql, array($l_user_key,$l_user_ip,$l_page_title,$l_page_url,$l_page_referer,$l_page_auther,$l_is_first_page,$l_sitecode));
        //print_r($this->HT->queries);
    }
	
	//根据ip获取首访页面
	function get_first_page($sitecode,$ip)
	{
		$this->init();
		$this->top_num=1;
		$this->l_user_ip=' AND v.l_user_ip= '.$this->IPDB->escape($ip);
		$this->l_sitecode=' AND v.l_sitecode= '.$this->IPDB->escape($sitecode);
		$this->order_by=' ORDER BY v.l_is_first_page DESC,v.l_id ASC ';
		$this->datetime=" AND DAY(GETDATE()) = DAY(l_datetime) AND MONTH(GETDATE()) = MONTH(l_datetime) AND YEAR(GETDATE()) = YEAR(l_datetime) ";
		return $this->get_list();
	}
	
	//根据ip绑定订单和首访页面
	function binding($l_id,$order_id,$order_type)
	{
		$sql = "UPDATE viewlogs \n"
			 . "SET    l_ht_order_id = ?, \n"
			 . "       l_ht_order_type = ?, \n"
			 . "       l_is_first_page = 1 \n"
			 . "WHERE  l_id = ? ";
		return $this->IPDB->query($sql,array($order_id,$order_type,$l_id));
	}
	
	//统计分析
	function analytics($sitecode,$author,$orderby,$start_datetime,$end_datetime,$url)
	{
		$where_sql=' WHERE 1=1 ';
		if($sitecode)
		{
			$where_sql.=' AND v.l_sitecode='.$this->IPDB->escape($sitecode);
		}	
		if($author)
		{
			$where_sql.=' AND v.l_page_auther='.$this->IPDB->escape($author);
		}
		if($start_datetime && $end_datetime)
		{
			$where_sql.= " AND v.l_datetime BETWEEN '$start_datetime' AND '$end_datetime' ";
		}
		if($url)
		{
			$where_sql.= " AND v.l_page_url LIKE N'%$url%' ";
		}
		$orderby_sql=' ORDER BY order_count DESC ';
		if($orderby=='view_count')
		{
			$orderby_sql=' ORDER BY view_count DESC ';
		}
		
		$sql = "SELECT TOP 100 v.l_page_url, \n"
			 . "       v.l_page_auther, \n"
			 . "       COUNT(v.l_page_url) AS view_count, \n"
			 . "       COUNT(v.l_ht_order_id) AS order_count \n"
			 . "FROM   viewlogs v \n";
		$sql.=$where_sql;
		$sql.= " GROUP BY \n"
			 . "       v.l_page_url, \n"
			 . "       v.l_page_auther \n";
		$sql.=$orderby_sql;
		$query=$this->IPDB->query($sql);
		return $query->result();
	}
	
	//根据页面URL查询信息对应的作者
	function get_author($sitecode,$page_url)
	{
		//查询信息需要过滤url上的域名
		if($page_url && $sitecode)
		{
			$page_url=substr($page_url,strpos($page_url,'/',8));//过滤掉url前面的http://
			$sql = "SELECT TOP 1 ic.ic_author \n"
				 . "FROM   infoContents ic \n"
				 . "WHERE  ic.ic_sitecode = ? \n"
				 . "       AND ic.ic_url = N? ";
			$query=$this->HT->query($sql,array($sitecode,$page_url));
			if ($query->num_rows() > 0)
			{
				return $query->row()->ic_author;
			}
		}
        return '';
        		
	}
	
	//根据URL列出详细订单信息
	function get_url_order($sitecode,$url)
	{
		$this->init();
		$this->order_by=' ORDER BY v.l_datetime DESC ';
		$this->have_order=" AND (l_ht_order_id<>'' AND l_ht_order_id IS NOT NULL )";
		$this->l_page_url=" AND l_page_url=".$this->IPDB->escape($url);;
		return $this->get_list();
	}
	
}