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 Dictionary materialsName { get; set; } = new Dictionary(); /// /// 配方名称 /// 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; } recipeMaterials?.ToList().ForEach(p => { if (materialsName.ContainsKey(p.ID)) p.Name = materialsName[p.ID]; }); 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(); ActionManage.GetInstance.Send("CloseRecipesConfigureView"); }); DeleteCommand = new RelayCommand(Delete); if (Json.Data.locaMaterails.Count > 0) { foreach (var materail in Json.Data.locaMaterails) { materialsName.Add(materail.ID,materail.Name); } } if (Globle.GlobleData.ChangeRecipes != null) { RecipeName = Globle.GlobleData.ChangeRecipes.Name.ToString(); recipeMaterials = Globle.GlobleData.ChangeRecipes.recipeMaterials; } } } }