<?php
/**
 * Description of GrowthBidHotSearchSet
 *
 * @author user
 */
class GrowthBidHotSearchSet {
    /**
     * 主数据库
     */
    private $db;

    /**
     * 从数据库
     */
    private $dbback;

    /**
     * 缓存对象
     */
    private $mc;
    
    /**
     * 操作记录对象
     */
    private $operateRecord;
    
    /**
     * 关联的表名
     * @var string
     */
    private $table;
    
    /**
     * 表中包含字段
     * @var array
     */
    private static $fields = array('id','type', 'words', 'ctime');

    public function __construct() {
        global $operateRecord;
        $container = Container::getInstance();
        $this->db = $container->get('db');
        $this->dbback = $container->get('dbback');
        $this->mc = $container->get('mc');
        $this->operateRecord = $operateRecord; 
        $this->table = 'growth_bid_hotsearch_set';
    }
    
    /**
     * 根据热搜词ID修改热搜词
     * @param int $id  热搜词ID
     * @param string $words  设置的热搜词
     * @return true 表示成功 false表示失败
     */
    public function setWords($id, $words) {
        $arrFieldValues = array();
        $arrFieldValues['words'] = $words;
        $sql = SqlCombinHelper::update_sql($this->table, $arrFieldValues, 
                "id = $id");
        $this->operateRecord->insertData('89', 'modify', 
                '修改热搜词', $_SESSION['esfadmin']['username']);
        return $this->db->execute($sql);
    }
    
    /**
     * 查找满足条件的热搜词数据
     * @param string $words  设置的热搜词
     * @return array 
     */
    public function findWords($where = '') {
        $sql = SqlCombinHelper::select_sql($this->table, self::$fields, $where);
        return $this->dbback->getAll($sql);
    }
    
    /**
     * 插入一条热搜词数据 
     * @param string $words  设置的热搜词
     * @return true 表示成功 false表示失败
     */
    public function addWords($type, $words) {
        $arrFieldValues = array();
        $arrFieldValues['type'] = $type;
        $arrFieldValues['words'] = $words;
        $this->operateRecord->insertData('89', 'add', 
                '添加热搜词' . $words, $_SESSION['esfadmin']['username']);
        $sql = SqlCombinHelper::insert_sql($this->table, $arrFieldValues);
        return $this->db->execute($sql);
    }
    
    /**
     * 
     * @param string $words  设置的热搜词
     * @param int $id  热搜词ID
     * @return type
     */
    public function findWordsExist($type, $words, $id = '') {
        if ($id) {$where = " AND id <> {$id}";}
        $sql = SqlCombinHelper::count_sql($this->table, "type = $type AND "
                . "words = '$words' $where");
        return $this->dbback->getOne($sql);
    }
    
    /**
     * 根据热搜词ID删除一条记录
     * @param int $id  热搜词ID
     * @return true 表示成功 false表示失败
     */
    public function delWords($id) {
        $sql = SqlCombinHelper::delete_sql($this->table, 'id = '. $id);
        $this->operateRecord->insertData('89', 'delete', 
                '删除热搜词', $_SESSION['esfadmin']['username']);
        return $this->db->execute($sql);
    }
    
    /**
     * 根据热搜词id获取一条记录
     * @param int $id  热搜词ID
     * @return array
     */
    public function findOneWordsById($id) {
        $sql = SqlCombinHelper::select_sql($this->table, self::$fields, 
                'id = '. $id);
        return $this->dbback->getRow($sql);
    }
}
