| | |
| | | using HMI.AlarmInfoManage.Entity; |
| | | using HMI.Core.Entity; |
| | | using HMI.Core.Util; |
| | | using HMI.UI.Util; |
| | | using HMI.Util; |
| | | using HMI.Util.Alarm; |
| | | using log4net; |
| | | using Microsoft.Win32; |
| | | using Panuon.WPF.UI; |
| | | using System; |
| | | using System.Collections.ObjectModel; |
| | | using System.IO; |
| | | using System.Linq; |
| | | using System.Windows; |
| | | using System.Windows.Controls; |
| | |
| | | public partial class AlarmSettingList : WindowX |
| | | { |
| | | private ILog log; |
| | | public AlarmSettingList(string blockFile, string markFile, ILog log) |
| | | public AlarmSettingList(ILog log) |
| | | { |
| | | this.blockFile = blockFile; |
| | | this.markFile = markFile; |
| | | BlockDatasource = AlarmUtil.BlockSettings; |
| | | MarkDatasource = AlarmUtil.MarkSettings; |
| | | this.log = log; |
| | | DataContext = this; |
| | | InitializeComponent(); |
| | | } |
| | | |
| | | private readonly string blockFile; |
| | | private readonly string markFile; |
| | | private ObservableCollection<AlarmSetting> blockDatasource; |
| | | |
| | | private ObservableCollection<AlarmSetting> datasource; |
| | | |
| | | public ObservableCollection<AlarmSetting> Datasource |
| | | public ObservableCollection<AlarmSetting> BlockDatasource |
| | | { |
| | | get => datasource; |
| | | set { Set(ref datasource, value); } |
| | | get => blockDatasource; |
| | | set { Set(ref blockDatasource, value); } |
| | | } |
| | | |
| | | private ObservableCollection<AlarmSetting> markDatasource; |
| | |
| | | } |
| | | |
| | | private ObservableCollection<string> items = |
| | | new ObservableCollection<string> { "DB块报警", "标志位报警" }; |
| | | new ObservableCollection<string> { LangUtil.Get("Alm.DbBlockAlarm"), LangUtil.Get("Alm.TagAlarm") }; |
| | | |
| | | public ObservableCollection<string> Items |
| | | { |
| | |
| | | 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(); |
| | | } |
| | | } |
| | |
| | | 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": |
| | |
| | | DeleteBatchData(); |
| | | break; |
| | | case "editBtn": |
| | | EditData(row.GetIndex(), btn.DataContext as AlarmSetting); |
| | | EditData(ItemIndex == 0 ? BlockDatasource.IndexOf(data) : MarkDatasource.IndexOf(data), data); |
| | | break; |
| | | case "exportBtn": |
| | | ExportData(); |
| | | break; |
| | | case "importBtn": |
| | | ImportData(); |
| | | break; |
| | | case "delBtn": |
| | | DeleteData(row.GetIndex()); |
| | | DeleteData(ItemIndex == 0 ? BlockDatasource.IndexOf(data) : MarkDatasource.IndexOf(data)); |
| | | 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}"); |
| | | } |
| | | BlockDatasource = AlarmUtil.BlockSettings; |
| | | MarkDatasource = AlarmUtil.MarkSettings; |
| | | } |
| | | |
| | | private void AddData(AlarmSetting data) |
| | | { |
| | | try |
| | | { |
| | | var file = SelectedValue == "DB块报警" ? 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 ? BlockDatasource : MarkDatasource, ItemIndex == 0) == true) |
| | | { |
| | | LoadData(); |
| | | } |
| | |
| | | catch (Exception ex) |
| | | { |
| | | log.Error("Add AlarmSetting Failed.", ex); |
| | | ToastHelper.Error(this, $"新增失败!{ex.Message}"); |
| | | ToastHelper.Error(this, $"{this.lang("App.AddFailed")}: {ex.Message}"); |
| | | } |
| | | } |
| | | |
| | |
| | | { |
| | | try |
| | | { |
| | | var file = SelectedValue == "DB块报警" ? 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 ? BlockDatasource : MarkDatasource, ItemIndex == 0) == true) |
| | | { |
| | | LoadData(); |
| | | } |
| | |
| | | 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 ? BlockDatasource : 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) |
| | | { |
| | | AlarmUtil.UpdateBlockSettings(datasource); |
| | | BlockDatasource = AlarmUtil.BlockSettings; |
| | | } |
| | | else |
| | | { |
| | | AlarmUtil.UpdateMarkSettings(datasource); |
| | | MarkDatasource = AlarmUtil.MarkSettings; |
| | | } |
| | | } |
| | | } |
| | | 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); |
| | | bool isBlock = ItemIndex == 0; |
| | | var temp = XmlHelper.DeepClone(isBlock ? BlockDatasource : MarkDatasource); |
| | | |
| | | temp.RemoveAt(index); |
| | | XmlHelper.SerializeToXml(file, temp); |
| | | if (isBlock) |
| | | { |
| | | AlarmUtil.UpdateBlockSettings(temp); |
| | | } |
| | | else |
| | | { |
| | | AlarmUtil.UpdateMarkSettings(temp); |
| | | } |
| | | LoadData(); |
| | | } |
| | | } |
| | | 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 ? BlockDatasource : 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(); |
| | | XmlHelper.SerializeToXml(file, temp); |
| | | bool isBlock = ItemIndex == 0; |
| | | temp = (isBlock ? BlockDatasource : MarkDatasource).Where(e => !e.IsSelected).ToList(); |
| | | if (isBlock) |
| | | { |
| | | AlarmUtil.UpdateBlockSettings(temp); |
| | | } |
| | | else |
| | | { |
| | | AlarmUtil.UpdateMarkSettings(temp); |
| | | } |
| | | LoadData(); |
| | | } |
| | | } |
| | | 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; |
| | | } |
| | | } |