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 Newtonsoft.Json.Linq; using Newtonsoft.Json; using Cowain.Bake.Common.Core; using Cowain.Bake.BLL; using Cowain.Bake.Model.Entity; using Cowain.Bake.Common.CsvMap; namespace Cowain.Bake.UI.DataQuery.ViewModels { public class NGCellInfoViewModel: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 NGCellInfoViewModel(IUnityContainer unityContainer, IRegionManager regionManager) : base(unityContainer, regionManager) { this.PageTitle = "烘烤NG电芯查询"; } 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(); var listCell = await System.Threading.Tasks.Task.Run(() => { //从数据库查询任务 return _unityContainer.Resolve().GetAllNGCell(); }); listCell = ExtractJson(listCell); listCell.ForEach(x => CellList.Add(x)); } public DelegateCommand ExportCommand => new DelegateCommand((x) => { if (0 == CellList.Count) { HandyControl.Controls.MessageBox.Warning("没有数据,导出失败!"); return; } Common.Core.CSVHelper.WriteMap(CellList); //Common.Core.CSVHelper.WriteDataTableToCsv(CellList, new List { "Id","BatteryCode", "PalletCode", "ChannelNo", // "Desc", "CreateTime"}, new List { "序号", "电芯条码", "托盘条码", "报警通道", "报警描述","报警时间"}); }); public DelegateCommand QueryCommand => new DelegateCommand((x) => { CellList.Clear(); if ((EndDatetime - StartDatetime).TotalDays > 5) { HandyControl.Controls.MessageBox.Warning("查询时间差在 5 天以内!"); return; } List queryCell = _unityContainer.Resolve().QueryNGCell(StartDatetime, EndDatetime, Code); //出可以直接在数据库中查询得到 SELECT `Desc`->>'$.Info.MOMMessage' cmd FROM TBatteryNG; queryCell = ExtractJson(queryCell); if (0 != queryCell.Count) { queryCell.ForEach(item => CellList.Add(item)); Growl.Success("查询完成!"); } else { Growl.Success("没有数据!"); } }); /// /// JSON 字符串截取 /// /// /// List ExtractJson(List failBatterys) { try { foreach (var item in failBatterys) { if (!IsValidJson(item.Desc)) { continue; } JObject jsonObject = JObject.Parse(item.Desc); string keyValue = (string)jsonObject["Info"]["MOMMessage"]; //为什么加try,怕没有MOMMessage item.Desc = keyValue; // 更新为Key的值 } } catch(Exception ex) { LogHelper.Instance.Info($"NG电芯,解析JSON失败:{ex.Message}"); } return failBatterys; } bool IsValidJson(string input) { try { JsonConvert.DeserializeObject(input); //JsonDocument.Parse(input); return true; } catch { return false; } } public DelegateCommand DeleteCommand => new DelegateCommand((x) => { var result = HandyControl.Controls.MessageBox.Ask($@"确定删除?", "操作提示"); if (result == System.Windows.MessageBoxResult.Cancel) { return; } var t = x as TBatteryNG; if (t != null) { int delCount = _unityContainer.Resolve().Delete(t); cellList.Remove(t); if (delCount > 0) { Growl.Success("删除成功!"); } else { Growl.Fatal("删除失败!"); } } else { Growl.Fatal("删除失败!"); } }); } }