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.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)); MainWindowViewModel.RefreDataGridEvent += LoadData; LoadData(); } #region 加载DataGrid数据 private async void LoadData() { await Task.Run(async () => { 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, Amount = x.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, Amount = item.Amount }); } })); } #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 /// /// 查询事件 /// public RelayCommand QueryCommand { get; set; } #region 查询 /// /// 查询 /// private async Task Query(object obj) { List list; try { LogoIdentifyDataGrid.Clear(); var result = (StatisticModel)obj; if (string.IsNullOrEmpty(result.BeginTime) && string.IsNullOrEmpty(result.EndTime)) { list = await logoIdentifyService.QueryAllByTime(null, null); } else if (string.IsNullOrEmpty(result.BeginTime)) { list = await logoIdentifyService.QueryAllByTime(null, result.EndTime); } else if (string.IsNullOrEmpty(result.EndTime)) { list = await logoIdentifyService.QueryAllByTime(result.BeginTime, null); } else { DateTime theBeginTime = Convert.ToDateTime(result.BeginTime); DateTime theEndTime = Convert.ToDateTime(result.EndTime); if (theBeginTime > theEndTime) { MessageBox.Show("结束时间要大于开始时间!"); return; } list = await logoIdentifyService.QueryAllByTime(result.BeginTime, result.EndTime); } 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 } }