using Cowain.Bake.BLL; using Cowain.Bake.Common; using Cowain.Bake.Model; using Prism.Regions; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using Unity; using Prism.Commands; using System.Windows; using HandyControl.Controls; using Cowain.Bake.UI.CsvMap; namespace Cowain.Bake.UI.DataQuery.ViewModels { public class IncomingCellInfoViewModel : ViewModelBase { private DateTime _startTime = DateTime.Now.AddDays(-1); public DateTime StartDatetime { get { return _startTime; } set { SetProperty(ref _startTime, DateTime.Parse(value.ToString("yyyy-MM-dd HH:mm:ss"))); } } private DateTime _endTime = DateTime.Now.AddHours(1); public DateTime EndDatetime { get { return _endTime; } set { SetProperty(ref _endTime, DateTime.Parse(value.ToString("yyyy-MM-dd HH:mm:ss"))); } } List batteryList = new List(); public IncomingCellInfoViewModel(IUnityContainer unityContainer, IRegionManager regionManager) : base(unityContainer, regionManager) { this.PageTitle = "来料查询"; } private ObservableCollection cellList; public ObservableCollection CellList { get => cellList ?? (cellList = new ObservableCollection()); set { SetProperty(ref cellList, value); } } public void AsyncRefreshTask() { Application.Current?.Dispatcher?.Invoke(new Action(() => { Refresh(); })); } public async override void Refresh() { CellList.Clear(); batteryList.Clear(); batteryList = await System.Threading.Tasks.Task.Run(() => { var cellInfoService = _unityContainer.Resolve(); //从数据库查询任务 return cellInfoService.GetIncomingCell(); }); batteryList.ForEach(x => CellList.Add(x)); } public DelegateCommand DeleteCommand => new DelegateCommand((x) => { var result = HandyControl.Controls.MessageBox.Ask($@"确定删除?", "操作提示"); if (result == System.Windows.MessageBoxResult.Cancel) { return; } var t = x as TBatteryInfo; if (t != null) { int delCount = _unityContainer.Resolve().Delete(t); cellList.Remove(t); if (delCount > 0) { Growl.Success("删除成功!"); } else { Growl.Fatal("删除失败!"); } } else { Growl.Fatal("删除失败!"); } }); public DelegateCommand ExportCommand => new DelegateCommand((x) => { if (batteryList.Count == 0) { HandyControl.Controls.MessageBox.Show("没有数据!"); return; } Common.Core.CSVHelper.WriteMap(batteryList); }); public DelegateCommand QueryCommand => new DelegateCommand((x) => { CellList.Clear(); batteryList.Clear(); if ((EndDatetime - StartDatetime).TotalDays > 5) { HandyControl.Controls.MessageBox.Warning("查询时间差在 5 天以内!"); return; } var cellService = _unityContainer.Resolve(); if (string.IsNullOrWhiteSpace(Code)) { batteryList = cellService.QueryIncomingCell(StartDatetime, EndDatetime); } else { batteryList = cellService.QueryIncomingCellByCode(Code); } if (0 != batteryList.Count) { batteryList.ForEach(item => CellList.Add(item)); Growl.Success("查询完成!"); } else { Growl.Success("没有数据!"); } }); } }