//新增加帐号
void CAccountDBAddHandler::AddAccount(unsigned uiSessionFd, const AccountID& stAccountID, int iWorldID, const std::string& strPasswd)
{
    static GameProtocolMsg stMsg;

    //生成消息头
    GenerateMsgHead(&stMsg, uiSessionFd, MSGID_ACCOUNTDB_ADDACCOUNT_REQUEST, GetAccountHash(stAccountID.straccount()));

    //AccountDB插入新帐号的请求
    AccountDB_AddAccount_Request* pstReq = stMsg.mutable_m_stmsgbody()->mutable_m_staccountdb_addaccount_request();
    pstReq->mutable_staccountid()->CopyFrom(stAccountID);
    pstReq->set_iworldid(iWorldID);

    //加密密码
    char szEncryptPasswd[256] = {0};
    int iEncryptBuffLen = sizeof(szEncryptPasswd);

    int iRet = CPasswordEncryptionUtility::DoPasswordEncryption(strPasswd.c_str(), strPasswd.size(), szEncryptPasswd, iEncryptBuffLen);
    if(iRet)
    {
        TRACESVR("Failed to encrypt account password, account: %s, password: %s\n", stAccountID.straccount().c_str(), strPasswd.c_str());
        return;
    }

    //设置密码为加密后的密码
    pstReq->set_strpassword(szEncryptPasswd, iEncryptBuffLen);

    //转发消息给AccountDBServer
    if(EncodeAndSendCode(SSProtocolEngine, NULL, &stMsg, GAME_SERVER_ACCOUNTDB) != 0)
    {
        TRACESVR("Failed to send add account request to Account DB server\n");
        return;
    }

    LOGDEBUG("Send add account request to Account DB server\n");

    return;
}
//拉取返回帐号详细信息
int CFetchAccountHandler::FetchAccountInfo(const AccountID& stAccountID, const std::string& strPassword, AccountDB_FetchAccount_Response& rstResp)
{
    //设置连接的DB
    const ONEACCOUNTDBINFO* pstDBConfig = (CAccountDBApp::m_stAccountDBConfigManager).GetOneAccountDBInfoByIndex(m_iThreadIdx);
    if(!pstDBConfig)
    {
        TRACE_THREAD(m_iThreadIdx, "Failed to get account db config, index %d\n", m_iThreadIdx);
        return -1;
    }

    int iRet = m_pDatabase->SetMysqlDBInfo(pstDBConfig->szDBHost, pstDBConfig->szUserName, pstDBConfig->szUserPasswd, pstDBConfig->szDBName);
    if(iRet)
    {
        TRACE_THREAD(m_iThreadIdx, "Failed to set mysql db info, ret %d\n", iRet);
        return iRet;
    }

    char* pszQueryString = m_szQueryString[m_iThreadIdx];
    int iLength = SAFE_SPRINTF(pszQueryString, sizeof(m_szQueryString[m_iThreadIdx])-1, "select * from %s where accountID='%s' and accountType=%d", 
                 MYSQL_ACCOUNTINFO_TABLE, stAccountID.straccount().c_str(), stAccountID.iaccounttype());

    iRet = m_pDatabase->ExecuteRealQuery(pszQueryString, iLength, true);
    if(iRet)
    {
        TRACE_THREAD(m_iThreadIdx, "Failed to execute sql query, ret %d\n", iRet);
        return iRet;
    }

     //分析结果
    int iRowNum = m_pDatabase->GetNumberRows();
    if(iRowNum != 1)
    {
        TRACE_THREAD(m_iThreadIdx, "Wrong result, invalid rows %d, account %s\n", iRowNum, stAccountID.straccount().c_str());
        return T_ACCOUNTDB_INVALID_RECORD;
    }

    MYSQL_ROW pstResult = NULL;
    unsigned long* pLengths = NULL;
    unsigned int uFields = 0;

    iRet = m_pDatabase->FetchOneRow(pstResult, pLengths, uFields);
    if(iRet)
    {
        TRACE_THREAD(m_iThreadIdx, "Failed to fetch rows, account %s, ret %d\n", stAccountID.straccount().c_str(), iRet);
        return iRet;
    }

    //判断uFields是否相符
    if(uFields != MYSQL_ACCOUNTINFO_FIELDS)
    {
        TRACE_THREAD(m_iThreadIdx, "Wrong result, real fields %u, needed %u\n", uFields, MYSQL_ACCOUNTINFO_FIELDS);
        return T_ACCOUNTDB_INVALID_RECORD;
    }

    //字段3是password
    std::string strDBPassword(pstResult[3], pLengths[3]);

    //检查密码是否一致
    if(strDBPassword.compare(strPassword) != 0)
    {
        return T_ACCOUNTDB_INVALID_RECORD;
    }

    //从结果中解析需要的字段
    rstResp.mutable_staccountid()->CopyFrom(stAccountID);

    //字段0是accountID, 字段1是accountType, 跳过

    //字段2是uin
    rstResp.set_uin(strtoul(pstResult[2],NULL,10));

    //字段4是lastWorldID
    rstResp.set_iworldid(atoi(pstResult[4]));

    //字段5是activeState
    rstResp.set_bisbinded(atoi(pstResult[5]));

    return T_SERVER_SUCESS;
}