-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_ReqAddCollect.cpp
More file actions
261 lines (238 loc) · 8.3 KB
/
cmd_ReqAddCollect.cpp
File metadata and controls
261 lines (238 loc) · 8.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include "cmd_ReqAddCollect.h"
void ReqAddCollectCmd::handle(Context* ct)throw (CmdException)
{
SProxy* proxy = ct->process->getSproxy();
m_pLog = proxy->getRollLog();
Connection* pConn = ct->connection;
m_pLog->debug("ReqAddCollectCmd::handle begining........");
c2sCmd::C2SReqAddCollectBody c2s;
s2cCmd::S2CAddCollectBody s2c;
s2c.res = ERROR_CODE::EC_Succeed;
int seqno = -1;
bool isNoExcept = false;
try
{
ProtocolMgr& pack = *(ct->pack);
seqno = pack.m_header.seqno;
m_zip = pack.m_header.zip ;
msgpack::object obj = pack.m_pUnpackBody->get();
obj.convert(&c2s);
int teamId = c2s.tid;
int userId = c2s.uid;
s2c.uid = userId;
s2c.tid = teamId;
m_pLog->debug("ReqAddCollectCmd::handle begining........minfo.size()[%d]",c2s.minfo.size() );
if( c2s.minfo.size() <= 0 || c2s.minfo.size() > 20 || teamId <= 0 || userId <= 0)
{
s2c.res = ERROR_CODE::EC_INVALID_INPUT;
s2c.err = ERROR_CODE::EC_INVALID_INPUT_DES;
m_pLog->error("ReqAddCollectCmd: EC_INVALID_INPUT! minfo.size:%d" ,c2s.minfo.size() );
sendResp(pConn , s2c , ct->puid , seqno ) ;
return ;
}
for(int i = 0; i < c2s.minfo.size(); i++)
{
if( c2s.minfo[i].sid <= 0 || c2s.minfo[i].toid <= 0 || c2s.minfo[i].mid <= 0 || c2s.minfo[i].stp < 0 || c2s.minfo[i].mtp < 0)
{
s2c.res = ERROR_CODE::EC_INVALID_INPUT;
s2c.err = ERROR_CODE::EC_INVALID_INPUT_DES;
m_pLog->error("ReqAddCollectCmd: EC_INVALID_INPUT! minfo");
sendResp(pConn , s2c , ct->puid , seqno ) ;
return ;
}
if( c2s.minfo[i].mtp == COMMON_DEF::MSGTYPE_SYS || c2s.minfo[i].mtp == COMMON_DEF::MSGTYPE_INVITED2TEAM || \
c2s.minfo[i].mtp == COMMON_DEF::MSGTYPE_INVITED2GRP || c2s.minfo[i].mtp == COMMON_DEF::MSGTYPE_SYSDESMSG || \
c2s.minfo[i].mtp == COMMON_DEF::MSGTYPE_RECALL)
{
s2c.res = ERROR_CODE::EC_INVALID_INPUT;
s2c.err = ERROR_CODE::EC_INVALID_INPUT_DES;
m_pLog->error("ReqAddCollectCmd: EC_INVALID_INPUT! minfo.mtp");
sendResp(pConn , s2c , ct->puid , seqno ) ;
return ;
}
//查询是否已收藏
string strSql = "select * from " + TBL_COLLECT + " where userId = '" + base::i2s(userId) + "' and teamId = '" + base::i2s(teamId) \
+ "' and fromId = '" + base::i2s(c2s.minfo[i].sid) + "' and toId = '" + base::i2s(c2s.minfo[i].toid) + "' and msgId = '" \
+ base::i2s(c2s.minfo[i].mid) + "' and clttype <> '" + base::i2s(COMMON_DEF::COLLECT_DELETE) + "' and status <> '" \
+ base::i2s(COMMON_DEF::COLLECT_DELETE) + "' ;";
base::MySqlData data = proxy->m_ar_dbMgr.query_sql(strSql.c_str());
int rowNum = data.num_rows();
//已有收藏
if(rowNum > 0)
{
bool hasSame = false;
string strNow = c2s.minfo[i].msg;
for(int j = 0; j < rowNum; j++)
{
string strSrc = data[j]["content"];
if(strSrc.compare(strNow) == 0)
{
hasSame = true;
break;
}
}
if(hasSame == true)
{
s2cCmd::CollectMsgInfoObj cinfo;
cinfo.cid = base::s2i(data[0]["cltId"]);
cinfo.sid = base::s2i(data[0]["fromId"]);
cinfo.nm = data[0]["fromName"];
cinfo.avt = data[0]["fromAvatar"];
cinfo.toid = base::s2i(data[0]["toId"]);
cinfo.mid = base::s2i(data[0]["msgId"]);
cinfo.stp = base::s2i(data[0]["stype"]);
cinfo.msg = data[0]["content"];
cinfo.param = data[0]["param"];
cinfo.mtp = base::s2i(data[0]["type"]);
cinfo.ctp = base::s2i(data[0]["clttype"]);
cinfo.cstat = base::s2i(data[0]["status"]);
cinfo.ct = base::s2l(data[0]["created"]);
s2c.cinfo.push_back(cinfo);
continue;
}
}
//获取用户名,头像
string strTmUsrTbl = TBL_TEAMUSER + "_" + base::i2s(teamId % 10);
QueryParam paraTmUsr;
paraTmUsr.tableName = strTmUsrTbl;
paraTmUsr.keyFieldName = "teamId";
paraTmUsr.key = teamId;
paraTmUsr.userIdFiledName = "userId";
paraTmUsr.userid = c2s.minfo[i].sid;
BsonDataVector resTmUsrInfo;
proxy->m_ar_dbMgr.get(paraTmUsr, resTmUsrInfo);
string userName = "";
string userAvatar = "";
if (resTmUsrInfo.size() > 0)
{
userName = resTmUsrInfo[0].getStringField("name");
userAvatar = resTmUsrInfo[0].getStringField("avatar");
}
//分配收藏资源id
int cltKeyId = proxy->m_sessionMgr.getCltKeyIdByUidTid(userId, teamId);
m_pLog->debug("ReqAddCollectCmd::userId[%d] teamId[%d] cltKeyId[%d]",userId, teamId, cltKeyId);
if(cltKeyId == 0)
{
cltKeyId = proxy->m_idMgr.dispatchCollectKeyId();
m_pLog->debug("ReqAddCollectCmd::dispatch userId[%d] teamId[%d] cltKeyId[%d]",userId, teamId, cltKeyId);
//维护该map
proxy->m_sessionMgr.setCltKeyIdByUidTid(userId, teamId, cltKeyId);
}
//分配收藏id
int cltId = proxy->m_idMgr.dispatchCollectMaxCtlId(cltKeyId, teamId, userId);
int curTime = (int)time(NULL);
int mtype = c2s.minfo[i].mtp;
/*if(c2s.minfo[i].mtp == COMMON_DEF::MSGTYPE_TEXT)
{
//if(common_utils::is_link_valid(c2s.minfo[i].msg))
if(proxy->m_chatMgr.linkVecFind(c2s.minfo[i].msg))
{
mtype = 99;
}
}*/
//不需要存redis
QueryParam paraCt;
paraCt.tableName = TBL_COLLECT;
bson::bo boGm = BSON("cltId" << cltId \
<< "cltKeyId" << cltKeyId \
<< "stype" << c2s.minfo[i].stp \
<< "userId" << userId \
<< "toId" << c2s.minfo[i].toid \
<< "msgId" << c2s.minfo[i].mid \
<< "fromId" << c2s.minfo[i].sid \
<< "fromName" << userName \
<< "fromAvatar" << userAvatar \
<< "content" << c2s.minfo[i].msg \
<< "param" << c2s.minfo[i].param \
<< "teamId" << teamId \
<< "type" << mtype \
<< "clttype" << COMMON_DEF::COLLECT_NORMAL \
<< "status" << COMMON_DEF::COLLECT_NORMAL \
<< "updated" << curTime \
<< "created" << curTime );
proxy->m_ar_dbMgr.save_db_shm(boGm,paraCt, DBOPER_ADD);
s2cCmd::CollectMsgInfoObj cinfo;
cinfo.cid = cltId;
cinfo.sid = c2s.minfo[i].sid;
cinfo.nm = userName;
cinfo.avt = userAvatar;
cinfo.toid = c2s.minfo[i].toid;
cinfo.mid = c2s.minfo[i].mid;
cinfo.stp = c2s.minfo[i].stp;
cinfo.msg = c2s.minfo[i].msg;
cinfo.param = c2s.minfo[i].param;
cinfo.mtp = mtype;
cinfo.ctp = COMMON_DEF::COLLECT_NORMAL;
cinfo.cstat = COMMON_DEF::COLLECT_NORMAL;
cinfo.ct = curTime;
s2c.cinfo.push_back(cinfo);
}
sendResp(pConn , s2c , ct->puid , seqno ) ;
isNoExcept = true;
}
catch (std::string& e)
{
m_pLog->error("ReqAddCollectCmd:string error ,%s",e.c_str());
}
catch (msgpack::type_error& e)
{
m_pLog->error("ReqAddCollectCmd:type_error ,%s",e.what());
}
catch (socket_error& e)
{
m_pLog->error("ReqAddCollectCmd:socket_error ,%s",e.what());
}
catch (...)
{
m_pLog->error("ReqAddCollectCmd:unknowed excp");
}
//有异常也要给客户端回应
if(!isNoExcept)
{
s2c.res = ERROR_CODE::EC_UNKNOWED_ERROR;
s2c.err = ERROR_CODE::EC_UNKNOW_ERR_DES;
sendResp(pConn , s2c , ct->puid , seqno ) ;
}
m_pLog->debug("ReqAddCollectCmd::handle ending........");
}
void ReqAddCollectCmd::sendResp(Connection* pConn, s2cCmd::S2CAddCollectBody& s2c, int puid, int seqno)
{
try
{
ProtocolMgr packSent;
packSent.m_header.stx = 0x09;
packSent.m_header.cmd = Cmd::RspAddCollect;
packSent.m_header.seqno = seqno ;
packSent.m_header.ext = puid;
packSent.m_header.zip = m_zip ;
packSent.m_pEncoder->pack(s2c);
char buf[1024*10]={'\0'};
int nLen = 1024*10;
packSent.setWebSocketSupport(pConn->isWebSocket());
packSent.encode(buf,nLen);
pConn->sendMessage(buf,nLen);
//--
ProtocolMgr proMgr;
proMgr.decode(buf,nLen,(ProtocolMgr::PackType)1);
msgpack::object objSnd = proMgr.m_pUnpackBody->get();
std::ostringstream osSnd("");
osSnd << objSnd;
m_pLog->debug("ReqAddCollectCmd::sendResp cmd:%d,body:%s, fd:%d, puid:%d",proMgr.m_header.cmd, osSnd.str().c_str(),pConn->fd(), puid);
}
catch (std::string& e)
{
m_pLog->error("ReqAddCollectCmd::sendResp:string error ,%s",e.c_str());
}
catch (msgpack::type_error& e)
{
m_pLog->error("ReqAddCollectCmd::sendResp:type_error ,%s",e.what());
}
catch (socket_error& e)
{
m_pLog->error("ReqAddCollectCmd::sendResp:socket_error ,%s",e.what());
}
catch (...)
{
m_pLog->error("ReqAddCollectCmd::sendResp:unknowed excp");
}
}