LAPTOP-SNT8I5JK\Boounion
2025-06-26 135d1d829b36eb06c8acc5b8782a77acda767198
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
// CPageRecipe.cpp: 实现文件
//
 
#include "stdafx.h"
#include "Servo.h"
#include "afxdialogex.h"
#include "PageRecipe.h"
#include "MsgDlg.h"
 
 
// CPageRecipe 对话框
 
IMPLEMENT_DYNAMIC(CPageRecipe, CDialogEx)
 
CPageRecipe::CPageRecipe(CWnd* pParent /*=nullptr*/)
    : CDialogEx(IDD_PAGE_RECIPE, pParent)
{
 
}
 
CPageRecipe::~CPageRecipe()
{
}
 
void CPageRecipe::FillDataToListCtrl(const std::vector<RecipeInfo>& vecRecipe) {
    CListCtrl* pListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST_PPID);
    if (pListCtrl == nullptr || !::IsWindow(pListCtrl->m_hWnd)) {
        return;
    }
 
    // 清空当前CListCtrl中的所有项
    pListCtrl->DeleteAllItems();
 
    // 遍历数据并插入到CListCtrl中
    for (int i = 0; i < static_cast<int>(vecRecipe.size()); ++i) {
        const RecipeInfo& recipe = vecRecipe[i];
 
        m_listPPID.InsertItem(i, _T("")); // 第0列空白
 
        CString strNo;
        strNo.Format(_T("%d"), i + 1);
        m_listPPID.SetItemText(i, 1, strNo);
 
        m_listPPID.SetItemText(i, 2, CA2T(recipe.strPPID.c_str()));
        m_listPPID.SetItemText(i, 3, CA2T(recipe.strDescription.c_str()));
        m_listPPID.SetItemText(i, 4, CA2T(recipe.strCreateTime.c_str()));
    }
 
    // 获取列数
    int nColCount = m_listPPID.GetHeaderCtrl()->GetItemCount();
    m_listPPID.SetColumnWidth(nColCount - 1, LVSCW_AUTOSIZE_USEHEADER);
}
 
void CPageRecipe::FillRecipeListToListCtrl(SERVO::CRecipeList* pList)
{
    CListCtrl* pListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST_PPID);
    if (pListCtrl == nullptr || !::IsWindow(pListCtrl->m_hWnd)) {
        return;
    }
 
    // 清空当前CListCtrl中的所有项
    pListCtrl->DeleteAllItems();
    if (pList == nullptr) {
        return;
    }
 
 
    // 遍历数据并插入到CListCtrl中
    std::map<int, short>& ids = pList->getIds();
    for (auto item : ids) {
        int index = m_listPPID.InsertItem(m_listPPID.GetItemCount(), _T(""));
        m_listPPID.SetItemText(index, 1, std::to_string(item.first).c_str());
        m_listPPID.SetItemText(index, 2, std::to_string(item.second).c_str());
    }
 
    // 获取列数
    int nColCount = m_listPPID.GetHeaderCtrl()->GetItemCount();
    m_listPPID.SetColumnWidth(nColCount - 1, LVSCW_AUTOSIZE_USEHEADER);
}
 
void CPageRecipe::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_LIST_PPID, m_listPPID);
}
 
 
BEGIN_MESSAGE_MAP(CPageRecipe, CDialogEx)
    ON_WM_SIZE()
    ON_BN_CLICKED(IDC_BUTTON_SEARCH, &CPageRecipe::OnBnClickedButtonSearch)
    ON_BN_CLICKED(IDC_BUTTON_MODIFY, &CPageRecipe::OnBnClickedButtonModify)
    ON_BN_CLICKED(IDC_BUTTON_DELETE, &CPageRecipe::OnBnClickedButtonDelete)
    ON_BN_CLICKED(IDC_BUTTON_DELETE_ALL, &CPageRecipe::OnBnClickedButtonDeleteAll)
    ON_BN_CLICKED(IDC_BUTTON_REFRESH, &CPageRecipe::OnBnClickedButtonRefresh)
    ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST_PPID, &CPageRecipe::OnLvnItemChangedListPPID)
    ON_WM_DESTROY()
    ON_CBN_SELCHANGE(IDC_COMBO_EQUIPMENT, &CPageRecipe::OnCbnSelchangeComboEquipment)
    ON_WM_SHOWWINDOW()
END_MESSAGE_MAP()
 
 
// CPageRecipe 消息处理程序
 
BOOL CPageRecipe::OnInitDialog()
{
    CDialogEx::OnInitDialog();
 
    // 读出列宽
    CString strIniFile, strItem;
    strIniFile.Format(_T("%s\\configuration.ini"), (LPTSTR)(LPCTSTR)theApp.m_strAppDir);
    int width[8] = { 0, 80, 180, 80, 80, 100, 80, 180 };
    for (int i = 0; i < 8; i++) {
        strItem.Format(_T("Col_%d_Width"), i);
        width[i] = GetPrivateProfileInt("PageRecipeListCtrl", strItem, width[i], strIniFile);
    }
 
 
    // TODO:  在此添加额外的初始化
    CListCtrl* pListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST_PPID);
    DWORD dwStyle = pListCtrl->GetExtendedStyle();
    dwStyle |= LVS_EX_FULLROWSELECT;
    dwStyle |= LVS_EX_GRIDLINES;
    pListCtrl->SetExtendedStyle(dwStyle);
 
    HIMAGELIST imageList = ImageList_Create(24, 24, ILC_COLOR24, 1, 1);
    ListView_SetImageList(pListCtrl->GetSafeHwnd(), imageList, LVSIL_SMALL);
    pListCtrl->InsertColumn(0, _T(""), LVCFMT_RIGHT, 0); // 隐藏列
    pListCtrl->InsertColumn(1, _T("No."), LVCFMT_LEFT, width[1]);
    pListCtrl->InsertColumn(2, _T("PPID/Recipe ID"), LVCFMT_LEFT, width[2]);
    pListCtrl->InsertColumn(3, _T("描述"), LVCFMT_LEFT, width[3]);
    pListCtrl->InsertColumn(4, _T("创建时间"), LVCFMT_LEFT, width[4]);
    pListCtrl->SetColumnWidth(4, LVSCW_AUTOSIZE_USEHEADER);
 
 
    // 获取所有数据
    auto vecData = RecipeManager::getInstance().getAllRecipes();
    FillDataToListCtrl(vecData);
 
    return TRUE;  // return TRUE unless you set the focus to a control
    // 异常: OCX 属性页应返回 FALSE
}
 
void CPageRecipe::OnSize(UINT nType, int cx, int cy)
{
    CDialogEx::OnSize(nType, cx, cy);
 
    if (!m_listPPID.GetSafeHwnd())
        return;
 
 
    // 左侧列表宽高自适应
    int margin = 12;
    int buttonWidth = 80;
    int buttonHeight = 30;
    int buttonSpacing = 10;
 
    CWnd* pItem;
    CRect rect, rcItem;
    GetClientRect(&rect);
 
    pItem = GetDlgItem(IDC_EDIT_KEYWORD);
    ASSERT(pItem);
    pItem->GetWindowRect(rcItem);
    ScreenToClient(&rcItem);
 
    int y = rcItem.bottom;
    y += 12;
    int listWidth = rect.Width() - buttonWidth - 3 * margin;
    m_listPPID.MoveWindow(margin, y, listWidth, rect.bottom - 12 - y);
 
 
    // 按钮竖直排列在右侧
    CWnd* buttons[] = {
        GetDlgItem(IDC_BUTTON_REFRESH),
        GetDlgItem(IDC_BUTTON_DELETE),
        GetDlgItem(IDC_BUTTON_DELETE_ALL),
        GetDlgItem(IDC_BUTTON_MODIFY)
    };
 
    for (auto pBtn : buttons) {
        if (pBtn && pBtn->GetSafeHwnd()) {
            pBtn->MoveWindow(rect.right - buttonWidth - margin, y, buttonWidth, buttonHeight);
            y += buttonHeight + buttonSpacing;
        }
    }
}
 
void CPageRecipe::OnBnClickedButtonSearch()
{
    CString strKeyword;
    GetDlgItemText(IDC_EDIT_KEYWORD, strKeyword);
    AfxMessageBox(strKeyword);
}
 
void CPageRecipe::OnBnClickedButtonModify()
{
    // TODO: 在此添加控件通知处理程序代码
    /*
    POSITION pos = m_listPPID.GetFirstSelectedItemPosition();
    if (!pos) {
        AfxMessageBox(_T("请选择要修改的配方"));
        return;
    }
 
    int nSel = m_listPPID.GetNextSelectedItem(pos);
    CString strOldPPID = m_listPPID.GetItemText(nSel, 2);
    CString strOldDesc = m_listPPID.GetItemText(nSel, 3);
 
    CString strNewPPID, strNewDesc;
    m_editPPID.GetWindowText(strNewPPID);
    m_editDesc.GetWindowText(strNewDesc);
 
    // 判空
    if (strOldPPID.IsEmpty() || strNewPPID.IsEmpty()) {
        AfxMessageBox(_T("PPID 不能为空"));
        return;
    }
 
    std::string oldPPID = CT2A(strOldPPID);
    std::string newPPID = CT2A(strNewPPID);
    std::string newDesc = CT2A(strNewDesc);
 
    bool bPPIDChanged = (strOldPPID.Compare(strNewPPID) != 0);
    bool bDescChanged = (strOldDesc.Compare(strNewDesc) != 0);
 
    if (!bPPIDChanged && !bDescChanged) {
        return;
    }
 
    if (bPPIDChanged) {
        // 新 PPID 不可重复
        if (RecipeManager::getInstance().ppidExists(newPPID)) {
            AfxMessageBox(_T("新 PPID 已存在,请使用其他值"));
            return;
        }
 
        // 调用 updatePPID,同时更新描述
        if (RecipeManager::getInstance().updatePPID(oldPPID, newPPID)) {
            m_listPPID.SetItemText(nSel, 2, strNewPPID);
        }
        else {
            AfxMessageBox(_T("更新失败,请检查日志"));
        }
    }
 
    if (bDescChanged) {
        // 更新描述
        if (RecipeManager::getInstance().updateDescription(oldPPID, newDesc)) {
            m_listPPID.SetItemText(nSel, 3, strNewDesc);
        }
        else {
            AfxMessageBox(_T("描述更新失败"));
        }
    }
    */
}
 
void CPageRecipe::OnBnClickedButtonDelete()
{
    // TODO: 在此添加控件通知处理程序代码
    POSITION pos = m_listPPID.GetFirstSelectedItemPosition();
    if (!pos) { 
        return;
    }
 
    int nSel = m_listPPID.GetNextSelectedItem(pos);
    CString strPPID = m_listPPID.GetItemText(nSel, 2);
    std::string ppid = CT2A(strPPID);
 
    if (!ppid.empty()) {
        CString msg;
        msg.Format(_T("确定要删除配方 [%s] 吗?"), strPPID);
        if (IDYES == AfxMessageBox(msg, MB_YESNO | MB_ICONQUESTION)) {
            if (RecipeManager::getInstance().deleteRecipeByPPID(ppid)) {
                m_listPPID.DeleteItem(nSel);
            }
            else {
                AfxMessageBox(_T("删除失败"));
            }
        }
    }
}
 
void CPageRecipe::OnBnClickedButtonDeleteAll()
{
    // TODO: 在此添加控件通知处理程序代码
    if (IDYES != AfxMessageBox(_T("确定要删除全部配方记录吗?"), MB_YESNO | MB_ICONWARNING)) {
        return;
    }
 
    // 获取所有 PPID
    int nCount = m_listPPID.GetItemCount();
    for (int i = 0; i < nCount; ++i) {
        CString strPPID = m_listPPID.GetItemText(i, 2);
        std::string ppid = CT2A(strPPID);
        if (!ppid.empty()) {
            RecipeManager::getInstance().deleteRecipeByPPID(ppid);
        }
    }
 
    // 清空列表显示
    m_listPPID.DeleteAllItems();
}
 
void CPageRecipe::OnBnClickedButtonRefresh()
{
    CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO_EQUIPMENT);
    int nSel = pComboBox->GetCurSel();
    SERVO::CEquipment* pEq = (SERVO::CEquipment*)pComboBox->GetItemDataPtr(nSel);
 
    if (pEq == nullptr) {
        // 获取master配方(本地)
        auto vecData = RecipeManager::getInstance().getAllRecipes();
        FillDataToListCtrl(vecData);
    }
    else {
        // enable port
        CMsgDlg msgDlg("请等待", "正在获取配方...");
        pEq->masterRecipeListRequest(0, [&](int status) -> void {
            if (status == SS_FAILED) {
                CString strMsg;
                strMsg.Format(_T("获取配方失败!"));
                msgDlg.DelayClose(3000);
                msgDlg.SetIcon(MSG_BOX_ERROR);
                msgDlg.SetTitle(_T("操作失败"));
                msgDlg.SetMessage((LPTSTR)(LPCTSTR)strMsg);
                msgDlg.SetMarquee(FALSE, 0);
                msgDlg.SetCompleteCode(-1);
            }
            else if (status == SS_COMPLETE) {
                CString strMsg;
                strMsg.Format(_T("获取配方完成!"));
                msgDlg.DelayClose(3000);
                msgDlg.SetIcon(MSG_BOX_SUCCEED);
                msgDlg.SetTitle(_T("操作成功"));
                msgDlg.SetMessage((LPTSTR)(LPCTSTR)strMsg);
                msgDlg.SetMarquee(FALSE, 0);
                msgDlg.SetCompleteCode(0);
            }
            });
        msgDlg.DoModal();
    }
}
 
void CPageRecipe::OnLvnItemChangedListPPID(NMHDR* pNMHDR, LRESULT* pResult)
{
    LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
    *pResult = 0;
 
    CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO_EQUIPMENT);
    int nEqSel = pComboBox->GetCurSel();
    int selectedCount = ListView_GetSelectedCount(m_listPPID.GetSafeHwnd());
 
    GetDlgItem(IDC_BUTTON_MODIFY)->EnableWindow(nEqSel == 0 && selectedCount > 0);
    GetDlgItem(IDC_BUTTON_DELETE)->EnableWindow(nEqSel == 0 && selectedCount > 0);
    GetDlgItem(IDC_BUTTON_DELETE_ALL)->EnableWindow(nEqSel == 0 && selectedCount > 0);
}
 
void CPageRecipe::OnCbnSelchangeComboEquipment()
{
    CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO_EQUIPMENT);
    int nEqSel = pComboBox->GetCurSel();
    int selectedCount = ListView_GetSelectedCount(m_listPPID.GetSafeHwnd());
 
    GetDlgItem(IDC_BUTTON_MODIFY)->EnableWindow(nEqSel == 0 && selectedCount > 0);
    GetDlgItem(IDC_BUTTON_DELETE)->EnableWindow(nEqSel == 0 && selectedCount > 0);
    GetDlgItem(IDC_BUTTON_DELETE_ALL)->EnableWindow(nEqSel == 0 && selectedCount > 0);
    GetDlgItem(IDC_EDIT_KEYWORD)->EnableWindow(nEqSel == 0);
    GetDlgItem(IDC_BUTTON_SEARCH)->EnableWindow(nEqSel == 0);
 
    SERVO::CEquipment* pEq = (SERVO::CEquipment*)pComboBox->GetItemDataPtr(nEqSel);
    if (pEq == nullptr) {
        auto vecData = RecipeManager::getInstance().getAllRecipes();
        FillDataToListCtrl(vecData);
    }
    else {
        SERVO::CRecipeList* pRecipeList = pEq->getRecipeList(0);
        FillRecipeListToListCtrl(pRecipeList);
    }
    
 
 
}
 
 
void CPageRecipe::OnDestroy()
{
    CDialogEx::OnDestroy();
 
    // 保存列宽
    CString strIniFile, strItem, strTemp;
    strIniFile.Format(_T("%s\\configuration.ini"), (LPTSTR)(LPCTSTR)theApp.m_strAppDir);
    CHeaderCtrl* pHeader = m_listPPID.GetHeaderCtrl();
    for (int i = 0; i < pHeader->GetItemCount(); i++) {
        RECT rect;
        pHeader->GetItemRect(i, &rect);
        strItem.Format(_T("Col_%d_Width"), i);
        strTemp.Format(_T("%d"), rect.right - rect.left);
        WritePrivateProfileString("PageRecipeListCtrl", strItem, strTemp, strIniFile);
    }
}
 
 
void CPageRecipe::OnShowWindow(BOOL bShow, UINT nStatus)
{
    CDialogEx::OnShowWindow(bShow, nStatus);
 
    if (bShow) {
        CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO_EQUIPMENT);
        if (pComboBox->GetCount() == 0) {
            SERVO::CMaster& master = theApp.m_model.getMaster();
            SERVO::CEquipment* pEq[] = {
                nullptr,
                master.getEquipment(EQ_ID_EFEM),
                master.getEquipment(EQ_ID_Bonder1),
                master.getEquipment(EQ_ID_Bonder2),
                master.getEquipment(EQ_ID_BAKE_COOLING),
                master.getEquipment(EQ_ID_VACUUMBAKE),
                master.getEquipment(EQ_ID_MEASUREMENT),
            };
 
            CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO_EQUIPMENT);
            for (int i = 0; i < sizeof(pEq) / sizeof(pEq[0]); i++) {
                pComboBox->InsertString(i,
                    pEq[i] == nullptr ? _T("Master") : pEq[i]->getName().c_str());
                pComboBox->SetItemDataPtr(i, pEq[i]);
            }
            pComboBox->SetCurSel(0);
        }
    }
}