<?php
/**
 * 链接类别逻辑层
 *
 * @author Wzb
 *        
 */
class BLL_LinkSort {
	/**
	 * 添加类别
	 *
	 * @param array $theArray
	 * @return int
	 */
	public static function add($theArray) {
		$newArray = array ();
		$newArray = COM_OftenFunction::makeArray ( $theArray );
		$newArray ['pid'] = intval ( $theArray ['theId'] );
		$newArray ['serialNum'] = 0;
		return DAL_LinkSort::add ( $newArray );
	}
	
	/**
	 * 修改类别
	 *
	 * @param array $theArray
	 * @return int
	 */
	public static function edit($theArray) {
		$newArray = array ();
		$newArray = COM_OftenFunction::makeArray ( $theArray );
		$newArray ['id'] = intval ( $theArray ['theId'] );
		return DAL_LinkSort::edit ( $newArray );
	}
	
	/**
	 * 删除指定类别
	 *
	 * @param int $id
	 * @return bool
	 */
	public static function delete($id) {
		DAL_Link::delete ( intval ( $id ), 1 );
		return DAL_LinkSort::delete ( intval ( $id ) );
	}
	
	/**
	 * 获取一个类别
	 *
	 * @param int $id
	 * @param string $code 需要的字段 默认值：*
	 * @return array
	 */
	public static function getOne($id, $code = '*') {
		return DAL_LinkSort::getOne ( intval ( $id ), $code );
	}
	
	/**
	 * 获取指定的类别
	 *
	 * @param int $pid 父类ID
	 * @param string $code 需要的字段(至少要有id,pid,serialNum) 默认值：*
	 * @param int $zindex 深度 1~10 默认值：10
	 * @param array $dataList 需要处理的数据 默认值：array()
	 * @param bool $isFrist 是否为第一次递归 默认值：true
	 * @return array
	 */
	public static function getList($pid, $code = '*', $zindex = 10, $dataList = array(), $isFrist = true) {
		if (count ( $dataList ) == 0 && $isFrist) {
			$dataList = DAL_LinkSort::getAllList ( $code );
		}
		if (count ( $dataList ) == 0) {
			return array ();
		}
		$pid = intval ( $pid );
		$thisList = array_filter ( $dataList, create_function ( '$one', "return intval(\$one['pid'])===$pid;" ) );
		$nextList = array_filter ( $dataList, create_function ( '$one', "return intval(\$one['pid'])!==$pid;" ) );
		$newArray = $thisList;
		foreach ( $thisList as $row ) {
			if ($zindex > 0) {
				$newArray = array_merge ( $newArray, self::getList ( $row ['id'], $code, $zindex - 1, $nextList, false ) );
			}
		}
		usort ( $newArray, create_function ( '$a,$b', "if (\$a['serialNum'] == \$b['serialNum']) {return 0;} return (\$a['serialNum'] < \$b['serialNum']) ? -1 : 1;" ) );
		return $newArray;
	}
	
	/**
	 * 获取上一个类别
	 *
	 * @param int $serialNum 作为对比的序号
	 * @param array $dataList 对比的数据列表 必须包含serialNum
	 * @return array
	 */
	public static function getPre($serialNum, $dataList) {
		if (count ( $dataList ) == 0) {
			return array ();
		}
		$serialNum = intval ( $serialNum );
		$tempList = array_filter ( $dataList, create_function ( '$oneSort', "return \$oneSort['serialNum']<$serialNum;" ) );
		if (count ( $tempList ) == 0) {
			return array ();
		}
		usort ( $tempList, create_function ( '$a,$b', "if (\$a['serialNum'] == \$b['serialNum']) {return 0;} return (\$a['serialNum'] < \$b['serialNum']) ? 1 : -1;" ) );
		return $tempList [0];
	}
	
	/**
	 * 获取下一个类别
	 *
	 * @param int $serialNum 作为对比的序号
	 * @param array $dataList 对比的数据列表 必须包含serialNum
	 * @return array
	 */
	public static function getNext($serialNum, $dataList) {
		if (count ( $dataList ) == 0) {
			return array ();
		}
		$serialNum = intval ( $serialNum );
		$tempList = array_filter ( $dataList, create_function ( '$oneSort', "return \$oneSort['serialNum']>$serialNum;" ) );
		if (count ( $tempList ) == 0) {
			return array ();
		}
		usort ( $tempList, create_function ( '$a,$b', "if (\$a['serialNum'] == \$b['serialNum']) {return 0;} return (\$a['serialNum'] > \$b['serialNum']) ? 1 : -1;" ) );
		return $tempList [0];
	}
	
	/**
	 * 交换两个类别的序号
	 *
	 * @param string $idStr
	 */
	public static function swapSerial($idStr) {
		$theArray = explode ( "-", $idStr );
		for($i = 0; $i < 4; $i ++) {
			$theArray [$i] = intval ( $theArray [$i] );
		}
		DAL_LinkSort::edit ( array ("id" => $theArray [0],"serialNum" => $theArray [3] ) );
		DAL_LinkSort::edit ( array ("id" => $theArray [1],"serialNum" => $theArray [2] ) );
	}
}
?>