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 { /// <summary> /// 页面列表集合 /// </summary> public ObservableCollection<UserTreeViewModel> pageTreeViewModels { get; set; } = new ObservableCollection<UserTreeViewModel>(); /// <summary> /// 已选择页面 /// </summary> public ObservableCollection<UserTreeViewModel> SelectePage { get; set; } = new ObservableCollection<UserTreeViewModel>(); /// <summary> /// 本地原料集合 /// </summary> public ObservableCollection<RawMaterialModel> LoaclMaterails { get; set; } = Json<LocaMaterial>.Data.LocalMaterails; /// <summary> /// 设备原料集合 /// </summary> public ObservableCollection<RawMaterialModel> DeviceMaterails { get; set; } = Global.DeviceRawMaterials; /// <summary> /// 用户本地原料权限 /// </summary> public ObservableCollection<RawMaterialModel> UserLoaclMaterails { get; set; } = new ObservableCollection<RawMaterialModel>(); /// <summary> /// 用户设备原料权限 /// </summary> public ObservableCollection<RawMaterialModel> UserDeviceMaterails { get; set; } = new ObservableCollection<RawMaterialModel>(); /// <summary> /// 切换本地和设备原料权限 /// </summary> public bool IsChecked { get { return _isChecked; } set { _isChecked = value;OnPropertyChanged(); } } private bool _isChecked = true; /// <summary> /// 用户名 /// </summary> public string UserName { get { return _username; } set { _username = value; OnPropertyChanged(); } } private string _username; /// <summary> /// 本地原料权限显示 /// </summary> public Visibility VisibilityLocal { get { return _visibilityLocal; } set { _visibilityLocal = value; OnPropertyChanged(); } } private Visibility _visibilityLocal = Visibility.Visible; /// <summary> /// 设备原料权限显示 /// </summary> public Visibility VisibilityDevice { get { return _visibilityDevice; } set { _visibilityDevice = value; OnPropertyChanged(); } } private Visibility _visibilityDevice = Visibility.Collapsed; public RelayCommand<object> AddUserPageCommand { get; set; } public RelayCommand<object> DeleteUserPageCommand { get; set; } public RelayCommand<object> DeleteAllCommand { get; set; } public RelayCommand<object> AddAllCommand { get; set; } public RelayCommand<object> ChangeMaterailPageCommand { get; set; } public RelayCommand SaveCommand { get; set; } public RelayCommand CancelCommand { get; set; } public RelayCommand<object> AddLoaclMaterialCommand { get; set; } public RelayCommand<object> DeleteLoaclMaterialCommand { get; set; } public RelayCommand<object> AddDeviceMaterialCommand { get; set; } public RelayCommand<object> 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<object>(AddUserPage); DeleteUserPageCommand = new RelayCommand<object>(DeleteUserPage); DeleteAllCommand = new RelayCommand<object>(DeleteAll); AddAllCommand = new RelayCommand<object>(AddAll); ChangeMaterailPageCommand = new RelayCommand<object>(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<UserTreeViewModel>(); res.userTreeViewModels.AddRange(SelectePage); res.locaRawMaterials = new List<RawMaterialModel>(); res.locaRawMaterials.AddRange(UserLoaclMaterails); res.devRawMaterials = new List<RawMaterialModel>(); 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<object>(AddLoaclMaterial); DeleteLoaclMaterialCommand = new RelayCommand<object>(DeleteLoaclMaterial); AddDeviceMaterialCommand = new RelayCommand<object>(AddDeviceMaterial); DeleteDeviceMaterialCommand = new RelayCommand<object>(DeleteDeviceMaterial); foreach (var item in MenuManage.GetInstance.menuModels) { List<UserTreeViewModel> list = new List<UserTreeViewModel>(); 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); } } } } } } }