Files
2024-02-27 15:29:15 +08:00

309 lines
8.5 KiB
C++

#include <string>
#include <iostream>
#include <qthread.h>
#include <qqueue.h>
#include <qmutex.h>
#include <iomanip>
#include <sstream>
#include <qdebug.h>
#include <qfile.h>
#include "threadPoolController.h"
//用于记录写入csv的缺陷数据
class ngNoteCls
{
public:
struct defectStruct
{
QString Name;
vector<double>Value;
defectStruct(QString nameInput)
{
Name = nameInput;
}
};
unordered_map<QString, defectStruct>ngDefectMap;
unordered_map<QString, defectStruct>okDefectMap;
unordered_map<QString, defectStruct>::iterator it;
/// <summary>
/// 数据存在返回true,不存在返回false
/// </summary>
/// <param name="name">数据名</param>
/// <returns></returns>
bool checkExisted(QString name, unordered_map<QString, defectStruct>* map)
{
it = map->find(name);
if (it == map->end())
{
return false;
}
return true;
}
void insertNgData(QString name)
{
if (checkExisted(name, &ngDefectMap) == false)
{
defectStruct defect(name);
ngDefectMap.insert({ name,defect });
}
}
void insertNgData(QString name, double width, double height)
{
if (checkExisted(name, &ngDefectMap) == false)
{
defectStruct defect(name);
defect.Value.push_back(width);
defect.Value.push_back(height);
ngDefectMap.insert({ name,defect });
}
else//负数缺陷大小设0
{
defectStruct* defect = &it->second;
for (auto& data : defect->Value)
data = 0;
}
}
void insertNgData(QString name, double width, double height, double depth)
{
if (checkExisted(name, &ngDefectMap) == false)
{
defectStruct defect(name);
defect.Value.push_back(width);
defect.Value.push_back(height);
defect.Value.push_back(depth);
ngDefectMap.insert({ name,defect });
}
else//负数缺陷大小设0
{
defectStruct* defect = &it->second;
for (auto& data : defect->Value)
data = 0;
}
}
QString getNgSummaryStr()
{
QString str;
for (auto it : ngDefectMap)
{
str += "&MAX-" + it.second.Name + "_(";
if (it.second.Value.size() > 0)
{
for (auto data : it.second.Value)
str += QString::number(data) + "*";
str.left(str.size() - 1);
}
str += ")";
}
return str;
}
void insertOkData(QString name)
{
if (checkExisted(name, &okDefectMap) == false)
{
defectStruct defect(name);
okDefectMap.insert({ name,defect });
}
}
void insertOkData(QString name, double width, double height)
{
if (checkExisted(name, &okDefectMap) == false)
{
defectStruct defect(name);
defect.Value.push_back(width);
defect.Value.push_back(height);
okDefectMap.insert({ name,defect });
}
else//负数缺陷大小设0
{
defectStruct* defect = &it->second;
for (auto& data : defect->Value)
data = 0;
}
}
void insertOkData(QString name, double width, double height, double depth)
{
if (checkExisted(name, &okDefectMap) == false)
{
defectStruct defect(name);
defect.Value.push_back(width);
defect.Value.push_back(height);
defect.Value.push_back(depth);
okDefectMap.insert({ name,defect });
}
else//负数缺陷大小设0
{
defectStruct* defect = &it->second;
for (auto& data : defect->Value)
data = 0;
}
}
QString getOkSummaryStr()
{
QString str;
for (auto it : okDefectMap)
{
str += "&OK-" + it.second.Name + "_(";
if (it.second.Value.size() > 0)
{
for (auto data : it.second.Value)
str += QString::number(data) + "*";
str.left(str.size() - 1);
}
str += ")";
}
return str;
}
private:
};
class csvController
{
public:
//std::string fileSavePath="D://CCD//";
static void writeToData_xxxCsvOkNgNew(detData* rdata, QString OKNG, ngNoteCls* note, std::string fileSavePath)
{
QDateTime currentDateTime = QDateTime::currentDateTime();
std::string ymd = currentDateTime.toString("yyyy-MM-dd").toStdString();
QString filePath = QString::fromStdString(fileSavePath + "//result//" + ymd + "//Data_" + ymd + ".csv");
/* QString searchString = QString::fromStdString(rdata->getSN() + "_" + to_string(rdata->snCountIndex));*/
QString searchString = QString::fromStdString(rdata->SNcode);
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug() << "Error opening file.";
}
QStringList headers;
QList<QStringList> data;
QTextStream in(&file);
while (!in.atEnd()) {
QString line = in.readLine();
QStringList fields = line.split(',');
if (headers.isEmpty()) {
// 如果headers为空,则当前行为标题行
headers = fields;
}
else {
data.append(fields);
}
}
file.close();
// 在data中进行字符串检索并修改匹配的行
int modifiedRowCount = 0;
for (int i = data.size() - 1; i > 0; --i) {
QStringList& row = data[i];
//if (row.size()<3||row.isEmpty())
//{
// break;
//}
// 在当前行中检索字符串
try
{
if (row.size() >= 6)
if (row[1].contains(searchString, Qt::CaseInsensitive)) {
// 匹配到字符串,进行修改
row[3] = OKNG;
row[4] += note->getNgSummaryStr();
row[5] += note->getOkSummaryStr();
modifiedRowCount++;
}
}
catch (...)
{
break;
}
}
// 将修改后的数据写回文件
QFile writeFile(filePath);
if (!writeFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
qDebug() << "Error opening file for writing.";
}
QTextStream out(&writeFile);
// 写入标题行
out << headers.join(",") << "\n";
// 写入修改后的数据行
for (const auto& row : data) {
out << row.join(",") << "\n";
}
writeFile.close();
qDebug() << "Modified rows:" << modifiedRowCount;
qDebug() << "Data modified and saved to data_modified.csv";
//return 0;
}
static void writeToData_xxxCSVNew(detData* resData, string OKNG, ngNoteCls* note, std::string fileSavePath)
{
QDateTime currentDateTime = QDateTime::currentDateTime();
std::string ymd = currentDateTime.toString("yyyy-MM-dd").toStdString();
std::string ymdHmsz = currentDateTime.toString("yyyy-MM-dd-hh-mm-ss-zzzz").toStdString();
int hour = currentDateTime.time().hour();
QString filePath = QString::fromStdString(fileSavePath + "//result//" + ymd + "//Data_" + ymd + ".csv");
//string sn(resData->SNcode + "-" + to_string(resData->snCountIndex));
string sn(resData->SNcode);
/*std::string str(to_string(hour) + "H//" + sn + "-" + to_string(resData->snCountIndex) + "//" + ymd
+ "-" + to_string(hour) + "_" + resData->getSN() + "-" + to_string(resData->snCountIndex));*/
std::string str(to_string(hour) + "H//" + sn + "//" + ymd
+ "-" + to_string(hour) + "_" + sn);
bool fileExist = false;
QStringList dataList;
//if (hour < 10)
// dataList.append("0" + QString::number(hour) + "H");
//else
// dataList.append(QString::number(hour) + "H");
dataList.append(QString::fromStdString(ymdHmsz));
dataList.append(QString::fromStdString(sn));
dataList.append(QString::fromStdString(str));
dataList.append(QString::fromStdString(OKNG));
dataList.append(note->getNgSummaryStr());
dataList.append(note->getOkSummaryStr());
// 创建CSV文件
QFile file(filePath);
if (!file.exists())
{
// 创建目录(如果不存在)
QDir dir;
if (!dir.mkpath(QFileInfo(filePath).absolutePath()))
{
//qWarning() << "无法创建目录:" << QFileInfo(filePath).absolutePath();
return;
}
fileExist = true;
}
// 打开CSV文件并写入数据
if (file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) {
QTextStream stream(&file);
if (fileExist == true)
{
stream << "Time,";
stream << "SN,";
stream << "Path,";
stream << "Result,";
stream << "summuryResult,";
stream << "\n";
}
// 将数据写入CSV文件
for (const QString& data : dataList) {
stream << data << ",";
}
stream << "\n";
file.close();
qDebug() << "CSV文件写入成功";
}
//else
// qWarning() << "无法打开CSV文件:" << filePath;
}
};
#pragma once