alielie
2025-03-26 4cac1b533e133fdc8b3def90a0f998820f51907a
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
216
217
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> { LangUtil.Get("Alm.DbBlockAlarm"), LangUtil.Get("Alm.TagAlarm") };
 
        public ObservableCollection<string> Items
        {
            get => items;
            set { Set(ref items, value); }
        }
 
        private int itemIndex;
        public int ItemIndex
        {
            get => itemIndex;
            set
            {
                Set(ref itemIndex, 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 (ItemIndex == 0 && File.Exists(blockFile))
                {
                    datasource =
                        XmlHelper.DeserializeFromXml<ObservableCollection<AlarmSetting>>(blockFile);
                }
                else if (ItemIndex == 1 && File.Exists(markFile))
                {
                    datasource =
                        XmlHelper.DeserializeFromXml<ObservableCollection<AlarmSetting>>(markFile);
                }
                Datasource = datasource;
            }
            catch (Exception ex)
            {
                ToastHelper.Error(this, $"{this.lang("App.LoadFailed")}: {ex.Message}");
            }
        }
 
        private void AddData(AlarmSetting data)
        {
            try
            {
                var file = ItemIndex == 0 ? blockFile : markFile;
                var temp = ItemIndex == 0 ? this.lang("Alm.DbBlockAlarm") : this.lang("Alm.TagAlarm");
                var modal = new AlarmSettingModal(log)
                {
                    Title = $"{this.lang("App.Add")}{temp}",
                    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, $"{this.lang("App.AddFailed")}: {ex.Message}");
            }
        }
 
        private void EditData(int index, AlarmSetting data)
        {
            try
            {
                var file = ItemIndex == 0 ? blockFile : markFile;
                var temp = ItemIndex == 0 ? this.lang("Alm.DbBlockAlarm") : this.lang("Alm.TagAlarm");
                var modal = new AlarmSettingModal(log)
                {
                    Title = $"{this.lang("App.Edit")}{temp}",
                    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, $"{this.lang("App.EditFailed")}: {ex.Message}");
            }
        }
        private void DeleteData(int index)
        {
            try
            {
                if (MsgBoxHelper.WarnMessage(Window.GetWindow(this), this.lang("HMI.MakeSureDeleteThis")) == MessageBoxResult.OK)
                {
                    var file = ItemIndex == 0 ? 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, $"{this.lang("App.DeleteFailed")}: {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), this.lang("HMI.NoDeleteData"));
                    return;
                }
 
                if (MsgBoxHelper.WarnMessage(Window.GetWindow(this), this.lang("HMI.MakeSureDeletes", temp.Count))
                    == MessageBoxResult.OK)
                {
                    var file = ItemIndex == 0 ? 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, $"{this.lang("App.DeleteFailed")}: {ex.Message}");
            }
        }
 
        private void WindowX_Loaded(object sender, RoutedEventArgs e)
        {
            ItemIndex = 0;
            Loaded -= WindowX_Loaded;
        }
    }
}