mrDarker
2025-07-21 f6b143d9a6f62875e62dae61a0af061139d7a156
SourceCode/Bond/Servo/PageRecipe.cpp
@@ -6,7 +6,7 @@
#include "afxdialogex.h"
#include "PageRecipe.h"
#include "MsgDlg.h"
#include "RecipeDeviceBindDlg.h"
// CPageRecipe 对话框
@@ -20,6 +20,60 @@
CPageRecipe::~CPageRecipe()
{
}
void CPageRecipe::UpdateRecipeByPPID(const CString& strPPID)
{
   if (strPPID.IsEmpty()) {
      AfxMessageBox(_T("请选择一个配方!"));
      return;
   }
   auto& mgr = RecipeManager::getInstance();
   // 查询选中配方的详细数据
   std::string oldPPID = CT2A(strPPID);
   RecipeInfo oldRecipe = mgr.getRecipeByPPID(oldPPID);
   if (oldRecipe.strPPID.empty()) {
      AfxMessageBox(_T("获取配方数据失败!"));
      return;
   }
   // 弹出编辑对话框,并初始化为当前内容
   CRecipeDeviceBindDlg dlg(this);
   dlg.SetRecipeInfo(oldRecipe);
   if (dlg.DoModal() == IDOK) {
      const RecipeInfo& newRecipe = dlg.GetRecipeInfo();
      bool success = false;
      // 判断PPID是否有改动
      if (oldRecipe.strPPID != newRecipe.strPPID) {
         // 先更新PPID,再整体更新内容
         if (mgr.updatePPID(oldRecipe.strPPID, newRecipe.strPPID)) {
            success = mgr.updateRecipe(newRecipe);
            if (!success) {
               AfxMessageBox(_T("已更改PPID,但更新配方内容失败,请检查日志"));
            }
         }
         else {
            AfxMessageBox(_T("更新PPID失败,请检查日志"));
            return;
         }
      }
      else {
         // 只更新内容
         success = mgr.updateRecipe(newRecipe);
         if (!success) {
            AfxMessageBox(_T("更新配方失败,请检查日志"));
         }
      }
      if (success) {
         auto vecData = mgr.getAllRecipes();
         FillDataToListCtrl(vecData);
      }
   }
}
void CPageRecipe::FillDataToListCtrl(const std::vector<RecipeInfo>& vecRecipe) {
@@ -64,7 +118,6 @@
      return;
   }
   // 遍历数据并插入到CListCtrl中
   std::map<int, short>& ids = pList->getIds();
   for (auto item : ids) {
@@ -87,15 +140,16 @@
BEGIN_MESSAGE_MAP(CPageRecipe, CDialogEx)
   ON_WM_SIZE()
   ON_WM_DESTROY()
   ON_WM_SHOWWINDOW()
   ON_BN_CLICKED(IDC_BUTTON_NEW, &CPageRecipe::OnBnClickedButtonNew)
   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()
@@ -172,9 +226,10 @@
   // 按钮竖直排列在右侧
   CWnd* buttons[] = {
      GetDlgItem(IDC_BUTTON_REFRESH),
      GetDlgItem(IDC_BUTTON_NEW),
      GetDlgItem(IDC_BUTTON_MODIFY),
      GetDlgItem(IDC_BUTTON_DELETE),
      GetDlgItem(IDC_BUTTON_DELETE_ALL),
      GetDlgItem(IDC_BUTTON_MODIFY)
      GetDlgItem(IDC_BUTTON_DELETE_ALL)
   };
   for (auto pBtn : buttons) {
@@ -185,74 +240,130 @@
   }
}
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);
      }
   }
}
void CPageRecipe::OnBnClickedButtonNew()
{
   // TODO: 在此添加控件通知处理程序代码
   //CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO_EQUIPMENT);
   //int nSel = pComboBox->GetCurSel();
   //SERVO::CEquipment* pEq = (SERVO::CEquipment*)pComboBox->GetItemDataPtr(nSel);
   //if (pEq == nullptr) {
   //   return;
   //}
   CRecipeDeviceBindDlg dlg(this);
   if (dlg.DoModal() == IDOK) {
      const RecipeInfo& newRecipe = dlg.GetRecipeInfo();
      auto& mgr = RecipeManager::getInstance();
      if (mgr.ppidExists(newRecipe.strPPID)) {
         // 已存在,询问是否覆盖
         int ret = AfxMessageBox(_T("该 PPID 已存在,是否覆盖原配方?"), MB_YESNO | MB_ICONQUESTION);
         if (ret == IDYES) {
            if (mgr.updateRecipe(newRecipe)) {
               auto vecData = mgr.getAllRecipes();
               FillDataToListCtrl(vecData);
            }
            else {
               AfxMessageBox(_T("更新配方失败,请检查日志"));
            }
         }
      }
      else {
         // 不存在,直接新增
         if (mgr.addRecipe(newRecipe)) {
            auto vecData = mgr.getAllRecipes();
            FillDataToListCtrl(vecData);
         }
         else {
            AfxMessageBox(_T("添加配方失败,请检查日志"));
         }
      }
   }
}
void CPageRecipe::OnBnClickedButtonSearch()
{
   CString strKeyword;
   GetDlgItemText(IDC_EDIT_KEYWORD, strKeyword);
   AfxMessageBox(strKeyword);
   strKeyword.Trim();
   std::vector<RecipeInfo> vecData;
   if (strKeyword.IsEmpty()) {
      // 关键词为空,显示全部配方
      vecData = RecipeManager::getInstance().getAllRecipes();
   }
   else {
      // 根据关键词搜索配方
      vecData = RecipeManager::getInstance().getRecipesByKeyword(std::string(CT2A(strKeyword)));
   }
   // 如果没有数据,弹出提示
   if (vecData.empty()) {
      AfxMessageBox(_T("未找到匹配的配方!"));
      return;
   }
   FillDataToListCtrl(vecData);
}
void CPageRecipe::OnBnClickedButtonModify()
{
   // TODO: 在此添加控件通知处理程序代码
   /*
   POSITION pos = m_listPPID.GetFirstSelectedItemPosition();
   if (!pos) {
      AfxMessageBox(_T("请选择要修改的配方"));
      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("描述更新失败"));
      }
   }
   */
   CString strPPID = m_listPPID.GetItemText(nSel, 2);
   UpdateRecipeByPPID(strPPID);
}
void CPageRecipe::OnBnClickedButtonDelete()
@@ -376,56 +487,5 @@
   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);
      }
   }
}