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.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 { public int Id { get; set; } = 0; public static int materialNum { get; set; } public ObservableCollection flowProcessModels { get; set; } = new ObservableCollection(); 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) //编辑已有工艺 { 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, 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(); //工艺桶号重复性检测 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; } } // name.FlowProcess.fpModels = flowProcessModels; name.FlowProcess.fpModels.Clear(); foreach (var item in flowProcessModels) { name.FlowProcess.fpModels.Add(new FlowProcessModel { FryMaterialNum = item.FryMaterialNum, FrySpeed = item.FrySpeed, FryWeight = item.FryWeight, FryTemperature = item.FryTemperature, FryPeriodTime = item.FryPeriodTime, SmallFirePercent = item.SmallFirePercent, MidFirePercent = item.MidFirePercent, BigFirePercent = item.BigFirePercent, StrongFirePercent = item.StrongFirePercent }); } //foreach (var item in name.materialCollection) //{ // string s= Json.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; }); } } }