using BPASmartClient.CustomResource.Pages.Model; using BPA.Helper; using BPASmartClient.Model; using BPA.Helper; using System; using System.Collections.ObjectModel; using System.Linq; namespace BPASmartClient.FoodStationTest.ViewModel; public class NewMaterialViewModel : NotifyBase { public ObservableCollection Materials { get; set; } = Json.Data.LocalMaterails; public string MaterialName { get { return _materialName; } set { _materialName = value; OnPropertyChanged(); } } private string _materialName = string.Empty; public string ErrorInfo { get { return _mErrorInfo; } set { _mErrorInfo = value; OnPropertyChanged(); } } private string _mErrorInfo; public BPARelayCommand RemoveCommand { get; set; } public BPARelayCommand SaveCommand { get; set; } private void Remove(object o) { if (o == null) return; if (o is string id) { var res = Materials.FirstOrDefault(p => p.RawMaterialId == id); Materials.Remove(res); Json.Save(); MessageNotify.GetInstance.ShowUserLog($"删除原料--{res.RawMaterialName}"); } } public NewMaterialViewModel() { RemoveCommand = new BPARelayCommand(Remove); SaveCommand = new BPARelayCommand(() => { if (MaterialName == String.Empty) { ErrorInfo = "原料名称不能为空"; return; } if (Global.DeviceRawMaterials.FirstOrDefault(p => p.RawMaterialName == MaterialName) != null) { ErrorInfo = "设备中已存在该原料名称"; return; } if (Json.Data.LocalMaterails.FirstOrDefault(p => p.RawMaterialName == MaterialName) != null) { ErrorInfo = "本地原料名称重复"; return; } Json.Data.LocalMaterails.Add(new RawMaterialModel { RawMaterialName = MaterialName, RawMaterialId = Guid.NewGuid().ToString(), RawMaterialSource = 0 }); Json.Save(); MessageNotify.GetInstance.ShowUserLog($"添加原料--{MaterialName}"); }); } }