优化了部分代码

This commit is contained in:
www-git-cn 2024-03-05 18:01:44 +08:00
parent af565e360a
commit ecef963f71
5 changed files with 66 additions and 16 deletions

View File

@ -41,7 +41,11 @@ void DataManager::recv_from_cln(QByteArray &data, QTcpSocket *sock)
QJsonDocument json_data = QJsonDocument::fromJson(list[i]);
if (json_data["type"] == "message")
{
if (json_data["action"] == "count")
{
p_this->count = p_this->linedb->getCount();
p_this->update_count(sock,p_this->count);
}
}
else if (json_data["type"] == "code")
{
@ -58,11 +62,16 @@ void DataManager::recv_from_cln(QByteArray &data, QTcpSocket *sock)
qDebug() << data_build;
data_encrypt = p_this->protocol->data_encrypt(data_build);
p_this->server->sendToclient(sock, data_encrypt);
p_this->count = p_this->linedb->getCount();
p_this->update_count(sock,p_this->count);
}
else if (json_data["action"] == "park") //存车消息
{
QString name = json_data["user"].toString();
QString find_code = p_this->database->findCode(name);
if(find_code == "000")
{
QString number = p_this->linedb->choose(); //获取一个可用的车位编号
qDebug() << "获取到的可用车位编号是" << number;
//随机生成取车码
@ -77,6 +86,17 @@ void DataManager::recv_from_cln(QByteArray &data, QTcpSocket *sock)
qDebug() << data_build;
data_encrypt = p_this->protocol->data_encrypt(data_build);
p_this->server->sendToclient(sock, data_encrypt);
p_this->count -= 1;
p_this->update_count(sock,p_this->count);
}
else
{
QByteArray data_build, data_encrypt;
data_build = p_this->protocol->data_maker(Protocol::CODE, QString("park"), QString("server"), find_code);
qDebug() << data_build;
data_encrypt = p_this->protocol->data_encrypt(data_build);
p_this->server->sendToclient(sock, data_encrypt);
}
}
else if(json_data["action"] == "update")
@ -201,6 +221,15 @@ void DataManager::update_namelist(QTcpSocket *sock)
}
}
void DataManager::update_count(QTcpSocket *sock, int count)
{
QByteArray data_build, data_encrypt;
data_build = p_this->protocol->data_maker(Protocol::MESSAGE, QString("count"), QString::number(count));
qDebug() << data_build << "发送count信息";
data_encrypt = p_this->protocol->data_encrypt(data_build);
p_this->server->sendToclient(sock, data_encrypt);
}
QString DataManager::getRandomString(int nLen)
{
srand(QDateTime::currentMSecsSinceEpoch());

View File

@ -6,7 +6,7 @@
#include <QRandomGenerator>
#include <QChar>
#include <QDateTime>
#include <QtGlobal>>
#include <QtGlobal>
#include "database.h"
#include "tcpserver.h"
@ -32,10 +32,12 @@ private:
Line *linedb;
Protocol *protocol;
TCPServer *server;
int count;
QList <QTcpSocket*> online_socket_list;
QList <QString> online_name_list;
static DataManager *p_this;
void update_namelist(QTcpSocket *sock);
void update_count(QTcpSocket *sock, int count);
};
#endif // DATAMANAGER_H

View File

@ -50,6 +50,21 @@ Line::Line()
db_.close();
}
int Line::getCount()
{
db_.open();
QString sql = QString("select * from table_line where state = '000'");
QSqlQuery query(db_);
query.exec(sql);
QSqlQueryModel *queryModel = new QSqlQueryModel();
queryModel->setQuery(query);
int nRecordCount = queryModel->rowCount();
qDebug() << "表的条目数:" << nRecordCount;
return nRecordCount;
}
QString Line::findCode(QString number)
{
QString sql = QString("select * from table_line where number = :number");

4
line.h
View File

@ -6,6 +6,8 @@
#include <QSqlError>
#include <QSqlQuery>
#include <QVector>
#include <QSqlQueryModel>
#include <QSqlQuery>
class Line
{
@ -23,6 +25,8 @@ public:
void resetState(QString number);
int getCount();
private:
QSqlDatabase db_;

View File

@ -27,7 +27,7 @@ QByteArray Protocol::data_builder(QList<any_types> &args)
case MESSAGE:
{
data.insert("type","message");
data.insert("sendname",args[1].get_string());
data.insert("action",args[1].get_string());
data.insert("data",args[2].get_string());
break;
}