<?php

class infoKeywordsanalytics_model extends CI_Model
{
    function __construct()
    {
        parent::__construct();
        $this->HT = $this->load->database('HT', TRUE);
    }

    //添加新的记录
    public function add_analytics_by_txt(){
        $txt_path=dirname(dirname(__FILE__));
        $dataurl=$txt_path.'\helpers\analytics_data.txt';
        $formaturl=$txt_path.'\helpers\analytics_data_format.xml';
        $sql = "INSERT INTO infoKeywordsanalytics (
                            kwa_pagepath,
                            kwa_keyword,
                            kwa_source,
                            kwa_pageviews,
                            kwa_uniquepageviews,
                            kwa_avgtimeonpage,
                            kwa_entrances,
                            kwa_exitrate,
                            kwa_conversionrate,
                            kwa_datetime,
                            kwa_sitecode
                    )
                    SELECT  kwa_pagepath,
                            kwa_keyword,
                            kwa_source,
                            kwa_pageviews,
                            kwa_uniquepageviews,
                            kwa_avgtimeonpage,
                            kwa_entrances,
                            kwa_exitrate,
                            kwa_conversionrate,
                            kwa_datetime,
                            kwa_sitecode
                            FROM  OPENROWSET(
                                BULK  '$dataurl',
                                FORMATFILE='$formaturl'
                            ) as t1";
        $query = $this->HT->query($sql);
        return $query->result();
    }

    //根据页面路径获取指定信息页的成效信息
    public function get_analytics_list($kwa_pagepath,$top=50,$startdate='',$enddate=''){
        $topsql='';
        if($top!=0) $topsql="TOP $top";
        if ($startdate == '') {
            $startdate = time()-7*24*60*60;
        }
        if ($enddate == '') {
            $enddate = time();
        }
        $sql = "SELECT $topsql 
                         kwa_pagepath,
                         kwa_keyword,
                         kwa_source,
                         kwa_pageviews,
                         kwa_uniquepageviews,
                         kwa_avgtimeonpage,
                         kwa_entrances,
                         kwa_exitrate,
                         kwa_conversionrate,
                         kwa_datetime,
                         kwa_sitecode
                FROM     infoKeywordsanalytics
                WHERE    kwa_sitecode = ? AND kwa_pagepath=? AND (kwa_datetime BETWEEN ? and ? )
                ORDER BY kwa_pageviews DESC";
        $query = $this->HT->query($sql,array($this->config->item('site_code'),$kwa_pagepath,$startdate,$enddate));
        return $query->result();
    }

    //获取指定关键词的成效信息
    public function get_keyword_analytics($keyword,$top=30){
        $sql = "SELECT TOP $top 
                         kwa_pagepath,
                         kwa_keyword,
                         kwa_source,
                         kwa_pageviews,
                         kwa_uniquepageviews,
                         kwa_avgtimeonpage,
                         kwa_entrances,
                         kwa_exitrate,
                         kwa_conversionrate,
                         kwa_datetime,
                         kwa_sitecode
                FROM     infoKeywordsanalytics
                WHERE    kwa_sitecode = ? AND kwa_keyword=?
                ORDER BY kwa_datetime desc,kwa_pageviews DESC";
        $query = $this->HT->query($sql,array($this->config->item('site_code'),$keyword));
        return $query->result();
    }

    //查询是否已经更新
    public function get_update_count($sitecode)
    {
        $startdate = strtotime(date('Y-m-d'))-6*24*60*60;
        $sql = "SELECT count(kwa_id) as num
                FROM     infokeywordsanalytics
                WHERE    kwa_datetime>? AND kwa_sitecode=? ";
        $query = $this->HT->query($sql,array($startdate,$sitecode));
        $result = $query->result();
        if (empty($result)) {
            $result=0;
        }else{
            $result=$result[0]->num;
        }
        return $result;
    }
}