重构了连接数据库、设置设备名的代码,封装为类
This commit is contained in:
parent
1a300f4ca7
commit
6cca0535bc
@ -14,6 +14,8 @@ SOURCES += \
|
||||
|
||||
HEADERS += \
|
||||
HF15693.h \
|
||||
databaseAPI.h \
|
||||
deviceAPI.h \
|
||||
mainwindow.h \
|
||||
newCardPage.h \
|
||||
quitAppPage.h \
|
||||
|
83
databaseAPI.h
Normal file
83
databaseAPI.h
Normal file
@ -0,0 +1,83 @@
|
||||
#ifndef DATABASEAPI_H
|
||||
#define DATABASEAPI_H
|
||||
|
||||
|
||||
#include <QSqlDatabase>
|
||||
#include <QString>
|
||||
|
||||
|
||||
class Database
|
||||
{
|
||||
private:
|
||||
QSqlDatabase db;
|
||||
bool connected = false;
|
||||
QString databaseName = QString("cardManageSystem");
|
||||
QString userName = QString("cardManageSystem");
|
||||
|
||||
public:
|
||||
Database(QSqlDatabase database)
|
||||
{
|
||||
db = database;
|
||||
db.setDatabaseName(databaseName);
|
||||
db.setUserName(userName);
|
||||
}
|
||||
|
||||
Database(QSqlDatabase database, QString hostName, int port, QString password)
|
||||
{
|
||||
db = database;
|
||||
db.setDatabaseName(databaseName);
|
||||
db.setUserName(userName);
|
||||
db.setHostName(hostName);
|
||||
db.setPort(port);
|
||||
db.setPassword(password);
|
||||
}
|
||||
|
||||
QSqlDatabase getDatabase()
|
||||
{
|
||||
return db;
|
||||
}
|
||||
|
||||
void setHostName(QString hostName)
|
||||
{
|
||||
db.setHostName(hostName);
|
||||
}
|
||||
|
||||
QString getHostName()
|
||||
{
|
||||
return db.hostName();
|
||||
}
|
||||
|
||||
void setPort(int port)
|
||||
{
|
||||
db.setPort(port);
|
||||
}
|
||||
|
||||
int getPort()
|
||||
{
|
||||
return db.port();
|
||||
}
|
||||
|
||||
void setPassword(QString password)
|
||||
{
|
||||
db.setPassword(password);
|
||||
}
|
||||
|
||||
bool is_connected()
|
||||
{
|
||||
return connected;
|
||||
}
|
||||
|
||||
bool open()
|
||||
{
|
||||
connected = db.open();
|
||||
return connected;
|
||||
}
|
||||
|
||||
~Database()
|
||||
{
|
||||
db.close();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#endif // DATABASEAPI_H
|
51
deviceAPI.h
Normal file
51
deviceAPI.h
Normal file
@ -0,0 +1,51 @@
|
||||
#ifndef DEVICEAPI_H
|
||||
#define DEVICEAPI_H
|
||||
|
||||
#include <QString>
|
||||
#include <QSqlQuery>
|
||||
#include <databaseAPI.h>
|
||||
|
||||
class Device
|
||||
{
|
||||
private:
|
||||
bool connected = false;
|
||||
bool depositAllowed = false;
|
||||
QString name = QString("未指定设备名");
|
||||
|
||||
public:
|
||||
bool is_connected()
|
||||
{
|
||||
return connected;
|
||||
}
|
||||
|
||||
bool is_depositAllowed()
|
||||
{
|
||||
return depositAllowed;
|
||||
}
|
||||
|
||||
void setDevice(QString name, Database *db)
|
||||
{
|
||||
QSqlQuery query(db->getDatabase());
|
||||
QString sql = QString("select * from device where `name` = '%1';").arg(name);
|
||||
query.exec(sql);
|
||||
if (query.next())
|
||||
{
|
||||
connected = true;
|
||||
this->name = name;
|
||||
depositAllowed = query.value(2).toBool();
|
||||
}
|
||||
else
|
||||
{
|
||||
connected = false;
|
||||
depositAllowed = false;
|
||||
}
|
||||
}
|
||||
|
||||
QString getName()
|
||||
{
|
||||
if (connected) return name;
|
||||
else return QString("未指定设备名");
|
||||
}
|
||||
};
|
||||
|
||||
#endif // DEVICEAPI_H
|
@ -33,7 +33,7 @@ MainWindow::MainWindow(QWidget *parent)
|
||||
databaseLabel = new QLabel("数据库无连接");
|
||||
ui->statusBar->addWidget(databaseLabel);
|
||||
|
||||
deviceLabel = new QLabel(device);
|
||||
deviceLabel = new QLabel(device.getName());
|
||||
ui->statusBar->addWidget(deviceLabel);
|
||||
|
||||
ui->stackedWidget->setCurrentWidget(ui->settingPage);
|
||||
|
11
mainwindow.h
11
mainwindow.h
@ -6,10 +6,11 @@
|
||||
#include <QLabel>
|
||||
#include <QStackedWidget>
|
||||
#include <QMessageBox>
|
||||
#include <QSqlDatabase>
|
||||
#include <QSqlQuery>
|
||||
|
||||
#include <readerAPI.h>
|
||||
#include <databaseAPI.h>
|
||||
#include <deviceAPI.h>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
namespace Ui {
|
||||
@ -43,12 +44,8 @@ private slots:
|
||||
private:
|
||||
Ui::MainWindow *ui;
|
||||
Reader reader;
|
||||
QSqlDatabase db;
|
||||
QString databaseIpAddr = "";
|
||||
int databasePort = 3306;
|
||||
QString databasePassword = "";
|
||||
QString device = QString("未指定设备名");
|
||||
bool depositAllowed = false;
|
||||
Database *db = nullptr;
|
||||
Device device;
|
||||
|
||||
QStatusBar *statusBar;
|
||||
QStackedWidget *stackedWidget;
|
||||
|
@ -63,43 +63,38 @@ void MainWindow::on_connectReaderButton_clicked()
|
||||
*/
|
||||
void MainWindow::on_connectDatabaseButton_clicked()
|
||||
{
|
||||
databaseIpAddr = ui->ipAddrEdit->text();
|
||||
databasePort = ui->portBox->value();
|
||||
databasePassword = ui->passwordEdit->text();
|
||||
if (db == nullptr)
|
||||
{
|
||||
db = new Database(QSqlDatabase::addDatabase("QMYSQL"), ui->ipAddrEdit->text(), ui->portBox->value(), ui->passwordEdit->text());
|
||||
}
|
||||
else
|
||||
{
|
||||
db->setHostName(ui->ipAddrEdit->text());
|
||||
db->setPort(ui->portBox->value());
|
||||
db->setPassword(ui->passwordEdit->text());
|
||||
}
|
||||
|
||||
db = QSqlDatabase::addDatabase("QMYSQL");
|
||||
db.setHostName(databaseIpAddr);
|
||||
db.setPort(databasePort);
|
||||
db.setPassword(databasePassword);
|
||||
db.setDatabaseName("cardManageSystem");
|
||||
db.setUserName("cardManageSystem");
|
||||
|
||||
if (!db.open())
|
||||
if (!db->open())
|
||||
{
|
||||
QMessageBox::warning(this, QString("数据库状态提示"), QString("数据库连接失败,请重试。"));
|
||||
databaseConnectStatusCheckBox->setChecked(false);
|
||||
databaseLabel->setText(QString("数据库未连接"));
|
||||
databaseIpAddr = "";
|
||||
return;
|
||||
}
|
||||
databaseConnectStatusCheckBox->setChecked(true);
|
||||
databaseLabel->setText(QString("数据库已连接:") + databaseIpAddr + QString(":") + QString::number(databasePort));
|
||||
databaseLabel->setText(QString("数据库已连接:") + db->getHostName() + QString(":") + QString::number(db->getPort()));
|
||||
|
||||
device = ui->deviceEdit->text();
|
||||
QSqlQuery query;
|
||||
QString sql = QString("select * from device where id = '%1';").arg(device);
|
||||
query.exec(sql);
|
||||
if (query.next())
|
||||
device.setDevice(ui->deviceEdit->text(), db);
|
||||
if (!device.is_connected())
|
||||
{
|
||||
if (query.value(2).toBool()) depositAllowed = true;
|
||||
else depositAllowed = false;
|
||||
QMessageBox::warning(this, QString("设备名提示"), QString("该设备名无效,请重试。"));
|
||||
deviceLabel->setText(device.getName());
|
||||
}
|
||||
else
|
||||
{
|
||||
device = QString("未指定设备名");
|
||||
QMessageBox::warning(this, QString("设备名提示"), QString("该设备名无效,请重试。"));
|
||||
if (device.is_depositAllowed()) deviceLabel->setText(device.getName() + QString("(可充值)"));
|
||||
else deviceLabel->setText(device.getName() + QString("(仅可消费)"));
|
||||
}
|
||||
deviceLabel->setText(device);
|
||||
}
|
||||
|
||||
|
||||
@ -110,7 +105,9 @@ void MainWindow::on_connectDatabaseButton_clicked()
|
||||
*/
|
||||
bool MainWindow::ready()
|
||||
{
|
||||
if (!reader.is_connected() || databaseIpAddr.isEmpty()) return false;
|
||||
if (!reader.is_connected()) return false;
|
||||
if (db == nullptr || !db->is_connected()) return false;
|
||||
if (!device.is_connected()) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user