News.class.php 6.28 KB
<?php
/**
 *  资讯
 *
 * @name Service_News
 * @version 1.0 (2013-6-5)
 * @package Service/News
 * @since 1.0
 */

class Service_News extends Lib_Service
{
    const ROUTER = 'news.yohood.news';
    private static $_tag = 'tag_yohood_news_';
    private static $_key = 'key_yohood_news_';
    
    /**
     * 增加数据
     * 
     * @param array $data
     * @return boolean
     */
    public function setInfo($data)
    {
        $column = Util_Utils_SqlString::mergeInsertFields($data);
        $value  = Util_Utils_SqlString::mergeInsertString($data);
        $up = Util_Utils_SqlString::mergeSqlUpstring($data);
        $replaces = array('column' => $column, 'value' => $value , 'up' => $up) ;
        $status = self::service(self::ROUTER)->cache(false)->insert('setInfo', $data, $replaces)->status();
        if($status)
        {
        	self::service(self::ROUTER)->delTags(self::$_tag);
        }
        return $status;
    }
    
    /**
     * 获取总数
     *
     * @param string $tag
     * @param array $exceptTags
     * @return int
     */
    public function getTotal($tag, $exceptTags)
    {
       $where = '1' ;
       $exceptWhere = '1';
       if(is_array($exceptTags) && !empty($exceptTags))
       {
       	    $exceptTags = implode("','", $exceptTags);
       		$exceptWhere = " `tag` NOT IN ('$exceptTags') ";
       }
       if ($tag)
       {      
       	   $tag = strip_tags($tag);
           $tag = addslashes($tag);
           $where = '`tag` like ("'.$tag.'%")' ;
       }
       $where = ' WHERE '.$where .' AND '.$exceptWhere;
       return self::service(self::ROUTER)->tag(self::$_tag)->fetchOne('getTotal', array(),array('where' => $where));
    }
    
    /**
     * 获取总数
     * 
     * @param int $tag
     * @param int $offset
     * @param int $limit
     * @param array $exceptTags
     * @return array
     */
    public function getList($tag, $offset, $limit, $exceptTags)
    {
       $where = '1' ;
       $exceptWhere = '1';
       if(is_array($exceptTags) && !empty($exceptTags))
       {
       	   $exceptTags = implode("','", $exceptTags);
       	   $exceptWhere = "`tag` NOT IN ('$exceptTags') ";
       }
       if ($tag)
       {
       	   $tag = strip_tags($tag);
           $tag = addslashes($tag);
           $where = '`tag` like ("'.$tag.'%")' ;
       }
       $where = ' WHERE '.$where .' AND '.$exceptWhere;
       return self::service(self::ROUTER)->tag(self::$_tag)->fetchAssoc('getList', array('offset' => $offset, 'limit' => $limit), array('where' => $where));
    }
     
	/**
     * 获取总数
     *
     * @param int $id
     * @return array
     */
    public function getOneById( $id )
    {
       return self::service(self::ROUTER)->tag(self::$_tag)->fetchRow('getOneById', array('id' => $id));
    }
    
    /**
     * 删除
     *
     * @param int $id
     * @return boolean
     */
    public function delInfo($id = 0 )
    {
       $status = self::service(self::ROUTER)->cache(false)->delete('delInfo', array('id' => $id))->status();
       if($status)
       {
       	   self::service(self::ROUTER)->delTags(self::$_tag);
       }
       return $status;
    }
    
    /**
     * 更新点击量
     *
     * @param unknown_type $id
     */
    public function updateHits($id)
    {
        return self::service(self::ROUTER)->cache(false)->update('updateHits', array('id' => $id));
    }
    
    /**
     * 删除所有标签 
     *
     * @return boolean
     */
    public function delAllTags()
    {
        $status  = self::service(self::ROUTER)->cache(false)->delete('delAllTags')->status();
        if($status)
        {
        	self::service(self::ROUTER)->delTags(self::$_tag);
        }
        return $status;
    }
    
    /**
     * 添加TAG
     *
     * @param string $tag
     * @param int $num
     * @return boolean
     */
    public function createTag($tag, $num)
    {
        $status = self::service(self::ROUTER)->cache(false)->insert('createTag',array('tag' => $tag, 'num' => $num))->status();
        if($status)
        {
        	self::service(self::ROUTER)->delTags(self::$_tag);
        }
        return $status;
    }
    
    /**
     * 获取所有tag
     * 
     * @return array
     */
    public function getTags()
    {
        return self::service(self::ROUTER)->tag(self::$_tag)->fetchAll('getTags');
    }
    
    /**
     * 获取下一个新闻
     * 
     * @param int $id
     * @return array
     */
    public function getNextNews($id)
    {
        return self::service(self::ROUTER)->tag(self::$_tag)->fetchRow('getNextNews',array('id' => $id));
    }
    
    /**
     * 获取上一个新闻
     *
     * @param int $id
     * @return array
     */    
    public function getPrevNews($id)
    {
        return self::service(self::ROUTER)->tag(self::$_tag)->fetchRow('getPrevNews',array('id' => $id));
    }
    
    /**
     * 获取所有资讯
     * 
     * @param int $offset
     * @param int $limit
     * @return array
     */
    public function getAllNews($offset, $limit)
    {
    	return self::service(self::ROUTER)->tag(self::$_tag)->fetchAssoc('getAllNews', array('offset' => intval($offset), 'limit' => intval($limit)));
    }
    
    /**
     * 时间之后获取视频
     * 
     * @param string $tag
     * @param string $datetime
     * @param int $offset
     * @param int $limit
     * @return array
     */
    public function getListAfterTime($tag, $datetime, $offset, $limit)
    {
    	return self::service(self::ROUTER)->tag(self::$_tag)->fetchAssoc('getListAfterTime', 
    	  array('time' => strtotime($datetime), 'offset' => intval($offset), 'limit' => intval($limit),'tag'=> $tag));
    }
    
    /**
     * 时间之后获取总数
     *
     * @param string $tag
     * @param string $datetime
     * @return int
     */
    public function getTotalAfterTime($tag, $datetime)
    {
        return self::service(self::ROUTER)->tag(self::$_tag)->fetchAssoc('getTotalAfterTime', array('time' => strtotime($datetime),'tag'=> $tag));
    }
    
    /**
     * 根据点击率获取列表
     *
     * @param string $tag
     * @param int $offset
     * @param int $limit
     * @return array
     */
    public function getListByHits($tag, $offset, $limit)
    {
    	return self::service(self::ROUTER)->tag(self::$_tag)->fetchAssoc('getListByHits',array('tag'=> $tag, 'offset'=> intval($offset), 'limit'=> intval($limit)));
    }
}