CSqlite3.cpp 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763
  1. #include "../pch/pch.h"
  2. #include "CSqlite3.h"
  3. CSqlite3::CSqlite3()
  4. {
  5. try
  6. {
  7. sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
  8. std::wstring folderPath = CSystem::GetProgramDir() + L"\\db";
  9. if (!CSystem::IsDirExist(folderPath))
  10. {
  11. LOG_INFO("folderPath:" << folderPath.c_str() << ",没有找到对应的目录,即将创建");
  12. bool flag = CreateDirectory(folderPath.c_str(), NULL);
  13. if (flag == false)
  14. {
  15. LOG_INFO("新建 db 目录失败!");
  16. }
  17. LOG_INFO("新建 db 目录成功!");
  18. }
  19. //如果没有这个文件,这里会创建这个文件
  20. std::wstring path = CSystem::GetProgramDir() + L"\\db\\pos.db";
  21. std::string s_path = CLewaimaiString::UnicodeToUTF8(path);
  22. //LOG_INFO("s_path:" << s_path.c_str());
  23. m_rc = sqlite3_open(s_path.c_str(), &m_db);
  24. if (m_rc != SQLITE_OK)
  25. {
  26. LOG_INFO("Can't open database: " << sqlite3_errmsg(m_db));
  27. return;
  28. }
  29. //LOG_INFO("Opened database successfully");
  30. }
  31. catch (const std::exception& e)
  32. {
  33. std::string aa = std::string(e.what());
  34. LOG_INFO(("创建数据库文件失败: " + std::string(e.what())).c_str());
  35. return ;
  36. }
  37. }
  38. CSqlite3::~CSqlite3()
  39. {
  40. if(m_db != NULL)
  41. {
  42. sqlite3_close(m_db);
  43. }
  44. }
  45. /**
  46. * 对数据库表进行一些初始化和检查,这个函数每次程序启动的时候执行1次,就不会再执行了
  47. */
  48. bool CSqlite3::InitDB()
  49. {
  50. //先处理pos_config表
  51. bool ret = this->InitPosConfig();
  52. if (!ret)
  53. {
  54. return false;
  55. }
  56. //再处理pos_chufang_printer表
  57. ret = this->InitPosChufangPrinter();
  58. if (!ret)
  59. {
  60. return false;
  61. }
  62. //再处理pos_user表
  63. ret = this->InitPosUser();
  64. if (!ret)
  65. {
  66. return false;
  67. }
  68. //再处理pos_food表
  69. ret = this->InitPosFood();
  70. if (!ret)
  71. {
  72. return false;
  73. }
  74. //再处理pos_foodtype表
  75. ret = this->InitPosFoodType();
  76. if (!ret)
  77. {
  78. return false;
  79. }
  80. //再处理pos_foodpackage表
  81. ret = this->InitPosFoodPackage();
  82. if (!ret)
  83. {
  84. return false;
  85. }
  86. return true;
  87. }
  88. bool CSqlite3::InitPosConfig()
  89. {
  90. //检查有没有pos_config这个表,如果没有就创建
  91. std::string sql = "SELECT COUNT(*) FROM sqlite_master where type = 'table' and name = 'pos_config';";
  92. sqlite3_stmt * stmt = NULL;
  93. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  94. {
  95. if (sqlite3_step(stmt) == SQLITE_ROW)
  96. {
  97. int count = sqlite3_column_int(stmt, 0);
  98. if (count == 0)
  99. {
  100. //说明没找到这个表,那么这个时候新建这个表,先释放前面的stmt
  101. sqlite3_finalize(stmt);
  102. stmt = NULL;
  103. sql = "CREATE TABLE pos_config(" \
  104. "name CHAR(100) UNIQUE NOT NULL," \
  105. "value CHAR(2000) NOT NULL);";
  106. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  107. {
  108. //执行该语句
  109. if (sqlite3_step(stmt) != SQLITE_DONE)
  110. {
  111. LOG_ERROR("create table fail: " << sqlite3_errmsg(m_db));
  112. sqlite3_finalize(stmt);
  113. return false;
  114. }
  115. //走到这里就是表创建成功了
  116. //LOG_INFO("create table success");
  117. sqlite3_finalize(stmt);
  118. }
  119. else
  120. {
  121. LOG_ERROR("create table prepare fail: " << sqlite3_errmsg(m_db));
  122. sqlite3_finalize(stmt);
  123. return false;
  124. }
  125. }
  126. else
  127. {
  128. //说明已经有这个表了,就不用再创建了
  129. sqlite3_finalize(stmt);
  130. }
  131. std::string sql = "SELECT * FROM pos_config;";
  132. sqlite3_stmt * stmt = NULL;
  133. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  134. {
  135. while (sqlite3_step(stmt) == SQLITE_ROW)
  136. {
  137. std::string name = (char*)sqlite3_column_text(stmt, 0);
  138. std::string value = (char*)sqlite3_column_text(stmt, 1);
  139. CSetting::GetInstance()->SetParam(name, value, false);
  140. }
  141. sqlite3_finalize(stmt);
  142. }
  143. else
  144. {
  145. //异常情况
  146. sqlite3_finalize(stmt);
  147. return false;
  148. }
  149. }
  150. else
  151. {
  152. //异常情况
  153. sqlite3_finalize(stmt);
  154. return false;
  155. }
  156. }
  157. else
  158. {
  159. //异常情况
  160. sqlite3_finalize(stmt);
  161. return false;
  162. }
  163. return true;
  164. }
  165. bool CSqlite3::InitPosChufangPrinter()
  166. {
  167. std::string sql = "SELECT COUNT(*) FROM sqlite_master where type = 'table' and name = 'pos_chufang_printer';";
  168. sqlite3_stmt * stmt = NULL;
  169. //读取厨房打印机的参数
  170. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  171. {
  172. if (sqlite3_step(stmt) == SQLITE_ROW)
  173. {
  174. int count = sqlite3_column_int(stmt, 0);
  175. if (count == 0)
  176. {
  177. //说明没找到这个表,那么这个时候新建这个表,先释放前面的stmt
  178. sqlite3_finalize(stmt);
  179. stmt = NULL;
  180. sql = "CREATE TABLE pos_chufang_printer(" \
  181. "id INTEGER PRIMARY KEY AUTOINCREMENT,"\
  182. "date CHAR(100) NOT NULL," \
  183. "name CHAR(100) NOT NULL," \
  184. "ip CHAR(100) NOT NULL," \
  185. "guige CHAR(100) NOT NULL," \
  186. "fendan CHAR(100) NOT NULL," \
  187. "fenlei CHAR(100) NOT NULL," \
  188. "fenlei_ids CHAR(2000) );";
  189. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  190. {
  191. //执行该语句
  192. if (sqlite3_step(stmt) != SQLITE_DONE)
  193. {
  194. std::string err = sqlite3_errmsg(m_db);
  195. LOG_INFO("create table fail: " << err.c_str());
  196. sqlite3_finalize(stmt);
  197. return false;
  198. }
  199. //走到这里就是表创建成功了
  200. sqlite3_finalize(stmt);
  201. }
  202. else
  203. {
  204. LOG_INFO("create table prepare fail: " << sqlite3_errmsg(m_db));
  205. sqlite3_finalize(stmt);
  206. return false;
  207. }
  208. }
  209. else
  210. {
  211. //说明已经有这个表了,就不用再创建了
  212. sqlite3_finalize(stmt);
  213. }
  214. //兼容性检查,判断字段是否存在
  215. std::string sql_pandduan = "select COUNT(*) from sqlite_master where type = 'table' and tbl_name = 'pos_chufang_printer' and sql like '%fenlei%';";
  216. stmt = NULL;
  217. if (sqlite3_prepare_v2(m_db, sql_pandduan.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  218. {
  219. if (sqlite3_step(stmt) == SQLITE_ROW)
  220. {
  221. int count = sqlite3_column_int(stmt, 0);
  222. if (count == 0)
  223. {
  224. //说明没找到这个字段,准备添加字段
  225. sqlite3_finalize(stmt);
  226. stmt = NULL;
  227. sql = "ALTER TABLE pos_chufang_printer " \
  228. "add fenlei CHAR(100) NOT NULL DEFAULT '0';";
  229. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  230. {
  231. //执行该语句
  232. if (sqlite3_step(stmt) != SQLITE_DONE)
  233. {
  234. std::string err = sqlite3_errmsg(m_db);
  235. LOG_INFO("alter table fail: " << err.c_str());
  236. sqlite3_finalize(stmt);
  237. return false;
  238. }
  239. //走到这里就是表创建成功了
  240. LOG_INFO("alter table success");
  241. sqlite3_finalize(stmt);
  242. }
  243. else
  244. {
  245. LOG_INFO("alter table prepare fail: " << sqlite3_errmsg(m_db));
  246. sqlite3_finalize(stmt);
  247. return false;
  248. }
  249. }
  250. else
  251. {
  252. //说明已经有这2个字段了
  253. sqlite3_finalize(stmt);
  254. }
  255. }
  256. }
  257. else
  258. {
  259. //异常情况
  260. sqlite3_finalize(stmt);
  261. return false;
  262. }
  263. sql_pandduan = "select COUNT(*) from sqlite_master where type = 'table' and tbl_name = 'pos_chufang_printer' and sql like '%fenlei_ids%';";
  264. stmt = NULL;
  265. if (sqlite3_prepare_v2(m_db, sql_pandduan.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  266. {
  267. if (sqlite3_step(stmt) == SQLITE_ROW)
  268. {
  269. int count = sqlite3_column_int(stmt, 0);
  270. if (count == 0)
  271. {
  272. //说明没找到这个字段,准备添加字段
  273. sqlite3_finalize(stmt);
  274. stmt = NULL;
  275. sql = "ALTER TABLE pos_chufang_printer " \
  276. "add fenlei_ids CHAR(2000) NOT NULL DEFAULT '';";
  277. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  278. {
  279. //执行该语句
  280. if (sqlite3_step(stmt) != SQLITE_DONE)
  281. {
  282. std::string err = sqlite3_errmsg(m_db);
  283. LOG_INFO("alter table fail: " << err.c_str());
  284. sqlite3_finalize(stmt);
  285. return false;
  286. }
  287. //走到这里就是表创建成功了
  288. sqlite3_finalize(stmt);
  289. }
  290. else
  291. {
  292. LOG_INFO("alter table prepare fail: " << sqlite3_errmsg(m_db));
  293. sqlite3_finalize(stmt);
  294. return false;;
  295. }
  296. }
  297. else
  298. {
  299. //说明已经有这2个字段了
  300. sqlite3_finalize(stmt);
  301. }
  302. }
  303. }
  304. else
  305. {
  306. //异常情况
  307. sqlite3_finalize(stmt);
  308. return false;
  309. }
  310. std::string sql = "SELECT * FROM pos_chufang_printer;";
  311. stmt = NULL;
  312. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  313. {
  314. while (sqlite3_step(stmt) == SQLITE_ROW)
  315. {
  316. std::string date = (char*)sqlite3_column_text(stmt, 1);
  317. std::string name = (char*)sqlite3_column_text(stmt, 2);
  318. std::string ip = (char*)sqlite3_column_text(stmt, 3);
  319. std::string guige = (char*)sqlite3_column_text(stmt, 4);
  320. std::string fendan = (char*)sqlite3_column_text(stmt, 5);
  321. std::string fenlei = (char*)sqlite3_column_text(stmt, 6);
  322. std::string fenlei_ids = (char*)sqlite3_column_text(stmt, 7);
  323. //这里仅仅是把数据库内容读到内存,所以之类用false
  324. CSetting::GetInstance()->AddChufangPrinter(date, name, ip, guige, fendan, fenlei, fenlei_ids, false);
  325. }
  326. sqlite3_finalize(stmt);
  327. }
  328. else
  329. {
  330. //异常情况
  331. sqlite3_finalize(stmt);
  332. return false;
  333. }
  334. }
  335. }
  336. return true;
  337. }
  338. bool CSqlite3::InitPosUser()
  339. {
  340. std::string sql = "SELECT COUNT(*) FROM sqlite_master where type = 'table' and name = 'pos_user';";
  341. sqlite3_stmt * stmt = NULL;
  342. //读取厨房打印机的参数
  343. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  344. {
  345. if (sqlite3_step(stmt) == SQLITE_ROW)
  346. {
  347. int count = sqlite3_column_int(stmt, 0);
  348. if (count == 0)
  349. {
  350. //说明没找到这个表,那么这个时候新建这个表,先释放前面的stmt
  351. sqlite3_finalize(stmt);
  352. stmt = NULL;
  353. sql = "CREATE TABLE pos_user(" \
  354. "id INTEGER PRIMARY KEY AUTOINCREMENT,"\
  355. "username CHAR(100) NOT NULL," \
  356. "password CHAR(100) NOT NULL);";
  357. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  358. {
  359. //执行该语句
  360. if (sqlite3_step(stmt) != SQLITE_DONE)
  361. {
  362. std::string err = sqlite3_errmsg(m_db);
  363. LOG_INFO("create table fail: " << err.c_str());
  364. sqlite3_finalize(stmt);
  365. return false;
  366. }
  367. //走到这里就是表创建成功了
  368. //LOG_INFO("create table success");
  369. sqlite3_finalize(stmt);
  370. }
  371. else
  372. {
  373. LOG_INFO("create table prepare fail: " << sqlite3_errmsg(m_db));
  374. sqlite3_finalize(stmt);
  375. return false;
  376. }
  377. }
  378. else
  379. {
  380. //说明已经有这个表了,就不用再创建了
  381. sqlite3_finalize(stmt);
  382. }
  383. std::string sql = "SELECT * FROM pos_user;";
  384. sqlite3_stmt * stmt = NULL;
  385. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  386. {
  387. while (sqlite3_step(stmt) == SQLITE_ROW)
  388. {
  389. std::string username = (char*)sqlite3_column_text(stmt, 1);
  390. std::string password = (char*)sqlite3_column_text(stmt, 2);
  391. //这里仅仅是把数据库内容读到内存,所以之类用false
  392. CSetting::GetInstance()->SetUser(username, password);
  393. }
  394. sqlite3_finalize(stmt);
  395. }
  396. else
  397. {
  398. //异常情况
  399. sqlite3_finalize(stmt);
  400. return false;
  401. }
  402. }
  403. }
  404. return true;
  405. }
  406. bool CSqlite3::InitPosFood()
  407. {
  408. //检查有没有pos_food这个表,如果没有就创建
  409. std::string sql = "SELECT COUNT(*) FROM sqlite_master where type = 'table' and name = 'pos_food';";
  410. sqlite3_stmt * stmt = NULL;
  411. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  412. {
  413. if (sqlite3_step(stmt) == SQLITE_ROW)
  414. {
  415. int count = sqlite3_column_int(stmt, 0);
  416. if (count == 0)
  417. {
  418. //说明没找到这个表,那么这个时候新建这个表,先释放前面的stmt
  419. sqlite3_finalize(stmt);
  420. stmt = NULL;
  421. sql = "CREATE TABLE pos_food(" \
  422. "id CHAR(20) UNIQUE NOT NULL,"\
  423. "shop_id CHAR(200) NOT NULL," \
  424. "name CHAR(100) NOT NULL," \
  425. "price CHAR(100) NOT NULL," \
  426. "tag CHAR(100) NOT NULL," \
  427. "status CHAR(100) NOT NULL," \
  428. "type_id CHAR(20) NOT NULL," \
  429. "is_dabao CHAR(20) NOT NULL," \
  430. "dabao_money CHAR(100) NOT NULL," \
  431. "is_nature CHAR(20) NOT NULL," \
  432. "nature CHAR(10000) NOT NULL," \
  433. "autostocknum CHAR(100) NOT NULL," \
  434. "goods_img CHAR(100) NOT NULL," \
  435. "unit CHAR(100) NOT NULL," \
  436. "barcode CHAR(100) NOT NULL," \
  437. "member_price_used CHAR(100) NOT NULL," \
  438. "member_price CHAR(100) NOT NULL," \
  439. "buying_price CHAR(100) NOT NULL," \
  440. "stock CHAR(100) NOT NULL," \
  441. "stockvalid CHAR(100) NOT NULL," \
  442. "stock_warning CHAR(100) NOT NULL," \
  443. "is_shouyinji_show CHAR(20) NOT NULL," \
  444. "is_waimai_show CHAR(20) NOT NULL," \
  445. "is_tangshi_show CHAR(20) NOT NULL," \
  446. "is_zhengcan_show CHAR(20) NOT NULL," \
  447. "expiration_date CHAR(100) NOT NULL," \
  448. "is_weight CHAR(20) NOT NULL," \
  449. "weight_plu_code CHAR(20) NOT NULL," \
  450. "weight_food_code CHAR(20) NOT NULL," \
  451. "jiagong_type CHAR(20) NOT NULL," \
  452. "cost_mode CHAR(20) NOT NULL," \
  453. "member_price_json CHAR(100) NOT NULL);";
  454. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  455. {
  456. //执行该语句
  457. if (sqlite3_step(stmt) != SQLITE_DONE)
  458. {
  459. LOG_ERROR("create table fail: " << sqlite3_errmsg(m_db));
  460. sqlite3_finalize(stmt);
  461. return false;
  462. }
  463. //走到这里就是表创建成功了
  464. //LOG_INFO("create table success");
  465. sqlite3_finalize(stmt);
  466. }
  467. else
  468. {
  469. LOG_ERROR("create table prepare fail: " << sqlite3_errmsg(m_db));
  470. sqlite3_finalize(stmt);
  471. return false;
  472. }
  473. }
  474. else
  475. {
  476. //说明已经有这个表了,就不用再创建了
  477. sqlite3_finalize(stmt);
  478. }
  479. }
  480. else
  481. {
  482. //异常情况
  483. sqlite3_finalize(stmt);
  484. return false;
  485. }
  486. }
  487. else
  488. {
  489. //异常情况
  490. sqlite3_finalize(stmt);
  491. return false;
  492. }
  493. return true;
  494. }
  495. bool CSqlite3::InitPosFoodType()
  496. {
  497. //检查有没有pos_foodtype这个表,如果没有就创建
  498. std::string sql = "SELECT COUNT(*) FROM sqlite_master where type = 'table' and name = 'pos_foodtype';";
  499. sqlite3_stmt * stmt = NULL;
  500. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  501. {
  502. if (sqlite3_step(stmt) == SQLITE_ROW)
  503. {
  504. int count = sqlite3_column_int(stmt, 0);
  505. if (count == 0)
  506. {
  507. //说明没找到这个表,那么这个时候新建这个表,先释放前面的stmt
  508. sqlite3_finalize(stmt);
  509. stmt = NULL;
  510. sql = "CREATE TABLE pos_foodtype(" \
  511. "id CHAR(20) UNIQUE NOT NULL,"\
  512. "name CHAR(200) NOT NULL," \
  513. "is_shouyinji_show CHAR(100) NOT NULL," \
  514. "tag CHAR(100) NOT NULL);";
  515. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  516. {
  517. //执行该语句
  518. if (sqlite3_step(stmt) != SQLITE_DONE)
  519. {
  520. LOG_ERROR("create table fail: " << sqlite3_errmsg(m_db));
  521. sqlite3_finalize(stmt);
  522. return false;
  523. }
  524. //走到这里就是表创建成功了
  525. //LOG_INFO("create table success");
  526. sqlite3_finalize(stmt);
  527. }
  528. else
  529. {
  530. LOG_ERROR("create table prepare fail: " << sqlite3_errmsg(m_db));
  531. sqlite3_finalize(stmt);
  532. return false;
  533. }
  534. }
  535. else
  536. {
  537. //说明已经有这个表了,就不用再创建了
  538. sqlite3_finalize(stmt);
  539. }
  540. }
  541. else
  542. {
  543. //异常情况
  544. sqlite3_finalize(stmt);
  545. return false;
  546. }
  547. }
  548. else
  549. {
  550. //异常情况
  551. sqlite3_finalize(stmt);
  552. return false;
  553. }
  554. return true;
  555. }
  556. bool CSqlite3::InitPosFoodPackage()
  557. {
  558. //检查有没有pos_foodpackage这个表,如果没有就创建
  559. std::string sql = "SELECT COUNT(*) FROM sqlite_master where type = 'table' and name = 'pos_foodpackage';";
  560. sqlite3_stmt * stmt = NULL;
  561. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  562. {
  563. if (sqlite3_step(stmt) == SQLITE_ROW)
  564. {
  565. int count = sqlite3_column_int(stmt, 0);
  566. if (count == 0)
  567. {
  568. //说明没找到这个表,那么这个时候新建这个表,先释放前面的stmt
  569. sqlite3_finalize(stmt);
  570. stmt = NULL;
  571. sql = "CREATE TABLE pos_foodpackage(" \
  572. "id CHAR(20) UNIQUE NOT NULL,"\
  573. "shop_id CHAR(200) NOT NULL," \
  574. "name CHAR(100) NOT NULL," \
  575. "price CHAR(100) NOT NULL," \
  576. "tag CHAR(100) NOT NULL," \
  577. "status CHAR(100) NOT NULL," \
  578. "is_dabao CHAR(20) NOT NULL," \
  579. "dabao_money CHAR(100) NOT NULL," \
  580. "nature CHAR(10000) NOT NULL," \
  581. "goods_img CHAR(100) NOT NULL," \
  582. "unit CHAR(100) NOT NULL," \
  583. "supporttype CHAR(100) NOT NULL," \
  584. "is_shouyinji_show CHAR(100) NOT NULL," \
  585. "barcode CHAR(100) NOT NULL);";
  586. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  587. {
  588. //执行该语句
  589. if (sqlite3_step(stmt) != SQLITE_DONE)
  590. {
  591. LOG_ERROR("create table fail: " << sqlite3_errmsg(m_db));
  592. sqlite3_finalize(stmt);
  593. return false;
  594. }
  595. //走到这里就是表创建成功了
  596. //LOG_INFO("create table success");
  597. sqlite3_finalize(stmt);
  598. }
  599. else
  600. {
  601. LOG_ERROR("create table prepare fail: " << sqlite3_errmsg(m_db));
  602. sqlite3_finalize(stmt);
  603. return false;
  604. }
  605. }
  606. else
  607. {
  608. //说明已经有这个表了,就不用再创建了
  609. sqlite3_finalize(stmt);
  610. }
  611. }
  612. else
  613. {
  614. //异常情况
  615. sqlite3_finalize(stmt);
  616. return false;
  617. }
  618. }
  619. else
  620. {
  621. //异常情况
  622. sqlite3_finalize(stmt);
  623. return false;
  624. }
  625. return true;
  626. }
  627. bool CSqlite3::SaveParams(std::map<std::string, std::string>& params)
  628. {
  629. int result = sqlite3_exec(m_db, "BEGIN;", 0, 0, 0);
  630. std::string sql = "delete from pos_config;";
  631. result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
  632. for(std::map<std::string, std::string>::iterator it = params.begin(); it != params.end(); it++)
  633. {
  634. std::string name = CLewaimaiString::SqlZhuanyi(it->first);
  635. std::string value = CLewaimaiString::SqlZhuanyi(it->second);
  636. sql = "INSERT INTO pos_config (name, value) VALUES ('" + name + "','" + value + "');";
  637. result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
  638. }
  639. result = sqlite3_exec(m_db, "COMMIT;", 0, 0, 0);
  640. if(result == SQLITE_OK)
  641. {
  642. //LOG_INFO("save params success");
  643. return true;
  644. }
  645. LOG_INFO("save params fail:"<< sqlite3_errmsg(m_db));
  646. return false;
  647. }
  648. bool CSqlite3::SaveChufangPrinter(std::vector<ChufangPrinter>& printers)
  649. {
  650. int result = sqlite3_exec(m_db, "BEGIN;", 0, 0, 0);
  651. std::string sql = "delete from pos_chufang_printer;";
  652. result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
  653. for (std::vector<ChufangPrinter>::iterator it = printers.begin(); it != printers.end(); it++)
  654. {
  655. std::string date = CLewaimaiString::SqlZhuanyi((*it).date);
  656. std::string name = CLewaimaiString::SqlZhuanyi((*it).name);
  657. std::string ip = CLewaimaiString::SqlZhuanyi((*it).ip);
  658. std::string guige = CLewaimaiString::SqlZhuanyi((*it).guige);
  659. std::string fendan = CLewaimaiString::SqlZhuanyi((*it).fendan);
  660. std::string fenlei = CLewaimaiString::SqlZhuanyi((*it).fenlei);
  661. std::string fenlei_ids = CLewaimaiString::SqlZhuanyi((*it).fenlei_ids);
  662. sql = "INSERT INTO pos_chufang_printer (date, name, ip, guige, fendan, fenlei, fenlei_ids) VALUES ('" + date + "' ,'" + name + "','" + ip + "','" + guige + "','" + fendan + "','" + fenlei + "','" + fenlei_ids + "')";
  663. result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
  664. }
  665. result = sqlite3_exec(m_db, "COMMIT;", 0, 0, 0);
  666. if (result == SQLITE_OK)
  667. {
  668. LOG_INFO("save params success");
  669. return true;
  670. }
  671. LOG_INFO("save params fail");
  672. return false;
  673. }
  674. bool CSqlite3::SaveUsers(std::map<std::string, std::string> users)
  675. {
  676. int result = sqlite3_exec(m_db, "BEGIN;", 0, 0, 0);
  677. std::string sql = "delete from pos_user;";
  678. result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
  679. for(std::map<std::string, std::string>::iterator it = users.begin(); it != users.end(); it++)
  680. {
  681. std::string name = CLewaimaiString::SqlZhuanyi(it->first);
  682. std::string password = CLewaimaiString::SqlZhuanyi(it->second);
  683. sql = "INSERT INTO pos_user (username, password) VALUES ('" + name + "' ,'" + password + "')";
  684. result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
  685. }
  686. result = sqlite3_exec(m_db, "COMMIT;", 0, 0, 0);
  687. if(result == SQLITE_OK)
  688. {
  689. //LOG_INFO("save params success");
  690. return true;
  691. }
  692. //LOG_INFO("save params fail");
  693. return false;
  694. }
  695. bool CSqlite3::InitFoodData(rapidjson::Value& foodrows)
  696. {
  697. this->ExeSQl("begin;");
  698. //先清空之前的旧数据,重新完整写入新数据
  699. std::string sql_delete = "delete from pos_food;";
  700. this->ExeSQl(sql_delete);
  701. for (rapidjson::SizeType i = 0; i < foodrows.Size(); ++i)
  702. {
  703. rapidjson::Value& foodinfo = foodrows[i];
  704. std::string id = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["id"]));
  705. std::string shop_id = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["shop_id"]));
  706. std::string name = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["name"]));
  707. std::string price = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["price"]));
  708. std::string tag = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["tag"]));
  709. std::string status = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["status"]));
  710. std::string type_id = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["type_id"]));
  711. std::string is_dabao = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["is_dabao"]));
  712. std::string dabao_money = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["dabao_money"]));
  713. std::string is_nature = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["is_nature"]));
  714. std::string nature = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::JsonToString(foodinfo["nature"]));
  715. std::string autostocknum = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["autostocknum"]));
  716. std::string goods_img = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["goods_img"]));
  717. std::string unit = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["unit"]));
  718. std::string barcode = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["barcode"]));
  719. std::string member_price_used = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["member_price_used"]));
  720. std::string member_price = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["member_price"]));
  721. std::string buying_price = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["buying_price"]));
  722. std::string stock = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["stock"]));
  723. std::string stockvalid = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["stockvalid"]));
  724. std::string stock_warning = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["stock_warning"]));
  725. std::string is_shouyinji_show = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["is_shouyinji_show"]));
  726. std::string expiration_date = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["expiration_date"]));
  727. std::string is_weight = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["is_weight"]));
  728. std::string member_price_json = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["member_price_json"]));
  729. std::string is_waimai_show = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["is_waimai_show"]));
  730. std::string is_tangshi_show = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["is_tangshi_show"]));
  731. std::string is_zhengcan_show = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["is_zhengcan_show"]));
  732. std::string jiagong_type = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["jiagong_type"]));
  733. std::string cost_mode = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["cost_mode"]));
  734. std::string weight_plu_code;
  735. if (foodinfo["weight_plu_code"].IsString())
  736. {
  737. weight_plu_code = CLewaimaiString::SqlZhuanyi(foodinfo["weight_plu_code"].GetString());
  738. }
  739. else
  740. {
  741. weight_plu_code = "";
  742. }
  743. std::string weight_food_code;
  744. if (foodinfo["weight_food_code"].IsString())
  745. {
  746. weight_food_code = CLewaimaiString::SqlZhuanyi(foodinfo["weight_food_code"].GetString());
  747. }
  748. else
  749. {
  750. weight_food_code = "";
  751. }
  752. //插入一个商品数据
  753. 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, \
  754. buying_price,stock,stockvalid,stock_warning,is_shouyinji_show,is_waimai_show,is_tangshi_show,is_zhengcan_show,expiration_date,is_weight,member_price_json,weight_plu_code,weight_food_code,jiagong_type,cost_mode) VALUES ('" + id + "' ,'" + shop_id + "','" \
  755. + name + "', '" + price + "', '" + tag + "', '" + status + "', '" + type_id + "', '" + is_dabao + "', '" + dabao_money + "', '" + is_nature + "', '" + nature + "', '" \
  756. + autostocknum + "', '" + goods_img + "', '" + unit + "', '" + barcode + "', '" + member_price_used + "', '" + member_price + "', '" + buying_price + "', '" + stock + "', '" \
  757. + stockvalid + "', '" + stock_warning + "', '" + is_shouyinji_show + "', '" + is_waimai_show + "', '" + is_tangshi_show + "', '" + is_zhengcan_show + "', '" + expiration_date + "','" + is_weight + "','" + member_price_json + "','" + weight_plu_code + "','" + weight_food_code + "','" + jiagong_type + "','" + cost_mode + "')";
  758. bool ret = this->ExeSQl(sql);
  759. if (!ret)
  760. {
  761. LOG_INFO("商品插入失败,sql:" << sql.c_str());
  762. return false;
  763. }
  764. }
  765. this->ExeSQl("commit;");
  766. return true;
  767. }
  768. bool CSqlite3::InitFoodtypeData(rapidjson::Value& foodtyperows)
  769. {
  770. this->ExeSQl("begin;");
  771. //先清空之前的旧数据,重新完整写入新数据
  772. std::string sql = "delete from pos_foodtype;";
  773. this->ExeSQl(sql);
  774. for (rapidjson::SizeType i = 0; i < foodtyperows.Size(); ++i)
  775. {
  776. rapidjson::Value& foodtypeinfo = foodtyperows[i];
  777. std::string type_id = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodtypeinfo["type_id"]));
  778. std::string name = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodtypeinfo["name"]));
  779. std::string is_shouyinji_show = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodtypeinfo["is_shouyinji_show"]));
  780. std::string tag = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodtypeinfo["tag"]));
  781. //插入一个商品数据
  782. std::string sql = "INSERT INTO pos_foodtype (id,name,is_shouyinji_show,tag) VALUES ('" + type_id + "' ,'" + name + "', '" + is_shouyinji_show + "', '" + tag + "')";
  783. bool ret = this->ExeSQl(sql);
  784. if (!ret)
  785. {
  786. LOG_INFO("商品分类插入失败:" << i);
  787. return false;
  788. }
  789. }
  790. this->ExeSQl("commit;");
  791. return true;
  792. }
  793. bool CSqlite3::InitFoodpackageData(rapidjson::Value& foodpackagerows)
  794. {
  795. //先清空之前的旧数据,重新完整写入新数据
  796. this->ExeSQl("begin;");
  797. std::string sql = "delete from pos_foodpackage;";
  798. this->ExeSQl(sql);
  799. for (rapidjson::SizeType i = 0; i < foodpackagerows.Size(); ++i)
  800. {
  801. rapidjson::Value& foodinfo = foodpackagerows[i];
  802. std::string id = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["id"]));
  803. std::string shop_id = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["shop_id"]));
  804. std::string name = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["name"]));
  805. std::string price = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["price"]));
  806. std::string tag = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["tag"]));
  807. std::string status = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["status"]));
  808. std::string is_dabao = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["is_dabao"]));
  809. std::string dabao_money = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["dabao_money"]));
  810. std::string nature = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::JsonToString(foodinfo["nature"]));
  811. std::string goods_img = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["goods_img"]));
  812. std::string unit = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["unit"]));
  813. std::string supporttype = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["supporttype"]));
  814. std::string is_shouyinji_show = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["is_shouyinji_show"]));
  815. std::string barcode = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["barcode"]));
  816. //插入一个商品数据
  817. std::string sql = "INSERT INTO pos_foodpackage (id,shop_id,name,price,tag,status,is_dabao,dabao_money,nature,goods_img,unit,supporttype,is_shouyinji_show,barcode) VALUES ('" + id + "' ,'" + shop_id + "','" \
  818. + name + "', '" + price + "', '" + tag + "', '" + status + "', '" + is_dabao + "', '" + dabao_money + "', '" + nature + "', '" \
  819. + goods_img + "', '" + unit + "', '" + supporttype + "', '" + is_shouyinji_show + "', '" + barcode + "')";
  820. bool ret = this->ExeSQl(sql);
  821. if (!ret)
  822. {
  823. LOG_INFO("商品套餐插入失败:" << i);
  824. return false;
  825. }
  826. }
  827. this->ExeSQl("commit;");
  828. return true;
  829. }
  830. bool CSqlite3::UpdateOneFood(rapidjson::Value& foodrows)
  831. {
  832. this->ExeSQl("begin;");
  833. rapidjson::Value& foodinfo = foodrows;
  834. std::string id = CLewaimaiString::SqlZhuanyi(foodinfo["goods_id"].GetString());
  835. //不管当前有没有这个商品,执行一下删除
  836. std::string sql_delete = "delete from pos_food where id = '" + id + "';";
  837. this->ExeSQl(sql_delete);
  838. std::string shop_id = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["shop_id"]));
  839. std::string name = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["goods_name"]));
  840. std::string price = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["goods_price"]));
  841. std::string tag = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["goods_tag"]));
  842. std::string status = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["goods_status"]));
  843. std::string type_id = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["type_lv1_id"]));
  844. std::string is_dabao = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["is_dabao"]));
  845. std::string dabao_money = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["dabao_money"]));
  846. std::string is_nature = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["is_nature"]));
  847. std::string nature;
  848. if (is_nature == "1")
  849. {
  850. nature = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::JsonToString(foodinfo["nature"]));
  851. }
  852. else
  853. {
  854. nature = "";
  855. }
  856. std::string autostocknum = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["autostocknum"]));
  857. std::string goods_img = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["goods_img"]));
  858. std::string unit = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["unit"]));
  859. std::string barcode = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["barcode"]));
  860. std::string member_price_used = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["member_price_used"]));
  861. std::string member_price = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["member_price"]));
  862. std::string buying_price = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["buying_price"]));
  863. std::string stock = CLewaimaiString::SqlZhuanyi(std::to_string(foodinfo["stock"].GetDouble()));
  864. std::string stockvalid;
  865. std::string stock_status = CLewaimaiJson::ToString(foodinfo["stock_status"]);
  866. if (stock_status == "OPEN")
  867. {
  868. stockvalid = "1";
  869. }
  870. else
  871. {
  872. stockvalid = "0";
  873. }
  874. std::string stock_warning = CLewaimaiString::SqlZhuanyi(std::to_string(foodinfo["stock_warning"].GetDouble()));
  875. std::string is_shouyinji_show = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["is_shouyinji_show"]));
  876. std::string expiration_date = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["expiration_date"]));
  877. std::string is_weight = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["is_weight"]));
  878. std::string member_price_json = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["member_price_json"]));
  879. std::string is_waimai_show = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["is_waimai_show"]));
  880. std::string is_tangshi_show = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["is_tangshi_show"]));
  881. std::string is_zhengcan_show = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["is_zhengcan_show"]));
  882. std::string jiagong_type = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["jiagong_type"]));
  883. std::string cost_mode = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["cost_mode"]));
  884. std::string weight_plu_code;
  885. if (foodinfo["weight_plu_code"].IsString())
  886. {
  887. weight_plu_code = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["weight_plu_code"]));
  888. }
  889. else
  890. {
  891. weight_plu_code = "";
  892. }
  893. std::string weight_food_code;
  894. if (foodinfo["weight_food_code"].IsString())
  895. {
  896. weight_food_code = CLewaimaiString::SqlZhuanyi(CLewaimaiJson::ToString(foodinfo["weight_food_code"]));
  897. }
  898. else
  899. {
  900. weight_food_code = "";
  901. }
  902. //插入一个商品数据
  903. 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, \
  904. buying_price,stock,stockvalid,stock_warning,is_shouyinji_show,is_waimai_show,is_tangshi_show,is_zhengcan_show,expiration_date,is_weight,member_price_json,weight_plu_code,weight_food_code,jiagong_type,cost_mode) VALUES ('" + id + "' ,'" + shop_id + "','" \
  905. + name + "', '" + price + "', '" + tag + "', '" + status + "', '" + type_id + "', '" + is_dabao + "', '" + dabao_money + "', '" + is_nature + "', '" + nature + "', '" \
  906. + autostocknum + "', '" + goods_img + "', '" + unit + "', '" + barcode + "', '" + member_price_used + "', '" + member_price + "', '" + buying_price + "', '" + stock + "', '" \
  907. + stockvalid + "', '" + stock_warning + "', '" + is_shouyinji_show + "', '" + is_waimai_show + "', '" + is_tangshi_show + "', '" + is_zhengcan_show + "', '" + expiration_date + "','" + is_weight + "','" + member_price_json + "','" + weight_plu_code + "','" + weight_food_code + "','" + jiagong_type + "','" + cost_mode + "')";
  908. bool ret = this->ExeSQl(sql);
  909. if (!ret)
  910. {
  911. return false;
  912. }
  913. this->ExeSQl("commit;");
  914. return true;
  915. }
  916. bool CSqlite3::DeleteOneFood(std::string food_id)
  917. {
  918. food_id = CLewaimaiString::SqlZhuanyi(food_id);
  919. this->ExeSQl("begin;");
  920. //不管当前有没有这个商品,执行一下删除
  921. std::string sql_delete = "delete from pos_food where id = '" + food_id + "';";
  922. this->ExeSQl(sql_delete);
  923. this->ExeSQl("commit;");
  924. return true;
  925. }
  926. bool CSqlite3::AddOneFoodtype(std::string type_id, std::string name, std::string is_shouyinji_show, std::string tag)
  927. {
  928. this->ExeSQl("begin;");
  929. type_id = CLewaimaiString::SqlZhuanyi(type_id);
  930. name = CLewaimaiString::SqlZhuanyi(name);
  931. is_shouyinji_show = CLewaimaiString::SqlZhuanyi(is_shouyinji_show);
  932. tag = CLewaimaiString::SqlZhuanyi(tag);
  933. //插入一个商品数据
  934. std::string sql = "INSERT INTO pos_foodtype (id,name,is_shouyinji_show,tag) VALUES ('" + type_id + "' ,'" + name + "', '" + is_shouyinji_show + "', '" + tag + "')";
  935. bool ret = this->ExeSQl(sql);
  936. if (!ret)
  937. {
  938. LOG_INFO("商品分类插入失败:");
  939. return false;
  940. }
  941. this->ExeSQl("commit;");
  942. return true;
  943. }
  944. bool CSqlite3::ExeSQl(std::string sql)
  945. {
  946. char *zErrMsg = 0;
  947. int rc;
  948. rc = sqlite3_exec(m_db, sql.c_str(), 0, 0, &zErrMsg);
  949. if (rc != SQLITE_OK)
  950. {
  951. LOG_ERROR("SQL error: "<<zErrMsg);
  952. sqlite3_free(zErrMsg);
  953. return false;
  954. }
  955. else
  956. {
  957. return true;
  958. }
  959. return true;
  960. }
  961. //获取套餐商品的数量
  962. int CSqlite3::GetFoodpackageNum()
  963. {
  964. int count = 0;
  965. std::string sql = "SELECT count(*) as num FROM pos_foodpackage WHERE status = 'NORMAL';";
  966. sqlite3_stmt * stmt = NULL;
  967. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  968. {
  969. if (sqlite3_step(stmt) == SQLITE_ROW)
  970. {
  971. count = sqlite3_column_int(stmt, 0);
  972. }
  973. sqlite3_finalize(stmt);
  974. }
  975. else
  976. {
  977. //异常情况
  978. sqlite3_finalize(stmt);
  979. }
  980. return count;
  981. }
  982. std::vector<CFoodType> CSqlite3::GetFoodtypes(bool is_shouyinji_show)
  983. {
  984. std::vector<CFoodType> data;
  985. std::string sql;
  986. if (is_shouyinji_show)
  987. {
  988. sql = "SELECT * FROM pos_foodtype WHERE is_shouyinji_show = '1';";
  989. }
  990. else
  991. {
  992. sql = "SELECT * FROM pos_foodtype;";
  993. }
  994. sqlite3_stmt * stmt = NULL;
  995. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  996. {
  997. while (sqlite3_step(stmt) == SQLITE_ROW)
  998. {
  999. std::string id = (char*)sqlite3_column_text(stmt, 0);
  1000. std::string name = (char*)sqlite3_column_text(stmt, 1);
  1001. std::string is_shouyinji_show = (char*)sqlite3_column_text(stmt, 2);
  1002. std::string tag = (char*)sqlite3_column_text(stmt, 3);
  1003. CFoodType newtype;
  1004. newtype.id = id;
  1005. newtype.name = name;
  1006. newtype.is_shouyinji_show = is_shouyinji_show;
  1007. newtype.tag = tag;
  1008. data.push_back(newtype);
  1009. }
  1010. sqlite3_finalize(stmt);
  1011. }
  1012. else
  1013. {
  1014. //异常情况
  1015. sqlite3_finalize(stmt);
  1016. }
  1017. return data;
  1018. }
  1019. bool CSqlite3::GetFoodtypeById(std::string foodtype_id, CFoodType& newFoodType)
  1020. {
  1021. foodtype_id = CLewaimaiString::SqlZhuanyi(foodtype_id);
  1022. bool is_found = false;
  1023. std::string sql = "SELECT * FROM pos_foodtype WHERE id = '" + foodtype_id + "'";
  1024. sqlite3_stmt * stmt = NULL;
  1025. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  1026. {
  1027. while (sqlite3_step(stmt) == SQLITE_ROW)
  1028. {
  1029. is_found = true;
  1030. newFoodType.id = (char*)sqlite3_column_text(stmt, 0);
  1031. newFoodType.name = (char*)sqlite3_column_text(stmt, 1);
  1032. newFoodType.is_shouyinji_show = (char*)sqlite3_column_text(stmt, 2);
  1033. newFoodType.tag = (char*)sqlite3_column_text(stmt, 3);
  1034. }
  1035. sqlite3_finalize(stmt);
  1036. }
  1037. else
  1038. {
  1039. //异常情况
  1040. sqlite3_finalize(stmt);
  1041. }
  1042. return is_found;
  1043. }
  1044. /**
  1045. * 如果type_id为0,表示读取所有商品,否则只读取当前type_id的商品
  1046. */
  1047. std::vector<CFood> CSqlite3::GetFoodByTypeid(std::string type_id, bool is_shouyinji_show)
  1048. {
  1049. type_id = CLewaimaiString::SqlZhuanyi(type_id);
  1050. std::vector<CFood> data;
  1051. std::string sql;
  1052. if (is_shouyinji_show)
  1053. {
  1054. sql = "SELECT * FROM pos_food WHERE status='NORMAL' and is_shouyinji_show = '1'";
  1055. }
  1056. else
  1057. {
  1058. sql = "SELECT * FROM pos_food WHERE status='NORMAL'";
  1059. }
  1060. if (type_id != "0")
  1061. {
  1062. sql += " AND type_id = '" + type_id + "'";
  1063. }
  1064. sql += " ORDER BY tag ASC";
  1065. sqlite3_stmt * stmt = NULL;
  1066. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  1067. {
  1068. while (sqlite3_step(stmt) == SQLITE_ROW)
  1069. {
  1070. CFood newFood;
  1071. newFood.id = (char*)sqlite3_column_text(stmt, 0);
  1072. newFood.shop_id = (char*)sqlite3_column_text(stmt, 1);
  1073. newFood.name = (char*)sqlite3_column_text(stmt, 2);
  1074. newFood.price = (char*)sqlite3_column_text(stmt, 3);
  1075. newFood.tag = (char*)sqlite3_column_text(stmt, 4);
  1076. newFood.status = (char*)sqlite3_column_text(stmt, 5);
  1077. newFood.type_id = (char*)sqlite3_column_text(stmt, 6);
  1078. newFood.is_dabao = (char*)sqlite3_column_text(stmt, 7);
  1079. newFood.dabao_money = (char*)sqlite3_column_text(stmt, 8);
  1080. newFood.is_nature = (char*)sqlite3_column_text(stmt, 9);
  1081. newFood.nature = (char*)sqlite3_column_text(stmt, 10);
  1082. newFood.autostocknum = (char*)sqlite3_column_text(stmt, 11);
  1083. newFood.goods_img = (char*)sqlite3_column_text(stmt, 12);
  1084. newFood.unit = (char*)sqlite3_column_text(stmt, 13);
  1085. newFood.barcode = (char*)sqlite3_column_text(stmt, 14);
  1086. newFood.member_price_used = (char*)sqlite3_column_text(stmt, 15);
  1087. newFood.member_price = (char*)sqlite3_column_text(stmt, 16);
  1088. newFood.buying_price = (char*)sqlite3_column_text(stmt, 17);
  1089. newFood.stock = (char*)sqlite3_column_text(stmt, 18);
  1090. newFood.stockvalid = (char*)sqlite3_column_text(stmt, 19);
  1091. newFood.stock_warning = (char*)sqlite3_column_text(stmt, 20);
  1092. newFood.is_shouyinji_show = (char*)sqlite3_column_text(stmt, 21);
  1093. newFood.is_waimai_show = (char*)sqlite3_column_text(stmt, 22);
  1094. newFood.is_tangshi_show = (char*)sqlite3_column_text(stmt, 23);
  1095. newFood.is_zhengcan_show = (char*)sqlite3_column_text(stmt, 24);
  1096. newFood.expiration_date = (char*)sqlite3_column_text(stmt, 25);
  1097. newFood.is_weight = (char*)sqlite3_column_text(stmt, 26);
  1098. newFood.weight_plu_code = (char*)sqlite3_column_text(stmt, 27);
  1099. newFood.weight_food_code = (char*)sqlite3_column_text(stmt, 28);
  1100. newFood.jiagong_type = (char*)sqlite3_column_text(stmt, 29);
  1101. newFood.cost_mode = (char*)sqlite3_column_text(stmt, 30);
  1102. newFood.member_price_json = (char*)sqlite3_column_text(stmt, 31);
  1103. data.push_back(newFood);
  1104. }
  1105. sqlite3_finalize(stmt);
  1106. }
  1107. else
  1108. {
  1109. //异常情况
  1110. sqlite3_finalize(stmt);
  1111. }
  1112. return data;
  1113. }
  1114. std::vector<CFood> CSqlite3::GetFoodForTiaomacheng()
  1115. {
  1116. std::vector<CFood> data;
  1117. std::string sql;
  1118. sql = "SELECT * FROM pos_food WHERE is_weight = '1' AND weight_plu_code != '' AND weight_food_code != ''";
  1119. sqlite3_stmt * stmt = NULL;
  1120. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  1121. {
  1122. while (sqlite3_step(stmt) == SQLITE_ROW)
  1123. {
  1124. CFood newFood;
  1125. newFood.id = (char*)sqlite3_column_text(stmt, 0);
  1126. newFood.shop_id = (char*)sqlite3_column_text(stmt, 1);
  1127. newFood.name = (char*)sqlite3_column_text(stmt, 2);
  1128. newFood.price = (char*)sqlite3_column_text(stmt, 3);
  1129. newFood.tag = (char*)sqlite3_column_text(stmt, 4);
  1130. newFood.status = (char*)sqlite3_column_text(stmt, 5);
  1131. newFood.type_id = (char*)sqlite3_column_text(stmt, 6);
  1132. newFood.is_dabao = (char*)sqlite3_column_text(stmt, 7);
  1133. newFood.dabao_money = (char*)sqlite3_column_text(stmt, 8);
  1134. newFood.is_nature = (char*)sqlite3_column_text(stmt, 9);
  1135. newFood.nature = (char*)sqlite3_column_text(stmt, 10);
  1136. newFood.autostocknum = (char*)sqlite3_column_text(stmt, 11);
  1137. newFood.goods_img = (char*)sqlite3_column_text(stmt, 12);
  1138. newFood.unit = (char*)sqlite3_column_text(stmt, 13);
  1139. newFood.barcode = (char*)sqlite3_column_text(stmt, 14);
  1140. newFood.member_price_used = (char*)sqlite3_column_text(stmt, 15);
  1141. newFood.member_price = (char*)sqlite3_column_text(stmt, 16);
  1142. newFood.buying_price = (char*)sqlite3_column_text(stmt, 17);
  1143. newFood.stock = (char*)sqlite3_column_text(stmt, 18);
  1144. newFood.stockvalid = (char*)sqlite3_column_text(stmt, 19);
  1145. newFood.stock_warning = (char*)sqlite3_column_text(stmt, 20);
  1146. newFood.is_shouyinji_show = (char*)sqlite3_column_text(stmt, 21);
  1147. newFood.is_waimai_show = (char*)sqlite3_column_text(stmt, 22);
  1148. newFood.is_tangshi_show = (char*)sqlite3_column_text(stmt, 23);
  1149. newFood.is_zhengcan_show = (char*)sqlite3_column_text(stmt, 24);
  1150. newFood.expiration_date = (char*)sqlite3_column_text(stmt, 25);
  1151. newFood.is_weight = (char*)sqlite3_column_text(stmt, 26);
  1152. newFood.weight_plu_code = (char*)sqlite3_column_text(stmt, 27);
  1153. newFood.weight_food_code = (char*)sqlite3_column_text(stmt, 28);
  1154. newFood.jiagong_type = (char*)sqlite3_column_text(stmt, 29);
  1155. newFood.cost_mode = (char*)sqlite3_column_text(stmt, 30);
  1156. newFood.member_price_json = (char*)sqlite3_column_text(stmt, 31);
  1157. data.push_back(newFood);
  1158. }
  1159. sqlite3_finalize(stmt);
  1160. }
  1161. else
  1162. {
  1163. //异常情况
  1164. sqlite3_finalize(stmt);
  1165. }
  1166. return data;
  1167. }
  1168. std::vector<CFood> CSqlite3::GetFoodByFoodname(std::string foodname, bool is_shouyinji_show)
  1169. {
  1170. std::vector<CFood> data;
  1171. std::string sql;
  1172. foodname = CLewaimaiString::SqlZhuanyi(foodname);
  1173. if (is_shouyinji_show)
  1174. {
  1175. sql = "SELECT * FROM pos_food WHERE name LIKE '%" + foodname + "%' AND status='NORMAL' AND is_shouyinji_show = '1'";
  1176. }
  1177. else
  1178. {
  1179. sql = "SELECT * FROM pos_food WHERE name LIKE '%" + foodname + "%' AND status='NORMAL'";
  1180. }
  1181. sql += " ORDER BY tag";
  1182. sqlite3_stmt * stmt = NULL;
  1183. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  1184. {
  1185. while (sqlite3_step(stmt) == SQLITE_ROW)
  1186. {
  1187. CFood newFood;
  1188. newFood.id = (char*)sqlite3_column_text(stmt, 0);
  1189. newFood.shop_id = (char*)sqlite3_column_text(stmt, 1);
  1190. newFood.name = (char*)sqlite3_column_text(stmt, 2);
  1191. newFood.price = (char*)sqlite3_column_text(stmt, 3);
  1192. newFood.tag = (char*)sqlite3_column_text(stmt, 4);
  1193. newFood.status = (char*)sqlite3_column_text(stmt, 5);
  1194. newFood.type_id = (char*)sqlite3_column_text(stmt, 6);
  1195. newFood.is_dabao = (char*)sqlite3_column_text(stmt, 7);
  1196. newFood.dabao_money = (char*)sqlite3_column_text(stmt, 8);
  1197. newFood.is_nature = (char*)sqlite3_column_text(stmt, 9);
  1198. newFood.nature = (char*)sqlite3_column_text(stmt, 10);
  1199. newFood.autostocknum = (char*)sqlite3_column_text(stmt, 11);
  1200. newFood.goods_img = (char*)sqlite3_column_text(stmt, 12);
  1201. newFood.unit = (char*)sqlite3_column_text(stmt, 13);
  1202. newFood.barcode = (char*)sqlite3_column_text(stmt, 14);
  1203. newFood.member_price_used = (char*)sqlite3_column_text(stmt, 15);
  1204. newFood.member_price = (char*)sqlite3_column_text(stmt, 16);
  1205. newFood.buying_price = (char*)sqlite3_column_text(stmt, 17);
  1206. newFood.stock = (char*)sqlite3_column_text(stmt, 18);
  1207. newFood.stockvalid = (char*)sqlite3_column_text(stmt, 19);
  1208. newFood.stock_warning = (char*)sqlite3_column_text(stmt, 20);
  1209. newFood.is_shouyinji_show = (char*)sqlite3_column_text(stmt, 21);
  1210. newFood.is_waimai_show = (char*)sqlite3_column_text(stmt, 22);
  1211. newFood.is_tangshi_show = (char*)sqlite3_column_text(stmt, 23);
  1212. newFood.is_zhengcan_show = (char*)sqlite3_column_text(stmt, 24);
  1213. newFood.expiration_date = (char*)sqlite3_column_text(stmt, 25);
  1214. newFood.is_weight = (char*)sqlite3_column_text(stmt, 26);
  1215. newFood.weight_plu_code = (char*)sqlite3_column_text(stmt, 27);
  1216. newFood.weight_food_code = (char*)sqlite3_column_text(stmt, 28);
  1217. newFood.jiagong_type = (char*)sqlite3_column_text(stmt, 29);
  1218. newFood.cost_mode = (char*)sqlite3_column_text(stmt, 30);
  1219. newFood.member_price_json = (char*)sqlite3_column_text(stmt, 31);
  1220. data.push_back(newFood);
  1221. }
  1222. sqlite3_finalize(stmt);
  1223. }
  1224. else
  1225. {
  1226. //异常情况
  1227. sqlite3_finalize(stmt);
  1228. }
  1229. return data;
  1230. }
  1231. std::vector<CFoodpackage> CSqlite3::GetFoodpackages(bool is_shouyinji_show)
  1232. {
  1233. std::vector<CFoodpackage> data;
  1234. std::string sql;
  1235. if (is_shouyinji_show)
  1236. {
  1237. sql = "SELECT * FROM pos_foodpackage WHERE status='NORMAL' AND is_shouyinji_show = '1' ORDER BY tag";
  1238. }
  1239. else
  1240. {
  1241. sql = "SELECT * FROM pos_foodpackage WHERE status='NORMAL' ORDER BY tag";
  1242. }
  1243. sqlite3_stmt * stmt = NULL;
  1244. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  1245. {
  1246. while (sqlite3_step(stmt) == SQLITE_ROW)
  1247. {
  1248. CFoodpackage newFood;
  1249. newFood.id = (char*)sqlite3_column_text(stmt, 0);
  1250. newFood.shop_id = (char*)sqlite3_column_text(stmt, 1);
  1251. newFood.name = (char*)sqlite3_column_text(stmt, 2);
  1252. newFood.price = (char*)sqlite3_column_text(stmt, 3);
  1253. newFood.tag = (char*)sqlite3_column_text(stmt, 4);
  1254. newFood.status = (char*)sqlite3_column_text(stmt, 5);
  1255. newFood.is_dabao = (char*)sqlite3_column_text(stmt, 6);
  1256. newFood.dabao_money = (char*)sqlite3_column_text(stmt, 7);
  1257. newFood.nature = (char*)sqlite3_column_text(stmt, 8);
  1258. newFood.goods_img = (char*)sqlite3_column_text(stmt, 9);
  1259. newFood.unit = (char*)sqlite3_column_text(stmt, 10);
  1260. newFood.supporttype = (char*)sqlite3_column_text(stmt, 11);
  1261. newFood.is_shouyinji_show = (char*)sqlite3_column_text(stmt, 12);
  1262. newFood.barcode = (char*)sqlite3_column_text(stmt, 13);
  1263. data.push_back(newFood);
  1264. }
  1265. sqlite3_finalize(stmt);
  1266. }
  1267. else
  1268. {
  1269. //异常情况
  1270. sqlite3_finalize(stmt);
  1271. }
  1272. return data;
  1273. }
  1274. bool CSqlite3::GetFoodById(std::string food_id, CFood& newFood)
  1275. {
  1276. food_id = CLewaimaiString::SqlZhuanyi(food_id);
  1277. bool is_found = false;
  1278. std::string sql = "SELECT * FROM pos_food WHERE id = '" + food_id + "'";
  1279. sqlite3_stmt * stmt = NULL;
  1280. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  1281. {
  1282. while (sqlite3_step(stmt) == SQLITE_ROW)
  1283. {
  1284. is_found = true;
  1285. newFood.id = (char*)sqlite3_column_text(stmt, 0);
  1286. newFood.shop_id = (char*)sqlite3_column_text(stmt, 1);
  1287. newFood.name = (char*)sqlite3_column_text(stmt, 2);
  1288. newFood.price = (char*)sqlite3_column_text(stmt, 3);
  1289. newFood.tag = (char*)sqlite3_column_text(stmt, 4);
  1290. newFood.status = (char*)sqlite3_column_text(stmt, 5);
  1291. newFood.type_id = (char*)sqlite3_column_text(stmt, 6);
  1292. newFood.is_dabao = (char*)sqlite3_column_text(stmt, 7);
  1293. newFood.dabao_money = (char*)sqlite3_column_text(stmt, 8);
  1294. newFood.is_nature = (char*)sqlite3_column_text(stmt, 9);
  1295. newFood.nature = (char*)sqlite3_column_text(stmt, 10);
  1296. newFood.autostocknum = (char*)sqlite3_column_text(stmt, 11);
  1297. newFood.goods_img = (char*)sqlite3_column_text(stmt, 12);
  1298. newFood.unit = (char*)sqlite3_column_text(stmt, 13);
  1299. newFood.barcode = (char*)sqlite3_column_text(stmt, 14);
  1300. newFood.member_price_used = (char*)sqlite3_column_text(stmt, 15);
  1301. newFood.member_price = (char*)sqlite3_column_text(stmt, 16);
  1302. newFood.buying_price = (char*)sqlite3_column_text(stmt, 17);
  1303. newFood.stock = (char*)sqlite3_column_text(stmt, 18);
  1304. newFood.stockvalid = (char*)sqlite3_column_text(stmt, 19);
  1305. newFood.stock_warning = (char*)sqlite3_column_text(stmt, 20);
  1306. newFood.is_shouyinji_show = (char*)sqlite3_column_text(stmt, 21);
  1307. newFood.is_waimai_show = (char*)sqlite3_column_text(stmt, 22);
  1308. newFood.is_tangshi_show = (char*)sqlite3_column_text(stmt, 23);
  1309. newFood.is_zhengcan_show = (char*)sqlite3_column_text(stmt, 24);
  1310. newFood.expiration_date = (char*)sqlite3_column_text(stmt, 25);
  1311. newFood.is_weight = (char*)sqlite3_column_text(stmt, 26);
  1312. newFood.weight_plu_code = (char*)sqlite3_column_text(stmt, 27);
  1313. newFood.weight_food_code = (char*)sqlite3_column_text(stmt, 28);
  1314. newFood.jiagong_type = (char*)sqlite3_column_text(stmt, 29);
  1315. newFood.cost_mode = (char*)sqlite3_column_text(stmt, 30);
  1316. newFood.member_price_json = (char*)sqlite3_column_text(stmt, 31);
  1317. }
  1318. sqlite3_finalize(stmt);
  1319. }
  1320. else
  1321. {
  1322. //异常情况
  1323. sqlite3_finalize(stmt);
  1324. }
  1325. return is_found;
  1326. }
  1327. bool CSqlite3::GetFoodByBarcode(std::string barcode, CFood& newFood)
  1328. {
  1329. bool is_found = false;
  1330. barcode = CLewaimaiString::SqlZhuanyi(barcode);
  1331. std::string sql = "SELECT * FROM pos_food WHERE barcode = '" + barcode + "'";
  1332. sqlite3_stmt * stmt = NULL;
  1333. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  1334. {
  1335. while (sqlite3_step(stmt) == SQLITE_ROW)
  1336. {
  1337. is_found = true;
  1338. newFood.id = (char*)sqlite3_column_text(stmt, 0);
  1339. newFood.shop_id = (char*)sqlite3_column_text(stmt, 1);
  1340. newFood.name = (char*)sqlite3_column_text(stmt, 2);
  1341. newFood.price = (char*)sqlite3_column_text(stmt, 3);
  1342. newFood.tag = (char*)sqlite3_column_text(stmt, 4);
  1343. newFood.status = (char*)sqlite3_column_text(stmt, 5);
  1344. newFood.type_id = (char*)sqlite3_column_text(stmt, 6);
  1345. newFood.is_dabao = (char*)sqlite3_column_text(stmt, 7);
  1346. newFood.dabao_money = (char*)sqlite3_column_text(stmt, 8);
  1347. newFood.is_nature = (char*)sqlite3_column_text(stmt, 9);
  1348. newFood.nature = (char*)sqlite3_column_text(stmt, 10);
  1349. newFood.autostocknum = (char*)sqlite3_column_text(stmt, 11);
  1350. newFood.goods_img = (char*)sqlite3_column_text(stmt, 12);
  1351. newFood.unit = (char*)sqlite3_column_text(stmt, 13);
  1352. newFood.barcode = (char*)sqlite3_column_text(stmt, 14);
  1353. newFood.member_price_used = (char*)sqlite3_column_text(stmt, 15);
  1354. newFood.member_price = (char*)sqlite3_column_text(stmt, 16);
  1355. newFood.buying_price = (char*)sqlite3_column_text(stmt, 17);
  1356. newFood.stock = (char*)sqlite3_column_text(stmt, 18);
  1357. newFood.stockvalid = (char*)sqlite3_column_text(stmt, 19);
  1358. newFood.stock_warning = (char*)sqlite3_column_text(stmt, 20);
  1359. newFood.is_shouyinji_show = (char*)sqlite3_column_text(stmt, 21);
  1360. newFood.is_waimai_show = (char*)sqlite3_column_text(stmt, 22);
  1361. newFood.is_tangshi_show = (char*)sqlite3_column_text(stmt, 23);
  1362. newFood.is_zhengcan_show = (char*)sqlite3_column_text(stmt, 24);
  1363. newFood.expiration_date = (char*)sqlite3_column_text(stmt, 25);
  1364. newFood.is_weight = (char*)sqlite3_column_text(stmt, 26);
  1365. newFood.weight_plu_code = (char*)sqlite3_column_text(stmt, 27);
  1366. newFood.weight_food_code = (char*)sqlite3_column_text(stmt, 28);
  1367. newFood.jiagong_type = (char*)sqlite3_column_text(stmt, 29);
  1368. newFood.cost_mode = (char*)sqlite3_column_text(stmt, 30);
  1369. newFood.member_price_json = (char*)sqlite3_column_text(stmt, 31);
  1370. }
  1371. sqlite3_finalize(stmt);
  1372. }
  1373. else
  1374. {
  1375. //异常情况
  1376. sqlite3_finalize(stmt);
  1377. }
  1378. return is_found;
  1379. }
  1380. bool CSqlite3::GetFoodByPluBianma(std::string barcode, CFood& newFood)
  1381. {
  1382. bool is_found = false;
  1383. barcode = CLewaimaiString::SqlZhuanyi(barcode);
  1384. std::string sql = "SELECT * FROM pos_food WHERE is_weight = '1' AND weight_food_code = '" + barcode + "'";
  1385. sqlite3_stmt * stmt = NULL;
  1386. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  1387. {
  1388. while (sqlite3_step(stmt) == SQLITE_ROW)
  1389. {
  1390. is_found = true;
  1391. newFood.id = (char*)sqlite3_column_text(stmt, 0);
  1392. newFood.shop_id = (char*)sqlite3_column_text(stmt, 1);
  1393. newFood.name = (char*)sqlite3_column_text(stmt, 2);
  1394. newFood.price = (char*)sqlite3_column_text(stmt, 3);
  1395. newFood.tag = (char*)sqlite3_column_text(stmt, 4);
  1396. newFood.status = (char*)sqlite3_column_text(stmt, 5);
  1397. newFood.type_id = (char*)sqlite3_column_text(stmt, 6);
  1398. newFood.is_dabao = (char*)sqlite3_column_text(stmt, 7);
  1399. newFood.dabao_money = (char*)sqlite3_column_text(stmt, 8);
  1400. newFood.is_nature = (char*)sqlite3_column_text(stmt, 9);
  1401. newFood.nature = (char*)sqlite3_column_text(stmt, 10);
  1402. newFood.autostocknum = (char*)sqlite3_column_text(stmt, 11);
  1403. newFood.goods_img = (char*)sqlite3_column_text(stmt, 12);
  1404. newFood.unit = (char*)sqlite3_column_text(stmt, 13);
  1405. newFood.barcode = (char*)sqlite3_column_text(stmt, 14);
  1406. newFood.member_price_used = (char*)sqlite3_column_text(stmt, 15);
  1407. newFood.member_price = (char*)sqlite3_column_text(stmt, 16);
  1408. newFood.buying_price = (char*)sqlite3_column_text(stmt, 17);
  1409. newFood.stock = (char*)sqlite3_column_text(stmt, 18);
  1410. newFood.stockvalid = (char*)sqlite3_column_text(stmt, 19);
  1411. newFood.stock_warning = (char*)sqlite3_column_text(stmt, 20);
  1412. newFood.is_shouyinji_show = (char*)sqlite3_column_text(stmt, 21);
  1413. newFood.is_waimai_show = (char*)sqlite3_column_text(stmt, 22);
  1414. newFood.is_tangshi_show = (char*)sqlite3_column_text(stmt, 23);
  1415. newFood.is_zhengcan_show = (char*)sqlite3_column_text(stmt, 24);
  1416. newFood.expiration_date = (char*)sqlite3_column_text(stmt, 25);
  1417. newFood.is_weight = (char*)sqlite3_column_text(stmt, 26);
  1418. newFood.weight_plu_code = (char*)sqlite3_column_text(stmt, 27);
  1419. newFood.weight_food_code = (char*)sqlite3_column_text(stmt, 28);
  1420. newFood.jiagong_type = (char*)sqlite3_column_text(stmt, 29);
  1421. newFood.cost_mode = (char*)sqlite3_column_text(stmt, 30);
  1422. newFood.member_price_json = (char*)sqlite3_column_text(stmt, 31);
  1423. }
  1424. sqlite3_finalize(stmt);
  1425. }
  1426. else
  1427. {
  1428. //异常情况
  1429. sqlite3_finalize(stmt);
  1430. }
  1431. return is_found;
  1432. }
  1433. bool CSqlite3::GetFoodpackageById(std::string foodpackage_id, CFoodpackage& newFoodpackage)
  1434. {
  1435. bool is_found = false;
  1436. std::string sql = "SELECT * FROM pos_foodpackage WHERE id = '" + foodpackage_id + "'";
  1437. sqlite3_stmt * stmt = NULL;
  1438. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  1439. {
  1440. while (sqlite3_step(stmt) == SQLITE_ROW)
  1441. {
  1442. is_found = true;
  1443. newFoodpackage.id = (char*)sqlite3_column_text(stmt, 0);
  1444. newFoodpackage.shop_id = (char*)sqlite3_column_text(stmt, 1);
  1445. newFoodpackage.name = (char*)sqlite3_column_text(stmt, 2);
  1446. newFoodpackage.price = (char*)sqlite3_column_text(stmt, 3);
  1447. newFoodpackage.tag = (char*)sqlite3_column_text(stmt, 4);
  1448. newFoodpackage.status = (char*)sqlite3_column_text(stmt, 5);
  1449. newFoodpackage.is_dabao = (char*)sqlite3_column_text(stmt, 6);
  1450. newFoodpackage.dabao_money = (char*)sqlite3_column_text(stmt, 7);
  1451. newFoodpackage.nature = (char*)sqlite3_column_text(stmt, 8);
  1452. newFoodpackage.goods_img = (char*)sqlite3_column_text(stmt, 9);
  1453. newFoodpackage.unit = (char*)sqlite3_column_text(stmt, 10);
  1454. newFoodpackage.supporttype = (char*)sqlite3_column_text(stmt, 11);
  1455. newFoodpackage.is_shouyinji_show = (char*)sqlite3_column_text(stmt, 12);
  1456. newFoodpackage.barcode = (char*)sqlite3_column_text(stmt, 13);
  1457. }
  1458. sqlite3_finalize(stmt);
  1459. }
  1460. else
  1461. {
  1462. //异常情况
  1463. sqlite3_finalize(stmt);
  1464. }
  1465. return is_found;
  1466. }
  1467. //更新商品ID的库存
  1468. void CSqlite3::UpdateFoodStock(std::string food_id, std::string stock)
  1469. {
  1470. food_id = CLewaimaiString::SqlZhuanyi(food_id);
  1471. int result = sqlite3_exec(m_db, "BEGIN;", 0, 0, 0);
  1472. std::string sql = "UPDATE pos_food SET stock = '" + stock + "' WHERE id = " + food_id;
  1473. result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
  1474. result = sqlite3_exec(m_db, "COMMIT;", 0, 0, 0);
  1475. }