<?php
! defined ( 'P_W' ) && exit ( 'Forbidden' );
/**
 * 用戶信息緩存類，包含對如下表的緩存 pw_members, pw_memberdata, pw_memberInfo, pw_memberCredit, pw_singleRight
 *
 */
class GatherCache_PW_Members_Cache extends GatherCache_Base_Cache {
	var $_defaultCache = PW_CACHE_MEMCACHE;
	var $_prefix = 'member_';

	var $_membersField = array ('uid' => null, 'username' => null, 'password' => null, 'safecv' => null, 'email' => null, 'groupid' => null, 'memberid' => null, 'groups' => null, 'icon' => null, 'gender' => null, 'regdate' => null, 'signature' => null, 'introduce' => null, 'oicq' => null, 'aliww' => null, 'icq' => null, 'msn' => null, 'yahoo' => null, 'site' => null, 'location' => null, 'honor' => null, 'bday' => null, 'lastaddrst' => null, 'yz' => null, 'timedf' => null, 'style' => null, 'datefm' => null, 't_num' => null, 'p_num' => null, 'attach' => null, 'hack' => null, 'newpm' => null, 'banpm' => null, 'msggroups' => null, 'medals' => null, 'userstatus' => null, 'shortcut' => null );
	var $_memberDataField = array ('uid' => null, 'postnum' => null, 'digests' => null, 'rvrc' => null, 'money' => null, 'credit' => null, 'currency' => null, 'lastvisit' => null, 'thisvisit' => null, 'lastpost' => null, 'onlinetime' => null, 'monoltime' => null, 'todaypost' => null, 'monthpost' => null, 'uploadtime' => null, 'uploadnum' => null, 'follows' => null, 'fans' => null, 'newfans' => null, 'newreferto' => null, 'newcomment' => null, 'onlineip' => null, 'starttime' => null, 'postcheck' => null, 'pwdctime' => null, 'f_num' => null, 'creditpop' => null, 'jobnum' => null, 'lastmsg' => null, 'lastgrab' => null, 'punch' => null,'newnotice' => null, 'newrequest' => null );
	var $_memberInfoField = array ('uid' => null, 'adsips' => null, 'credit' => null, 'deposit' => null, 'startdate' => null, 'ddeposit' => null, 'dstartdate' => null, 'regreason' => null, 'readmsg' => null, 'delmsg' => null, 'tooltime' => null, 'replyinfo' => null, 'lasttime' => null, 'digtid' => null, 'customdata' => null, 'tradeinfo' => null );
	var $_singleRightField = array ('uid' => null, 'visit' => null, 'post' => null, 'reply' => null );

	/**
	 * 獲取一條members表信息
	 *
	 * @param int $userId
	 * @return array
	 */
	function getMembersByUserId($userId) {
		$userId = S::int ( $userId );
		if ($userId < 1) {
			return false;
		}
		$key = $this->_getMembersKey ( $userId );
		$result = $this->_cacheService->get ( $key );
		if ($result === false) {
			$result = $this->_getMembersByUserIdNoCache ( $userId );
			$result = $result ? $result : array();
			$this->_cacheService->set ( $key,  $result);
		}
		return $result;
	}

	/**
	 * 獲取一條MemberData信息
	 *
	 * @param int $userId
	 * @return array
	 */
	function getMemberDataByUserId($userId) {
		$userId = S::int ( $userId );
		if ($userId < 1) {
			return false;
		}
		$key = $this->_getMemberDataKey ( $userId );
		$result = $this->_cacheService->get ( $key );
		if ($result === false) {
			$result = $this->_getMemberDataByUserIdNoCache ( $userId );
			$result = $result ? $result : array();
			$this->_cacheService->set ( $key, $result );
		}
		return $result;
	}

	/**
	 * 獲取一條MemberInfo信息
	 *
	 * @param int $userId
	 * @return array
	 */
	function getMemberInfoByUserId($userId) {
		$userId = S::int ( $userId );
		if ($userId < 1) {
			return false;
		}
		$key = $this->_getMemberInfoKey ( $userId );
		$result = $this->_cacheService->get ( $key );
		if ($result === false ) {
			$result = $this->_getMemberInfoByUserIdNoCache ( $userId );
			$result = $result ? $result : array();
			$this->_cacheService->set ( $key, $result );
		}
		return $result;
	}

	/**
	 * 獲取一條SingleRight信息
	 *
	 * @param int $userId
	 * @return array
	 */
	function getSingleRightByUserId($userId) {
		$userId = S::int ( $userId );
		if ($userId < 1 ) {
			return false;
		}
		$key = $this->_getSingleRightKey ( $userId );
		$result = $this->_cacheService->get ( $key );
		if ($result === false){
			$result = $this->_getSingleRightByUserIdNoCache ( $userId );
			$result = $result ? $result : array();
			$this->_cacheService->set ( $key, $result );
		}
		return $result;
	}

	/**
	 * 批量獲取一組Members信息
	 *
	 * @param array $userIds
	 * @return array
	 */
	function getMembersByUserIds($userIds) {
		if (! S::isArray ( $userIds )) {
			return false;
		}
		$userIds = array_unique ( $userIds );
		$result = $_tmpResult = $keys = $_tmpUserIds = array ();	
		foreach ( $userIds as $userId ) {
			$keys [$this->_getMembersKey ( $userId )] = $userId;
		}
		if (($members = $this->_cacheService->get ( array_keys($keys) ))) {
			$_unique = $this->getUnique();
			foreach ($keys as $key=>$userId){
				$_key = $_unique . $key;
				if (isset($members[$_key]) && is_array($members[$_key])){
					$_tmpUserIds [] = $userId;
					$result[$userId] = $members[$_key];
				}
			}
		}		
		$userIds = array_diff ( $userIds, $_tmpUserIds );
		if ($userIds) {
			$_tmpResult = $this->_getMembersByUserIdsNoCache ( $userIds );
			foreach ($userIds as $userId){
				$this->_cacheService->set ( $this->_getMembersKey ( $userId ), isset($_tmpResult[$userId]) ? $_tmpResult[$userId] : array() );
			}
		}			
		return  (array)$result + (array)$_tmpResult;
	}

	/**
	 * 批量獲取一組MemberData信息
	 *
	 * @param array $userIds
	 * @return array
	 */
	function getMemberDataByUserIds($userIds) {
		if (! S::isArray ( $userIds )) {
			return false;
		}
		$userIds = array_unique ( $userIds );
		$result = $_tmpResult = $keys = $_tmpUserIds = array ();
		foreach ( $userIds as $userId ) {
			$keys [$this->_getMemberDataKey ( $userId )] = $userId;
		}
		if (($members = $this->_cacheService->get ( array_keys($keys) ))) {
			$_unique = $this->getUnique();
			foreach ($keys as $key=>$userId){
				$_key = $_unique . $key;
				if (isset($members[$_key]) && is_array($members[$_key])){
					$_tmpUserIds [] = $userId;
					$result[$userId] = $members[$_key];
				}
			}
		}
		$userIds = array_diff ( $userIds, $_tmpUserIds );
		if ($userIds) {
			$_tmpResult = $this->_getMemberDataByUserIdsNoCache ( $userIds );
			foreach ($userIds as $userId){
				$this->_cacheService->set ( $this->_getMemberDataKey ( $userId ), isset($_tmpResult[$userId]) ? $_tmpResult[$userId] : array() );
			}
		}		
		
		return (array)$result + (array)$_tmpResult;
	}

	/**
	 * 批量獲取一組MemberInfo信息
	 *
	 * @param array $userIds
	 * @return array
	 */
	function getMemberInfoByUserIds($userIds) {
		if (! S::isArray ( $userIds )) {
			return false;
		}
		$userIds = array_unique ( $userIds );
		$result = $_tmpResult = $keys = $_tmpUserIds = array ();
		foreach ( $userIds as $userId ) {
			$keys [$this->_getMemberInfoKey ( $userId )] = $userId;
		}
		if (($members = $this->_cacheService->get ( array_keys($keys) ))) {
			$_unique = $this->getUnique();
			foreach ($keys as $key=>$userId){
				$_key = $_unique . $key;
				if (isset($members[$_key]) && is_array($members[$_key])){
					$_tmpUserIds [] = $userId;
					$result[$userId] = $members[$_key];
				}
			}
		}		
		$userIds = array_diff ( $userIds, $_tmpUserIds );
		if ($userIds) {
			$_tmpResult = $this->_getMemberInfoByUserIdsNoCache ( $userIds );
			foreach ($userIds as $userId){
				$this->_cacheService->set ( $this->_getMemberInfoKey ( $userId ), isset($_tmpResult[$userId]) ? $_tmpResult[$userId] : array() );
			}
		}			
		return (array)$result + (array)$_tmpResult;
	}

	/**
	 * 批量獲取一組MemberCredit信息
	 *
	 * @param array $userIds
	 * @return array
	 */
	function getMemberCreditByUserIds($userIds) {
		if (! S::isArray ( $userIds )) {
			return false;
		}
		$userIds = array_unique ( $userIds );
		$result = $_tmpResult = $keys = $_tmpUserIds = array ();
		foreach ( $userIds as $userId ) {
			$keys [$this->_getMemberCreditKey ( $userId )] = $userId;
		}
		if (($members = $this->_cacheService->get ( array_keys($keys) ))) {
			$_unique = $this->getUnique();
			foreach ($keys as $key=>$userId){
				$_key = $_unique . $key;
				if (isset($members[$_key]) && is_array($members[$_key])){
					$_tmpUserIds [] = $userId;
					$result[$userId] = $members[$_key];
				}
			}
		}
		$userIds = array_diff ( $userIds, $_tmpUserIds );
		if ($userIds) {
			$_tmpResult = $this->_getMemberCreditByUserIdsNoCache ( $userIds );
			foreach ($userIds as $userId){
				$this->_cacheService->set ( $this->_getMemberCreditKey ( $userId ), isset($_tmpResult[$userId]) ? $_tmpResult[$userId] : array() );
			}
		}

		return (array)$result + (array)$_tmpResult;
	}

	/**
	 * 批量獲取用戶群組信息， 僅供read.php在獲取用戶信息時調用
	 *
	 * @param array $userIds
	 * @return array
	 */
	function getCmemberAndColonyByUserIds($userIds) {
		if (! S::isArray ( $userIds )) {
			return false;
		}
		$userIds = array_unique ( $userIds );
		$result = $_tmpResult = $keys = $_tmpUserIds = array ();
		foreach ( $userIds as $userId ) {
			$keys [$this->_getCmemberAndColonyKey ( $userId )] = $userId;
		}
		if (($members = $this->_cacheService->get ( array_keys($keys) ))) {
			$_unique = $this->getUnique();
			foreach ($keys as $key=>$userId){
				$_key = $_unique . $key;
				if (isset($members[$_key]) && is_array($members[$_key])){
					$_tmpUserIds [] = $userId;
					$result[$userId] = $members[$_key];
				}
			}
		}
		$userIds = array_diff ( $userIds, $_tmpUserIds );
		if ($userIds) {
			$_tmpResult = $this->_getCmemberAndColonyByUserIdsNoCache ( $userIds );
			foreach ($userIds as $userId){
				$this->_cacheService->set ( $this->_getCmemberAndColonyKey ( $userId ), isset($_tmpResult[$userId]) ? $_tmpResult[$userId] : array() );
			}
		}		
		return (array)$result + (array)$_tmpResult;
	}

	/**
	 * 獲取一條用戶基本信息和Data信息和SingleRight信息，僅供global.php里的getUserByUid函數調用
	 * 實現了這條sql語句 "SELECT m.*, md.*, sr.* FROM pw_members m LEFT JOIN pw_memberdata md ON m.uid=md.uid LEFT JOIN pw_singleright sr ON m.uid=sr.uid WHERE m.uid=" . S::sqlEscape($uid) . " AND m.groupid<>'0' AND md.uid IS NOT NULL"
	 * 
	 * @param int $userId
	 * @return array
	 */
	/**
	function getMembersAndMemberDataAndSingleRightByUserId($userId) {
		$userId = S::int ( $userId );
		if ($userId < 1) return false;
		$membersAndMemberData = $this->getAllByUserId($userId, true, true);
		$singleRight = $this->getSingleRightByUserId($userId);
		return  (array)$membersAndMemberData + ($singleRight ? (array)$singleRight : $this->_singleRightField);
	}
	**/

	/**
	 * 根據一個用戶id獲取用戶名
	 * 
	 * @param int $userId 用戶id
	 * @return string
	 */
	function getUserNameByUserId($userId) {
		$userId = S::int ( $userId );
		if ($userId < 1) return false;
		$result = $this->getMembersByUserId ( $userId );
		return $result ? $result['username'] : false;
	}

	/**
	 * 根據用戶id批量獲取用戶名
	 * 
	 * @param array $userIds 用戶id數組
	 * @return array 以uid為key，用戶名為值的數組
	 */
	function getUserNameByUserIds($userIds) {
		if (! S::isArray ( $userIds )) {
			return false;
		}
		if (!($members = $this->getMembersByUserIds($userIds))) return false;
		$_userNames = array ();
		foreach ( $members as $member ) {
			$_userNames [$member ['uid']] = $member ['username'];
		}
		return $_userNames;
	}

	/**
	 * 獲取用戶信息
	 *
	 * @param int $userId 用戶ID
	 * @param bool $isMembers 是否取用戶主要信息
	 * @param bool $isMemberData 是否取用戶基本信息
	 * @param bool $isMemberInfo 是否取用戶相關信息
	 * @return array|boolean
	 */
	
	function getAllFieldByUserId($userId, $isMembers = true, $isMemberData = false, $isMemberInfo = false) {
		$userId = S::int($userId);
		if ($userId < 1) return false;
		$members = $isMembers ? $this->getMembersByUserId($userId) : false;
		$memberData = $isMemberData ? $this->getMemberDataByUserId($userId) : false;
		$memberInfo = $isMemberInfo ? $this->getMemberInfoByUserId($userId) : false;
		return $this->_joinTables(array($isMembers, $isMemberData, $isMemberInfo), array($members, $memberData, $memberInfo));
	}
	
	
	/**
	 * 從緩存中批量獲取用戶基本信息，Data信息，Info信息
	 *
	 * @param array $userIds
	 * @return array
	 */
	
	function getAllFieldByUserIds($userIds, $isMembers = true, $isMemberData = false, $isMemberInfo = false) {
		if (! S::isArray ( $userIds )) return false;
		$arrMembers = $isMembers ? $this->getMembersByUserIds($userIds) : array();
		$arrMemberData = $isMemberData ? $this->getMemberDataByUserIds($userIds) : array();
		$arrMemberInfo = $isMemberInfo ? $this->getMemberInfoByUserIds($userIds) : array();
		$result = array();
		foreach ($userIds as $userId){
			$isMembers && $members = isset ( $arrMembers [$userId]) ?  $arrMembers [$userId] : false;
			$isMemberData && $memberData = isset ( $arrMemberData [$userId]) ?  $arrMemberData [$userId] : false;
			$isMemberInfo && $memberInfo = isset ( $arrMemberInfo [$userId]) ?  $arrMemberInfo [$userId] : false;
			$tmp = $this->_joinTables(array($isMembers, $isMemberData, $isMemberInfo), array($members, $memberData, $memberInfo));
			$tmp && $result [$userId] = $tmp;
		}
		return $result;
	}
	
	
	/**
	 * 模擬數據庫left join的效果
	 *
	 * @param array $tables  需要查詢的表 array(true, false, true)
	 * @param array $values  對應上面需要查詢的表 array($result1, false, $result3)
	 * @return array
	 */
	function _joinTables($tables, $values){
		$tableField = array($this->_membersField, $this->_memberDataField, $this->_memberInfoField);
		$tableAlias = array('m.', 'md.', 'mi.');
		$first = false;
		$result = array();
		foreach ($tables as $k => $table){
			if (!$first && $table){
				if (!$values[$k]) return false;
				$first = true;
			}
			if ($first){
				!$values[$k] && $values[$k] = $tableField[$k];
				$values[$k][$tableAlias[$k]. 'uid'] = $values[$k]['uid'];
				if (isset($result['credit'])) {
					$values[$k]['creditinfo'] = $values[$k]['credit'];
					$values[$k][$tableAlias[$k]. 'credit'] = $values[$k]['credit'];
				}
				(!isset($result['credit']) && $table && $values[$k]['credit']) && $result['credit'] = $values[$k]['credit'];
				$result += $values[$k];
			}
		}
		return $first ? $result : false;
	}

	/**
	 * 清除用戶基本信息緩存
	 *
	 * @param array $userIds
	 */
	function clearCacheForMembersByUserIds($userIds) {
		$userIds = ( array ) $userIds;
		foreach ( $userIds as $uid ) {
			$this->_cacheService->delete ( $this->_getAllMembersKey ( $uid ) );
			$this->_cacheService->delete ( $this->_getMembersKey ( $uid ) );
		}
		return true;
	}

	/**
	 * 清除用戶Data信息緩存
	 *
	 * @param array $userIds
	 */
	function clearCacheForMemberDataByUserIds($userIds) {
		$userIds = ( array ) $userIds;
		foreach ( $userIds as $uid ) {
			$this->_cacheService->delete ( $this->_getAllMembersKey ( $uid ) );
			$this->_cacheService->delete ( $this->_getMemberDataKey ( $uid ) );
		}
		return true;
	}

	/**
	 * 清除用戶Info信息緩存
	 *
	 * @param array $userIds
	 */
	function clearCacheForMemberInfoByUserIds($userIds) {
		$userIds = ( array ) $userIds;
		foreach ( $userIds as $uid ) {
			$this->_cacheService->delete ( $this->_getAllMembersKey ( $uid ) );
			$this->_cacheService->delete ( $this->_getMemberInfoKey ( $uid ) );
		}
		return true;
	}

	/**
	 * 清除用戶的SingleRight信息
	 *
	 * @param array $userIds
	 */
	function clearCacheForSingleRightByUserIds($userIds) {
		$userIds = ( array ) $userIds;
		foreach ( $userIds as $uid ) {
			$this->_cacheService->delete ( $this->_getSingleRightKey ( $uid ) );
		}
		return true;
	}

	function clearCacheForMemberCreditByUserIds($userIds) {
		$userIds = ( array ) $userIds;
		foreach ( $userIds as $uid ) {
			$this->_cacheService->delete ( $this->_getMemberCreditKey ( $uid ) );
		}
		return true;
	}

	function clearCacheForCmemberAndColonyByUserIds($userIds) {
		$userIds = ( array ) $userIds;
		foreach ( $userIds as $uid ) {
			$this->_cacheService->delete ( $this->_getCmemberAndColonyKey ( $uid ) );
		}
		return true;
	}

	function clearCacheForMemberTagsByUserIds($userIds) {
		$userIds = ( array ) $userIds;
		foreach ( $userIds as $uid ) {
			$this->_cacheService->delete ( $this->_getMemberTagsKey ( $uid ) );
		}
		return true;
	}
	
	/**
	 * 不通過緩存直接從數據庫獲取一組用戶基本信息
	 *
	 * @param array $userIds 用戶id數組
	 * @return array
	 */
	function _getMembersByUserIdsNoCache($userIds) {
		if (! S::isArray ( $userIds )) return false;
		$membersDb = L::loadDB ( 'Members', 'user' );
		return $membersDb->getUsersByUserIds ( $userIds );
	}

	/**
	 * 不通過緩存直接從數據庫獲取一組用戶的Data信息
	 *
	 * @param array $userIds 用戶id數組
	 * @return array
	 */
	function _getMemberDataByUserIdsNoCache($userIds) {
		if (! S::isArray ( $userIds )) return false;
		$memberDataDb = L::loadDB ( 'MemberData', 'user' );
		return $memberDataDb->getUsersByUserIds ( $userIds );
	}

	/**
	 * 不通過緩存直接從數據庫獲取一組用戶的Info信息
	 *
	 * @param array $userIds 用戶id數組
	 * @return array
	 */
	function _getMemberInfoByUserIdsNoCache($userIds) {
		if (! S::isArray ( $userIds )) return false;
		$memberInfoDb = L::loadDB ( 'MemberInfo', 'user' );
		return $memberInfoDb->getUsersByUserIds ( $userIds );
	}

	/**
	 * 不同過緩存直接從數據庫獲取一條用戶基本信息
	 *
	 * @param int $userId 用戶id
	 * @return array
	 */
	function _getMembersByUserIdNoCache($userId) {
		$userId = S::int ( $userId );
		if ($userId < 1) return false;
		$membersDb = L::loadDB ( 'Members', 'user' );
		return $membersDb->get ( $userId );
	}

	/**
	 * 不同過緩存直接從數據庫獲取一條用戶Data信息
	 *
	 * @param int $userId 用戶id
	 * @return array
	 */
	function _getMemberDataByUserIdNoCache($userId) {
		$userId = S::int ( $userId );
		if ($userId < 1) return false;
		$memberDataDb = L::loadDB ( 'MemberData', 'user' );
		return $memberDataDb->get ( $userId );
	}

	/**
	 * 不同過緩存直接從數據庫獲取一條用戶Info信息
	 *
	 * @param int $userId 用戶id
	 * @return array
	 */
	function _getMemberInfoByUserIdNoCache($userId) {
		$userId = S::int ( $userId );
		if ($userId < 1) return false;
		$memberInfoDb = L::loadDB ( 'MemberInfo', 'user' );
		return $memberInfoDb->get ( $userId );
	}

	/**
	 * 不通過緩存從數據庫獲取一條用戶權限信息, 即查詢pw_singleRight表
	 *
	 * @param int $userId
	 * @return array
	 */
	function _getSingleRightByUserIdNoCache($userId) {
		$userId = S::int ( $userId );
		if ($userId < 1) return false;
		$singleRightDb = L::loadDB ( 'SingleRight', 'user' );
		return $singleRightDb->get ( $userId );
	}

	/**
	 * 從數據庫獲取一組MemberCredit數據
	 *
	 * @param array $userIds
	 * @return array
	 */
	function _getMemberCreditByUserIdsNoCache($userIds) {
		if (!S::isArray($userIds)) return false;
		$memberCreditDb = L::loadDB ( 'MemberCredit', 'user' );
		$memberCredits = $memberCreditDb->gets ( $userIds );
		if (! S::isArray ( $memberCredits )) return false;
		$result = array ();
		foreach ( $memberCredits as $mc ) {
			$result [$mc ['uid']] [$mc ['cid']] = $mc ['value'];
			$result[$mc ['uid']]['uid'] = $mc ['uid'];
		}
		return $result;
	}

	/**
	 * 不通過緩存直接從數據庫獲取用戶群組信息， 需要連表查詢pw_cmembers和pw_colonys
	 *
	 * @param int $threadId 帖子id
	 * @return array
	 */
	function _getCmemberAndColonyByUserIdsNoCache($userIds) {
		$cmembersDb = L::loadDB ( 'cmembers', 'colony' );
		return $cmembersDb->getsCmemberAndColonyByUserIds ( $userIds );
	}

	/**
	 * 獲取用戶基本信息的緩存key
	 *
	 * @param int $userId 用戶id
	 * @return string
	 */
	function _getMembersKey($userId) {
		return $this->_prefix . 'main_uid_' . $userId;
	}

	/**
	 * 獲取用戶Data信息的緩存key
	 *
	 * @param int $userId 用戶id
	 * @return string
	 */
	function _getMemberDataKey($userId) {
		return $this->_prefix . 'data_uid_' . $userId;
	}

	/**
	 * 獲取用戶Info信息的緩存key
	 *
	 * @param int $userId 用戶id
	 * @return string
	 */
	function _getMemberInfoKey($userId) {
		return $this->_prefix . 'info_uid_' . $userId;
	}

	/**
	 * 獲取SingleRight表緩存key
	 *
	 * @param int $userId
	 * @return string
	 */
	function _getSingleRightKey($userId) {
		return $this->_prefix . 'singleright_uid_' . $userId;
	}

	/**
	 * 獲取MemberCredit表緩存key
	 *
	 * @param int $userId
	 * @return string
	 */
	function _getMemberCreditKey($userId) {
		return $this->_prefix . 'credit_uid_' . $userId;
	}

	/**
	 * 獲會員群組信息在memcache緩存的key
	 *
	 * @param int $userId 用戶id
	 * @return string
	 */
	function _getCmemberAndColonyKey($userId) {
		return $this->_prefix . 'colony_uid_' . $userId;
	}

	/**
	 * 獲取用戶標簽在memcache緩存的key
	 *
	 * @param int $userId 用戶id
	 * @return string
	 */
	function _getMemberTagsKey($userId) {
		return $this->_prefix . 'membertag_uid_' . $userId;
	}
	
	
	/************************ 分隔符**********************************/

	function _getMembersAndMemberDataAndMemberInfoByUserIdsNoCache($userIds){
		global $customfield;
		$fieldinfo = '';
		if (is_array($customfield)) {
			foreach ($customfield as $value) {
				!$value['ifsys'] && $fieldinfo .= ',mi.field_'.(int)$value['id'];
			}
		}
		$membersDb = L::loadDB ( 'Members', 'user' );
		return $membersDb->getMembersAndMemberDataAndMemberInfoByUserIds ( $userIds,$fieldinfo );
	}
	function _getAllMembersKey($userId){
		return $this->_prefix . 'all_uid_' . $userId;
	}
	
	/**
	 * 獲取一組用戶信息
	 * 查詢members,memberData, memberInfo三張表的部分字段， 僅供global.php, read.php頁面里特定地方調用
	 *
	 * @param array $userIds
	 * @param unknown_type $a 備用
	 * @param unknown_type $b 備用
	 * @param unknown_type $c 備用
	 * @return array
	 */
	function getAllByUserIds($userIds, $a=false, $b=false, $c=false){
		$userIds = array_unique ( (array)$userIds );
		$result = $_tmpResult = $keys = $_tmpUserIds = array ();
		foreach ( $userIds as $userId ) {
			$keys [$this->_getAllMembersKey ( $userId )] = $userId;
		}
		if (($members = $this->_cacheService->get ( array_keys($keys) ))) {
			$_unique = $this->getUnique();
			foreach ($keys as $key=>$userId){
				$_key = $_unique . $key;
				if (isset($members[$_key]) && is_array($members[$_key])){
					$_tmpUserIds [] = $userId;
					$result[$userId] = $members[$_key];
				}
			}
		}
		$userIds = array_diff ( $userIds, $_tmpUserIds );
		if ($userIds) {
			$_tmpResult = $this->_getMembersAndMemberDataAndMemberInfoByUserIdsNoCache ( $userIds );
			foreach ($userIds as $userId){
				$this->_cacheService->set ( $this->_getAllMembersKey ( $userId ), isset($_tmpResult[$userId]) ? $_tmpResult[$userId] : array() );
			}
		}		
		return (array)$result + (array)$_tmpResult;		
	}
	
	/**
	 * 獲取一條用戶信息
	 * 查詢members,memberData, memberInfo三張表的部分字段， 僅供global.php, read.php頁面里特定地方調用
	 *
	 * @param array $userIds
	 * @param unknown_type $a 備用
	 * @param unknown_type $b 備用
	 * @param unknown_type $c 備用
	 * @return array
	 */	
	function getAllByUserId($userId, $a=false, $b=false, $c=false){
		$userId = S::int($userId);
		if ($userId < 1) return false;
		$members = $this->getAllByUserIds($userId);
		return $members ? current($members) : array();
	}
	
	/**
	 * 獲取一條用戶信息
	 * 查詢members,memberData, singleRight三張表的部分字段， 僅供global.php頁面里特定地方調用
	 *
	 * @param array $userIds
	 * @return array
	 */		
	function getMembersAndMemberDataAndSingleRightByUserId($userId) {
		$userId = S::int ( $userId );
		if ($userId < 1) return false;
		$membersAndMemberData = $this->getAllByUserId($userId);
		if (!$membersAndMemberData) return array();
		$singleRight = $this->getSingleRightByUserId($userId);
		return  (array)$membersAndMemberData + ($singleRight ? (array)$singleRight : $this->_singleRightField);
	}	
		
	/**
	 * 根據一個用戶id獲取用戶標簽
	 * 
	 * @param int $uid 用戶id
	 * @return array
	 */
	function getMemberTagsByUserid($userId) {
		$userId = S::int ( $userId );
		if ($userId < 1) {
			return false;
		}
		$key = $this->_getMemberTagsKey ( $userId );
		$result = $this->_cacheService->get ( $key );
		if ($result === false) {
			$memberTagsService = L::loadClass('memberTagsService', 'user');
			$result = $memberTagsService->getMemberTagsByUidFromDB($userId);
			$result = $result ? $result : array();
			$this->_cacheService->set ( $key,  $result);
		}
		return $result;
	}
}