using CommunityToolkit.Mvvm.ComponentModel; 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 Admin.Core.IService; using Microsoft.Extensions.DependencyInjection; using System.Collections.ObjectModel; using Admin.Core.Model; using Admin.Core.IRepository; using Admin.Core.Common; using log4net; using System.Collections; using System.Text.RegularExpressions; using System.Windows.Controls; using System.Threading; using Aucma.Core.DoorFoam.Business; using Admin.Core.Service; using Admin.Core.Model.Model_New; using Admin.Core.Socket; namespace Aucma.Core.DoorFoam.ViewModels { public partial class RealTimePageViewModel : ObservableObject { private static readonly log4net.ILog log = LogManager.GetLogger(typeof(RealTimePageViewModel)); protected readonly IProductPlanInfoServices? _productPlanInfoServices; protected readonly IBaseOrderInfoServices? _baseOrderInfoServices; protected readonly IDoorMateHistoryServices? _doorMateHistoryServices; protected readonly IPrintBarCodeServices? _printBarCodeServices; protected readonly ISysUserInfoRepository _sysUserInfoRepository; protected readonly IOrderBomInfoServices _orderBomInfoServices; private readonly IMaterialCompletionServices _materialCompletionServices; private ObservableCollection listItems = new ObservableCollection(); public RealTimePageViewModel() { _orderBomInfoServices = App.ServiceProvider.GetService(); _baseOrderInfoServices = App.ServiceProvider.GetService(); _productPlanInfoServices = App.ServiceProvider.GetService(); _doorMateHistoryServices = App.ServiceProvider.GetService(); _sysUserInfoRepository = App.ServiceProvider.GetService(); _printBarCodeServices = App.ServiceProvider.GetService(); _materialCompletionServices = App.ServiceProvider.GetService(); TouchSocketService.DoorReceiveCodeDelegateEvent += OnLoadPlanData; Task.Run(async () => { await InitEveryDayMethodAsync(); }); } /// /// 获取今天的数据 /// /// private async Task InitEveryDayMethodAsync() { ChartValues achievement = new ChartValues(); MaterialNameList = new List(); var info =await _doorMateHistoryServices.QueryAsync(x => x.ScanTime.ToString("yyyy-MM-dd").Contains(DateTime.Now.ToString("yyyy-MM-dd"))); _ = App.Current.Dispatcher.BeginInvoke((Action)(() => { ModelStatistics.Clear(); if (info != null) { var groupResult = from p in info group p by p.MaterialName into g select new { MaterialName = g.Key, Count = g.Count() }; foreach (var groupItem in groupResult) { achievement.Add(groupItem.Count); MaterialNameList.Add(FormatMaterialType(groupItem.MaterialName)); } } ColumnSeries column = new ColumnSeries() { DataLabels = true, Title = "型号", Values = achievement, Fill = new SolidColorBrush(Color.FromRgb(15, 209, 226)), Foreground = Brushes.White, FontSize = 18 }; ModelStatistics.Add(column); //if (ModelStatistics.Count > 0) //{ // #region 这样添加数据会有问题 // try // { // for (int i = 0; i < achievement.Count; i++) // { // ModelStatistics.FirstOrDefault().Values[i] = achievement[i]; // } // } // catch (Exception) // { // throw; // } // #endregion // //从数据库重新加载 //} //else //{ // ModelStatistics.Add(column); //} })); } #region 扫描信息 #region 物料条码 private string _stationName; public string StationName { get { return _stationName; } set { _stationName = value; SetProperty(ref _stationName, value); } } #endregion #region 物料名称 private string _materialName; public string MaterialName { get { return _materialName; } set { _materialName = value; SetProperty(ref _materialName, value); } } #endregion #region 订单信息 private string _orderNo; public string OrderNo { get { return _orderNo; } set { _orderNo = value; SetProperty(ref _orderNo, value); } } #endregion #region 开始时间 private string _beginTime; public string BeginTime { get { return _beginTime; } set { _beginTime = value; SetProperty(ref _beginTime, value); } } #endregion #endregion #region 当日产量 #region 日产量柱状图X轴日期 /// /// 日产量柱状图X轴日期 /// private List productionHourList; public List ProductionHourList { get { return productionHourList; } set { productionHourList = value; } } #endregion /// /// 日产量柱状图 /// private SeriesCollection achievement = new SeriesCollection(); public SeriesCollection Achievement { get { return achievement; } set { achievement = 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 int i = 0; /// /// 门体匹配队列 /// /// public async Task OnLoadPlanData(string code) { string stationCode = Appsettings.app("StationInfo", "StationCode"); string phproductlineCode = Appsettings.app("StationInfo", "PHProductlineCode"); if (string.IsNullOrEmpty(code)) return; var result = await _doorMateHistoryServices.FirstAsync(d=>d.MaterialBarCode== code); if (result!=null) return; var plan = await _productPlanInfoServices.QueryAsync(d => d.PlanAmount > d.CompleteAmount); //var plan = _orderBomInfoServices.Query(d => d.MaterialType == "200"); //var plan = _baseOrderInfoServices.QueryAsync().Result; if (plan == null) { PrintMessageToListBox("未查询到计划信息"); Console.WriteLine("未查询到计划信息"); return; } var obj = await _printBarCodeServices.FirstAsync(d => d.MaterialBarcode == code); if (obj == null) { PrintMessageToListBox($"未查询到条码打印信息【{code}】"); Console.WriteLine($"未查询到条码打印信息【{code}】"); return; } ProductPlanInfo stationPlan = plan.FirstOrDefault(d => d.OrderCode == obj.OrderCode); if (stationPlan == null) { PrintMessageToListBox($"未查询到该条码订单信息;物料二维码【{code}】"); Console.WriteLine($"未查询到该条码订单信息;条码【{code}】"); log.Error($"未查询到该条码订单信息;条码【{code}】"); return; } DoorMateHistory model = new DoorMateHistory(); if (planInfoDataGrid.Count() == 0) model.ObjId = 1; else model.ObjId = planInfoDataGrid.Count() + 1; model.PlanCode = stationPlan.PlanCode; model.OrderCode = stationPlan.OrderCode; model.MaterialBarCode = code; model.MaterialCode = stationPlan.MaterialCode; model.MaterialName = stationPlan.MaterialName; model.ScanTime = DateTime.Now; SaveMateHistory(code, obj, phproductlineCode, plan, model); Console.WriteLine($"箱门匹配信息记录成功!箱体码【{code}】"); PrintMessageToListBox($"箱门匹配信息记录成功!箱体码【{code}】"); model.MaterialName = this.FormatMaterialType(stationPlan.MaterialName); System.Windows.Application.Current.Dispatcher.Invoke((Action)(() => { planInfoDataGrid.Insert(0, model); })); await InsertToCompleteAndCheck(model, code); await InitEveryDayMethodAsync();//刷新型号统计图表 Console.WriteLine($"箱门匹配信息记录成功!刷新型号统计图表"); } #region 记录历史 /// /// 记录历史 /// /// /// /// /// private void SaveMateHistory(string code, PrintBarCode obj, string productLineCode, List plan, DoorMateHistory model) { var planObj = plan.FirstOrDefault(d => d.OrderCode == obj.OrderCode); if (planObj == null) return; var stationList = _sysUserInfoRepository.GetProductLineInfo(productLineCode).Result; if (stationList != null) { var station = stationList.FirstOrDefault(); model.ProductLineCode = planObj.ProductLineCode; model.PlantCode = station.PlantCode; model.StationCode = station.StationCode; int result = _doorMateHistoryServices.AddAsync(model).Result; if (result != 0) { PrintMessageToListBox($"箱门匹配记录异常!扫描箱体条码:【{code}】"); log.Error($"箱门匹配记录异常!扫描箱体条码:【{code}】"); } else { PrintMessageToListBox($"箱门匹配成功!扫描箱体条码:【{code}】"); log.Info($"箱门匹配成功!扫描箱体条码:【{code}】"); } } } #endregion #endregion #region 初始化datagrid private ObservableCollection planInfoDataGrid = new ObservableCollection(); public ObservableCollection PlanInfoDataGrid { get { return planInfoDataGrid; } set { planInfoDataGrid = value; OnPropertyChanged();//属性通知 } } #endregion #region 实时展示数据 /// /// LisBox数据模板 /// private IEnumerable logInfoListBox; public IEnumerable LogInfoListBox { get => logInfoListBox; set => SetProperty(ref logInfoListBox, value); } #endregion #region listBox绑定日志 /// /// listBox绑定日志 /// /// private void PrintMessageToListBox(string message) { try { listItems.Add($"{DateTime.Now.ToString("HH:mm:ss")}==>{message}"); LogInfoListBox = listItems.OrderByDescending(x => x); } catch (Exception ex) { log.Error("日志数据绑定异常", ex); } } #endregion #region 截取物料编码 public string SubString(string barCode) { string result = string.Empty; if (!string.IsNullOrEmpty(barCode)) { result = barCode.Substring(7, 10); } return result; } #endregion #region 格式化物料类型 /// /// 格式化物料类型 /// /// /// private string FormatMaterialType(string materialType) { string result = ""; System.Text.RegularExpressions.Match match = Regex.Match(materialType, @".*?,(.*?),"); if (match.Success && match.Groups.Count > 1) { result = match.Groups[1].Value; } else { result = materialType; } return result; } #endregion #region 保存过点数 /// /// 保存过点数 /// /// /// /// public async Task InsertToCompleteAndCheck(DoorMateHistory history, string barCode) { if (history != null) return ; List completionList = new List(); #region 更新过点数据 string stationCode = Appsettings.app("StationInfo", "StationCode"); MaterialCompletion completion = new MaterialCompletion(); completion.OrderCode = history.OrderCode; completion.MaterialBarcode = barCode; completion.MaterialCode = history.MaterialCode; completion.MaterialName = history.MaterialName; completion.StationName = stationCode; completion.ProductLineCode = "CX_02"; completion.CompleteDate = DateTime.Now; completionList.Add(completion); #endregion await _materialCompletionServices.AddAsync(completionList); } #endregion } }