CSqlite3.cpp 45 KB

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