using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Toolkit.Mvvm.ComponentModel; using System.Collections.ObjectModel; using BPASmartClient.DosingHKProject.Model; using BPASmartClient.Helper; using Microsoft.Toolkit.Mvvm.Input; using BPASmartClient.CustomResource.UserControls.MessageShow; using BPASmartClient.CustomResource.UserControls; using System.Diagnostics; using BPASmartClient.DosingProject; namespace BPASmartClient.DosingHKProject.ViewModel { public class DeviceMaterialParViewModel : ObservableObject { public DeviceMaterialParViewModel() { deviceParModels = Json.Data.deviceParModels; RemoveCommand = new RelayCommand((o) => { var res = deviceParModels.FirstOrDefault(p => p.MaterialName == o?.ToString()); if (res != null) deviceParModels.Remove(res); NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"{o.ToString()}:原料删除成功!"); }); AddCommand = new RelayCommand(() => { deviceParModels.Add(new DeviceParMode()); }); SaveCommand = new RelayCommand(() => { if (deviceParModels == null || deviceParModels.Count <= 0) { NoticeDemoViewModel.OpenMsg(EnumPromptType.Warn, App.MainWindow, "警告", $"没有可保存的参数!"); return; } for (int i = 0; i < deviceParModels.Count; i++) { if (deviceParModels.Where(p => p.MaterialName == deviceParModels.ElementAt(i).MaterialName)?.ToList()?.Count >= 2) deviceParModels.ElementAt(i).IsRedundant = true; else deviceParModels.ElementAt(i).IsRedundant = false; } if (deviceParModels.FirstOrDefault(p => p.IsRedundant == true) != null) { NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, App.MainWindow, "错误", $"原料名称冲突,请检查后重试!"); return; } deviceParModels.Where(P => P.MaterialName.Length <= 0)?.ToList()?.ForEach(item => { Json.Data.deviceParModels.Remove(item); }); Json.Save(); NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"原料参数保存成功!"); }); } public ObservableCollection deviceParModels { get; set; } public RelayCommand RemoveCommand { get; set; } public RelayCommand AddCommand { get; set; } public RelayCommand SaveCommand { get; set; } } }