using Admin.Core.IService; using Admin.Core.Model; using Admin.Core.Service; using Aucma.Core.CodeBinding.Models; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using log4net; using Microsoft.Extensions.DependencyInjection; 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.Xml.Linq; namespace Aucma.Core.CodeBinding.ViewModels { public partial class StatisticsPageViewModel : ObservableObject { private static readonly log4net.ILog log = LogManager.GetLogger(typeof(StatisticsPageViewModel)); private readonly ICodeBindingRecordServices? _codeBindingRecordServices; public StatisticsPageViewModel() { _codeBindingRecordServices = App.ServiceProvider.GetService(); LoadData(); } List materialComplateInfos = new List(); #region 加载DataGrid数据 private async void LoadData() { List records = null; records = await _codeBindingRecordServices.QueryAsync(x => x.BoxCode != null, "RECORD_TIME1 desc"); if (records != null) { foreach (CodeBindingRecord record in records) { Application.Current.Dispatcher.Invoke(() => { ListItems.Add(new ReaderInfo() { No = record.ObjId, BoxCode = record.BoxCode, ProductCode = record.ProductCode, BindingResult = record.BindingResult, RecordTime = record.RecordTime1.ToString() }); }); } } add(); } private async void add() { //await Task.Delay(5000); //ListItems.Add(new ReaderInfo() { No = 2, BoxCode = "42423444", BoxName = "SC-箱体11111", ProductCode = "8659452123", BindingResult = "成功", RecordTime = System.DateTime.Now.ToString() }); //ListItems.Add(new ReaderInfo() { No = 3, BoxCode = "42423444", BoxName = "SC-箱体11111", ProductCode = "8659452123", BindingResult = "成功", RecordTime = System.DateTime.Now.ToString() }); //ListItems.Add(new ReaderInfo() { No = 4, BoxCode = "42423444", BoxName = "SC-箱体11111", ProductCode = "8659452123", BindingResult = "成功", RecordTime = System.DateTime.Now.ToString() }); //ListItems.Add(new ReaderInfo() { No = 5, BoxCode = "42423444", BoxName = "SC-箱体11111", ProductCode = "8659452123", BindingResult = "成功", RecordTime = System.DateTime.Now.ToString() }); //ListItems.Add(new ReaderInfo() { No = 6, BoxCode = "42423444", BoxName = "SC-箱体11111", ProductCode = "8659452123", BindingResult = "成功", RecordTime = System.DateTime.Now.ToString() }); //ListItems.Add(new ReaderInfo() { No = 7, BoxCode = "42423444", BoxName = "SC-箱体11111", ProductCode = "8659452123", BindingResult = "成功", RecordTime = System.DateTime.Now.ToString() }); //ListItems.Add(new ReaderInfo() { No = 8, BoxCode = "42423444", BoxName = "SC-箱体11111", ProductCode = "8659452123", BindingResult = "成功", RecordTime = System.DateTime.Now.ToString() }); ListItems.Add(new ReaderInfo() { No = 1, BoxCode = "42423444", ProductCode = "8659452123", BindingResult = "成功", RecordTime = System.DateTime.Now.ToString() }); //ListItems.Add(new ReaderInfo() { No = 9, BoxCode = "42423444", BoxName = "SC-箱体11111", ProductCode = "8659452123", BindingResult = "成功", RecordTime = System.DateTime.Now.ToString() }); //ListItems.Add(new ReaderInfo() { No = 10, BoxCode = "42423444",BoxName = "SC-箱体11111", ProductCode = "8659452123", BindingResult = "成功", RecordTime = System.DateTime.Now.ToString() }); } #endregion #region 初始化datagrid private ObservableCollection listItems = new ObservableCollection(); public ObservableCollection ListItems { get { return listItems; } set { listItems = value; OnPropertyChanged();//属性通知 } } #endregion #region 查询 /// /// 查询 /// [RelayCommand] private async Task ExecQueryAsync(object obj) { List list; var result = (StatisticModel)obj; if (string.IsNullOrEmpty(result.BeginTime) && string.IsNullOrEmpty(result.EndTime)) { list = await _codeBindingRecordServices.QueryAllByTime(null,null); }else if (string.IsNullOrEmpty(result.BeginTime)) { list = await _codeBindingRecordServices.QueryAllByTime(result.BeginTime, null); }else if (string.IsNullOrEmpty(result.EndTime)) { list = await _codeBindingRecordServices.QueryAllByTime(null, result.EndTime); } else { DateTime theBeginTime = Convert.ToDateTime(result.BeginTime); DateTime theEndTime = Convert.ToDateTime(result.EndTime); if (theBeginTime > theEndTime) { MessageBox.Show("结束时间要大于开始时间!"); return; } list = await _codeBindingRecordServices.QueryAllByTime(result.BeginTime, result.EndTime); } //DateTime startTime = Convert.ToDateTime(theBeginTime.ToString("yyyy-MM-dd 00:00:00")); //DateTime endTime = Convert.ToDateTime(theEndTime.ToString("yyyy-MM-dd 23:59:59")); if (list != null) { System.Windows.Application.Current.Dispatcher.Invoke((Action)(async () => { ListItems.Clear(); // var list = await _codeBindingRecordServices.QueryAsync(d => d.BeginTime >= startTime && d.EndTime <= endTime); foreach(CodeBindingRecord item in list) { ListItems.Add(new ReaderInfo() { No = item.ObjId, BoxCode = item.BoxCode, BoxName = item.BoxName, ProductCode = item.ProductCode, BindingResult = item.BindingResult, RecordTime = item.RecordTime1.ToString() }); } })); } } #endregion } }