MenuTreeViewModel.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. using GalaSoft.MvvmLight;
  2. using GalaSoft.MvvmLight.Ioc;
  3. using Muchinfo.MTPClient.Account.Utilities;
  4. using Muchinfo.MTPClient.Bank.ViewModels;
  5. using Muchinfo.MTPClient.Data;
  6. using Muchinfo.MTPClient.Data.Enums;
  7. using Muchinfo.MTPClient.Delivery.Utilities;
  8. using Muchinfo.MTPClient.Infrastructure.Utilities;
  9. using Muchinfo.MTPClient.IService;
  10. using Muchinfo.MTPClient.Listing.Utilities;
  11. using Muchinfo.MTPClient.Report;
  12. using Muchinfo.MTPClient.Sale.Utilities;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Linq;
  16. //----------------------------------------------------------------
  17. //Module Name: $safeprojectname$
  18. //Purpose:
  19. //CopyRight: Muchinfo
  20. //History:
  21. //----------------------------------------------------------------
  22. //DateTime 2016/2/3 15:26:19
  23. //Author
  24. //Description Create
  25. //----------------------------------------------------------------
  26. using System.Windows;
  27. using Muchinfo.MTPClient.Analysis.Views;
  28. using Muchinfo.MTPClient.Infrastructure.Helpers;
  29. using Muchinfo.MTPClient.Infrastructure.Interfaces;
  30. namespace Muchinfo.MTPClient.UI.ViewModels
  31. {
  32. public class MenuTreeViewModel : ViewModelBase
  33. {
  34. public MenuTreeViewModel()
  35. {
  36. MessengerHelper.DefaultRegister<LayoutStyle>(this, MessengerTokens.UpdateLayoutChange, (layout) =>
  37. {
  38. LogInfoHelper.WriteInfo("MenuTreeViewModel Enter MessengerTokens.UpdateLayoutChange");
  39. LoadMenus(UserManager.AccessFuncMenus);// 重新加载界面
  40. });
  41. }
  42. private SysItemList _mSysItemList;
  43. public SysItemList MenuSource
  44. {
  45. get { return _mSysItemList; }
  46. set
  47. {
  48. Set(() => MenuSource, ref _mSysItemList, value);
  49. }
  50. }
  51. //public void InitTreeview()
  52. //{
  53. // var loginService = SimpleIoc.Default.GetInstance<ILoginService>();
  54. // loginService.QueryAccountFuncMenu(UserManager.CurrentTradeAccount.LoginCode, FuncMenusSuccess, FuncMenusErrorFunc);
  55. //}
  56. public void LoadMenus(List<SysMenuItem> funcs)
  57. {
  58. ////todo:获取程序集实现了IModuleMenuService
  59. var account = new AccountMenuService();
  60. var childMenus = account.GetModuleMenuItems();
  61. var bankService = new BankMenuService();
  62. var bankMenus = bankService.GetModuleMenuItems();
  63. var saleService = new SaleMenuService();
  64. var saleMenus = saleService.GetModuleMenuItems();
  65. var reportService = new ReportMenuService();
  66. var reportMenus = reportService.GetModuleMenuItems();
  67. //交收管理 Add By DK 20160804
  68. var deliveryService = new DeliveryMenuService();
  69. var deliveryMenus = deliveryService.GetModuleMenuItems();
  70. //var listingService = new ListingMenuService();
  71. //var listingMenus = listingService.GetModuleMenuItems();
  72. //todo:修改添加其他模块的菜单
  73. foreach (var sysMenuItem in bankMenus.Keys)
  74. {
  75. childMenus[sysMenuItem] = bankMenus[sysMenuItem];
  76. }
  77. foreach (var sysMenuItem in saleMenus.Keys)
  78. {
  79. childMenus[sysMenuItem] = saleMenus[sysMenuItem];
  80. }
  81. if (reportMenus!=null) //错误 #24677【客户端报表】交易员登录客户端,应不显示报表菜单
  82. foreach (var sysMenuItem in reportMenus.Keys)
  83. {
  84. childMenus[sysMenuItem] = reportMenus[sysMenuItem];
  85. }
  86. //交收管理 Add By DK 20160804
  87. foreach (var sysMenuItem in deliveryMenus.Keys)
  88. {
  89. childMenus[sysMenuItem] = deliveryMenus[sysMenuItem];
  90. }
  91. //挂牌
  92. //foreach (var sysMenuItem in listingMenus.Keys)
  93. //{
  94. // childMenus[sysMenuItem] = listingMenus[sysMenuItem];
  95. //}
  96. //交易菜单
  97. if (UserManager.TradeFuncMenus != null && UserManager.TradeFuncMenus.Any())
  98. {
  99. foreach (var sysMenuItem in UserManager.TradeFuncMenus )
  100. {
  101. if (childMenus.ContainsKey(sysMenuItem.SysCommandType)) ////交易菜单有默认的命令,使用自身的参数
  102. {
  103. childMenus[sysMenuItem.SysCommandType].Title = sysMenuItem.Title;
  104. sysMenuItem.Command = childMenus[sysMenuItem.SysCommandType].Command;
  105. }
  106. else
  107. {
  108. childMenus[sysMenuItem.SysCommandType] = sysMenuItem;
  109. }
  110. if (sysMenuItem.SysCommandType == MenuCommandType.Cancel) //撤单默认为false
  111. {
  112. sysMenuItem.IsEnabled = false;
  113. }
  114. }
  115. }
  116. CreateAccessMenus(funcs, childMenus); ////
  117. }
  118. /// <summary>
  119. ///
  120. /// </summary>
  121. /// <param name="funcs"></param>
  122. private void FuncMenusSuccess(List<SysMenuItem> funcs)
  123. {
  124. if (funcs == null || !funcs.Any()) return;
  125. UserManager.AccessFuncMenus = funcs;
  126. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  127. {
  128. this.LoadMenus(UserManager.AccessFuncMenus); ////树菜单生成
  129. }));
  130. }
  131. /// <summary>
  132. /// 错误处理
  133. /// </summary>
  134. /// <param name="error">错误内容</param>
  135. private void FuncMenusErrorFunc(ErrorEntity error)
  136. {
  137. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  138. {
  139. //todo:查询菜单出错提示
  140. }));
  141. }
  142. /// <summary>
  143. /// 查找交易汇总节点
  144. /// </summary>
  145. /// <param name="funcs"></param>
  146. /// <returns>如果有,返回true</returns>
  147. private bool HasTradeSummary(List<SysMenuItem> funcs)
  148. {
  149. if (funcs == null)
  150. {
  151. return false;
  152. }
  153. // 查找交易汇总的节点
  154. var found = funcs.Find((item) =>
  155. {
  156. if (item.Children == null)
  157. {
  158. return false;
  159. }
  160. // 再查查子类节点
  161. var foundChild = item.Children.Find((c) =>
  162. {
  163. var commandType = UserManager.GetMenuCommandType(c.FuncMenuCode);
  164. return commandType == MenuCommandType.TradeSum; // 如果有交易汇总tree节点,
  165. });
  166. return foundChild != null;
  167. });
  168. return found != null;
  169. }
  170. /// <summary>
  171. /// 重新规律tree的节点
  172. /// </summary>
  173. /// <param name="funcs"></param>
  174. /// <returns></returns>
  175. private List<SysMenuItem> ResetTreeWithLayoutType(List<SysMenuItem> funcs)
  176. {
  177. List<SysMenuItem> temps = new List<SysMenuItem>(funcs);
  178. if (UserManager.GetCurrentLayoutStyle() == LayoutStyle.Tradition) // 不需要显示交易节点
  179. {
  180. for (int i = 0; i < temps.Count; i++)
  181. {
  182. if (temps[i].SysCommandType == MenuCommandType.Trade)// 不需要交易
  183. {
  184. temps.RemoveAt(i);
  185. break;
  186. }
  187. }
  188. }
  189. return temps;
  190. }
  191. /// <summary>
  192. /// 生成配置有权限的菜单
  193. /// </summary>
  194. /// <param name="funcs"></param>
  195. /// <param name="menuItems"></param>
  196. private void CreateAccessMenus(List<SysMenuItem> funcs, Dictionary<MenuCommandType, SysMenuItem> menuItems)
  197. {
  198. List<SysMenuItem> temps = ResetTreeWithLayoutType(funcs);
  199. var menuSource = new SysItemList();
  200. bool hasTradeSummary = HasTradeSummary(temps);// :是否包含交易汇总,如果有,表示会显示在账户资金前面,那账户资金就不需要高亮了
  201. foreach (var funcMenu in temps)
  202. {
  203. var commandType = UserManager.GetMenuCommandType(funcMenu.FuncMenuCode);
  204. Console.WriteLine(funcMenu.Title + ":" + funcMenu.FuncMenuCode);
  205. if (menuItems.ContainsKey(commandType))
  206. {
  207. RecurMenus(funcMenu, menuItems[commandType], menuItems);
  208. menuItems[commandType].Title = funcMenu.Title;
  209. menuSource.Add(menuItems[commandType]);
  210. if (commandType == MenuCommandType.AccountInfo)
  211. {
  212. if (!hasTradeSummary)// 如果没有交易汇总节点,那账户资金就可以高亮了
  213. {
  214. menuItems[commandType].IsSelected = true;
  215. }
  216. menuItems[commandType].Command.Execute(menuItems[commandType]);
  217. }
  218. //if (commandType == MenuCommandType.Trade || commandType==MenuCommandType.Query) //展开交易和查询菜单
  219. //{
  220. // menuItems[commandType].IsExpanded = true;
  221. //}
  222. menuItems[commandType].IsExpanded = true;
  223. }
  224. }
  225. MenuSource = menuSource;
  226. }
  227. /// <summary>
  228. ///生成菜单树
  229. /// </summary>
  230. /// <param name="funcMenu">服务端父功能菜单</param>
  231. /// <param name="parentMenu">本地菜单</param>
  232. /// <param name="menuItems">菜单字典</param>
  233. private void RecurMenus(SysMenuItem funcMenu, SysMenuItem parentMenu,
  234. Dictionary<MenuCommandType, SysMenuItem> menuItems)
  235. {
  236. if (parentMenu.Children == null)
  237. {
  238. parentMenu.Children = new List<SysMenuItem>();
  239. }
  240. var items = new List<SysMenuItem>();
  241. items.Add(funcMenu);
  242. bool hasTradeSummary = HasTradeSummary(items);// :是否包含交易汇总,如果有,表示会显示在账户资金前面,那账户资金就不需要高亮了
  243. // FIXME: 先选择一下资金账号,以便初始化相关对应 @_@!...
  244. foreach (var childItem in funcMenu.Children)
  245. {
  246. var commandType = UserManager.GetMenuCommandType(childItem.FuncMenuCode);
  247. if (menuItems.ContainsKey(commandType))
  248. {
  249. if (commandType == MenuCommandType.AccountInfo)
  250. {
  251. if (!hasTradeSummary)// 如果没有交易汇总 那就显示账户资金高亮
  252. {
  253. menuItems[commandType].IsSelected = true;
  254. }
  255. menuItems[commandType].Command.Execute(menuItems[commandType]);
  256. }
  257. }
  258. //if (childItem.Children != null && childItem.Children.Any())
  259. //{
  260. // RecurMenus(childItem, menuItems[commandType], menuItems);
  261. //}
  262. }
  263. foreach (var childItem in funcMenu.Children)
  264. {
  265. var commandType = UserManager.GetMenuCommandType(childItem.FuncMenuCode);
  266. Console.WriteLine(childItem.Title + ":" + childItem.FuncMenuCode);
  267. if ( menuItems.ContainsKey(commandType))
  268. {
  269. menuItems[commandType].Title = childItem.Title;
  270. ////添加父项,以便后面判断
  271. menuItems[commandType].ParentItem = parentMenu;
  272. parentMenu.Children.Add(menuItems[commandType]);
  273. //if (commandType == MenuCommandType.AccountInfo)
  274. //{
  275. // menuItems[commandType].IsSelected = true;
  276. // menuItems[commandType].Command.Execute(menuItems[commandType]);
  277. //}
  278. // 默认选择“交易汇总”
  279. if (commandType == MenuCommandType.TradeSum)
  280. {
  281. menuItems[commandType].IsSelected = true;
  282. menuItems[commandType].Command.Execute(menuItems[commandType]);
  283. }
  284. }
  285. if (childItem.Children != null && childItem.Children.Any())
  286. {
  287. RecurMenus(childItem, menuItems[commandType], menuItems);
  288. }
  289. }
  290. }
  291. /// <summary>
  292. /// 查找菜单
  293. /// </summary>
  294. /// <param name="menuCommandType"></param>
  295. /// <returns></returns>
  296. public SysMenuItem FindSystemMenuItem(MenuCommandType menuCommandType)
  297. {
  298. if (MenuSource == null || !MenuSource.Any())
  299. {
  300. return null;
  301. }
  302. foreach (var menuItem in MenuSource)
  303. {
  304. if (menuItem.SysCommandType == menuCommandType)
  305. {
  306. return menuItem;
  307. }
  308. else
  309. {
  310. var item = FindTreeViewMenuItem(menuItem, menuCommandType);
  311. if (item != null)
  312. {
  313. return item;
  314. }
  315. }
  316. }
  317. return null;
  318. }
  319. /// <summary>
  320. /// 遍历树查找相应的菜单项
  321. /// </summary>
  322. /// <param name="sysItem"></param>
  323. /// <param name="menuCommandType"></param>
  324. /// <returns></returns>
  325. public SysMenuItem FindTreeViewMenuItem(SysMenuItem sysItem, MenuCommandType menuCommandType)
  326. {
  327. if (sysItem.Children == null)
  328. {
  329. return null;
  330. }
  331. foreach (var menuItem in sysItem.Children)
  332. {
  333. if (menuItem.SysCommandType == menuCommandType)
  334. {
  335. return menuItem;
  336. }
  337. else
  338. {
  339. var item = FindTreeViewMenuItem(menuItem, menuCommandType);
  340. if (item != null)
  341. {
  342. return item;
  343. }
  344. }
  345. }
  346. return null;
  347. }
  348. }
  349. public class SysItemList : List<SysMenuItem>
  350. {
  351. }
  352. }