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 { /// /// AlarmSettingList.xaml 的交互逻辑 /// 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 datasource; public ObservableCollection Datasource { get => datasource; set { Set(ref datasource, value); } } private ObservableCollection markDatasource; public ObservableCollection MarkDatasource { get => markDatasource; set { Set(ref markDatasource, value); } } private ObservableCollection items = new ObservableCollection { LangUtil.Get("Alm.DbBlockAlarm"), LangUtil.Get("Alm.TagAlarm") }; public ObservableCollection 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 datasource = new ObservableCollection(); if (ItemIndex == 0 && File.Exists(blockFile)) { datasource = XmlHelper.DeserializeFromXml>(blockFile); } else if (ItemIndex == 1 && File.Exists(markFile)) { datasource = XmlHelper.DeserializeFromXml>(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; } } }