void GlobalObjectIndex::removeObject(DWORD thisid)
{
	mlock.lock();
	zEntry *e=getEntryByID(thisid);
	if(e)
		removeEntry(e);
	mlock.unlock();
}
/**
* \brief 增加一个新的社会关系类型
* \param dwID 对端角色id
* \param type 社会关系类型
*/
void CRelationManager::addRelation(const DWORD dwID,const BYTE type)
{
  UserSession *otherUser = NULL;

  otherUser = UserSessionManager::getInstance()->getUserByID(dwID);
  if (otherUser)
  {
    CRelation *relation = NULL;
    relation = (CRelation *)getEntryByID(dwID);
    if (relation)
    {
      if (relation->type == type)
      {
        user->sendSysChat(Cmd::INFO_TYPE_FAIL,"玩家已经在列表中");
      }
      else
      {
        changeRelationType(otherUser->name,type);
        relation->online = true;
      }
    }
    else
    {
      zRTime ctv;
      relation = new CRelation();
      if (relation)
      {
        relation->online = true;
        relation->id = otherUser->id;
        relation->level = 0;
        strncpy(relation->name,otherUser->name,MAX_NAMESIZE);
        relation->type = type;
        relation->lasttime = ctv.sec();
        relation->occupation = otherUser->occupation;

        //if (insertDBRecord(relation))
        //{
          //rwlock.wrlock();
          addEntry(relation);
          //rwlock.unlock();
          insertDBRecord(relation);
          sendStatusChange(relation,Cmd::RELATION_ADD);
          relation->sendNotifyToScene();
        //}
        //else
        //{
        //  SAFE_DELETE(relation);
        //}
      }
    }
  }
  else
  {
    if (user) user->sendSysChat(Cmd::INFO_TYPE_FAIL,"人不在线无法确认");
  }
}
/**
* \brief 在线处理
* \param dwID 上线角色的 id
*/
void CRelationManager::online(const DWORD dwID)
{
  CRelation *relation;
  //rwlock.rdlock();
  relation = (CRelation *)getEntryByID(dwID);
  if (relation)
  {
    relation->online = true;
    UserSession *pUser = UserSessionManager::getInstance()->getUserByID(dwID);    

    if (pUser && relation->type!=Cmd::RELATION_TYPE_BAD)
    {
      if (relation->occupation != pUser->occupation)
      {
        relation->occupation = pUser->occupation;
        this->updateDBRecord(relation);
      }
        
      sendStatusChange(relation,Cmd::RELATION_ONLINE);

      if (this->user)
      {
        switch(relation->type)
        {
          case Cmd::RELATION_TYPE_LOVE:
            {
              this->user->sendSysChat(Cmd::INFO_TYPE_SYS,"配偶 %s 上线了",pUser->name);
            }
            break;
          case Cmd::RELATION_TYPE_BAD:
            {
              this->user->sendSysChat(Cmd::INFO_TYPE_SYS,"坏蛋 %s 上线了",pUser->name);
            }
            break;
          case Cmd::RELATION_TYPE_FRIEND:
            {
              this->user->sendSysChat(Cmd::INFO_TYPE_SYS,"好友 %s 上线了",pUser->name);
            }
            break;
          case Cmd::RELATION_TYPE_ENEMY:
            {
              this->user->sendSysChat(Cmd::INFO_TYPE_SYS,"仇人 %s 上线了",pUser->name);
            }
            break;
          default:
            {
              this->user->sendSysChat(Cmd::INFO_TYPE_SYS,"%s 上线了",pUser->name);
            }
            break;
        }
      }
    }

  }
  //rwlock.unlock();
}
/**
* \brief 下线处理
* \param dwID 下线角色的 id
*/
void CRelationManager::offline(const DWORD dwID,const char* name)
{
  CRelation *relation = NULL;
  //rwlock.rdlock();

  relation = (CRelation *)getEntryByID(dwID);
  
  if (relation)
  {
    if (relation->type!=Cmd::RELATION_TYPE_BAD)
    {
      sendStatusChange(relation,Cmd::RELATION_OFFLINE);

      if (relation->online && this->user)
      {
        switch(relation->type)
        {
          case Cmd::RELATION_TYPE_LOVE:
            {
              this->user->sendSysChat(Cmd::INFO_TYPE_SYS,"配偶 %s 下线了",name);
            }
            break;
          case Cmd::RELATION_TYPE_BAD:
            {
              this->user->sendSysChat(Cmd::INFO_TYPE_SYS,"坏蛋 %s 下线了",name);
            }
            break;
          case Cmd::RELATION_TYPE_FRIEND:
            {
              this->user->sendSysChat(Cmd::INFO_TYPE_SYS,"好友 %s 下线了",name);
            }
            break;
          case Cmd::RELATION_TYPE_ENEMY:
            {
              this->user->sendSysChat(Cmd::INFO_TYPE_SYS,"仇人 %s 下线了",name);
            }
            break;
          default:
            {
              this->user->sendSysChat(Cmd::INFO_TYPE_SYS,"%s 下线了",name);
            }
            break;
        }
      }
    }

    relation->online = false;
  }
  
  
  //rwlock.unlock();
}
/**
* \brief 设置对端友好度,主要用来做双边友好度同步
* \param dwUserID 对端角色id
* \param wdDegree 友好度
* \param currTime 当前时间
*/
void CRelationManager::setFriendDegreeByOtherUser(const DWORD dwUserID,const WORD wdDegree,const DWORD currTime)
{
  CRelation *rel = NULL;

  //rwlock.wrlock();
  rel = (CRelation *)getEntryByID(dwUserID);
  if (rel)
  {
    if (rel->level < wdDegree)
    {
      rel->level = wdDegree;
      rel->lasttime = currTime;
      this->updateDBRecord(rel);
    }
  }
  //rwlock.unlock();
}
bool GlobalObjectIndex::addObject(zObject * o)
{
	bool bret=false;
	if(o)
	{
		mlock.lock();
		zObject *ret =(zObject *)getEntryByID(o->id);
		if(ret)//物品重复处理
		{
			if(ret==o)
			{
				bret=true;
				Zebra::logger->debug("全局物品管理器中发现有重复添加物品(%s,%d)",ret->data.strName,ret->data.qwThisID);
			}
			else if(ret->createtime==o->createtime && ret->data.dwObjectID==o->data.dwObjectID && ret->data.qwThisID == o->data.qwThisID)
			{
				Zebra::logger->debug("复制物品:%s 创建时间:%llu",
						ret->name,ret->createid);
				ret=false;
			}
			else
			{
				Zebra::logger->debug("id冲突%s(%d)", ret->name, ret->id);
				do {
					//重新生成ID
					o->generateThisID();
					bret = addEntry((zEntry *)o);
				} while (!bret);
				//if (!bret) Zebra::logger->fatal("添加物品到物品表失败1");
			}
		}
		else
		{
			bret=addEntry((zEntry *)o);
			if(!bret) Zebra::logger->fatal("添加物品到物品表失败");
		}
		mlock.unlock();
	}else {
		Zebra::logger->fatal("添加非法物品");
	}
	
	return bret;
}
/**
* \brief 设置友好度
* \param rev 友好度设置请求消息
*/
void CRelationManager::setFriendDegree(Cmd::Session::t_CountFriendDegree_SceneSession *rev)
{
  //rwlock.wrlock();
  for (int i=0; i<rev->size; i++)
  {
    CRelation *rel = NULL;
    rel = (CRelation *)getEntryByID(rev->namelist[i].dwUserID);
    if (rel)
    {
#ifdef _DEBUG
      Xlogger->info("设置友好度:设置%s 与 %s 的 %u 友好度为 %d",this->user->name,rel->name,rel->type,rev->namelist[i].wdDegree);
#endif
      if (rel->type == rev->namelist[i].byType)
      {
        if (rel->level < rev->namelist[i].wdDegree)
        {
#ifdef _DEBUG
        Xlogger->info("设置友好度:成功设置");
#endif
          UserSession *otherUser = NULL;
          zRTime ctv;

          rel->level = rev->namelist[i].wdDegree;
          rel->lasttime = ctv.sec();
          this->updateDBRecord(rel);
          otherUser = UserSessionManager::getInstance()->getUserByID(rel->id);
          if (otherUser) 
          {
            otherUser->relationManager.setFriendDegreeByOtherUser(user->id,rel->level,ctv.sec());
          }
          else
          {
            updateOtherOfflineUserDBRecord(rel);
          }
        }
      }
    }
  }
  //rwlock.unlock();
}
/**
* \brief 根据对端关系ID获取关系对象
* \param  dwRelationID 对端关系ID
* \return 简单社会关系对象
*/
CRelation* CRelationManager::getRelationByID(DWORD dwRelationID)
{
  return (CRelation*)getEntryByID(dwRelationID);
}
zObject * UserObjectM::getObjectByThisID( DWORD thisid)
{
	return (zObject *)getEntryByID(thisid);
}
void UserObjectM::removeObjectByThisID(DWORD thisid)
{
	zEntry *e=getEntryByID(thisid);
	if(e)
		removeEntry(e);
}