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.

175 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()
{
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()
}).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<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
/// <summary>
/// 查询事件
/// </summary>
public RelayCommand<object> QueryCommand { get; set; }
#region 查询
/// <summary>
/// 查询
/// </summary>
private async Task Query(object obj)
{
List<LogoIdentify> 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
}
}