News.class.php 9.3 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);
        if(isset($data['id']))
        {
        	$func = 'status';
        }
        else
        {
            $func = 'lastInsertId';
        }
        $replaces = array('column' => $column, 'value' => $value , 'up' => $up) ;
        $status = self::service(self::ROUTER)->cache(false)->insert('setInfo', $data, $replaces)->$func();
        if($status)
        {
        	self::service(self::ROUTER)->delTags(self::$_tag);
        }
        return $status;
    }
    
    /**
     * 获取总数
     *
     * @param string $tag
     * @param array $exceptTags
     * @return int
     */
    public function getTotal($tag, $exceptTags,$type)
    {
       $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.'%")' ;
       }
        if($type==1) {
            $where = ' WHERE ' . $where . ' OR is_video=1 AND ' . $exceptWhere;
        }else{
            $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,$type)
    {
       $where = '1' ;
       $exceptWhere = '1';
       if(is_array($exceptTags) && !empty($exceptTags))
       {
       	   $exceptTags = implode("','", $exceptTags);
       	   $exceptWhere = "`tag` NOT IN ('$exceptTags') ";
       }
       if (!empty($tag))
       {
       	   $tag = strip_tags($tag);
           $tag = addslashes($tag);
           $where = '`tag` like ("'.$tag.'%")' ;
       }
        if($type==1) {
            $where = ' WHERE ' . $where . ' or is_video=1 AND ' . $exceptWhere;
        }else{
            $where = ' WHERE ' . $where . ' AND ' . $exceptWhere;
        }
       return self::service(self::ROUTER)->tag(self::$_tag)->fetchAssoc('getList', array('offset' => intval($offset), 'limit' => intval($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 getListBeforeTime($tag, $datetime, $offset, $limit)
    {
    	return self::service(self::ROUTER)->tag(self::$_tag)->fetchAssoc('getListBeforeTime', 
    	  array('time' => strtotime($datetime), 'offset' => intval($offset), 'limit' => intval($limit),'tag'=> $tag));
    }
    
    /**
     * 时间之前获取总数
     *
     * @param string $tag
     * @param string $datetime
     * @return int
     */
    public function getTotalBeforeTime($tag, $datetime)
    {
        return self::service(self::ROUTER)->tag(self::$_tag)->fetchOne('getTotalBeforeTime', array('time' => strtotime($datetime),'tag'=> $tag));
    }
    
    /**
     * 根据点击率获取列表
     *
     * @param string $tag
     * @param int $offset
     * @param int $limit
     * @param array $exceptTags
     * @return array
     */
    public function getListByHits($tag, $offset, $limit, $exceptTags)
    {
        $exceptWhere = "1";
        if(is_array($exceptTags) && !empty($exceptTags))
        {
            $exceptTags = implode("','", $exceptTags);
            $exceptWhere = " `tag` NOT IN ('$exceptTags') ";
        }
    	return self::service(self::ROUTER)->tag(self::$_tag)->fetchAssoc('getListByHits',array('tag'=> $tag, 
    	        'offset'=> intval($offset), 'limit'=> intval($limit)), array('where'=> $exceptWhere));
    }
    
    /**
     * 获取排序ID新闻
     *
     * @param array $ids
     * @param array $exceptTags
     * @return int
     */
    public function getNewsByIds($ids, $exceptTags)
    {
        $exceptWhere = "1";
        if(is_numeric($ids))
        {
        	$ids = array($ids);
        }
        $ids = implode(',', $ids);
        if(is_array($exceptTags) && !empty($exceptTags))
        {
            $exceptTags = implode("','", $exceptTags);
            $exceptWhere = " `tag` NOT IN ('$exceptTags') ";
        }
        return self::service(self::ROUTER)->tag(self::$_tag)->fetchRow('getNewsByIds', array(), 
                array('where'=> $exceptWhere, 'ids'=> $ids));
    }
    
    /**
     * 获取信息
     *
     * @param string $tag
     * @param int $offset
     * @param int $limit
     * @param array $exceptTags
     * @return array
     */
    public static function getLatestNews($tag, $offset, $limit, $exceptTags = array())
    {
        $exceptWhere = '1';
        $where = '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('getLatestNews', array('offset' => $offset, 'limit' => $limit), array('where' => $where));
    }
    
    /**
     * 根据日期点击率获取列表
     *
     * @param string $tag
     * @param int $offset
     * @param int $limit
     * @param array $exceptTags
     * @return array
     */
    public function getListByDateHits($tag, $offset, $limit, $exceptTags)
    {
        $exceptWhere = "1";
        if(is_array($exceptTags) && !empty($exceptTags))
        {
            $exceptTags = implode("','", $exceptTags);
            $exceptWhere = " `tag` NOT IN ('$exceptTags') ";
        }
        return self::service(self::ROUTER)->tag(self::$_tag)->fetchAssoc('getListByDateHits',array('tag'=> $tag,
                'offset'=> intval($offset), 'limit'=> intval($limit)), array('where'=> $exceptWhere));
         
    }
}