| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593 |
- #include "../pch/pch.h"
- #include "CSqlite3.h"
- #include "CSetting.h"
- CSqlite3::CSqlite3()
- {
- wstring folderPath = CSystem::GetProgramDir() + L"\\db";
- if (!CSystem::IsDirExist(folderPath))
- {
- LOG_INFO("folderPath:" << folderPath.c_str()<<",没有找到对应的目录,即将创建");
- bool flag = CreateDirectory(folderPath.c_str(), NULL);
- if (flag == false)
- {
- LOG_INFO("新建 db 目录失败!");
- }
- LOG_INFO("新建 db 目录成功!");
- }
- //如果没有这个文件,这里会创建这个文件
- wstring path = CSystem::GetProgramDir() + L"\\db\\pos.db";
- string s_path = CLewaimaiString::UnicodeToUTF8(path);
- m_rc = sqlite3_open(s_path.c_str(), &m_db);
- if(m_rc)
- {
- LOG_INFO("Can't open database: " << sqlite3_errmsg(m_db));
- return;
- }
- else
- {
- //LOG_INFO("Opened database successfully");
- }
- }
- CSqlite3::~CSqlite3()
- {
- if(m_db != NULL)
- {
- sqlite3_close(m_db);
- }
- }
- /**
- * 对数据库表进行一些初始化和检查,这个函数每次程序启动的时候执行1次,就不会再执行了
- */
- bool CSqlite3::InitDB()
- {
- //先处理pos_config表
- bool ret = this->InitPosConfig();
- if (!ret)
- {
- return false;
- }
- //再处理pos_chufang_printer表
- ret = this->InitPosChufangPrinter();
- if (!ret)
- {
- return false;
- }
- //再处理pos_user表
- ret = this->InitPosUser();
- if (!ret)
- {
- return false;
- }
- //再处理pos_food表
- ret = this->InitPosFood();
- if (!ret)
- {
- return false;
- }
- //再处理pos_foodtype表
- ret = this->InitPosFoodType();
- if (!ret)
- {
- return false;
- }
- //再处理pos_foodpackage表
- ret = this->InitPosFoodPackage();
- if (!ret)
- {
- return false;
- }
- return true;
- }
- bool CSqlite3::InitPosConfig()
- {
- //检查有没有pos_config这个表,如果没有就创建
- std::string sql = "SELECT COUNT(*) FROM sqlite_master where type = 'table' and name = 'pos_config';";
- sqlite3_stmt * stmt = NULL;
- if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
- {
- if (sqlite3_step(stmt) == SQLITE_ROW)
- {
- int count = sqlite3_column_int(stmt, 0);
- if (count == 0)
- {
- //说明没找到这个表,那么这个时候新建这个表,先释放前面的stmt
- sqlite3_finalize(stmt);
- stmt = NULL;
- sql = "CREATE TABLE pos_config(" \
- "name CHAR(100) UNIQUE NOT NULL," \
- "value CHAR(2000) NOT NULL);";
- if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
- {
- //执行该语句
- if (sqlite3_step(stmt) != SQLITE_DONE)
- {
- LOG_ERROR("create table fail: " << sqlite3_errmsg(m_db));
- sqlite3_finalize(stmt);
- return false;
- }
- //走到这里就是表创建成功了
- //LOG_INFO("create table success");
- sqlite3_finalize(stmt);
- }
- else
- {
- LOG_ERROR("create table prepare fail: " << sqlite3_errmsg(m_db));
- sqlite3_finalize(stmt);
- return false;
- }
- }
- else
- {
- //说明已经有这个表了,就不用再创建了
- sqlite3_finalize(stmt);
- }
- std::string sql = "SELECT * FROM pos_config;";
- sqlite3_stmt * stmt = NULL;
- if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
- {
- while (sqlite3_step(stmt) == SQLITE_ROW)
- {
- std::string name = (char*)sqlite3_column_text(stmt, 0);
- std::string value = (char*)sqlite3_column_text(stmt, 1);
- CSetting::GetInstance()->SetParam(name, value, false);
- }
- sqlite3_finalize(stmt);
- }
- else
- {
- //异常情况
- sqlite3_finalize(stmt);
- return false;
- }
- }
- else
- {
- //异常情况
- sqlite3_finalize(stmt);
- return false;
- }
- }
- else
- {
- //异常情况
- sqlite3_finalize(stmt);
- return false;
- }
- return true;
- }
- bool CSqlite3::InitPosChufangPrinter()
- {
- std::string sql = "SELECT COUNT(*) FROM sqlite_master where type = 'table' and name = 'pos_chufang_printer';";
- sqlite3_stmt * stmt = NULL;
- //读取厨房打印机的参数
- if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
- {
- if (sqlite3_step(stmt) == SQLITE_ROW)
- {
- int count = sqlite3_column_int(stmt, 0);
- if (count == 0)
- {
- //说明没找到这个表,那么这个时候新建这个表,先释放前面的stmt
- sqlite3_finalize(stmt);
- stmt = NULL;
- sql = "CREATE TABLE pos_chufang_printer(" \
- "id INTEGER PRIMARY KEY AUTOINCREMENT,"\
- "date CHAR(100) NOT NULL," \
- "name CHAR(100) NOT NULL," \
- "ip CHAR(100) NOT NULL," \
- "guige CHAR(100) NOT NULL," \
- "fendan CHAR(100) NOT NULL," \
- "fenlei CHAR(100) NOT NULL," \
- "fenlei_ids CHAR(2000) );";
- if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
- {
- //执行该语句
- if (sqlite3_step(stmt) != SQLITE_DONE)
- {
- std::string err = sqlite3_errmsg(m_db);
- LOG_INFO("create table fail: " << err.c_str());
- sqlite3_finalize(stmt);
- return false;
- }
- //走到这里就是表创建成功了
- sqlite3_finalize(stmt);
- }
- else
- {
- LOG_INFO("create table prepare fail: " << sqlite3_errmsg(m_db));
- sqlite3_finalize(stmt);
- return false;
- }
- }
- else
- {
- //说明已经有这个表了,就不用再创建了
- sqlite3_finalize(stmt);
- }
- //兼容性检查,判断字段是否存在
- std::string sql_pandduan = "select COUNT(*) from sqlite_master where type = 'table' and tbl_name = 'pos_chufang_printer' and sql like '%fenlei%';";
- stmt = NULL;
- if (sqlite3_prepare_v2(m_db, sql_pandduan.c_str(), -1, &stmt, NULL) == SQLITE_OK)
- {
- if (sqlite3_step(stmt) == SQLITE_ROW)
- {
- int count = sqlite3_column_int(stmt, 0);
- if (count == 0)
- {
- //说明没找到这个字段,准备添加字段
- sqlite3_finalize(stmt);
- stmt = NULL;
- sql = "ALTER TABLE pos_chufang_printer " \
- "add fenlei CHAR(100) NOT NULL DEFAULT '0';";
- if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
- {
- //执行该语句
- if (sqlite3_step(stmt) != SQLITE_DONE)
- {
- std::string err = sqlite3_errmsg(m_db);
- LOG_INFO("alter table fail: " << err.c_str());
- sqlite3_finalize(stmt);
- return false;
- }
- //走到这里就是表创建成功了
- LOG_INFO("alter table success");
- sqlite3_finalize(stmt);
- }
- else
- {
- LOG_INFO("alter table prepare fail: " << sqlite3_errmsg(m_db));
- sqlite3_finalize(stmt);
- return false;
- }
- }
- else
- {
- //说明已经有这2个字段了
- sqlite3_finalize(stmt);
- }
- }
- }
- else
- {
- //异常情况
- sqlite3_finalize(stmt);
- return false;
- }
- sql_pandduan = "select COUNT(*) from sqlite_master where type = 'table' and tbl_name = 'pos_chufang_printer' and sql like '%fenlei_ids%';";
- stmt = NULL;
- if (sqlite3_prepare_v2(m_db, sql_pandduan.c_str(), -1, &stmt, NULL) == SQLITE_OK)
- {
- if (sqlite3_step(stmt) == SQLITE_ROW)
- {
- int count = sqlite3_column_int(stmt, 0);
- if (count == 0)
- {
- //说明没找到这个字段,准备添加字段
- sqlite3_finalize(stmt);
- stmt = NULL;
- sql = "ALTER TABLE pos_chufang_printer " \
- "add fenlei_ids CHAR(2000) NOT NULL DEFAULT '';";
- if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
- {
- //执行该语句
- if (sqlite3_step(stmt) != SQLITE_DONE)
- {
- std::string err = sqlite3_errmsg(m_db);
- LOG_INFO("alter table fail: " << err.c_str());
- sqlite3_finalize(stmt);
- return false;
- }
- //走到这里就是表创建成功了
- sqlite3_finalize(stmt);
- }
- else
- {
- LOG_INFO("alter table prepare fail: " << sqlite3_errmsg(m_db));
- sqlite3_finalize(stmt);
- return false;;
- }
- }
- else
- {
- //说明已经有这2个字段了
- sqlite3_finalize(stmt);
- }
- }
- }
- else
- {
- //异常情况
- sqlite3_finalize(stmt);
- return false;
- }
- std::string sql = "SELECT * FROM pos_chufang_printer;";
- stmt = NULL;
- if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
- {
- while (sqlite3_step(stmt) == SQLITE_ROW)
- {
- std::string date = (char*)sqlite3_column_text(stmt, 1);
- std::string name = (char*)sqlite3_column_text(stmt, 2);
- std::string ip = (char*)sqlite3_column_text(stmt, 3);
- std::string guige = (char*)sqlite3_column_text(stmt, 4);
- std::string fendan = (char*)sqlite3_column_text(stmt, 5);
- std::string fenlei = (char*)sqlite3_column_text(stmt, 6);
- std::string fenlei_ids = (char*)sqlite3_column_text(stmt, 7);
- //这里仅仅是把数据库内容读到内存,所以之类用false
- CSetting::GetInstance()->AddChufangPrinter(date, name, ip, guige, fendan, fenlei, fenlei_ids, false);
- }
- sqlite3_finalize(stmt);
- }
- else
- {
- //异常情况
- sqlite3_finalize(stmt);
- return false;
- }
- }
- }
- return true;
- }
- bool CSqlite3::InitPosUser()
- {
- std::string sql = "SELECT COUNT(*) FROM sqlite_master where type = 'table' and name = 'pos_user';";
- sqlite3_stmt * stmt = NULL;
- //读取厨房打印机的参数
- if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
- {
- if (sqlite3_step(stmt) == SQLITE_ROW)
- {
- int count = sqlite3_column_int(stmt, 0);
- if (count == 0)
- {
- //说明没找到这个表,那么这个时候新建这个表,先释放前面的stmt
- sqlite3_finalize(stmt);
- stmt = NULL;
- sql = "CREATE TABLE pos_user(" \
- "id INTEGER PRIMARY KEY AUTOINCREMENT,"\
- "username CHAR(100) NOT NULL," \
- "password CHAR(100) NOT NULL);";
- if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
- {
- //执行该语句
- if (sqlite3_step(stmt) != SQLITE_DONE)
- {
- std::string err = sqlite3_errmsg(m_db);
- LOG_INFO("create table fail: " << err.c_str());
- sqlite3_finalize(stmt);
- return false;
- }
- //走到这里就是表创建成功了
- //LOG_INFO("create table success");
- sqlite3_finalize(stmt);
- }
- else
- {
- LOG_INFO("create table prepare fail: " << sqlite3_errmsg(m_db));
- sqlite3_finalize(stmt);
- return false;
- }
- }
- else
- {
- //说明已经有这个表了,就不用再创建了
- sqlite3_finalize(stmt);
- }
- std::string sql = "SELECT * FROM pos_user;";
- sqlite3_stmt * stmt = NULL;
- if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
- {
- while (sqlite3_step(stmt) == SQLITE_ROW)
- {
- std::string username = (char*)sqlite3_column_text(stmt, 1);
- std::string password = (char*)sqlite3_column_text(stmt, 2);
- //这里仅仅是把数据库内容读到内存,所以之类用false
- CSetting::GetInstance()->SetUser(username, password);
- }
- sqlite3_finalize(stmt);
- }
- else
- {
- //异常情况
- sqlite3_finalize(stmt);
- return false;
- }
- }
- }
- return true;
- }
- bool CSqlite3::InitPosFood()
- {
- //检查有没有pos_food这个表,如果没有就创建
- std::string sql = "SELECT COUNT(*) FROM sqlite_master where type = 'table' and name = 'pos_food';";
- sqlite3_stmt * stmt = NULL;
- if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
- {
- if (sqlite3_step(stmt) == SQLITE_ROW)
- {
- int count = sqlite3_column_int(stmt, 0);
- if (count == 0)
- {
- //说明没找到这个表,那么这个时候新建这个表,先释放前面的stmt
- sqlite3_finalize(stmt);
- stmt = NULL;
- sql = "CREATE TABLE pos_food(" \
- "id CHAR(20) UNIQUE NOT NULL,"\
- "shop_id CHAR(200) NOT NULL," \
- "name CHAR(100) NOT NULL," \
- "price CHAR(100) NOT NULL," \
- "tag CHAR(100) NOT NULL," \
- "status CHAR(100) NOT NULL," \
- "type_id CHAR(20) NOT NULL," \
- "is_dabao CHAR(20) NOT NULL," \
- "dabao_money CHAR(100) NOT NULL," \
- "is_nature CHAR(20) NOT NULL," \
- "nature CHAR(10000) NOT NULL," \
- "autostocknum CHAR(100) NOT NULL," \
- "goods_img CHAR(100) NOT NULL," \
- "unit CHAR(100) NOT NULL," \
- "barcode CHAR(100) NOT NULL," \
- "member_price_used CHAR(100) NOT NULL," \
- "member_price CHAR(100) NOT NULL," \
- "buying_price CHAR(100) NOT NULL," \
- "stock CHAR(100) NOT NULL," \
- "stockvalid CHAR(100) NOT NULL," \
- "stock_warning CHAR(100) NOT NULL," \
- "is_shouyinji_show CHAR(20) NOT NULL," \
- "expiration_date CHAR(100) NOT NULL," \
- "is_weight CHAR(20) NOT NULL," \
- "weight_plu_code CHAR(20) NOT NULL," \
- "weight_food_code CHAR(20) NOT NULL," \
- "member_price_json CHAR(100) NOT NULL);";
- if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
- {
- //执行该语句
- if (sqlite3_step(stmt) != SQLITE_DONE)
- {
- LOG_ERROR("create table fail: " << sqlite3_errmsg(m_db));
- sqlite3_finalize(stmt);
- return false;
- }
- //走到这里就是表创建成功了
- //LOG_INFO("create table success");
- sqlite3_finalize(stmt);
- }
- else
- {
- LOG_ERROR("create table prepare fail: " << sqlite3_errmsg(m_db));
- sqlite3_finalize(stmt);
- return false;
- }
- }
- else
- {
- //说明已经有这个表了,就不用再创建了
- sqlite3_finalize(stmt);
- }
- }
- else
- {
- //异常情况
- sqlite3_finalize(stmt);
- return false;
- }
- }
- else
- {
- //异常情况
- sqlite3_finalize(stmt);
- return false;
- }
- return true;
- }
- bool CSqlite3::InitPosFoodType()
- {
- //检查有没有pos_foodtype这个表,如果没有就创建
- std::string sql = "SELECT COUNT(*) FROM sqlite_master where type = 'table' and name = 'pos_foodtype';";
- sqlite3_stmt * stmt = NULL;
- if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
- {
- if (sqlite3_step(stmt) == SQLITE_ROW)
- {
- int count = sqlite3_column_int(stmt, 0);
- if (count == 0)
- {
- //说明没找到这个表,那么这个时候新建这个表,先释放前面的stmt
- sqlite3_finalize(stmt);
- stmt = NULL;
- sql = "CREATE TABLE pos_foodtype(" \
- "id CHAR(20) UNIQUE NOT NULL,"\
- "name CHAR(200) NOT NULL," \
- "is_shouyinji_show CHAR(100) NOT NULL);";
- if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
- {
- //执行该语句
- if (sqlite3_step(stmt) != SQLITE_DONE)
- {
- LOG_ERROR("create table fail: " << sqlite3_errmsg(m_db));
- sqlite3_finalize(stmt);
- return false;
- }
- //走到这里就是表创建成功了
- //LOG_INFO("create table success");
- sqlite3_finalize(stmt);
- }
- else
- {
- LOG_ERROR("create table prepare fail: " << sqlite3_errmsg(m_db));
- sqlite3_finalize(stmt);
- return false;
- }
- }
- else
- {
- //说明已经有这个表了,就不用再创建了
- sqlite3_finalize(stmt);
- }
- }
- else
- {
- //异常情况
- sqlite3_finalize(stmt);
- return false;
- }
- }
- else
- {
- //异常情况
- sqlite3_finalize(stmt);
- return false;
- }
- return true;
- }
- bool CSqlite3::InitPosFoodPackage()
- {
- //检查有没有pos_foodpackage这个表,如果没有就创建
- std::string sql = "SELECT COUNT(*) FROM sqlite_master where type = 'table' and name = 'pos_foodpackage';";
- sqlite3_stmt * stmt = NULL;
- if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
- {
- if (sqlite3_step(stmt) == SQLITE_ROW)
- {
- int count = sqlite3_column_int(stmt, 0);
- if (count == 0)
- {
- //说明没找到这个表,那么这个时候新建这个表,先释放前面的stmt
- sqlite3_finalize(stmt);
- stmt = NULL;
- sql = "CREATE TABLE pos_foodpackage(" \
- "id CHAR(20) UNIQUE NOT NULL,"\
- "shop_id CHAR(200) NOT NULL," \
- "name CHAR(100) NOT NULL," \
- "price CHAR(100) NOT NULL," \
- "tag CHAR(100) NOT NULL," \
- "status CHAR(100) NOT NULL," \
- "is_dabao CHAR(20) NOT NULL," \
- "dabao_money CHAR(100) NOT NULL," \
- "nature CHAR(10000) NOT NULL," \
- "goods_img CHAR(100) NOT NULL," \
- "unit CHAR(100) NOT NULL," \
- "supporttype CHAR(100) NOT NULL," \
- "is_shouyinji_show CHAR(100) NOT NULL," \
- "barcode CHAR(100) NOT NULL);";
- if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
- {
- //执行该语句
- if (sqlite3_step(stmt) != SQLITE_DONE)
- {
- LOG_ERROR("create table fail: " << sqlite3_errmsg(m_db));
- sqlite3_finalize(stmt);
- return false;
- }
- //走到这里就是表创建成功了
- //LOG_INFO("create table success");
- sqlite3_finalize(stmt);
- }
- else
- {
- LOG_ERROR("create table prepare fail: " << sqlite3_errmsg(m_db));
- sqlite3_finalize(stmt);
- return false;
- }
- }
- else
- {
- //说明已经有这个表了,就不用再创建了
- sqlite3_finalize(stmt);
- }
- }
- else
- {
- //异常情况
- sqlite3_finalize(stmt);
- return false;
- }
- }
- else
- {
- //异常情况
- sqlite3_finalize(stmt);
- return false;
- }
- return true;
- }
- bool CSqlite3::SaveParams(std::map<std::string, std::string>& params)
- {
- int result = sqlite3_exec(m_db, "BEGIN;", 0, 0, 0);
- std::string sql = "delete from pos_config;";
- result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
- for(std::map<std::string, std::string>::iterator it = params.begin(); it != params.end(); it++)
- {
- std::string name = it->first;
- std::string value = it->second;
- sql = "INSERT INTO pos_config (name, value) VALUES ('" + name + "','" + value + "');";
- result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
- }
- result = sqlite3_exec(m_db, "COMMIT;", 0, 0, 0);
- if(result == SQLITE_OK)
- {
- //LOG_INFO("save params success");
- return true;
- }
- LOG_INFO("save params fail:"<< sqlite3_errmsg(m_db));
- return false;
- }
- bool CSqlite3::SaveChufangPrinter(std::vector<ChufangPrinter>& printers)
- {
- int result = sqlite3_exec(m_db, "BEGIN;", 0, 0, 0);
- std::string sql = "delete from pos_chufang_printer;";
- result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
- for (std::vector<ChufangPrinter>::iterator it = printers.begin(); it != printers.end(); it++)
- {
- std::string date = (*it).date;
- std::string name = (*it).name;
- std::string ip = (*it).ip;
- std::string guige = (*it).guige;
- std::string fendan = (*it).fendan;
- std::string fenlei = (*it).fenlei;
- std::string fenlei_ids = (*it).fenlei_ids;
- sql = "INSERT INTO pos_chufang_printer (date, name, ip, guige, fendan, fenlei, fenlei_ids) VALUES ('" + date + "' ,'" + name + "','" + ip + "','" + guige + "','" + fendan + "','" + fenlei + "','" + fenlei_ids + "')";
- result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
- }
- result = sqlite3_exec(m_db, "COMMIT;", 0, 0, 0);
- if (result == SQLITE_OK)
- {
- LOG_INFO("save params success");
- return true;
- }
- LOG_INFO("save params fail");
- return false;
- }
- bool CSqlite3::SaveUsers(std::map<string, string> users)
- {
- int result = sqlite3_exec(m_db, "BEGIN;", 0, 0, 0);
- std::string sql = "delete from pos_user;";
- result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
- for(std::map<std::string, std::string>::iterator it = users.begin(); it != users.end(); it++)
- {
- std::string name = it->first;
- std::string password = it->second;
- sql = "INSERT INTO pos_user (username, password) VALUES ('" + name + "' ,'" + password + "')";
- result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
- }
- result = sqlite3_exec(m_db, "COMMIT;", 0, 0, 0);
- if(result == SQLITE_OK)
- {
- //LOG_INFO("save params success");
- return true;
- }
- //LOG_INFO("save params fail");
- return false;
- }
- bool CSqlite3::InitFoodData(rapidjson::Value& foodrows)
- {
- this->ExeSQl("begin;");
- //先清空之前的旧数据,重新完整写入新数据
- std::string sql = "delete from pos_food;";
- this->ExeSQl(sql);
- for (rapidjson::SizeType i = 0; i < foodrows.Size(); ++i)
- {
- rapidjson::Value& foodinfo = foodrows[i];
- std::string id = foodinfo["id"].GetString();
- std::string shop_id = foodinfo["shop_id"].GetString();
- std::string name = foodinfo["name"].GetString();
- std::string price = foodinfo["price"].GetString();
- std::string tag = foodinfo["tag"].GetString();
- std::string status = foodinfo["status"].GetString();
- std::string type_id = foodinfo["type_id"].GetString();
- std::string is_dabao = foodinfo["is_dabao"].GetString();
- std::string dabao_money = foodinfo["dabao_money"].GetString();
- std::string is_nature = foodinfo["is_nature"].GetString();
- std::string nature = CLewaimaiJson::JsonToString(foodinfo["nature"]);
- std::string autostocknum = foodinfo["autostocknum"].GetString();
- std::string goods_img = foodinfo["goods_img"].GetString();
- std::string unit = foodinfo["unit"].GetString();
- std::string barcode = foodinfo["barcode"].GetString();
- std::string member_price_used = foodinfo["member_price_used"].GetString();
- std::string member_price = foodinfo["member_price"].GetString();
- std::string buying_price = foodinfo["buying_price"].GetString();
- std::string stock = foodinfo["stock"].GetString();
- std::string stockvalid = foodinfo["stockvalid"].GetString();
- std::string stock_warning = foodinfo["stock_warning"].GetString();
- std::string is_shouyinji_show = foodinfo["is_shouyinji_show"].GetString();
- std::string expiration_date = foodinfo["expiration_date"].GetString();
- std::string is_weight = foodinfo["is_weight"].GetString();
- std::string member_price_json = foodinfo["member_price_json"].GetString();
- std::string weight_plu_code;
- if (foodinfo["weight_plu_code"].IsString())
- {
- weight_plu_code = foodinfo["weight_plu_code"].GetString();
- }
- else
- {
- weight_plu_code = "";
- }
- std::string weight_food_code;
- if (foodinfo["weight_food_code"].IsString())
- {
- weight_food_code = foodinfo["weight_food_code"].GetString();
- }
- else
- {
- weight_food_code = "";
- }
- //插入一个商品数据
- std::string sql = "INSERT INTO pos_food (id,shop_id,name,price,tag,status,type_id,is_dabao,dabao_money,is_nature,nature,autostocknum,goods_img,unit,barcode,member_price_used,member_price, \
- buying_price,stock,stockvalid,stock_warning,is_shouyinji_show,expiration_date,is_weight,member_price_json,weight_plu_code,weight_food_code) VALUES ('" + id + "' ,'" + shop_id + "','" \
- + name + "', '" + price + "', '" + tag + "', '" + status + "', '" + type_id + "', '" + is_dabao + "', '" + dabao_money + "', '" + is_nature + "', '" + nature + "', '" \
- + autostocknum + "', '" + goods_img + "', '" + unit + "', '" + barcode + "', '" + member_price_used + "', '" + member_price + "', '" + buying_price + "', '" + stock + "', '" \
- + stockvalid + "', '" + stock_warning + "', '" + is_shouyinji_show + "', '" + expiration_date + "','" + is_weight + "','" + member_price_json + "','" + weight_plu_code + "','" + weight_food_code + "')";
- bool ret = this->ExeSQl(sql);
- if (!ret)
- {
- LOG_INFO("商品插入失败:" << i);
- return false;
- }
- }
- this->ExeSQl("commit;");
- return true;
- }
- bool CSqlite3::InitFoodtypeData(rapidjson::Value& foodtyperows)
- {
- this->ExeSQl("begin;");
- //先清空之前的旧数据,重新完整写入新数据
- std::string sql = "delete from pos_foodtype;";
- this->ExeSQl(sql);
- for (rapidjson::SizeType i = 0; i < foodtyperows.Size(); ++i)
- {
- rapidjson::Value& foodtypeinfo = foodtyperows[i];
- std::string type_id = foodtypeinfo["type_id"].GetString();
- std::string name = foodtypeinfo["name"].GetString();
- std::string is_shouyinji_show = foodtypeinfo["is_shouyinji_show"].GetString();
- //插入一个商品数据
- std::string sql = "INSERT INTO pos_foodtype (id,name,is_shouyinji_show) VALUES ('" + type_id + "' ,'" + name + "', '" + is_shouyinji_show + "')";
- bool ret = this->ExeSQl(sql);
- if (!ret)
- {
- LOG_INFO("商品分类插入失败:" << i);
- return false;
- }
- }
- this->ExeSQl("commit;");
- return true;
- }
- bool CSqlite3::InitFoodpackageData(rapidjson::Value& foodpackagerows)
- {
- //先清空之前的旧数据,重新完整写入新数据
- this->ExeSQl("begin;");
- std::string sql = "delete from pos_foodpackage;";
- this->ExeSQl(sql);
- for (rapidjson::SizeType i = 0; i < foodpackagerows.Size(); ++i)
- {
- rapidjson::Value& foodinfo = foodpackagerows[i];
- std::string id = foodinfo["id"].GetString();
- std::string shop_id = foodinfo["shop_id"].GetString();
- std::string name = foodinfo["name"].GetString();
- std::string price = foodinfo["price"].GetString();
- std::string tag = foodinfo["tag"].GetString();
- std::string status = foodinfo["status"].GetString();
- std::string is_dabao = foodinfo["is_dabao"].GetString();
- std::string dabao_money = foodinfo["dabao_money"].GetString();
- std::string nature = CLewaimaiJson::JsonToString(foodinfo["nature"]);
- std::string goods_img = foodinfo["goods_img"].GetString();
- std::string unit = foodinfo["unit"].GetString();
- std::string supporttype = foodinfo["supporttype"].GetString();
- std::string is_shouyinji_show = foodinfo["is_shouyinji_show"].GetString();
- std::string barcode = foodinfo["barcode"].GetString();
- //插入一个商品数据
- 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 + "','" \
- + name + "', '" + price + "', '" + tag + "', '" + status + "', '" + is_dabao + "', '" + dabao_money + "', '" + nature + "', '" \
- + goods_img + "', '" + unit + "', '" + supporttype + "', '" + is_shouyinji_show + "', '" + barcode + "')";
- bool ret = this->ExeSQl(sql);
- if (!ret)
- {
- LOG_INFO("商品套餐插入失败:" << i);
- return false;
- }
- }
- this->ExeSQl("commit;");
- return true;
- }
- bool CSqlite3::UpdateOneFood(rapidjson::Value& foodrows)
- {
- this->ExeSQl("begin;");
- rapidjson::Value& foodinfo = foodrows;
- std::string id = foodinfo["goods_id"].GetString();
- std::string shop_id = foodinfo["shop_id"].GetString();
- std::string name = foodinfo["goods_name"].GetString();
- std::string price = foodinfo["goods_price"].GetString();
- std::string tag = foodinfo["goods_tag"].GetString();
- std::string status = foodinfo["goods_status"].GetString();
- std::string type_id = foodinfo["type_lv1_id"].GetString();
- std::string is_dabao = foodinfo["is_dabao"].GetString();
- std::string dabao_money = foodinfo["dabao_money"].GetString();
- std::string is_nature = foodinfo["is_nature"].GetString();
- std::string nature;
- if (is_nature == "1")
- {
- nature = CLewaimaiJson::JsonToString(foodinfo["nature"]);
- }
- else
- {
- nature = "";
- }
- std::string autostocknum = foodinfo["autostocknum"].GetString();
- std::string goods_img = foodinfo["goods_img"].GetString();
- std::string unit = foodinfo["unit"].GetString();
- std::string barcode = foodinfo["barcode"].GetString();
- std::string member_price_used = foodinfo["member_price_used"].GetString();
- std::string member_price = foodinfo["member_price"].GetString();
- std::string buying_price = foodinfo["buying_price"].GetString();
- std::string stock = to_string(foodinfo["stock"].GetDouble());
- std::string stockvalid = "1";
- std::string stock_warning = foodinfo["stock_warning"].GetString();
- std::string is_shouyinji_show = foodinfo["is_shouyinji_show"].GetString();
- std::string expiration_date = foodinfo["expiration_date"].GetString();
- std::string is_weight = foodinfo["is_weight"].GetString();
- std::string member_price_json = foodinfo["member_price_json"].GetString();
- std::string weight_plu_code;
- if (foodinfo["weight_plu_code"].IsString())
- {
- weight_plu_code = foodinfo["weight_plu_code"].GetString();
- }
- else
- {
- weight_plu_code = "";
- }
- std::string weight_food_code;
- if (foodinfo["weight_food_code"].IsString())
- {
- weight_food_code = foodinfo["weight_food_code"].GetString();
- }
- else
- {
- weight_food_code = "";
- }
- //插入一个商品数据
- std::string sql = "INSERT INTO pos_food (id,shop_id,name,price,tag,status,type_id,is_dabao,dabao_money,is_nature,nature,autostocknum,goods_img,unit,barcode,member_price_used,member_price, \
- buying_price,stock,stockvalid,stock_warning,is_shouyinji_show,expiration_date,is_weight,member_price_json,weight_plu_code,weight_food_code) VALUES ('" + id + "' ,'" + shop_id + "','" \
- + name + "', '" + price + "', '" + tag + "', '" + status + "', '" + type_id + "', '" + is_dabao + "', '" + dabao_money + "', '" + is_nature + "', '" + nature + "', '" \
- + autostocknum + "', '" + goods_img + "', '" + unit + "', '" + barcode + "', '" + member_price_used + "', '" + member_price + "', '" + buying_price + "', '" + stock + "', '" \
- + stockvalid + "', '" + stock_warning + "', '" + is_shouyinji_show + "', '" + expiration_date + "','" + is_weight + "','" + member_price_json + "','" + weight_plu_code + "','" + weight_food_code + "')";
- bool ret = this->ExeSQl(sql);
- if (!ret)
- {
- return false;
- }
- this->ExeSQl("commit;");
- return true;
- }
- bool CSqlite3::ExeSQl(std::string sql)
- {
- char *zErrMsg = 0;
- int rc;
- rc = sqlite3_exec(m_db, sql.c_str(), 0, 0, &zErrMsg);
- if (rc != SQLITE_OK)
- {
- LOG_ERROR("SQL error: "<<zErrMsg);
- sqlite3_free(zErrMsg);
- return false;
- }
- else
- {
- return true;
- }
- return true;
- }
- //获取套餐商品的数量
- int CSqlite3::GetFoodpackageNum()
- {
- int count = 0;
- std::string sql = "SELECT count(*) as num FROM pos_foodpackage WHERE status = 'NORMAL';";
- sqlite3_stmt * stmt = NULL;
- if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
- {
- if (sqlite3_step(stmt) == SQLITE_ROW)
- {
- count = sqlite3_column_int(stmt, 0);
- }
- sqlite3_finalize(stmt);
- }
- else
- {
- //异常情况
- sqlite3_finalize(stmt);
- }
- return count;
- }
- std::vector<CFoodType> CSqlite3::GetFoodtypes(bool is_shouyinji_show)
- {
- std::vector<CFoodType> data;
- std::string sql;
-
- if (is_shouyinji_show)
- {
- sql = "SELECT * FROM pos_foodtype WHERE is_shouyinji_show = '1';";
- }
- else
- {
- sql = "SELECT * FROM pos_foodtype;";
- }
-
- sqlite3_stmt * stmt = NULL;
- if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
- {
- while (sqlite3_step(stmt) == SQLITE_ROW)
- {
- std::string id = (char*)sqlite3_column_text(stmt, 0);
- std::string name = (char*)sqlite3_column_text(stmt, 1);
- std::string is_shouyinji_show = (char*)sqlite3_column_text(stmt, 2);
- CFoodType newtype;
- newtype.id = id;
- newtype.name = name;
- newtype.is_shouyinji_show = is_shouyinji_show;
- data.push_back(newtype);
- }
- sqlite3_finalize(stmt);
- }
- else
- {
- //异常情况
- sqlite3_finalize(stmt);
- }
- return data;
- }
- bool CSqlite3::GetFoodtypeById(std::string foodtype_id, CFoodType& newFoodType)
- {
- bool is_found = false;
- std::string sql = "SELECT * FROM pos_foodtype WHERE id = '" + foodtype_id + "'";
- sqlite3_stmt * stmt = NULL;
- if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
- {
- while (sqlite3_step(stmt) == SQLITE_ROW)
- {
- is_found = true;
- newFoodType.id = (char*)sqlite3_column_text(stmt, 0);
- newFoodType.name = (char*)sqlite3_column_text(stmt, 1);
- newFoodType.is_shouyinji_show = (char*)sqlite3_column_text(stmt, 2);
- }
- sqlite3_finalize(stmt);
- }
- else
- {
- //异常情况
- sqlite3_finalize(stmt);
- }
- return is_found;
- }
- /**
- * 如果type_id为0,表示读取所有商品,否则只读取当前type_id的商品
- */
- std::vector<CFood> CSqlite3::GetFoodByTypeid(std::string type_id, bool is_shouyinji_show)
- {
- std::vector<CFood> data;
- std::string sql;
- if (is_shouyinji_show)
- {
- sql = "SELECT * FROM pos_food WHERE status='NORMAL' and is_shouyinji_show = '1'";
- }
- else
- {
- sql = "SELECT * FROM pos_food WHERE status='NORMAL'";
- }
- if (type_id != "0")
- {
- sql += " AND type_id = '" + type_id + "'";
- }
- sql += " ORDER BY tag";
- sqlite3_stmt * stmt = NULL;
- if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
- {
- while (sqlite3_step(stmt) == SQLITE_ROW)
- {
- CFood newFood;
- newFood.id = (char*)sqlite3_column_text(stmt, 0);
- newFood.shop_id = (char*)sqlite3_column_text(stmt, 1);
- newFood.name = (char*)sqlite3_column_text(stmt, 2);
- newFood.price = (char*)sqlite3_column_text(stmt, 3);
- newFood.tag = (char*)sqlite3_column_text(stmt, 4);
- newFood.status = (char*)sqlite3_column_text(stmt, 5);
- newFood.type_id = (char*)sqlite3_column_text(stmt, 6);
- newFood.is_dabao = (char*)sqlite3_column_text(stmt, 7);
- newFood.dabao_money = (char*)sqlite3_column_text(stmt, 8);
- newFood.is_nature = (char*)sqlite3_column_text(stmt, 9);
- newFood.nature = (char*)sqlite3_column_text(stmt, 10);
- newFood.autostocknum = (char*)sqlite3_column_text(stmt, 11);
- newFood.goods_img = (char*)sqlite3_column_text(stmt, 12);
- newFood.unit = (char*)sqlite3_column_text(stmt, 13);
- newFood.barcode = (char*)sqlite3_column_text(stmt, 14);
- newFood.member_price_used = (char*)sqlite3_column_text(stmt, 15);
- newFood.member_price = (char*)sqlite3_column_text(stmt, 16);
- newFood.buying_price = (char*)sqlite3_column_text(stmt, 17);
- newFood.stock = (char*)sqlite3_column_text(stmt,18);
- newFood.stockvalid = (char*)sqlite3_column_text(stmt, 19);
- newFood.stock_warning = (char*)sqlite3_column_text(stmt, 20);
- newFood.is_shouyinji_show = (char*)sqlite3_column_text(stmt, 21);
- newFood.expiration_date = (char*)sqlite3_column_text(stmt, 22);
- newFood.is_weight = (char*)sqlite3_column_text(stmt, 23);
- newFood.weight_plu_code = (char*)sqlite3_column_text(stmt, 24);
- newFood.weight_food_code = (char*)sqlite3_column_text(stmt, 25);
- newFood.member_price_json = (char*)sqlite3_column_text(stmt, 26);
- data.push_back(newFood);
- }
- sqlite3_finalize(stmt);
- }
- else
- {
- //异常情况
- sqlite3_finalize(stmt);
- }
- return data;
- }
- std::vector<CFood> CSqlite3::GetFoodForTiaomacheng()
- {
- std::vector<CFood> data;
- std::string sql;
- sql = "SELECT * FROM pos_food WHERE is_weight = '1' AND weight_plu_code != '' AND weight_food_code != ''";
- sqlite3_stmt * stmt = NULL;
- if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
- {
- while (sqlite3_step(stmt) == SQLITE_ROW)
- {
- CFood newFood;
- newFood.id = (char*)sqlite3_column_text(stmt, 0);
- newFood.shop_id = (char*)sqlite3_column_text(stmt, 1);
- newFood.name = (char*)sqlite3_column_text(stmt, 2);
- newFood.price = (char*)sqlite3_column_text(stmt, 3);
- newFood.tag = (char*)sqlite3_column_text(stmt, 4);
- newFood.status = (char*)sqlite3_column_text(stmt, 5);
- newFood.type_id = (char*)sqlite3_column_text(stmt, 6);
- newFood.is_dabao = (char*)sqlite3_column_text(stmt, 7);
- newFood.dabao_money = (char*)sqlite3_column_text(stmt, 8);
- newFood.is_nature = (char*)sqlite3_column_text(stmt, 9);
- newFood.nature = (char*)sqlite3_column_text(stmt, 10);
- newFood.autostocknum = (char*)sqlite3_column_text(stmt, 11);
- newFood.goods_img = (char*)sqlite3_column_text(stmt, 12);
- newFood.unit = (char*)sqlite3_column_text(stmt, 13);
- newFood.barcode = (char*)sqlite3_column_text(stmt, 14);
- newFood.member_price_used = (char*)sqlite3_column_text(stmt, 15);
- newFood.member_price = (char*)sqlite3_column_text(stmt, 16);
- newFood.buying_price = (char*)sqlite3_column_text(stmt, 17);
- newFood.stock = (char*)sqlite3_column_text(stmt, 18);
- newFood.stockvalid = (char*)sqlite3_column_text(stmt, 19);
- newFood.stock_warning = (char*)sqlite3_column_text(stmt, 20);
- newFood.is_shouyinji_show = (char*)sqlite3_column_text(stmt, 21);
- newFood.expiration_date = (char*)sqlite3_column_text(stmt, 22);
- newFood.is_weight = (char*)sqlite3_column_text(stmt, 23);
- newFood.weight_plu_code = (char*)sqlite3_column_text(stmt, 24);
- newFood.weight_food_code = (char*)sqlite3_column_text(stmt, 25);
- newFood.member_price_json = (char*)sqlite3_column_text(stmt, 26);
- data.push_back(newFood);
- }
- sqlite3_finalize(stmt);
- }
- else
- {
- //异常情况
- sqlite3_finalize(stmt);
- }
- return data;
- }
- std::vector<CFood> CSqlite3::GetFoodByFoodname(std::string foodname, bool is_shouyinji_show)
- {
- std::vector<CFood> data;
- std::string sql;
- if (is_shouyinji_show)
- {
- sql = "SELECT * FROM pos_food WHERE name LIKE '%" + foodname + "%' AND status='NORMAL' AND is_shouyinji_show = '1'";
- }
- else
- {
- sql = "SELECT * FROM pos_food WHERE name LIKE '%" + foodname + "%' AND status='NORMAL'";
- }
- sql += " ORDER BY tag";
- sqlite3_stmt * stmt = NULL;
- if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
- {
- while (sqlite3_step(stmt) == SQLITE_ROW)
- {
- CFood newFood;
- newFood.id = (char*)sqlite3_column_text(stmt, 0);
- newFood.shop_id = (char*)sqlite3_column_text(stmt, 1);
- newFood.name = (char*)sqlite3_column_text(stmt, 2);
- newFood.price = (char*)sqlite3_column_text(stmt, 3);
- newFood.tag = (char*)sqlite3_column_text(stmt, 4);
- newFood.status = (char*)sqlite3_column_text(stmt, 5);
- newFood.type_id = (char*)sqlite3_column_text(stmt, 6);
- newFood.is_dabao = (char*)sqlite3_column_text(stmt, 7);
- newFood.dabao_money = (char*)sqlite3_column_text(stmt, 8);
- newFood.is_nature = (char*)sqlite3_column_text(stmt, 9);
- newFood.nature = (char*)sqlite3_column_text(stmt, 10);
- newFood.autostocknum = (char*)sqlite3_column_text(stmt, 11);
- newFood.goods_img = (char*)sqlite3_column_text(stmt, 12);
- newFood.unit = (char*)sqlite3_column_text(stmt, 13);
- newFood.barcode = (char*)sqlite3_column_text(stmt, 14);
- newFood.member_price_used = (char*)sqlite3_column_text(stmt, 15);
- newFood.member_price = (char*)sqlite3_column_text(stmt, 16);
- newFood.buying_price = (char*)sqlite3_column_text(stmt, 17);
- newFood.stock = (char*)sqlite3_column_text(stmt, 18);
- newFood.stockvalid = (char*)sqlite3_column_text(stmt, 19);
- newFood.stock_warning = (char*)sqlite3_column_text(stmt, 20);
- newFood.is_shouyinji_show = (char*)sqlite3_column_text(stmt, 21);
- newFood.expiration_date = (char*)sqlite3_column_text(stmt, 22);
- newFood.is_weight = (char*)sqlite3_column_text(stmt, 23);
- newFood.weight_plu_code = (char*)sqlite3_column_text(stmt, 24);
- newFood.weight_food_code = (char*)sqlite3_column_text(stmt, 25);
- newFood.member_price_json = (char*)sqlite3_column_text(stmt, 26);
- data.push_back(newFood);
- }
- sqlite3_finalize(stmt);
- }
- else
- {
- //异常情况
- sqlite3_finalize(stmt);
- }
- return data;
- }
- std::vector<CFoodpackage> CSqlite3::GetFoodpackages(bool is_shouyinji_show)
- {
- std::vector<CFoodpackage> data;
- std::string sql;
- if (is_shouyinji_show)
- {
- sql = "SELECT * FROM pos_foodpackage WHERE status='NORMAL' AND is_shouyinji_show = '1' ORDER BY tag";
- }
- else
- {
- sql = "SELECT * FROM pos_foodpackage WHERE status='NORMAL' ORDER BY tag";
- }
- sqlite3_stmt * stmt = NULL;
- if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
- {
- while (sqlite3_step(stmt) == SQLITE_ROW)
- {
- CFoodpackage newFood;
- newFood.id = (char*)sqlite3_column_text(stmt, 0);
- newFood.shop_id = (char*)sqlite3_column_text(stmt, 1);
- newFood.name = (char*)sqlite3_column_text(stmt, 2);
- newFood.price = (char*)sqlite3_column_text(stmt, 3);
- newFood.tag = (char*)sqlite3_column_text(stmt, 4);
- newFood.status = (char*)sqlite3_column_text(stmt, 5);
- newFood.is_dabao = (char*)sqlite3_column_text(stmt, 6);
- newFood.dabao_money = (char*)sqlite3_column_text(stmt, 7);
- newFood.nature = (char*)sqlite3_column_text(stmt, 8);
- newFood.goods_img = (char*)sqlite3_column_text(stmt, 9);
- newFood.unit = (char*)sqlite3_column_text(stmt, 10);
- newFood.supporttype = (char*)sqlite3_column_text(stmt, 11);
- newFood.is_shouyinji_show = (char*)sqlite3_column_text(stmt, 12);
- newFood.barcode = (char*)sqlite3_column_text(stmt, 13);
- data.push_back(newFood);
- }
- sqlite3_finalize(stmt);
- }
- else
- {
- //异常情况
- sqlite3_finalize(stmt);
- }
- return data;
- }
- bool CSqlite3::GetFoodById(std::string food_id, CFood& newFood)
- {
- bool is_found = false;
- std::string sql = "SELECT * FROM pos_food WHERE id = '" + food_id + "'";
- sqlite3_stmt * stmt = NULL;
- if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
- {
- while (sqlite3_step(stmt) == SQLITE_ROW)
- {
- is_found = true;
- newFood.id = (char*)sqlite3_column_text(stmt, 0);
- newFood.shop_id = (char*)sqlite3_column_text(stmt, 1);
- newFood.name = (char*)sqlite3_column_text(stmt, 2);
- newFood.price = (char*)sqlite3_column_text(stmt, 3);
- newFood.tag = (char*)sqlite3_column_text(stmt, 4);
- newFood.status = (char*)sqlite3_column_text(stmt, 5);
- newFood.type_id = (char*)sqlite3_column_text(stmt, 6);
- newFood.is_dabao = (char*)sqlite3_column_text(stmt, 7);
- newFood.dabao_money = (char*)sqlite3_column_text(stmt, 8);
- newFood.is_nature = (char*)sqlite3_column_text(stmt, 9);
- newFood.nature = (char*)sqlite3_column_text(stmt, 10);
- newFood.autostocknum = (char*)sqlite3_column_text(stmt, 11);
- newFood.goods_img = (char*)sqlite3_column_text(stmt, 12);
- newFood.unit = (char*)sqlite3_column_text(stmt, 13);
- newFood.barcode = (char*)sqlite3_column_text(stmt, 14);
- newFood.member_price_used = (char*)sqlite3_column_text(stmt, 15);
- newFood.member_price = (char*)sqlite3_column_text(stmt, 16);
- newFood.buying_price = (char*)sqlite3_column_text(stmt, 17);
- newFood.stock = (char*)sqlite3_column_text(stmt, 18);
- newFood.stockvalid = (char*)sqlite3_column_text(stmt, 19);
- newFood.stock_warning = (char*)sqlite3_column_text(stmt, 20);
- newFood.is_shouyinji_show = (char*)sqlite3_column_text(stmt, 21);
- newFood.expiration_date = (char*)sqlite3_column_text(stmt, 22);
- newFood.is_weight = (char*)sqlite3_column_text(stmt, 23);
- newFood.weight_plu_code = (char*)sqlite3_column_text(stmt, 24);
- newFood.weight_food_code = (char*)sqlite3_column_text(stmt, 25);
- newFood.member_price_json = (char*)sqlite3_column_text(stmt, 26);
- }
- sqlite3_finalize(stmt);
- }
- else
- {
- //异常情况
- sqlite3_finalize(stmt);
- }
- return is_found;
- }
- bool CSqlite3::GetFoodByBarcode(std::string barcode, CFood& newFood)
- {
- bool is_found = false;
- std::string sql = "SELECT * FROM pos_food WHERE barcode = '" + barcode + "'";
- sqlite3_stmt * stmt = NULL;
- if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
- {
- while (sqlite3_step(stmt) == SQLITE_ROW)
- {
- is_found = true;
- newFood.id = (char*)sqlite3_column_text(stmt, 0);
- newFood.shop_id = (char*)sqlite3_column_text(stmt, 1);
- newFood.name = (char*)sqlite3_column_text(stmt, 2);
- newFood.price = (char*)sqlite3_column_text(stmt, 3);
- newFood.tag = (char*)sqlite3_column_text(stmt, 4);
- newFood.status = (char*)sqlite3_column_text(stmt, 5);
- newFood.type_id = (char*)sqlite3_column_text(stmt, 6);
- newFood.is_dabao = (char*)sqlite3_column_text(stmt, 7);
- newFood.dabao_money = (char*)sqlite3_column_text(stmt, 8);
- newFood.is_nature = (char*)sqlite3_column_text(stmt, 9);
- newFood.nature = (char*)sqlite3_column_text(stmt, 10);
- newFood.autostocknum = (char*)sqlite3_column_text(stmt, 11);
- newFood.goods_img = (char*)sqlite3_column_text(stmt, 12);
- newFood.unit = (char*)sqlite3_column_text(stmt, 13);
- newFood.barcode = (char*)sqlite3_column_text(stmt, 14);
- newFood.member_price_used = (char*)sqlite3_column_text(stmt, 15);
- newFood.member_price = (char*)sqlite3_column_text(stmt, 16);
- newFood.buying_price = (char*)sqlite3_column_text(stmt, 17);
- newFood.stock = (char*)sqlite3_column_text(stmt, 18);
- newFood.stockvalid = (char*)sqlite3_column_text(stmt, 19);
- newFood.stock_warning = (char*)sqlite3_column_text(stmt, 20);
- newFood.is_shouyinji_show = (char*)sqlite3_column_text(stmt, 21);
- newFood.expiration_date = (char*)sqlite3_column_text(stmt, 22);
- newFood.is_weight = (char*)sqlite3_column_text(stmt, 23);
- newFood.weight_plu_code = (char*)sqlite3_column_text(stmt, 24);
- newFood.weight_food_code = (char*)sqlite3_column_text(stmt, 25);
- newFood.member_price_json = (char*)sqlite3_column_text(stmt, 26);
- }
- sqlite3_finalize(stmt);
- }
- else
- {
- //异常情况
- sqlite3_finalize(stmt);
- }
- return is_found;
- }
- bool CSqlite3::GetFoodByPluBianma(std::string barcode, CFood& newFood)
- {
- bool is_found = false;
- std::string sql = "SELECT * FROM pos_food WHERE is_weight = '1' AND weight_food_code = '" + barcode + "'";
- sqlite3_stmt * stmt = NULL;
- if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
- {
- while (sqlite3_step(stmt) == SQLITE_ROW)
- {
- is_found = true;
- newFood.id = (char*)sqlite3_column_text(stmt, 0);
- newFood.shop_id = (char*)sqlite3_column_text(stmt, 1);
- newFood.name = (char*)sqlite3_column_text(stmt, 2);
- newFood.price = (char*)sqlite3_column_text(stmt, 3);
- newFood.tag = (char*)sqlite3_column_text(stmt, 4);
- newFood.status = (char*)sqlite3_column_text(stmt, 5);
- newFood.type_id = (char*)sqlite3_column_text(stmt, 6);
- newFood.is_dabao = (char*)sqlite3_column_text(stmt, 7);
- newFood.dabao_money = (char*)sqlite3_column_text(stmt, 8);
- newFood.is_nature = (char*)sqlite3_column_text(stmt, 9);
- newFood.nature = (char*)sqlite3_column_text(stmt, 10);
- newFood.autostocknum = (char*)sqlite3_column_text(stmt, 11);
- newFood.goods_img = (char*)sqlite3_column_text(stmt, 12);
- newFood.unit = (char*)sqlite3_column_text(stmt, 13);
- newFood.barcode = (char*)sqlite3_column_text(stmt, 14);
- newFood.member_price_used = (char*)sqlite3_column_text(stmt, 15);
- newFood.member_price = (char*)sqlite3_column_text(stmt, 16);
- newFood.buying_price = (char*)sqlite3_column_text(stmt, 17);
- newFood.stock = (char*)sqlite3_column_text(stmt, 18);
- newFood.stockvalid = (char*)sqlite3_column_text(stmt, 19);
- newFood.stock_warning = (char*)sqlite3_column_text(stmt, 20);
- newFood.is_shouyinji_show = (char*)sqlite3_column_text(stmt, 21);
- newFood.expiration_date = (char*)sqlite3_column_text(stmt, 22);
- newFood.is_weight = (char*)sqlite3_column_text(stmt, 23);
- newFood.weight_plu_code = (char*)sqlite3_column_text(stmt, 24);
- newFood.weight_food_code = (char*)sqlite3_column_text(stmt, 25);
- newFood.member_price_json = (char*)sqlite3_column_text(stmt, 26);
- }
- sqlite3_finalize(stmt);
- }
- else
- {
- //异常情况
- sqlite3_finalize(stmt);
- }
- return is_found;
- }
- //更新商品ID的库存
- void CSqlite3::UpdateFoodStock(std::string food_id, std::string stock)
- {
- int result = sqlite3_exec(m_db, "BEGIN;", 0, 0, 0);
- std::string sql = "UPDATE pos_food SET stock = '" + stock + "' WHERE id = " + food_id;
- result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
- result = sqlite3_exec(m_db, "COMMIT;", 0, 0, 0);
- }
|