Sic.Framework-Nanjing-Baishi/MECF.Framework.UI.Client/CenterViews/Configs/Roles/RoleViewModel.cs

376 lines
11 KiB
C#
Raw Normal View History

2023-04-13 11:51:03 +08:00
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Aitex.Core.RT.Log;
using MECF.Framework.UI.Client.ClientBase;
using OpenSEMI.ClientBase;
using OpenSEMI.ClientBase.Command;
namespace MECF.Framework.UI.Client.CenterViews.Configs.Roles
{
public class RoleViewModel : BaseModel
{
#region Variables
2023-04-13 11:51:03 +08:00
private bool _isEnabledRoleName;
private RoleItem _treeSelectedRole;
private CtrlMode _controlMode = CtrlMode.VIEW;
#endregion
#region Constructors
public RoleViewModel()
2023-04-13 11:51:03 +08:00
{
DisplayName = "Role";
2023-04-13 11:51:03 +08:00
}
#endregion
2023-04-13 11:51:03 +08:00
#region Properties
/// <summary>
/// 返回当前视图是否具有访问权限
/// </summary>
public bool IsPermission => Permission == 3;
public bool IsEnabledRoleName
2023-04-13 11:51:03 +08:00
{
get => _isEnabledRoleName;
set { _isEnabledRoleName = value; NotifyOfPropertyChange(); }
2023-04-13 11:51:03 +08:00
}
public static RoleManager RoleManager => RoleManager.Instance;
public ObservableCollection<PermissionType> PermissionDictionary => RolePermissionMapper.Instance.PermissionDictionary;
public ObservableCollection<PermissionType> PermissionTrueFalseDictionary => RolePermissionTrueFalseMapper.Instance.PermissionDictionary;
public ObservableCollection<RoleItem> RoleList { get; } = new();
2023-04-13 11:51:03 +08:00
public RoleItem TreeSelectedRole
2023-04-13 11:51:03 +08:00
{
get => _treeSelectedRole;
set
{
_treeSelectedRole = value;
NotifyOfPropertyChange();
}
}
public CtrlMode ControlMode
{
get => _controlMode;
set
{
_controlMode = value;
NotifyOfPropertyChange();
}
2023-04-13 11:51:03 +08:00
}
#endregion
#region Methods
2023-04-13 11:51:03 +08:00
protected override void OnActivate()
{
RoleList.Clear();
_treeSelectedRole = null;
2023-04-13 11:51:03 +08:00
RoleManager.Initialize();
LoadRoleList();
base.OnActivate();
}
protected override void OnDeactivate(bool close)
{
if (ControlMode == CtrlMode.EDIT && IsPermission)
{
if (DialogBox.Confirm("The data has been modified. Do you want to save the change(s)?"))
{
if (SaveChanges())
2023-04-13 11:51:03 +08:00
{
ControlMode = CtrlMode.VIEW;
DialogBox.ShowInfo("Operated successfully.");
}
}
}
base.OnDeactivate(close);
}
public void OnRoleChanged()
{
var roleNameList = new[] { "管理员", "设备工程师", "工艺工程师", "操作员" };
var roleName = roleNameList.Contains(TreeSelectedRole.DisplayRoleName) ? TreeSelectedRole.DisplayRoleName : "";
2023-04-13 11:51:03 +08:00
IsEnabledRoleName = true;
if (!string.IsNullOrEmpty(roleName))
2023-04-13 11:51:03 +08:00
IsEnabledRoleName = false;
if (ControlMode == CtrlMode.EDIT)
{
if (!string.IsNullOrEmpty(TreeSelectedRole.DisplayRoleName) && TreeSelectedRole.DisplayRoleName.Length > 18)
TreeSelectedRole.DisplayRoleName = TreeSelectedRole.DisplayRoleName.Substring(0, 18);
return;
}
//check role to set the mode from view to edit
if (_treeSelectedRole != null && _treeSelectedRole.IsRoleChanged())
2023-04-13 11:51:03 +08:00
ControlMode = CtrlMode.EDIT;
}
2023-04-13 11:51:03 +08:00
public bool OnAutoLogoutTimeChecked(object sender)
{
ControlMode = CtrlMode.EDIT;
return ((CheckBox)(sender)).IsChecked.Value;
}
private bool SaveChanges()
2023-04-13 11:51:03 +08:00
{
if (string.IsNullOrWhiteSpace(TreeSelectedRole.DisplayRoleName))
2023-04-13 11:51:03 +08:00
{
DialogBox.ShowWarning("{0} cannot be empty.", "Role name");
//TreeSelectedRole.DisplayRoleName = "No Name";
return false;
}
if (IsRoleExists(TreeSelectedRole))
{
DialogBox.ShowWarning("{0} already exists.", "Role");
return false;
}
TreeSelectedRole.RoleName = TreeSelectedRole.DisplayRoleName;
TreeSelectedRole.IsAutoLogout = TreeSelectedRole.DisplayIsAutoLogout;
TreeSelectedRole.AutoLogoutTime = TreeSelectedRole.DisplayAutoLogoutTime;
TreeSelectedRole.RoleNameTextSaved = TreeSelectedRole.TimeTextSaved = true;
TreeSelectedRole.Description = TreeSelectedRole.DisplayDescription;
try
{
RoleManager.SaveRole(TreeSelectedRole);
}
catch (Exception ex)
{
LOG.Write(ex);
return false;
}
return true;
}
private bool IsRoleExists(RoleItem role)
2023-04-13 11:51:03 +08:00
{
if (RoleList == null || RoleList.Count == 0)
return false;
var sameNameList = RoleList.Where(t => t.DisplayRoleName == role.DisplayRoleName);
if (sameNameList == null || sameNameList.Count() <= 1)
return false;
return true;
}
2023-04-13 11:51:03 +08:00
private void LoadRoleList()
{
RoleList.Clear();
var roles = RoleManager.GetAllRoles();
2023-04-13 11:51:03 +08:00
if (roles == null || roles.Count == 0)
return;
foreach (var ri in roles)
RoleList.Add(ri);
TreeSelectedRole = RoleList.FirstOrDefault();
2023-04-13 11:51:03 +08:00
TreeSelectedRole.IsSelected = true;
ControlMode = CtrlMode.VIEW;
}
2023-04-13 11:51:03 +08:00
private void OnRoleTreeSelectedChanged(EventCommandParameter<object, RoutedEventArgs> arg)
{
var roleItem = arg.CustomParameter as RoleItem;
2023-04-13 11:51:03 +08:00
if (roleItem == null)
return;
TreeSelectedRole = roleItem;
}
private void OnBtnAddRoleCommand(object arg)
2023-04-13 11:51:03 +08:00
{
var newRole = RoleManager.CreateRole();
2023-04-13 11:51:03 +08:00
if (newRole != null)
{
RoleList.Add(newRole);
2023-04-13 11:51:03 +08:00
TreeSelectedRole = newRole;
TreeSelectedRole.IsSelected = true;
}
ControlMode = CtrlMode.EDIT;
}
private void OnBtnDeleteRoleCommand(object arg)
2023-04-13 11:51:03 +08:00
{
if (TreeSelectedRole == null)
return;
2023-04-13 11:51:03 +08:00
if (TreeSelectedRole.IsBuildIn)
2023-04-13 11:51:03 +08:00
{
DialogBox.ShowWarning("Can not delete a build-in role");
2023-04-13 11:51:03 +08:00
return;
}
if (!DialogBox.Confirm("Are you sure that you want to delete this role?"))
{
return;
}
if (BaseApp.Instance.UserContext.Role.RoleId == TreeSelectedRole.RoleId)
2023-04-13 11:51:03 +08:00
{
DialogBox.ShowWarning("The action cannot be completed because {0} is currently in use.", "the role");
return;
}
2023-04-13 11:51:03 +08:00
try
{
var index = RoleList.IndexOf(TreeSelectedRole);
RoleList.Remove(TreeSelectedRole);
RoleManager.DeleteRole(TreeSelectedRole.RoleId);
2023-04-13 11:51:03 +08:00
index = index > 1 ? index - 1 : 0;
TreeSelectedRole = RoleList == null ? null : RoleList[index];
2023-04-13 11:51:03 +08:00
TreeSelectedRole.IsSelected = true;
// DialogBox.ShowInfo("Operated successfully.");
2023-04-13 11:51:03 +08:00
}
catch (Exception ex)
{
LOG.Error(ex.StackTrace);
DialogBox.ShowInfo("Operation failed.");
}
}
private void OnBtnCloneRoleCommand(object arg)
2023-04-13 11:51:03 +08:00
{
if (_treeSelectedRole != null)
2023-04-13 11:51:03 +08:00
{
var newRole = RoleManager.CreateRole(_treeSelectedRole);
2023-04-13 11:51:03 +08:00
if (newRole != null)
{
newRole.DisplayRoleName = newRole.RoleName = "Copy of " + newRole.DisplayRoleName;
RoleList.Add(newRole);
2023-04-13 11:51:03 +08:00
TreeSelectedRole = newRole;
TreeSelectedRole.IsSelected = true;
ControlMode = CtrlMode.EDIT;
}
}
}
private void OnBtnSaveCommand(object arg)
2023-04-13 11:51:03 +08:00
{
try
{
if (SaveChanges())
2023-04-13 11:51:03 +08:00
{
// DialogBox.ShowInfo("Operated successfully.");
ControlMode = CtrlMode.VIEW;
2023-04-13 11:51:03 +08:00
}
else
DialogBox.ShowInfo("Operation failed.");
}
catch (Exception ex)
{
LOG.Error(ex.StackTrace);
DialogBox.ShowInfo("Operation failed.");
}
}
private void OnBtnCancelRoleCommand(object arg)
2023-04-13 11:51:03 +08:00
{
LoadRoleList();
ControlMode = CtrlMode.VIEW;
}
#endregion
2023-04-13 11:51:03 +08:00
#region commands
private ICommand _roleTreeSelectChangedCmd;
2023-04-13 11:51:03 +08:00
public ICommand RoleTreeSelectChangedCommand
{
get
{
if (_roleTreeSelectChangedCmd == null)
_roleTreeSelectChangedCmd = new BaseCommand<EventCommandParameter<object, RoutedEventArgs>>((EventCommandParameter<object, RoutedEventArgs> arg) => OnRoleTreeSelectedChanged(arg));
return _roleTreeSelectChangedCmd;
2023-04-13 11:51:03 +08:00
}
}
private ICommand _btnSaveCommand;
public ICommand BtnSaveCommand
2023-04-13 11:51:03 +08:00
{
get
{
if (_btnSaveCommand == null)
_btnSaveCommand = new BaseCommand<object>(OnBtnSaveCommand);
return _btnSaveCommand;
2023-04-13 11:51:03 +08:00
}
}
private ICommand _btnAddRoleCommand;
public ICommand BtnAddRoleCommand
2023-04-13 11:51:03 +08:00
{
get
{
if (_btnAddRoleCommand == null)
_btnAddRoleCommand = new BaseCommand<object>(OnBtnAddRoleCommand);
return _btnAddRoleCommand;
2023-04-13 11:51:03 +08:00
}
}
private ICommand _btnDeleteRoleCommand;
public ICommand BtnDeleteRoleCommand
2023-04-13 11:51:03 +08:00
{
get
{
if (_btnDeleteRoleCommand == null)
_btnDeleteRoleCommand = new BaseCommand<object>(OnBtnDeleteRoleCommand);
return _btnDeleteRoleCommand;
2023-04-13 11:51:03 +08:00
}
}
private ICommand _btnCloneRoleCommand;
public ICommand BtnCloneRoleCommand
2023-04-13 11:51:03 +08:00
{
get
{
if (_btnCloneRoleCommand == null)
_btnCloneRoleCommand = new BaseCommand<object>(OnBtnCloneRoleCommand);
return _btnCloneRoleCommand;
2023-04-13 11:51:03 +08:00
}
}
private ICommand _btnCancelRoleCommand;
2023-04-13 11:51:03 +08:00
public ICommand BtnCancelRoleCommand
{
get
{
if (_btnCancelRoleCommand == null)
_btnCancelRoleCommand = new BaseCommand<object>(OnBtnCancelRoleCommand);
return _btnCancelRoleCommand;
2023-04-13 11:51:03 +08:00
}
}
2023-04-13 11:51:03 +08:00
#endregion
2023-04-13 11:51:03 +08:00
2023-04-13 11:51:03 +08:00
2023-04-13 11:51:03 +08:00
2023-04-13 11:51:03 +08:00
2023-04-13 11:51:03 +08:00
}
}