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() { // 查询条件初始化 BeginDate = DateTime.Now.AddDays(-1); EndDate = DateTime.Now; 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(), 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, 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 { if (_EndDate != value) { _EndDate = value; RaisePropertyChanged();//属性通知 } } } /// /// 查询事件 /// public RelayCommand QueryCommand { 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 } }