You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
AUCMA_SCADA/Aucma.Core.DoorFoam/ViewModels/RealTimePageViewModel.cs

456 lines
15 KiB
C#

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;
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<dynamic> listItems = new ObservableCollection<dynamic>();
public RealTimePageViewModel()
{
_orderBomInfoServices = App.ServiceProvider.GetService<IOrderBomInfoServices>();
_baseOrderInfoServices = App.ServiceProvider.GetService<IBaseOrderInfoServices>();
_productPlanInfoServices = App.ServiceProvider.GetService<IProductPlanInfoServices>();
_doorMateHistoryServices = App.ServiceProvider.GetService<IDoorMateHistoryServices>();
_sysUserInfoRepository = App.ServiceProvider.GetService<ISysUserInfoRepository>();
_printBarCodeServices = App.ServiceProvider.GetService<IPrintBarCodeServices>();
_materialCompletionServices = App.ServiceProvider.GetService<IMaterialCompletionServices>();
MvCodeHelper.DoorReceiveCodeDelegateEvent += OnLoadPlanData;
InitEveryDayMethodAsync();
//Task.Run(() =>
//{
// Thread.Sleep(2000);
// while(true)
// {
// InitEveryDayMethodAsync();//刷新型号统计图表
// Thread.Sleep(1000);
// }
//});
}
/// <summary>
/// 获取今天的数据
/// </summary>
/// <returns></returns>
private void InitEveryDayMethodAsync()
{
ChartValues<double> achievement = new ChartValues<double>();
MaterialNameList = new List<string>();
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));
}
}
ColumnSeries column = new ColumnSeries()
{
DataLabels = true,
Title = "型号",
Values = achievement,
Fill = new SolidColorBrush(Color.FromRgb(15, 209, 226)),
Foreground = Brushes.White,
FontSize = 18
};
if (ModelStatistics.Count > 0)
{
#region 这样添加数据会有问题
//for (int i = 0; i < achievement.Count; i++)
//{
// ModelStatistics.FirstOrDefault().Values[i] = achievement[i];
//}
#endregion
//从数据库重新加载
InitEveryDayMethodAsync();
}
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轴日期
/// <summary>
/// 日产量柱状图X轴日期
/// </summary>
private List<string> productionHourList;
public List<string> ProductionHourList
{
get { return productionHourList; }
set { productionHourList = value; }
}
#endregion
/// <summary>
/// 日产量柱状图
/// </summary>
private SeriesCollection achievement = new SeriesCollection();
public SeriesCollection Achievement
{
get { return achievement; }
set { achievement = value; }
}
#endregion
#region 型号统计
#region 型号统计柱状图x轴物料类型
/// <summary>
/// 型号统计柱状图x轴物料类型
/// </summary>
private List<string> materialNameList;
public List<string> MaterialNameList
{
get { return materialNameList; }
set { materialNameList = value; }
}
#endregion
#region 型号统计柱状图
/// <summary>
/// 型号统计柱状图
/// </summary>
private SeriesCollection modelStatistics = new SeriesCollection();
public SeriesCollection ModelStatistics
{
get { return modelStatistics; }
set { modelStatistics = value; }
}
#endregion
#endregion
#region 门体匹配队列
private int i = 0;
/// <summary>
/// 门体匹配队列
/// </summary>
/// <param name="code"></param>
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("未查询到条码打印信息");
Console.WriteLine("未查询到条码打印信息");
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)(async () =>
{
planInfoDataGrid.Insert(0, model);
}));
InsertToCompleteAndCheck(model, code);
InitEveryDayMethodAsync();//刷新型号统计图表
Console.WriteLine($"箱门匹配信息记录成功!刷新型号统计图表");
}
#region 记录历史
/// <summary>
/// 记录历史
/// </summary>
/// <param name="code"></param>
/// <param name="productLineCode"></param>
/// <param name="plan"></param>
/// <param name="model"></param>
private void SaveMateHistory(string code, PrintBarCode obj, string productLineCode, List<ProductPlanInfo> 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<DoorMateHistory> planInfoDataGrid = new ObservableCollection<DoorMateHistory>();
public ObservableCollection<DoorMateHistory> PlanInfoDataGrid
{
get { return planInfoDataGrid; }
set
{
planInfoDataGrid = value;
OnPropertyChanged();//属性通知
}
}
#endregion
#region 实时展示数据
/// <summary>
/// LisBox数据模板
/// </summary>
private IEnumerable logInfoListBox;
public IEnumerable LogInfoListBox
{
get => logInfoListBox;
set => SetProperty(ref logInfoListBox, value);
}
#endregion
#region listBox绑定日志
/// <summary>
/// listBox绑定日志
/// </summary>
/// <param name="message"></param>
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 格式化物料类型
/// <summary>
/// 格式化物料类型
/// </summary>
/// <param name="materialType"></param>
/// <returns></returns>
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 保存过点数
/// <summary>
/// 保存过点数
/// </summary>
/// <param name="history"></param>
/// <param name="barCode"></param>
/// <returns></returns>
public async void InsertToCompleteAndCheck(DoorMateHistory history, string barCode)
{
if (history != null) return ;
List<MaterialCompletion> completionList = new List<MaterialCompletion>();
#region 更新过点数据
string stationName = Appsettings.app("StationInfo", "StationName");
MaterialCompletion completion = new MaterialCompletion();
completion.OrderCode = history.OrderCode;
completion.MaterialBarcode = barCode;
completion.MaterialCode = history.MaterialCode;
completion.MaterialName = history.MaterialName;
completion.StationName = stationName;
completion.ProductLineCode = "CX_02";
completion.CompleteDate = DateTime.Now;
completionList.Add(completion);
#endregion
int r = _materialCompletionServices.AddAsync(completionList).Result;
}
#endregion
}
}