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 private bool _isEnabledRoleName; private RoleItem _treeSelectedRole; private CtrlMode _controlMode = CtrlMode.VIEW; #endregion #region Constructors public RoleViewModel() { DisplayName = "Role"; } #endregion #region Properties /// /// 返回当前视图是否具有访问权限 /// public bool IsPermission => Permission == 3; public bool IsEnabledRoleName { get => _isEnabledRoleName; set { _isEnabledRoleName = value; NotifyOfPropertyChange(); } } public static RoleManager RoleManager => RoleManager.Instance; public ObservableCollection PermissionDictionary => RolePermissionMapper.Instance.PermissionDictionary; public ObservableCollection PermissionTrueFalseDictionary => RolePermissionTrueFalseMapper.Instance.PermissionDictionary; public ObservableCollection RoleList { get; } = new(); public RoleItem TreeSelectedRole { get => _treeSelectedRole; set { _treeSelectedRole = value; NotifyOfPropertyChange(); } } public CtrlMode ControlMode { get => _controlMode; set { _controlMode = value; NotifyOfPropertyChange(); } } #endregion #region Methods protected override void OnActivate() { RoleList.Clear(); _treeSelectedRole = null; 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()) { ControlMode = CtrlMode.VIEW; DialogBox.ShowInfo("Operated successfully."); } } } base.OnDeactivate(close); } public void OnRoleChanged() { var roleNameList = new[] { "管理员", "设备工程师", "工艺工程师", "操作员" }; var roleName = roleNameList.Contains(TreeSelectedRole.DisplayRoleName) ? TreeSelectedRole.DisplayRoleName : ""; IsEnabledRoleName = true; if (!string.IsNullOrEmpty(roleName)) 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()) ControlMode = CtrlMode.EDIT; } public bool OnAutoLogoutTimeChecked(object sender) { ControlMode = CtrlMode.EDIT; return ((CheckBox)(sender)).IsChecked.Value; } private bool SaveChanges() { if (string.IsNullOrWhiteSpace(TreeSelectedRole.DisplayRoleName)) { 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) { 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; } private void LoadRoleList() { RoleList.Clear(); var roles = RoleManager.GetAllRoles(); if (roles == null || roles.Count == 0) return; foreach (var r in roles) { var treeRole = RoleManager.CloneRole(r); if (treeRole != null) { RoleList.Add(treeRole); } } TreeSelectedRole = RoleList.FirstOrDefault(); TreeSelectedRole.IsSelected = true; ControlMode = CtrlMode.VIEW; } private void OnRoleTreeSelectedChanged(EventCommandParameter arg) { var roleItem = arg.CustomParameter as RoleItem; if (roleItem == null) return; TreeSelectedRole = roleItem; } private void OnBtnAddRoleCommand(object arg) { var newRole = RoleManager.CreateRole(); if (newRole != null) { RoleList.Add(newRole); TreeSelectedRole = newRole; TreeSelectedRole.IsSelected = true; } ControlMode = CtrlMode.EDIT; } private void OnBtnDeleteRoleCommand(object arg) { var lstRoleCanNotDelete = new List() { "管理员", "设备工程师", "工艺工程师", "操作员" }; if (TreeSelectedRole == null) return; if (lstRoleCanNotDelete.Contains(TreeSelectedRole.DisplayRoleName)) { DialogBox.ShowWarning("Can not delete a fixed role"); return; } if (!DialogBox.Confirm("Are you sure that you want to delete this role?")) { return; } if (BaseApp.Instance.UserContext.Role.RoleId == TreeSelectedRole.RoleId) { DialogBox.ShowWarning("The action cannot be completed because {0} is currently in use.", "the role"); return; } if(TreeSelectedRole.RoleId == "0" || TreeSelectedRole.RoleId == "1" || TreeSelectedRole.RoleId == "2" || TreeSelectedRole.RoleId == "3") { DialogBox.ShowWarning("Can not delete a fixed role"); return; } try { var index = RoleList.IndexOf(TreeSelectedRole); RoleList.Remove(TreeSelectedRole); RoleManager.DeleteRole(TreeSelectedRole.RoleId); index = index > 1 ? index - 1 : 0; TreeSelectedRole = RoleList == null ? null : RoleList[index]; TreeSelectedRole.IsSelected = true; DialogBox.ShowInfo("Operated successfully."); } catch (Exception ex) { LOG.Error(ex.StackTrace); DialogBox.ShowInfo("Operation failed."); } } private void OnBtnCloneRoleCommand(object arg) { if (_treeSelectedRole != null) { var newRole = RoleManager.CreateRole(_treeSelectedRole); if (newRole != null) { newRole.DisplayRoleName = newRole.RoleName = "Copy of " + newRole.DisplayRoleName; RoleList.Add(newRole); TreeSelectedRole = newRole; TreeSelectedRole.IsSelected = true; ControlMode = CtrlMode.EDIT; } } } private void OnBtnSaveCommand(object arg) { try { if (SaveChanges()) { DialogBox.ShowInfo("Operated successfully."); ControlMode = CtrlMode.VIEW; } else DialogBox.ShowInfo("Operation failed."); } catch (Exception ex) { LOG.Error(ex.StackTrace); DialogBox.ShowInfo("Operation failed."); } } private void OnBtnCancelRoleCommand(object arg) { LoadRoleList(); ControlMode = CtrlMode.VIEW; } #endregion #region commands private ICommand _roleTreeSelectChangedCmd; public ICommand RoleTreeSelectChangedCommand { get { if (_roleTreeSelectChangedCmd == null) _roleTreeSelectChangedCmd = new BaseCommand>((EventCommandParameter arg) => OnRoleTreeSelectedChanged(arg)); return _roleTreeSelectChangedCmd; } } private ICommand _btnSaveCommand; public ICommand BtnSaveCommand { get { if (_btnSaveCommand == null) _btnSaveCommand = new BaseCommand((object arg) => OnBtnSaveCommand(arg)); return _btnSaveCommand; } } private ICommand _btnAddRoleCommand; public ICommand BtnAddRoleCommand { get { if (_btnAddRoleCommand == null) _btnAddRoleCommand = new BaseCommand((object arg) => OnBtnAddRoleCommand(arg)); return _btnAddRoleCommand; } } private ICommand _btnDeleteRoleCommand; public ICommand BtnDeleteRoleCommand { get { if (_btnDeleteRoleCommand == null) _btnDeleteRoleCommand = new BaseCommand((object arg) => OnBtnDeleteRoleCommand(arg)); return _btnDeleteRoleCommand; } } private ICommand _btnCloneRoleCommand; public ICommand BtnCloneRoleCommand { get { if (_btnCloneRoleCommand == null) _btnCloneRoleCommand = new BaseCommand((object arg) => OnBtnCloneRoleCommand(arg)); return _btnCloneRoleCommand; } } private ICommand _btnCancelRoleCommand; public ICommand BtnCancelRoleCommand { get { if (_btnCancelRoleCommand == null) _btnCancelRoleCommand = new BaseCommand((object arg) => OnBtnCancelRoleCommand(arg)); return _btnCancelRoleCommand; } } #endregion } }