using BPASmartClient.CustomResource.Pages.Model; using BPASmartClient.CustomResource.UserControls; using BPASmartClient.CustomResource.UserControls.MessageShow; using BPASmartClient.Helper; using FryPot_DosingSystem.Model; using Microsoft.Toolkit.Mvvm.ComponentModel; using Microsoft.Toolkit.Mvvm.Input; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; namespace FryPot_DosingSystem.ViewModel { internal class FlowProcessSetViewModel:ObservableObject { public int Id { get; set; } = 0; public static int materialNum { get; set; } public ObservableCollection flowProcessModels { get; set; } = new ObservableCollection(); /// /// 目标重量偏差 /// public string targetWeightOffset { get { return _targetWeightOffset; } set { _targetWeightOffset = value;OnPropertyChanged(); } } private string _targetWeightOffset="0"; private string _stirTime="0"; /// /// 秘制配方搅拌时间 /// public string StirTime { get { return _stirTime; } set { _stirTime = value; OnPropertyChanged(); } } public string currentRecipeName { get; set; } public static int currentItemId { get; set; } public RelayCommand CloseWindowCommand { get; set; } public ObservableCollection FlowItems { get; set; } = new ObservableCollection(); public RelayCommand ConfirmCommand { get; set; } public RelayCommand AddLastFlowItemCommand { get; set; } public RelayCommand AddFrontFlowItemCommand { get; set; } public RelayCommand DeleteFlowItemCommand { get; set; } public FlowProcessSetViewModel() { Json.Read(); // FlowProcessNames.GetInstance.Names = Json.Data.Names; // FlowItems = Json.Data.NameId; foreach (KeyValuePair item in Json.Data.NameId) { FlowItems.Add(item.Key); } ActionManage.GetInstance.Register(new Action((obj) => { if (obj != null) try { currentItemId = Convert.ToInt32(obj); } catch (Exception) { } }), "CurrentItemId"); //ActionManage.GetInstance.Register(new Action(obj => //{ // if (obj != null) // materialNum = Convert.ToInt32(obj); //}), "MaterialNum"); ActionManage.GetInstance.Register(new Action(recipeName => { if (recipeName != null) { currentRecipeName = recipeName.ToString(); var res = Json.Data.Recipes.FirstOrDefault(p => p.FlowProcess?.RecipeName == recipeName?.ToString()); if (res != null && res is NewRecipeModel recipe) //编辑已有工艺 { targetWeightOffset = recipe.FlowProcess.targetWeightOffset.ToString(); StirTime=recipe.FlowProcess.StirTime.ToString(); foreach (var item in recipe.materialCollection) { if (!FlowItems.Contains(item.MaterialLoc.ToString())) FlowItems.Add(item.MaterialLoc.ToString());//下拉框 } foreach (var item in recipe.FlowProcess.fpModels) { flowProcessModels.Add(new FlowProcessModel { FryMaterialNum = item.FryMaterialNum, FrySpeed = item.FrySpeed, FryWeight = item.FryWeight, TargeWeight=item.TargeWeight, FryTemperature = item.FryTemperature, FryPeriodTime = item.FryPeriodTime, SmallFirePercent = item.SmallFirePercent, MidFirePercent = item.MidFirePercent, BigFirePercent = item.BigFirePercent, StrongFirePercent = item.StrongFirePercent }); } // ActionManage.GetInstance.Send("MaterialNum", recipe.materialCollection.Count); } else//创建新工艺 { var name = Json.Data.Recipes.FirstOrDefault(p => p.RecipeName == recipeName.ToString()); if (name != null) { name.FlowProcess.RecipeName = recipeName.ToString(); foreach (var item in name.materialCollection) { if (!FlowItems.Contains(item.MaterialLoc.ToString())) FlowItems.Add(item.MaterialLoc.ToString());//下拉框 } ActionManage.GetInstance.Send("MaterialNum", name.materialCollection.Count); //name.FlowProcess.fpModels = flowProcessModels; } } } }), "EditFlowProcess"); CloseWindowCommand = new RelayCommand(() => { ActionManage.GetInstance.Send("CloseFlowProcessView"); }); ConfirmCommand = new RelayCommand(() => { var name = Json.Data.Recipes.FirstOrDefault(p => p.RecipeName == currentRecipeName.ToString()); if (name != null) { name.FlowProcess.RecipeName = currentRecipeName.ToString(); if (float.TryParse(targetWeightOffset.Trim(), out float offset)) { name.FlowProcess.targetWeightOffset = offset;//目标重量偏差 } else { MessageBox.Show("非法目标重量偏差设置", "提示", MessageBoxButton.OK, MessageBoxImage.Error); name.FlowProcess.targetWeightOffset = 0.0F; return; } if (ushort.TryParse(StirTime.Trim(), out ushort time)) { name.FlowProcess.StirTime = time;//秘制配方搅拌时间 } else { MessageBox.Show("非法秘制配方搅拌时间设置", "提示", MessageBoxButton.OK, MessageBoxImage.Error); name.FlowProcess.StirTime = 0; return; } //工艺桶号重复性检测 foreach (var item in name.materialCollection) { var res = flowProcessModels.Where(p => p.FryMaterialNum == item.MaterialLoc.ToString()); if (res.Count() > 1) { // NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, App.MainWindow, "提示", $"保存失败,桶号重复!"); MessageNotify.GetInstance.ShowUserLog("保存失败,桶号重复"); MessageBox.Show("保存失败,桶号重复", "提示", MessageBoxButton.OK, MessageBoxImage.Error); return; } } //工艺步骤中清洗工艺重复性检测 if (flowProcessModels.FirstOrDefault(p=>p.FryMaterialNum=="清洗")!=null) { if (flowProcessModels.Where(p => p.FryMaterialNum == "清洗").Count() > 1||flowProcessModels.IndexOf(flowProcessModels.FirstOrDefault(p => p.FryMaterialNum == "清洗")).Data.Names.FirstOrDefault(p=>p == item.MaterialLoc.ToString()); // Json.Data.Names.Remove(s); //} } Json.Save(); Json.Save(); ActionManage.GetInstance.Send("CloseFlowProcessView"); }); AddFrontFlowItemCommand = new RelayCommand(() => { try { if (currentItemId == 0) { flowProcessModels.Insert(0, new FlowProcessModel()); } else { flowProcessModels.Insert(currentItemId, new FlowProcessModel()); currentItemId = currentItemId + 1; } } catch (Exception) { //throw; } }); AddLastFlowItemCommand = new RelayCommand(() => { try { if (flowProcessModels.Count <= 0) { flowProcessModels.Insert(0, new FlowProcessModel()); } else if (currentItemId != 0) { flowProcessModels.Insert(currentItemId + 1, new FlowProcessModel()); } else { flowProcessModels.Add(new FlowProcessModel()); } } catch (Exception) { // throw; } }); DeleteFlowItemCommand = new RelayCommand(() => { if (flowProcessModels.Count > 0) flowProcessModels.RemoveAt(currentItemId); currentItemId = 0; }); } } }