using BPASmartClient.CustomResource.Pages.Model; using BPASmartClient.CustomResource.Pages.View; using BPASmartClient.Helper; using BPASmartClient.Model; 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; using System.Windows; namespace BPASmartClient.CustomResource.Pages.ViewModel { public class UserConfigViewModel:ObservableObject { /// /// 页面列表集合 /// public ObservableCollection pageTreeViewModels { get; set; } = new ObservableCollection(); /// /// 已选择页面 /// public ObservableCollection SelectePage { get; set; } = new ObservableCollection(); /// /// 本地原料集合 /// public ObservableCollection LoaclMaterails { get; set; } = Json.Data.LocalMaterails; /// /// 设备原料集合 /// public ObservableCollection DeviceMaterails { get; set; } = Global.DeviceRawMaterials; /// /// 用户本地原料权限 /// public ObservableCollection UserLoaclMaterails { get; set; } = new ObservableCollection(); /// /// 用户设备原料权限 /// public ObservableCollection UserDeviceMaterails { get; set; } = new ObservableCollection(); /// /// 切换本地和设备原料权限 /// public bool IsChecked { get { return _isChecked; } set { _isChecked = value;OnPropertyChanged(); } } private bool _isChecked = true; /// /// 用户名 /// public string UserName { get { return _username; } set { _username = value; OnPropertyChanged(); } } private string _username; /// /// 本地原料权限显示 /// public Visibility VisibilityLocal { get { return _visibilityLocal; } set { _visibilityLocal = value; OnPropertyChanged(); } } private Visibility _visibilityLocal = Visibility.Visible; /// /// 设备原料权限显示 /// public Visibility VisibilityDevice { get { return _visibilityDevice; } set { _visibilityDevice = value; OnPropertyChanged(); } } private Visibility _visibilityDevice = Visibility.Collapsed; public RelayCommand AddUserPageCommand { get; set; } public RelayCommand DeleteUserPageCommand { get; set; } public RelayCommand DeleteAllCommand { get; set; } public RelayCommand AddAllCommand { get; set; } public RelayCommand ChangeMaterailPageCommand { get; set; } public RelayCommand SaveCommand { get; set; } public RelayCommand CancelCommand { get; set; } public RelayCommand AddLoaclMaterialCommand { get; set; } public RelayCommand DeleteLoaclMaterialCommand { get; set; } public RelayCommand AddDeviceMaterialCommand { get; set; } public RelayCommand DeleteDeviceMaterialCommand { get; set; } private void AddUserPage(object o) { if (o == null) return; if(o is string name) { var s = SelectePage.FirstOrDefault(x => x.Name == name); if (!SelectePage.Contains(s)) { foreach (UserTreeViewModel item in pageTreeViewModels) { var res = item.TreeViewItems.FirstOrDefault(P => P.Name == name); if(res != null) { SelectePage.Add(res); return; } } } } } private void DeleteUserPage(object o) { if (o == null) return; if (o is string name) { var res = SelectePage.FirstOrDefault(p=>p.Name == name); SelectePage.Remove(res); } } private void ChangeMaterailPage(object o) { if (o == null) return; if (o is string type) { if(type == "Local") {IsChecked = true;VisibilityLocal = Visibility.Visible; VisibilityDevice = Visibility.Collapsed; } else {IsChecked = false; VisibilityDevice = Visibility.Visible;VisibilityLocal = Visibility.Collapsed; } } } private void AddLoaclMaterial(object o) { if (o == null) return; if (o is string materail) { var res = LoaclMaterails.FirstOrDefault(p=>p.RawMaterialName == materail); if(UserLoaclMaterails.FirstOrDefault(p=>p.RawMaterialName == materail)==null) UserLoaclMaterails.Add(res); } } private void DeleteLoaclMaterial(object o) { if (o == null) return; if (o is string materail) { var res = UserLoaclMaterails.FirstOrDefault(p => p.RawMaterialName == materail); UserLoaclMaterails.Remove(res); } } private void AddDeviceMaterial(object o) { if (o == null) return; if (o is string materail) { var res = DeviceMaterails.FirstOrDefault(p => p.RawMaterialName == materail); if(UserDeviceMaterails.FirstOrDefault(p=>p.DeviceIp == res.DeviceIp) == null) { UserDeviceMaterails.Add(res); } } } private void DeleteDeviceMaterial(object o) { if (o == null) return; if (o is string materail) { var res = UserDeviceMaterails.FirstOrDefault(p => p.RawMaterialName == materail); UserDeviceMaterails.Remove(res); } } private void AddAll(object o) { if (o == null) return; if (o.ToString() == "page") { SelectePage.Clear(); foreach (UserTreeViewModel item in pageTreeViewModels) { foreach (var res in item.TreeViewItems) { SelectePage.Add(res); } } } else if (o.ToString() == "local") { UserLoaclMaterails.Clear(); foreach (RawMaterialModel item in LoaclMaterails) { UserLoaclMaterails.Add(item); } } else if (o.ToString() == "device") { UserDeviceMaterails.Clear(); foreach (RawMaterialModel item in DeviceMaterails) { UserDeviceMaterails.Add(item); } } } private void DeleteAll(object o) { if (o == null) return; if(o.ToString() == "page") { SelectePage.Clear(); } else if(o.ToString() == "local") { UserLoaclMaterails.Clear(); } else if(o.ToString() == "device") { UserDeviceMaterails.Clear(); } } public UserConfigViewModel() { AddUserPageCommand = new RelayCommand(AddUserPage); DeleteUserPageCommand = new RelayCommand(DeleteUserPage); DeleteAllCommand = new RelayCommand(DeleteAll); AddAllCommand = new RelayCommand(AddAll); ChangeMaterailPageCommand = new RelayCommand(ChangeMaterailPage); SaveCommand = new RelayCommand(() => { var res = Global.userManager.userInfos.FirstOrDefault(p => p.UserName == Global.changeUserInfo.UserName && p.Password == Global.changeUserInfo.Password); if(res != null) { res.userTreeViewModels = new List(); res.userTreeViewModels.AddRange(SelectePage); res.locaRawMaterials = new List(); res.locaRawMaterials.AddRange(UserLoaclMaterails); res.devRawMaterials = new List(); res.devRawMaterials.AddRange(UserDeviceMaterails); Config.GetInstance.SaveUser(); } Global.changeUserInfo = null; ActionManage.GetInstance.Send("UserConfigViewConfirm"); }); CancelCommand = new RelayCommand(() => { Global.changeUserInfo = null; ActionManage.GetInstance.Send("UserConfigViewCancel"); }); AddLoaclMaterialCommand = new RelayCommand(AddLoaclMaterial); DeleteLoaclMaterialCommand = new RelayCommand(DeleteLoaclMaterial); AddDeviceMaterialCommand = new RelayCommand(AddDeviceMaterial); DeleteDeviceMaterialCommand = new RelayCommand(DeleteDeviceMaterial); foreach (var item in MenuManage.GetInstance.menuModels) { List list = new List(); foreach(var res in item.subMenumodels) { list.Add(new UserTreeViewModel { Name = res.SubMenuName}); } pageTreeViewModels.Add(new UserTreeViewModel { Name = item.MainMenuName, TreeViewItems = list }); } //用户已拥有权限 if(Global.changeUserInfo != null) { UserName = Global.changeUserInfo.UserName; if (Global.changeUserInfo.userTreeViewModels != null) { if(Global.changeUserInfo.userTreeViewModels.Count > 0) { foreach(UserTreeViewModel item in Global.changeUserInfo.userTreeViewModels) { SelectePage.Add(item); } } if(Global.changeUserInfo.locaRawMaterials.Count>0) { foreach(RawMaterialModel model in Global.changeUserInfo.locaRawMaterials) { UserLoaclMaterails.Add(model); } } if (Global.changeUserInfo.devRawMaterials.Count > 0) { foreach (RawMaterialModel model in Global.changeUserInfo.devRawMaterials) { UserDeviceMaterails.Add(model); } } } } } } }