alielie
2025-08-25 c4a3f8db90530a727ffab77c3bb5bb7d782792bf
HMI.AlermInfoManage/AlarmSettingList.xaml.cs
@@ -1,7 +1,10 @@
using 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();
@@ -69,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":
@@ -82,36 +89,57 @@
                    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 (ItemIndex == 0 && File.Exists(blockFile))
                if (File.Exists(blockFile))
                {
                    datasource =
                    Datasource =
                        XmlHelper.DeserializeFromXml<ObservableCollection<AlarmSetting>>(blockFile);
                }
                else if (ItemIndex == 1 && File.Exists(markFile))
                else
                {
                    datasource =
                        XmlHelper.DeserializeFromXml<ObservableCollection<AlarmSetting>>(markFile);
                    Datasource = new ObservableCollection<AlarmSetting>();
                }
                Datasource = datasource;
            }
            catch (Exception ex)
            {
                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}");
            }
        }
@@ -127,7 +155,7 @@
                    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();
                }
@@ -150,7 +178,7 @@
                    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();
                }
@@ -161,6 +189,61 @@
                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
@@ -168,7 +251,7 @@
                if (MsgBoxHelper.WarnMessage(Window.GetWindow(this), this.lang("HMI.MakeSureDeleteThis")) == MessageBoxResult.OK)
                {
                    var file = ItemIndex == 0 ? blockFile : markFile;
                    var temp = XmlHelper.DeepClone(Datasource);
                    var temp = XmlHelper.DeepClone(ItemIndex == 0 ? Datasource : MarkDatasource);
                    temp.RemoveAt(index);
                    XmlHelper.SerializeToXml(file, temp);
@@ -185,7 +268,7 @@
        {
            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), this.lang("HMI.NoDeleteData"));
@@ -196,7 +279,7 @@
                    == MessageBoxResult.OK)
                {
                    var file = ItemIndex == 0 ? blockFile : markFile;
                    temp = Datasource.Where(e => !e.IsSelected).ToList();
                    temp = (ItemIndex == 0 ? Datasource : MarkDatasource).Where(e => !e.IsSelected).ToList();
                    XmlHelper.SerializeToXml(file, temp);
                    LoadData();
                }