chr
2025-03-03 7b903bb86375e9df24a725288d1c876b8d2b0e61
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
using HMI.AlarmInfoManage.Entity;
using HMI.Core.Util;
using HMI.UI.Util;
using log4net;
using Panuon.WPF.UI;
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
 
namespace HMI.AlarmInfoManage
{
    /// <summary>
    /// AlarmSettingList.xaml 的交互逻辑
    /// </summary>
    public partial class AlarmSettingList : WindowX
    {
        private ILog log;
        public AlarmSettingList(string blockFile, string markFile, ILog log)
        {
            this.blockFile = blockFile;
            this.markFile = markFile;
            this.log = log;
            DataContext = this;
            InitializeComponent();
        }
 
        private readonly string blockFile;
        private readonly string markFile;
 
        private ObservableCollection<AlarmSetting> datasource;
 
        public ObservableCollection<AlarmSetting> Datasource
        {
            get => datasource;
            set { Set(ref datasource, value); }
        }
 
        private ObservableCollection<AlarmSetting> markDatasource;
 
        public ObservableCollection<AlarmSetting> MarkDatasource
        {
            get => markDatasource;
            set { Set(ref markDatasource, value); }
        }
 
        private ObservableCollection<string> items =
            new ObservableCollection<string> { "DB块报警", "标志位报警" };
 
        public ObservableCollection<string> Items
        {
            get => items;
            set { Set(ref items, value); }
        }
 
        private string selectedValue;
 
        public string SelectedValue
        {
            get { return selectedValue; }
            set
            {
                Set(ref selectedValue, value);
                LoadData();
            }
        }
 
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            DataGridRow row = btn?.Tag as DataGridRow;
            switch (btn.Name)
            {
                case "refreshBtn":
                    LoadData();
                    break;
                case "addBtn":
                    AddData(new AlarmSetting());
                    break;
                case "delBatchBtn":
                    DeleteBatchData();
                    break;
                case "editBtn":
                    EditData(row.GetIndex(), btn.DataContext as AlarmSetting);
                    break;
                case "delBtn":
                    DeleteData(row.GetIndex());
                    break;
                default:
                    break;
            }
 
        }
 
        private void LoadData()
        {
            try
            {
                ObservableCollection<AlarmSetting> datasource = new ObservableCollection<AlarmSetting>();
                if (SelectedValue == "DB块报警" && File.Exists(blockFile))
                {
                    datasource =
                        XmlHelper.DeserializeFromXml<ObservableCollection<AlarmSetting>>(blockFile);
                }
                else if (SelectedValue == "标志位报警" && File.Exists(markFile))
                {
                    datasource =
                        XmlHelper.DeserializeFromXml<ObservableCollection<AlarmSetting>>(markFile);
                }
                Datasource = datasource;
            }
            catch (Exception ex)
            {
                ToastHelper.Error(this, $"加载错误!{ex.Message}");
            }
        }
 
        private void AddData(AlarmSetting data)
        {
            try
            {
                var file = SelectedValue == "DB块报警" ? blockFile : markFile;
                var modal = new AlarmSettingModal(log)
                {
                    Title = $"新增{SelectedValue}",
                    Owner = Window.GetWindow(this),
                };
                if (modal.Open(-1, data, Datasource, file) == true)
                {
                    LoadData();
                }
            }
            catch (Exception ex)
            {
                log.Error("Add AlarmSetting Failed.", ex);
                ToastHelper.Error(this, $"新增失败!{ex.Message}");
            }
        }
 
        private void EditData(int index, AlarmSetting data)
        {
            try
            {
                var file = SelectedValue == "DB块报警" ? blockFile : markFile;
                var modal = new AlarmSettingModal(log)
                {
                    Title = $"编辑{SelectedValue}",
                    Owner = Window.GetWindow(this),
                };
                if (modal.Open(index, data, Datasource, file) == true)
                {
                    LoadData();
                }
            }
            catch (Exception ex)
            {
                log.Error("Edit AlarmSetting Failed.", ex);
                ToastHelper.Error(this, $"编辑失败!{ex.Message}");
            }
        }
        private void DeleteData(int index)
        {
            try
            {
                if (MsgBoxHelper.WarnMessage(Window.GetWindow(this), "确定要删除该项吗?该操作不可逆") == MessageBoxResult.OK)
                {
                    var file = SelectedValue == "DB块报警" ? blockFile : markFile;
                    var temp = XmlHelper.DeepClone(Datasource);
 
                    temp.RemoveAt(index);
                    XmlHelper.SerializeToXml(file, temp);
                    LoadData();
                }
            }
            catch (Exception ex)
            {
                log.Error("Delete AlarmSetting Failed.", ex);
                ToastHelper.Error(this, $"删除失败!{ex.Message}");
            }
        }
        private void DeleteBatchData()
        {
            try
            {
                var temp = Datasource.Where(e => e.IsSelected).ToList();
                if (temp == null || temp.Count <= 0)
                {
                    ToastHelper.Warning(Window.GetWindow(this), "请选择要删除的数据");
                    return;
                }
 
                if (MsgBoxHelper.WarnMessage(Window.GetWindow(this), $"确定要删除{temp.Count}项数据吗?该操作不可逆") == MessageBoxResult.OK)
                {
                    var file = SelectedValue == "DB块报警" ? blockFile : markFile;
                    temp = Datasource.Where(e => !e.IsSelected).ToList();
                    XmlHelper.SerializeToXml(file, temp);
                    LoadData();
                }
            }
            catch (Exception ex)
            {
                log.Error("Delete Batch AlarmSetting Failed.", ex);
                ToastHelper.Error(this, $"删除失败!{ex.Message}");
            }
        }
 
        private void WindowX_Loaded(object sender, RoutedEventArgs e)
        {
            SelectedValue = "DB块报警";
            Loaded -= WindowX_Loaded;
        }
    }
}