366 lines
12 KiB
C#
366 lines
12 KiB
C#
|
|
using Cowain.Bake.BLL;
|
|||
|
|
using Cowain.Bake.Common;
|
|||
|
|
using Cowain.Bake.Common.Core;
|
|||
|
|
using Cowain.Bake.UI.CommonView.Views;
|
|||
|
|
using LiveCharts;
|
|||
|
|
using LiveCharts.Wpf;
|
|||
|
|
using Prism.Commands;
|
|||
|
|
using Prism.Regions;
|
|||
|
|
using System;
|
|||
|
|
using System.Collections.ObjectModel;
|
|||
|
|
using System.Data;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
using System.Windows.Media;
|
|||
|
|
using Unity;
|
|||
|
|
|
|||
|
|
namespace Cowain.Bake.UI.DataQuery.ViewModels
|
|||
|
|
{
|
|||
|
|
public enum EChartType
|
|||
|
|
{
|
|||
|
|
Temp = 1,
|
|||
|
|
Vacuum = 2,
|
|||
|
|
PID = 3
|
|||
|
|
}
|
|||
|
|
public class DemoDataModel
|
|||
|
|
{
|
|||
|
|
public int Index { get; set; }
|
|||
|
|
|
|||
|
|
public string Name { get; set; } = string.Empty;
|
|||
|
|
|
|||
|
|
public bool IsSelected { get; set; }
|
|||
|
|
}
|
|||
|
|
public class CurveViewModel : ViewModelBase
|
|||
|
|
{
|
|||
|
|
//private DataTable _dtChartTemp;
|
|||
|
|
private DataTable _dtTemp;
|
|||
|
|
|
|||
|
|
private ObservableCollection<DemoDataModel> _dataList;
|
|||
|
|
public ObservableCollection<DemoDataModel> DataList
|
|||
|
|
{
|
|||
|
|
get => _dataList;
|
|||
|
|
set => SetProperty(ref _dataList, value);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public DataTable DtTemp
|
|||
|
|
{
|
|||
|
|
get => _dtTemp;
|
|||
|
|
set => SetProperty(ref _dtTemp, value);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private DataTable _dtPID;
|
|||
|
|
public DataTable DtPID
|
|||
|
|
{
|
|||
|
|
get => _dtPID;
|
|||
|
|
set => SetProperty(ref _dtPID, value);
|
|||
|
|
}
|
|||
|
|
public ChartValues<string> TimeLine { get; set; }
|
|||
|
|
public string XName { get; set; }
|
|||
|
|
public string YName { get; set; }
|
|||
|
|
public SeriesCollection SeriesCollection { get; set; }
|
|||
|
|
private int _stepId = 1;
|
|||
|
|
public int StepId
|
|||
|
|
{
|
|||
|
|
get => _stepId;
|
|||
|
|
set => SetProperty(ref _stepId, value);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private string _selectOneItem;
|
|||
|
|
public string SelectOneItem
|
|||
|
|
{
|
|||
|
|
get => _selectOneItem;
|
|||
|
|
set => SetProperty(ref _selectOneItem, value);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private DateTime _curveStartDateTime = DateTime.Now.AddHours(-23);
|
|||
|
|
public DateTime CurveStartDateTime
|
|||
|
|
{
|
|||
|
|
get { return _curveStartDateTime; }
|
|||
|
|
set { SetProperty(ref _curveStartDateTime, DateTime.Parse(value.ToString("yyyy-MM-dd HH:mm:ss"))); }
|
|||
|
|
}
|
|||
|
|
private DateTime _curveEndDateTime = DateTime.Now;
|
|||
|
|
public DateTime CurveEndDateTime
|
|||
|
|
{
|
|||
|
|
get { return _curveEndDateTime; }
|
|||
|
|
set { SetProperty(ref _curveEndDateTime, DateTime.Parse(value.ToString("yyyy-MM-dd HH:mm:ss"))); }
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
public CurveViewModel(IUnityContainer unityContainer, IRegionManager regionManager) : base(unityContainer, regionManager)
|
|||
|
|
{
|
|||
|
|
DtTemp = null;
|
|||
|
|
DtPID = null;;
|
|||
|
|
DataList = new ObservableCollection<DemoDataModel>();
|
|||
|
|
|
|||
|
|
SetCheckCombox();
|
|||
|
|
TimeLine = new ChartValues<string>();
|
|||
|
|
SeriesCollection = new SeriesCollection();
|
|||
|
|
this.PageTitle = "曲线图表";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void SetCheckCombox()
|
|||
|
|
{
|
|||
|
|
for (int i = 1; i <= 8; i++)
|
|||
|
|
{
|
|||
|
|
DataList.Add(new DemoDataModel()
|
|||
|
|
{
|
|||
|
|
Index = i,
|
|||
|
|
Name = "温度" + i.ToString(),
|
|||
|
|
IsSelected = true,
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void GetTempChartData(DataTable table)
|
|||
|
|
{
|
|||
|
|
for (int i = 2; i < table.Columns.Count; i++)
|
|||
|
|
{
|
|||
|
|
var columnName = table.Columns[i].ColumnName;
|
|||
|
|
|
|||
|
|
var model = DataList.Where(x => x.IsSelected && x.Name == columnName).FirstOrDefault();
|
|||
|
|
if (null == model)
|
|||
|
|
{
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
ChartValues<double> chartValues = new ChartValues<double>();
|
|||
|
|
table.AsEnumerable().Select(item => item.Field<double>(columnName)).ToList().ForEach(item => chartValues.Add(item));
|
|||
|
|
|
|||
|
|
var lineSeries = new LineSeries
|
|||
|
|
{
|
|||
|
|
Title = columnName,
|
|||
|
|
Values = chartValues,
|
|||
|
|
StrokeThickness = 1,
|
|||
|
|
LineSmoothness = 0,
|
|||
|
|
Fill = Brushes.Transparent,
|
|||
|
|
PointGeometry = null // 不画圆点
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
SeriesCollection.Add(lineSeries);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void GetVacuumChartData(DataTable table)
|
|||
|
|
{
|
|||
|
|
var columnName = table.Columns[2].ColumnName;
|
|||
|
|
ChartValues<double> chartValues = new ChartValues<double>();
|
|||
|
|
table.AsEnumerable().Select(item => item.Field<float>(columnName)).ToList().ForEach(item => chartValues.Add(Math.Round(item,2)));
|
|||
|
|
|
|||
|
|
var lineSeries = new LineSeries
|
|||
|
|
{
|
|||
|
|
Title = columnName,
|
|||
|
|
Values = chartValues,
|
|||
|
|
StrokeThickness = 1,
|
|||
|
|
LineSmoothness = 0,
|
|||
|
|
Fill = Brushes.Transparent,
|
|||
|
|
PointGeometry = null // 不画圆点
|
|||
|
|
};
|
|||
|
|
SeriesCollection.Add(lineSeries);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void GetPIDChartData(DataTable table)
|
|||
|
|
{
|
|||
|
|
for (int i = 2; i < table.Columns.Count; i++)
|
|||
|
|
{
|
|||
|
|
var columnName = table.Columns[i].ColumnName;
|
|||
|
|
ChartValues<double> chartValues = new ChartValues<double>();
|
|||
|
|
table.AsEnumerable().Select(item => item.Field<double>(columnName)).ToList().ForEach(item => chartValues.Add(item));
|
|||
|
|
|
|||
|
|
var lineSeries = new LineSeries
|
|||
|
|
{
|
|||
|
|
Title = columnName,
|
|||
|
|
Values = chartValues,
|
|||
|
|
StrokeThickness = 1,
|
|||
|
|
LineSmoothness = 0,
|
|||
|
|
Fill = Brushes.Transparent,
|
|||
|
|
PointGeometry = null // 不画圆点
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
SeriesCollection.Add(lineSeries);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void UpdateSeries(DataTable table, EChartType chartType)
|
|||
|
|
{
|
|||
|
|
if (null == table
|
|||
|
|
|| 0 == table.Rows.Count)
|
|||
|
|
{
|
|||
|
|
HandyControl.Controls.Growl.Warning("请点【查询】数据,或查询数据为空!");
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
StepId = table.Rows.Count / 10;
|
|||
|
|
System.Threading.SynchronizationContext.SetSynchronizationContext(new
|
|||
|
|
System.Windows.Threading.DispatcherSynchronizationContext(System.Windows.Application.Current.Dispatcher));
|
|||
|
|
System.Threading.SynchronizationContext.Current.Post(p1 =>
|
|||
|
|
{
|
|||
|
|
TimeLine.Clear();
|
|||
|
|
SeriesCollection.Clear();
|
|||
|
|
|
|||
|
|
if (chartType == EChartType.Vacuum)
|
|||
|
|
{
|
|||
|
|
GetVacuumChartData(table);
|
|||
|
|
}
|
|||
|
|
else if (chartType == EChartType.Temp)
|
|||
|
|
{
|
|||
|
|
GetTempChartData(table);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
GetPIDChartData(table);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
SetXAxis(table);
|
|||
|
|
}, null);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void SetXAxis(DataTable table)
|
|||
|
|
{
|
|||
|
|
XName = table.Columns[0].ColumnName;
|
|||
|
|
table.AsEnumerable().Select(item => item.Field<string>(XName)).ToList().ForEach(item => TimeLine.Add(item));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public DelegateCommand QueryTempCommand { get => new DelegateCommand(async () => await QueryTemp()); }
|
|||
|
|
|
|||
|
|
public async Task QueryTemp()
|
|||
|
|
{
|
|||
|
|
DtTemp = null;
|
|||
|
|
if (!IsAccord("温度曲线(℃)"))
|
|||
|
|
{
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
ProgressBarView dlg = new ProgressBarView();
|
|||
|
|
dlg.Show();
|
|||
|
|
string sql = $@"CALL ProcGetTemperatureAndPressureData('{CurveStartDateTime.ToString("yyyy-MM-dd HH:mm:ss")}'
|
|||
|
|
,'{CurveEndDateTime.ToString("yyyy-MM-dd HH:mm:ss")}','{Code}')";
|
|||
|
|
await Task.Run(new Action(() =>
|
|||
|
|
{
|
|||
|
|
DtTemp = _unityContainer.Resolve<StoveSctualPatrolService>().GetDataTable(sql);
|
|||
|
|
}));
|
|||
|
|
dlg.Close();
|
|||
|
|
Tip(DtTemp);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void Tip(DataTable dt)
|
|||
|
|
{
|
|||
|
|
if (0 == dt.Rows.Count)
|
|||
|
|
{
|
|||
|
|
HandyControl.Controls.Growl.Warning("查询数据为空!");
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
HandyControl.Controls.Growl.Success("查询数据成功!");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
public DelegateCommand ChartTempCommand => new DelegateCommand(async() =>
|
|||
|
|
{
|
|||
|
|
await WaitChartTemp(DtTemp, EChartType.Temp);
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
|
|||
|
|
public async Task AsyncChart(DataTable table, EChartType chartType)
|
|||
|
|
{
|
|||
|
|
Task queryTask = Task.Run(() => UpdateSeries(table, chartType));
|
|||
|
|
var timeouttask = Task.Delay(30 * 1000);
|
|||
|
|
|
|||
|
|
var completedTask = await Task.WhenAny(queryTask, timeouttask);
|
|||
|
|
if (completedTask == timeouttask)
|
|||
|
|
{
|
|||
|
|
HandyControl.Controls.MessageBox.Warning("查询超时!");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public DelegateCommand ChartVacuumCommand => new DelegateCommand(async() =>
|
|||
|
|
{
|
|||
|
|
if (!IsAccord("真空压力"))
|
|||
|
|
{
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
await WaitChartTemp(DtTemp, EChartType.Vacuum);
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
//public DelegateCommand QueryPIDCommand { get => new DelegateCommand(QueryPID); }
|
|||
|
|
public DelegateCommand QueryPIDCommand { get => new DelegateCommand(async() => await QueryPID()); }
|
|||
|
|
public async Task QueryPID()
|
|||
|
|
{
|
|||
|
|
if (!IsAccord("PID"))
|
|||
|
|
{
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
ProgressBarView dlg = new ProgressBarView();
|
|||
|
|
dlg.Show();
|
|||
|
|
string sql = $@"CALL ProcGetPidData ('{CurveStartDateTime.ToString("yyyy-MM-dd HH:mm:ss")}'
|
|||
|
|
,'{CurveEndDateTime.ToString("yyyy-MM-dd HH:mm:ss")}','{Code}')";
|
|||
|
|
|
|||
|
|
await Task.Run(new Action(() =>
|
|||
|
|
{
|
|||
|
|
DtPID = _unityContainer.Resolve<StoveSctualPatrolService>().GetDataTable(sql);
|
|||
|
|
}));
|
|||
|
|
dlg.Close();
|
|||
|
|
Tip(DtPID);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public DelegateCommand ChartPIDCommand => new DelegateCommand(async() =>
|
|||
|
|
{
|
|||
|
|
await WaitChartTemp(DtPID, EChartType.PID);
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
public async Task WaitChartTemp(DataTable table, EChartType eChart)
|
|||
|
|
{
|
|||
|
|
ProgressBarView dlg = new ProgressBarView();
|
|||
|
|
dlg.Show();
|
|||
|
|
await AsyncChart(table, eChart);
|
|||
|
|
dlg.Close();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public DelegateCommand<object> ExportTempCommand => new DelegateCommand<object>((x) =>
|
|||
|
|
{
|
|||
|
|
if (null == DtTemp)
|
|||
|
|
{
|
|||
|
|
HandyControl.Controls.MessageBox.Error("温度数据为空!", "操作提示");
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
CSVHelper.WriteDataTableToCsv(DtTemp);
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
public DelegateCommand<object> ExportPIDCommand => new DelegateCommand<object>((x) =>
|
|||
|
|
{
|
|||
|
|
if (null == DtPID)
|
|||
|
|
{
|
|||
|
|
HandyControl.Controls.MessageBox.Error("温度数据为空!", "操作提示");
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
CSVHelper.WriteDataTableToCsv(DtPID);
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
bool IsAccord(string headName)
|
|||
|
|
{
|
|||
|
|
YName = headName;
|
|||
|
|
if (string.IsNullOrWhiteSpace(Code))
|
|||
|
|
{
|
|||
|
|
HandyControl.Controls.MessageBox.Error("请输入条码", "操作提示");
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var p = _unityContainer.Resolve<PalletInfoService>().GetPalletInfo(Code);
|
|||
|
|
if (p == null)
|
|||
|
|
{
|
|||
|
|
HandyControl.Controls.MessageBox.Error("没有该托盘码", "操作提示");
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
TimeSpan timeSpan = CurveEndDateTime - CurveStartDateTime;
|
|||
|
|
if (timeSpan.TotalHours > 24)
|
|||
|
|
{
|
|||
|
|
HandyControl.Controls.MessageBox.Error("请选择时间区间小于24小时的数据", "操作提示");
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|