/* * IMMessage 分表 * AddFriendShip() * if nFromId or nToId is ShopEmployee * GetShopId * Insert into IMMessage_ShopId%8 */ bool CMessageModel::sendMessage(uint32_t nRelateId, uint32_t nFromId, uint32_t nToId, IM::BaseDefine::MsgType nMsgType, uint32_t nCreateTime, uint32_t nMsgId, string& strMsgContent) { bool bRet =false; if (nFromId == 0 || nToId == 0) { log("invalied userId.%u->%u", nFromId, nToId); return bRet; } CDBManager* pDBManager = CDBManager::getInstance(); CDBConn* pDBConn = pDBManager->GetDBConn("teamtalk_master"); if (pDBConn) { string strTableName = "IMMessage_" + int2string(nRelateId % 8); string strSql = "insert into " + strTableName + " (`relateId`, `fromId`, `toId`, `msgId`, `content`, `status`, `type`, `created`, `updated`) values(?, ?, ?, ?, ?, ?, ?, ?, ?)"; // 必须在释放连接前delete CPrepareStatement对象,否则有可能多个线程操作mysql对象,会crash CPrepareStatement* pStmt = new CPrepareStatement(); if (pStmt->Init(pDBConn->GetMysql(), strSql)) { uint32_t nStatus = 0; uint32_t nType = nMsgType; uint32_t index = 0; pStmt->SetParam(index++, nRelateId); pStmt->SetParam(index++, nFromId); pStmt->SetParam(index++, nToId); pStmt->SetParam(index++, nMsgId); pStmt->SetParam(index++, strMsgContent); pStmt->SetParam(index++, nStatus); pStmt->SetParam(index++, nType); pStmt->SetParam(index++, nCreateTime); pStmt->SetParam(index++, nCreateTime); bRet = pStmt->ExecuteUpdate(); } delete pStmt; pDBManager->RelDBConn(pDBConn); if (bRet) { uint32_t nNow = (uint32_t) time(NULL); incMsgCount(nFromId, nToId); } else { log("insert message failed: %s", strSql.c_str()); } } else { log("no db connection for teamtalk_master"); } return bRet; }
bool CGroupModel::insertNewGroup(uint32_t nUserId, const string& strGroupName, const string& strGroupAvatar, uint32_t nGroupType, uint32_t nMemberCnt, uint32_t& nGroupId) { bool bRet = false; nGroupId = INVALID_VALUE; CDBManager* pDBManager = CDBManager::getInstance(); CDBConn* pDBConn = pDBManager->GetDBConn("dffxIMDB_master"); if (pDBConn) { string strSql = "insert into IMGroup(`name`, `avatar`, `creator`, `type`,`userCnt`, `status`, `version`, `lastChated`, `updated`, `created`) "\ "values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; CPrepareStatement* pStmt = new CPrepareStatement(); if (pStmt->Init(pDBConn->GetMysql(), strSql)) { uint32_t nCreated = (uint32_t)time(NULL); uint32_t index = 0; uint32_t nStatus = 0; uint32_t nVersion = 1; uint32_t nLastChat = 0; pStmt->SetParam(index++, strGroupName); pStmt->SetParam(index++, strGroupAvatar); pStmt->SetParam(index++, nUserId); pStmt->SetParam(index++, nGroupType); pStmt->SetParam(index++, nMemberCnt); pStmt->SetParam(index++, nStatus); pStmt->SetParam(index++, nVersion); pStmt->SetParam(index++, nLastChat); pStmt->SetParam(index++, nCreated); pStmt->SetParam(index++, nCreated); bRet = pStmt->ExecuteUpdate(); if(bRet) { nGroupId = pStmt->GetInsertId(); } } delete pStmt; pDBManager->RelDBConn(pDBConn); } else { log("no db connection for dffxIMDB_master"); } return bRet; }
bool CGroupModel::insertNewMember(uint32_t nGroupId, set<uint32_t>& setUsers) { bool bRet = false; uint32_t nUserCnt = (uint32_t)setUsers.size(); if(nGroupId != INVALID_VALUE && nUserCnt > 0) { CDBManager* pDBManager = CDBManager::getInstance(); CDBConn* pDBConn = pDBManager->GetDBConn("dffxIMDB_slave"); if (pDBConn) { uint32_t nCreated = (uint32_t)time(NULL); // 获取 已经存在群里的用户 string strClause; bool bFirst = true; for (auto it=setUsers.begin(); it!=setUsers.end(); ++it) { if(bFirst) { bFirst = false; strClause = int2string(*it); } else { strClause += ("," + int2string(*it)); } } string strSql = "select userId from IMGroupMember where groupId=" + int2string(nGroupId) + " and userId in (" + strClause + ")"; CResultSet* pResultSet = pDBConn->ExecuteQuery(strSql.c_str()); set<uint32_t> setHasUser; if(pResultSet) { while (pResultSet->Next()) { setHasUser.insert(pResultSet->GetInt("userId")); } pResultSet->Clear(); } else { log("no result for sql:%s", strSql.c_str()); } pDBManager->RelDBConn(pDBConn); pDBConn = pDBManager->GetDBConn("dffxIMDB_master"); if (pDBConn) { CacheManager* pCacheManager = CacheManager::getInstance(); CacheConn* pCacheConn = pCacheManager->GetCacheConn("group_member"); if (pCacheConn) { // 设置已经存在群中人的状态 if (!setHasUser.empty()) { strClause.clear(); bFirst = true; for (auto it=setHasUser.begin(); it!=setHasUser.end(); ++it) { if(bFirst) { bFirst = false; strClause = int2string(*it); } else { strClause += ("," + int2string(*it)); } } strSql = "update IMGroupMember set status=0, updated="+int2string(nCreated)+" where groupId=" + int2string(nGroupId) + " and userId in (" + strClause + ")"; pDBConn->ExecuteUpdate(strSql.c_str()); } strSql = "insert into IMGroupMember(`groupId`, `userId`, `status`, `created`, `updated`) values\ (?,?,?,?,?)"; //插入新成员 auto it = setUsers.begin(); uint32_t nStatus = 0; uint32_t nIncMemberCnt = 0; for (;it != setUsers.end();) { uint32_t nUserId = *it; if(setHasUser.find(nUserId) == setHasUser.end()) { CPrepareStatement* pStmt = new CPrepareStatement(); if (pStmt->Init(pDBConn->GetMysql(), strSql)) { uint32_t index = 0; pStmt->SetParam(index++, nGroupId); pStmt->SetParam(index++, nUserId); pStmt->SetParam(index++, nStatus); pStmt->SetParam(index++, nCreated); pStmt->SetParam(index++, nCreated); pStmt->ExecuteUpdate(); ++nIncMemberCnt; delete pStmt; } else { setUsers.erase(it++); delete pStmt; continue; } } ++it; } if(nIncMemberCnt != 0) { strSql = "update IMGroup set userCnt=userCnt+" + int2string(nIncMemberCnt) + " where id="+int2string(nGroupId); pDBConn->ExecuteUpdate(strSql.c_str()); } //更新一份到redis中 string strKey = "group_member_"+int2string(nGroupId); for(auto it = setUsers.begin(); it!=setUsers.end(); ++it) { pCacheConn->hset(strKey, int2string(*it), int2string(nCreated)); } pCacheManager->RelCacheConn(pCacheConn); bRet = true; } else { log("no cache connection"); } pDBManager->RelDBConn(pDBConn); } else {