AccountViewModel.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections;
  6. using System.Xml;
  7. using Aitex.Core.Account;
  8. using Aitex.Core.UI.MVVM;
  9. using Aitex.Core.Util;
  10. using Aitex.Triton160.Common;
  11. using Aitex.Triton160.UI;
  12. using System.ComponentModel;
  13. using System.Collections.ObjectModel;
  14. using System.Windows.Controls;
  15. using System.Windows;
  16. using Aitex.Core.RT.Log;
  17. namespace Triton160.UI.ViewModel
  18. {
  19. public class AccountViewModel
  20. {
  21. public class AccountInfo
  22. {
  23. public Account Account { get; set; }
  24. public int No { get; set; }
  25. public string AccountId { get { return Account.AccountId; } }
  26. public bool IsEnabled { get { return Account.AccountStatus; } }
  27. public string RealName { get { return Account.RealName; } }
  28. public string Role { get { return Account.Role; } }
  29. public string Department { get { return Account.Department; } }
  30. public string LastLoginTime { get { return Account.LastLoginTime; } }
  31. public string Description { get { return Account.Description; } }
  32. public string Email { get { return Account.Email; } }
  33. public string Telephone { get { return Account.Telephone; } }
  34. }
  35. public List<AccountInfo> AccountList { get; private set; }
  36. /// <summary>
  37. /// 总共创建的账号数目
  38. /// </summary>
  39. public int TotalAccountNum
  40. {
  41. get
  42. {
  43. return AccountList.Count;
  44. }
  45. }
  46. /// <summary>
  47. /// 账号有效的用户数
  48. /// </summary>
  49. public int EnabledAccountNum
  50. {
  51. get
  52. {
  53. int num = 0;
  54. foreach (var item in AccountList)
  55. {
  56. if (item.IsEnabled) num++;
  57. }
  58. return num;
  59. }
  60. }
  61. /// <summary>
  62. /// Construction
  63. /// </summary>
  64. /// <param name="hideDisabledAccounts"></param>
  65. public AccountViewModel(bool hideDisabledAccounts)
  66. {
  67. AccountList = new List<AccountInfo>();
  68. //var accounts = ServiceClients.AccountServiceClient.GetAccountList();
  69. var accounts = Triton160UiSystem.Instance.WCF.Account.GetAccountList(); // chaosong@mod 20160325 to add account management
  70. int num = 1;
  71. foreach (var account in accounts.AccountList)
  72. {
  73. if (!account.AccountStatus && hideDisabledAccounts) continue;
  74. AccountList.Add(new AccountInfo() { Account = account, No = num++ });
  75. }
  76. }
  77. }
  78. public class RolePermissionViewModel : INotifyPropertyChanged
  79. {
  80. #region Command
  81. public DelegateCommand<object> SavePermissionCommand { get; set; }
  82. #endregion
  83. public SerializableDictionary<string, SerializableDictionary<string, ViewPermission>> Roles
  84. {
  85. get;
  86. set;
  87. }
  88. ObservableCollection<string> roleNames = new ObservableCollection<string>();
  89. public ObservableCollection<string> RoleNames
  90. {
  91. get
  92. {
  93. return roleNames;
  94. }
  95. set
  96. {
  97. roleNames = value;
  98. }
  99. }
  100. public RolePermissionViewModel()
  101. {
  102. //cs 设置
  103. loadPermissionFile();
  104. Roles = Triton160UiSystem.Instance.WCF.Account.GetAllRolesPermission();
  105. RoleNames.Clear();
  106. foreach (string rolename in Roles.Keys)
  107. {
  108. RoleNames.Add(rolename);
  109. }
  110. }
  111. public void SelectRoleChanged(string role)
  112. {
  113. CurrentRoleName = role;
  114. BindAll();
  115. }
  116. public void InitialRolePermissionVM()
  117. {
  118. SavePermissionCommand = new DelegateCommand<object>(
  119. param =>
  120. {
  121. try
  122. {
  123. //_xmlRecipeFormat.Save(recipePermissionFile);
  124. bool suc = Triton160UiSystem.Instance.WCF.Account.SaveProcessViewPermission(_xmlRecipeFormat.InnerXml);
  125. if (suc)
  126. MessageBox.Show("保存菜单查看权限成功");
  127. //Publisher.Notify(Subject.SendSaveSuccessMessage, "保存菜单查看权限成功");
  128. }
  129. catch (Exception ex)
  130. {
  131. LOG.Write(ex);
  132. MessageBox.Show("保存菜单查看权限失败!");
  133. //Publisher.Notify(Subject.SendWarningMessage, "保存菜单查看权限失败!");
  134. }
  135. },
  136. param => { if (string.IsNullOrEmpty(CurrentRoleName))return false; return true; });
  137. }
  138. public CheckTreeViewModel ChamberAViewModel { get; set; }
  139. //public CheckTreeViewModel ChamberBViewModel { get; set; }
  140. //public CheckTreeViewModel ChamberCViewModel { get; set; }
  141. //public CheckTreeViewModel ChamberDViewModel { get; set; }
  142. public string CurrentRoleName { get; set; }
  143. #region UI Logical
  144. public
  145. void loadPermissionFile()
  146. {
  147. ChamberAViewModel = new CheckTreeViewModel();
  148. //ChamberBViewModel = new CheckTreeViewModel();
  149. //ChamberCViewModel = new CheckTreeViewModel();
  150. //ChamberDViewModel = new CheckTreeViewModel();
  151. string permission = Triton160UiSystem.Instance.WCF.Account.GetProcessViewPermission();
  152. _xmlRecipeFormat.LoadXml(permission);
  153. }
  154. ///// <summary>
  155. ///// Recipe format
  156. ///// </summary>
  157. //public string RecipeFormat(ModuleName chamber)
  158. //{
  159. // return recipeEditor.GetRecipeFormatXml(chamber.ToString());
  160. //}
  161. public CheckTreeViewModel GetTreeVM(ModuleName cs)
  162. {
  163. switch (cs)
  164. {
  165. case ModuleName.System: return ChamberAViewModel;
  166. //case ModuleName.ReactorB: return ChamberBViewModel;
  167. //case ModuleName.ReactorC: return ChamberCViewModel;
  168. //case ModuleName.ReactorD: return ChamberDViewModel;
  169. //default:
  170. //throw new Exception("ModuleName Error");
  171. }
  172. return null;
  173. }
  174. public static XmlDocument _xmlRecipeFormat = new XmlDocument();
  175. public void BindAll()
  176. {
  177. // loadPermissionFile();
  178. //ChamberAViewModel = new CheckTreeViewModel();
  179. //ChamberBViewModel = new CheckTreeViewModel();
  180. //ChamberCViewModel = new CheckTreeViewModel();
  181. //ChamberDViewModel = new CheckTreeViewModel();
  182. //if (MainWindow.IsReactorAPresence)
  183. BindTreeView(ModuleName.System);
  184. //if (MainWindow.IsReactorBPresence)
  185. // BindTreeView(ModuleName.ReactorB);
  186. //if (MainWindow.IsReactorCPresence)
  187. // BindTreeView(ModuleName.ReactorC);
  188. //if (MainWindow.IsReactorDPresence)
  189. // BindTreeView(ModuleName.ReactorD);
  190. }
  191. public void BindTreeView(ModuleName cs)
  192. {
  193. HashSet<string> groupPermission = new HashSet<string>();
  194. HashSet<string> stepPermission = new HashSet<string>();
  195. BindTreeView(cs, ref groupPermission, ref stepPermission);
  196. }
  197. //void addNewChamberPermssion(ModuleName cs)
  198. //{
  199. // XmlDocument formatXml = new XmlDocument();
  200. // string recipeformat = RecipeFormat(cs);
  201. // if (string.IsNullOrEmpty(recipeformat)) return;//没有对应的腔体
  202. // formatXml.LoadXml(recipeformat);
  203. // XmlNodeList newnodes = formatXml.SelectNodes(@"TableRecipeFormat");
  204. // XmlElement newElement = _xmlRecipeFormat.CreateElement("TableRecipeFormat");
  205. // newElement.InnerXml = newnodes[0].InnerXml;
  206. // XmlAttribute nameAttribute = _xmlRecipeFormat.CreateAttribute("PMName");
  207. // nameAttribute.Value = cs + "";
  208. // newElement.Attributes.Append(nameAttribute);
  209. // _xmlRecipeFormat.SelectNodes("Aitex")[0].AppendChild(newElement);
  210. //}
  211. /// <summary>
  212. /// 解析权限,并且填充显示model和权限
  213. /// </summary>
  214. public void BindTreeView(ModuleName cs, ref HashSet<string> groupPermission, ref HashSet<string> stepPermission)
  215. {
  216. XmlNodeList nodes = _xmlRecipeFormat.SelectNodes(@"Aitex/TableRecipeFormat");
  217. groupPermission = new HashSet<string>();
  218. stepPermission = new HashSet<string>();
  219. GetTreeVM(cs).Nodes.Remove();
  220. // Dictionary<string, AitexTableRecipeFormatCatalogGroup> permissions = new Dictionary<string, AitexTableRecipeFormatCatalogGroup>();
  221. bool hasChamberPermissions = false;
  222. foreach (XmlNode node in nodes)
  223. {
  224. if (node.Attributes["PMName"].Value == cs + "")
  225. {
  226. hasChamberPermissions = true;
  227. }
  228. }
  229. if (!hasChamberPermissions)
  230. {
  231. //addNewChamberPermssion(cs);
  232. //XmlDocument formatXml = new XmlDocument();
  233. //string recipeformat = RecipeFormat(cs);
  234. //if(string.IsNullOrEmpty(recipeformat))return;//没有对应的腔体
  235. //formatXml.LoadXml(recipeformat);
  236. //XmlNodeList newnodes = formatXml.SelectNodes(@"TableRecipeFormat");
  237. //XmlAttribute nameAttribute = formatXml.CreateAttribute("PMName");
  238. //nameAttribute.Value = cs + "";
  239. //newnodes[0].Attributes.Append(nameAttribute);
  240. //nodes[0].InsertBefore(newnodes[0], nodes[0]);
  241. }
  242. }
  243. #endregion
  244. #region INotifyPropertyChanged Members
  245. public event PropertyChangedEventHandler PropertyChanged;
  246. protected virtual void OnPropertyChanged(string proName)
  247. {
  248. if (PropertyChanged != null)
  249. PropertyChanged(this, new PropertyChangedEventArgs(proName));
  250. }
  251. #endregion
  252. }
  253. public class CheckTreeViewModel : INotifyPropertyChanged
  254. {
  255. public NodeViewModel Nodes { get; private set; }
  256. public NodeInfo NodeInfo { get; private set; }
  257. public CheckTreeViewModel()
  258. {
  259. NodeInfo = new NodeInfo();
  260. NodeInfo.SelectedNodeChanged += (s, e) => RefreshCommands();
  261. Nodes = new NodeViewModel(NodeInfo);
  262. }
  263. NodeViewModel GetOperationNode()
  264. {
  265. if (NodeInfo.SelectedNode == null)
  266. return Nodes;
  267. return NodeInfo.SelectedNode;
  268. }
  269. bool CheckSelection(object obj)
  270. {
  271. return NodeInfo.SelectedNode != null;
  272. }
  273. void RefreshCommands()
  274. {
  275. //foreach (var cmd in Commands)
  276. // if (cmd.CanExecute(null))
  277. // cmd.Execute(null);
  278. }
  279. #region INotifyPropertyChanged Members
  280. public event PropertyChangedEventHandler PropertyChanged;
  281. protected virtual void OnPropertyChanged(string proName)
  282. {
  283. if (PropertyChanged != null)
  284. PropertyChanged(this, new PropertyChangedEventArgs(proName));
  285. }
  286. #endregion
  287. }
  288. public class NodeInfo : INotifyPropertyChanged
  289. {
  290. NodeViewModel selectedNode;
  291. int count;
  292. public NodeViewModel SelectedNode
  293. {
  294. get { return selectedNode; }
  295. set
  296. {
  297. if (selectedNode != value)
  298. {
  299. selectedNode = value;
  300. OnSelectedNodeChanged();
  301. OnPropertyChanged("SelectedNode");
  302. }
  303. }
  304. }
  305. public int Count
  306. {
  307. get { return count; }
  308. private set
  309. {
  310. if (count != value)
  311. {
  312. count = value;
  313. OnPropertyChanged("Count");
  314. }
  315. }
  316. }
  317. internal void SetCount(int newcount)
  318. {
  319. Count = newcount;
  320. }
  321. public event EventHandler SelectedNodeChanged;
  322. protected virtual void OnSelectedNodeChanged()
  323. {
  324. if (SelectedNodeChanged != null)
  325. SelectedNodeChanged(this, EventArgs.Empty);
  326. }
  327. #region INotifyPropertyChanged Members
  328. public event PropertyChangedEventHandler PropertyChanged;
  329. protected virtual void OnPropertyChanged(string proName)
  330. {
  331. if (PropertyChanged != null)
  332. PropertyChanged(this, new PropertyChangedEventArgs(proName));
  333. }
  334. #endregion
  335. }
  336. public class NodeViewModel : INotifyPropertyChanged
  337. {
  338. /*
  339. * 省去构造函数,公共方法和一些静态成员
  340. * */
  341. const string DataName = "数据 ";
  342. public XmlNode NodeXmlData { get; set; }
  343. public string CurrentRoleName { get; set; }
  344. static int DataCounter = 1;
  345. public static string GetNextDataName()
  346. {
  347. return String.Concat(DataName, DataCounter++);
  348. }
  349. public NodeViewModel(NodeInfo info)
  350. {
  351. children = new ObservableCollection<NodeViewModel>();
  352. NodeInfo = info;
  353. CheckBoxCommand = new DelegateCommand<object>((p) => checkBoxSelect(p), param => { return true; });
  354. }
  355. private NodeViewModel(NodeViewModel parent, string name, string role)
  356. : this(parent, name, false, null, role)
  357. {
  358. }
  359. private NodeViewModel(NodeViewModel parent, string name, bool isSelected, XmlNode node, string role)
  360. {
  361. Parent = parent;
  362. Name = name;
  363. NodeInfo = parent.NodeInfo;
  364. IsSelected = isSelected;
  365. children = new ObservableCollection<NodeViewModel>();
  366. SelectedNode = node;
  367. CheckBoxCommand = new DelegateCommand<object>((p) => checkBoxSelect(p), param =>
  368. {
  369. return true;
  370. });
  371. CurrentRoleName = role;
  372. }
  373. public XmlNode SelectedNode = null;
  374. void checkBoxSelect(object p)
  375. {
  376. if (SelectedNode != null)
  377. {
  378. CheckBox cb = p as CheckBox;
  379. if (null != SelectedNode.Attributes["DenyRole"])
  380. {
  381. string denyroles = "," + SelectedNode.Attributes["DenyRole"].Value + ",";
  382. string tagrole = "," + CurrentRoleName + ",";
  383. if (cb.IsChecked.Value)
  384. {
  385. denyroles = denyroles.Replace(tagrole, ",");
  386. SelectedNode.Attributes["DenyRole"].Value = denyroles.Trim(","[0]);
  387. }
  388. else
  389. {
  390. //没权限
  391. if (denyroles.IndexOf(tagrole) < 0)
  392. {
  393. //本来有权限,现在要没权限
  394. SelectedNode.Attributes["DenyRole"].Value = (denyroles + CurrentRoleName).Trim(","[0]);
  395. }
  396. }
  397. }
  398. else
  399. {
  400. if (cb.IsChecked.Value)
  401. {
  402. }
  403. else
  404. {
  405. XmlAttribute xmlAttribute = RolePermissionViewModel._xmlRecipeFormat.CreateAttribute("DenyRole");
  406. xmlAttribute.Value = CurrentRoleName;
  407. SelectedNode.Attributes.Append(xmlAttribute);
  408. }
  409. }
  410. //SelectedNode.Attributes["DenyRole"].Value = cb.IsChecked + "";
  411. }
  412. }
  413. #region 字段
  414. string name;
  415. bool isExpanded;
  416. bool isSelected;
  417. ObservableCollection<NodeViewModel> children;
  418. #endregion
  419. #region 属性
  420. public DelegateCommand<object> CheckBoxCommand { get; private set; }
  421. public NodeInfo NodeInfo { get; private set; }
  422. public NodeViewModel Parent { get; private set; }
  423. public string Name
  424. {
  425. get
  426. {
  427. return name;
  428. }
  429. set
  430. {
  431. if (name != value)
  432. {
  433. name = value;
  434. OnPropertyChanged("Name");
  435. }
  436. }
  437. }
  438. public bool IsExpanded
  439. {
  440. get
  441. {
  442. return isExpanded;
  443. }
  444. set
  445. {
  446. if (isExpanded != value)
  447. {
  448. isExpanded = value;
  449. OnPropertyChanged("IsExpanded");
  450. OnIsExpandedChanged();
  451. }
  452. }
  453. }
  454. public bool IsSelected
  455. {
  456. get { return isSelected; }
  457. set
  458. {
  459. if (isSelected != value)
  460. {
  461. isSelected = value;
  462. OnPropertyChanged("IsSelected");
  463. OnIsSelectedChanged();
  464. }
  465. }
  466. }
  467. public ReadOnlyObservableCollection<NodeViewModel> Children
  468. {
  469. get
  470. {
  471. return new ReadOnlyObservableCollection<NodeViewModel>(children);
  472. }
  473. }
  474. #endregion
  475. #region 方法
  476. public void Remove()
  477. {
  478. //if (Parent != null)
  479. //{
  480. // Parent.children.Remove(this);
  481. // RefreshInfoCount(-1);
  482. //}
  483. children.Clear();
  484. RefreshInfoCount(-1);
  485. }
  486. public NodeViewModel Add(string name, XmlNode node, string role)
  487. {
  488. return Add(name, false, node, role);
  489. }
  490. public NodeViewModel Add(string name, bool ischecked, XmlNode node, string role)
  491. {
  492. //SelectedNode = node;
  493. IsExpanded = true;
  494. NodeViewModel nodeModel = new NodeViewModel(this, name, ischecked, node, role);
  495. children.Insert(0, nodeModel);
  496. RefreshInfoCount(1);
  497. return nodeModel;
  498. }
  499. public void Append(string name, string role)
  500. {
  501. IsExpanded = true;
  502. children.Add(new NodeViewModel(this, name, role));
  503. RefreshInfoCount(1);
  504. }
  505. public void Rename()
  506. {
  507. Name = GetNextDataName();
  508. }
  509. public void MoveUp()
  510. {
  511. if (Parent != null)
  512. {
  513. var idx = Parent.children.IndexOf(this);
  514. if (idx > 0)
  515. {
  516. Parent.children.RemoveAt(idx);
  517. Parent.children.Insert(--idx, this);
  518. }
  519. IsSelected = true;
  520. }
  521. }
  522. public void MoveDown()
  523. {
  524. if (Parent != null)
  525. {
  526. var idx = Parent.children.IndexOf(this);
  527. if (idx < Parent.children.Count - 1)
  528. {
  529. Parent.children.RemoveAt(idx);
  530. Parent.children.Insert(++idx, this);
  531. }
  532. IsSelected = true;
  533. }
  534. }
  535. #endregion
  536. #region 私有方法
  537. void RefreshInfoCount(int addition)
  538. {
  539. if (NodeInfo != null)
  540. NodeInfo.SetCount(NodeInfo.Count + addition);
  541. }
  542. #endregion
  543. #region 事件
  544. public event EventHandler IsExpandedChanged;
  545. public event EventHandler IsSelectedChanged;
  546. protected virtual void OnIsExpandedChanged()
  547. {
  548. if (IsExpandedChanged != null)
  549. IsExpandedChanged(this, EventArgs.Empty);
  550. }
  551. protected virtual void OnIsSelectedChanged()
  552. {
  553. if (IsSelectedChanged != null)
  554. IsSelectedChanged(this, EventArgs.Empty);
  555. if (IsSelected)
  556. NodeInfo.SelectedNode = this;
  557. }
  558. #endregion
  559. #region INotifyPropertyChanged Members
  560. public event PropertyChangedEventHandler PropertyChanged;
  561. protected virtual void OnPropertyChanged(string proName)
  562. {
  563. if (PropertyChanged != null)
  564. PropertyChanged(this, new PropertyChangedEventArgs(proName));
  565. }
  566. #endregion
  567. }
  568. }