using BPASmart.Model.配方; using BPASmart.RecipeManagement.Globle; 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 MaterialConfigureViewModel:ObservableObject { public string MaterialName { get { return _materialName; } set { _materialName = value; OnPropertyChanged(); } } private string _materialName; public ObservableCollection MaterialTypes { get; set; } = new ObservableCollection(); public MaterialType MaterialType { get{ return _materialType; } set { _materialType = value; OnPropertyChanged(); } } private MaterialType _materialType = 0; public string MaterialPosion { get { return _materialPosion; } set { _materialPosion = value; OnPropertyChanged(); } } private string _materialPosion; public string ErrorMessage { get { return _errorMessage; } set { _errorMessage = value; OnPropertyChanged(); } } private string _errorMessage; public RelayCommand SaveCommand { get; set; } public MaterialConfigureViewModel() { if(GlobleData.ChangeMaterail != null) { MaterialName = GlobleData.ChangeMaterail.Name; MaterialType = GlobleData.ChangeMaterail.MaterialType; MaterialPosion = GlobleData.ChangeMaterail.MaterialPosion; } foreach(MaterialType item in Enum.GetValues(typeof(MaterialType))) { MaterialTypes.Add(item); } SaveCommand = new RelayCommand(() => { if(MaterialName == null) { ErrorMessage = "原料名称不能为空"; return; } if(GlobleData.ChangeMaterail!=null)//编辑原料 { var res = Json.Data.locaMaterails.FirstOrDefault(p=>p.ID == GlobleData.ChangeMaterail.ID); if (res != null) { res.Name = MaterialName; res.MaterialType = MaterialType; res.MaterialPosion = MaterialPosion; Json.Save(); ErrorMessage = "保存成功"; } } else //添加新原料 { if(Json.Data.locaMaterails != null) { var res = Json.Data.locaMaterails.FirstOrDefault(p => p.Name == MaterialName); if (res != null) { ErrorMessage = "原料名称已存在"; return; } Json.Data.locaMaterails.Add(new RecipeMaterials { ID = Guid.NewGuid().ToString(), Name = MaterialName, MaterialType = MaterialType, MaterialPosion = MaterialPosion }); Json.Save(); ErrorMessage = "保存成功"; } else { Json.Data.locaMaterails.Add(new RecipeMaterials { ID = Guid.NewGuid().ToString(), Name = MaterialName, MaterialType = MaterialType, MaterialPosion = MaterialPosion }); Json.Save(); ErrorMessage = "保存成功"; } } }); } } }