浏览代码

商品数据库保存成功

张洋 4 年之前
父节点
当前提交
72bfbfc9f6

二进制
bin/Win32/Debug/zhipuzi_pos_windows/db/pos.db


+ 19 - 0
zhipuzi_pos_windows/helper/CLewaimaiJson.cpp

@@ -0,0 +1,19 @@
+#include "../pch/pch.h"
+#include "CLewaimaiJson.h"
+
+CLewaimaiJson::CLewaimaiJson()
+{
+}
+
+
+CLewaimaiJson::~CLewaimaiJson()
+{
+}
+
+std::string CLewaimaiJson::JsonToString(const rapidjson::Value& valObj)
+{
+	rapidjson::StringBuffer sbBuf;
+	rapidjson::Writer<rapidjson::StringBuffer> jWriter(sbBuf);
+	valObj.Accept(jWriter);
+	return std::string(sbBuf.GetString());
+}

+ 17 - 0
zhipuzi_pos_windows/helper/CLewaimaiJson.h

@@ -0,0 +1,17 @@
+#pragma once
+
+#include <codecvt>
+#include <iostream>
+#include <sstream>
+#include <regex>
+#include <string>
+
+class CLewaimaiJson
+{
+public:
+	CLewaimaiJson();
+	~CLewaimaiJson();
+
+	static std::string JsonToString(const rapidjson::Value& valObj);
+};
+

+ 1 - 0
zhipuzi_pos_windows/pch/pch.h

@@ -69,6 +69,7 @@ using namespace rapidjson;
 #include "../helper/CRandomHelper.h"
 #include "../helper/CSystem.h"
 #include "../helper/CLewaimaiString.h"
+#include "../helper/CLewaimaiJson.h"
 
 #include "../tool/CLewaimaiLog.h"
 #include "../tool/CLewaimaiTime.h"

+ 373 - 2
zhipuzi_pos_windows/tool/CSqlite3.cpp

@@ -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;
+}

+ 14 - 3
zhipuzi_pos_windows/tool/CSqlite3.h

@@ -11,17 +11,28 @@ public:
 	~CSqlite3();
 
 public:
+	//下面这些是初始化数据库表的函数
 	bool InitDB();
-
 	bool InitPosConfig();
 	bool InitPosChufangPrinter();
 	bool InitPosUser();
+	bool InitPosFood();
+	bool InitPosFoodType();
+	bool InitPosFoodPackage();
 
+	//下面这些是读写数据库特定表的函数
 	bool SaveParams(std::map<std::string, std::string>& params);
-
 	bool SaveChufangPrinter(std::vector<ChufangPrinter>& printers);
-
 	bool SaveUsers(std::map<string, string> users);
+
+	//登录成功的时候,初始化数据库数据
+	bool InitFoodData(rapidjson::Value& foodrows);
+	bool InitFoodtypeData(rapidjson::Value& foodtyperows);
+	bool InitFoodpackageData(rapidjson::Value& foodpackagerows);
+
+private:
+	bool ExeSQl(std::string sql);
+
 private:
 	int m_rc;
 

+ 31 - 16
zhipuzi_pos_windows/wnd/CLoginWnd.cpp

@@ -532,9 +532,11 @@ void CLoginWnd::HandleLogin()
 		PostMessage(WM_LOGIN_START_INIT_DATA);
 
 		//然后进行服务器数据的同步,同步完成了才切换到收银系统主页面
-		this->HandleInitData();
-
-		PostMessage(WM_LOGIN_SUCCESS);
+		bool ret = this->HandleInitData();
+		if (ret)
+		{
+			PostMessage(WM_LOGIN_SUCCESS);
+		}
 
 		return;
     }
@@ -627,7 +629,7 @@ void CLoginWnd::ShowInitData()
 	pIniting->SetVisible(true);
 }
 
-void CLoginWnd::HandleInitData()
+bool CLoginWnd::HandleInitData()
 {
 	std::map<string, string> params;
 
@@ -638,7 +640,7 @@ void CLoginWnd::HandleInitData()
 		m_wsLoginErrMsg = _T("网络请求出错");
 		PostMessage(WM_LOGIN_ERROR);
 
-		return;
+		return false;
 	}
 
 	rapidjson::Document document;
@@ -649,33 +651,46 @@ void CLoginWnd::HandleInitData()
 		m_wsLoginErrMsg = _T("服务器返回数据格式错误");
 		PostMessage(WM_LOGIN_ERROR);
 
-		return;
+		return false;
 	}
 
 	//获得数据成功
 	rapidjson::Value& data = document["data"];
 
-	rapidjson::Value& v_goods_rows = data["goods_rows"];
-	rapidjson::Value& v_types_lv1_ids = data["types_lv1_ids"];
-	rapidjson::Value& v_food_package = data["food_package"];
+	CSqlite3 sqlite;
 
 	//先处理商品的
-	for (rapidjson::SizeType i = 0; i < v_goods_rows.Size(); ++i)
+	rapidjson::Value& v_goods_rows = data["goods_rows"];
+	ret = sqlite.InitFoodData(v_goods_rows);
+	if (!ret)
 	{
-		rapidjson::Value& foodinfo = v_goods_rows[i];
+		m_wsLoginErrMsg = _T("商品数据库数据同步失败!");
+		PostMessage(WM_LOGIN_ERROR);
 
-		std::string id = foodinfo["id"].GetString();
+		return false;
 	}
 
 	//再处理商品分类的
-	for (rapidjson::SizeType i = 0; i < v_types_lv1_ids.Size(); ++i)
+	rapidjson::Value& v_types_lv1_ids = data["types_lv1_ids"];
+	ret = sqlite.InitFoodtypeData(v_types_lv1_ids);
+	if (!ret)
 	{
-		rapidjson::Value& foodtypeinfo = v_types_lv1_ids[i];
+		m_wsLoginErrMsg = _T("商品分类数据库数据同步失败!");
+		PostMessage(WM_LOGIN_ERROR);
+
+		return false;
 	}
 
 	//再处理商品套餐的
-	for (rapidjson::SizeType i = 0; i < v_food_package.Size(); ++i)
+	rapidjson::Value& v_food_package = data["food_package"];
+	ret = sqlite.InitFoodpackageData(v_food_package);
+	if (!ret)
 	{
-		rapidjson::Value& foodpackageinfo = v_food_package[i];
+		m_wsLoginErrMsg = _T("商品套餐数据库数据同步失败!");
+		PostMessage(WM_LOGIN_ERROR);
+
+		return false;
 	}
+
+	return true;
 }

+ 1 - 1
zhipuzi_pos_windows/wnd/CLoginWnd.h

@@ -76,7 +76,7 @@ public:
 	void HandleLogin();
 
 	//处理服务器数据同步工作,把服务器数据存在本地数据库(商品数据等)
-	void HandleInitData();
+	bool HandleInitData();
 
 public:
     CPaintManagerUI m_pm;

+ 2 - 0
zhipuzi_pos_windows/zhipuzi_pos_windows.vcxproj

@@ -225,6 +225,7 @@ copy $(ProjectDir)conf\ $(SolutionDir)bin\$(Platform)\$(Configuration)\conf\</Co
     </PostBuildEvent>
   </ItemDefinitionGroup>
   <ItemGroup>
+    <ClInclude Include="helper\CLewaimaiJson.h" />
     <ClInclude Include="control\CDiandanFoodItemUI.h" />
     <ClInclude Include="control\CFoodlistUI.h" />
     <ClInclude Include="helper\CBitmapHelper.h" />
@@ -261,6 +262,7 @@ copy $(ProjectDir)conf\ $(SolutionDir)bin\$(Platform)\$(Configuration)\conf\</Co
     <ClInclude Include="control\OrderListUI.h" />
   </ItemGroup>
   <ItemGroup>
+    <ClCompile Include="helper\CLewaimaiJson.cpp" />
     <ClCompile Include="control\CDiandanFoodItemUI.cpp" />
     <ClCompile Include="control\CFoodlistUI.cpp" />
     <ClCompile Include="helper\CBitmapHelper.cpp" />

+ 6 - 0
zhipuzi_pos_windows/zhipuzi_pos_windows.vcxproj.filters

@@ -117,6 +117,9 @@
     <ClInclude Include="network\PosMessage.h">
       <Filter>头文件</Filter>
     </ClInclude>
+    <ClInclude Include="helper\CLewaimaiJson.h">
+      <Filter>头文件</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="pch\pch.cpp">
@@ -209,6 +212,9 @@
     <ClCompile Include="network\CZhipuziHttpClient.cpp">
       <Filter>源文件</Filter>
     </ClCompile>
+    <ClCompile Include="helper\CLewaimaiJson.cpp">
+      <Filter>源文件</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <Image Include="resource\zhipuzi.ico">