|
|
@@ -44,6 +44,9 @@ CSqlite3::~CSqlite3()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * 对数据库表进行一些初始化和检查,这个函数每次程序启动的时候执行1次,就不会再执行了
|
|
|
+ */
|
|
|
bool CSqlite3::InitDB()
|
|
|
{
|
|
|
//先处理pos_config表
|
|
|
@@ -67,6 +70,27 @@ bool CSqlite3::InitDB()
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
+ //再处理pos_food表
|
|
|
+ ret = this->InitPosFood();
|
|
|
+ if (!ret)
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ //再处理pos_foodtype表
|
|
|
+ ret = this->InitPosFoodType();
|
|
|
+ if (!ret)
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ //再处理pos_foodpackage表
|
|
|
+ ret = this->InitPosFoodPackage();
|
|
|
+ if (!ret)
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
@@ -97,7 +121,7 @@ bool CSqlite3::InitPosConfig()
|
|
|
//执行该语句
|
|
|
if (sqlite3_step(stmt) != SQLITE_DONE)
|
|
|
{
|
|
|
- LOG_INFO("create table fail: " << sqlite3_errmsg(m_db));
|
|
|
+ LOG_ERROR("create table fail: " << sqlite3_errmsg(m_db));
|
|
|
|
|
|
sqlite3_finalize(stmt);
|
|
|
return false;
|
|
|
@@ -109,7 +133,7 @@ bool CSqlite3::InitPosConfig()
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- LOG_INFO("create table prepare fail: " << sqlite3_errmsg(m_db));
|
|
|
+ LOG_ERROR("create table prepare fail: " << sqlite3_errmsg(m_db));
|
|
|
|
|
|
sqlite3_finalize(stmt);
|
|
|
|
|
|
@@ -461,6 +485,249 @@ bool CSqlite3::InitPosUser()
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+bool CSqlite3::InitPosFood()
|
|
|
+{
|
|
|
+ //检查有没有pos_food这个表,如果没有就创建
|
|
|
+ std::string sql = "SELECT COUNT(*) FROM sqlite_master where type = 'table' and name = 'pos_food';";
|
|
|
+ sqlite3_stmt * stmt = NULL;
|
|
|
+
|
|
|
+ if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
|
|
|
+ {
|
|
|
+ if (sqlite3_step(stmt) == SQLITE_ROW)
|
|
|
+ {
|
|
|
+ int count = sqlite3_column_int(stmt, 0);
|
|
|
+
|
|
|
+ if (count == 0)
|
|
|
+ {
|
|
|
+ //说明没找到这个表,那么这个时候新建这个表,先释放前面的stmt
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ stmt = NULL;
|
|
|
+
|
|
|
+ sql = "CREATE TABLE pos_food(" \
|
|
|
+ "id CHAR(20) UNIQUE NOT NULL,"\
|
|
|
+ "shop_id CHAR(200) NOT NULL," \
|
|
|
+ "name CHAR(100) NOT NULL," \
|
|
|
+ "price CHAR(100) NOT NULL," \
|
|
|
+ "tag CHAR(100) NOT NULL," \
|
|
|
+ "status CHAR(100) NOT NULL," \
|
|
|
+ "type_id CHAR(20) NOT NULL," \
|
|
|
+ "is_dabao CHAR(20) NOT NULL," \
|
|
|
+ "dabao_money CHAR(100) NOT NULL," \
|
|
|
+ "is_nature CHAR(20) NOT NULL," \
|
|
|
+ "nature CHAR(10000) NOT NULL," \
|
|
|
+ "autostocknum CHAR(100) NOT NULL," \
|
|
|
+ "goods_img CHAR(100) NOT NULL," \
|
|
|
+ "unit CHAR(100) NOT NULL," \
|
|
|
+ "barcode CHAR(100) NOT NULL," \
|
|
|
+ "member_price_used CHAR(100) NOT NULL," \
|
|
|
+ "member_price CHAR(100) NOT NULL," \
|
|
|
+ "buying_price CHAR(100) NOT NULL," \
|
|
|
+ "stock CHAR(100) NOT NULL," \
|
|
|
+ "stockvalid CHAR(100) NOT NULL," \
|
|
|
+ "stock_warning CHAR(100) NOT NULL," \
|
|
|
+ "is_shouyinji_show CHAR(20) NOT NULL," \
|
|
|
+ "expiration_date CHAR(100) NOT NULL," \
|
|
|
+ "is_weight CHAR(20) NOT NULL," \
|
|
|
+ "member_price_json CHAR(100) NOT NULL);";
|
|
|
+
|
|
|
+ if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
|
|
|
+ {
|
|
|
+ //执行该语句
|
|
|
+ if (sqlite3_step(stmt) != SQLITE_DONE)
|
|
|
+ {
|
|
|
+ LOG_ERROR("create table fail: " << sqlite3_errmsg(m_db));
|
|
|
+
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ //走到这里就是表创建成功了
|
|
|
+ //LOG_INFO("create table success");
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ LOG_ERROR("create table prepare fail: " << sqlite3_errmsg(m_db));
|
|
|
+
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ //说明已经有这个表了,就不用再创建了
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ //异常情况
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ //异常情况
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+bool CSqlite3::InitPosFoodType()
|
|
|
+{
|
|
|
+ //检查有没有pos_foodtype这个表,如果没有就创建
|
|
|
+ std::string sql = "SELECT COUNT(*) FROM sqlite_master where type = 'table' and name = 'pos_foodtype';";
|
|
|
+ sqlite3_stmt * stmt = NULL;
|
|
|
+
|
|
|
+ if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
|
|
|
+ {
|
|
|
+ if (sqlite3_step(stmt) == SQLITE_ROW)
|
|
|
+ {
|
|
|
+ int count = sqlite3_column_int(stmt, 0);
|
|
|
+
|
|
|
+ if (count == 0)
|
|
|
+ {
|
|
|
+ //说明没找到这个表,那么这个时候新建这个表,先释放前面的stmt
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ stmt = NULL;
|
|
|
+
|
|
|
+ sql = "CREATE TABLE pos_foodtype(" \
|
|
|
+ "id CHAR(20) UNIQUE NOT NULL,"\
|
|
|
+ "name CHAR(200) NOT NULL," \
|
|
|
+ "is_shouyinji_show CHAR(100) NOT NULL);";
|
|
|
+
|
|
|
+ if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
|
|
|
+ {
|
|
|
+ //执行该语句
|
|
|
+ if (sqlite3_step(stmt) != SQLITE_DONE)
|
|
|
+ {
|
|
|
+ LOG_ERROR("create table fail: " << sqlite3_errmsg(m_db));
|
|
|
+
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ //走到这里就是表创建成功了
|
|
|
+ //LOG_INFO("create table success");
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ LOG_ERROR("create table prepare fail: " << sqlite3_errmsg(m_db));
|
|
|
+
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ //说明已经有这个表了,就不用再创建了
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ //异常情况
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ //异常情况
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+bool CSqlite3::InitPosFoodPackage()
|
|
|
+{
|
|
|
+ //检查有没有pos_foodpackage这个表,如果没有就创建
|
|
|
+ std::string sql = "SELECT COUNT(*) FROM sqlite_master where type = 'table' and name = 'pos_foodpackage';";
|
|
|
+ sqlite3_stmt * stmt = NULL;
|
|
|
+
|
|
|
+ if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
|
|
|
+ {
|
|
|
+ if (sqlite3_step(stmt) == SQLITE_ROW)
|
|
|
+ {
|
|
|
+ int count = sqlite3_column_int(stmt, 0);
|
|
|
+
|
|
|
+ if (count == 0)
|
|
|
+ {
|
|
|
+ //说明没找到这个表,那么这个时候新建这个表,先释放前面的stmt
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ stmt = NULL;
|
|
|
+
|
|
|
+ sql = "CREATE TABLE pos_foodpackage(" \
|
|
|
+ "id CHAR(20) UNIQUE NOT NULL,"\
|
|
|
+ "shop_id CHAR(200) NOT NULL," \
|
|
|
+ "name CHAR(100) NOT NULL," \
|
|
|
+ "price CHAR(100) NOT NULL," \
|
|
|
+ "tag CHAR(100) NOT NULL," \
|
|
|
+ "status CHAR(100) NOT NULL," \
|
|
|
+ "is_dabao CHAR(20) NOT NULL," \
|
|
|
+ "dabao_money CHAR(100) NOT NULL," \
|
|
|
+ "nature CHAR(10000) NOT NULL," \
|
|
|
+ "goods_img CHAR(100) NOT NULL," \
|
|
|
+ "unit CHAR(100) NOT NULL," \
|
|
|
+ "supporttype CHAR(100) NOT NULL," \
|
|
|
+ "is_shouyinji_show CHAR(100) NOT NULL," \
|
|
|
+ "barcode CHAR(100) NOT NULL);";
|
|
|
+
|
|
|
+ if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
|
|
|
+ {
|
|
|
+ //执行该语句
|
|
|
+ if (sqlite3_step(stmt) != SQLITE_DONE)
|
|
|
+ {
|
|
|
+ LOG_ERROR("create table fail: " << sqlite3_errmsg(m_db));
|
|
|
+
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ //走到这里就是表创建成功了
|
|
|
+ //LOG_INFO("create table success");
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ LOG_ERROR("create table prepare fail: " << sqlite3_errmsg(m_db));
|
|
|
+
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ //说明已经有这个表了,就不用再创建了
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ //异常情况
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ //异常情况
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
bool CSqlite3::SaveParams(std::map<std::string, std::string>& params)
|
|
|
{
|
|
|
int result = sqlite3_exec(m_db, "BEGIN;", 0, 0, 0);
|
|
|
@@ -549,3 +816,107 @@ bool CSqlite3::SaveUsers(std::map<string, string> users)
|
|
|
//LOG_INFO("save params fail");
|
|
|
return false;
|
|
|
}
|
|
|
+
|
|
|
+bool CSqlite3::InitFoodData(rapidjson::Value& foodrows)
|
|
|
+{
|
|
|
+ //先清空之前的旧数据,重新完整写入新数据
|
|
|
+ std::string sql = "delete from pos_food;";
|
|
|
+ this->ExeSQl(sql);
|
|
|
+
|
|
|
+ for (rapidjson::SizeType i = 0; i < foodrows.Size(); ++i)
|
|
|
+ {
|
|
|
+ rapidjson::Value& foodinfo = foodrows[i];
|
|
|
+
|
|
|
+ std::string id = foodinfo["id"].GetString();
|
|
|
+ std::string shop_id = foodinfo["shop_id"].GetString();
|
|
|
+ std::string name = foodinfo["name"].GetString();
|
|
|
+ std::string price = foodinfo["price"].GetString();
|
|
|
+ std::string tag = foodinfo["tag"].GetString();
|
|
|
+ std::string status = foodinfo["status"].GetString();
|
|
|
+ std::string type_id = foodinfo["type_id"].GetString();
|
|
|
+ std::string is_dabao = foodinfo["is_dabao"].GetString();
|
|
|
+ std::string dabao_money = foodinfo["dabao_money"].GetString();
|
|
|
+ std::string is_nature = foodinfo["is_nature"].GetString();
|
|
|
+
|
|
|
+ std::string nature = CLewaimaiJson::JsonToString(foodinfo["nature"]);
|
|
|
+
|
|
|
+ std::string autostocknum = foodinfo["autostocknum"].GetString();
|
|
|
+ std::string goods_img = foodinfo["goods_img"].GetString();
|
|
|
+ std::string unit = foodinfo["unit"].GetString();
|
|
|
+ std::string barcode = foodinfo["barcode"].GetString();
|
|
|
+ std::string member_price_used = foodinfo["member_price_used"].GetString();
|
|
|
+ std::string member_price = foodinfo["member_price"].GetString();
|
|
|
+ std::string buying_price = foodinfo["buying_price"].GetString();
|
|
|
+ std::string stock = foodinfo["stock"].GetString();
|
|
|
+ std::string stockvalid = foodinfo["stockvalid"].GetString();
|
|
|
+ std::string stock_warning = foodinfo["stock_warning"].GetString();
|
|
|
+ std::string is_shouyinji_show = foodinfo["is_shouyinji_show"].GetString();
|
|
|
+ std::string expiration_date = foodinfo["expiration_date"].GetString();
|
|
|
+ std::string is_weight = foodinfo["is_weight"].GetString();
|
|
|
+ std::string member_price_json = foodinfo["member_price_json"].GetString();
|
|
|
+
|
|
|
+ //插入一个商品数据
|
|
|
+ std::string sql = "INSERT INTO pos_food (id,shop_id,name,price,tag,status,type_id,is_dabao,dabao_money,is_nature,nature,autostocknum,goods_img,unit,barcode,member_price_used,member_price, \
|
|
|
+ buying_price,stock,stockvalid,stock_warning,is_shouyinji_show,expiration_date,is_weight,member_price_json) VALUES ('" + id + "' ,'" + shop_id + "','" \
|
|
|
+ + name + "', '" + price + "', '" + tag + "', '" + status + "', '" + type_id + "', '" + is_dabao + "', '" + dabao_money + "', '" + is_nature + "', '" + nature + "', '" \
|
|
|
+ + autostocknum + "', '" + goods_img + "', '" + unit + "', '" + barcode + "', '" + member_price_used + "', '" + member_price + "', '" + buying_price + "', '" + stock + "', '" \
|
|
|
+ + stockvalid + "', '" + stock_warning + "', '" + is_shouyinji_show + "', '" + expiration_date + "','" + is_weight + "','" + member_price_json + "')";
|
|
|
+
|
|
|
+ bool ret = this->ExeSQl(sql);
|
|
|
+ if (!ret)
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+bool CSqlite3::InitFoodtypeData(rapidjson::Value& foodtyperows)
|
|
|
+{
|
|
|
+ //先清空之前的旧数据,重新完整写入新数据
|
|
|
+ std::string sql = "delete from pos_foodtype;";
|
|
|
+ sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
|
|
|
+
|
|
|
+ for (rapidjson::SizeType i = 0; i < foodtyperows.Size(); ++i)
|
|
|
+ {
|
|
|
+ rapidjson::Value& foodinfo = foodtyperows[i];
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+bool CSqlite3::InitFoodpackageData(rapidjson::Value& foodpackagerows)
|
|
|
+{
|
|
|
+ //先清空之前的旧数据,重新完整写入新数据
|
|
|
+ std::string sql = "delete from pos_foodpackage;";
|
|
|
+ sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
|
|
|
+
|
|
|
+ for (rapidjson::SizeType i = 0; i < foodpackagerows.Size(); ++i)
|
|
|
+ {
|
|
|
+ rapidjson::Value& foodinfo = foodpackagerows[i];
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+bool CSqlite3::ExeSQl(std::string sql)
|
|
|
+{
|
|
|
+ char *zErrMsg = 0;
|
|
|
+ int rc;
|
|
|
+
|
|
|
+ rc = sqlite3_exec(m_db, sql.c_str(), 0, 0, &zErrMsg);
|
|
|
+ if (rc != SQLITE_OK)
|
|
|
+ {
|
|
|
+ LOG_ERROR("SQL error: "<<zErrMsg);
|
|
|
+ sqlite3_free(zErrMsg);
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+}
|