LAPTOP-SNT8I5JK\Boounion
2025-06-12 552b0d6e5d9f02c5f50e7407f0b274f4b926a043
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// SystemLogManagerDlg.cpp: 实现文件
//
 
#include "stdafx.h"
#include "Servo.h"
#include "afxdialogex.h"
#include "SystemLogManagerDlg.h"
#include "UserManager.h"
#include "SystemLogManager.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);
    }
}