Base.class.php 8.41 KB
<?php
/**
 * 服务基础类
 * 
 * @name Service_Base
 * @package service/
 * @copyright yoho.inc
 * @author XMG
 */
class Service_Base extends Lib_Service
{
    const EXPIRE = 3600; // 缓存1小时
    const ROUTER = 'auth.yh_uuc.service'; // 路由规则
    
    protected static $_tag = 'tag_yh_service_'; // 缓存的tag
    protected static $_key = 'key_yh_service_'; // 缓存的KEY
    
    /**
     * 设置
     * 
     * @param array $params (添加的参数)
     * @param boolean $last_insert_id 是否返回主键ID,否则返回状态
     * @return boolean (false:失败, true:成功)
     */
    protected function set($table, $params,$last_insert_id = true)
    {
        $replaces = array('table' => $table);
        $replaces['fields'] = Util_Utils_SqlString::mergeInsertFields($params);
        $replaces['string'] = Util_Utils_SqlString::mergeInsertString($params);
        $update = $params ;
        if (isset($update['create_time']))
        {
            unset($update['create_time']);
        }
        if (isset($update['id']))
        {
            unset($update['id']);
        }
        $replaces['upstring'] = Util_Utils_SqlString::mergeSqlUpstring($update);
        if ($last_insert_id) 
        {
        	return self::service(self::ROUTER)
                ->tag(self::$_tag.$table)
                ->insert('set', $params, $replaces)
                ->lastInsertId();
        }
        return self::service(self::ROUTER)
                ->tag(self::$_tag.$table)
                ->insert('set', $params, $replaces)
                ->status();
    }
    
    /**
     * 更新
     * 
     * @param array $params (添加的参数)
     * @return boolean (false:失败, true:成功)
     */
    protected function update($table,$conditions, $data)
    {
        $replaces = array('table' => $table);
        $replaces['where'] = Util_Utils_SqlString::mergeSqlWhereString($conditions);
        $replaces['upstring'] = Util_Utils_SqlString::mergeSqlUpstring($data);
        $params = array_merge($data,$conditions);
        return self::service(self::ROUTER)
                ->tag(self::$_tag.$table)
                ->update('update', $params, $replaces)
                ->status();
    }
    
    
    /**
     * 删除
     * 
     * @param array $condition (删除的条件)
     * @return boolean (false:失败, true:成功)
     */
    protected function del($table, $condition)
    {
        $replaces = array
        (
    		'table' => $table,
    		'condition' => Util_Utils_SqlString::mergeSqlWhereString($condition)
        );
        return self::service(self::ROUTER)
                ->tag(self::$_tag.$table)
                ->delete('del', $condition, $replaces)
                ->status();
    }
    
    
    /**
     * 获取列表
     *
     * @param unknown_type $table
     * @param array $conditions
     * @param unknown_type $offset
     * @param unknown_type $limit
     */
    protected function getList($table, array $conditions, $offset, $limit,$order='',$map = '')
    {
        $replaces = array('table' => $table, 'order' => ($order)?('ORDER BY '.$order):'');
        $replaces['condition'] = Util_Utils_SqlString::mergeSqlWhereString($conditions);
        if ($limit || $offset)
        {
            $replaces['limits'] = 'LIMIT :offset,:limit';
            $conditions['offset'] = (int)$offset;
            $conditions['limit'] = (int)$limit;
        }
        if ($replaces['condition'])
        {
            $replaces['condition'] = 'WHERE '.$replaces['condition']; 
        }
        if($map)
        {
            $replaces['condition'] = ($replaces['condition'])?$replaces['condition'].' AND '.$map:' WHERE '.$map ;
        }
        //echo self::service(self::ROUTER)->tag(self::$_tag.$table)->getSql('getList', $conditions, $replaces);
        $key = md5(sprintf(self::$_key . '_%s_%s_%s_%s_%s',serialize($conditions),$offset,$limit,$order,$map).$table.'getList');
        return self::service(self::ROUTER)
                ->tag(self::$_tag.$table)
                ->key($key)
                ->fetchAll('getList', $conditions, $replaces);
    }
    
	/**
     * 获取总数
     *
     * @param unknown_type $table
     * @param array $conditions
     */
    protected function getListTotal($table, array $conditions  ,$map = '')
    {
        $replaces = array('table' => $table);
        $key = md5(sprintf(self::$_key . '_%s_%s_total',serialize( $conditions),$map).$table.'getListTotal');
        $replaces['condition'] = Util_Utils_SqlString::mergeSqlWhereString($conditions);
        if ($replaces['condition'])
        {
            $replaces['condition'] = 'WHERE '.$replaces['condition']; 
        }
        if($map)
        {
            $replaces['condition'] = ($replaces['condition'])?$replaces['condition'].' AND '.$map:' WHERE '.$map ;
        }
        //echo self::service(self::ROUTER)->tag(self::$_tag.$table)->key($key)->getSql('getListTotal', $conditions , $replaces);
        return self::service(self::ROUTER)
                ->tag(self::$_tag.$table)
                ->key($key)
                ->fetchOne('getListTotal', $conditions , $replaces);
    }
    
    
    /**
     * 批量获取
     *
     * @param unknown_type $shop_ids
     */
    protected function getListByIds($table, $ids,$order='', $id_column = 'id')
    {
        $replaces = array('table' => $table, 'order' => ($order)?('ORDER BY '.$order):'');
        $replaces['ids'] = Util_Utils_SqlString::mergeSqlInString($ids);
        $replaces['column'] = $id_column ;
        $key = md5(sprintf(self::$_key . '_%s_%s_%s',implode('_',$ids),$order,$id_column).$table.'getListByIds');
        return self::service(self::ROUTER)
                ->tag(self::$_tag.$table)
                ->key($key)
                ->fetchAll('getListByIds', array(), $replaces);
    }
    
    /**
     * 根据条件获取一条记录
     *
     * @param unknown_type $id
     */
    protected function getOne($table,  array $conditions,$column = '*')
    {
        $replaces = array('table' => $table,'column' => $column);
        $replaces['condition'] = Util_Utils_SqlString::mergeSqlWhereString($conditions);
        $key = md5(sprintf(self::$_key . '_%s',serialize( $conditions)).$table.'getOne');
        return self::service(self::ROUTER)
                ->tag(self::$_tag.$table)
                ->key($key)
                ->fetchRow('getOne', $conditions, $replaces);
    }
    
    /**
     * 获取统计数量(去重)
     *
     * @param unknown_type $table
     * @param array $conditions
     * @param unknown_type $distinct_column
     */
    protected function getDistinctTotal($table, array $conditions , $distinct_column)
    {
        $replaces = array('table' => $table,'distinct_column' => $distinct_column);
        $key = md5(sprintf(self::$_key . '_%s',serialize( $conditions)).$table.'getDistinctTotal');
        $replaces['condition'] = Util_Utils_SqlString::mergeSqlWhereString($conditions);
        if ($replaces['condition'])
        {
            $replaces['condition'] = 'WHERE '.$replaces['condition']; 
        }
        return self::service(self::ROUTER)
                ->tag(self::$_tag.$table)
                ->key($key)
                ->fetchOne('getDistinctTotal', $conditions , $replaces);
    }
    
    /**
     * 更新记录
     *
     * @param unknown_type $where
     */
    protected function updateDateByQuery( $table,  $data, $where )
    {
        $replaces = array
        (
    		'table' => $table,
            'upstring' => Util_Utils_SqlString::mergeSqlUpstring($data),
        	'where' => $where
        );
        return self::service(self::ROUTER)
                ->tag(self::$_tag.$table)
                ->update('updateDateByQuery', $data, $replaces)
                ->status();
    }
    
    /**
     * 为某条字段,自增计数
     *
     * @param string $table 表名
     * @param array $conditions 条件
     * @param string $column 要更新的字段
     * @param unknown_type $step 增加的数量
     * @return boolean
     */
    protected function inc($table, array $conditions , $column , $step)
    {
        $replaces = array('table' => $table,'column' => $column);
        $replaces['condition'] = Util_Utils_SqlString::mergeSqlWhereString($conditions);
        if ($replaces['condition'])
        {
            $replaces['condition'] = 'WHERE '.$replaces['condition']; 
        }
        $conditions['step'] = $step ;
        return self::service(self::ROUTER)
                ->tag(self::$_tag.$table)
                ->update('inc', $conditions , $replaces);
    }
}