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 Unity; using Prism.Commands; using System.Windows; using HandyControl.Controls; using Cowain.Bake.UI.CsvMap; namespace Cowain.Bake.UI.DataQuery.ViewModels { public class PalletInfoViewModel: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"))); } } public PalletInfoViewModel(IUnityContainer unityContainer, IRegionManager regionManager) : base(unityContainer, regionManager) { this.PageTitle = "托盘追踪"; } private ObservableCollection palletList; public ObservableCollection PalletList { get => palletList ?? (palletList = new ObservableCollection()); set { SetProperty(ref palletList, value); } } List SaveList = new List(); public void AsyncRefreshTask() { Application.Current?.Dispatcher?.Invoke(new Action(() => { Refresh(); })); } public async override void Refresh() { PalletList.Clear(); SaveList.Clear(); SaveList = await System.Threading.Tasks.Task.Run(() => { //从数据库查询任务 return _unityContainer.Resolve().GetAll(); }); SaveList.ForEach(x => PalletList.Add(x)); } public DelegateCommand ExportCommand => new DelegateCommand((x) => { if (SaveList.Count == 0) { HandyControl.Controls.MessageBox.Show("没有数据!"); return; } Common.Core.CSVHelper.WriteMap(SaveList); //Common.Core.CSVHelper.WriteDataTableToCsv(SaveList, new List { "Id","PalletCode", "PalletStatus", // "BatteryQty", "BakingCount","BakingPosition", "ScanTime", "LoadingOverTime","InStoveTime","BakingBeginTime","BakingOverTime","OutStoveTime","JobNum"}, // new List { "序号", "托盘条码", "托盘状态", "电芯数量", "烘烤次数", "烘烤位置", "扫码时间", "上料完成时间", "入炉时间", "开始烘烤时间", "烘烤完成时间", "出炉时间", "工单号"}); }); public DelegateCommand QueryCommand => new DelegateCommand((x) => { PalletList.Clear(); SaveList.Clear(); if ((EndDatetime - StartDatetime).TotalDays > 30) { HandyControl.Controls.MessageBox.Warning("查询时间差在 30 天以内!"); return; } var palletInfoService = _unityContainer.Resolve(); SaveList = palletInfoService.Query(StartDatetime, EndDatetime, Code); if (0 != SaveList.Count) { SaveList.ForEach(item => PalletList.Add(item)); Growl.Success("查询完成!"); } else { Growl.Success("没有数据!"); } }); public DelegateCommand DeleteCommand => new DelegateCommand((x) => { var result = HandyControl.Controls.MessageBox.Ask($@"确定删除?", "操作提示"); if (result == System.Windows.MessageBoxResult.Cancel) { return; } var t = x as TPalletInfo; if (t != null) { int delCount = _unityContainer.Resolve().Delete(t); palletList.Remove(t); if (delCount > 0) { Growl.Success("删除成功!"); } else { Growl.Fatal("删除失败!"); } } else { Growl.Fatal("删除失败!"); } }); } }