rank.cpp 2.09 KB
#include "rank.h"
#include "func_rount.h"

rank_t::rank_t( el::lib_mysql_if* db )
	: el::lib_mysql_table_route1x1(db, g_gen_db_name("USER"), "t_rank")
{
}

int rank_t::load(db_msg::load_rank_msg_res* out, role_t* role)
{

	GEN_SQLSTR(this->sqlstr, "select type, uid, val, tim from %s",
		this->get_table_name());

	PB_STD_QUERY_WHILE_BEGIN(this->sqlstr, out, add_rank_user);

	uint32_t t_type = 0;
	USER_ID t_uid = 0;
	uint32_t t_val = 0;
	uint32_t t_tim = 0;


	GET_FIELD_UINT64(t_type);
	member->set_type(t_type);

	GET_FIELD_UINT64(t_uid);
	member->mutable_user_show()->set_uid(t_uid);

	GET_FIELD_UINT64(t_val);
	member->set_val(t_val);

	GET_FIELD_UINT64(t_tim);
	member->set_tim(t_tim);

	std::string nick;
	USER_HEAD_ID head;
	role->load(t_uid, nick, head);
	member->mutable_user_show()->set_nick(nick);

	PB_STD_QUERY_WHILE_END();
}

int Cfunc_route::on_load_rank_msg(USER_ID uid, google::protobuf::Message* msg)
{
	db_msg::load_rank_msg_res res;

	this->ret = this->rank.load(&res, &this->role);
	if (SUCC == this->ret) {
		this->encode_msg2sendbuf(&res);
	}

	return this->ret;
}

int rank_t::update(uint32_t type, USER_ID uid, uint32_t val, uint32_t tim)
{
	GEN_SQLSTR(this->sqlstr, "insert into %s (type,uid,val,tim)"
		"values(%u, %" PRIu64 ", %u, %u) on duplicate key update val=%u, tim=%u",
		this->get_table_name(),
		type, uid, val, tim,
		val, tim);
	return this->exec_update_sqls(this->sqlstr);
}

int Cfunc_route::on_update_user_rank_msg(USER_ID uid, google::protobuf::Message* msg)
{
	auto in = (db_msg::update_user_rank_msg*)msg;

	auto ptr_rank = in->mutable_rank_user();

	this->ret = this->rank.update(ptr_rank->type(), ptr_rank->user_show().uid(), ptr_rank->val(), ptr_rank->tim());

	return this->ret;
}

int rank_t::del(uint32_t type, USER_ID uid)
{
	GEN_SQLSTR(this->sqlstr, "delete from %s where type=%u and uid=%" PRIu64,
		this->get_table_name(), 
		type,
		uid);

	return this->exec_update_sqls(this->sqlstr);
}
int Cfunc_route::on_delete_user_rank_msg(USER_ID uid, google::protobuf::Message* msg)
{
	auto in = (db_msg::delete_user_rank_msg*)msg;
	this->rank.del(in->type(), in->uid());

	return this->ret;
}