<?php

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

    //获取结构列表
    public function get_structure_list(){
        $sql = "SELECT   is1.is_id AS id,
                         is1.is_parent_id AS pId,
                         kw_keyworlds+'('+isnull((select top 1 ISNULL(kwr_rank, kwr_mobile_rank) AS kwr_rank from infoKeywordsrank where kwr_kw_id=kw_id order by kwr_id desc),'#')+')' as name,
                         kw_status AS status,
                         kw_id
                FROM     infoKeywords
                         INNER JOIN infoStructures is1 ON kw_is_id = is1.is_id
                WHERE    kw_sitecode = ?
                ORDER BY is1.is_level ASC,
                         is1.is_sort ASC,
                         is1.is_path ASC";
        $query = $this->HT->query($sql, array($this->config->item('site_code')));
        return $query->result();
    }

    //添加关键词
    public function add_keyworlds($kw_keyworlds,$kw_sitecode,$kw_datetime,$kw_is_id,$kw_status=0){
        $sql = "INSERT INTO infoKeywords (
                             kw_keyworlds,
                             kw_sitecode,
                             kw_datetime,
                             kw_is_id,
                             kw_status
                    ) VALUES (?,?,?,?,?)";
        $query = $this->HT->query($sql, array($kw_keyworlds,$kw_sitecode,$kw_datetime,$kw_is_id,$kw_status));
        return $this->HT->last_id('infoKeywords');
    }

    //编辑关键词
    public function edit_keyworlds($kw_keyworld,$kw_id,$status){
        $set_sql = "UPDATE infoKeywords
                    SET    kw_keyworlds = ?,
                           kw_status = ?
                    WHERE  kw_id = ?";
        $result=$this->HT->query($set_sql, array($kw_keyworld,$status,$kw_id));
        return $result;
    }
    public function update_time($kw_id){
        $set_sql = "UPDATE infoKeywords
                    SET    kw_datetime = ?
                    WHERE  kw_id = ?";
        $result=$this->HT->query($set_sql, array(time(),$kw_id));
        return $result;
    }

    //删除关键词
    public function delete($kw_id){
        //删除关键词
        $set_sql = "DELETE FROM infoKeywords WHERE  kw_id = ?";
        $result=$this->HT->query($set_sql, array($kw_id));
        return $result;
    }

    //获取关键词列表用于抓取排名
    public function get_keyworld_list_old($top = 1, $engines='',$site_code=false)
    {
        $today = mktime(0,0,0,date("m"),(date("d")-$this->catch_day), date("Y"));
        //只查某个站点，原因是个事业部服务器分开，只需要查询本事业部站点即可
        $map   = " ";
        if(!empty($site_code)){
            $map.=" AND kw_sitecode in ('$site_code') ";
        }else if ($engines=='yandex') {
            $map=" AND kw_sitecode='ru' ";
        }elseif ($engines=='baidu') {
            $map.=" AND kw_sitecode='wt' ";
        }elseif ($engines=='en') {
            $map.=" AND kw_sitecode in ('cht','ct','sht','tbt','yz','gl','mbj','dct','mct','bht','ah') ";
        }elseif ($engines=='eu') {
            $map.=" AND kw_sitecode in ('gm','vc','vac','ru','it','jp') ";
        }
        $today0 = mktime(0,0,0,date("m"),(date("d")-2), date("Y"));
        $sql = "SELECT TOP 1 kw_id,
                             kw_keyworlds,
                             kw_is_id,
                             kw_sitecode,
                             kw_datetime,
                             kw_status
                FROM infoKeywords
                WHERE  kw_status=1 AND kw_datetime<($today0) $map
                ORDER BY kw_datetime asc";

        $query = $this->HT->query($sql);
        $result = $query->result();
        return $result;
    }
    public function get_keyworld_list($top=1,$engines='',$site_code=false)
    {
        $today0 = mktime(0,0,0,date("m"),(date("d")-$this->catch_day), date("Y"));
        $map   = " ";
        //只查某个站点，原因是个事业部服务器分开，只需要查询本事业部站点即可
        if(!empty($site_code)){
            $map.=" AND kw_sitecode in ('$site_code') ";
        }else if ($engines=='yandex') {
            $map=" AND kw_sitecode='ru' ";
        }elseif ($engines=='baidu') {
            $map.=" AND kw_sitecode='wt' ";
        }elseif ($engines=='en') {
            $map.=" AND kw_sitecode in ('cht','ct','sht','tbt','yz','gl','mbj','dct','mct','bht','ah') ";
        }elseif ($engines=='eu') {
            $map.=" AND kw_sitecode in ('gm','vc','vac','ru','it','jp') ";
        }
        $sql = "SELECT TOP $top kw_id,
                             kw_keyworlds,
                             kw_is_id,
                             kw_sitecode,
                             kw_datetime,
                             kw_status,
                             kwr_datetime,
                             kwr_rank,
                             kwr_mobile_rank,
                             kwr_id
                FROM     infoKeywords INNER JOIN infoKeywordsrank ON kwr_kw_id=kw_id
                AND kwr_id = ( SELECT top 1 kwr_id FROM infoKeywordsrank WHERE kwr_kw_id=kw_id order by kwr_id desc )
                WHERE kw_status=1 $map
                AND (
                    (kw_datetime>$today0 and kwr_mobile_rank IS NULL)
                    or
                    (kw_datetime<$today0)
                )
                ORDER BY kw_datetime ASC ";
        $query = $this->HT->query($sql);
        //print_r($this->HT->queries);
        $result = $query->result();
        return $result;
    }

    //根据关键词ID获取指定关键词
    public function get_keyworld($keyworld_id)
    {
        $sql = "SELECT TOP 1 kw_id,
                             kw_keyworlds,
                             kw_is_id,
                             kw_sitecode,
                             kw_datetime,
                             kw_status
                FROM     infoKeywords
                WHERE    kw_id=?";

        $query = $this->HT->query($sql,array($keyworld_id));
        $result = $query->result();
        if (!empty($result)) {
            $result=$result[0];
        }
        return $result;
    }

    //根据结构ID获取关键词内容
    public function get_keyworld_bytreeid($treeid){
        $sql = "SELECT TOP 1
                         kw_keyworlds,
                         kw_is_id,
                         kw_id,
                         kw_sitecode,
                         kw_datetime,
                         kw_status
                FROM     infoKeywords
                WHERE    kw_sitecode = ? AND kw_is_id=?";
        $query = $this->HT->query($sql, array($this->config->item('site_code'),$treeid));
        $result = $query->result();
        if (!empty($result)) {
            $result=$result[0];
        }
        return $result;
    }

    //根据关键词获取指定关键词列表
    public function get_keyworld_by_word($keyworld)
    {
        $sql = "SELECT TOP 30 kw_id,
                             kw_keyworlds,
                             isnull((select top 1 kwr_rank from infoKeywordsrank where kwr_kw_id=kw_id order by kwr_id desc),'#') as kw_rank,
                             kw_is_id,
                             kw_sitecode,
                             kw_datetime,
                             kw_status
                FROM     infoKeywords
                WHERE    kw_keyworlds like '%$keyworld%' AND kw_sitecode = ?";

        $query = $this->HT->query($sql, array($this->config->item('site_code')));
        return $query->result();
    }

    public function get_compare_rank($order='asc')
    {
        $m_datetime=date('Y-m-d 00:00:00');
        $sql="SELECT top 50 *
                FROM infoKeywords
                     INNER JOIN infosms on m_object_type='keywords_rank' and kw_id=m_object_id
               WHERE kw_sitecode = ? and m_readed = 0 order by cast(cast(m_content AS CHAR(50)) as int) $order";
        $query = $this->HT->query($sql, array($this->config->item('site_code'),$m_datetime));
        return $query->result();
    }
}
