Files
SamplePreSystem-CS/SamplePreSystem.UI/ViewModel/ConfigManager/PermissionViewModel.cs

172 lines
4.7 KiB
C#
Raw Normal View History

2026-04-30 11:34:41 +08:00
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<TreeItemModel> _funList = new List<TreeItemModel>();
public List<TreeItemModel> 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<int> ids = rolesBll.GetPermission(configRole.id);
LoadRolePermissions(ids);
}
/// <summary>
/// 加载指定角色已分配的权限(用于编辑角色权限)
/// </summary>
/// <param name="roleFunctionIds">角色已拥有的功能ID列表</param>
public void LoadRolePermissions(List<int> 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<int> selectedIds = GetSelectedFunctionIds();
if (selectedIds.Count == 0)
{
MessageBox.Show("请至少选择一个功能权限!", "提示");
return false;
}
List<config_role_permisson> rolePermissonList = new List<config_role_permisson>();
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<int> GetSelectedFunctionIds()
{
List<int> selectedIds = new List<int>();
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;
}
}
}