2 Star 0 Fork 0

SmartFridge/fridge_terminal

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
sql.cpp 25.78 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
#include "sql.h"
SQL::SQL(QObject *parent) : QObject(parent)
{
if (QSqlDatabase::contains("qt_sql_default_connection"))
{
database = QSqlDatabase::database("qt_sql_default_connection");
}
else
{
// 建立和SQlite数据库的连接
database = QSqlDatabase::addDatabase("QSQLITE");
// 设置数据库文件的名字
database.setDatabaseName("MyDataBase.db");
}
}
void SQL::open(){
if (!database.open())
{
qDebug() << "Error: Failed to connect database." << database.lastError();
}
else
{
// do something
}
}
void SQL::close(){
database.close();
}
// 清除所有的核心数据
void SQL::foodClearCoreData(){
open();
QSqlQuery sqlQuery;
QString sql = "delete from food where create_user = 0";
qDebug()<<sql;
if(!sqlQuery.exec(sql))
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
}
void SQL::foodClearUserData(){
open();
QSqlQuery sqlQuery;
QString sql = "delete from food where create_user != 0";
qDebug()<<sql;
if(!sqlQuery.exec(sql))
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
}
// 添加食物
void SQL::foodAdd(Food food){
open();
QSqlQuery sqlQuery;
QString sql = "SELECT create_id FROM food where create_user = 0 order by create_id desc limit 0,1";
qDebug()<<sql;
int index = 1;
if(!sqlQuery.exec(sql))
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
else
{
while(sqlQuery.next())
{
index = sqlQuery.value("create_id").toInt() + 1;
}
}
sql = "insert into food(create_user,create_id,name,type,shelf_time,unit,info,alias,pic) values("
+QString::number(food.create_user)+","+QString::number(index)+",'"
+food.name+"',"+QString::number(food.type)+","+QString::number(food.shelf_time)+","
+QString::number(food.unit)+",'"+food.info+"','"+food.alias+"','"+food.pic+"')";
qDebug()<<sql;
if(!sqlQuery.exec(sql))
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
}
void SQL::foodAdd(QList<Food> foods){
open();
QSqlQuery sqlQuery;
for(int i =0 ;i<foods.size();i++)
{
Food food = foods.at(i);
QString sql = "insert into food(create_user,create_id,name,type,shelf_time,unit,info,alias,pic) values("
+QString::number(food.create_user)+","+QString::number(food.create_id)+",'"
+food.name+"',"+QString::number(food.type)+","+QString::number(food.shelf_time)+","
+QString::number(food.unit)+",'"+food.info+"','"+food.alias+"','"+food.pic+"')";
qDebug()<<sql;
if(!sqlQuery.exec(sql))
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
}
}
void SQL::unitClearCoreData(){
open();
QSqlQuery sqlQuery;
QString sql = "delete from unit";
qDebug()<<sql;
if(!sqlQuery.exec(sql))
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
}
void SQL::unitAdd(Unit unit){
open();
QSqlQuery sqlQuery;
QString sql = "SELECT id FROM unit order by id desc limit 0,1";
qDebug()<<sql;
int index = 1;
if(!sqlQuery.exec(sql))
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
else
{
while(sqlQuery.next())
{
index = sqlQuery.value("id").toInt() + 1;
}
}
sql = "insert into unit(id,name) values("
+QString::number(index)+",'"+unit.name+"')";
qDebug()<<sql;
if(!sqlQuery.exec(sql))
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
}
void SQL::unitAdd(QList<Unit> units){
open();
QSqlQuery sqlQuery;
for(int i=0;i<units.size();i++)
{
Unit unit = units.at(i);
QString sql = "insert into unit(id,name) values("
+QString::number(unit.id)+",'"+unit.name+"')";
qDebug()<<sql;
if(!sqlQuery.exec(sql))
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
}
}
void SQL::foodtypeClearCoreData(){
open();
QSqlQuery sqlQuery;
QString sql = "delete from food_type";
qDebug()<<sql;
if(!sqlQuery.exec(sql))
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
}
void SQL::foodtypeAdd(FoodType foodtype){
open();
QSqlQuery sqlQuery;
QString sql = "SELECT id FROM food_type order by id desc limit 0,1";
qDebug()<<sql;
int index = 1;
if(!sqlQuery.exec(sql))
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
else
{
while(sqlQuery.next())
{
index = sqlQuery.value("id").toInt() + 1;
}
}
sql = "insert into food_type(id,name,pic) values("
+QString::number(index)+",'"+foodtype.name+"','"
+foodtype.pic+"')";
qDebug()<<sql;
if(!sqlQuery.exec(sql))
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
}
void SQL::foodtypeAdd(QList<FoodType> foodtypes){
open();
QSqlQuery sqlQuery;
for(int i =0 ;i<foodtypes.size();i++)
{
FoodType foodtype = foodtypes.at(i);
QString sql = "insert into food_type(id,name,pic) values("
+QString::number(foodtype.id)+",'"+foodtype.name+"','"
+foodtype.pic+"')";
qDebug()<<sql;
if(!sqlQuery.exec(sql))
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
}
}
// 食物
QList<Food> SQL::foodSearch(QString name,int size,int index){
open();
QSqlQuery sqlQuery;
if(index>0)
index--;
int start = index*size;
QList<Food> list;
QString sql = "SELECT * FROM v_food where name like '%"+name+"%' limit "+ QString::number(start)+","+QString::number(size);
qDebug()<<sql;
if(!sqlQuery.exec(sql))
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
else
{
while(sqlQuery.next())
{
Food food;
food.create_id = sqlQuery.value("create_id").toInt();
food.create_user = sqlQuery.value("create_user").toInt();
food.name = sqlQuery.value("name").toString();
food.type = sqlQuery.value("type").toInt();
food.shelf_time = sqlQuery.value("shelf_time").toInt();
food.unit = sqlQuery.value("unit").toInt();
food.info = sqlQuery.value("info").toString();
food.alias = sqlQuery.value("alias").toString();
food.pic = sqlQuery.value("pic").toString();
food.unitName = sqlQuery.value("unitName").toString();
food.typeName = sqlQuery.value("typeName").toString();
food.typePic = sqlQuery.value("typePic").toString();
list.append(food);
}
}
return list;
}
QList<Food> SQL::foodUserData(){
open();
QSqlQuery sqlQuery;
QList<Food> list;
QString sql = "SELECT * FROM food where create_user != 0";
qDebug()<<sql;
if(!sqlQuery.exec(sql))
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
else
{
while(sqlQuery.next())
{
Food food;
food.create_id = sqlQuery.value("create_id").toInt();
food.create_user = sqlQuery.value("create_user").toInt();
food.name = sqlQuery.value("name").toString();
food.type = sqlQuery.value("type").toInt();
food.shelf_time = sqlQuery.value("shelf_time").toInt();
food.unit = sqlQuery.value("unit").toInt();
food.info = sqlQuery.value("info").toString();
food.alias = sqlQuery.value("alias").toString();
food.pic = sqlQuery.value("pic").toString();
list.append(food);
}
}
return list;
}
// 存储
void SQL::storageClearUserData(){
open();
QSqlQuery sqlQuery;
QString sql = "delete from storage";
qDebug()<<sql;
if(!sqlQuery.exec(sql))
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
}
QList<Storage> SQL::storageSearchAll(){
open();
QSqlQuery sqlQuery;
QList<Storage> list;
if(!sqlQuery.exec("SELECT * FROM v_storage"))
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
else
{
while(sqlQuery.next())
{
Storage storage;
storage.user_id = sqlQuery.value("create_user").toInt();
storage.food_id = sqlQuery.value("create_id").toInt();
storage.name = sqlQuery.value("name").toString();
storage.type = sqlQuery.value("typeName").toString();
storage.unit = sqlQuery.value("unitName").toString();
storage.pic = sqlQuery.value("pic").toString();
storage.info = sqlQuery.value("info").toString();
storage.amount = sqlQuery.value("amount").toInt();
storage.alias = sqlQuery.value("alias").toString();
list.append(storage);
}
}
return list;
}
QList<Storage> SQL::storageUserData(){
open();
QSqlQuery sqlQuery;
QList<Storage> list;
if(!sqlQuery.exec("SELECT * FROM storage"))
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
else
{
while(sqlQuery.next())
{
Storage storage;
storage.user_id = sqlQuery.value("user_id").toInt();
storage.food_id = sqlQuery.value("food_id").toInt();
storage.amount = sqlQuery.value("amount").toInt();
list.append(storage);
}
}
return list;
}
void SQL::storageAdd(long user_id,long food_id,int amount){
open();
QSqlQuery sqlQuery;
qDebug()<<"user_id:"<<(int)user_id;
qDebug()<<"food_id:"<<(int)food_id;
qDebug()<<"amount:"<<amount;
QString sql = "SELECT amount FROM storage where user_id = ? and food_id = ?";
qDebug()<<sql;
sqlQuery.prepare(sql);
sqlQuery.addBindValue((int)user_id);
sqlQuery.addBindValue((int)food_id);
sqlQuery.exec();
bool have = false;
int total = 0;
if(!sqlQuery.exec())
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
else
{
while(sqlQuery.next())
{
have = true;
total = sqlQuery.value("amount").toInt();
}
}
if(!have)
{
sql = "insert into storage(user_id,food_id,amount) values("+QString::number(user_id)+","+QString::number(food_id)+","+QString::number(amount)+")";
qDebug()<<sql;
if(!sqlQuery.exec(sql))
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
}
else{
sql = "update storage set amount = "+QString::number(amount+total)+" where user_id = "+QString::number(user_id)+" and food_id = "+QString::number(food_id);
qDebug()<<sql;
if(!sqlQuery.exec(sql))
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
}
}
void SQL::storageAdd(QList<Storage> storages)
{
open();
QSqlQuery sqlQuery;
for(int i = 0 ;i<storages.size();i++)
{
Storage storage = storages.at(i);
QString sql = "insert into storage(user_id,food_id,amount) values("
+QString::number(storage.user_id)+","+QString::number(storage.food_id)
+","+QString::number(storage.amount)+")";
qDebug()<<sql;
if(!sqlQuery.exec(sql))
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
}
}
void SQL::storageUpdate(long user_id,long food_id,int amount){
open();
QSqlQuery sqlQuery;
qDebug()<<"user_id:"<<(int)user_id;
qDebug()<<"food_id:"<<(int)food_id;
qDebug()<<"amount:"<<amount;
QString sql = "update storage set amount = "+QString::number(amount)+" where user_id = "+QString::number(user_id)+" and food_id = "+QString::number(food_id);
qDebug()<<sql;
if(!sqlQuery.exec(sql))
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
}
void SQL::storageDelete(long user_id,long food_id){
open();
QSqlQuery sqlQuery;
qDebug()<<"user_id:"<<(int)user_id;
qDebug()<<"food_id:"<<(int)food_id;
QString sql = "delete from storage where user_id = "+QString::number(user_id)+" and food_id = "+QString::number(food_id);
qDebug()<<sql;
if(!sqlQuery.exec(sql))
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
}
// 提醒
void SQL::remindClearUserData(){
open();
QSqlQuery sqlQuery;
QString sql = "delete from remind";
qDebug()<<sql;
if(!sqlQuery.exec(sql))
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
}
QList<Remind> SQL::remindSearchAll(){
open();
QSqlQuery sqlQuery;
QList<Remind> list;
sqlQuery.exec("SELECT * FROM v_remind");
if(!sqlQuery.exec())
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
else
{
while(sqlQuery.next())
{
Remind remind;
remind.user_id = sqlQuery.value("create_user").toInt();
remind.food_id = sqlQuery.value("create_id").toInt();
remind.name = sqlQuery.value("name").toString();
remind.type = sqlQuery.value("typeName").toString();
remind.unit = sqlQuery.value("unitName").toString();
remind.pic = sqlQuery.value("pic").toString();
remind.info = sqlQuery.value("info").toString();
remind.amount = sqlQuery.value("amount").toInt();
remind.id = sqlQuery.value("id").toInt();
// QString string = sqlQuery.value("date").toString();
// remind.date = QDate::fromString("yyyy-MM-dd",string);
remind.date = sqlQuery.value("date").toDate();
list.append(remind);
}
}
return list;
}
QList<Remind> SQL::remindUserData(){
open();
QSqlQuery sqlQuery;
QList<Remind> list;
sqlQuery.exec("SELECT * FROM remind");
if(!sqlQuery.exec())
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
else
{
while(sqlQuery.next())
{
Remind remind;
remind.user_id = sqlQuery.value("user_id").toInt();
remind.food_id = sqlQuery.value("food_id").toInt();
remind.amount = sqlQuery.value("amount").toInt();
remind.id = sqlQuery.value("id").toInt();
remind.date = sqlQuery.value("date").toDate();
list.append(remind);
}
}
return list;
}
void SQL::remindAdd(long user_id,long food_id,int amount,QDate date){
open();
QSqlQuery sqlQuery;
qDebug()<<"user_id:"<<(int)user_id;
qDebug()<<"food_id:"<<(int)food_id;
qDebug()<<"amount:"<<amount;
qDebug()<<"date:"<<date;
QString sql = "SELECT id FROM remind order by id desc limit 0,1";
qDebug()<<sql;
int index = 0;
if(!sqlQuery.exec(sql))
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
else
{
while(sqlQuery.next())
{
index = sqlQuery.value("id").toInt() + 1;
}
}
sql = "insert into remind(id,user_id,food_id,amount,date) values("+QString::number(index)+","
+QString::number(user_id)+","+QString::number(food_id)+","+QString::number(amount)+",'"+date.toString("yyyy-MM-dd")+"')";
qDebug()<<sql;
if(!sqlQuery.exec(sql))
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
}
void SQL::remindAdd(QList<Remind> reminds)
{
open();
QSqlQuery sqlQuery;
for(int i = 0 ;i<reminds.size();i++)
{
Remind remind = reminds.at(i);
QString sql = "insert into remind(id,user_id,food_id,amount,date) values("+QString::number(remind.id)+","
+QString::number(remind.user_id)+","+QString::number(remind.food_id)+","
+QString::number(remind.amount)+",'"+remind.date.toString("yyyy-MM-dd")+"')";
qDebug()<<sql;
if(!sqlQuery.exec(sql))
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
}
}
void SQL::remindUpdate(long id,int amount, QDate date){
open();
QSqlQuery sqlQuery;
qDebug()<<"id:"<<id;
qDebug()<<"amount:"<<amount;
qDebug()<<"date:"<<date;
QString sql = "update remind set amount = "+QString::number(amount)+" , date ='"+date.toString("yyyy-MM-dd")+"' where id = "+QString::number(id);
qDebug()<<sql;
if(!sqlQuery.exec(sql))
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
}
void SQL::remindDelete(long id){
open();
QSqlQuery sqlQuery;
qDebug()<<"id:"<<id;
QString sql = "delete from remind where id = "+QString::number(id);
qDebug()<<sql;
if(!sqlQuery.exec(sql))
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
}
QList<Option> SQL::optionSearchUncommitAll(){
open();
QSqlQuery sqlQuery;
QList<Option> list;
QString sql = "SELECT * FROM v_option where status = 0 order by datetime";
qDebug()<<sql;
if(!sqlQuery.exec(sql))
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
else
{
while(sqlQuery.next())
{
Option option;
option.id = sqlQuery.value("id").toInt();
option.name = sqlQuery.value("name").toString();
option.pic = sqlQuery.value("pic").toString();
option.amount = sqlQuery.value("amount").toInt();
option.status = sqlQuery.value("status").toInt();
option.direction = sqlQuery.value("direction").toInt();
option.food_id = sqlQuery.value("create_id").toInt();
option.user_id = sqlQuery.value("create_user").toInt();
option.datetime = sqlQuery.value("datetime").toDateTime();
option.typeName = sqlQuery.value("typeName").toString();
option.unitName = sqlQuery.value("unitName").toString();
list.append(option);
}
}
return list;
}
void SQL::optionAdd(Option option){
open();
QSqlQuery sqlQuery;
qDebug()<<"option: "<<option;
QString sql = "SELECT id FROM option order by id desc limit 0,1";
qDebug()<<sql;
int index = 0;
if(!sqlQuery.exec(sql))
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
else
{
while(sqlQuery.next())
{
index = sqlQuery.value("id").toInt() + 1;
}
}
sql = "insert into option(id,user_id,food_id,status,direction,amount,datetime) values("+QString::number(index)+","
+QString::number(option.user_id)+","+QString::number(option.food_id)+","
+QString::number(option.status)+","+QString::number(option.direction)+","+QString::number(option.amount)+",'"
+option.datetime.toString("yyyy-MM-dd hh:mm:ss")+"')";
qDebug()<<sql;
if(!sqlQuery.exec(sql))
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
}
void SQL::optionAdd(QList<Option> options){
open();
if(options.size()>=1){
QSqlQuery sqlQuery;
qDebug()<<"option: "<<options;
QString sql = "SELECT id FROM option order by id desc limit 0,1";
qDebug()<<sql;
int index = 0;
if(!sqlQuery.exec(sql))
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
else
{
while(sqlQuery.next())
{
index = sqlQuery.value("id").toInt() + 1;
}
}
sql = "insert into option(id,user_id,food_id,status,direction,amount,datetime) values("+QString::number(index++)+","
+QString::number(options.at(0).user_id)+","+QString::number(options.at(0).food_id)+","
+QString::number(options.at(0).status)+","+QString::number(options.at(0).direction)+","+QString::number(options.at(0).amount)
+",'"+options.at(0).datetime.toString("yyyy-MM-dd hh:mm:ss")+"')";
for (int i=1;i<options.size();i++)
{
sql = sql +",("+QString::number(index++)+","
+QString::number(options.at(i).user_id)+","+QString::number(options.at(i).food_id)+","
+QString::number(options.at(i).status)+","+QString::number(options.at(i).direction)+","+QString::number(options.at(i).amount)
+",'"+options.at(i).datetime.toString("yyyy-MM-dd hh:mm:ss")+"')";
}
qDebug()<<sql;
if(!sqlQuery.exec(sql))
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
}
else{
return;
}
}
QList<Option> SQL::optionSearchCommitAll(){
open();
QSqlQuery sqlQuery;
QList<Option> list;
QString sql = "SELECT * FROM v_option where status = 1 order by datetime";
qDebug()<<sql;
if(!sqlQuery.exec(sql))
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
else
{
while(sqlQuery.next())
{
Option option;
option.id = sqlQuery.value("id").toInt();
option.name = sqlQuery.value("name").toString();
option.pic = sqlQuery.value("pic").toString();
option.amount = sqlQuery.value("amount").toInt();
option.status = sqlQuery.value("status").toInt();
option.direction = sqlQuery.value("direction").toInt();
option.food_id = sqlQuery.value("create_id").toInt();
option.user_id = sqlQuery.value("create_user").toInt();
option.datetime = sqlQuery.value("datetime").toDateTime();
option.typeName = sqlQuery.value("typeName").toString();
option.unitName = sqlQuery.value("unitName").toString();
list.append(option);
}
}
return list;
}
void SQL::optionCommit(long id){
open();
QSqlQuery sqlQuery;
bool haveFind = false;
int shelf_time=0;
Option option;
QString sql = "SELECT * FROM v_option where status = 0 and id = "+QString::number(id);
qDebug()<<sql;
if(!sqlQuery.exec(sql))
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
else
{
while(sqlQuery.next())
{
haveFind = true;
option.id = sqlQuery.value("id").toInt();
option.name = sqlQuery.value("name").toString();
option.pic = sqlQuery.value("pic").toString();
option.amount = sqlQuery.value("amount").toInt();
option.status = sqlQuery.value("status").toInt();
option.direction = sqlQuery.value("direction").toInt();
option.food_id = sqlQuery.value("create_id").toInt();
shelf_time = sqlQuery.value("shelf_time").toInt();
option.user_id = sqlQuery.value("create_user").toInt();
option.datetime = sqlQuery.value("datetime").toDateTime();
option.typeName = sqlQuery.value("typeName").toString();
option.unitName = sqlQuery.value("unitName").toString();
}
}
if(haveFind)
{
haveFind = false;
int amount = 0;
QString sql = "SELECT * FROM storage where user_id = "+QString::number(option.user_id)+" and food_id = "+QString::number(option.food_id);
qDebug()<<sql;
if(!sqlQuery.exec(sql))
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
else
{
while(sqlQuery.next())
{
haveFind = true;
amount = sqlQuery.value("amount").toInt();
}
}
// 记录本来就存在
if(haveFind){
if(option.direction==1)
{
amount = amount + option.amount;
}
else{
amount = amount - option.amount;
}
if(amount>0)
{
storageUpdate(option.user_id,option.food_id,amount);
}
else{
storageDelete(option.user_id,option.food_id);
}
}
else{
// 插入记录
if(option.direction==1){
storageAdd(option.user_id,option.food_id,option.amount);
}
}
if(option.direction==1){
remindAdd(option.user_id,option.food_id,option.amount,QDate::fromString(option.datetime.addDays(shelf_time).toString("yyyy-MM-dd"),"yyyy-MM-dd"));
}
sql = "update option set status = 1 where id = "+QString::number(id);
qDebug()<<sql;
if(!sqlQuery.exec(sql))
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
}
}
void SQL::optionCommit(QList<long> ids){
for (int i = 0;i<ids.size();i++)
{
optionCommit(ids.at(i));
}
}
void SQL::optionUpdate(long id,int amount,int direction){
open();
QSqlQuery sqlQuery;
qDebug()<<"id:"<<id;
qDebug()<<"amount:"<<amount;
qDebug()<<"direction:"<<direction;
QString sql = "update option set amount = "+QString::number(amount)+",direction = "+QString::number(direction)+" where id = "+QString::number(id);
qDebug()<<sql;
if(!sqlQuery.exec(sql))
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
}
void SQL::optionDelete(long id){
open();
QSqlQuery sqlQuery;
qDebug()<<"id:"<<id;
QString sql = "delete from option where id = "+QString::number(id);
qDebug()<<sql;
if(!sqlQuery.exec(sql))
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
}
void SQL::optionDeleteAllCommit(){
open();
QSqlQuery sqlQuery;
QString sql = "delete from option where status = 1";
qDebug()<<sql;
if(!sqlQuery.exec(sql))
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/smart-ice-box/fridge_terminal.git
git@gitee.com:smart-ice-box/fridge_terminal.git
smart-ice-box
fridge_terminal
fridge_terminal
master

搜索帮助