using Admin.Core.IService; using Admin.Core.Model.Model_New; using Admin.Core.Service; using Aucma.Core.OldBoxFoam.Business; using Aucma.Core.OldBoxFoam.Models; using Aucma.Core.OldBoxFoam.Views; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using CommunityToolkit.Mvvm.Messaging; using LiveCharts.Wpf; using LiveCharts; using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Timers; using System.Windows; using Admin.Core.Model; using NPOI.SS.Formula.Functions; using System.Windows.Documents; namespace Aucma.Core.OldBoxFoam.ViewModels { /** * 发泡计划 * * */ public partial class RealRoadPageViewModel : ObservableObject { protected readonly IBoxFoamPlanServices? _boxFoamPlanServices; private IOldBoxFoamTypeServices? _oldBoxFoamTypeServices; private IBoxFoamDataServices? _boxFoamDataServices; System.Timers.Timer timer2 = new System.Timers.Timer(3000); public RealRoadPageViewModel() { YAxisLabelFormatter = value => Math.Round(value, 2).ToString(); _boxFoamPlanServices = App.ServiceProvider.GetService(); _oldBoxFoamTypeServices = App.ServiceProvider.GetService(); _boxFoamDataServices = App.ServiceProvider.GetService(); //InitData(); Collection.RefreshSpaceEvent += InitData; timer2.Elapsed += new System.Timers.ElapsedEventHandler(InitData); //到达时间的时候执行事件; timer2.AutoReset = true;//设置是执行一次(false)还是一直执行(true); timer2.Enabled = true;//需要调用 timer.Start()或者timer.Enabled = true来启动它, timer2.Start();//timer.Start()的内部原理还是设置timer.Enabled = true; //RefreshHourOutPutChart(); //RefreshMaterialTypeChart(); } #region 参数定义 private Func _yAxisLabelFormatter; public Func YAxisLabelFormatter { get { return _yAxisLabelFormatter; } set { _yAxisLabelFormatter = value; OnPropertyChanged(nameof(YAxisLabelFormatter)); } } /// /// 小时产量统计图表 /// private SeriesCollection _hourAchievement = new SeriesCollection(); public SeriesCollection HourAchievement { get { return _hourAchievement; } set { _hourAchievement = value; OnPropertyChanged(nameof(HourAchievement)); } } /// /// 小时产量统计图表X轴 /// private List _hourOutPutList; public List HourOutPutList { get { return _hourOutPutList; } set { _hourOutPutList = value; OnPropertyChanged(nameof(HourOutPutList)); } } /// /// 型号统计图表 /// private SeriesCollection _materialTypeAchievement = new SeriesCollection(); public SeriesCollection MaterialTypeAchievement { get { return _materialTypeAchievement; } set { _materialTypeAchievement = value; OnPropertyChanged(nameof(MaterialTypeAchievement)); } } private List _materialTypeOutPutList; public List MaterialTypeOutPutList { get { return _materialTypeOutPutList; } set { _materialTypeOutPutList = value; OnPropertyChanged(nameof(MaterialTypeOutPutList)); } } #endregion public async void InitData(object? sender, ElapsedEventArgs e) { List list = await _oldBoxFoamTypeServices.QueryAsync(); if (list == null) return; System.Windows.Application.Current.Dispatcher.Invoke((Action)(() => { ListItems.Clear(); foreach (OldBoxFoamType item in list) { ListItems.Add(new OldBoxFoamTypeModel() { ObjId = item.ObjId, Local = item.Local, Status = item.Storeamount==3?"1":"0", Storeamount = item.Storeamount, MaterialCode = item.MaterialCode, MaterialName = item.MaterialName, Boxtype = item.Boxtype, }); ; } })); } #region 初始化datagrid private ObservableCollection listItems = new ObservableCollection(); public ObservableCollection ListItems { get { return listItems; } set { listItems = value; OnPropertyChanged();//属性通知 } } #endregion /// /// 更新货道信息 /// /// public void InitData(List spaceDetailModel) { if (spaceDetailModel == null) return; foreach(var item in spaceDetailModel) { var spaceList = _oldBoxFoamTypeServices.Query(x=>x.ObjId == item.ObjId); if(spaceList != null) { if(spaceDetailModel.Count > 0) { var spaceInfo = spaceList.FirstOrDefault(); spaceInfo.Storeamount = item.StoreAmount; spaceInfo.Boxtype = item.StoreType; _oldBoxFoamTypeServices.UpdateAsync(spaceInfo); } } } RefreshHourOutPutChart(); RefreshMaterialTypeChart(); } /// /// 小时统计图表 /// private void RefreshHourOutPutChart() { ChartValues hourAchievement = new ChartValues(); HourOutPutList = new List(); App.Current.Dispatcher.BeginInvoke((Action)(() => { DateTime now = DateTime.Now; // 判断当前是否是白班时间段 if (now.Hour >= 8 && now.Hour < 20) { HourOutPutList = new List() { "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19" }; } else { HourOutPutList = new List() { "20", "21", "22", "23", "00", "01", "02", "03", "04", "05", "06", "07" }; } var hourFoamDataList = _boxFoamDataServices.Query(x=>x.ProductLineCode == "CX_01" && x.StationCode == "1005"); if(hourFoamDataList != null) { HourAchievement.Clear(); hourAchievement.Add(hourFoamDataList.Sum(x => x.AnHour)); hourAchievement.Add(hourFoamDataList.Sum(x => x.TwoHour)); hourAchievement.Add(hourFoamDataList.Sum(x => x.ThreeHour)); hourAchievement.Add(hourFoamDataList.Sum(x => x.FourHour)); hourAchievement.Add(hourFoamDataList.Sum(x => x.FiveHour)); hourAchievement.Add(hourFoamDataList.Sum(x => x.SixHour)); hourAchievement.Add(hourFoamDataList.Sum(x => x.SevenHour)); hourAchievement.Add(hourFoamDataList.Sum(x => x.EightHour)); hourAchievement.Add(hourFoamDataList.Sum(x => x.NineHour)); hourAchievement.Add(hourFoamDataList.Sum(x => x.TenHour)); hourAchievement.Add(hourFoamDataList.Sum(x => x.ElevenHour)); hourAchievement.Add(hourFoamDataList.Sum(x => x.TwelveHour)); } var shellColumn = new ColumnSeries(); shellColumn.DataLabels = true; shellColumn.Title = "小时产量"; shellColumn.Values = hourAchievement; HourAchievement.Add(shellColumn); })); } /// /// 型号统计图表 /// private void RefreshMaterialTypeChart() { ChartValues materialTypeAchievement = new ChartValues(); MaterialTypeOutPutList = new List(); App.Current.Dispatcher.BeginInvoke((Action)(() => { DateTime now = DateTime.Now; var hourFoamDataList = _boxFoamDataServices.Query(x => x.ProductLineCode == "CX_01" && x.StationCode == "1005"); if (hourFoamDataList != null) { MaterialTypeAchievement.Clear(); var grouped = hourFoamDataList.GroupBy(x => x.Fixtureboxtype); foreach (var item in grouped) { var flattenedList = item.SelectMany(p => new[] { p.AnHour, p.TwoHour, p.ThreeHour, p.FourHour, p.FiveHour, p.SixHour, p.SevenHour, p.EightHour, p.NineHour, p.TenHour, p.ElevenHour, p.TwelveHour }).ToList(); MaterialTypeOutPutList.Add(item.Key); materialTypeAchievement.Add(flattenedList.Sum()); } } var shellColumn = new ColumnSeries(); shellColumn.DataLabels = true; shellColumn.Title = "型号统计"; shellColumn.Values = materialTypeAchievement; MaterialTypeAchievement.Add(shellColumn); })); } } }