using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using BPA.Helper; using System.Collections.ObjectModel; using BPA.Helper; namespace BPASmartClient.SmallBatchingSystem.ViewModels { public class NewRecipeViewModel : BaseModel { public NewRecipeViewModel() { Json.Data.SiloInfoModels.ToList()?.ForEach(item => { SileName.Add(item.SiloName); }); ActionManage.GetInstance.Register(new Action((o) => { if (o != null && o is RecipeInfo tempRecipeInfo) { RecipeName = tempRecipeInfo.RecipeName; tempRecipeInfo.SiloInfoModels.ToList()?.ForEach(item => { int tempIndex = Array.FindIndex(SileName.ToArray(), p => p == item.SiloName); SiloInfos.Add(new RecipeRawMaterialInfo() { SiloName = item.SiloName, SiloWeight = item.SiloWeight, SelectIndex = tempIndex }); }); Index = Array.FindIndex(Json.Data.Recipes.ToArray(), p => p.RecipeName == tempRecipeInfo.RecipeName); } }), "OpenNewRecipe", true); AddCommand = new BPARelayCommand(() => { SiloInfos.Add(new RecipeRawMaterialInfo()); }); CancelCommand = new BPARelayCommand(() => { ActionManage.GetInstance.Send("NewRecipeViewModelClose"); }); SaveCommand = new BPARelayCommand(() => { if (Index >= 0 && Index < Json.Data.OutletInfoModels.Count) { var res = Array.FindIndex(Json.Data.Recipes.ToArray(), p => p.RecipeName == RecipeName); if (res >= 0 && res != Index) { ErrorInfo = "配方名称已经存在!"; return; } Json.Data.Recipes.ElementAt(Index).RecipeName = RecipeName; Json.Data.Recipes.ElementAt(Index).SiloInfoModels.Clear(); SiloInfos.ToList()?.ForEach(item => { Json.Data.Recipes.ElementAt(Index).SiloInfoModels.Add(item); }); Control.GetInstance.OperationLog($"{RecipeName} 编辑完成"); } else { var res = Json.Data.Recipes.FirstOrDefault(p => p.RecipeName == RecipeName); if (res != null) { ErrorInfo = "配方名称已经存在!"; return; } Json.Data.Recipes.Add(new RecipeInfo() { RecipeName = RecipeName, SiloInfoModels = SiloInfos }); Control.GetInstance.OperationLog($"{RecipeName} 添加成功"); } ActionManage.GetInstance.Send("NewRecipeViewModelClose"); }); RemoveCommand = new BPARelayCommand((o) => { if (!string.IsNullOrEmpty(o?.ToString())) { var res = SiloInfos.FirstOrDefault(p => p.SiloName == o.ToString()); if (res != null) SiloInfos.Remove(res); } }); } public string RecipeName { get { return _mRecipeName; } set { _mRecipeName = value; OnPropertyChanged(); } } private string _mRecipeName; public ObservableCollection SiloInfos { get; set; } = new ObservableCollection(); public ObservableCollection SileName { get; set; } = new ObservableCollection(); } }