item.cpp 3.87 KB
#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;
}