LAPTOP-SNT8I5JK\Boounion
2025-10-13 047c7cbd047e11fba8d7872e69a11a13e463aec4
SourceCode/Bond/Servo/CPageGlassList.cpp
@@ -372,6 +372,55 @@
    return true;
}
// 辅助函数:将 ANSI CString 写入文件为 UTF-8 编码
bool CPageGlassList::WriteAnsiStringAsUtf8ToFile(const CString& ansiContent, const CString& filePath)
{
    CFile file;
    if (!file.Open(filePath, CFile::modeCreate | CFile::modeWrite)) {
        return false;
    }
    // 写入 UTF-8 BOM
    const unsigned char bom[] = { 0xEF, 0xBB, 0xBF };
    file.Write(bom, 3);
    // 将 ANSI 转换为 Unicode
    int unicodeLength = MultiByteToWideChar(CP_ACP, 0,
        ansiContent, ansiContent.GetLength(),
        NULL, 0);
    if (unicodeLength <= 0) {
        file.Close();
        return false;
    }
    wchar_t* unicodeBuffer = new wchar_t[unicodeLength + 1];
    MultiByteToWideChar(CP_ACP, 0,
        ansiContent, ansiContent.GetLength(),
        unicodeBuffer, unicodeLength);
    unicodeBuffer[unicodeLength] = 0;
    // 将 Unicode 转换为 UTF-8
    int utf8Length = WideCharToMultiByte(CP_UTF8, 0,
        unicodeBuffer, unicodeLength,
        NULL, 0, NULL, NULL);
    bool success = false;
    if (utf8Length > 0) {
        char* utf8Buffer = new char[utf8Length];
        WideCharToMultiByte(CP_UTF8, 0,
            unicodeBuffer, unicodeLength,
            utf8Buffer, utf8Length, NULL, NULL);
        file.Write(utf8Buffer, utf8Length);
        delete[] utf8Buffer;
        success = true;
    }
    delete[] unicodeBuffer;
    file.Close();
    return success;
}
// CPageGlassList 对话框
@@ -426,6 +475,7 @@
    ON_BN_CLICKED(IDC_BUTTON_PREV_PAGE, &CPageGlassList::OnBnClickedButtonPrevPage)
    ON_BN_CLICKED(IDC_BUTTON_NEXT_PAGE, &CPageGlassList::OnBnClickedButtonNextPage)
    ON_NOTIFY(ELCN_SHOWFULLTEXT, IDC_LIST_ALARM, &CPageGlassList::OnShowFullText)
    ON_BN_CLICKED(IDC_BUTTON_EXPORT_ROW, &CPageGlassList::OnBnClickedButtonExportRow)
END_MESSAGE_MAP()
// ===== 私有小工具 =====
@@ -1240,6 +1290,158 @@
    }
}
void CPageGlassList::OnBnClickedButtonExportRow()
{
    int nSelected = m_listCtrl.GetSelectionMark();
    if (nSelected == -1) {
        AfxMessageBox(_T("请先选择一行记录!"));
        return;
    }
    // 直接从第一列获取 ID
    CString strId = m_listCtrl.GetItemText(nSelected, 1);
    if (strId.IsEmpty()) {
        AfxMessageBox(_T("WIP记录暂不支持保存"));
        return;
    }
    // 数据库记录
    long long recordId = _ttoi64(strId);
    // 从数据库查询完整记录
    auto& db = GlassLogDb::Instance();
    auto row = db.queryById(recordId);
    if (!row) {
        AfxMessageBox(_T("查询记录失败"));
        return;
    }
    // 使用 Glass ID 构建默认文件名
    CString strDefaultFileName;
    CString strGlassId = row->classId.c_str();
    // 移除文件名中的非法字符
    CString strSanitizedGlassId = strGlassId;
    strSanitizedGlassId.Remove('\\');
    strSanitizedGlassId.Remove('/');
    strSanitizedGlassId.Remove(':');
    strSanitizedGlassId.Remove('*');
    strSanitizedGlassId.Remove('?');
    strSanitizedGlassId.Remove('"');
    strSanitizedGlassId.Remove('<');
    strSanitizedGlassId.Remove('>');
    strSanitizedGlassId.Remove('|');
    strDefaultFileName.Format(_T("Glass_%s.json"), strSanitizedGlassId);
    // 文件保存对话框,设置默认文件名
    CFileDialog fileDialog(FALSE, _T("json"), strDefaultFileName,
        OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
        _T("JSON Files (*.json)|*.json|CSV Files (*.csv)|*.csv||"));
    if (fileDialog.DoModal() != IDOK) return;
    CString filePath = fileDialog.GetPathName();
    CString fileExt = fileDialog.GetFileExt();
    if (fileExt.CompareNoCase(_T("json")) == 0) {
        // 保存为 JSON
        if (!row->pretty.empty()) {
            CFile file;
            if (file.Open(filePath, CFile::modeCreate | CFile::modeWrite)) {
                file.Write(row->pretty.c_str(), row->pretty.length());
                file.Close();
                CString strSuccess;
                strSuccess.Format(_T("记录已保存为JSON文件:\n%s"), filePath);
                AfxMessageBox(strSuccess);
            }
            else {
                AfxMessageBox(_T("保存文件失败"));
            }
        }
        else {
            AfxMessageBox(_T("该记录没有JSON数据"));
        }
    }
    else {
        // 保存为 CSV 格式 - 分段式
        CString csvContent;
        // === 第一部分:基础信息 ===
        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()));
        csvContent += baseInfoRow;
        // === 第二部分:工艺参数 ===
        csvContent += _T("\n=== 工艺参数 ===\n");
        // 如果有 pretty 字段,解析工艺参数
        if (!row->pretty.empty()) {
            SERVO::CGlass 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;
                        // 根据参数类型格式化数值
                        if (param.getValueType() == PVT_INT) {
                            valueStr.Format(_T("%d"), param.getIntValue());
                        }
                        else {
                            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())); // 这里显示机器单元
                        csvContent += paramRow;
                    }
                }
                else {
                    csvContent += _T("无工艺参数数据\n");
                }
            }
            else {
                csvContent += _T("无法解析工艺参数\n");
            }
        }
        else {
            csvContent += _T("无工艺参数数据\n");
        }
        // 使用辅助函数保存为 UTF-8 编码
        if (WriteAnsiStringAsUtf8ToFile(csvContent, filePath)) {
            CString strSuccess;
            strSuccess.Format(_T("记录已保存为CSV文件:\n%s"), filePath);
            AfxMessageBox(strSuccess);
        }
        else {
            AfxMessageBox(_T("保存文件失败"));
        }
    }
}
void CPageGlassList::OnBnClickedButtonPrevPage()
{
    if (m_nCurPage > 1) {