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 ObservableCollection currentProperty { get; set; } = new ObservableCollection(); 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; } private void AddMaterials() { Json.Data.locaMaterails.Add(new RecipeMaterials { ID = Guid.NewGuid().ToString(), Name = MaterialName, MaterialType = MaterialType, MaterialPosion = MaterialPosion, PropertyCollections = currentProperty, }); } public MaterialConfigureViewModel() { foreach (Property item in Json.Data.PorpertyCollections) { currentProperty.Add(new Property { PropertyName = item.PropertyName, PropertyId = item.PropertyId, _propertyDsecription = item._propertyDsecription }); } if (GlobleData.ChangeMaterail != null) { MaterialName = GlobleData.ChangeMaterail.Name; MaterialType = GlobleData.ChangeMaterail.MaterialType; MaterialPosion = GlobleData.ChangeMaterail.MaterialPosion; foreach (var item in currentProperty) { var res = GlobleData.ChangeMaterail.PropertyCollections.FirstOrDefault(p => p.PropertyId == item.PropertyId); if (res != null) { item.PropertyValue = res.PropertyValue; } } } 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) { if (MaterialName != res.Name)//修改了原料名称 { if (Json.Data.locaMaterails.FirstOrDefault(p => p.Name == MaterialName) != null) { ErrorMessage = "原料名称已存在"; return; } } if (MaterialPosion != res.MaterialPosion)//修改了原料位置 { if (Json.Data.locaMaterails.FirstOrDefault(p => p.MaterialPosion == MaterialPosion) != null) { ErrorMessage = "原料位置重复"; return; } } res.Name = MaterialName; res.MaterialType = MaterialType; res.MaterialPosion = MaterialPosion; res.PropertyCollections = currentProperty; } } else //添加新原料 { if (Json.Data.locaMaterails != null) { var res = Json.Data.locaMaterails.FirstOrDefault(p => p.Name == MaterialName); if (res != null) { ErrorMessage = "原料名称已存在"; return; } var item = Json.Data.locaMaterails.FirstOrDefault(p => p.MaterialPosion == MaterialPosion); if (item != null) { ErrorMessage = "原料位置重复"; } AddMaterials(); } else { AddMaterials(); } } Json.Save(); ActionManage.GetInstance.Send("CloseMaterialConfigureView"); }); } } }