From 06f6b8b9060ea0fa7b1d76ad80c66ec599e388b1 Mon Sep 17 00:00:00 2001 From: liuwf Date: Sat, 12 Oct 2024 14:06:51 +0800 Subject: [PATCH] =?UTF-8?q?add-=E6=B7=BB=E5=8A=A0=E9=A6=96=E9=A1=B5?= =?UTF-8?q?=E5=9B=BE=E8=A1=A8=E5=88=B7=E6=96=B0=E7=BB=9F=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SlnMesnac.WPF/Page/ProdMgmtPage.xaml | 2 +- SlnMesnac.WPF/ViewModel/ProdMgmtViewModel.cs | 131 +++++++++++++++---- 2 files changed, 108 insertions(+), 25 deletions(-) diff --git a/SlnMesnac.WPF/Page/ProdMgmtPage.xaml b/SlnMesnac.WPF/Page/ProdMgmtPage.xaml index fd42f39..9a0f275 100644 --- a/SlnMesnac.WPF/Page/ProdMgmtPage.xaml +++ b/SlnMesnac.WPF/Page/ProdMgmtPage.xaml @@ -176,7 +176,7 @@ - + diff --git a/SlnMesnac.WPF/ViewModel/ProdMgmtViewModel.cs b/SlnMesnac.WPF/ViewModel/ProdMgmtViewModel.cs index e85b0a4..f917700 100644 --- a/SlnMesnac.WPF/ViewModel/ProdMgmtViewModel.cs +++ b/SlnMesnac.WPF/ViewModel/ProdMgmtViewModel.cs @@ -23,6 +23,14 @@ using static System.Net.Mime.MediaTypeNames; using Application = System.Windows.Application; using LiveCharts; using System.Threading; +using SlnMesnac.Repository.service; +using LiveCharts.Defaults; +using static MaterialDesignThemes.Wpf.Theme.ToolBar; +using LiveCharts.Wpf; +using System.Windows.Media; +using System.Windows.Threading; +using Microsoft.AspNetCore.Mvc.ModelBinding; +using SlnMesnac.Config; namespace SlnMesnac.WPF.ViewModel { @@ -37,11 +45,13 @@ namespace SlnMesnac.WPF.ViewModel private readonly PalletStowBusiness? _palletStowBusiness; + private readonly IMesBaseBarcodeInfoService? _mesBaseBarcodeInfoService; + private readonly ProdCompletionBusiness? _prodCompletionBusiness; private ObservableCollection listItems = new ObservableCollection(); - + #region 参数定义 /// @@ -93,7 +103,7 @@ namespace SlnMesnac.WPF.ViewModel get { return beginTime; } set { beginTime = value; RaisePropertyChanged(nameof(BeginTime)); } } - + /// /// 计划数量 /// @@ -133,7 +143,7 @@ namespace SlnMesnac.WPF.ViewModel get { return compleRoution; } set { compleRoution = value; RaisePropertyChanged(nameof(CompleRoution)); } } - + /// /// 计划列表DataGrid /// @@ -198,13 +208,14 @@ namespace SlnMesnac.WPF.ViewModel public ProdMgmtViewModel() { - + StartProdPlanCommand = new RelayCommand(obj => StartProdPlan(obj)); StopProdPlanCommand = new RelayCommand(obj => StopProdPlan(obj)); _logger = App.ServiceProvider.GetService>(); _prodMgmtBusiness = App.ServiceProvider.GetService(); _palletStowBusiness = App.ServiceProvider.GetService(); _prodCompletionBusiness = App.ServiceProvider.GetService(); + _mesBaseBarcodeInfoService = App.ServiceProvider.GetService(); Task.Run(() => { _prodMgmtBusiness.RefreshProdPlanListEvent += RefreshPlanDataGrid; @@ -212,22 +223,33 @@ namespace SlnMesnac.WPF.ViewModel _prodMgmtBusiness.PrintMessageToListBoxEvent += PrintMessageToListBox; _prodMgmtBusiness.InitProdPlan(); }); - //Task.Run(() => - //{ - // _palletStowBusiness.PrintMessageToListBoxEvent += PrintMessageToListBox; - // _palletStowBusiness.Init(); - //}); + Task.Run(() => { _prodCompletionBusiness.PrintMessageToListBoxEvent += PrintMessageToListBox; - - // _prodCompletionBusiness.Init(); + // _prodCompletionBusiness.Init(); }); + Init(); + + } + + private void Init() + { + RefreChart(); + DispatcherTimer _timer = new DispatcherTimer(); + _timer.Interval = TimeSpan.FromMinutes(5); + _timer.Tick += Timer_Tick; + _timer.Start(); + } + private void Timer_Tick(object sender, EventArgs e) + { RefreChart(); } - + + + public void test() { // gunHelper.InstanceSerialPort(); @@ -285,13 +307,14 @@ namespace SlnMesnac.WPF.ViewModel }); var info = list.Where(x => x.PlanStatus == PlanStatusEnum.已开始).ToList(); - if(info.Count > 0) + if (info.Count > 0) { RefreshPlanExec(info.First()); } - + } - }catch(Exception ex) + } + catch (Exception ex) { _logger.LogError($"生产计划刷新异常:{ex.Message}"); } @@ -306,11 +329,11 @@ namespace SlnMesnac.WPF.ViewModel try { //先检查是否有正在执行的计划 - var hasPlan = planInfoDataGrid.Where(x=>x.PlanStatus== PlanStatusEnum.已开始).FirstOrDefault(); - if(hasPlan != null) + var hasPlan = planInfoDataGrid.Where(x => x.PlanStatus == PlanStatusEnum.已开始).FirstOrDefault(); + if (hasPlan != null) { - var result = MessageBox.Show("有正在执行的计划,是否确认切换计划!","确认", MessageBoxButton.YesNo); - if(result == MessageBoxResult.Yes) + var result = MessageBox.Show("有正在执行的计划,是否确认切换计划!", "确认", MessageBoxButton.YesNo); + if (result == MessageBoxResult.Yes) { hasPlan.PlanStatus = PlanStatusEnum.待执行; _prodMgmtBusiness.UpdateProdPlan(hasPlan); @@ -339,7 +362,7 @@ namespace SlnMesnac.WPF.ViewModel _logger.LogError($"开始事件处理异常:{e.Message}"); } } - + /// /// 暂停生产计划事件 /// @@ -359,7 +382,7 @@ namespace SlnMesnac.WPF.ViewModel PrintMessageToListBox($"暂停执行{obj}计划"); _prodMgmtBusiness.UpdateProdPlan(info); - + this.RefreshPlanExec(null); } catch (Exception e) @@ -376,7 +399,7 @@ namespace SlnMesnac.WPF.ViewModel /// /// /// - private bool MatPutInValidHandleEvent(int validType,MesProductPlan productPlan, string materialName,string msg) + private bool MatPutInValidHandleEvent(int validType, MesProductPlan productPlan, string materialName, string msg) { var result = false; @@ -440,8 +463,68 @@ namespace SlnMesnac.WPF.ViewModel /// private async void RefreChart() { - ProductionHourList = new List() {"8:00","9:00","10:00","11:00","12:00","13:00", "14:00", "15:00", "16:00", "17:00","18:00","19:00","20:00" }; - + try + { + await Task.Run(() => + { + ProductionHourList = new List() { "8:00", "9:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00", "18:00", "20:00" }; + List list = _mesBaseBarcodeInfoService.Query(x => x.bindTime != null && x.bindTime >= DateTime.Now.Date && x.bindTime <= DateTime.Now.AddDays(1)).ToList(); + if (list != null && list.Count > 0) + { + + Dictionary hourProductionCount = new Dictionary(); + // 初始化字典,每个小时的产量初始为0 + foreach (var hour in ProductionHourList) + { + hourProductionCount[hour] = 0; + } + + // 遍历查询结果,统计每个小时的产量 + foreach (var item in list) + { + // 获取 bindTime 的小时部分 + string hour = item.bindTime.Value.ToString("H:00"); + + // 如果小时在 ProductionHourList 中,则统计该小时的产量 + if (hourProductionCount.ContainsKey(hour)) + { + hourProductionCount[hour]++; + } + } + Application.Current.Dispatcher.Invoke(() => + { + Achievement.Clear(); + ChartValues achievement = new ChartValues(); + int i = 0; + foreach (var kvp in hourProductionCount) + { + achievement.Add(new ObservablePoint(i++, kvp.Value)); + } + //加一个汇总柱状图 + ProductionHourList.Add("合计"); + achievement.Add(new ObservablePoint(i, list.Count)); + + var column = new ColumnSeries(); + column.DataLabels = true; + column.Title = "小时产量"; + column.Values = achievement; + column.Foreground = Brushes.White; + Achievement.Add(column); + + }); + } + else + { + // ProductionHourList.Clear(); + Achievement.Clear(); + } + }); + + }catch(Exception ex) + { + _logger.LogError($"产量统计图表异常:{ex.Message}"); + + } } } }