From 444ad650bc8d00e38a85a6e671278adf658fe08b Mon Sep 17 00:00:00 2001
From: mrDarker <mr.darker@163.com>
Date: 星期二, 13 五月 2025 16:44:54 +0800
Subject: [PATCH] 1. 删除之前的报警和日志按钮 2. 完善PPID和RecipeNo的数据库操作

---
 SourceCode/Bond/Servo/Servo.cpp              |   36 ++++++------
 SourceCode/Bond/Servo/Servo.rc               |    0 
 SourceCode/Bond/Servo/SECSRuntimeManager.cpp |   74 +++++++++++++++++++++++-
 SourceCode/Bond/Servo/ServoDlg.cpp           |    7 --
 SourceCode/Bond/Servo/SECSRuntimeManager.h   |   10 +++
 SourceCode/Bond/Servo/ServoDlg.h             |    2 
 6 files changed, 100 insertions(+), 29 deletions(-)

diff --git a/SourceCode/Bond/Servo/SECSRuntimeManager.cpp b/SourceCode/Bond/Servo/SECSRuntimeManager.cpp
index 3f8d08a..708dcf1 100644
--- a/SourceCode/Bond/Servo/SECSRuntimeManager.cpp
+++ b/SourceCode/Bond/Servo/SECSRuntimeManager.cpp
@@ -1401,8 +1401,8 @@
     // 创建 EqpPPID 表
     std::string createTableSQL =
         "CREATE TABLE IF NOT EXISTS EqpPPID ("
-        "BitNo INTEGER PRIMARY KEY AUTOINCREMENT, "
-        "PPID INTEGER NULL);";
+        "RecipeNo INTEGER PRIMARY KEY, "
+        "PPID TEXT NULL);";
     if (!m_pDB->executeQuery(createTableSQL)) {
         throw std::runtime_error("Failed to create EqpPPID table.");
     }
@@ -1412,7 +1412,7 @@
     if (nCount == 0) {
         // 插入初始数据(512 行)
         for (int nBitNo = 0; nBitNo < 512; ++nBitNo) {
-            std::string insertSQL = "INSERT INTO EqpPPID (BitNo) VALUES (" + std::to_string(nBitNo) + ");";
+            std::string insertSQL = "INSERT INTO EqpPPID (RecipeNo) VALUES (" + std::to_string(nBitNo) + ");";
             if (!m_pDB->executeQuery(insertSQL)) {
                 throw std::runtime_error("Failed to insert data into EqpPPID table.");
             }
@@ -1420,6 +1420,74 @@
     }
 }
 
+bool SECSRuntimeManager::updatePPIDForRecipe(int nRecipeNo, const std::string& strPPID) {
+    std::lock_guard<std::mutex> lock(m_mutex);
+    if (m_pDB == nullptr) {
+        return false;
+    }
+
+    // 转义单引号,防止 SQL 注入
+    std::string safePPID = strPPID;
+    size_t pos = 0;
+    while ((pos = safePPID.find('\'', pos)) != std::string::npos) {
+        safePPID.insert(pos, 1, '\'');
+        pos += 2;
+    }
+
+    std::string updateSQL = "UPDATE EqpPPID SET PPID = '" + safePPID + "' WHERE RecipeNo = " + std::to_string(nRecipeNo) + ";";
+    return m_pDB->executeQuery(updateSQL);
+}
+
+std::string SECSRuntimeManager::getPPIDForRecipe(int nRecipeNo) {
+	std::lock_guard<std::mutex> lock(m_mutex);
+	if (m_pDB == nullptr) {
+		return "";
+	}
+
+	std::string querySQL = "SELECT PPID FROM EqpPPID WHERE RecipeNo = " + std::to_string(nRecipeNo) + ";";
+	std::vector<std::vector<std::string>> results = m_pDB->fetchResults(querySQL);
+	if (!results.empty() && !results[0].empty()) {
+		return results[0][0];
+	}
+
+	return "";
+}
+
+int SECSRuntimeManager::getRecipeForPPID(std::string strPPID) {
+	std::lock_guard<std::mutex> lock(m_mutex);
+	if (m_pDB == nullptr) {
+		return -1;
+	}
+
+	std::string querySQL = "SELECT RecipeNo FROM EqpPPID WHERE PPID = '" + strPPID + "';";
+	std::vector<std::vector<std::string>> results = m_pDB->fetchResults(querySQL);
+	if (!results.empty() && !results[0].empty()) {
+		return std::stoi(results[0][0]);
+	}
+
+	return -1;
+}
+
+bool SECSRuntimeManager::deletePPIDForRecipe(int nRecipeNo) {
+	std::lock_guard<std::mutex> lock(m_mutex);
+	if (m_pDB == nullptr) {
+		return false;
+	}
+
+	std::string deleteSQL = "UPDATE EqpPPID SET PPID = NULL WHERE RecipeNo = " + std::to_string(nRecipeNo) + ";";
+	return m_pDB->executeQuery(deleteSQL);
+}
+
+bool SECSRuntimeManager::deletePPIDForAllRecipes() {
+	std::lock_guard<std::mutex> lock(m_mutex);
+	if (m_pDB == nullptr) {
+		return false;
+	}
+
+	std::string deleteSQL = "UPDATE EqpPPID SET PPID = NULL WHERE RecipeNo BETWEEN 0 AND 511;";
+	return m_pDB->executeQuery(deleteSQL);
+}
+
 // 初始化 RPTID 表
 void SECSRuntimeManager::initRPTIDTable() {
     std::lock_guard<std::mutex> lock(m_mutex);
diff --git a/SourceCode/Bond/Servo/SECSRuntimeManager.h b/SourceCode/Bond/Servo/SECSRuntimeManager.h
index 44473fe..67a194e 100644
--- a/SourceCode/Bond/Servo/SECSRuntimeManager.h
+++ b/SourceCode/Bond/Servo/SECSRuntimeManager.h
@@ -389,6 +389,16 @@
 	 */
     void initPPIDTable();
 
+    bool updatePPIDForRecipe(int nRecipeNo, const std::string& strPPID);
+
+    std::string getPPIDForRecipe(int nRecipeNo);
+
+    int getRecipeForPPID(std::string strPPID);
+
+    bool deletePPIDForRecipe(int nRecipeNo);
+
+    bool deletePPIDForAllRecipes();
+
 	/**
 	* 鍒濆鍖� RPTID 琛�
     */ 
diff --git a/SourceCode/Bond/Servo/Servo.cpp b/SourceCode/Bond/Servo/Servo.cpp
index 5866c64..e6eb33a 100644
--- a/SourceCode/Bond/Servo/Servo.cpp
+++ b/SourceCode/Bond/Servo/Servo.cpp
@@ -130,35 +130,35 @@
 
 
 	// 初始化生产履历管理器
-	try {
-		if (!ProductionLogManager::getInstance().initProductionTable()) {
-			AfxMessageBox("初始化生产履历管理器失败!");
-			return FALSE;
-		}
-	}
-	catch (const std::exception& ex) {
-		CString errorMsg;
-		errorMsg.Format(_T("初始化生产履历管理器失败:%s"), CString(ex.what()));
-		AfxMessageBox(errorMsg, MB_ICONERROR);
-		return FALSE;
-	}
-
-
-	// 初始化SECS运行设置管理库
 	//try {
-	//	if (!SECSRuntimeManager::getInstance().initRuntimeSetting()) {
-	//		AfxMessageBox("初始化SECS运行设置失败!");
+	//	if (!ProductionLogManager::getInstance().initProductionTable()) {
+	//		AfxMessageBox("初始化生产履历管理器失败!");
 	//		return FALSE;
 	//	}
 	//}
 	//catch (const std::exception& ex) {
 	//	CString errorMsg;
-	//	errorMsg.Format(_T("初始化SECS运行设置失败:%s"), CString(ex.what()));
+	//	errorMsg.Format(_T("初始化生产履历管理器失败:%s"), CString(ex.what()));
 	//	AfxMessageBox(errorMsg, MB_ICONERROR);
 	//	return FALSE;
 	//}
 
 
+	// 初始化SECS运行设置管理库
+	try {
+		if (!SECSRuntimeManager::getInstance().initRuntimeSetting()) {
+			AfxMessageBox("初始化SECS运行设置失败!");
+			return FALSE;
+		}
+	}
+	catch (const std::exception& ex) {
+		CString errorMsg;
+		errorMsg.Format(_T("初始化SECS运行设置失败:%s"), CString(ex.what()));
+		AfxMessageBox(errorMsg, MB_ICONERROR);
+		return FALSE;
+	}
+
+
 	CServoDlg dlg;
 	m_pMainWnd = &dlg;
 	INT_PTR nResponse = dlg.DoModal();
diff --git a/SourceCode/Bond/Servo/Servo.rc b/SourceCode/Bond/Servo/Servo.rc
index 0233b72..b9250e7 100644
--- a/SourceCode/Bond/Servo/Servo.rc
+++ b/SourceCode/Bond/Servo/Servo.rc
Binary files differ
diff --git a/SourceCode/Bond/Servo/ServoDlg.cpp b/SourceCode/Bond/Servo/ServoDlg.cpp
index 3bf1852..eeba9a2 100644
--- a/SourceCode/Bond/Servo/ServoDlg.cpp
+++ b/SourceCode/Bond/Servo/ServoDlg.cpp
@@ -60,7 +60,6 @@
 // CServoDlg 对话框
 
 
-
 CServoDlg::CServoDlg(CWnd* pParent /*=NULL*/)
 	: CDialogEx(IDD_SERVO_DIALOG, pParent)
 {
@@ -81,8 +80,6 @@
 void CServoDlg::DoDataExchange(CDataExchange* pDX)
 {
 	CDialogEx::DoDataExchange(pDX);
-	DDX_Control(pDX, IDC_BUTTON_LOG, m_btnLog);
-	DDX_Control(pDX, IDC_BUTTON_ALARM, m_btnAlarm);
 }
 
 BEGIN_MESSAGE_MAP(CServoDlg, CDialogEx)
@@ -666,6 +663,4 @@
 	for (int i = 0; i < 4; i++) {
 		pPages[i]->ShowWindow(i == index ? SW_SHOW : SW_HIDE);
 	}
-}
-
-
+}
\ No newline at end of file
diff --git a/SourceCode/Bond/Servo/ServoDlg.h b/SourceCode/Bond/Servo/ServoDlg.h
index 9ff5766..272028e 100644
--- a/SourceCode/Bond/Servo/ServoDlg.h
+++ b/SourceCode/Bond/Servo/ServoDlg.h
@@ -54,8 +54,6 @@
 	HICON m_hIcon;
 	COLORREF m_crBkgnd;
 	HBRUSH m_hbrBkgnd;
-	CBlButton m_btnLog;
-	CBlButton m_btnAlarm;
 	CPanelMaster* m_pPanelMaster;
 	CPanelEquipment* m_pPanelEquipment;
 	CPanelAttributes* m_pPanelAttributes;

--
Gitblit v1.9.3