item.cpp
3.87 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
#include "item.h"
#include "func_rount.h"
namespace {
const char* STR_UID = "uid";
const char* STR_ITEM_ID= "item_id";
const char* STR_CNT = "cnt";
}
item_t::item_t( el::lib_mysql_if* db )
: el::lib_mysql_table_route10x10(db, g_gen_db_name("USER"), "t_item")
{
}
int item_t::update( USER_ID uid, uint32_t item_id, uint32_t cnt )
{
GEN_SQLSTR(this->sqlstr, "insert into %s (%s, %s, %s)"
"values(%" PRIu64 ",%u,%u) on duplicate key update %s=%u",
this->get_table_name(uid),
STR_UID, STR_ITEM_ID, STR_CNT,
uid, item_id, cnt,
STR_CNT, cnt);
return this->exec_update_sqls(this->sqlstr);
}
int item_t::del( USER_ID uid, uint32_t item_id )
{
GEN_SQLSTR(this->sqlstr, "delete from %s where %s=%" PRIu64 " and %s=%u"
, this->get_table_name(uid),
STR_UID, uid,
STR_ITEM_ID, item_id);
return this->exec_delete_sql(this->sqlstr, el::ERR_DB::KEY_INEXIST);
}
int item_t::get( USER_ID uid, db_msg::load_user_msg_res* out )
{
GEN_SQLSTR(this->sqlstr,"select %s,%s from %s where %s=%" PRIu64,
STR_ITEM_ID, STR_CNT,
this->get_table_name(uid),
STR_UID, uid);
PB_STD_QUERY_WHILE_BEGIN(this->sqlstr, out, add_items);
uint32_t t_id = 0;
uint32_t t_cnt = 0;
GET_FIELD_UINT64(t_id);
member->set_id(t_id);
GET_FIELD_UINT64(t_cnt);
member->set_cnt(t_cnt);
PB_STD_QUERY_WHILE_END();
}
int item_t::get_item_cnt( USER_ID uid, uint32_t item_id, uint32_t& cnt )
{
GEN_SQLSTR(this->sqlstr,"select %s from %s where %s=%" PRIu64 " and item_id=%u",
STR_CNT,
this->get_table_name(uid),
STR_UID, uid, item_id);
STD_QUERY_ONE_BEGIN(this->sqlstr, el::ERR_DB::KEY_INEXIST);
uint32_t t_cnt = 0;
GET_FIELD_UINT64(t_cnt);
cnt = t_cnt;
STD_QUERY_ONE_END();
}
int item_t::reduce( USER_ID uid, uint32_t item_id, uint32_t cnt )
{
GEN_SQLSTR(this->sqlstr, "update %s set cnt=cnt-%u where uid=%" PRIu64 " and item_id=%u and cnt >= %u"
, this->get_table_name(uid),
cnt, uid, item_id, cnt);
return this->exec_update_sqls(this->sqlstr);
}
int item_t::add( USER_ID uid, uint32_t item_id, uint32_t cnt )
{
uint32_t old_cnt = 0;
this->get_item_cnt(uid, item_id, old_cnt);
return this->update(uid, item_id, cnt + old_cnt);
// GEN_SQLSTR(this->sqlstr, "update %s set cnt=cnt+%u where uid=%" PRIu64 " and item_id=%u"
// , this->get_table_name(uid),
// cnt, uid, item_id);
// return this->exec_update_sqls(this->sqlstr);
}
int item_t::get_type_cnt( USER_ID uid, uint32_t item_id_b, uint32_t item_id_e, uint32_t& type_cnt )
{
GEN_SQLSTR(this->sqlstr,"select COUNT(*) from %s where %u<=item_id and item_id<%u and %s=%" PRIu64,
this->get_table_name(uid), item_id_b, item_id_e, STR_UID, uid);
STD_QUERY_ONE_BEGIN(this->sqlstr, el::ERR_DB::KEY_INEXIST);
uint32_t v = 0;
GET_FIELD_UINT64(v);
type_cnt = v;
STD_QUERY_ONE_END();
}
//////////////////////////////////////////////////////////////////////////
//
int Cfunc_route::on_update_item_msg(USER_ID uid, google::protobuf::Message* msg)
{
auto in = (db_msg::update_item_msg*)msg;
auto pitem = in->mutable_item();
this->ret = this->item.update(uid, pitem->id(), pitem->cnt());
return this->ret;
}
int Cfunc_route::on_del_item_msg(USER_ID uid, google::protobuf::Message* msg)
{
auto in = (db_msg::del_item_msg*)msg;
auto pitem = in->mutable_item();
this->ret = this->item.del(uid, pitem->id());
return this->ret;
}
int Cfunc_route::on_reduce_item_msg(USER_ID uid, google::protobuf::Message* msg)
{
auto in = (db_msg::reduce_item_msg*)msg;
auto pitem = in->mutable_item();
uint32_t cnt = 0;
this->item.get_item_cnt(uid, pitem->id(), cnt);
if (0 == cnt){
} else if (cnt <= pitem->cnt()){
this->item.del(uid, pitem->id());
} else {
this->item.reduce(uid, pitem->id(), pitem->cnt());
}
return this->ret;
}
int Cfunc_route::on_add_item_msg(USER_ID uid, google::protobuf::Message* msg)
{
auto in = (db_msg::add_item_msg*)msg;
auto pitem = in->mutable_item();
this->ret = this->item.add(uid, pitem->id(), pitem->cnt());
return this->ret;
}