using Admin.Core.Common; using Admin.Core.IService; using Admin.Core.Model; using Admin.Core.Service; using Aucma.Core.HwPLc; using Aucma.Core.SheetMetal.Business; using Aucma.Core.SheetMetal.Models; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using CommunityToolkit.Mvvm.Messaging; using log4net; using Microsoft.Extensions.DependencyInjection; using Microsoft.IdentityModel.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Reflection.Metadata; using System.Text; using System.Threading.Tasks; using System.Windows; namespace Aucma.Core.SheetMetal.ViewModels { public partial class QuantityIssuedViewModel : ObservableObject { #region 刷新创建计划 /// /// 刷新创建计划 /// public delegate Task RefreshCretaePlanInfo(); public static event RefreshCretaePlanInfo RefreshCretaePlanInfoEvent; #endregion private static readonly log4net.ILog logHelper = LogManager.GetLogger(typeof(QuantityIssuedViewModel)); private IBaseBomInfoServices _bomInfoServices; private IBaseSpaceDetailServices _spaceDetailServices; protected readonly IExecutePlanInfoServices? _executePlanInfoServices; protected readonly IProductPlanInfoServices? _productPlanInfoServices; #region 构造函数 public QuantityIssuedViewModel(ProductPlanInfoModel productPlanInfo) { _bomInfoServices = App.ServiceProvider.GetService(); _spaceDetailServices = App.ServiceProvider.GetService(); _executePlanInfoServices = App.ServiceProvider.GetService(); _productPlanInfoServices = App.ServiceProvider.GetService(); PlanInfo = productPlanInfo; } #endregion #region 属性 private ProductPlanInfoModel _PlanInfo = new ProductPlanInfoModel(); public ProductPlanInfoModel PlanInfo { get => _PlanInfo; set => SetProperty(ref _PlanInfo, value); } private string _TransmitAmount = string.Empty; public string TransmitAmount { get => _TransmitAmount; set => SetProperty(ref _TransmitAmount, value); } #endregion #region 下传计划 /// /// 下传计划,前后板联动计划创建两条分别为前后板计划 /// [RelayCommand] private async Task PlanInfoTransmit() { if (string.IsNullOrEmpty(TransmitAmount)) { MessageBox.Show("计划数量不能为空!", "系统提醒"); return; } if (PlanInfo.PlanAmount<=Convert.ToInt32(TransmitAmount)) { MessageBox.Show("下发数量不能大于计划数量!", "系统提醒"); return; } //根据传入的订单号查询 前后板完成清理,如果数量超出给出提醒 var productPlanInfo = _PlanInfo; if (productPlanInfo != null) { //下传到PLC string stationCode = Appsettings.app("StationInfo", "StationCode"); var list = await _executePlanInfoServices.QueryAsync(d => d.ProductLineCode.Equals(stationCode)); ExecutePlanInfo task = new ExecutePlanInfo(); task.ExecutePlanCode = Guid.NewGuid().ToString(); task.ProductPlanCode = PlanInfo.PlanCode; task.OrderCode = PlanInfo.OrderCode; task.ProductLineCode = stationCode;//计划工位 task.TaskCode = GetMaxNum(PlanInfo.PlanCode);// DateTime.Now.ToString("yyMMddHHmmss"); task.MaterialCode = PlanInfo.MaterialCode; task.MaterialName = PlanInfo.MaterialName; if (list.Count == 0) task.ExecuteOrder = 1; if (list.Count != 0) task.ExecuteOrder = list.Max(d => d.ExecuteOrder) + 1; task.ExecuteMethod = 2;//不做要求,系统自动确定 task.PlanAmount = Convert.ToInt32(TransmitAmount); task.CompleteAmount = 0; task.CreatedTime = DateTime.Now; task.BeginTime = DateTime.Now; task.ExecuteStatus = 1; task.PlanType = productPlanInfo.PlanType; task.MaterialSpecificatons = productPlanInfo.MaterialSpecificatons; var result = await _executePlanInfoServices.AddAsync(task); if (result > 0) { var obj = await _productPlanInfoServices.FirstAsync(d => d.ProductLineCode == stationCode && d.OrderCode == PlanInfo.OrderCode); var execPlanList= _executePlanInfoServices.QueryAsync(d=>d.OrderCode == PlanInfo.OrderCode).Result; if (execPlanList.Count==0) { obj.BeginTime = DateTime.Now; await _productPlanInfoServices.UpdateAsync(obj); } RefreshCretaePlanInfoEvent?.Invoke(); MessageBox.Show("计划下发成功!", "系统提醒"); } else { MessageBox.Show("计划拆分失败,请检查后重试!", "系统提醒"); } } else { MessageBox.Show("生产计划获取失败,加载为空", "提示", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK, MessageBoxOptions.DefaultDesktopOnly); return; } } #endregion #region 清除 /// /// 清除 /// [RelayCommand] private void ClearTransmitAmount() { string amount = _TransmitAmount.ToString(); if (amount.Length > 0) { TransmitAmount = amount.Substring(0, amount.Length - 1); } } #endregion #region KeypadButton [RelayCommand] private void KeypadButton(object obj) { var info = obj as string; TransmitAmount += info; } #endregion #region 关闭 /// /// 关闭 /// /// [RelayCommand] private void CloseWindow(object parameter) { var window = parameter as Window; if (window != null) { window.Close(); } } #endregion #region 获取最大值 /// /// 获取最大值 /// /// MES编码 /// public string GetMaxNum(string mesId) { try { List tempList = new List(); var list = _executePlanInfoServices.QueryAsync(d => d.TaskCode.Contains(mesId)).Result; if (list.Count() == 0) { return mesId + "0001"; } foreach (var item in list) { string code = item.TaskCode.Substring(item.TaskCode.Length - 4); int num = Convert.ToInt32(code); tempList.Add(num); } string maxStr = (tempList.Max() + 1).ToString(); string taskCode = mesId+ maxStr.PadLeft(6, '0'); return taskCode; } catch { return mesId + "0001"; } } #endregion } }