From 15bbd62b0b24b72c717ae91ab45348a2083ad04e Mon Sep 17 00:00:00 2001
From: alielie <alielie@git.com>
Date: 星期五, 10 十月 2025 08:58:18 +0800
Subject: [PATCH] 优化bug;

---
 HMI.AlermInfoManage/AlarmSettingList.xaml.cs |  161 +++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 123 insertions(+), 38 deletions(-)

diff --git a/HMI.AlermInfoManage/AlarmSettingList.xaml.cs b/HMI.AlermInfoManage/AlarmSettingList.xaml.cs
index 04a2a06..ba8ac96 100644
--- a/HMI.AlermInfoManage/AlarmSettingList.xaml.cs
+++ b/HMI.AlermInfoManage/AlarmSettingList.xaml.cs
@@ -1,7 +1,10 @@
 锘縰sing HMI.AlarmInfoManage.Entity;
 using HMI.Core.Util;
 using HMI.UI.Util;
+using HMI.Util;
 using log4net;
+using Microsoft.Win32;
+using NPOI.SS.Formula.Functions;
 using Panuon.WPF.UI;
 using System;
 using System.Collections.ObjectModel;
@@ -18,10 +21,14 @@
     public partial class AlarmSettingList : WindowX
     {
         private ILog log;
-        public AlarmSettingList(string blockFile, string markFile, ILog log)
+        public AlarmSettingList(string blockFile, string markFile, ObservableCollection<AlarmSetting> datasource,
+            ObservableCollection<AlarmSetting> markDatasource,
+            ILog log)
         {
             this.blockFile = blockFile;
             this.markFile = markFile;
+            Datasource = datasource;
+            MarkDatasource = markDatasource;
             this.log = log;
             DataContext = this;
             InitializeComponent();
@@ -47,7 +54,7 @@
         }
 
         private ObservableCollection<string> items =
-            new ObservableCollection<string> { "DB鍧楁姤璀�", "鏍囧織浣嶆姤璀�" };
+            new ObservableCollection<string> { LangUtil.Get("Alm.DbBlockAlarm"), LangUtil.Get("Alm.TagAlarm") };
 
         public ObservableCollection<string> Items
         {
@@ -55,14 +62,13 @@
             set { Set(ref items, value); }
         }
 
-        private string selectedValue;
-
-        public string SelectedValue
+        private int itemIndex;
+        public int ItemIndex
         {
-            get { return selectedValue; }
+            get => itemIndex;
             set
             {
-                Set(ref selectedValue, value);
+                Set(ref itemIndex, value);
                 LoadData();
             }
         }
@@ -70,7 +76,7 @@
         private void Button_Click(object sender, RoutedEventArgs e)
         {
             Button btn = sender as Button;
-            DataGridRow row = btn?.Tag as DataGridRow;
+            AlarmSetting data = btn?.Tag as AlarmSetting;
             switch (btn.Name)
             {
                 case "refreshBtn":
@@ -83,37 +89,58 @@
                     DeleteBatchData();
                     break;
                 case "editBtn":
-                    EditData(row.GetIndex(), btn.DataContext as AlarmSetting);
+                    EditData(ItemIndex == 0 ? Datasource.IndexOf(data) : MarkDatasource.IndexOf(data), data);
+                    break;
+                case "exportBtn":
+                    ExportData();
+                    break;
+                case "importBtn":
+                    ImportData();
                     break;
                 case "delBtn":
-                    DeleteData(row.GetIndex());
+                    DeleteData(ItemIndex == 0 ? Datasource.IndexOf(data) : MarkDatasource.IndexOf(data));
                     break;
                 default:
                     break;
             }
-
         }
 
         private void LoadData()
         {
             try
             {
-                ObservableCollection<AlarmSetting> datasource = new ObservableCollection<AlarmSetting>();
-                if (SelectedValue == "DB鍧楁姤璀�" && File.Exists(blockFile))
+                if (File.Exists(blockFile))
                 {
-                    datasource =
+                    Datasource =
                         XmlHelper.DeserializeFromXml<ObservableCollection<AlarmSetting>>(blockFile);
                 }
-                else if (SelectedValue == "鏍囧織浣嶆姤璀�" && File.Exists(markFile))
+                else
                 {
-                    datasource =
-                        XmlHelper.DeserializeFromXml<ObservableCollection<AlarmSetting>>(markFile);
+                    Datasource = new ObservableCollection<AlarmSetting>();
                 }
-                Datasource = datasource;
             }
             catch (Exception ex)
             {
-                ToastHelper.Error(this, $"鍔犺浇閿欒锛亄ex.Message}");
+                Datasource = new ObservableCollection<AlarmSetting>();
+                ToastHelper.Error(this, $"{this.lang("App.LoadFailed")}: {ex.Message}");
+            }
+
+            try
+            {
+                if (File.Exists(markFile))
+                {
+                    MarkDatasource =
+                        XmlHelper.DeserializeFromXml<ObservableCollection<AlarmSetting>>(markFile);
+                }
+                else
+                {
+                    MarkDatasource = new ObservableCollection<AlarmSetting>();
+                }
+            }
+            catch (Exception ex)
+            {
+                MarkDatasource = new ObservableCollection<AlarmSetting>();
+                ToastHelper.Error(this, $"{this.lang("App.LoadFailed")}: {ex.Message}");
             }
         }
 
@@ -121,13 +148,14 @@
         {
             try
             {
-                var file = SelectedValue == "DB鍧楁姤璀�" ? blockFile : markFile;
+                var file = ItemIndex == 0 ? blockFile : markFile;
+                var temp = ItemIndex == 0 ? this.lang("Alm.DbBlockAlarm") : this.lang("Alm.TagAlarm");
                 var modal = new AlarmSettingModal(log)
                 {
-                    Title = $"鏂板{SelectedValue}",
+                    Title = $"{this.lang("App.Add")}{temp}",
                     Owner = Window.GetWindow(this),
                 };
-                if (modal.Open(-1, data, Datasource, file) == true)
+                if (modal.Open(-1, data, ItemIndex == 0 ? Datasource : MarkDatasource, file, ItemIndex != 0) == true)
                 {
                     LoadData();
                 }
@@ -135,7 +163,7 @@
             catch (Exception ex)
             {
                 log.Error("Add AlarmSetting Failed.", ex);
-                ToastHelper.Error(this, $"鏂板澶辫触锛亄ex.Message}");
+                ToastHelper.Error(this, $"{this.lang("App.AddFailed")}: {ex.Message}");
             }
         }
 
@@ -143,13 +171,14 @@
         {
             try
             {
-                var file = SelectedValue == "DB鍧楁姤璀�" ? blockFile : markFile;
+                var file = ItemIndex == 0 ? blockFile : markFile;
+                var temp = ItemIndex == 0 ? this.lang("Alm.DbBlockAlarm") : this.lang("Alm.TagAlarm");
                 var modal = new AlarmSettingModal(log)
                 {
-                    Title = $"缂栬緫{SelectedValue}",
+                    Title = $"{this.lang("App.Edit")}{temp}",
                     Owner = Window.GetWindow(this),
                 };
-                if (modal.Open(index, data, Datasource, file) == true)
+                if (modal.Open(index, data, ItemIndex == 0 ? Datasource : MarkDatasource, file, ItemIndex != 0) == true)
                 {
                     LoadData();
                 }
@@ -157,17 +186,72 @@
             catch (Exception ex)
             {
                 log.Error("Edit AlarmSetting Failed.", ex);
-                ToastHelper.Error(this, $"缂栬緫澶辫触锛亄ex.Message}");
+                ToastHelper.Error(this, $"{this.lang("App.EditFailed")}: {ex.Message}");
             }
         }
+
+        private void ExportData()
+        {
+            try
+            {
+                SaveFileDialog sfd = new SaveFileDialog
+                {
+                    FileName = ItemIndex == 0 ? "DB鍧楁姤璀�.xls" : "鏍囧織浣嶆姤璀�.xls",
+                    Filter = "Excel|*.xls"
+                };
+                if (sfd.ShowDialog() == true)
+                {
+                    ExcelHelper.ExportExcel(sfd.FileName, ItemIndex == 0 ? Datasource : MarkDatasource);
+                }
+            }
+            catch (Exception ex)
+            {
+                log.Error("Export Failed.", ex);
+                ToastHelper.Error(this, $"{this.lang("App.ExportFailed")}: {ex.Message}");
+            }
+        }
+
+        private void ImportData()
+        {
+            try
+            {
+                OpenFileDialog ofd = new OpenFileDialog
+                {
+                    Filter = "Excel|*.xls"
+                };
+                if (ofd.ShowDialog() == true)
+                {
+                    var datasource = new ObservableCollection<AlarmSetting>(
+                        ExcelHelper.ReadFromExcel<AlarmSetting>(ofd.FileName));
+                    if (ItemIndex == 0)
+                    {
+                        Datasource = datasource;
+                    }
+                    else
+                    {
+                        MarkDatasource = datasource;
+                    }
+                    var file = ItemIndex == 0 ? blockFile : markFile;
+                    var temp = ItemIndex == 0 ? Datasource : MarkDatasource;
+                    XmlHelper.SerializeToXml(file, temp);
+                    LoadData();
+                }
+            }
+            catch (Exception ex)
+            {
+                log.Error("Import Failed.", ex);
+                ToastHelper.Error(this, $"{this.lang("App.ImportFailed")}: {ex.Message}");
+            }
+        }
+
         private void DeleteData(int index)
         {
             try
             {
-                if (MsgBoxHelper.WarnMessage(Window.GetWindow(this), "纭畾瑕佸垹闄よ椤瑰悧锛熻鎿嶄綔涓嶅彲閫�") == MessageBoxResult.OK)
+                if (MsgBoxHelper.WarnMessage(Window.GetWindow(this), this.lang("HMI.MakeSureDeleteThis")) == MessageBoxResult.OK)
                 {
-                    var file = SelectedValue == "DB鍧楁姤璀�" ? blockFile : markFile;
-                    var temp = XmlHelper.DeepClone(Datasource);
+                    var file = ItemIndex == 0 ? blockFile : markFile;
+                    var temp = XmlHelper.DeepClone(ItemIndex == 0 ? Datasource : MarkDatasource);
 
                     temp.RemoveAt(index);
                     XmlHelper.SerializeToXml(file, temp);
@@ -177,24 +261,25 @@
             catch (Exception ex)
             {
                 log.Error("Delete AlarmSetting Failed.", ex);
-                ToastHelper.Error(this, $"鍒犻櫎澶辫触锛亄ex.Message}");
+                ToastHelper.Error(this, $"{this.lang("App.DeleteFailed")}: {ex.Message}");
             }
         }
         private void DeleteBatchData()
         {
             try
             {
-                var temp = Datasource.Where(e => e.IsSelected).ToList();
+                var temp = (ItemIndex == 0 ? Datasource : MarkDatasource).Where(e => e.IsSelected).ToList();
                 if (temp == null || temp.Count <= 0)
                 {
-                    ToastHelper.Warning(Window.GetWindow(this), "璇烽�夋嫨瑕佸垹闄ょ殑鏁版嵁");
+                    ToastHelper.Warning(Window.GetWindow(this), this.lang("HMI.NoDeleteData"));
                     return;
                 }
 
-                if (MsgBoxHelper.WarnMessage(Window.GetWindow(this), $"纭畾瑕佸垹闄temp.Count}椤规暟鎹悧锛熻鎿嶄綔涓嶅彲閫�") == MessageBoxResult.OK)
+                if (MsgBoxHelper.WarnMessage(Window.GetWindow(this), this.lang("HMI.MakeSureDeletes", temp.Count))
+                    == MessageBoxResult.OK)
                 {
-                    var file = SelectedValue == "DB鍧楁姤璀�" ? blockFile : markFile;
-                    temp = Datasource.Where(e => !e.IsSelected).ToList();
+                    var file = ItemIndex == 0 ? blockFile : markFile;
+                    temp = (ItemIndex == 0 ? Datasource : MarkDatasource).Where(e => !e.IsSelected).ToList();
                     XmlHelper.SerializeToXml(file, temp);
                     LoadData();
                 }
@@ -202,13 +287,13 @@
             catch (Exception ex)
             {
                 log.Error("Delete Batch AlarmSetting Failed.", ex);
-                ToastHelper.Error(this, $"鍒犻櫎澶辫触锛亄ex.Message}");
+                ToastHelper.Error(this, $"{this.lang("App.DeleteFailed")}: {ex.Message}");
             }
         }
 
         private void WindowX_Loaded(object sender, RoutedEventArgs e)
         {
-            SelectedValue = "DB鍧楁姤璀�";
+            ItemIndex = 0;
             Loaded -= WindowX_Loaded;
         }
     }

--
Gitblit v1.9.1