account.cpp
12 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#include <lib_err_code.h>
#include "func_rount.h"
#include "account.h"
#include "role.h"
account_t::account_t(el::lib_mysql_if* db)
: el::lib_mysql_table_route1x1(db, g_gen_db_name("USER"), "t_account")
{
}
int account_t::check_pltform_account_existed( uint32_t platform,
const std::string& account_name,
USER_ID& uid)
{
GEN_MYSQL_STRING(account_name_mysql_string, account_name.c_str(),
get_valid_len(account_name.c_str(), ACCOUNT_MAX_LEN));
GEN_SQLSTR(this->sqlstr,"select uid,three from %s where account='%s' and platform=%u",
this->get_table_name(),
account_name_mysql_string, platform);
STD_QUERY_ONE_BEGIN(this->sqlstr, el::ERR_DB::KEY_INEXIST);
USER_ID t_uid = 0;
GET_FIELD_UINT64(t_uid);
uid = t_uid;
uint32_t t_three = 0;
GET_FIELD_UINT64(t_three);
if (0 != t_three){
return -1;
}
STD_QUERY_ONE_END();
return SUCC;
}
// uint32_t account_t::is_nick_existed( const std::string& nick )
// {
// GEN_MYSQL_STRING(nick_mysql_string, nick.c_str(),
// get_valid_len(nick.c_str(), NICK_LEN));
//
// GEN_SQLSTR(this->sqlstr, "select uid from %s where nick = '%s' ",
// this->get_table_name(),
// nick_mysql_string);
//
// my_ulonglong row_count = 0;
// STD_QUERY_AFFECTED_ROWS(this->sqlstr, row_count);
// if (row_count > 0){
// return el::ERR_DB::KEY_EXIST;
// }
//
// return SUCC;
// }
uint32_t account_t::update_uid_nick(uint32_t platform, const std::string& account, USER_ID uid, const std::string& nick )
{
GEN_MYSQL_STRING(account_mysql_string, account.c_str(),
get_valid_len(account.c_str(), ACCOUNT_MAX_LEN));
GEN_MYSQL_STRING(nick_escape_string, nick.c_str(),
get_valid_len(nick.c_str(), NICK_LEN));
GEN_SQLSTR(this->sqlstr, "update %s set uid=%" PRIu64 ", nick='%s' where platform=%u and account='%s'"
, this->get_table_name(),
uid, nick_escape_string, platform, account_mysql_string
);
return this->exec_update_sql(this->sqlstr, el::ERR_DB::KEY_INEXIST);
}
uint32_t account_t::get_uid_by_account( uint32_t platform, const std::string& account, USER_ID& uid )
{
GEN_MYSQL_STRING(account_escape_string, account.c_str(),
get_valid_len(account.c_str(), ACCOUNT_MAX_LEN));
GEN_SQLSTR(this->sqlstr, "select uid from %s where platform=%u and account='%s'"
, this->get_table_name(),
platform, account_escape_string
);
STD_QUERY_ONE_BEGIN(this->sqlstr, el::ERR_DB::KEY_INEXIST);
USER_ID tmp_uid = 0;
GET_FIELD_UINT64(tmp_uid);
uid = tmp_uid;
STD_QUERY_ONE_END();
}
int account_t::create_account(uint32_t platform, const std::string& account_name)
{
GEN_MYSQL_STRING(account_mysql_string, account_name.c_str(),
get_valid_len(account_name.c_str(), ACCOUNT_MAX_LEN));
GEN_SQLSTR(this->sqlstr, "insert into %s (platform,account,uid,status,nick,three,u_account)"
" values(%u, '%s', 0, 0, '%s', 0, '%s')",
this->get_table_name(),
platform, account_mysql_string, "", account_mysql_string);
return this->exec_insert_sql(this->sqlstr, el::ERR_DB::KEY_EXIST);
}
int account_t::change_nick(USER_ID uid, const std::string& nick)
{
// this->ret = is_nick_existed(nick);
// if (SUCC != this->ret){
// return el::ERR_DB::KEY_EXIST;
// }
GEN_MYSQL_STRING(str_nick, nick.c_str(), get_valid_len(nick.c_str(), NICK_LEN));
GEN_SQLSTR(this->sqlstr,"update %s set nick='%s' where uid=%" PRIu64 "", this->get_table_name(), str_nick, uid);
this->ret = this->exec_update_sql(this->sqlstr, el::ERR_DB::KEY_INEXIST);
if (SUCC != this->ret){
return el::ERR_DB::KEY_EXIST;
}
this->ret = g_route_func->role.change_nick(uid, nick);
if (SUCC != this->ret) {
this->ret = el::ERR_DB::KEY_EXIST;
}
return this->ret;
}
int account_t::check_pltform_wx_unionid_existed( uint32_t platform, const std::string& wx_unionid, USER_ID& uid )
{
GEN_MYSQL_STRING(wx_unionid_mysql_string, wx_unionid.c_str(),
get_valid_len(wx_unionid.c_str(), ACCOUNT_MAX_LEN));
GEN_SQLSTR(this->sqlstr,"select uid,three from %s where account='%s' and platform=%u",
this->get_table_name(),
wx_unionid_mysql_string, platform);
STD_QUERY_ONE_BEGIN(this->sqlstr, el::ERR_DB::KEY_INEXIST);
USER_ID t_uid = 0;
GET_FIELD_UINT64(t_uid);
uid = t_uid;
uint32_t t_three = 0;
GET_FIELD_UINT64(t_three);
if (1 != t_three){
return -1;
}
STD_QUERY_ONE_END();
return SUCC;
}
int account_t::update_wx_account( uint32_t platform, USER_ID uid, const std::string& wx_unionid, const std::string& wx_nick )
{
GEN_MYSQL_STRING(wx_unionid_escape_string, wx_unionid.c_str(),
get_valid_len(wx_unionid.c_str(), ACCOUNT_MAX_LEN));
GEN_MYSQL_STRING(wx_nick_escape_string, wx_nick.c_str(),
get_valid_len(wx_nick.c_str(), NICK_LEN));
GEN_SQLSTR(this->sqlstr, "update %s set account='%s', nick='%s', three=1 where uid=%" PRIu64,
this->get_table_name(), wx_unionid_escape_string, wx_nick_escape_string, uid);
return this->exec_update_sql(this->sqlstr, el::ERR_DB::KEY_INEXIST);
}
int account_t::create_wx_account( uint32_t platform, const std::string& wx_unionid, const std::string& wx_nick, const std::string& account )
{
GEN_MYSQL_STRING(wx_unionid_mysql_string, wx_unionid.c_str(),
get_valid_len(wx_unionid.c_str(), ACCOUNT_MAX_LEN));
GEN_MYSQL_STRING(wx_nick_escape_string, wx_nick.c_str(),
get_valid_len(wx_nick.c_str(), NICK_LEN));
GEN_MYSQL_STRING(account_mysql_string, account.c_str(),
get_valid_len(account.c_str(), ACCOUNT_MAX_LEN));
GEN_SQLSTR(this->sqlstr, "insert into %s (platform,account,uid,status,nick,three,u_account)"
" values(%u, '%s', 0, 0, '%s', 1, '%s')",
this->get_table_name(),
platform, wx_unionid_mysql_string, wx_nick_escape_string, account_mysql_string);
//////////////////////////////////////////////////////////////////////////
// return this->exec_insert_sql(this->sqlstr, el::ERR_DB::KEY_EXIST);
//////////////////////////////////////////////////////////////////////////
this->ret = this->exec_insert_sql(this->sqlstr, el::ERR_DB::KEY_EXIST);
if (el::ERR_DB::SYS == this->ret){
GEN_SQLSTR(this->sqlstr, "insert into %s (platform,account,uid,status,nick,three,u_account)"
" values(%u, '%s', 0, 0, 'dcyly', 1, '%s')",
this->get_table_name(),
platform, wx_unionid_mysql_string,account_mysql_string);
this->ret = this->exec_insert_sql(this->sqlstr, el::ERR_DB::KEY_EXIST);
}
return this->ret;
}
int account_t::load( USER_ID uid, std::string& nick )
{
GEN_SQLSTR(this->sqlstr, "select nick from %s where uid=%" PRIu64, this->get_table_name(), uid);
STD_QUERY_ONE_BEGIN(this->sqlstr, SUCC);
static char buf[NICK_LEN];
::memset(buf, 0, sizeof(buf));
GET_NEXT_FIELD_BIN(buf, sizeof(buf));
nick = buf;
STD_QUERY_ONE_END_WITHOUT_RETURN();
return SUCC;
}
int account_t::check_uid_existed( USER_ID uid )
{
GEN_SQLSTR(this->sqlstr, "select uid from %s where uid=%" PRIu64,
this->get_table_name(), uid);
STD_QUERY_ONE_BEGIN(this->sqlstr, el::ERR_DB::KEY_INEXIST);
STD_QUERY_ONE_END();
return SUCC;
}
int account_t::load_account( USER_ID uid, std::string& account )
{
GEN_SQLSTR(this->sqlstr, "select account from %s where uid=%" PRIu64, this->get_table_name(), uid);
STD_QUERY_ONE_BEGIN(this->sqlstr, SUCC);
static char buf[ACCOUNT_MAX_LEN];
::memset(buf, 0, sizeof(buf));
GET_NEXT_FIELD_BIN(buf, sizeof(buf));
account = buf;
STD_QUERY_ONE_END_WITHOUT_RETURN();
return SUCC;
}
uint32_t account_t::gen_gateway_id( uint32_t platform, std::string account, uint32_t gateway_cnt )
{
std::string str_gateway_key;
str_gateway_key += "platform=" + el::convert_to_string(platform);
str_gateway_key += "account=" + account;
return el::g_hash_str(str_gateway_key.c_str())%gateway_cnt + 1;
}
int account_t::get_random( db_msg::random_friend_msg_res* out )
{
GEN_SQLSTR(this->sqlstr,"select uid from %s ORDER BY RAND() LIMIT 10", this->get_table_name());
PB_STD_QUERY_WHILE_BEGIN_NO_ADD(this->sqlstr);
USER_ID t_id = 0;
GET_FIELD_UINT64(t_id);
out->add_uid(t_id);
PB_STD_QUERY_WHILE_END();
}
int Cfunc_route::on_change_nick_msg(USER_ID uid, google::protobuf::Message* msg)
{
auto in = (db_msg::change_nick_msg*)msg;
int change_ret = this->account.change_nick(uid, in->nick());
db_msg::change_nick_msg_res out;
if (el::ERR_DB::KEY_EXIST == change_ret){
out.set_res(1);
}
out.set_nick(in->nick());
this->encode_msg2sendbuf(&out);
return this->ret;
}
int Cfunc_route::on_login_msg(USER_ID uid, google::protobuf::Message* msg)
{
auto in = (db_msg::login_msg*)msg;
db_msg::login_msg_res out;
this->ret = this->account.check_pltform_account_existed(in->platform(), in->account(), uid);
if (-1 == this->ret){
return this->ret;
}
if (el::ERR_DB::KEY_INEXIST == this->ret){
this->ret = this->account.create_account(in->platform(), in->account());
if (SUCC != this->ret){
return this->ret;
}
} else if (SUCC != this->ret){
return this->ret;
}
this->head.id = uid;
this->encode_msg2sendbuf(&out);
return this->ret;
}
int Cfunc_route::on_login_wx_msg(USER_ID uid, google::protobuf::Message* msg)
{
auto in = (db_msg::login_wx_msg*)msg;
db_msg::login_wx_msg_res out;
this->ret = this->account.check_pltform_wx_unionid_existed(in->platform(), in->wx_unionid(), uid);
if (-1 == this->ret){
return this->ret;
}
if (el::ERR_DB::KEY_INEXIST == this->ret){
// 新开户微信 用account去查找用户,并标记微信,替换account
int r1 = this->account.check_pltform_account_existed(in->platform(), in->account(), uid);
if (-1 == r1){
return r1;
}
if (el::ERR_DB::KEY_INEXIST == r1){
int r2 = this->account.create_wx_account(in->platform(), in->wx_unionid(), in->wx_nick(), in->account());
if (SUCC != r2){
return r2;
}
this->ret = 0;
} else if (SUCC != r1){
return r1;
} else {
int r3 = this->account.update_wx_account(in->platform(), uid, in->wx_unionid(), in->wx_nick());
if (SUCC != r3){
return r3;
}
this->ret = 0;
}
} else if (SUCC != this->ret){
return this->ret;
}
this->head.id = uid;
this->encode_msg2sendbuf(&out);
return this->ret;
}
int Cfunc_route::on_create_role_msg(USER_ID uid, google::protobuf::Message* msg)
{
auto in = (db_msg::create_role_msg*)msg;
db_msg::create_role_msg_res out;
USER_ID user_id = 0;
this->ret = this->account.get_uid_by_account(in->platform(), in->account(), user_id);
if (this->ret != SUCC){
return share_msg::EC_INEXISTENT_ACCOUNT;
}
if (user_id != 0){
WARN_LOG("role exist %s", in->account().c_str());
return share_msg::EC_EXISTENT_ROLE;
}
this->ret = this->gen_uid(user_id);
if (this->ret != SUCC){
return this->ret;
}
std::string str_nick = in->nick();//el::convert_to_string(user_id);
if (str_nick.empty()){
char c_nick[NICK_LEN];
::memset(c_nick,0,NICK_LEN);
::sprintf(c_nick, "%s%06u", "豆豆", (uint32_t)user_id%1000000);
str_nick = c_nick;
}
// this->ret = this->account.is_nick_existed(str_nick);
// if (this->ret != SUCC){
// return this->ret;
// }
this->ret = this->account.update_uid_nick(in->platform(), in->account(), user_id, str_nick);
if (this->ret != SUCC){
return this->ret;
}
this->ret = this->role.create(user_id, in->head(), str_nick);
if (this->ret != SUCC){
return this->ret;
}
{
FOREACH_PB(in->add_items, idx){
const share_msg::item_t& item = in->add_items(idx);
this->ret = this->item.update(user_id, item.id(), item.cnt());
if (this->ret != SUCC){
return this->ret;
}
}
}
{
FOREACH_PB(in->add_events, idx){
const common_msg::event_t& event = in->add_events(idx);
this->ret = this->user_event.update(user_id, event.type(), event.id(), event.data(),
event.time(), event.str_data(), event.bin_data());
if (this->ret != SUCC){
return this->ret;
}
}
}
this->head.id = user_id;
this->encode_msg2sendbuf(&out);
return this->ret;
}
int Cfunc_route::on_random_friend_msg(USER_ID uid, google::protobuf::Message* msg)
{
db_msg::random_friend_msg_res out;
this->account.get_random(&out);
this->encode_msg2sendbuf(&out);
return this->ret;
}
int Cfunc_route::on_check_uid_msg(USER_ID uid, google::protobuf::Message* msg)
{
db_msg::check_uid_msg_res out;
auto in = (db_msg::check_uid_msg*)msg;
USER_ID peer_uid = in->uid();
this->ret = this->account.check_uid_existed(peer_uid);
if (SUCC != this->ret){
out.set_uid(0);
} else {
out.set_uid(peer_uid);
}
this->encode_msg2sendbuf(&out);
return 0;
}