LAPTOP-SNT8I5JK\Boounion
2025-10-14 cfcab53b0e7d5918c79cc77f0f447730682f94b1
SourceCode/Bond/Servo/CPageGlassList.cpp
@@ -1347,11 +1347,20 @@
    CString fileExt = fileDialog.GetFileExt();
    if (fileExt.CompareNoCase(_T("json")) == 0) {
        ExportToJson(*row, filePath);
    }
    else {
        ExportToCsv(*row, filePath);
    }
}
void CPageGlassList::ExportToJson(const GlassLogDb::Row& row, const CString& filePath)
{
        // 保存为 JSON
        if (!row->pretty.empty()) {
    if (!row.pretty.empty()) {
            CFile file;
            if (file.Open(filePath, CFile::modeCreate | CFile::modeWrite)) {
                file.Write(row->pretty.c_str(), row->pretty.length());
            file.Write(row.pretty.c_str(), row.pretty.length());
                file.Close();
                CString strSuccess;
@@ -1366,36 +1375,60 @@
            AfxMessageBox(_T("该记录没有JSON数据"));
        }
    }
    else {
        // 保存为 CSV 格式 - 分段式
void CPageGlassList::ExportToCsv(const GlassLogDb::Row& row, const CString& filePath)
{
        CString csvContent;
        // === 第一部分:基础信息 ===
    ExportBasicInfo(csvContent, row);
    // === 第二部分:工艺参数 ===
    ExportProcessParams(csvContent, row);
    // === 第三部分:传感器数据详情 ===
    ExportSensorData(csvContent, row);
    // 使用辅助函数保存为 UTF-8 编码
    if (WriteAnsiStringAsUtf8ToFile(csvContent, filePath)) {
        CString strSuccess;
        strSuccess.Format(_T("记录已保存为CSV文件:\n%s"), filePath);
        AfxMessageBox(strSuccess);
    }
    else {
        AfxMessageBox(_T("保存文件失败"));
    }
}
void CPageGlassList::ExportBasicInfo(CString& csvContent, const GlassLogDb::Row& row)
{
        csvContent += _T("=== 基础信息 ===\n");
        csvContent += _T("ID,Cassette序列号,Job序列号,Glass ID,物料类型,状态,开始时间,结束时间,绑定Glass ID,AOI结果,路径\n");
        CString baseInfoRow;
        baseInfoRow.Format(_T("%lld,%d,%d,%s,%d,%d,%s,%s,%s,%d,%s\n"),
            row->id, row->cassetteSeqNo, row->jobSeqNo,
            CString(row->classId.c_str()), row->materialType, row->state,
            CString(row->tStart.c_str()), CString(row->tEnd.c_str()),
            CString(row->buddyId.c_str()), row->aoiResult,
            CString(row->path.c_str()));
        row.id, row.cassetteSeqNo, row.jobSeqNo,
        CString(row.classId.c_str()), row.materialType, row.state,
        CString(row.tStart.c_str()), CString(row.tEnd.c_str()),
        CString(row.buddyId.c_str()), row.aoiResult,
        CString(row.path.c_str()));
        csvContent += baseInfoRow;
}
        // === 第二部分:工艺参数 ===
void CPageGlassList::ExportProcessParams(CString& csvContent, const GlassLogDb::Row& row)
{
        csvContent += _T("\n=== 工艺参数 ===\n");
        // 如果有 pretty 字段,解析工艺参数
        if (!row->pretty.empty()) {
    if (!row.pretty.empty()) {
            SERVO::CGlass tempGlass;
            if (GlassJson::FromString(row->pretty, tempGlass)) {
        if (GlassJson::FromString(row.pretty, tempGlass)) {
                auto& params = tempGlass.getParams();
                if (!params.empty()) {
                    // 工艺参数表头 - 调整后的列
                // 工艺参数表头
                    csvContent += _T("参数名称,参数ID,数值,机器单元\n");
                    // 工艺参数数据 - 调整后的格式
                // 工艺参数数据
                    for (auto& param : params) {
                        CString paramRow;
                        CString valueStr;
@@ -1408,12 +1441,11 @@
                            valueStr.Format(_T("%.3f"), param.getDoubleValue());
                        }
                        // 调整后的格式:去掉数值类型列
                        paramRow.Format(_T("%s,%s,%s,%s\n"),
                            CString(param.getName().c_str()),
                            CString(param.getId().c_str()),
                            valueStr,
                            CString(param.getUnit().c_str())); // 这里显示机器单元
                        CString(param.getUnit().c_str()));
                        csvContent += paramRow;
                    }
@@ -1429,16 +1461,96 @@
        else {
            csvContent += _T("无工艺参数数据\n");
        }
}
        // 使用辅助函数保存为 UTF-8 编码
        if (WriteAnsiStringAsUtf8ToFile(csvContent, filePath)) {
            CString strSuccess;
            strSuccess.Format(_T("记录已保存为CSV文件:\n%s"), filePath);
            AfxMessageBox(strSuccess);
void CPageGlassList::ExportSensorData(CString& csvContent, const GlassLogDb::Row& row)
{
    csvContent += _T("\n=== 传感器数据详情 ===\n");
    // 如果有 pretty 字段,解析传感器数据
    if (!row.pretty.empty()) {
        SERVO::CGlass tempGlass;
        if (GlassJson::FromString(row.pretty, tempGlass)) {
            // 对每个机器生成表格
            for (const auto& machinePair : tempGlass.getAllSVData()) {
                int machineId = machinePair.first;
                CString machineName = CString(SERVO::CServoUtilsTool::getEqName(machineId).c_str());
                csvContent += _T("\n[") + machineName + _T("]\n");
                // 获取该机器的预定义列顺序
                auto columnOrder = getMachineColumnOrder(machineId);
                if (columnOrder.empty()) {
                    csvContent += _T("无预定义列配置\n");
                    continue;
                }
                // 构建表头 - 直接使用中文列名
                CString header = _T("时间戳(ms),本地时间");
                for (const auto& dataType : columnOrder) {
                    header += _T(",");
                    header += CString(dataType.c_str()); // 直接使用中文列名
                }
                header += _T("\n");
                csvContent += header;
                // 检查是否有数据
                if (machinePair.second.empty()) {
                    csvContent += _T("无传感器数据\n");
                    continue;
                }
                // 使用第一个数据类型的时间序列作为基准
                const std::string& firstDataType = columnOrder[0];
                auto firstDataTypeIt = machinePair.second.find(firstDataType);
                if (firstDataTypeIt == machinePair.second.end() || firstDataTypeIt->second.empty()) {
                    csvContent += _T("无基准数据类型数据\n");
                    continue;
                }
                const auto& timeSeries = firstDataTypeIt->second;
                // 对于每个时间点,输出一行数据
                for (size_t i = 0; i < timeSeries.size(); i++) {
                    auto timestamp = timeSeries[i].timestamp;
                    // 时间戳(毫秒)
                    auto ms = timePointToMs(timestamp);
                    CString row;
                    row.Format(_T("%lld,"), ms);
                    // 本地时间字符串
                    CString localTime = CString(timePointToString(timestamp).c_str());
                    row += localTime;
                    // 按照预定义的列顺序输出数据
                    for (const auto& dataType : columnOrder) {
                        row += _T(",");
                        auto dataTypeIt = machinePair.second.find(dataType);
                        if (dataTypeIt != machinePair.second.end() && i < dataTypeIt->second.size()) {
                            // 直接按索引获取数据
                            CString valueStr;
                            valueStr.Format(_T("%.3f"), dataTypeIt->second[i].value);
                            row += valueStr;
        }
        else {
            AfxMessageBox(_T("保存文件失败"));
                            // 理论上不应该发生,因为您说没有空值
                            row += _T("N/A");
        }
                    }
                    row += _T("\n");
                    csvContent += row;
                }
            }
        }
        else {
            csvContent += _T("无法解析传感器数据\n");
        }
    }
    else {
        csvContent += _T("无传感器数据\n");
    }
}
@@ -1798,3 +1910,28 @@
    return CDialogEx::PreTranslateMessage(pMsg);
}
// 获取机器预定义的列顺序
std::vector<std::string> CPageGlassList::getMachineColumnOrder(int machineId)
{
    auto dataTypes = SERVO::CServoUtilsTool::getEqDataTypes();
    auto it = dataTypes.find(machineId);
    return it != dataTypes.end() ? it->second : std::vector<std::string>();
}
// 时间戳转换为字符串
std::string CPageGlassList::timePointToString(const std::chrono::system_clock::time_point& tp)
{
    auto time_t = std::chrono::system_clock::to_time_t(tp);
    std::tm tm;
    localtime_s(&tm, &time_t);
    char buffer[20];
    std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &tm);
    return buffer;
}
// 时间戳转换为毫秒
int64_t CPageGlassList::timePointToMs(const std::chrono::system_clock::time_point& tp)
{
    return std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch()).count();
}