using BPASmart.Model.配方; using BPASmartClient.Helper; 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; namespace BPASmart.RecipeManagement.ViewModel { public class RecipesConfigureViewModel:ObservableObject { public ObservableCollection recipeMaterials { get; set; } = new ObservableCollection(); /// /// 原料名称集合 /// public ObservableCollection materialsName { get; set; } = new ObservableCollection(); /// /// 配方名称 /// public string RecipeName { get { return _repiceName; } set { _repiceName = value; OnPropertyChanged(); } } private string _repiceName; public string ErrorMessage { get { return _errorMessage; } set { _errorMessage = value; OnPropertyChanged(); } } private string _errorMessage; public RelayCommand AddMaterailsCommand { get; set; } public RelayCommand SaveCommand { get; set; } public RelayCommand DeleteCommand { get; set; } private void Delete(object o) { if (o == null) return; if(o is string id) { var res = recipeMaterials.FirstOrDefault(p=>p.ID == id); if (res != null) recipeMaterials.Remove(res); } } public RecipesConfigureViewModel() { AddMaterailsCommand = new RelayCommand(() => { recipeMaterials.Add(new RecipeMaterials() { ID = Guid.NewGuid().ToString() }); }); SaveCommand = new RelayCommand(() => { if(RecipeName == null) { ErrorMessage = "配方名称为空"; return; } if (Globle.GlobleData.ChangeRecipes!=null) { var res = Json.Data.locaRecipes.FirstOrDefault(p => p.ID == Globle.GlobleData.ChangeRecipes.ID); res.recipeMaterials = recipeMaterials; res.Name = RecipeName; } else { var res = Json.Data.locaRecipes.FirstOrDefault(p => p.Name == RecipeName); if (res != null) { ErrorMessage = "配方名称已存在"; return; } Json.Data.locaRecipes.Add(new Recipes { ID = Guid.NewGuid().ToString() , Name = RecipeName, recipeMaterials = recipeMaterials }); } Json.Save(); }); DeleteCommand = new RelayCommand(Delete); if (Json.Data.locaMaterails.Count > 0) { foreach (var materail in Json.Data.locaMaterails) { materialsName.Add(materail.Name); } } if(Globle.GlobleData.ChangeRecipes != null) { RecipeName = Globle.GlobleData.ChangeRecipes.Name.ToString(); recipeMaterials = Globle.GlobleData.ChangeRecipes.recipeMaterials; } } } }