using OxyPlot; using OxyPlot.Axes; using OxyPlot.Legends; using OxyPlot.Series; using Prism.Commands; using Prism.Ioc; using Prism.Mvvm; using System; using System.Collections.Generic; using System.Linq; using System.Windows.Documents; using TeamAAS_VP.Core; using TeamAAS_VP.Data; using TeamAAS_VP.Interfaces; using TeamAAS_VP.Models; using TeamAAS_VP.Resources.Languages; using TeamAAS_VP.Services; using static MaterialDesignThemes.Wpf.Theme.ToolBar; namespace TeamAAS_VP.ViewModels.Statistics { public class ProductionStatementViewModel : BindableBase { IContainerProvider _container; ISystemDatabaseService _systemDatabaseService; IProductService _productService; Management management; #region 属性 private Int64 _TotalCount; /// /// 总产能 /// public Int64 TotalCount { get { return _TotalCount; } set { SetProperty(ref _TotalCount, value); } } private Int64 _MonthCount; /// /// 月产能 /// public Int64 MonthCount { get { return _MonthCount; } set { SetProperty(ref _MonthCount, value); } } private Int64 _WeekCount; /// /// 周产能 /// public Int64 WeekCount { get { return _WeekCount; } set { SetProperty(ref _WeekCount, value); } } private Int64 _DayCount; /// /// 日产能 /// public Int64 DayCount { get { return _DayCount; } set { SetProperty(ref _DayCount, value); } } private PlotModel _YieldChartModel; /// /// 良率图表Model的mvvm属性,可通知UI更新 /// public PlotModel YieldChartModel { get { return _YieldChartModel; } set { SetProperty(ref _YieldChartModel, value); } } private PlotModel _MonthChartModel; /// /// 月产能 /// public PlotModel MonthChartModel { get { return _MonthChartModel; } set { SetProperty(ref _MonthChartModel, value); } } private PlotModel _WeekChartModel; /// /// 周产能 /// public PlotModel WeekChartModel { get { return _WeekChartModel; } set { SetProperty(ref _WeekChartModel, value); } } private PlotModel _DayChartModel; /// /// 日产能 /// public PlotModel DayChartModel { get { return _DayChartModel; } set { SetProperty(ref _DayChartModel, value); } } #endregion #region 命令 private DelegateCommand _LoadedCommand; public DelegateCommand LoadedCommand => _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand)); #endregion #region 事件 #endregion public ProductionStatementViewModel(IContainerProvider container, ISystemDatabaseService systemDatabaseService, IProductService productService) { _container = container; _systemDatabaseService = systemDatabaseService; _productService = productService; } #region 方法 async void ExecuteLoadedCommand() { var currentproduct = _productService.GetCurrentProduct(); if (currentproduct == null) return; await App.Current.Dispatcher.InvokeAsync(new Action(async () => { YieldChartModel = CreateYieldChartModel(await _systemDatabaseService.GetProductionByCategoriesAsync(currentproduct.Name)); TotalCount = await _systemDatabaseService.GetOverallProductionAsync(currentproduct.Name); MonthCount = await _systemDatabaseService.GetCurrentMonthProductionAsync(currentproduct.Name); WeekCount = await _systemDatabaseService.GetCurrentWeekProductionAsync(currentproduct.Name); DayCount = await _systemDatabaseService.GetTodayProductionAsync(currentproduct.Name); MonthChartModel = CreateMonth(await _systemDatabaseService.GetMonthlyProductionByDayAndCategoryAsync(currentproduct.Name)); WeekChartModel = CreateWeek(await _systemDatabaseService.GetWeeklyProductionByDayAndCategoryAsync(currentproduct.Name)); DayChartModel = CreateDay(await _systemDatabaseService.GetDailyProductionByHourAndCategoryAsync(currentproduct.Name)); })); } /// /// 创建产量图表模型 /// /// private PlotModel CreateYieldChartModel(Dictionary Items) { List PieSlices = new List(); PieSlices = new List(); foreach (var item in Items) { PieSlices.Add(new PieSlice(item.Key, (double)item.Value)); } var model = new PlotModel { Title = Lang.产能统计 }; var series = new PieSeries { StrokeThickness = 1.0, InsideLabelPosition = 0.5, AngleSpan = 360, StartAngle = 0 }; foreach (var slice in PieSlices) { series.Slices.Add(slice); } model.Series.Add(series); return model; } /// /// 创建月统计模型 /// /// /// private PlotModel CreateMonth(Dictionary> Items) { var model = new PlotModel { Title = Lang.当前月产能统计 }; // 添加图例说明 model.Legends.Add(new Legend { LegendPlacement = LegendPlacement.Outside, LegendPosition = LegendPosition.RightMiddle, LegendOrientation = LegendOrientation.Vertical, LegendBorderThickness = 0, LegendTextColor = OxyColors.LightGray }); ; // 定义第一个Y轴y1,显示数量 var ay1 = new LinearAxis() { Key = "y1", Title = Lang.数量, TitlePosition = 1, Minimum = 0, Position = AxisPosition.Left, IsZoomEnabled = false, IsPanEnabled = false, TickStyle = TickStyle.None, MinorTickSize = 0, MajorGridlineStyle = LineStyle.Solid, MajorGridlineColor = OxyColor.Parse("#F3F3F3"), }; var now = DateTime.UtcNow; var monthStart = DateTimeAxis.ToDouble(new DateTime(now.Year, now.Month, 1)); var temp = new DateTime(now.Year, now.Month, 1); var monthEnd = DateTimeAxis.ToDouble(temp.AddMonths(1).AddDays(-1)); // 定义X轴为日期轴 //var minValue = DateTimeAxis.ToDouble(Items.Keys.ElementAt(0)); //var maxValue = DateTimeAxis.ToDouble(Items.Keys.ElementAt(Items.Keys.Count - 1)); var ax = new DateTimeAxis() { Minimum = monthStart, Maximum = monthEnd, StringFormat = $"dd{Lang.日}", MajorStep = 1, Position = AxisPosition.Bottom, Angle = 45, IsZoomEnabled = false, IsPanEnabled = false, TickStyle = TickStyle.None, MinorTickSize = 0, MajorGridlineStyle = LineStyle.Solid, MajorGridlineColor = OxyColor.Parse("#F3F3F3"), }; model.Axes.Add(ay1); model.Axes.Add(ax); if (Items.Count <= 0) { return model; } foreach (var category in Items.First().Value.Keys) { var barSeries = new LinearBarSeries { Title = category }; barSeries.YAxisKey = "y1"; barSeries.BarWidth = 10; // 点击时弹出的label内容 barSeries.TrackerFormatString = $"{0}\r\n{2:dd}{Lang.日}: {4:0}"; foreach (var item in Items) { if (item.Value.ContainsKey(category)) { barSeries.Points.Add(new OxyPlot.DataPoint(DateTimeAxis.ToDouble(item.Key), (double)(item.Value[category]))); } else { barSeries.Points.Add(new OxyPlot.DataPoint(DateTimeAxis.ToDouble(item.Key), 0)); } } model.Series.Add(barSeries); } var Series = new LineSeries { Title = Lang.总产量 }; Series.YAxisKey = "y1"; Series.TrackerFormatString = $"{0}\r\n{2:dd}{Lang.日}: {4:0}"; foreach (var item in Items) { Int64 count = 0; foreach (var item1 in item.Value) { count += item1.Value; } Series.Points.Add(new OxyPlot.DataPoint(DateTimeAxis.ToDouble(item.Key), (double)count)); } model.Series.Add(Series); return model; } /// /// 创建周统计模型 /// /// /// private PlotModel CreateWeek(Dictionary> Items) { var model = new PlotModel { Title = Lang.当前周产能统计 }; // 添加图例说明 model.Legends.Add(new Legend { LegendPlacement = LegendPlacement.Outside, LegendPosition = LegendPosition.RightMiddle, LegendOrientation = LegendOrientation.Vertical, LegendBorderThickness = 0, LegendTextColor = OxyColors.LightGray }); // 定义第一个Y轴y1,显示数量 var ay1 = new LinearAxis() { Key = "y1", Title = Lang.数量, Minimum = 0, Position = AxisPosition.Left, IsZoomEnabled = false, IsPanEnabled = false, TickStyle = TickStyle.None, MinorTickSize = 0, MajorGridlineStyle = LineStyle.Solid, MajorGridlineColor = OxyColor.Parse("#F3F3F3"), }; // 定义X轴为日期轴 var ax = new LinearAxis() { Minimum = 0, Maximum = 7.5, MajorStep = 1, Position = AxisPosition.Bottom, StringFormat = $"{Lang.周} 0", Angle = 0, IsZoomEnabled = false, IsPanEnabled = false, TickStyle = TickStyle.None, MinorTickSize = 0, MajorGridlineStyle = LineStyle.Solid, MajorGridlineColor = OxyColor.Parse("#F3F3F3"), }; model.Axes.Add(ay1); model.Axes.Add(ax); if (Items.Count <= 0) { return model; } foreach (var category in Items.First().Value.Keys) { var barSeries = new LinearBarSeries { Title = category }; barSeries.YAxisKey = "y1"; barSeries.BarWidth = 10; // 点击时弹出的label内容 barSeries.TrackerFormatString = $"{0}\r\n{Lang.周}{2:0}: {4:0}"; foreach (var item in Items) { if (item.Value.ContainsKey(category)) { barSeries.Points.Add(new OxyPlot.DataPoint(DateTimeAxis.ToDouble(item.Key), (double)(item.Value[category]))); } else { barSeries.Points.Add(new OxyPlot.DataPoint(DateTimeAxis.ToDouble(item.Key), 0)); } } model.Series.Add(barSeries); } var Series = new LineSeries { Title = Lang.当前周产能统计 }; Series.YAxisKey = "y1"; Series.TrackerFormatString = $"{0}\r\n{2:dd}{Lang.日}: {4:0}"; foreach (var item in Items) { Int64 count = 0; foreach (var item1 in item.Value) { count += item1.Value; } Series.Points.Add(new OxyPlot.DataPoint(DateTimeAxis.ToDouble(item.Key), (double)count)); } model.Series.Add(Series); return model; } /// /// 创建日统计模型 /// /// /// private PlotModel CreateDay(Dictionary> Items) { var model = new PlotModel { Title = Lang.当前日产能统计 }; // 添加图例说明 model.Legends.Add(new Legend { LegendPlacement = LegendPlacement.Outside, LegendPosition = LegendPosition.RightMiddle, LegendOrientation = LegendOrientation.Vertical, LegendBorderThickness = 0, LegendTextColor = OxyColors.LightGray }); // 定义第一个Y轴y1,显示数量 var ay1 = new LinearAxis() { Key = "y1", Title = $"{Lang.数量}", Minimum = 0, Position = AxisPosition.Left, IsZoomEnabled = false, IsPanEnabled = false, TickStyle = TickStyle.None, MinorTickSize = 0, MajorGridlineStyle = LineStyle.Solid, MajorGridlineColor = OxyColor.Parse("#F3F3F3"), }; // 定义X轴为小时 var ax = new LinearAxis() { Minimum = 0, Maximum = 24.5, MajorStep = 1, StringFormat = "0 H", Position = AxisPosition.Bottom, Angle = 0, IsZoomEnabled = false, IsPanEnabled = false, TickStyle = TickStyle.None, MinorTickSize = 0, MajorGridlineStyle = LineStyle.Solid, MajorGridlineColor = OxyColor.Parse("#F3F3F3"), }; model.Axes.Add(ay1); model.Axes.Add(ax); if (Items.Count<=0) { return model; } foreach (var category in Items.First().Value.Keys) { var barSeries = new LinearBarSeries { Title = category }; barSeries.YAxisKey = "y1"; barSeries.BarWidth = 10; // 点击时弹出的label内容 barSeries.TrackerFormatString = "{0}\r\n{2:0}Hours: {4:0}"; foreach (var item in Items) { barSeries.Points.Add(new OxyPlot.DataPoint(LinearAxis.ToDouble(item.Key), (double)(item.Value[category]))); } model.Series.Add(barSeries); } var Series = new LineSeries { Title = Lang.总产量 }; Series.YAxisKey = "y1"; Series.TrackerFormatString = $"{0}\r\n{2:dd}{Lang.小时}: {4:0}"; foreach (var item in Items) { Int64 count = 0; foreach (var item1 in item.Value) { count += item1.Value; } Series.Points.Add(new OxyPlot.DataPoint(DateTimeAxis.ToDouble(item.Key), (double)count)); } model.Series.Add(Series); return model; } #endregion } }