using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; using System.Xml; using Aitex.Core.Account; using Aitex.Core.UI.MVVM; using Aitex.Core.Util; using Aitex.Triton160.Common; using Aitex.Triton160.UI; using System.ComponentModel; using System.Collections.ObjectModel; using System.Windows.Controls; using System.Windows; using Aitex.Core.RT.Log; namespace Triton160.UI.ViewModel { public class AccountViewModel { public class AccountInfo { public Account Account { get; set; } public int No { get; set; } public string AccountId { get { return Account.AccountId; } } public bool IsEnabled { get { return Account.AccountStatus; } } public string RealName { get { return Account.RealName; } } public string Role { get { return Account.Role; } } public string Department { get { return Account.Department; } } public string LastLoginTime { get { return Account.LastLoginTime; } } public string Description { get { return Account.Description; } } public string Email { get { return Account.Email; } } public string Telephone { get { return Account.Telephone; } } } public List AccountList { get; private set; } /// /// 总共创建的账号数目 /// public int TotalAccountNum { get { return AccountList.Count; } } /// /// 账号有效的用户数 /// public int EnabledAccountNum { get { int num = 0; foreach (var item in AccountList) { if (item.IsEnabled) num++; } return num; } } /// /// Construction /// /// public AccountViewModel(bool hideDisabledAccounts) { AccountList = new List(); //var accounts = ServiceClients.AccountServiceClient.GetAccountList(); var accounts = Triton160UiSystem.Instance.WCF.Account.GetAccountList(); // chaosong@mod 20160325 to add account management int num = 1; foreach (var account in accounts.AccountList) { if (!account.AccountStatus && hideDisabledAccounts) continue; AccountList.Add(new AccountInfo() { Account = account, No = num++ }); } } } public class RolePermissionViewModel : INotifyPropertyChanged { #region Command public DelegateCommand SavePermissionCommand { get; set; } #endregion public SerializableDictionary> Roles { get; set; } ObservableCollection roleNames = new ObservableCollection(); public ObservableCollection RoleNames { get { return roleNames; } set { roleNames = value; } } public RolePermissionViewModel() { //cs 设置 loadPermissionFile(); Roles = Triton160UiSystem.Instance.WCF.Account.GetAllRolesPermission(); RoleNames.Clear(); foreach (string rolename in Roles.Keys) { RoleNames.Add(rolename); } } public void SelectRoleChanged(string role) { CurrentRoleName = role; BindAll(); } public void InitialRolePermissionVM() { SavePermissionCommand = new DelegateCommand( param => { try { //_xmlRecipeFormat.Save(recipePermissionFile); bool suc = Triton160UiSystem.Instance.WCF.Account.SaveProcessViewPermission(_xmlRecipeFormat.InnerXml); if (suc) MessageBox.Show("保存菜单查看权限成功"); //Publisher.Notify(Subject.SendSaveSuccessMessage, "保存菜单查看权限成功"); } catch (Exception ex) { LOG.Write(ex); MessageBox.Show("保存菜单查看权限失败!"); //Publisher.Notify(Subject.SendWarningMessage, "保存菜单查看权限失败!"); } }, param => { if (string.IsNullOrEmpty(CurrentRoleName))return false; return true; }); } public CheckTreeViewModel ChamberAViewModel { get; set; } //public CheckTreeViewModel ChamberBViewModel { get; set; } //public CheckTreeViewModel ChamberCViewModel { get; set; } //public CheckTreeViewModel ChamberDViewModel { get; set; } public string CurrentRoleName { get; set; } #region UI Logical public void loadPermissionFile() { ChamberAViewModel = new CheckTreeViewModel(); //ChamberBViewModel = new CheckTreeViewModel(); //ChamberCViewModel = new CheckTreeViewModel(); //ChamberDViewModel = new CheckTreeViewModel(); string permission = Triton160UiSystem.Instance.WCF.Account.GetProcessViewPermission(); _xmlRecipeFormat.LoadXml(permission); } ///// ///// Recipe format ///// //public string RecipeFormat(ModuleName chamber) //{ // return recipeEditor.GetRecipeFormatXml(chamber.ToString()); //} public CheckTreeViewModel GetTreeVM(ModuleName cs) { switch (cs) { case ModuleName.System: return ChamberAViewModel; //case ModuleName.ReactorB: return ChamberBViewModel; //case ModuleName.ReactorC: return ChamberCViewModel; //case ModuleName.ReactorD: return ChamberDViewModel; //default: //throw new Exception("ModuleName Error"); } return null; } public static XmlDocument _xmlRecipeFormat = new XmlDocument(); public void BindAll() { // loadPermissionFile(); //ChamberAViewModel = new CheckTreeViewModel(); //ChamberBViewModel = new CheckTreeViewModel(); //ChamberCViewModel = new CheckTreeViewModel(); //ChamberDViewModel = new CheckTreeViewModel(); //if (MainWindow.IsReactorAPresence) BindTreeView(ModuleName.System); //if (MainWindow.IsReactorBPresence) // BindTreeView(ModuleName.ReactorB); //if (MainWindow.IsReactorCPresence) // BindTreeView(ModuleName.ReactorC); //if (MainWindow.IsReactorDPresence) // BindTreeView(ModuleName.ReactorD); } public void BindTreeView(ModuleName cs) { HashSet groupPermission = new HashSet(); HashSet stepPermission = new HashSet(); BindTreeView(cs, ref groupPermission, ref stepPermission); } //void addNewChamberPermssion(ModuleName cs) //{ // XmlDocument formatXml = new XmlDocument(); // string recipeformat = RecipeFormat(cs); // if (string.IsNullOrEmpty(recipeformat)) return;//没有对应的腔体 // formatXml.LoadXml(recipeformat); // XmlNodeList newnodes = formatXml.SelectNodes(@"TableRecipeFormat"); // XmlElement newElement = _xmlRecipeFormat.CreateElement("TableRecipeFormat"); // newElement.InnerXml = newnodes[0].InnerXml; // XmlAttribute nameAttribute = _xmlRecipeFormat.CreateAttribute("PMName"); // nameAttribute.Value = cs + ""; // newElement.Attributes.Append(nameAttribute); // _xmlRecipeFormat.SelectNodes("Aitex")[0].AppendChild(newElement); //} /// /// 解析权限,并且填充显示model和权限 /// public void BindTreeView(ModuleName cs, ref HashSet groupPermission, ref HashSet stepPermission) { XmlNodeList nodes = _xmlRecipeFormat.SelectNodes(@"Aitex/TableRecipeFormat"); groupPermission = new HashSet(); stepPermission = new HashSet(); GetTreeVM(cs).Nodes.Remove(); // Dictionary permissions = new Dictionary(); bool hasChamberPermissions = false; foreach (XmlNode node in nodes) { if (node.Attributes["PMName"].Value == cs + "") { hasChamberPermissions = true; } } if (!hasChamberPermissions) { //addNewChamberPermssion(cs); //XmlDocument formatXml = new XmlDocument(); //string recipeformat = RecipeFormat(cs); //if(string.IsNullOrEmpty(recipeformat))return;//没有对应的腔体 //formatXml.LoadXml(recipeformat); //XmlNodeList newnodes = formatXml.SelectNodes(@"TableRecipeFormat"); //XmlAttribute nameAttribute = formatXml.CreateAttribute("PMName"); //nameAttribute.Value = cs + ""; //newnodes[0].Attributes.Append(nameAttribute); //nodes[0].InsertBefore(newnodes[0], nodes[0]); } } #endregion #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string proName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(proName)); } #endregion } public class CheckTreeViewModel : INotifyPropertyChanged { public NodeViewModel Nodes { get; private set; } public NodeInfo NodeInfo { get; private set; } public CheckTreeViewModel() { NodeInfo = new NodeInfo(); NodeInfo.SelectedNodeChanged += (s, e) => RefreshCommands(); Nodes = new NodeViewModel(NodeInfo); } NodeViewModel GetOperationNode() { if (NodeInfo.SelectedNode == null) return Nodes; return NodeInfo.SelectedNode; } bool CheckSelection(object obj) { return NodeInfo.SelectedNode != null; } void RefreshCommands() { //foreach (var cmd in Commands) // if (cmd.CanExecute(null)) // cmd.Execute(null); } #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string proName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(proName)); } #endregion } public class NodeInfo : INotifyPropertyChanged { NodeViewModel selectedNode; int count; public NodeViewModel SelectedNode { get { return selectedNode; } set { if (selectedNode != value) { selectedNode = value; OnSelectedNodeChanged(); OnPropertyChanged("SelectedNode"); } } } public int Count { get { return count; } private set { if (count != value) { count = value; OnPropertyChanged("Count"); } } } internal void SetCount(int newcount) { Count = newcount; } public event EventHandler SelectedNodeChanged; protected virtual void OnSelectedNodeChanged() { if (SelectedNodeChanged != null) SelectedNodeChanged(this, EventArgs.Empty); } #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string proName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(proName)); } #endregion } public class NodeViewModel : INotifyPropertyChanged { /* * 省去构造函数,公共方法和一些静态成员 * */ const string DataName = "数据 "; public XmlNode NodeXmlData { get; set; } public string CurrentRoleName { get; set; } static int DataCounter = 1; public static string GetNextDataName() { return String.Concat(DataName, DataCounter++); } public NodeViewModel(NodeInfo info) { children = new ObservableCollection(); NodeInfo = info; CheckBoxCommand = new DelegateCommand((p) => checkBoxSelect(p), param => { return true; }); } private NodeViewModel(NodeViewModel parent, string name, string role) : this(parent, name, false, null, role) { } private NodeViewModel(NodeViewModel parent, string name, bool isSelected, XmlNode node, string role) { Parent = parent; Name = name; NodeInfo = parent.NodeInfo; IsSelected = isSelected; children = new ObservableCollection(); SelectedNode = node; CheckBoxCommand = new DelegateCommand((p) => checkBoxSelect(p), param => { return true; }); CurrentRoleName = role; } public XmlNode SelectedNode = null; void checkBoxSelect(object p) { if (SelectedNode != null) { CheckBox cb = p as CheckBox; if (null != SelectedNode.Attributes["DenyRole"]) { string denyroles = "," + SelectedNode.Attributes["DenyRole"].Value + ","; string tagrole = "," + CurrentRoleName + ","; if (cb.IsChecked.Value) { denyroles = denyroles.Replace(tagrole, ","); SelectedNode.Attributes["DenyRole"].Value = denyroles.Trim(","[0]); } else { //没权限 if (denyroles.IndexOf(tagrole) < 0) { //本来有权限,现在要没权限 SelectedNode.Attributes["DenyRole"].Value = (denyroles + CurrentRoleName).Trim(","[0]); } } } else { if (cb.IsChecked.Value) { } else { XmlAttribute xmlAttribute = RolePermissionViewModel._xmlRecipeFormat.CreateAttribute("DenyRole"); xmlAttribute.Value = CurrentRoleName; SelectedNode.Attributes.Append(xmlAttribute); } } //SelectedNode.Attributes["DenyRole"].Value = cb.IsChecked + ""; } } #region 字段 string name; bool isExpanded; bool isSelected; ObservableCollection children; #endregion #region 属性 public DelegateCommand CheckBoxCommand { get; private set; } public NodeInfo NodeInfo { get; private set; } public NodeViewModel Parent { get; private set; } public string Name { get { return name; } set { if (name != value) { name = value; OnPropertyChanged("Name"); } } } public bool IsExpanded { get { return isExpanded; } set { if (isExpanded != value) { isExpanded = value; OnPropertyChanged("IsExpanded"); OnIsExpandedChanged(); } } } public bool IsSelected { get { return isSelected; } set { if (isSelected != value) { isSelected = value; OnPropertyChanged("IsSelected"); OnIsSelectedChanged(); } } } public ReadOnlyObservableCollection Children { get { return new ReadOnlyObservableCollection(children); } } #endregion #region 方法 public void Remove() { //if (Parent != null) //{ // Parent.children.Remove(this); // RefreshInfoCount(-1); //} children.Clear(); RefreshInfoCount(-1); } public NodeViewModel Add(string name, XmlNode node, string role) { return Add(name, false, node, role); } public NodeViewModel Add(string name, bool ischecked, XmlNode node, string role) { //SelectedNode = node; IsExpanded = true; NodeViewModel nodeModel = new NodeViewModel(this, name, ischecked, node, role); children.Insert(0, nodeModel); RefreshInfoCount(1); return nodeModel; } public void Append(string name, string role) { IsExpanded = true; children.Add(new NodeViewModel(this, name, role)); RefreshInfoCount(1); } public void Rename() { Name = GetNextDataName(); } public void MoveUp() { if (Parent != null) { var idx = Parent.children.IndexOf(this); if (idx > 0) { Parent.children.RemoveAt(idx); Parent.children.Insert(--idx, this); } IsSelected = true; } } public void MoveDown() { if (Parent != null) { var idx = Parent.children.IndexOf(this); if (idx < Parent.children.Count - 1) { Parent.children.RemoveAt(idx); Parent.children.Insert(++idx, this); } IsSelected = true; } } #endregion #region 私有方法 void RefreshInfoCount(int addition) { if (NodeInfo != null) NodeInfo.SetCount(NodeInfo.Count + addition); } #endregion #region 事件 public event EventHandler IsExpandedChanged; public event EventHandler IsSelectedChanged; protected virtual void OnIsExpandedChanged() { if (IsExpandedChanged != null) IsExpandedChanged(this, EventArgs.Empty); } protected virtual void OnIsSelectedChanged() { if (IsSelectedChanged != null) IsSelectedChanged(this, EventArgs.Empty); if (IsSelected) NodeInfo.SelectedNode = this; } #endregion #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string proName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(proName)); } #endregion } }