diff --git a/CapMachine.Model/HistoryWorkCondFile.cs b/CapMachine.Model/HistoryWorkCondFile.cs index bc8eb49..f1928a5 100644 --- a/CapMachine.Model/HistoryWorkCondFile.cs +++ b/CapMachine.Model/HistoryWorkCondFile.cs @@ -1,4 +1,5 @@ using FreeSql.DataAnnotations; +using System.ComponentModel; namespace CapMachine.Model { @@ -49,5 +50,23 @@ namespace CapMachine.Model /// public long HistoryExpId { get; set; } public HistoryExp? HistoryExp { get; set; } + + [Column(IsIgnore = true)] + public bool IsSelected + { + get { return _isSelected; } + set + { + if (_isSelected != value) + { + _isSelected = value; + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsSelected))); + } + } + } + + private bool _isSelected; + + public event PropertyChangedEventHandler? PropertyChanged; } } diff --git a/CapMachine.Wpf/ViewModels/HistoryDataViewModel.cs b/CapMachine.Wpf/ViewModels/HistoryDataViewModel.cs index bd17564..1b5026b 100644 --- a/CapMachine.Wpf/ViewModels/HistoryDataViewModel.cs +++ b/CapMachine.Wpf/ViewModels/HistoryDataViewModel.cs @@ -15,8 +15,8 @@ using Prism.Events; using Prism.Regions; using Prism.Services.Dialogs; using System; +using System.Collections; using System.Collections.Generic; -using System.Collections.ObjectModel; using System.Diagnostics; using System.Globalization; using System.IO; @@ -51,7 +51,7 @@ namespace CapMachine.Wpf.ViewModels /// /// /// - public HistoryDataViewModel(IDialogService dialogService, IFreeSql freeSql, IEventAggregator eventAggregator, IMapper mapper,ConfigService configService) + public HistoryDataViewModel(IDialogService dialogService, IFreeSql freeSql, IEventAggregator eventAggregator, IMapper mapper, ConfigService configService) { DialogService = dialogService; FreeSql = freeSql; @@ -132,6 +132,20 @@ namespace CapMachine.Wpf.ViewModels set { _IsLeftDrawerOpen = value; RaisePropertyChanged(); } } + private bool _IsBusy; + public bool IsBusy + { + get { return _IsBusy; } + set { _IsBusy = value; RaisePropertyChanged(); } + } + + private string _BusyMessage = "正在加载,请稍候..."; + public string BusyMessage + { + get { return _BusyMessage; } + set { _BusyMessage = value; RaisePropertyChanged(); } + } + /// @@ -224,15 +238,7 @@ namespace CapMachine.Wpf.ViewModels set { _SelectedHistoryExp = value; RaisePropertyChanged(); } } - private HistoryWorkCondFile _SelectedHistoryWorkCondFile; - /// - /// 选中的历史数据文件 - /// - public HistoryWorkCondFile SelectedHistoryWorkCondFile - { - get { return _SelectedHistoryWorkCondFile; } - set { _SelectedHistoryWorkCondFile = value; RaisePropertyChanged(); } - } + /// /// 当前搜索的工况条件 @@ -268,7 +274,13 @@ namespace CapMachine.Wpf.ViewModels if (par is HistoryExp) { SelectedHistoryExp = par as HistoryExp; - ListHistoryWorkCondFile = SelectedHistoryExp!.HistoryWorkCondFiles!; + var files = SelectedHistoryExp!.HistoryWorkCondFiles!; + foreach (var f in files) + { + if (f != null) f.IsSelected = false; + } + ListHistoryWorkCondFile = files.ToList(); + //ListHistoryWorkCondFile = ListHistoryWorkCondFile.ToList(); return; } if ((par as SelectionChangedEventArgs)!.AddedItems.Count == 0) @@ -290,63 +302,18 @@ namespace CapMachine.Wpf.ViewModels if (Selecteddata != null) { SelectedHistoryExp = Selecteddata; - ListHistoryWorkCondFile = SelectedHistoryExp.HistoryWorkCondFiles!; - } - } - - - private DelegateCommand _FileGridSelectionChangedCmd; - /// - /// 试验信息选中行数据命令 - /// - public DelegateCommand FileGridSelectionChangedCmd - { - set - { - _FileGridSelectionChangedCmd = value; - } - get - { - if (_FileGridSelectionChangedCmd == null) + var files = SelectedHistoryExp.HistoryWorkCondFiles!; + foreach (var f in files) { - _FileGridSelectionChangedCmd = new DelegateCommand((par) => FileGridSelectionChangedCmdMethod(par)); + if (f != null) f.IsSelected = false; } - return _FileGridSelectionChangedCmd; + ListHistoryWorkCondFile = files; + ListHistoryWorkCondFile = ListHistoryWorkCondFile.ToList(); } } - private void FileGridSelectionChangedCmdMethod(object par) - { - if (par == null) - { - return; - } - if (par is HistoryWorkCondFile) - { - SelectedHistoryWorkCondFile = par as HistoryWorkCondFile; - return; - } - if ((par as SelectionChangedEventArgs)!.AddedItems.Count == 0) - { - return; - } - //先判断是否是正确的集合数据,防止DataGrid的数据源刷新导致的触发事件 - var Selecteddata = (par as SelectionChangedEventArgs)!.AddedItems[0] as HistoryWorkCondFile; - ////选中的行数据 - //var Selecteddata = (par as DataGrid)!.SelectedItem as HistoryExp; - ////选中的列数据 - //var selectedColumn = (par as DataGrid)!.CurrentColumn; - //SelectedIndex= ProParsHelper.GetIndexByName(selectedColumn); - - //SelectedProStepDto = par as ProStepDto; - if (Selecteddata != null) - { - SelectedHistoryWorkCondFile = Selecteddata; - - } - } private DelegateCommand _FilterHistoryDataFileCmd; /// @@ -397,6 +364,48 @@ namespace CapMachine.Wpf.ViewModels HasHeaderRecord = false, }; + private DelegateCommand _SelectAllFilesCmd; + public DelegateCommand SelectAllFilesCmd + { + set + { + _SelectAllFilesCmd = value; + } + get + { + if (_SelectAllFilesCmd == null) + { + _SelectAllFilesCmd = new DelegateCommand((par) => SelectAllFilesCmdMethod(par)); + } + return _SelectAllFilesCmd; + } + } + private void SelectAllFilesCmdMethod(object par) + { + bool isChecked = false; + if (par is bool b) { isChecked = b; } + else + { + var nb = par as bool?; + if (nb.HasValue) isChecked = nb.Value; + else + { + var sb = par as string; + if (!string.IsNullOrWhiteSpace(sb)) + { + bool.TryParse(sb, out isChecked); + } + } + } + if (ListHistoryWorkCondFile == null) return; + foreach (var f in ListHistoryWorkCondFile) + { + if (f != null) f.IsSelected = isChecked; + } + // 触发刷新 + ListHistoryWorkCondFile = ListHistoryWorkCondFile.ToList(); + } + Stopwatch stopwatch = new Stopwatch(); private DelegateCommand _HistoryDataFileCmd; @@ -422,118 +431,134 @@ namespace CapMachine.Wpf.ViewModels /// /// 获取试验数据的命令的执行方法 /// - private void HistoryDataFileCmdMethod() + private async void HistoryDataFileCmdMethod() { - //SelectedHistoryWorkCondFile - if (SelectedHistoryExp != null && SelectedHistoryWorkCondFile != null) - { - CsvRecordModels = new List(); - - ////第一次计时 - stopwatch.Start(); //启动Stopwatch - - if (!string.IsNullOrEmpty(SelectedHistoryWorkCondFile.FilePath)) - { - if (File.Exists(SelectedHistoryWorkCondFile.FilePath))//是否存在文件 - { - lock (ConfigService.CsvFileLock) - { - using (var reader = new StreamReader(SelectedHistoryWorkCondFile.FilePath)) - using (var csv = new CsvReader(reader, CultureInfo.CurrentCulture)) - { - csv.Context.RegisterClassMap(); - //GetRecords().ToList(); - - CsvRecordModels.AddRange(csv.GetRecords().ToList()); - } - } - } - else - { - System.Windows.MessageBox.Show("没有发现地址对应的文件"); - } - } - else - { - System.Windows.MessageBox.Show("发现空的文件地址"); - } - - //数据加载完毕后进入到Chart曲线中 - if (CsvRecordModels.Count > 0) - { - EventAggregator.GetEvent().Publish(new HistoryDataToChartMsg() { Machine = "History", Data = CsvRecordModels }); - } - - stopwatch.Stop(); //停止Stopwatch - Console.WriteLine("加载CSV数据耗时::{0}", stopwatch.Elapsed.TotalSeconds.ToString()); - stopwatch.Reset(); - - IsLeftDrawerOpen = false; - } - else + if (SelectedHistoryExp == null) { System.Windows.MessageBox.Show("请选中后再操作", "提示", MessageBoxButton.OK, MessageBoxImage.Hand); + return; } + var files = new List(); + if (ListHistoryWorkCondFile != null) + { + foreach (var f in ListHistoryWorkCondFile) + { + if (f != null && f.IsSelected) + { + files.Add(f); + } + } + } - #region 整个工况 - //整个试验工况一起加载数据 - //if (SelectedHistoryExp != null && SelectedHistoryExp.HistoryWorkCondFiles != null) - //{ - // CsvRecordModels = new List(); + files = files + .Where(f => f != null) + .GroupBy(f => string.IsNullOrWhiteSpace(f.FilePath) ? $"ID:{f.Id}" : f.FilePath) + .Select(g => g.First()) + .ToList(); - // ////第一次计时 - // stopwatch.Start(); //启动Stopwatch + if (files.Count == 0) + { + System.Windows.MessageBox.Show("请先在右侧勾选至少一个CSV文件", "提示", MessageBoxButton.OK, MessageBoxImage.Information); + return; + } - // foreach (var item in SelectedHistoryExp.HistoryWorkCondFiles) - // { - // if (!string.IsNullOrEmpty(item.FilePath)) - // { - // if (File.Exists(item.FilePath))//是否存在文件 - // { - // lock (ConfigService.CsvFileLock) - // { - // using (var reader = new StreamReader(item.FilePath)) - // using (var csv = new CsvReader(reader, CultureInfo.CurrentCulture)) - // { - // csv.Context.RegisterClassMap(); - // //GetRecords().ToList(); + BusyMessage = "正在加载CSV数据,请稍候..."; + IsBusy = true; - // CsvRecordModels.AddRange(csv.GetRecords().ToList()); - // } - // } + var loaded = new List(); + string errorText = string.Empty; - // } - // else - // { - // System.Windows.MessageBox.Show("没有发现地址对应的文件"); - // } - // } - // else - // { - // System.Windows.MessageBox.Show("发现空的文件地址"); - // } - // } + stopwatch.Start(); - // //数据加载完毕后进入到Chart曲线中 - // if (CsvRecordModels.Count > 0) - // { - // EventAggregator.GetEvent().Publish(new HistoryDataToChartMsg() { Machine = "History", Data = CsvRecordModels }); - // } + try + { + await Task.Run(() => + { + var errors = new StringBuilder(); + for (int i = 0; i < files.Count; i++) + { + var f = files[i]; + try + { + Application.Current.Dispatcher.Invoke(() => + { + BusyMessage = $"正在加载({i + 1}/{files.Count}):{System.IO.Path.GetFileName(f?.FilePath)}"; + }); - // stopwatch.Stop(); //停止Stopwatch - // Console.WriteLine("加载CSV数据耗时::{0}", stopwatch.Elapsed.TotalSeconds.ToString()); - // stopwatch.Reset(); + if (f == null) continue; + if (string.IsNullOrWhiteSpace(f.FilePath)) + { + errors.AppendLine("发现空的文件地址"); + continue; + } + if (!File.Exists(f.FilePath)) + { + errors.AppendLine($"没有发现地址对应的文件: {f.FilePath}"); + continue; + } - // IsLeftDrawerOpen = false; - //} - //else - //{ - // System.Windows.MessageBox.Show("请选中后再操作", "提示", MessageBoxButton.OK, MessageBoxImage.Hand); - //} + var rec = ReadCsvFile(f.FilePath); + if (rec != null && rec.Count > 0) + { + loaded.AddRange(rec); + } + } + catch (Exception ex) + { + errors.AppendLine($"读取CSV失败: {ex.Message}"); + } + } + errorText = errors.ToString(); + }); - #endregion + if (!string.IsNullOrWhiteSpace(errorText)) + { + System.Windows.MessageBox.Show(errorText, "读取提示", MessageBoxButton.OK, MessageBoxImage.Warning); + } + CsvRecordModels = new List(); + if (loaded != null && loaded.Count > 0) + { + CsvRecordModels = loaded + .Where(r => r != null) + .OrderBy(r => r.CreateTime) + .ToList(); + Application.Current.Dispatcher.Invoke(() => + { + EventAggregator.GetEvent().Publish(new HistoryDataToChartMsg() { Machine = "History", Data = CsvRecordModels }); + }); + } + } + finally + { + stopwatch.Stop(); + Console.WriteLine("加载CSV数据耗时::{0}", stopwatch.Elapsed.TotalSeconds.ToString()); + stopwatch.Reset(); + IsBusy = false; + IsLeftDrawerOpen = false; + } + + } + + private List ReadCsvFile(string filePath) + { + var result = new List(); + lock (ConfigService.CsvFileLock) + { + using (var reader = new StreamReader(filePath, Encoding.UTF8)) + using (var csv = new CsvReader(reader, CultureInfo.CurrentCulture)) + { + csv.Context.RegisterClassMap(); + var list = csv.GetRecords().ToList(); + if (list != null && list.Count > 0) + { + // 过滤无效时间,避免排序异常 + result = list.Where(r => r != null && r.CreateTime != default && r.CreateTime != DateTime.MinValue).ToList(); + } + } + } + return result; } @@ -1075,7 +1100,7 @@ namespace CapMachine.Wpf.ViewModels SeletedGroup6ListViewTabIndex = 0; SeletedGroup7ListViewTabIndex = 0; SeletedGroup8ListViewTabIndex = 0; - + } else { diff --git a/CapMachine.Wpf/Views/HistoryDataView.xaml b/CapMachine.Wpf/Views/HistoryDataView.xaml index 468c246..c2e6860 100644 --- a/CapMachine.Wpf/Views/HistoryDataView.xaml +++ b/CapMachine.Wpf/Views/HistoryDataView.xaml @@ -26,6 +26,7 @@ + - + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - + + + + + + + + + + + + +