<?php

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

    //添加关键词排名
    public function add_rank($kwr_kw_id,$kwr_rank,$kwr_mobile_rank,$kwr_source,$kwr_mobile_source,$kwr_engines)
    {
        $sql = "INSERT INTO infoKeywordsrank (
                             kwr_kw_id,
                             kwr_rank,
                             kwr_mobile_rank,
                             kwr_source,
                             kwr_mobile_source,
                             kwr_datetime,
                             kwr_engines
                    ) VALUES (?,?,?,?,?,?,?)";
        $query = $this->HT->query($sql, array($kwr_kw_id,$kwr_rank,$kwr_mobile_rank,$kwr_source,$kwr_mobile_source,time(),$kwr_engines));
        return $this->HT->last_id('infoKeywordsrank');
    }

    //更新关键词排名
    public function update_rank($kwr_id,$rank,$mobile_rank,$source,$mobile_source){
        $set_sql = "UPDATE infoKeywordsrank
                    SET    kwr_datetime = ?,
                           kwr_rank=?,
                           kwr_mobile_rank=?,
                           kwr_source=?,
                           kwr_mobile_source=?
                    WHERE  kwr_id = ?";
        $result=$this->HT->query($set_sql, array(time(),$rank,$mobile_rank,$source,$mobile_source,$kwr_id));
        return $result;
    }

    //根据关键词id获取排名
    public function get_rank_list($kw_id_string,$top=1,$startdate=false,$enddate=false)
    {
        $map=$topmap='';
        if ($startdate) {
            if (!$enddate) $enddate=time();
            $map=" AND kwr_datetime BETWEEN '$startdate' AND '$enddate'";
        }else{
            $topmap= "TOP $top";
        }
        $sql = "SELECT $topmap kwr_id,
                         kwr_kw_id,
                         kwr_rank,
                         kwr_mobile_rank,
                         kwr_source,
                         kwr_mobile_source,
                         kwr_engines,
                         kwr_datetime,
                         kw_keyworlds
                FROM     infoKeywordsrank LEFT JOIN infoKeywords ON kw_id=kwr_kw_id
                WHERE    kwr_kw_id in ($kw_id_string) $map
                ORDER BY kwr_id DESC";
        $query = $this->HT->query($sql);
        return $query->result();
    }
    public function get_last_pc_rank($kw_id_string, $top=1)
    {
        $sql = "SELECT top $top kwr_id,
                         kwr_kw_id,
                         kwr_rank,
                         kwr_mobile_rank,
                         kwr_source,
                         kwr_mobile_source,
                         kwr_engines,
                         kwr_datetime
                FROM     infoKeywordsrank
                WHERE    1=1
                and kwr_kw_id in ($kw_id_string)
                and kwr_rank > 0
                ORDER BY kwr_id DESC ";
        $query = $this->HT->query($sql);
        return $query->result();
    }

    //删除指定关键词的所有排名
    public function delete($kw_id)
    {
        $sql = "DELETE FROM   infoKeywordsrank WHERE  kwr_kw_id = ?";
        $query = $this->HT->query($sql, array($kw_id));
        return $query;
    }
    //删除指定关键词的某个排名
    public function delete_one_rank($kwr_id)
    {
        $sql = "DELETE FROM   infoKeywordsrank WHERE  kwr_id = ?";
        $query = $this->HT->query($sql, array($kwr_id));
        return $query;
    }
}
