You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
5.7 KiB
C#

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<ILogoIdentifyService>();
QueryCommand = new RelayCommand<object>(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<LogoIdentify> 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);
});
}
/// <summary>
/// 型号统计
/// </summary>
public void ModelList(List<LogoIdentify> 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<LogoIdentify> _LogoIdentifyDataGrid = new ObservableCollection<LogoIdentify>();
public ObservableCollection<LogoIdentify> LogoIdentifyDataGrid
{
get { return _LogoIdentifyDataGrid; }
set
{
_LogoIdentifyDataGrid = value;
RaisePropertyChanged();//属性通知
}
}
private ObservableCollection<ModelCount> _ModelDataGrid = new ObservableCollection<ModelCount>();
public ObservableCollection<ModelCount> 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();//属性通知
}
}
}
/// <summary>
/// 查询事件
/// </summary>
public RelayCommand<object> QueryCommand { get; set; }
#region 查询
private async Task Query(object obj)
{
List<LogoIdentify> 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
}
}