<?php
/**
 * 数据层基础类
 *
 * @author Wzb
 */
class DAL_Base {
	/**
	 * 表前缀
	 *
	 * @var string
	 */
	private static $_prefix = "tb_";
	
	/**
	 * 获取表名
	 *
	 * @param $type 1:admin 后台管理员表 2:config 系统设置 3:float 右侧浮动 4:onepage 单页 5:newssort 新闻类别 6:news 新闻
	 *        7:linksort 链接类别 8:link 链接 9:guestbook 留言 10:member 会员
	 */
	protected static function _tableName($type) {
		switch (intval ( $type )) {
			case 1 :
				return self::$_prefix . 'admin';
			case 2 :
				return self::$_prefix . 'config';
			case 3 :
				return self::$_prefix . 'float';
			case 4 :
				return self::$_prefix . 'onepage';
			case 5 :
				return self::$_prefix . 'newssort';
			case 6 :
				return self::$_prefix . 'news';
			case 7 :
				return self::$_prefix . 'linksort';
			case 8 :
				return self::$_prefix . 'link';
			case 9 :
				return self::$_prefix . 'guestbook';
			case 10 :
				return self::$_prefix . 'member';
			default :
				die ( "表名错误" );
				break;
		}
	}
	
	/**
	 * 添加一个记录
	 *
	 * @param array $theArray
	 * @param int $type
	 * @return int
	 */
	protected static function _add($theArray, $type) {
		global $conn;
		$rs = $conn->Execute ( "select * from " . self::_tableName ( $type ) . " where 1<>1" );
		if ($conn->Execute ( $conn->GetInsertSQL ( $rs, $theArray, false ) )) {
			$theId = $conn->Insert_ID ();
			if (array_key_exists ( 'serialNum', $theArray ) && intval ( $theId ) > 0) {
				$conn->Execute ( "update " . self::_tableName ( $type ) . " set serialNum=id where id=$theId" );
			}
			return $theId;
		} else {
			return 0;
		}
	}
	
	/**
	 * 修改一个记录
	 *
	 * @param array $theArray (必须包含id)
	 * @param int $type
	 * @return int
	 */
	protected static function _edit($theArray, $type) {
		global $conn;
		$rs = $conn->Execute ( "select * from " . self::_TableName ( $type ) . " where id=" . $theArray ['id'] );
		if ($conn->Execute ( $conn->GetUpdateSQL ( $rs, $theArray, true, false ) )) {
			return $theArray ['id'];
		} else {
			return 0;
		}
	}
	
	/**
	 * 删除一系列记录
	 *
	 * @param string $idList 不能为空 多个必须用-分割开
	 * @param int $type
	 * @return bool
	 */
	protected static function _delete($idList, $type) {
		global $conn;
		$idList = preg_replace ( '/[^\d^-]/', '', $idList );
		$sql = "delete from " . self::_TableName ( $type ) . " where ";
		if (count ( explode ( '-', $idList ) ) > 0) {
			$sql .= 'id in (' . str_replace ( '-', ',', $idList ) . ')';
		} else {
			$sql .= "id = $idList";
		}
		if ($conn->Execute ( $sql )) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * 获取一个记录
	 *
	 * @param int $id
	 * @param int $type
	 * @param string $code 需要的字段 默认值*
	 * @return array
	 */
	protected static function _getOne($id, $type, $code = '*') {
		global $conn;
		$sql = "select $code from " . self::_TableName ( $type ) . " where id=$id";
		return $conn->GetRow ( $sql );
	}
	
	/**
	 * 获取所有记录
	 *
	 * @param int $type
	 * @param array $limitArr 格式array('begin'=>0,'num'=>0)
	 * @param string $orderBy 排序方式 默认值：id desc
	 * @param string $code 需要的字段 默认值：*
	 * @return array
	 */
	protected static function _getAllList($type, $limitArr, $orderBy = "id desc", $code = '*') {
		$sql = "select $code from " . self::_TableName ( $type ) . " order by $orderBy";
		return self::_getDataList ( $sql, $limitArr );
	}
	
	/**
	 * 获取指定SQL语句的记录
	 *
	 * @param string $sql SQL语句
	 * @param array $limitArr 包含两项值limit数组 格式array('begin'=>0,'num'=>0)
	 * @return array
	 */
	protected static function _getDataList($sql, $limitArr) {
		global $conn;
		// echo $sql.'<br />';
		$rs = null;
		$isLimit = false;
		$dataList = array ();
		if (intval ( $limitArr ['begin'] ) === 0 && intval ( $limitArr ['num'] ) === 0) {
			$rs = $conn->Execute ( $sql );
		} else {
			$rs = $conn->SelectLimit ( $sql, $limitArr ['num'], $limitArr ['begin'] );
			$isLimit = true;
		}
		if (! $rs->EOF) {
			$dataList = $rs->GetRows ();
			if ($isLimit) {
				$dataList [0] ['PdAllNum'] = $conn->GetOne ( preg_replace ( '/select(.+?)from/', 'select count(id) from', $sql ) );
			}
		}
		return $dataList;
	}
}

