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 Admin.Core.Model.ViewModels; using System.Collections.ObjectModel; using Aucma.Core.DoorFoam.Models; using Admin.Core.Model; using Castle.Core.Internal; using Aucma.Core.Scanner; using Admin.Core.Service; using Admin.Core.IRepository; using Admin.Core.Repository; using Admin.Core.Common; using log4net; using System.Collections; using System.Windows.Documents; using NPOI.SS.Formula.Functions; using Microsoft.Extensions.FileSystemGlobbing.Internal; using System.Text.RegularExpressions; using System.Windows.Controls; 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 IDoorMateHistoryServices? _doorMateHistoryServices; protected readonly IPrintBarCodeServices? _printBarCodeServices; protected readonly ISysUserInfoRepository _sysUserInfoRepository; private ObservableCollection listItems = new ObservableCollection(); public RealTimePageViewModel() { _productPlanInfoServices = App.ServiceProvider.GetService(); _doorMateHistoryServices = App.ServiceProvider.GetService(); _sysUserInfoRepository = App.ServiceProvider.GetService(); _printBarCodeServices = App.ServiceProvider.GetService(); InitEveryDayMethodAsync(); MvCodeHelper.DoorReceiveCodeDelegateEvent += OnLoadPlanData; OnLoadPlanData("B24010181060282920001"); } /// /// 获取今天的数据 /// /// private void InitEveryDayMethodAsync() { ChartValues achievement = new ChartValues(); MaterialNameList = new List(); var info = _doorMateHistoryServices.Query(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)); } } ModelStatistics.Add(new ColumnSeries() { DataLabels = true, Title = "型号", Values = achievement, Fill = new SolidColorBrush(Color.FromRgb(15, 209, 226)), Foreground = Brushes.White, FontSize = 18 }); })); } #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 void OnLoadPlanData(string code) { string stationCode = Appsettings.app("StationInfo", "StationCode"); string productLineCode = Appsettings.app("StationInfo", "ProductlineCode"); if (string.IsNullOrEmpty(code)) return; var plan = _productPlanInfoServices.QueryAsync(d => d.ProductLineCode == stationCode && d.PlanAmount > d.CompleteAmount).Result; if (plan == null) return; var obj = _printBarCodeServices.FirstAsync(d => d.MaterialBarcode == code).Result; if (obj == null) return; //string materialCode = this.SubString(code); ProductPlanInfo stationPlan = plan.FirstOrDefault(d => d.OrderCode == obj.OrderCode); if (stationPlan == null) 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.MaterialCode = stationPlan.MaterialCode; model.MaterialName = stationPlan.MaterialName; model.ScanTime = DateTime.Now; SaveMateHistory(code,obj, productLineCode, plan, model); model.MaterialName = this.FormatMaterialType(stationPlan.MaterialName); model.ObjId = i+1; planInfoDataGrid.Insert(0, model); InitEveryDayMethodAsync();//刷新型号统计图表 } #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}】"); else PrintMessageToListBox($"箱门匹配成功!扫描箱体条码:【{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 /// /// 格式化物料类型 /// /// /// 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; } } }