You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
125 lines
3.7 KiB
C++
125 lines
3.7 KiB
C++
#ifndef QSAVECSVTHREAD_H
|
|
#define QSAVECSVTHREAD_H
|
|
|
|
#include <QThread>
|
|
#include "QFile"
|
|
#include "QTextStream"
|
|
#include "QSqlQuery"
|
|
#include "QDateTime"
|
|
#include "databasesql.h"
|
|
#pragma execution_character_set("utf-8")
|
|
class QSaveCSVThread : public QThread
|
|
{
|
|
Q_OBJECT
|
|
|
|
public:
|
|
QSaveCSVThread(QObject *parent){};
|
|
~QSaveCSVThread(){};
|
|
void setInform(QString Title, QString username)
|
|
{
|
|
m_title = Title;
|
|
m_username = username;
|
|
}
|
|
void setCheckStr(DataBaseSql *pDb, QString m_str, QString savefilePath,int nType){
|
|
m_pDb = pDb;
|
|
m_CheckStr = m_str;
|
|
m_saveFilename = savefilePath;
|
|
m_Type = nType;
|
|
};
|
|
void run() Q_DECL_OVERRIDE{
|
|
if (m_Type == 0){
|
|
saveHistory();
|
|
}
|
|
else if (m_Type==1)
|
|
{
|
|
saveLog();
|
|
}
|
|
|
|
emit(resultReady(m_Type));
|
|
};
|
|
private:
|
|
void saveLog()
|
|
{
|
|
QSqlQuery sql;
|
|
if (m_pDb){
|
|
m_pDb->checkoutData(m_CheckStr, sql);
|
|
QFile file(m_saveFilename);
|
|
if (!file.open(QIODevice::WriteOnly | QIODevice::Text/* | QIODevice::Append*/))// 追加写入数据
|
|
return;
|
|
QTextStream out(&file);
|
|
QString tableData;
|
|
int nIndex = 0;
|
|
out << m_title << "\n";
|
|
out << QString("序号") << "," << QString("时间") << "," << QString("信息") << "\n";
|
|
while (sql.next())
|
|
{
|
|
nIndex = nIndex + 1;
|
|
out << nIndex << ",";
|
|
QString time = sql.value("time").toString();
|
|
QString message = sql.value("message").toString();
|
|
QString classd = sql.value("class").toString();
|
|
out << time << "," << message << ",";
|
|
out << "\n";
|
|
}
|
|
|
|
out << QString("总数") << "," << QString::number(nIndex) << "\n";
|
|
out << QString("保存时间:") << "," << QDateTime::currentDateTime().toString("yyyy-MM-dd") << "," << QDateTime::currentDateTime().toString("hh:mm:ss") << "\n";
|
|
out << QString("用户:") << "," << m_username << "," << "\n";
|
|
file.close();
|
|
}
|
|
};
|
|
void saveHistory()
|
|
{
|
|
QSqlQuery sql;
|
|
if (m_pDb){
|
|
m_pDb->checkoutData(m_CheckStr, sql);
|
|
|
|
QFile file(m_saveFilename);
|
|
if (!file.open(QIODevice::WriteOnly | QIODevice::Text/* | QIODevice::Append*/))// 追加写入数据
|
|
return ;
|
|
QTextStream out(&file);
|
|
QString tableData;
|
|
|
|
out << m_title << "\n";
|
|
|
|
out << QString("序号") << "," << QString("时间") << "," << QString("匹配型号") << "," << QString("相似度") << "," << QString("消耗时间") << "," << QString("直径") << "," << QString("厚度") << "\n";
|
|
int nIndex = 0;
|
|
while (sql.next())
|
|
{
|
|
QString time = sql.value("time").toString();
|
|
QString model = sql.value("model").toString();
|
|
double dCorrelate = sql.value("correlate").toDouble();
|
|
QString correlate;
|
|
if (dCorrelate >= 1.7976931348623158e+308)
|
|
dCorrelate = 0;
|
|
correlate = QString::number(dCorrelate * 100, 'f', 3) + "%";
|
|
double dDetecttime = sql.value("detecttime").toDouble();
|
|
QString detecttime = QString::number(dDetecttime, 'f', 3);
|
|
double dDiameter = sql.value("diameter").toDouble();
|
|
QString diameter = QString::number(dDiameter, 'f', 3);
|
|
double dHight = sql.value("hight").toDouble();
|
|
QString hight = QString::number(dHight, 'f', 3);
|
|
|
|
nIndex = nIndex + 1;
|
|
out << nIndex << ",";
|
|
out << time << "," << model << "," << correlate << "," << detecttime << "," << diameter << "," << hight << "\n";
|
|
}
|
|
out << QString("总数") << "," << QString::number(nIndex) << "\n";
|
|
out << QString("时间:") << "," << QDateTime::currentDateTime().toString("yyyy-MM-dd") << "," << QDateTime::currentDateTime().toString("hh:mm:ss") << "\n";
|
|
out << QString("用户:") << "," << m_username << "," << "\n";
|
|
file.close();
|
|
}
|
|
};
|
|
private:
|
|
int m_Type;
|
|
DataBaseSql *m_pDb;
|
|
QString m_CheckStr;
|
|
QString m_saveFilename;
|
|
QString m_title;
|
|
QString m_username;
|
|
signals:
|
|
void resultReady(int nType);
|
|
};
|
|
|
|
#endif // QSAVECSVTHREAD_H
|