You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
99 lines
3.4 KiB
99 lines
3.4 KiB
#include "message.h"
|
|
#include <QSqlQuery>
|
|
#include <QNetworkRequest>
|
|
#include <QString>
|
|
#include <QDebug>
|
|
#include <QDateTime>
|
|
#include "widget.h"
|
|
#include <QMessageBox>
|
|
|
|
message::message()
|
|
{
|
|
}
|
|
|
|
void message::mySlot(QString str)
|
|
{
|
|
// 使用在 main 中创建的数据库连接
|
|
QSqlDatabase db = QSqlDatabase::database("text01");
|
|
|
|
if (!db.isOpen()) {
|
|
QMessageBox::warning(this, tr("警告!"), tr("数据库未打开!"), QMessageBox::Yes);
|
|
return;
|
|
}
|
|
|
|
// 查询空车位数量
|
|
QString temp_car = QString("select * from cnt where number='%1' ").arg("number_of_c");
|
|
QSqlQuery temp_query(db); // 使用 db 对象来执行查询
|
|
temp_query.exec(temp_car);
|
|
temp_query.next();
|
|
int temp_cnt = temp_query.value(1).toInt();
|
|
if (temp_cnt == 0) {
|
|
QMessageBox::warning(this, tr("警告!"), tr("停车场已无车位"), QMessageBox::Yes);
|
|
} else {
|
|
// 进入操作
|
|
QString number = QString("select * from message where number='%1' ").arg(str);
|
|
QSqlQuery query(db), query_1(db), query_2(db);
|
|
int times;
|
|
// 存储进入次数
|
|
if (query.exec(number) && query.next()) {
|
|
// 修改已有信息
|
|
times = query.value(1).toInt();
|
|
times++;
|
|
QString temp = QString("update message set times = '%1' where number = '%2'").arg(times).arg(str);
|
|
query.exec(temp);
|
|
} else {
|
|
// 第一次进入
|
|
QString s = QString("insert into message(number, times, total_time, pay) values('%1', '%2', '%3', '%4')")
|
|
.arg(str).arg(1).arg(0).arg(0);
|
|
query_2.exec(s);
|
|
}
|
|
|
|
// 更改车场信息
|
|
temp_cnt--;
|
|
QString temp_change = QString("update cnt set cnt = '%1' where number = '%2'").arg(temp_cnt).arg("number_of_c");
|
|
temp_query.exec(temp_change);
|
|
|
|
// 获取当前时间
|
|
QDateTime dateTime(QDateTime::currentDateTime());
|
|
QString time = dateTime.toString("yyyy-MM-dd hh:mm:ss");
|
|
|
|
// 存储车牌和进入时间
|
|
QString ch = QString("insert into car_number(number, time) values('%1', '%2')").arg(str).arg(time);
|
|
query_1.exec(ch);
|
|
}
|
|
}
|
|
|
|
void message::Slot_out(QString str)
|
|
{
|
|
// 使用在 main 中创建的数据库连接
|
|
QSqlDatabase db = QSqlDatabase::database("text01");
|
|
|
|
if (!db.isOpen()) {
|
|
QMessageBox::warning(this, tr("警告!"), tr("数据库未打开!"), QMessageBox::Yes);
|
|
return;
|
|
}
|
|
|
|
QString Str = QString("select * from car_number where number='%1' ").arg(str);
|
|
QSqlQuery query(db);
|
|
query.exec(Str);
|
|
query.next();
|
|
QString time = query.value(1).toString();
|
|
QDateTime time_1 = QDateTime::fromString(time, "yyyy-MM-dd hh:mm:ss");
|
|
QDateTime time_2 = QDateTime::currentDateTime();
|
|
int second = time_1.secsTo(time_2); // 计算时间差
|
|
|
|
// 删除数据库中车牌号
|
|
QString a = QString("delete from car_number where number = '%1'").arg(str);
|
|
query.exec(a);
|
|
|
|
// 更改车场信息
|
|
QString temp_car = QString("select * from cnt where number='%1' ").arg("number_of_c");
|
|
QSqlQuery temp_query(db);
|
|
temp_query.exec(temp_car);
|
|
temp_query.next();
|
|
int temp_cnt = temp_query.value(1).toInt();
|
|
temp_cnt++;
|
|
QString temp_change = QString("update cnt set cnt = '%1' where number = '%2'").arg(temp_cnt).arg("number_of_c");
|
|
temp_query.exec(temp_change);
|
|
}
|