180 lines
5.8 KiB
C#
180 lines
5.8 KiB
C#
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<TBatteryNG> cellList;
|
|
public ObservableCollection<TBatteryNG> CellList
|
|
{
|
|
get => cellList ?? (cellList = new ObservableCollection<TBatteryNG>());
|
|
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<BatteryNGService>().GetAllNGCell();
|
|
});
|
|
|
|
listCell = ExtractJson(listCell);
|
|
listCell.ForEach(x => CellList.Add(x));
|
|
}
|
|
|
|
public DelegateCommand<object> ExportCommand => new DelegateCommand<object>((x) =>
|
|
{
|
|
if (0 == CellList.Count)
|
|
{
|
|
HandyControl.Controls.MessageBox.Warning("没有数据,导出失败!");
|
|
return;
|
|
}
|
|
|
|
Common.Core.CSVHelper.WriteMap<TBatteryNG, FailBatteryMap>(CellList);
|
|
//Common.Core.CSVHelper.WriteDataTableToCsv(CellList, new List<string> { "Id","BatteryCode", "PalletCode", "ChannelNo",
|
|
// "Desc", "CreateTime"}, new List<string> { "序号", "电芯条码", "托盘条码", "报警通道", "报警描述","报警时间"});
|
|
});
|
|
|
|
public DelegateCommand<object> QueryCommand => new DelegateCommand<object>((x) =>
|
|
{
|
|
CellList.Clear();
|
|
|
|
if ((EndDatetime - StartDatetime).TotalDays > 5)
|
|
{
|
|
HandyControl.Controls.MessageBox.Warning("查询时间差在 5 天以内!");
|
|
return;
|
|
}
|
|
|
|
|
|
List<TBatteryNG> queryCell = _unityContainer.Resolve<BatteryNGService>().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("没有数据!");
|
|
}
|
|
});
|
|
|
|
/// <summary>
|
|
/// JSON 字符串截取
|
|
/// </summary>
|
|
/// <param name="failBatterys"></param>
|
|
/// <returns></returns>
|
|
List<TBatteryNG> ExtractJson(List<TBatteryNG> 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<object> DeleteCommand => new DelegateCommand<object>((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<BatteryNGService>().Delete(t);
|
|
cellList.Remove(t);
|
|
if (delCount > 0)
|
|
{
|
|
Growl.Success("删除成功!");
|
|
|
|
}
|
|
else
|
|
{
|
|
Growl.Fatal("删除失败!");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Growl.Fatal("删除失败!");
|
|
}
|
|
});
|
|
}
|
|
}
|