csv写入阶段备份
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
#include "Qt_epWindow.h"
|
||||
#include "qapplication.h"
|
||||
#include <rlib_writeCsv.hpp>
|
||||
#include <rlib_writeCsv.h>
|
||||
#include <global.h>
|
||||
#pragma execution_character_set("utf-8")
|
||||
|
||||
@@ -2118,7 +2118,7 @@ void Qt_epWindow::getTransferData(QString key, QString cameraName, QString snInf
|
||||
strList.push_back(cameraName.toStdString());//相机名
|
||||
for (int cnt = 1; cnt < outDataList.size(); cnt++)
|
||||
strList.push_back(outDataList[cnt].toStdString());
|
||||
WriteToCsv("./csv/" + ymd.toStdString()+".csv", strList);
|
||||
//WriteToCsv("./csv/" + ymd.toStdString()+".csv", strList);
|
||||
}
|
||||
ui.plainTextEdit_run->appendPlainText(runLogList.last());
|
||||
//qApp->processEvents();
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
#include <rlib_writeCsv.h>
|
||||
|
||||
@@ -0,0 +1,155 @@
|
||||
#include <qfile.h>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <mutex>
|
||||
#include <qdatetime.h>
|
||||
#include <memory>
|
||||
#include <qtextstream.h>
|
||||
class CsvWriter
|
||||
{
|
||||
public:
|
||||
CsvWriter()
|
||||
{
|
||||
init();
|
||||
}
|
||||
|
||||
//输入 键 和 数值的键值对,键默认为条码
|
||||
void WriteToCsv(QString key,std::vector<std::pair<QString, QString>> strList)
|
||||
{
|
||||
//查询条码是否存在
|
||||
std::lock_guard<std::mutex> locker(_mutex);
|
||||
QTextStream currentCsvStream(currentCsvFile.get());
|
||||
auto it = currentKeys->find(key);
|
||||
if (it != currentKeys->end()) {
|
||||
QList<QString> lines;
|
||||
while (!currentCsvStream.atEnd()) {
|
||||
lines.append(currentCsvStream.readLine());
|
||||
}
|
||||
//lines[it->second]
|
||||
}
|
||||
/*QByteArray qstr;
|
||||
for (int cnt = 0; cnt < strList.size() - 1; cnt++)
|
||||
qstr.append(QByteArray::fromStdString(strList[cnt]) + ",");
|
||||
qstr.append(QByteArray::fromStdString(strList[strList.size() - 1]) + CRLF);*/
|
||||
}
|
||||
void switchCsvFile(std::string path)
|
||||
{
|
||||
//创建文件并检查是否存在
|
||||
|
||||
}
|
||||
void timeCheck()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
std::mutex _mutex;
|
||||
std::shared_ptr<QFile> lastCsvFile;
|
||||
std::shared_ptr<QFile> currentCsvFile;
|
||||
const char* CRLF = "\r\n";
|
||||
QByteArray csvHeaderStr = "";
|
||||
|
||||
std::unordered_map <QString, int> headers;
|
||||
const char* keyName = "条码";
|
||||
std::unordered_map<QString, int>* lastKeys;
|
||||
std::unordered_map<QString, int>* currentKeys;
|
||||
void init()
|
||||
{
|
||||
std::lock_guard<std::mutex> locker(_mutex);
|
||||
addHeader("条码");
|
||||
addHeader("创建时间");
|
||||
addHeader("修改时间");
|
||||
{
|
||||
lastCsvFile = openCsvFile(getLastProcessCsv());
|
||||
QTextStream lastCsvStream(lastCsvFile.get());
|
||||
while (!lastCsvStream.atEnd()) {
|
||||
auto line = lastCsvStream.readLine();
|
||||
auto lineSplit = line.split(",");
|
||||
if (lineSplit.size() > 0){
|
||||
lastKeys->insert({ lineSplit[0],lastKeys->size() + 1 });
|
||||
}
|
||||
}
|
||||
}
|
||||
{
|
||||
currentCsvFile = openCsvFile(getCurrentProcessCsv());
|
||||
QTextStream currentCsvStream(currentCsvFile.get());
|
||||
while (!currentCsvStream.atEnd()) {
|
||||
auto line = currentCsvStream.readLine();
|
||||
auto lineSplit = line.split(",");
|
||||
if (lineSplit.size() > 0) {
|
||||
currentKeys->insert({ lineSplit[0],currentKeys->size() + 1 });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
void addHeader(QString header) {
|
||||
headers.insert(std::make_pair(header, headers.size()));
|
||||
if (csvHeaderStr == "")
|
||||
csvHeaderStr.append(header);
|
||||
else
|
||||
csvHeaderStr.append(header + ",");
|
||||
}
|
||||
std::string getCurrentProcessCsv(){
|
||||
// 获取当前时间
|
||||
QDateTime currentDateTime = QDateTime::currentDateTime();
|
||||
|
||||
// 获取当前小时和分钟
|
||||
int hour = currentDateTime.time().hour();
|
||||
int minute = currentDateTime.time().minute();
|
||||
|
||||
// 判断是否在白班时间范围(8:30到20:30)
|
||||
bool isDayShift = (hour > 8 || (hour == 8 && minute >= 30)) && (hour < 20 || (hour == 20 && minute <= 30));
|
||||
|
||||
// 获取当前日期(yyyy-MM-dd格式)
|
||||
QString dateStr = currentDateTime.toString("yyyy-MM-dd");
|
||||
|
||||
// 根据时间段返回班次信息
|
||||
if (isDayShift) {
|
||||
return (dateStr + "白班").toStdString();
|
||||
}
|
||||
else {
|
||||
return (dateStr + "夜班").toStdString();
|
||||
}
|
||||
}
|
||||
std::string getLastProcessCsv(){
|
||||
// 获取当前时间
|
||||
QDateTime currentDateTime = QDateTime::currentDateTime();
|
||||
|
||||
// 获取当前小时和分钟
|
||||
int hour = currentDateTime.time().hour();
|
||||
int minute = currentDateTime.time().minute();
|
||||
|
||||
// 判断是否在白班时间范围(8:30到20:30)
|
||||
bool isDayShift = (hour > 8 || (hour == 8 && minute >= 30)) && (hour < 20 || (hour == 20 && minute <= 30));
|
||||
|
||||
// 获取上一班的日期
|
||||
QDateTime lastShiftDateTime;
|
||||
if (isDayShift) {
|
||||
// 当前是白班,上一班是前一天的夜班
|
||||
lastShiftDateTime = currentDateTime.addDays(-1);
|
||||
}
|
||||
else {
|
||||
// 当前是夜班,上一班是前一天的白班
|
||||
lastShiftDateTime = currentDateTime.addDays(-1);
|
||||
}
|
||||
|
||||
// 获取上一班的日期(yyyy-MM-dd格式)
|
||||
QString dateStr = lastShiftDateTime.toString("yyyy-MM-dd");
|
||||
|
||||
// 根据时间段返回班次信息
|
||||
if (isDayShift) {
|
||||
return (dateStr + "夜班").toStdString(); // 上一班是夜班
|
||||
}
|
||||
else {
|
||||
return (dateStr + "白班").toStdString(); // 上一班是白班
|
||||
}
|
||||
}
|
||||
std::shared_ptr<QFile>openCsvFile(std::string path){
|
||||
std::shared_ptr<QFile> CsvFile = std::make_shared<QFile>(QString::fromStdString(path));
|
||||
bool FileExist = CsvFile->exists();
|
||||
CsvFile->open(QIODevice::ReadWrite);
|
||||
if (FileExist == false)
|
||||
CsvFile->write(csvHeaderStr);
|
||||
}
|
||||
};
|
||||
@@ -1,19 +0,0 @@
|
||||
#include <qfile.h>
|
||||
#include <string>
|
||||
#define CsvHeadStr "时间,条码,相机名,总结果,撕裂,翻折,极耳宽,极耳高,焊印1,焊印2\r\n"
|
||||
#define CRLF "\r\n"
|
||||
void WriteToCsv(std::string filePath,std::vector<std::string> strList)
|
||||
{
|
||||
QFile CsvFile(QString::fromStdString(filePath));
|
||||
bool FileExist = CsvFile.exists();
|
||||
CsvFile.open(QIODevice::ReadWrite);
|
||||
if (FileExist == false)
|
||||
CsvFile.write(CsvHeadStr);
|
||||
|
||||
QByteArray qstr;
|
||||
for (int cnt = 0; cnt <strList.size()-1;cnt++)
|
||||
qstr.append(QByteArray::fromStdString(strList[cnt])+",");
|
||||
qstr.append(QByteArray::fromStdString(strList[strList.size()-1]) + CRLF);
|
||||
CsvFile.write(qstr);
|
||||
CsvFile.close();
|
||||
}
|
||||
Reference in New Issue
Block a user