using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command; using Microsoft.Extensions.DependencyInjection; using SlnMesnac.Model.domain; using SlnMesnac.Repository.service; using SlnMesnac.WPF.Models; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Xml.Linq; namespace SlnMesnac.WPF.ViewModel { public partial class StatisticsPageViewModel : ObservableObject { private readonly ILogoIdentifyService? logoIdentifyService; public StatisticsPageViewModel() { // MainWindowViewModel.queryListEvent += ExecQueryAsync; logoIdentifyService = App.ServiceProvider.GetService(); QueryCommand = new RelayCommand(obj => Query(obj)); QueryCodeCommand = new RelayCommand(QueryByCode); QueryResultCommand = new RelayCommand(QueryByResult); MainWindowViewModel.RefreDataGridEvent += LoadData; LoadData(); } #region 加载DataGrid数据 private async void LoadData() { // 查询条件初始化 BeginDate = DateTime.Now.Date; // 当天的 0:00:00 EndDate = DateTime.Now.Date.AddDays(2).AddSeconds(-1); // 当天的 23:59:59 await Task.Run(async () => { List list = await logoIdentifyService.QueryAllByTime(BeginDate, EndDate); //List list = await logoIdentifyService.GetAllRecordAsync(); if (list == null || list.Count == 0) return; list = list.OrderByDescending(x => x.RecordTime).ToList(); await App.Current.Dispatcher.BeginInvoke((Action)(() => { LogoIdentifyDataGrid.Clear(); foreach (LogoIdentify verify in list) { LogoIdentifyDataGrid.Add(verify); } })); // 刷新型号统计 ModelList(list); }); } /// /// 型号统计 /// public void ModelList(List list) { var ModelList = list.GroupBy(x => x.MaterialName). Select(x => new { ProductModel = x.Key, MaterialType = x.FirstOrDefault().MaterialType, IsChecked = x.FirstOrDefault().isChecked , Amount = x.Count(), PassAmount = x.Where(s => s.Result == 1).Count(), }).ToList(); App.Current.Dispatcher.BeginInvoke((Action)(() => { ModelDataGrid.Clear(); int index = 1; foreach (var item in ModelList) { ModelDataGrid.Add(new ModelCount() { No = index++, MaterialType = item.MaterialType, ProductName = item.ProductModel, IsChecked = item.IsChecked, TotalAmount = item.Amount, PassAmount = item.PassAmount, PassRate = ((double)item.PassAmount / item.Amount).ToString("P2") }); } })); } #endregion #region 初始化datagrid private ObservableCollection _LogoIdentifyDataGrid = new ObservableCollection(); public ObservableCollection LogoIdentifyDataGrid { get { return _LogoIdentifyDataGrid; } set { _LogoIdentifyDataGrid = value; RaisePropertyChanged();//属性通知 } } private ObservableCollection _ModelDataGrid = new ObservableCollection(); public ObservableCollection ModelDataGrid { get { return _ModelDataGrid; } set { _ModelDataGrid = value; RaisePropertyChanged();//属性通知 } } #endregion private DateTime _BeginDate; public DateTime BeginDate { get { return _BeginDate; } set { if (_BeginDate != value) { _BeginDate = value; RaisePropertyChanged();//属性通知 } } } private DateTime _EndDate; public DateTime EndDate { get { return _EndDate; } set { _EndDate = value; RaisePropertyChanged();//属性通知 } } private string _QueryCode; public string QueryCode { get { return _QueryCode; } set { if (_QueryCode != value) { _QueryCode = value; RaisePropertyChanged();//属性通知 } } } private ComboBoxItem _SelectedOption = new ComboBoxItem() { Content = ""}; public ComboBoxItem SelectedOption { get => _SelectedOption; set { if (_SelectedOption != value) { _SelectedOption = value; RaisePropertyChanged();//属性通知 } } } /// /// 时间查询事件 /// public RelayCommand QueryCommand { get; set; } /// /// 根据型号与条码查询事件 /// public RelayCommand QueryCodeCommand { get; set; } /// /// 根据时间与选择结果查询 /// public RelayCommand QueryResultCommand { get; set; } #region 时间查询 private async Task Query(object obj) { List list; try { LogoIdentifyDataGrid.Clear(); var result = (StatisticModel)obj; if (EndDate <= BeginDate) { MessageBox.Show("结束时间要大于开始时间!"); return; } list = await logoIdentifyService.QueryAllByTime(BeginDate, EndDate); if (list != null && list.Count > 0) { await App.Current.Dispatcher.BeginInvoke((Action)(() => { list = list.OrderByDescending(x => x.RecordTime).ToList(); foreach (LogoIdentify verify in list) { LogoIdentifyDataGrid.Add(verify); } })); // 刷新型号统计 ModelList(list); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } #endregion #region 根据条码号准确查询或者根据型号加上时间段与结果模糊查询 private async void QueryByCode() { List list; try { LogoIdentifyDataGrid.Clear(); // 条码准确查询 if (QueryCode.Length == 21) { list = await logoIdentifyService.GetRecordByCodeAsync(QueryCode); } else //根据型号查询,先查询时间段 { list = await logoIdentifyService.QueryAllByTime(BeginDate, EndDate); //过滤型号 list = list.Where(x => x.ProductCode.Contains(QueryCode) || x.MaterialName.Contains(QueryCode) || x.MaterialType.Contains(QueryCode)).ToList(); if (SelectedOption.Content.ToString() == "OK") { list = list.Where(x => x.Result == 1).ToList(); } else if (SelectedOption.Content.ToString() == "NG") { list = list.Where(x => x.Result == 0).ToList(); } } if (list != null && list.Count > 0) { await App.Current.Dispatcher.BeginInvoke((Action)(() => { list = list.OrderByDescending(x => x.RecordTime).ToList(); foreach (LogoIdentify verify in list) { LogoIdentifyDataGrid.Add(verify); } })); // 刷新型号统计 ModelList(list); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } #endregion #region 根据结果加上时间段模糊查询 private async void QueryByResult() { List list; try { LogoIdentifyDataGrid.Clear(); list = await logoIdentifyService.QueryAllByTime(BeginDate, EndDate); if (SelectedOption.Content.ToString() == "OK") { list = list.Where(x => x.Result == 1).ToList(); } else if (SelectedOption.Content.ToString() == "NG") { list = list.Where(x => x.Result == 0).ToList(); } if (list != null && list.Count > 0) { await App.Current.Dispatcher.BeginInvoke((Action)(() => { list = list.OrderByDescending(x => x.RecordTime).ToList(); foreach (LogoIdentify verify in list) { LogoIdentifyDataGrid.Add(verify); } })); // 刷新型号统计 ModelList(list); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } #endregion } }