From 0d2da4a2507a5a8d34bf7f736727917d791df0d2 Mon Sep 17 00:00:00 2001
From: mrDarker <mr.darker@163.com>
Date: 星期三, 14 五月 2025 14:48:31 +0800
Subject: [PATCH] 1. 添加数据库事务提交,减少频繁执行 SQL 写操作

---
 SourceCode/Bond/Servo/SECSRuntimeManager.cpp |   22 ++++++++++++++++++++++
 SourceCode/Bond/Servo/PageRecipe.cpp         |   35 ++++++++++++++++++-----------------
 SourceCode/Bond/Servo/SECSRuntimeManager.h   |    2 ++
 3 files changed, 42 insertions(+), 17 deletions(-)

diff --git a/SourceCode/Bond/Servo/PageRecipe.cpp b/SourceCode/Bond/Servo/PageRecipe.cpp
index a741eea..85f369a 100644
--- a/SourceCode/Bond/Servo/PageRecipe.cpp
+++ b/SourceCode/Bond/Servo/PageRecipe.cpp
@@ -32,15 +32,21 @@
 	pListCtrl->DeleteAllItems();
 
 	// 閬嶅巻鏁版嵁骞舵彃鍏ュ埌CListCtrl涓�
-	int nIndex = 0;
-	for (const auto& row : vecData) {
-		int nNewItem = pListCtrl->InsertItem(nIndex, _T(""));
+	for (int i = 0; i < static_cast<int>(vecData.size()); ++i) {
+		// 鎻掑叆琛�
+		pListCtrl->InsertItem(i, _T(""));
 
-		CString str;
-		str.Format(_T("%d"), nIndex++);
-		pListCtrl->SetItemText(nNewItem, 1, str); // Recipe No
-		str.Format(_T("%s"), row.c_str());
-		pListCtrl->SetItemText(nNewItem, 2, str); // PPID
+		// 璁剧疆 Recipe No锛堢1鍒楋級
+		CString strRecipeNo;
+		strRecipeNo.Format(_T("%d"), i);
+		pListCtrl->SetItemText(i, 1, strRecipeNo);
+
+		// 璁剧疆 PPID锛堢2鍒楋級
+		CString strPPID = CA2T(vecData[i].c_str());
+		if (strPPID.CompareNoCase(_T("NULL")) == 0) {
+			strPPID.Empty();
+		}
+		pListCtrl->SetItemText(i, 2, strPPID);
 	}
 
 	// 鑾峰彇鍒楁暟
@@ -147,18 +153,13 @@
 void CPageRecipe::OnBnClickedButtonSave()
 {
 	// TODO: 鍦ㄦ娣诲姞鎺т欢閫氱煡澶勭悊绋嬪簭浠g爜
+	std::vector<std::string> vecPPID;
 	int nCount = m_listPPID.GetItemCount();
 	for (int i = 0; i < nCount; ++i) {
-		std::string strPPID = CT2A(m_listPPID.GetItemText(i, 2));
-		SECSRuntimeManager::getInstance().updatePPIDForRecipe(i, strPPID);
+		CString str = m_listPPID.GetItemText(i, 2);
+		vecPPID.emplace_back(CT2A(str));
 	}
-
-	//if (!SECSRuntimeManager::getInstance().saveAllPPID(vecData)) {
-	//	AfxMessageBox(_T("淇濆瓨澶辫触"));
-	//}
-	//else {
-	//	AfxMessageBox(_T("淇濆瓨鎴愬姛"));
-	//}
+	SECSRuntimeManager::getInstance().setAllPPID(vecPPID);
 }
 
 void CPageRecipe::OnBnClickedButtonRefresh()
diff --git a/SourceCode/Bond/Servo/SECSRuntimeManager.cpp b/SourceCode/Bond/Servo/SECSRuntimeManager.cpp
index cd4d27b..ccbcf77 100644
--- a/SourceCode/Bond/Servo/SECSRuntimeManager.cpp
+++ b/SourceCode/Bond/Servo/SECSRuntimeManager.cpp
@@ -1436,6 +1436,28 @@
 	return vecResult;
 }
 
+void SECSRuntimeManager::setAllPPID(const std::vector<std::string>& vecPPIDList) {
+    std::lock_guard<std::mutex> lock(m_mutex);
+    if (m_pDB == nullptr) return;
+
+    // 开启事务
+    m_pDB->executeQuery("BEGIN TRANSACTION;");
+
+    for (size_t i = 0; i < vecPPIDList.size(); ++i) {
+        std::string safePPID = vecPPIDList[i];
+        size_t pos = 0;
+        while ((pos = safePPID.find('\'', pos)) != std::string::npos) {
+            safePPID.insert(pos, 1, '\'');
+            pos += 2;
+        }
+        std::string sql = "UPDATE EqpPPID SET PPID = '" + safePPID + "' WHERE RecipeNo = " + std::to_string(i) + ";";
+        m_pDB->executeQuery(sql);
+    }
+
+    // 提交事务
+    m_pDB->executeQuery("COMMIT;");
+}
+
 bool SECSRuntimeManager::updatePPIDForRecipe(int nRecipeNo, const std::string& strPPID) {
     std::lock_guard<std::mutex> lock(m_mutex);
     if (m_pDB == nullptr) {
diff --git a/SourceCode/Bond/Servo/SECSRuntimeManager.h b/SourceCode/Bond/Servo/SECSRuntimeManager.h
index af9b16c..a3bc39d 100644
--- a/SourceCode/Bond/Servo/SECSRuntimeManager.h
+++ b/SourceCode/Bond/Servo/SECSRuntimeManager.h
@@ -391,6 +391,8 @@
 
 	std::vector<std::string> getAllPPID();
 
+    void setAllPPID(const std::vector<std::string>& vecPPIDList);
+
     bool updatePPIDForRecipe(int nRecipeNo, const std::string& strPPID);
 
     std::string getPPIDForRecipe(int nRecipeNo);

--
Gitblit v1.9.3