From 5e9b9b53a8a853365c29149871bd024c9ca0cbac Mon Sep 17 00:00:00 2001
From: chenluhua1980 <Chenluhua@qq.com>
Date: 星期四, 11 十二月 2025 11:26:27 +0800
Subject: [PATCH] 1.报告的删除功能;

---
 SourceCode/Bond/Servo/HsmsPassive.h     |    5 ++
 SourceCode/Bond/Servo/CUserManager2.cpp |    1 
 SourceCode/Bond/Servo/HsmsPassive.cpp   |   67 +++++++++++++++++++++++++++++++++
 SourceCode/Bond/Servo/CPageReport.h     |    1 
 SourceCode/Bond/Servo/CReport.h         |    1 
 SourceCode/Bond/Servo/CPageReport.cpp   |   30 +++++++++++++++
 6 files changed, 105 insertions(+), 0 deletions(-)

diff --git a/SourceCode/Bond/Servo/CPageReport.cpp b/SourceCode/Bond/Servo/CPageReport.cpp
index ca178ef..b537d06 100644
--- a/SourceCode/Bond/Servo/CPageReport.cpp
+++ b/SourceCode/Bond/Servo/CPageReport.cpp
@@ -148,3 +148,33 @@
 
 	*pResult = 0;
 }
+
+void CPageReport::OnClickedBtn(const char* btnName)
+{
+	ASSERT(btnName);
+	if (_strcmpi(btnName, "鍒犻櫎") == 0) {
+		POSITION pos = m_listCtrl.GetFirstSelectedItemPosition();
+		if (pos == nullptr) return;
+		int nItem = m_listCtrl.GetNextSelectedItem(pos);
+		auto pRpt = reinterpret_cast<SERVO::CReport*>(m_listCtrl.GetItemData(nItem));
+		if (pRpt == nullptr) return;
+
+		int rc = UX_CanExecute(L"delReports");
+		if (rc != 1) {
+			AfxMessageBox("鎿嶄綔鏉冮檺涓嶈冻锛岃鑱旂郴绠$悊浜哄憳锛�");
+			return;
+		}
+
+		int ret = theApp.m_model.m_hsmsPassive.deleteReport((int)pRpt->getReportId());
+		if (ret == 0) {
+			UX_RecordAction(L"delReports");
+			m_listCtrl.DeleteAllItems();
+			loadReports();
+			if (CButton* pDel = GetBtnByName("鍒犻櫎")) pDel->EnableWindow(FALSE);
+			if (CButton* pEdit = GetBtnByName("缂栬緫")) pEdit->EnableWindow(FALSE);
+		}
+		else {
+			AfxMessageBox(_T("鍒犻櫎鎶ュ憡澶辫触"));
+		}
+	}
+}
diff --git a/SourceCode/Bond/Servo/CPageReport.h b/SourceCode/Bond/Servo/CPageReport.h
index 6cea3e4..bec5588 100644
--- a/SourceCode/Bond/Servo/CPageReport.h
+++ b/SourceCode/Bond/Servo/CPageReport.h
@@ -17,6 +17,7 @@
 private:
 	CListCtrlEx m_listCtrl;
 	void OnCreateBtns() override;
+	void OnClickedBtn(const char* btnName) override;
 
 // 瀵硅瘽妗嗘暟鎹�
 #ifdef AFX_DESIGN_TIME
diff --git a/SourceCode/Bond/Servo/CReport.h b/SourceCode/Bond/Servo/CReport.h
index 191d539..8597a1e 100644
--- a/SourceCode/Bond/Servo/CReport.h
+++ b/SourceCode/Bond/Servo/CReport.h
@@ -18,6 +18,7 @@
 		std::vector<CVariable*>& getVariables();
 		std::string getVariablesIdsText();
 		bool getVariableName(unsigned int vid, std::string& strName);
+		const std::vector<unsigned int>& getVids() const { return m_vids; }
 
 	private:
 		unsigned int m_nReportId;
diff --git a/SourceCode/Bond/Servo/CUserManager2.cpp b/SourceCode/Bond/Servo/CUserManager2.cpp
index 251b65e..334333c 100644
--- a/SourceCode/Bond/Servo/CUserManager2.cpp
+++ b/SourceCode/Bond/Servo/CUserManager2.cpp
@@ -78,6 +78,7 @@
 		UX_DefineAction(L"addVarialbles", L"鏂板鍙橀噺", L"PE");
 		UX_DefineAction(L"editVarialbles", L"缂栬緫鍙橀噺", L"PE");
 	}
+	UX_DefineAction(L"delReports", L"鍒犻櫎Report", L"PE");
 }
 
 bool CUserManager2::login(const char* pszAccount, const char* pszPwd)
diff --git a/SourceCode/Bond/Servo/HsmsPassive.cpp b/SourceCode/Bond/Servo/HsmsPassive.cpp
index d6f97cb..722eacd 100644
--- a/SourceCode/Bond/Servo/HsmsPassive.cpp
+++ b/SourceCode/Bond/Servo/HsmsPassive.cpp
@@ -600,6 +600,9 @@
 
 int CHsmsPassive::loadReports(const char* pszFilepath)
 {
+	m_strReportFilepath = pszFilepath;
+	m_bReportUtf8 = false;
+	m_bReportUtf8Bom = false;
 	// 鍏煎 UTF-8/BOM 涓庢湰鍦扮紪鐮佽鍙�
 	CFile file;
 	if (!file.Open(pszFilepath, CFile::modeRead | CFile::shareDenyNone)) {
@@ -623,6 +626,8 @@
 	// UTF-8 BOM
 	if (nLen >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) {
 		offset = 3;
+		m_bReportUtf8 = true;
+		m_bReportUtf8Bom = true;
 	}
 
 	// UTF-16 LE BOM
@@ -652,6 +657,7 @@
 			MultiByteToWideChar(CP_UTF8, 0, buffer.data() + off,
 				static_cast<int>(buffer.size() - off), temp.data(), need);
 			content = temp.c_str();
+			m_bReportUtf8 = true;
 			return true;
 		};
 
@@ -752,6 +758,14 @@
 	return false;
 }
 
+int CHsmsPassive::deleteReport(int rptid)
+{
+	if (!removeReport(rptid)) {
+		return -1;
+	}
+	return writeReportsToFile(m_strReportFilepath);
+}
+
 void CHsmsPassive::clearAllReport()
 {
 	for (auto item : m_reports) {
@@ -760,6 +774,59 @@
 	m_reports.clear();
 }
 
+int CHsmsPassive::writeReportsToFile(const std::string& filepath)
+{
+	if (filepath.empty()) return -1;
+
+	CFile file;
+	if (!file.Open(filepath.c_str(), CFile::modeCreate | CFile::modeWrite)) {
+		return -1;
+	}
+
+	if (m_bReportUtf8 && m_bReportUtf8Bom) {
+		const BYTE bom[3] = { 0xEF, 0xBB, 0xBF };
+		file.Write(bom, 3);
+	}
+
+	// header
+	const std::string headerAnsi = "RPTID,(VID1,VID2,...)\r\n";	
+	if (m_bReportUtf8) {
+		CStringA header = AnsiToUtf8(headerAnsi);
+		file.Write(header.GetString(), header.GetLength());
+	}
+	else {
+		file.Write(headerAnsi.data(), (UINT)headerAnsi.size());
+	}
+
+	for (auto rpt : m_reports) {
+		if (rpt == nullptr) continue;
+		std::string line;
+		line.reserve(64);
+		line += std::to_string(rpt->getReportId());
+		line += ",(";
+
+		const auto& vids = rpt->getVids();
+		for (size_t i = 0; i < vids.size(); ++i) {
+			line += std::to_string(vids[i]);
+			if (i + 1 < vids.size()) {
+				line.push_back(',');
+			}
+		}
+		line += ")\r\n";
+
+		if (m_bReportUtf8) {
+			CStringA out = AnsiToUtf8(line);
+			file.Write(out.GetString(), out.GetLength());
+		}
+		else {
+			file.Write(line.data(), (UINT)line.size());
+		}
+	}
+
+	file.Close();
+	return 0;
+}
+
 int CHsmsPassive::loadCollectionEvents(const char* pszFilepath)
 {
 	CFile file;
diff --git a/SourceCode/Bond/Servo/HsmsPassive.h b/SourceCode/Bond/Servo/HsmsPassive.h
index 95622d2..aeb5bde 100644
--- a/SourceCode/Bond/Servo/HsmsPassive.h
+++ b/SourceCode/Bond/Servo/HsmsPassive.h
@@ -176,6 +176,7 @@
 
 	// 鍙栧緱Report
 	SERVO::CReport* getReport(int rptid);
+	int deleteReport(int rptid);
 
 	void setListener(SECSListener listener);
 	unsigned OnCimWork();
@@ -239,6 +240,7 @@
 	void clearAllVariabel();
 	std::vector<unsigned int> parseVidList(CString& strNums);
 	int writeVariablesToFile(const std::string& filepath);
+	int writeReportsToFile(const std::string& filepath);
 
 private:
 	CModel* m_pModel;
@@ -259,6 +261,9 @@
 	std::string m_strVariableFilepath;
 	bool m_bVariableUtf8{ false };
 	bool m_bVariableUtf8Bom{ false };
+	std::string m_strReportFilepath;
+	bool m_bReportUtf8{ false };
+	bool m_bReportUtf8Bom{ false };
 	BOOL m_bCimWorking;
 	HANDLE m_hCimWorkEvent;
 	HANDLE m_hCimWorkThreadHandle;

--
Gitblit v1.9.3