<?php

class Infotaskarticles_model extends CI_Model
{
	var $insert_id = -1;
	var $top_num=false;
	var $ta_id=false;
	var $ta_t_id=false;
	var $order_by = false;
	var $ta_status = false;
	
    function __construct()
    {
        parent::__construct();
        $this->HT = $this->load->database('HT', TRUE);
    }
	
	function init()
    {
		$this->top_num=false;
		$this->ta_id=false;
		$this->ta_t_id=false;
		$this->order_by = " ORDER BY i.ta_id DESC ";
		$this->ta_status = false;	
    }
	
	//获取版本列表
	function version_list($ta_t_id)
	{
		$this->init();
		$this->ta_status=" AND i.ta_status= 'version' ";
		$this->ta_t_id=" AND i.ta_t_id= ".$this->HT->escape($ta_t_id);
		return $this->get_list();
	}
	
	//获取最后的版本
	function version_last($ta_t_id)
	{
		/*$this->init();
		$this->top_num=1;
		$this->ta_status=" AND i.ta_status= 'version' ";
		$this->ta_t_id=" AND i.ta_t_id= ".$this->HT->escape($ta_t_id);
		return $this->get_list();*/

		$sql = "SELECT  TOP 1
						i.ta_id,
						i.ta_t_id,
						i.ta_ic_id,
						i.ta_title,
						i.ta_content,
						i.ta_summary,
						i.ta_seo_title,
						i.ta_seo_description,
						i.ta_seo_keywords,
						i.ta_status,
						i.ta_photo,
						i.ta_count,
						i.ta_charge,
						i.ta_datetime,
						ta_isbest,
						ic.ic_url,
						ic.ic_sitecode
				 FROM   infotaskarticles i 
				 		LEFT JOIN infoContents ic ON ic.ic_id=i.ta_ic_id
				WHERE   1 = 1 AND i.ta_status= 'version' AND i.ta_t_id= ".$this->HT->escape($ta_t_id)."ORDER BY i.ta_id DESC";
		$query=$this->HT->query($sql);
		if ($query->num_rows() > 0)
		{
			$row = $query->row();
			return $row;
		}
		else
		{
			return FALSE;
		}
	}
	
	//获取当前编辑版本
	function detail($ta_t_id)
	{
		$this->init();
		$this->top_num=1;
		$this->ta_status=" AND i.ta_status= 'used' ";
		$this->ta_t_id=" AND i.ta_t_id= ".$this->HT->escape($ta_t_id);
		return $this->get_list();
	}
	
	//获取原始版本版本
	function detail_original($ta_t_id)
	{
		if(empty($ta_t_id))
		{
			return false;
		}
		$this->init();
		$this->top_num=1;
		$this->ta_status=" AND i.ta_status= 'original' ";
		$this->ta_t_id=" AND i.ta_t_id= ".$this->HT->escape($ta_t_id);
		return $this->get_list();
	}
	
	//设置审核字数和金额
	function update_review($ta_t_id,$ta_count,$ta_charge,$ta_isbest=NULL)
	{
		$sql = "UPDATE infotaskarticles \n"
			 . "SET    ta_count = ? , \n"
			 . "       ta_isbest = ?, \n"
			 . "       ta_charge = ? \n"
			 . "WHERE  ta_id = ( \n"
			 . "           SELECT TOP 1 ta_id \n"
			 . "           FROM   infotaskarticles \n"
			 . "           WHERE  ta_status = 'version' \n"
			 . "                  AND ta_t_id = ? \n"
			 . "           ORDER BY \n"
			 . "                  ta_id DESC \n"
			 . "       )";	
		return $this->HT->query($sql,array($ta_count,$ta_isbest,$ta_charge,$ta_t_id));
	}
	
	function get_list()
	{
		$this->top_num ? $sql = "SELECT TOP " . $this->top_num : $sql = "SELECT ";
		$sql .= "      i.ta_id, \n"
			 . "       i.ta_t_id, \n"
			 . "       i.ta_ic_id, \n"
			 . "       i.ta_title, \n"
			 . "       i.ta_content, \n"
			 . "       i.ta_summary, \n"
			 . "       i.ta_seo_title, \n"
			 . "       i.ta_seo_description, \n"
			 . "       i.ta_seo_keywords, \n"
			 . "       i.ta_status, \n"
			 . "       i.ta_photo, \n"
			 . "       i.ta_count, \n"
			 . "       i.ta_charge, \n"
			 . "       i.ta_isbest, \n"
			 . "       i.ta_datetime \n"
			 . "FROM   infotaskarticles i \n"
			 . "WHERE  1 = 1 ";
		$this->ta_id ? $sql.=$this->ta_id : false;
		$this->ta_t_id ? $sql.=$this->ta_t_id : false;
		$this->ta_status ? $sql.=$this->ta_status : false;
		$this->order_by ? $sql.=$this->order_by : false;
		$query=$this->HT->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($ta_t_id,$ta_ic_id, $ta_title, $ta_content, $ta_summary, $ta_seo_title, $ta_seo_description, $ta_seo_keywords, $ta_photo,$ta_status='used')
    {
		$sql = "  INSERT INTO infotaskarticles \n"
			 . "  ( \n"
			 . "  	ta_t_id, \n"
			 . "  	ta_ic_id, \n"
			 . "  	ta_title, \n"
			 . "  	ta_content, \n"
			 . "  	ta_summary, \n"
			 . "  	ta_seo_title, \n"
			 . "  	ta_seo_description, \n"
			 . "  	ta_seo_keywords, \n"
			 . "  	ta_photo, \n"
			 . "  	ta_status, \n"
			 . "  	ta_count, \n"
			 . "  	ta_charge, \n"
			 . "  	ta_datetime \n"
			 . "  ) \n"
			 . "  VALUES \n"
			 . "  ( \n"
			 . "	?,?,N?,N?,N?,N?,N?,N?,?,?,0,0,GETDATE() \n"
			 . "  )";
        $query=$this->HT->query($sql, array($ta_t_id,$ta_ic_id, $ta_title, $ta_content, $ta_summary, $ta_seo_title, $ta_seo_description, $ta_seo_keywords, $ta_photo,$ta_status));
        $this->insert_id = $this->HT->last_id('infotaskarticles');
		//print_r($this->HT->queries);
		return $query;
		
    }
	
	function update($ta_id, $ta_title, $ta_content, $ta_summary, $ta_seo_title, $ta_seo_description, $ta_seo_keywords, $ta_photo)
	{
		$sql = "UPDATE infotaskarticles \n"
			 . "SET    ta_title = N?, \n"
			 . "       ta_content = N?, \n"
			 . "       ta_summary = N?, \n"
			 . "       ta_seo_title = N?, \n"
			 . "       ta_seo_description = N?, \n"
			 . "       ta_seo_keywords = N?, \n"
			 . "       ta_photo = ?, \n"
			 . "       ta_datetime = GETDATE() \n"
			 . "WHERE  ta_id = ?";
		$query=$this->HT->query($sql,array($ta_title, $ta_content, $ta_summary, $ta_seo_title, $ta_seo_description, $ta_seo_keywords, $ta_photo,$ta_id));
		//print_r($this->HT->queries);
		return $query;
	}
	
	
	//设置任务为原始版本状态
	function set_original($ta_id)
	{
		$sql = "UPDATE infotaskarticles \n"
			 . "SET    ta_status = 'original' \n"
			 . "WHERE  ta_id = ? ";
		return $this->HT->query($sql,array($ta_id));
	}
	
	//关联任务到信息平台
	//把所有任务都关联到新的信息中
	function link($ta_id,$ta_ic_id)
	{
		$sql = "UPDATE infotaskarticles \n"
			 . "SET    ta_ic_id = ? \n"
			 . "WHERE  ta_t_id IN (SELECT TOP 1 ita.ta_t_id \n"
			 . "                   FROM   infotaskarticles ita \n"
			 . "                   WHERE  ita.ta_id = ?)";
        $query=$this->HT->query($sql, array($ta_ic_id,$ta_id));
		return $query;	
	}	
	
	//获取网前url和标题等
	function get_information($ta_id)
	{
		$sql = "SELECT TOP 1  \n"
			 . "       ic.ic_url, \n"
			 . "       ic.ic_title \n"
			 . "FROM   infoContents ic \n"
			 . "WHERE  ic_status = 1 \n"
			 . "       AND ic.ic_id = ?";
		$query=$this->HT->query($sql,array($ta_id));
		if ($query->num_rows() > 0)
		{
			$row = $query->row();
			return $row;
		}
		else
		{
			return FALSE;
		}
	}

	//获取公告列表
	public function get_announce($announce_id='',$announce_type='notice'){
		$map='';
		if ($announce_id!='') {
			$map=" AND i.ta_id = $announce_id";
		}
		$sql = "SELECT  i.ta_id,
						i.ta_title,
						i.ta_content,
						ta_seo_title,
						i.ta_status,
						i.ta_datetime
				FROM    infotaskarticles i 
				WHERE   i.ta_status='announce' AND i.ta_seo_title=? $map
				ORDER BY ta_datetime DESC";
		$query=$this->HT->query($sql,array($announce_type));
		return $query->result();
	}
	//删除公告
	public function delete_announce($announce_id){
		$sql = "DELETE FROM infotaskarticles WHERE ta_id=?";
		$query=$this->HT->query($sql,array($announce_id));
		return $query;
	}

	//获取任务审核次数
    public function get_review_count($t_id){
    	$sql="select count(0) as review_count from infoTaskArticles where ta_t_id=? AND ta_status='version'";
    	$query = $this->HT->query($sql,array($t_id));
    	$result = $query->result();
    	$num=0;
    	if (!empty($result)) {
    		$num=$result[0]->review_count;
    	}
        return $num;
    }

    //根据页面路径获取指定信息页的成效信息
    public function get_analytics_list($kwa_pagepath,$kwa_sitecode,$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($kwa_sitecode,$kwa_pagepath,$startdate,$enddate));
        return $query->result();
    }
}