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; 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.Names; 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.MaterialName)) FlowItems.Insert(0, item.MaterialName);//下拉框 } 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 }); } // 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.MaterialName)) FlowItems.Insert(0, item.MaterialName);//下拉框 } 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(); // 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 }); } foreach (var item in name.materialCollection) { string s= Json.Data.Names.FirstOrDefault(p=>p == item.MaterialName); 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; }); } } }