LAPTOP-T815PCOQ\25526
2024-12-10 726f0553fa435809d10c8a33abe46473ee975db1
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
// SystemLogManagerDlg.cpp: 实现文件
//
 
#include "stdafx.h"
#include "BondEq.h"
#include "afxdialogex.h"
#include "SystemLogManagerDlg.h"
 
 
// CSystemLogManagerDlg 对话框
 
IMPLEMENT_DYNAMIC(CSystemLogManagerDlg, CDialogEx)
 
CSystemLogManagerDlg::CSystemLogManagerDlg(CWnd* pParent /*=nullptr*/)
    : CDialogEx(IDD_DIALOG_SYSTEM_LOG_MANAGER, pParent)
{
    m_nInitialWidth = 0;
    m_nInitialHeight = 0;
}
 
CSystemLogManagerDlg::~CSystemLogManagerDlg()
{
    for (auto& pair : m_mapFonts) {
        if (pair.second) {
            pair.second->DeleteObject();
            delete pair.second;
        }
    }
    m_mapFonts.clear();
}
 
void CSystemLogManagerDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::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();
}
 
CFont* CSystemLogManagerDlg::GetOrCreateFont(int nFontSize)
{
    auto it = m_mapFonts.find(nFontSize);
    if (it != m_mapFonts.end()) {
        return it->second;
    }
 
    CFont* font = new CFont();
    LOGFONT logFont = { 0 };
    _tcscpy_s(logFont.lfFaceName, _T("Segoe UI"));
    logFont.lfHeight = -nFontSize;
    logFont.lfQuality = CLEARTYPE_QUALITY;
    font->CreateFontIndirect(&logFont);
    m_mapFonts[nFontSize] = font;
 
    return font;
}
 
void CSystemLogManagerDlg::UpdatePageInfo()
{
    // 格式化页码信息为 "当前页/总页数"
    CString pageInfo;
    pageInfo.Format(_T("%d/%d 页"), m_nCurrentPage, m_nTotalPages);
    m_staticPageNum.SetWindowText(pageInfo);
}
 
void CSystemLogManagerDlg::SetDefaultFont()
{
    CFont* defaultFont = GetOrCreateFont(12);
 
    // 遍历所有控件,应用默认字体
    CWnd* pWnd = GetWindow(GW_CHILD);
    while (pWnd) {
        // 跳过特殊控件(如 MFCGridCtrl)
        TCHAR szClassName[256];
        GetClassName(pWnd->m_hWnd, szClassName, sizeof(szClassName));
        if (_tcsicmp(szClassName, _T("MFCGridCtrl")) == 0) {
            pWnd = pWnd->GetNextWindow();
            continue;
        }
 
        pWnd->SetFont(defaultFont, TRUE);
        pWnd = pWnd->GetNextWindow();
    }
}
 
void CSystemLogManagerDlg::AdjustControls(float dScaleX, float dScaleY)
{
    CWnd* pWnd = GetWindow(GW_CHILD);
    while (pWnd) {
        int nCtrlID = pWnd->GetDlgCtrlID();
        if (nCtrlID != -1 && m_mapCtrlLayouts.find(nCtrlID) != m_mapCtrlLayouts.end())
        {
            CRect originalRect = m_mapCtrlLayouts[nCtrlID];
            CRect newRect(
                static_cast<int>(originalRect.left * dScaleX),
                static_cast<int>(originalRect.top * dScaleY),
                static_cast<int>(originalRect.right * dScaleX),
                static_cast<int>(originalRect.bottom * dScaleY));
 
            TCHAR szClassName[256];
            GetClassName(pWnd->m_hWnd, szClassName, sizeof(szClassName));
 
            if (_tcsicmp(szClassName, _T("ComboBox")) == 0) {
                CComboBox* pComboBox = (CComboBox*)pWnd;
                pComboBox->SetItemHeight(-1, newRect.Height());  // -1 表示所有项的高度
            }
 
            if (_tcsicmp(szClassName, _T("MFCGridCtrl")) == 0) {
                CGridCtrl* pGridCtrl = (CGridCtrl*)pWnd;
                pGridCtrl->SetDefCellHeight(newRect.Height() / 21);
                pGridCtrl->ExpandColumnsToFit(TRUE);
                pGridCtrl->ExpandLastColumn();
                pGridCtrl->Invalidate();
                pGridCtrl->UpdateWindow();
            }
 
            pWnd->MoveWindow(&newRect);
            AdjustControlFont(pWnd, newRect.Width(), newRect.Height());
        }
        pWnd = pWnd->GetNextWindow();
    }
}
 
void CSystemLogManagerDlg::AdjustControlFont(CWnd* pWnd, int nWidth, int nHeight)
{
    TCHAR szClassName[256];
    GetClassName(pWnd->m_hWnd, szClassName, sizeof(szClassName));
 
    // 跳过特殊控件(如 MFCGridCtrl)
    if (_tcsicmp(szClassName, _T("MFCGridCtrl")) == 0) {
        return;
    }
 
    // 根据控件高度动态调整字体大小
    int fontSize = nHeight / 2;
    if (fontSize < 8) fontSize = 8;
 
    // 获取或创建字体
    CFont* pFont = GetOrCreateFont(fontSize);
 
    pWnd->SetFont(pFont);
    pWnd->Invalidate(); // 刷新控件显示
}
 
 
void CSystemLogManagerDlg::AdjustComboBoxStyle(CComboBox& comboBox)
{
    DWORD dwStyle = comboBox.GetStyle();
    comboBox.ModifyStyle(0, CBS_DROPDOWNLIST | CBS_HASSTRINGS | CBS_OWNERDRAWFIXED);
 
    comboBox.Invalidate();
    comboBox.UpdateWindow();
}
 
 
void CSystemLogManagerDlg::AdjustDateTimeCtrlStyle(CDateTimeCtrl& dateTimeCtrl)
{
    dateTimeCtrl.ModifyStyle(0, DTS_RIGHTALIGN);
    dateTimeCtrl.Invalidate();
    dateTimeCtrl.UpdateWindow();
}
 
 
BEGIN_MESSAGE_MAP(CSystemLogManagerDlg, CDialogEx)
    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)
    ON_WM_SIZE()
    ON_WM_GETMINMAXINFO()
END_MESSAGE_MAP()
 
 
// CSystemLogManagerDlg 消息处理程序
 
 
BOOL CSystemLogManagerDlg::OnInitDialog()
{
    CDialogEx::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);
 
    CRect screenRect, dlgRect, clientRect;
    GetClientRect(&clientRect);
    m_nInitialWidth = clientRect.Width();
    m_nInitialHeight = clientRect.Height();
 
    // 初始化默认字体
    CFont* pDefaultFont = GetOrCreateFont(12);
 
    // 遍历所有子控件,记录初始位置并设置默认字体
    CWnd* pWnd = GetWindow(GW_CHILD);
    while (pWnd) {
        int nCtrlID = pWnd->GetDlgCtrlID();
        if (nCtrlID != -1) {
            // 记录控件初始布局
            CRect ctrlRect;
            pWnd->GetWindowRect(&ctrlRect);
            ScreenToClient(&ctrlRect);
            m_mapCtrlLayouts[nCtrlID] = ctrlRect;
 
            // 跳过特殊控件(如 MFCGridCtrl)
            TCHAR szClassName[256];
            GetClassName(pWnd->m_hWnd, szClassName, sizeof(szClassName));
            if (_tcsicmp(szClassName, _T("MFCGridCtrl")) == 0) {
                pWnd = pWnd->GetNextWindow();
                continue;
            }
 
            // 设置默认字体
            pWnd->SetFont(pDefaultFont);
        }
        pWnd = pWnd->GetNextWindow();
    }
 
    GetWindowRect(&dlgRect);
    int dlgWidth = dlgRect.Width() * 2;
    int dlgHeight = dlgRect.Height() * 2;
 
    SystemParametersInfo(SPI_GETWORKAREA, 0, &screenRect, 0);
    if (dlgWidth > screenRect.Width()) {
        dlgWidth = screenRect.Width();
    }
    if (dlgHeight > screenRect.Height()) {
        dlgHeight = screenRect.Height();
    }
 
    int centerX = screenRect.left + (screenRect.Width() - dlgWidth) / 2;
    int centerY = screenRect.top + (screenRect.Height() - dlgHeight) / 2;
    MoveWindow(centerX, centerY, dlgWidth, dlgHeight);
 
    InitSystemLogManager();
 
    return TRUE;  // return TRUE unless you set the focus to a control
    // 异常: OCX 属性页应返回 FALSE
}
 
 
void CSystemLogManagerDlg::OnSize(UINT nType, int cx, int cy)
{
    CDialogEx::OnSize(nType, cx, cy);
 
    // TODO: 在此处添加消息处理程序代码
    if (nType == SIZE_MINIMIZED || m_mapCtrlLayouts.empty()) {
        return;
    }
 
    float dScaleX = static_cast<float>(cx) / m_nInitialWidth;
    float dScaleY = static_cast<float>(cy) / m_nInitialHeight;
 
    // 遍历对话框中的所有控件
    AdjustControls(dScaleX, dScaleY);
}
 
 
void CSystemLogManagerDlg::OnGetMinMaxInfo(MINMAXINFO* lpMMI)
{
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    lpMMI->ptMinTrackSize.x = 400; // 最小宽度
    lpMMI->ptMinTrackSize.y = 300; // 最小高度
 
    CDialogEx::OnGetMinMaxInfo(lpMMI);
}
 
 
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);
    }
}