<?php
/**
 * 链接逻辑层
 *
 * @author Wzb
 *        
 */
class BLL_Link {
	/**
	 * 添加一个链接
	 *
	 * @param array $theArray
	 * @return int
	 */
	public static function add($theArray) {
		$newArray = array ();
		$newArray = COM_OftenFunction::makeArray ( $theArray );
		$newArray ['serialNum'] = 0;
		$newArray ['sort'] = intval ( $theArray ['theId'] );
		return DAL_Link::add ( $newArray );
	}
	
	/**
	 * 修改一个链接
	 *
	 * @param array $theArray
	 * @return int
	 */
	public static function edit($theArray) {
		$newArray = COM_OftenFunction::makeArray ( $theArray );
		$newArray ['id'] = intval ( $theArray ['theId'] );
		return DAL_Link::edit ( $newArray );
	}
	
	/**
	 * 删除指定条链接
	 *
	 * @param string $idList 多个必须用-分割开
	 * @return string string error1 id字符串为空|error2 删除失败|ok 删除成功
	 */
	public static function delete($idList) {
		if (empty ( $idList )) {
			return 'error1';
		}
		if (DAL_Link::delete ( $idList )) {
			return 'ok';
		} else {
			return 'error2';
		}
	}
	
	/**
	 * 获取一条链接
	 *
	 * @param int $id
	 * @param string $code 需要的字段 默认值：*
	 * @return array
	 */
	public static function getOne($id, $code = '*') {
		return DAL_Link::getOne ( intval ( $id ), $code );
	}
	
	/**
	 * 获取链接列表
	 *
	 * @param string $sortIdList 类别ID字符串,多个使用逗号分割
	 * @param bool $children 是否查找子类 默认值：false
	 * @param string $code 需要的字段 默认值：*
	 * @param array $limitArr 默认值：array('begin'=>0,'num'=>0)
	 * @return array
	 */
	public static function getList($sortIdList, $children = false, $code = '*', $limitArr = array('begin'=>0,'num'=>0)) {
		if (empty ( $sortIdList )) {
			return array ();
		}
		
		if ($children) {
			$idtempArray = explode ( ",", $sortIdList );
			$allSortList = BLL_LinkSort::getList ( 0, 'id,pid,serialNum' );
			$sortArray = array ();
			foreach ( $idtempArray as $value ) {
				$sortArray = array_merge ( $sortArray, BLL_LinkSort::getList ( intval ( $value ), 'id,pid,serialNum', 10, $allSortList, false ) );
			}
			for($i = 0; $i < count ( $sortArray ); $i ++) {
				$sortIdList .= "," . $sortArray [$i] ['id'];
			}
		}
		
		return DAL_Link::getList ( implode ( ',', array_unique ( explode ( ',', $sortIdList ) ) ), $code, $limitArr );
	}
	
	/**
	 * 搜索列表
	 *
	 * @param string $sortIdList 类别ID，多个用逗号分割
	 * @param array $whereArray 搜索条件(type,name,value) type:likeString|=String|=Int|>Int|<Int
	 * @param bool $children 是否查找子类 默认值：false
	 * @param string $code 需要的字段 默认值：*
	 * @param array $limitArr 默认值：array('begin'=>0,'num'=>0)
	 * @return array
	 */
	public static function getSerachList($sortIdList, $whereArray, $children = false, $code = '*', $limitArr = array('begin'=>0,'num'=>0)) {
		if (empty ( $sortIdList )) {
			return array ();
		}
		if ($children) {
			$idtempArray = explode ( ",", $sortIdList );
			$allSortList = BLL_LinkSort::getList ( 0, 'id,pid,serialNum' );
			$sortArray = array ();
			foreach ( $idtempArray as $value ) {
				$sortArray = array_merge ( $sortArray, BLL_LinkSort::getList ( intval ( $value ), 'id,pid,serialNum', 10, $allSortList, false ) );
			}
			for($i = 0; $i < count ( $sortArray ); $i ++) {
				$sortIdList .= "," . $sortArray [$i] ['id'];
			}
		}
		return DAL_Link::getSerachList ( implode ( ',', array_unique ( explode ( ',', $sortIdList ) ) ), $whereArray, $code, $limitArr );
	}
	
	/**
	 * 交换两个类别的序号
	 *
	 * @param string $idStr
	 */
	public static function swapSerial($idStr) {
		$theArray = explode ( "-", $idStr );
		for($i = 0; $i < 4; $i ++) {
			$theArray [$i] = intval ( $theArray [$i] );
		}
		DAL_Link::edit ( array ("id" => $theArray [0],"serialNum" => $theArray [3] ) );
		DAL_Link::edit ( array ("id" => $theArray [1],"serialNum" => $theArray [2] ) );
	}
	
	/**
	 * 获取某类别下的链接数量
	 *
	 * @param int $id 类别ID
	 * @return int
	 */
	public static function getCount($id) {
		return DAL_Link::getCount ( intval ( $id ) );
	}
	
	/**
	 * 获取一组链接的上一条与下一条
	 *
	 * @param array $theList 需要排序的链接的二维数组
	 * @return array
	 */
	public static function getPreNext($theList) {
		if (count ( $theList ) == 0) {
			return array ();
		}
		$preObj = DAL_Link::getOnePreNext ( $theList [0] ['sort'], $theList [0] ['serialNum'], "pre", 'id,serialNum' );
		$nextObj = DAL_Link::getOnePreNext ( $theList [count ( $theList ) - 1] ['sort'], $theList [count ( $theList ) - 1] ['serialNum'], "next", 'id,serialNum' );
		for($i = 0; $i < count ( $theList ); $i ++) {
			if ($i == 0) {
				if (count ( $preObj ) > 0) {
					$theList [$i] ['preStr'] = $theList [$i] ['id'] . '-' . $preObj ['id'] . '-' . $theList [$i] ['serialNum'] . '-' . $preObj ['serialNum'];
				} else {
					$theList [$i] ['preStr'] = '';
				}
			} else {
				$theList [$i] ['preStr'] = $theList [$i] ['id'] . '-' . $theList [$i - 1] ['id'] . '-' . $theList [$i] ['serialNum'] . '-' . $theList [$i - 1] ['serialNum'];
			}
			if ($i == count ( $theList ) - 1) {
				if (count ( $nextObj ) > 0) {
					$theList [$i] ['nextStr'] = $theList [$i] ['id'] . '-' . $nextObj ['id'] . '-' . $theList [$i] ['serialNum'] . '-' . $nextObj ['serialNum'];
				} else {
					$theList [$i] ['nextStr'] = '';
				}
			} else {
				$theList [$i] ['nextStr'] = $theList [$i] ['id'] . '-' . $theList [$i + 1] ['id'] . '-' . $theList [$i] ['serialNum'] . '-' . $theList [$i + 1] ['serialNum'];
			}
		}
		return $theList;
	}
	
	/**
	 * 获取一个链接对象的上一篇与下一篇
	 *
	 * @param array $theArray
	 * @param string $code 需要的字段 默认值：*
	 */
	public static function getOnePreNext($theArray, $code = '*') {
		return array (DAL_Link::getOnePreNext ( $theArray ['sort'], $theArray ['serialNum'], "pre", $code ),DAL_Link::getOnePreNext ( $theArray ['sort'], $theArray ['serialNum'], "next", $code ) );
	}
}
?>