mail.cpp
3.55 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
#include <lib_err_code.h>
#include "func_rount.h"
#include "mail.h"
int Cfunc_route::on_add_mail_msg(USER_ID uid, google::protobuf::Message* msg)
{
auto in = (db_msg::add_mail_msg*)msg;
this->ret = this->mail.add_mail(uid, in->mutable_mail());
return this->ret;
}
int mail_t::add_mail(USER_ID uid, share_msg::mail_t* in)
{
//附件
GEN_MYSQL_STRING(attachement_escape_string, in->attachment().c_str(),
get_valid_len(in->attachment().c_str(), MAIL_ATTACHMENT_MAX_LEN));
GEN_MYSQL_STRING(str_bin_escape_string, in->bin_data().c_str(),
min(in->bin_data().size(), MAIL_BIN_DATA_MAX_LEN));
EVENT_DATA idx = in->idx();
if (0 == idx){
// 获取idx 并 ++ 更新 idx
g_route_func->user_event.get_user_data(uid, common_msg::FOREVER_EVENT_IDX, 0, idx);
idx++;
g_route_func->user_event.update(uid, common_msg::FOREVER_EVENT_IDX, 0, idx, 0, "");
}
GEN_SQLSTR(this->sqlstr, "insert into %s (idx,uid,state,mail_id,attachment,bin_data,time) "
" values(%" PRIu64 ", %" PRIu64 ", %u, %u, '%s', '%s', %u)"
, this->get_table_name(uid),
idx, uid, in->state(), in->mail_id(),
attachement_escape_string, str_bin_escape_string, in->time());
return this->exec_update_sql(this->sqlstr, el::ERR_DB::SYS);
}
int Cfunc_route::on_update_mail_state_msg(USER_ID uid, google::protobuf::Message* msg)
{
auto in = (db_msg::update_mail_state_msg*)msg;
share_msg::mail_t* mail = in->mutable_mail();
this->ret = this->mail.update_mail(uid, mail->idx(), mail->state(), mail->time());
return this->ret;
}
int mail_t::update_mail(USER_ID uid, uint64_t idx, uint32_t state, uint32_t time)
{
GEN_SQLSTR(this->sqlstr, "update %s set state=%u, time=%u where idx=%" PRIu64 " and uid=%" PRIu64
, this->get_table_name(uid),
state, time, idx, uid
);
return this->exec_update_sql(this->sqlstr, el::ERR_DB::KEY_INEXIST);
}
int Cfunc_route::on_del_mail_msg(USER_ID uid, google::protobuf::Message* msg)
{
auto in = (db_msg::del_mail_msg*)msg;
this->ret = this->mail.del_mail(uid, in->idx());
return this->ret;
}
int mail_t::del_mail(USER_ID uid, uint64_t idx)
{
GEN_SQLSTR(this->sqlstr, "delete from %s where idx=%" PRIu64 " and uid=%" PRIu64
, this->get_table_name(uid),
idx, uid
);
return this->exec_update_sql(this->sqlstr, el::ERR_DB::KEY_INEXIST);
}
//////////////////////////////////////////////////////////////////////////
mail_t::mail_t(el::lib_mysql_if* db) : el::lib_mysql_table_route10x10(db, g_gen_db_name("USER"), "t_mail")
{
}
int mail_t::get(USER_ID uid, db_msg::load_user_msg_res* out)
{
this->del_exp_daily_mail(uid);
GEN_SQLSTR(this->sqlstr,"select idx,state,mail_id,attachment,bin_data,time "
" from %s where uid=%" PRIu64,
this->get_table_name(uid),
uid);
PB_STD_QUERY_WHILE_BEGIN(this->sqlstr, out, add_mails);
uint64_t idx = 0;
GET_FIELD_UINT64(idx);
member->set_idx(idx);
uint32_t state = 0;
GET_FIELD_UINT64(state);
member->set_state(state);
uint32_t mail_id = 0;
GET_FIELD_UINT64(mail_id);
member->set_mail_id(mail_id);
std::string t_attachment;
GET_NEXT_FIELD_STRING16(t_attachment, MAIL_ATTACHMENT_MAX_LEN);
member->set_attachment(t_attachment);
std::string t_bin_data;
GET_NEXT_FIELD_STRING16(t_bin_data, MAIL_BIN_DATA_MAX_LEN);
member->set_bin_data(t_bin_data);
uint32_t time = 0;
GET_FIELD_UINT64(time);
member->set_time(time);
PB_STD_QUERY_WHILE_END();
}
void mail_t::del_exp_daily_mail( USER_ID uid )
{
GEN_SQLSTR(this->sqlstr, "delete from %s where uid=%" PRIu64 " and time<=%u and time <> 0"
, this->get_table_name(uid), uid, el_async::get_now_sec()
);
this->exec_delete_sql(this->sqlstr, el::ERR_DB::KEY_INEXIST);
}