using GalaSoft.MvvmLight;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using SlnMesnac.Model.domain;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using GalaSoft.MvvmLight.Command;
using SlnMesnac.Business;
using SlnMesnac.Model.dto;
using SlnMesnac.Model.enums;
using SlnMesnac.WPF.Page;
using Newtonsoft.Json.Linq;
using System.Collections;
using Microsoft.IdentityModel.Logging;
using System.Windows.Documents;
using SlnMesnac.Common;
using HslCommunication.Profinet.GE;
using static System.Net.Mime.MediaTypeNames;
using Application = System.Windows.Application;
namespace SlnMesnac.WPF.ViewModel
{
///
/// 生产管理ViewModel
///
public class ProdMgmtViewModel : ViewModelBase
{
private readonly ILogger _logger;
private readonly ProdMgmtBusiness? _prodMgmtBusiness;
private readonly PalletStowBusiness? _palletStowBusiness;
private readonly ProdCompletionBusiness? _prodCompletionBusiness;
private ObservableCollection listItems = new ObservableCollection();
private readonly GunHelper gunHelper = GunHelper.Instance;
#region 参数定义
///
/// 工位名称
///
private string stationName = string.Empty;
public string StationName
{
get { return stationName; }
set { stationName = value; RaisePropertyChanged(nameof(StationName)); }
}
///
/// 订单编号
///
private string orderCode = string.Empty;
public string OrderCode
{
get { return orderCode; }
set { orderCode = value; RaisePropertyChanged(nameof(OrderCode)); }
}
///
/// 计划编号
///
private string planCode = string.Empty;
public string PlanCode
{
get { return planCode; }
set { planCode = value; RaisePropertyChanged(nameof(PlanCode)); }
}
///
/// 产品型号
///
private string productModel = string.Empty;
public string ProductModel
{
get { return productModel; }
set { productModel = value; RaisePropertyChanged(nameof(ProductModel)); }
}
///
/// 开始时间
///
private string beginTime = string.Empty;
public string BeginTime
{
get { return beginTime; }
set { beginTime = value; RaisePropertyChanged(nameof(BeginTime)); }
}
///
/// 计划数量
///
private int planAmount = 0;
public int PlanAmount
{
get { return planAmount; }
set { planAmount = value; RaisePropertyChanged(nameof(planAmount)); }
}
///
/// 完成数量
///
public int complateAmout;
public int ComplateAmout
{
get { return complateAmout; }
set { complateAmout = value; RaisePropertyChanged(nameof(ComplateAmout)); }
}
///
/// 差异值
///
public int diffAmount;
public int DiffAmount
{
get { return diffAmount; }
set { diffAmount = value; RaisePropertyChanged(nameof(DiffAmount)); }
}
///
/// 完成率
///
public string compleRoution;
public string CompleRoution
{
get { return compleRoution; }
set { compleRoution = value; RaisePropertyChanged(nameof(CompleRoution)); }
}
///
/// 计划列表DataGrid
///
private ObservableCollection planInfoDataGrid;
public ObservableCollection PlanInfoDataGrid
{
get { return planInfoDataGrid; }
set { planInfoDataGrid = value; RaisePropertyChanged(() => PlanInfoDataGrid); }
}
///
/// LisBox数据模板
///
private IEnumerable logInfoListBox;
public IEnumerable LogInfoListBox
{
get { return logInfoListBox; }
set { logInfoListBox = value; RaisePropertyChanged(() => LogInfoListBox); }
}
#endregion
#region 事件定义
///
/// 开始执行生产计划
///
public RelayCommand StartProdPlanCommand { get; set; }
///
/// 暂停生产计划
///
public RelayCommand StopProdPlanCommand { get; set; }
#endregion
public ProdMgmtViewModel()
{
test();
StartProdPlanCommand = new RelayCommand(obj => StartProdPlan(obj));
StopProdPlanCommand = new RelayCommand(obj => StopProdPlan(obj));
_logger = App.ServiceProvider.GetService>();
_prodMgmtBusiness = App.ServiceProvider.GetService();
_palletStowBusiness = App.ServiceProvider.GetService();
_prodCompletionBusiness = App.ServiceProvider.GetService();
Task.Run(() =>
{
_prodMgmtBusiness.RefreshProdPlanListEvent += RefreshPlanDataGrid;
_prodMgmtBusiness.MatPutInValidEvent += MatPutInValidHandleEvent;
_prodMgmtBusiness.PrintMessageToListBoxEvent += PrintMessageToListBox;
_prodMgmtBusiness.InitProdPlan();
});
Task.Run(() =>
{
_palletStowBusiness.PrintMessageToListBoxEvent += PrintMessageToListBox;
_palletStowBusiness.Init();
});
Task.Run(() =>
{
_prodCompletionBusiness.PrintMessageToListBoxEvent += PrintMessageToListBox;
_prodCompletionBusiness.Init();
});
}
public void test()
{
// gunHelper.InstanceSerialPort();
// HttpHelper.JudgeAgvTaskComplete();
// HttpHelper.SendAgvTask("20245603");
//HttpHelper.JudgeAgvTaskComplete();
}
///
/// 刷新计划执行
///
///
private void RefreshPlanExec(MesProductPlanDto prodPlan)
{
if (prodPlan != null)
{
StationName = "磁悬";
OrderCode = prodPlan.OrderCode;
PlanCode = prodPlan.PlanCode;
ProductModel = prodPlan.MaterialName;
BeginTime = prodPlan.RealBeginTime.ToString("yyyy-MM-dd HH:mm:ss");
PlanAmount = Convert.ToInt32(prodPlan.PlanAmount);
ComplateAmout = Convert.ToInt32(prodPlan.CompleteAmount);
DiffAmount = Convert.ToInt32(prodPlan.PlanAmount) - Convert.ToInt32(prodPlan.CompleteAmount);
CompleRoution = "50";
}
else
{
OrderCode = string.Empty;
PlanCode = string.Empty;
ProductModel = string.Empty;
BeginTime = string.Empty;
PlanAmount = 0;
ComplateAmout = 0;
DiffAmount = 0;
CompleRoution = string.Empty;
}
}
///
/// 刷新生产计划
///
///
private void RefreshPlanDataGrid(List list)
{
try
{
PlanInfoDataGrid = new ObservableCollection();
if (list != null)
{
list.OrderByDescending(x => x.PlanStatus);
list.ForEach(
arg =>
{
PlanInfoDataGrid.Add(arg);
});
var info = list.Where(x => x.PlanStatus == PlanStatusEnum.已开始).ToList();
if(info.Count > 0)
{
RefreshPlanExec(info.First());
}
}
}catch(Exception ex)
{
_logger.LogError($"生产计划刷新异常:{ex.Message}");
}
}
///
/// 开始生产计划事件
///
///
private void StartProdPlan(string obj)
{
try
{
_logger.LogInformation($"开始执行{obj}计划");
var info = planInfoDataGrid.Where(x => x.PlanCode == obj).First();
if (info != null)
{
info.PlanStatus = PlanStatusEnum.已开始;
info.RealBeginTime = DateTime.Now;
}
_prodMgmtBusiness.UpdateProdPlan(info);
PrintMessageToListBox($"开始执行{obj}计划");
this.RefreshPlanExec(info);
}
catch (Exception e)
{
_logger.LogError($"开始事件处理异常:{e.Message}");
}
}
///
/// 暂停生产计划事件
///
///
private void StopProdPlan(string obj)
{
try
{
_logger.LogInformation($"暂停执行{obj}计划");
var info = planInfoDataGrid.Where(x => x.PlanCode == obj).First();
if (info != null)
{
info.PlanStatus = PlanStatusEnum.待执行;
//info.RealBeginTime = DateTime.Now;
}
PrintMessageToListBox($"暂停执行{obj}计划");
_prodMgmtBusiness.UpdateProdPlan(info);
this.RefreshPlanExec(null);
}
catch (Exception e)
{
_logger.LogError($"暂停事件处理异常:{e.Message}");
}
}
///
/// 校验失败人工确认事件
///
///
///
///
///
///
private bool MatPutInValidHandleEvent(int validType,MesProductPlan productPlan, string materialName,string msg)
{
var result = false;
Application.Current.Dispatcher.Invoke(() =>
{
var alarmWindow = new SystemAlarmWindow();
var viewModel = new SystemAlarmViewModel();
viewModel.AlarmMsg = msg;
viewModel.AlarmConfirmed += (sender, isConfirmed) =>
{
result = isConfirmed;
alarmWindow.Close();
};
alarmWindow.DataContext = viewModel;
alarmWindow.ShowDialog();
});
return result;
}
///
/// 系统运行日志输出
///
///
private async void PrintMessageToListBox(string message)
{
await Task.Run(() =>
{
try
{
string formattedMessage = $"{DateTime.Now.ToString("HH:mm:ss.ss")} ==> {message}";
lock (listItems)
{
listItems.Add(formattedMessage);
while (listItems.Count > 120)
{
listItems.RemoveAt(0);
}
var orderedList = listItems.OrderByDescending(x => x).ToList(); // 排序后转为 List
Application.Current.Dispatcher.Invoke(() =>
{
LogInfoListBox = orderedList; // 更新 UI
});
}
}
catch (Exception ex)
{
_logger.LogError($"日志数据绑定异常:{ex.Message}");
}
});
}
}
}