using SamplePre.Models.Ext; using SamplePre.Models.Tables; using SamplePre.ProcessBll.BLL; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using System.Windows; namespace SamplePreSystem.UI.ViewModel.ConfigManager { public class PermissionViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; RolesBll rolesBll = new RolesBll(); config_role configRole; public PermissionViewModel(RoleViewModel vm) { configRole = vm.ConfigRole; RoleName = configRole.name; GetData(); } protected void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } private string _roleName; public string RoleName { get { return _roleName; } set { _roleName = value; } } private List _funList = new List(); public List FunList { get { return _funList; } set { _funList = value; OnPropertyChanged(); } } public void GetData() { var FunListTemp = rolesBll.GetFuncDataTree(); foreach (var item in FunListTemp.Where(p=>p.parent_id == 0)) { // 根节点1 var root1 = new TreeItemModel { Id = item.id, Name = item.name }; foreach(var child in FunListTemp.Where(p => p.parent_id == item.id)) { root1.Children.Add(new TreeItemModel { Id = child.id, Name = child.name, Parent = root1 }); } FunList.Add(root1); } //选择中角色已有权限 List ids = rolesBll.GetPermission(configRole.id); LoadRolePermissions(ids); } /// /// 加载指定角色已分配的权限(用于编辑角色权限) /// /// 角色已拥有的功能ID列表 public void LoadRolePermissions(List roleFunctionIds) { if (roleFunctionIds == null || roleFunctionIds.Count == 0) return; foreach (var item in FunList) { bool isAll = true; foreach (var fuc in item.Children) { if (roleFunctionIds.Contains(fuc.Id)) { fuc.IsChecked = true; //item.IsChecked = true; } else { isAll = false; } } if(isAll) { item.IsChecked = true; } } } public bool SavePermission() { // 获取选中的功能ID列表 List selectedIds = GetSelectedFunctionIds(); if (selectedIds.Count == 0) { MessageBox.Show("请至少选择一个功能权限!", "提示"); return false; } List rolePermissonList = new List(); foreach (int item in selectedIds) { config_role_permisson rolePermisson = new config_role_permisson(); rolePermisson.function_id = item; rolePermisson.role_id = configRole.id; rolePermissonList.Add(rolePermisson); } if (rolesBll.SavePermission(rolePermissonList) < 0) { MessageBox.Show("保存失败!"); return false; } return true; } private List GetSelectedFunctionIds() { List selectedIds = new List(); foreach (var item in FunList) { foreach(var fuc in item.Children) { if(fuc.IsChecked) { //添加父节点功能id if(!selectedIds.Contains(item.Id)) { selectedIds.Add(item.Id); } selectedIds.Add(fuc.Id); } } } return selectedIds; } } }