// SystemLogManagerDlg.cpp: 实现文件 // #include "stdafx.h" #include "BondEq.h" #include "afxdialogex.h" #include "SystemLogManagerDlg.h" // CSystemLogManagerDlg 对话框 IMPLEMENT_DYNAMIC(CSystemLogManagerDlg, CBaseDlg) CSystemLogManagerDlg::CSystemLogManagerDlg(CWnd* pParent /*=nullptr*/) : CBaseDlg(IDD_DIALOG_SYSTEM_LOG_MANAGER, pParent) { } CSystemLogManagerDlg::~CSystemLogManagerDlg() { } void CSystemLogManagerDlg::DoDataExchange(CDataExchange* pDX) { CBaseDlg::DoDataExchange(pDX); DDX_Control(pDX, IDC_COMBO_TYPE, m_comboType); DDX_Control(pDX, IDC_COMBO_USER, m_comboUser); DDX_Control(pDX, IDC_DATETIMEPICKER_START, m_dateTimeStart); DDX_Control(pDX, IDC_DATETIMEPICKER_END, m_dateTimeEnd); DDX_Control(pDX, IDC_EDIT_DESCRIPTION, m_editDescription); DDX_Control(pDX, IDC_CUSTOM_LIST_LOGS, m_listLogs); DDX_Control(pDX, IDC_STATIC_PAGE_NUMBER, m_staticPageNum); } void CSystemLogManagerDlg::InitSystemLogManager() { if (m_listLogs.GetSafeHwnd() == NULL) return; int nRows = 21; // 包括表头(1 行)和数据(20 行) int nCols = 5; int nFixRows = 1; int nFixCols = 0; int nRowIdx = 0; int nColIdx = 0; m_listLogs.DeleteAllItems(); m_listLogs.SetVirtualMode(FALSE); m_listLogs.GetDefaultCell(TRUE, FALSE)->SetBackClr(g_nGridFixCellColor); // 设置固定行背景色 m_listLogs.GetDefaultCell(FALSE, TRUE)->SetBackClr(g_nGridFixCellColor); // 设置固定列背景色 m_listLogs.GetDefaultCell(FALSE, FALSE)->SetBackClr(g_nGridCellColor); // 设置单元格背景色 m_listLogs.SetFixedTextColor(g_nGridFixFontColor); // 设置固定行列字体颜色 m_listLogs.SetRowCount(nRows); m_listLogs.SetColumnCount(nCols); m_listLogs.SetFixedRowCount(nFixRows); m_listLogs.SetFixedColumnCount(nFixCols); // Col m_listLogs.SetColumnWidth(nColIdx, 10); m_listLogs.SetItemText(nRowIdx, nColIdx++, _T("No.")); m_listLogs.SetColumnWidth(nColIdx, 10); m_listLogs.SetItemText(nRowIdx, nColIdx++, _T("类型")); m_listLogs.SetColumnWidth(nColIdx, 100); m_listLogs.SetItemText(nRowIdx, nColIdx++, _T("事件")); m_listLogs.SetColumnWidth(nColIdx, 30); m_listLogs.SetItemText(nRowIdx, nColIdx++, _T("用户")); m_listLogs.SetColumnWidth(nColIdx, 50); m_listLogs.SetItemText(nRowIdx, nColIdx++, _T("时间")); // 创建 20 行空白数据行 for (int i = 1; i < nRows; ++i) { for (int j = 0; j < nCols; ++j) { m_listLogs.SetItemText(i, j, _T("")); // 初始化为空字符串 } } m_listLogs.SetFixedRowSelection(FALSE); m_listLogs.SetFixedColumnSelection(FALSE); m_listLogs.SetEditable(FALSE); m_listLogs.SetRowResize(FALSE); m_listLogs.SetColumnResize(TRUE); m_listLogs.ExpandColumnsToFit(TRUE); m_listLogs.SetListMode(TRUE); // 启用列表模式 m_listLogs.EnableSelection(TRUE); // 启用选择 m_listLogs.SetSingleRowSelection(TRUE); // 自动整行高亮(限制为单行选择) m_listLogs.ExpandLastColumn(); // 最后一列填充网格 try { FillSystemLogManager(); } catch (const std::exception& ex) { CString errorMsg; errorMsg.Format(_T("初始化运行日志失败:%s"), CString(ex.what())); AfxMessageBox(errorMsg, MB_ICONERROR); } } void CSystemLogManagerDlg::FillSystemLogManager() { // 获取筛选条件 CString selectedType, selectedUser, description; m_comboType.GetLBText(m_comboType.GetCurSel(), selectedType); m_comboUser.GetLBText(m_comboUser.GetCurSel(), selectedUser); m_editDescription.GetWindowText(description); COleDateTime startTime, endTime; m_dateTimeStart.GetTime(startTime); m_dateTimeEnd.GetTime(endTime); CString strStartTime = startTime.Format(_T("%Y-%m-%d %H:%M:%S")); CString strEndTime = endTime.Format(_T("%Y-%m-%d %H:%M:%S")); std::string type = CT2A(selectedType); std::string user = CT2A(selectedUser); std::string desc = CT2A(description); std::string start = CT2A(strStartTime); std::string end = CT2A(strEndTime); // 获取日志管理实例 SystemLogManager& logManager = SystemLogManager::getInstance(); int pageSize = 20; // 每页显示 20 条记录 int totalRecords = logManager.getTotalLogCount(type, user, desc, start, end); m_nTotalPages = (totalRecords + pageSize - 1) / pageSize; auto logs = logManager.getFilteredLogs(type, user, desc, start, end, m_nCurrentPage, pageSize); // 更新表格数据 int rowIdx = 1; for (const auto& log : logs) { m_listLogs.SetItemText(rowIdx, 0, CString(std::to_string(rowIdx).c_str())); // 序号 m_listLogs.SetItemText(rowIdx, 1, CString(log[1].c_str())); // 类型 m_listLogs.SetItemText(rowIdx, 2, CString(log[2].c_str())); // 事件 m_listLogs.SetItemText(rowIdx, 3, CString(log[3].c_str())); // 用户 m_listLogs.SetItemText(rowIdx, 4, CString(log[4].c_str())); // 时间 ++rowIdx; } // 清空多余行 for (; rowIdx <= 20; ++rowIdx) { m_listLogs.SetItemText(rowIdx, 0, CString(std::to_string(rowIdx).c_str())); // 序号列 for (int colIdx = 1; colIdx < m_listLogs.GetColumnCount(); ++colIdx) { m_listLogs.SetItemText(rowIdx, colIdx, _T("")); } } m_listLogs.ExpandColumnsToFit(FALSE); m_listLogs.ExpandLastColumn(); m_listLogs.Invalidate(); m_listLogs.UpdateWindow(); UpdatePageInfo(); } void CSystemLogManagerDlg::UpdatePageInfo() { // 格式化页码信息为 "当前页/总页数" CString pageInfo; pageInfo.Format(_T("%d/%d 页"), m_nCurrentPage, m_nTotalPages); m_staticPageNum.SetWindowText(pageInfo); } BEGIN_MESSAGE_MAP(CSystemLogManagerDlg, CBaseDlg) ON_BN_CLICKED(IDC_BUTTON_SEARCH, &CSystemLogManagerDlg::OnBnClickedButtonSearch) ON_BN_CLICKED(IDC_BUTTON_PREV_PAGE, &CSystemLogManagerDlg::OnBnClickedButtonPrevPage) ON_BN_CLICKED(IDC_BUTTON_NEXT_PAGE, &CSystemLogManagerDlg::OnBnClickedButtonNextPage) ON_CBN_SELCHANGE(IDC_COMBO_TYPE, &CSystemLogManagerDlg::OnSelchangeComboType) ON_CBN_SELCHANGE(IDC_COMBO_USER, &CSystemLogManagerDlg::OnSelchangeComboUser) END_MESSAGE_MAP() // CSystemLogManagerDlg 消息处理程序 BOOL CSystemLogManagerDlg::OnInitDialog() { CBaseDlg::OnInitDialog(); // TODO: 在此添加额外的初始化 SetWindowText(_T("系统运行日志")); m_nCurrentPage = 1; // 从第一页开始 m_nTotalPages = 1; // 默认总页数为 1 m_comboType.AddString(_T("ALL")); m_comboType.AddString(_T("信息")); m_comboType.AddString(_T("操作")); m_comboType.AddString(_T("错误")); m_comboType.AddString(_T("未知")); m_comboType.SetCurSel(0); m_comboUser.AddString(_T("ALL")); m_comboUser.AddString(_T("SYSTEM")); auto usernames = UserManager::getInstance().getUsernames(); for (const auto& username : usernames) { CString cstrUsername(username.c_str()); m_comboUser.AddString(cstrUsername); } m_comboUser.SetCurSel(0); // 设置为 30 天前 COleDateTime currentTime = COleDateTime::GetCurrentTime(); COleDateTime defaultStartTime = currentTime - COleDateTimeSpan(30, 0, 0, 0); m_dateTimeStart.SetTime(defaultStartTime); InitSystemLogManager(); return TRUE; // return TRUE unless you set the focus to a control // 异常: OCX 属性页应返回 FALSE } void CSystemLogManagerDlg::OnBnClickedButtonSearch() { // TODO: 在此添加控件通知处理程序代码 try { m_nCurrentPage = 1; FillSystemLogManager(); } catch (const std::exception& ex) { CString errorMsg; errorMsg.Format(_T("搜索失败:%s"), CString(ex.what())); AfxMessageBox(errorMsg, MB_ICONERROR); } } void CSystemLogManagerDlg::OnBnClickedButtonPrevPage() { // TODO: 在此添加控件通知处理程序代码 try { if (m_nCurrentPage > 1) { m_nCurrentPage--; FillSystemLogManager(); } else { AfxMessageBox(_T("已经是第一页!")); } } catch (const std::exception& ex) { CString errorMsg; errorMsg.Format(_T("切换到上一页失败:%s"), CString(ex.what())); AfxMessageBox(errorMsg, MB_ICONERROR); } } void CSystemLogManagerDlg::OnBnClickedButtonNextPage() { // TODO: 在此添加控件通知处理程序代码 try { if (m_nCurrentPage < m_nTotalPages) { m_nCurrentPage++; FillSystemLogManager(); } else { AfxMessageBox(_T("已经是最后一页!")); } } catch (const std::exception& ex) { CString errorMsg; errorMsg.Format(_T("切换到下一页失败:%s"), CString(ex.what())); AfxMessageBox(errorMsg, MB_ICONERROR); } } void CSystemLogManagerDlg::OnSelchangeComboType() { // TODO: 在此添加控件通知处理程序代码 try { m_nCurrentPage = 1; FillSystemLogManager(); } catch (const std::exception& ex) { CString errorMsg; errorMsg.Format(_T("切换类型失败:%s"), CString(ex.what())); AfxMessageBox(errorMsg, MB_ICONERROR); } } void CSystemLogManagerDlg::OnSelchangeComboUser() { // TODO: 在此添加控件通知处理程序代码 try { m_nCurrentPage = 1; FillSystemLogManager(); } catch (const std::exception& ex) { CString errorMsg; errorMsg.Format(_T("切换角色失败:%s"), CString(ex.what())); AfxMessageBox(errorMsg, MB_ICONERROR); } }