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 PowerManagerViewModel : ObservableObject { public ObservableCollection<string> UserName { get; set; } = new ObservableCollection<string>(); /// <summary> /// 全部物料 /// </summary> public ObservableCollection<RecipeMaterials> AllMaterials { get; set; } = Json<LocalMaterails>.Data.locaMaterails; /// <summary> /// 用户权限物料 /// </summary> public ObservableCollection<RecipeMaterials> UserMaterials { get; set; } = new ObservableCollection<RecipeMaterials>(); /// <summary> /// 全部配方 /// </summary> public ObservableCollection<Recipes> AllRecipes { get; set; } = Json<LocalRecipes>.Data.locaRecipes; /// <summary> /// 用户权限配方 /// </summary> public ObservableCollection<Recipes> UserRecipes { get; set; } = new ObservableCollection<Recipes>(); public RelayCommand<object> DeleteAllCommand { get; set; } public RelayCommand<object> AddAllCommand { get; set; } public RelayCommand<object> AddMaterialCommand { get; set; } public RelayCommand<object> DeleteMaterialCommand { get; set; } public RelayCommand<object> AddRecipeCommand { get; set; } public RelayCommand<object> DeleteRecipeCommand { get; set; } private void DeleteAll(object o) { if (o == null) return; if (o.ToString() == "Materials") { UserMaterials.Clear(); } else if (o.ToString() == "Recipes") { UserRecipes.Clear(); } } private void AddAll(object o) { if (o == null) return; if (o.ToString() == "Materials") { UserMaterials.Clear(); foreach (RecipeMaterials item in AllMaterials) { UserMaterials.Add(item); } } else if (o.ToString() == "Recipes") { UserRecipes.Clear(); foreach (Recipes item in AllRecipes) { UserRecipes.Add(item); } } } private void AddMaterial(object o) { if (o == null) return; if (o is string materail) { var res = AllMaterials.FirstOrDefault(p => p.Name == materail); if (UserMaterials.FirstOrDefault(p => p.Name == res.Name) == null) { UserMaterials.Add(res); } } } private void DeleteMaterial(object o) { if (o == null) return; if (o is string materail) { var res = UserMaterials.FirstOrDefault(p => p.Name == materail); UserMaterials.Remove(res); } } private void AddRecipe(object o) { if (o == null) return; if (o is string materail) { var res = AllRecipes.FirstOrDefault(p => p.Name == materail); if (UserRecipes.FirstOrDefault(p => p.Name == res.Name) == null) { UserRecipes.Add(res); } } } private void DeleteRecipe(object o) { if (o == null) return; if (o is string materail) { var res = UserRecipes.FirstOrDefault(p => p.Name == materail); UserRecipes.Remove(res); } } public PowerManagerViewModel() { DeleteAllCommand = new RelayCommand<object>(DeleteAll); AddAllCommand = new RelayCommand<object>(AddAll); AddMaterialCommand = new RelayCommand<object>(AddMaterial); DeleteMaterialCommand = new RelayCommand<object>(DeleteMaterial); AddRecipeCommand = new RelayCommand<object>(AddRecipe); DeleteRecipeCommand = new RelayCommand<object>(DeleteRecipe); UserName.Add("admin"); UserName.Add("员工1号"); UserName.Add("员工2号"); UserName.Add("员工3号"); UserName.Add("员工4号"); UserName.Add("员工5号"); UserName.Add("员工6号"); } } }