using CommunityToolkit.Mvvm.ComponentModel; using LiveCharts.Defaults; using LiveCharts.Wpf; using LiveCharts; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Media; using System.Collections.ObjectModel; using Admin.Core.IService; using Admin.Core.Service; using Microsoft.Extensions.DependencyInjection; using Admin.Core.Common; using Admin.Core.Tasks; using Aucma.Core.BoxFoam.Business; using System.Windows.Shapes; using System.Timers; using Aucma.Core.BoxFoam.Models; using Admin.Core.Model; using System.Threading; using Aucma.Core.HwPLc; using Admin.Core.Model.ViewModels; namespace Aucma.Core.BoxFoam.ViewModels { public partial class FoamMonitorPageViewModel : ObservableObject { private readonly IBaseBomInfoServices? _baseBomInfoServices; private readonly IBoxFoamDataServices _boxFoamDataServices; private readonly ICurrentTeamTimeServices _currentTeamTimeServices; private readonly IBoxFoamDataServices? _oldBoxFoamDataServices; private readonly IBoxFoamPlanServices _boxFoamPlanServices; private readonly ISysUserInfoServices _sysUserInfoServices; private readonly IBoxFoamDataRecordServices _boxFoamDataRecordServices; private readonly IBoxFoamDataRealTimeServices? _boxFoamDataRealTimeServices; private readonly IBoxFoamDataRealTimeHistoryServices _boxFoamDataRealTimeHistoryServices; System.Timers.Timer timer2 = new System.Timers.Timer(5000); int totalAmount = 0; public FoamMonitorPageViewModel() { _boxFoamPlanServices = App.ServiceProvider.GetService(); _boxFoamDataRealTimeServices = App.ServiceProvider.GetService(); _boxFoamDataRecordServices = App.ServiceProvider.GetService(); _currentTeamTimeServices = App.ServiceProvider.GetService(); _boxFoamDataRealTimeHistoryServices = App.ServiceProvider.GetService(); _sysUserInfoServices = App.ServiceProvider.GetService(); _boxFoamDataServices = App.ServiceProvider.GetService(); _baseBomInfoServices = App.ServiceProvider.GetService(); CollectionFoamLine.RefreshBoxFoamDataDelegateEvent += InitData; //timer2.Elapsed += new System.Timers.ElapsedEventHandler(RealTimeFoamingTask); //到达时间的时候执行事件; //timer2.AutoReset = true;//设置是执行一次(false)还是一直执行(true); //timer2.Enabled = true;//需要调用 timer.Start()或者timer.Enabled = true来启动它, //timer2.Start();//timer.Start()的内部原理还是设置timer.Enabled = true //Task.WaitAll(InitData // Task.Run(() => { while (true) { Thread.Sleep(3000); App.Current.Dispatcher.BeginInvoke((Action)(() => { InitEveryDayMethod(); })); } }); } #region 日产量 /// /// 每日生产 /// /// private Task InitEveryDayMethod() { try { if (ProductionHourList!=null && ProductionHourList.Count>0) { ProductionHourList.Clear(); } ProductionHourList = new List(); List listTime = _baseBomInfoServices.getWorkTime().Result; foreach (WorkTime time in listTime) { ProductionHourList.Add(time.hourTime); } // 货道列表 List list = _boxFoamDataServices.QueryAsync(x => x.ProductLineCode.Equals("CX_02") && x.StationCode == "1005").Result; if (list != null) { ChartValues achievement = new ChartValues(); //Achievement.Clear(); #region 按时间统计 if (list.Count > 0) { achievement.Add(list.Sum(x => x.AnHour)); achievement.Add(list.Sum(x => x.TwoHour)); achievement.Add(list.Sum(x => x.ThreeHour)); achievement.Add(list.Sum(x => x.FourHour)); achievement.Add(list.Sum(x => x.FiveHour)); achievement.Add(list.Sum(x => x.SixHour)); achievement.Add(list.Sum(x => x.SevenHour)); achievement.Add(list.Sum(x => x.EightHour)); achievement.Add(list.Sum(x => x.NineHour)); achievement.Add(list.Sum(x => x.TenHour)); achievement.Add(list.Sum(x => x.ElevenHour)); achievement.Add(list.Sum(x => x.TwelveHour)); } var column = new ColumnSeries(); column.DataLabels = true; column.Title = "当日小时产量统计"; column.Values = achievement; column.Foreground = Brushes.White; // Achievement.Add(column); if (Achievement.Count > 0) { for (int i = 0; i < achievement.Count; i++) { Achievement.FirstOrDefault().Values[i] = achievement[i]; } } else { Achievement.Add(column); } #endregion #region 按类型统计 MaterialNameList = new List(); ChartValues achievement2 = new ChartValues(); //foreach(BoxFoamData space in list) //{ // MaterialNameList.Add(space.Fixtureboxtype); //} var grouped = list.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(); MaterialNameList.Add(item.Key); achievement2.Add(flattenedList.Sum()); } // ModelStatistics.Clear(); var column2 = new ColumnSeries(); column2.DataLabels = true; column2.Title = "当日型号产量统计"; column2.Values = achievement2; column2.Foreground = Brushes.White; // ModelStatistics.Add(column2); if (ModelStatistics.Count > 0) { for (int i = 0; i < achievement2.Count; i++) { ModelStatistics.FirstOrDefault().Values[i] = achievement2[i]; } } else { ModelStatistics.Add(column2); } #endregion } } catch (Exception ex) { Console.WriteLine(ex.Message.ToString()); } return Task.CompletedTask; } #endregion #region 日产量柱状图X轴日期 /// /// 日产量柱状图X轴日期 /// private List productionHourList; public List ProductionHourList { get { return productionHourList; } set { productionHourList = value; } } #endregion #region 型号统计 #region 型号统计柱状图x轴物料类型 /// /// 型号统计柱状图x轴物料类型 /// private List materialNameList; public List MaterialNameList { get { return materialNameList; } set { materialNameList = value; } } #endregion #region 型号统计柱状图 /// /// 型号统计柱状图 /// private SeriesCollection modelStatistics = new SeriesCollection(); public SeriesCollection ModelStatistics { get { return modelStatistics; } set { modelStatistics = value; } } #endregion #endregion #region 日产量柱状图 /// /// 日产量柱状图 /// private SeriesCollection achievement = new SeriesCollection(); public SeriesCollection Achievement { get { return achievement; } set { achievement = value; } } #endregion #region 颜色 /// /// 颜色 /// private ObservableCollection _color = new ObservableCollection(); public ObservableCollection Color { get => _color; set => SetProperty(ref _color, value); } #endregion #region 总计 private int _totle; public int Totle { get => _totle; set => SetProperty(ref _totle, value); } #endregion #region 夹具状态 private ObservableCollection _statusColor = new ObservableCollection(); public ObservableCollection StatusColor { get => _statusColor; set => SetProperty(ref _statusColor, value); } #endregion #region 夹具名称 private ObservableCollection _fixtureName = new ObservableCollection(); public ObservableCollection FixtureName { get => _fixtureName; set => SetProperty(ref _fixtureName, value); } #endregion #region 夹具产量 /// ///夹具产量 /// private ObservableCollection _production = new ObservableCollection(); public ObservableCollection Production { get => _production; set => SetProperty(ref _production, value); } #endregion #region 发泡量 /// /// 发泡量 /// private ObservableCollection _foamVolume = new ObservableCollection(); public ObservableCollection FoamVolume { get => _foamVolume; set => SetProperty(ref _foamVolume, value); } #endregion #region 内侧温度 private ObservableCollection _internalTemperature = new ObservableCollection(); public ObservableCollection InternalTemperature { get => _internalTemperature; set => SetProperty(ref _internalTemperature, value); } #endregion #region 侧板温度温度 private ObservableCollection _outsideTemperature = new ObservableCollection(); public ObservableCollection OutsideTemperature { get => _outsideTemperature; set => SetProperty(ref _outsideTemperature, value); } #endregion #region 夹具节拍 private ObservableCollection _beat = new ObservableCollection(); public ObservableCollection Beat { get => _beat; set => SetProperty(ref _beat, value); } #endregion #region 初始化发泡数据 /// /// 初始化发泡数据 /// /// public async Task InitData(List list) { try { int totalAmount = 0; if (list == null) return; FixtureName.Clear(); Production.Clear(); Beat.Clear(); InternalTemperature.Clear(); OutsideTemperature.Clear(); StatusColor.Clear(); foreach (var item in list) { if (item.Status == 1) StatusColor.Add("Green"); if (item.Status == 2) StatusColor.Add("Red"); if (!string.IsNullOrEmpty(item.FixtureBoxType)) FixtureName.Add(item.FixtureBoxType); if (!string.IsNullOrEmpty(item.Yield.ToString())) Production.Add(item.Yield.ToString()); // if (!string.IsNullOrEmpty(item.InternalTemperature.ToString())) InternalTemperature.Add(item.InternalTemperature.ToString()); // if (!string.IsNullOrEmpty(item.OutsideTemperature.ToString())) OutsideTemperature.Add(item.OutsideTemperature.ToString()); if (!string.IsNullOrEmpty(item.ProductionCycle.ToString())) Beat.Add(item.ProductionCycle.ToString()); totalAmount = totalAmount + item.Yield; } Totle = totalAmount; } catch(Exception ex) { Console.WriteLine(ex.Message.ToString()); } } #endregion #region 更新夹具型号 /// /// 更新夹具型号 /// public async void UpdateClampData(FixtureStatus fixtureStatus, BoxFoamDataRealTime bfd) { try { bfd.FixtureBoxtype = fixtureStatus.FixtureBoxType; bfd.FixtureStatus = fixtureStatus.Status.ToString(); bfd.FixtureProductioncapacity = fixtureStatus.Yield; bfd.FixtureCuringtimesettingvalue = fixtureStatus.PlanCuringTime.ToString(); bfd.FixtureActualtime = fixtureStatus.RealCuringTime.ToString(); bfd.ModeInternaltemperature = fixtureStatus.InternalTemperature; bfd.ModeOutsidetemperature = fixtureStatus.InternalTemperature; bfd.Beat = fixtureStatus.Yield.ToString(); bfd.UpdatedTime = DateTime.Now; await _boxFoamDataRealTimeServices.UpdateAsync(bfd); } catch { } } #endregion #region 新增夹具型号 /// /// 更新夹具型号 /// public async void SaveClampData(string productLineCode,FixtureStatus fixtureStatus) { var obj=await _sysUserInfoServices.GetProductLineInfo(productLineCode); if (obj!=null) { BoxFoamDataRealTime bfd = new BoxFoamDataRealTime(); bfd.PlantCode = obj.PlantCode; bfd.ProductlineCode = productLineCode; bfd.ProcessCode = obj.ProcessCode; bfd.StationCode = obj.StationCode; bfd.SerialNumber = fixtureStatus.ObjId; bfd.FixtureBoxtype = fixtureStatus.FixtureBoxType; bfd.FixtureStatus = fixtureStatus.Status.ToString(); bfd.FixtureProductioncapacity = fixtureStatus.Yield; bfd.FixtureCuringtimesettingvalue = fixtureStatus.PlanCuringTime.ToString(); bfd.FixtureActualtime = fixtureStatus.RealCuringTime.ToString(); bfd.ModeInternaltemperature = fixtureStatus.InternalTemperature; bfd.ModeOutsidetemperature = fixtureStatus.InternalTemperature; bfd.CreatedTime = DateTime.Now; bfd.UpdatedTime = DateTime.Now; await _boxFoamDataRealTimeServices.AddAsync(bfd); } } #endregion #region 原逻辑 Delete By wenjy 2024-01-09 14:38:00 //Semaphore semaphore = new Semaphore(1, 1); ///// ///// 将采集到的数据保存 ///// ///// //public async void RealTimeFoamingTask(object? sender, ElapsedEventArgs e) //{ // semaphore.WaitOne(); // try // { // string productLineCode = Appsettings.app("StationInfo", "ProductLineCode"); // DateTime now = DateTime.Now; // CurrentTeamTime currentTeamTime = await _currentTeamTimeServices.FirstAsync(d => d.StartTime <= now && d.EndTime >= now); // if (currentTeamTime!=null) // { // //比较计划更新数据 // ExecHourDataAsync(productLineCode); // } // else // { // // 白夜班切换 // ShiftChangeAsync(productLineCode); // } // } // catch (Exception ex) // { // Console.WriteLine(ex.Message); // } // finally // { // semaphore.Release(); // } //} //public async void ExecHourDataAsync(string productLineCode) //{ // List bLis = CollectionFoamLine.listFixtureStatus; // if (bLis.Count == 0) return; // List planList = await _boxFoamPlanServices.QueryAsync(); // List bfds = await _boxFoamDataRealTimeServices.QueryAsync(); // for (int j = 0; j < bLis.Count; j++) // { // try // { // if (string.IsNullOrEmpty(bLis[j].FixtureBoxType)) continue; // BoxFoamPlan plan = planList.FirstOrDefault(d => d.MaterialCode == bLis[j].FixtureBoxType && d.PlanAmount > d.CompleteAmount); // if (plan != null) // { // int complateAmount = 0; // BoxFoamDataRealTime bfd = bfds.SingleOrDefault(d => d.FixtureBoxtype == bLis[j].FixtureBoxType && d.SerialNumber == bLis[j].ObjId); // if (bfd != null) // { // //更新计划数量 // complateAmount = bLis[j].Yield - bfd.FixtureProductioncapacity; // UpdateClampData(bLis[j], bfd); // } // else // { // FixtureStatus fixtureStatus = bLis[j]; // SaveClampData(productLineCode, fixtureStatus); // } // plan.CompleteAmount = plan.CompleteAmount + complateAmount; // if (complateAmount>0) // { // //保存历史记录 // BoxFoamDataRealTimeHistory history = new BoxFoamDataRealTimeHistory(); // history.PlantCode = bfd.PlantCode; // history.ProductlineCode = bfd.ProductlineCode; // history.ProcessCode = bfd.ProcessCode; // history.StationCode= bfd.StationCode; // history.FixtureProductioncapacity = bLis[j].Yield; // history.FixtureCuringtimesettingvalue = bLis[j].PlanCuringTime.ToString(); // history.FixtureActualtime = bLis[j].RealCuringTime.ToString(); // history.ModeInternaltemperature = bLis[j].InternalTemperature; // history.ModeOutsidetemperature = bLis[j].InternalTemperature; // history.UpdatedTime = DateTime.Now; // //事务合并 // await _boxFoamDataRealTimeHistoryServices.AddAsync(history); // await _boxFoamPlanServices.UpdateAsync(plan); // } // } // else // { // //更新小时数据 // BoxFoamDataRealTime bfd = bfds.SingleOrDefault(d => d.FixtureBoxtype == bLis[j].FixtureBoxType && d.SerialNumber == bLis[j].ObjId); // if (bfd != null) // { // UpdateClampData(bLis[j], bfd); // } // else // { // SaveClampData(productLineCode, bLis[j]); // } // } // } // catch { } // } //} //private async void ShiftChangeAsync(string productLineCode) //{ // try // { // //切换班组计划 // var currentTeamTimeList = await _sysUserInfoServices.GetTeamData(); // if (currentTeamTimeList.Count == 0) return; // // 夹具 // var list = await _boxFoamDataRealTimeServices.QueryAsync(d => d.ProductlineCode == productLineCode); // //更新计划表操作 // CurrentTeamTime currentTeamTime = await _currentTeamTimeServices.FirstAsync(); // // 更新当班时间 // currentTeamTime.TeamName = currentTeamTimeList.First().TeamName; // currentTeamTime.StartTime = currentTeamTimeList.Min(d => d.StartTime); // currentTeamTime.EndTime = currentTeamTimeList.Max(d => d.EndTime); // bool result = await _currentTeamTimeServices.UpdateAsync(currentTeamTime); // if (result) // { // //清空 当班计划 // // var realTimeList = await _boxFoamDataRealTimeHistoryServices.QueryAsync(d => d.ProductlineCode == productLineCode); // var plan = await _boxFoamPlanServices.QueryAsync(d => d.ProductLineCode == productLineCode); // await _boxFoamPlanServices.DeletesAsync(plan); // } // } // catch { } //} #endregion } }