| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388 |
- using GalaSoft.MvvmLight;
- using GalaSoft.MvvmLight.Ioc;
- using Muchinfo.MTPClient.Account.Utilities;
- using Muchinfo.MTPClient.Bank.ViewModels;
- using Muchinfo.MTPClient.Data;
- using Muchinfo.MTPClient.Data.Enums;
- using Muchinfo.MTPClient.Delivery.Utilities;
- using Muchinfo.MTPClient.Infrastructure.Utilities;
- using Muchinfo.MTPClient.IService;
- using Muchinfo.MTPClient.Listing.Utilities;
- using Muchinfo.MTPClient.Report;
- using Muchinfo.MTPClient.Sale.Utilities;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- //----------------------------------------------------------------
- //Module Name: $safeprojectname$
- //Purpose:
- //CopyRight: Muchinfo
- //History:
- //----------------------------------------------------------------
- //DateTime 2016/2/3 15:26:19
- //Author
- //Description Create
- //----------------------------------------------------------------
- using System.Windows;
- using Muchinfo.MTPClient.Analysis.Views;
- using Muchinfo.MTPClient.Infrastructure.Helpers;
- using Muchinfo.MTPClient.Infrastructure.Interfaces;
- namespace Muchinfo.MTPClient.UI.ViewModels
- {
- public class MenuTreeViewModel : ViewModelBase
- {
- public MenuTreeViewModel()
- {
- MessengerHelper.DefaultRegister<LayoutStyle>(this, MessengerTokens.UpdateLayoutChange, (layout) =>
- {
- LogInfoHelper.WriteInfo("MenuTreeViewModel Enter MessengerTokens.UpdateLayoutChange");
- LoadMenus(UserManager.AccessFuncMenus);// 重新加载界面
- });
- }
- private SysItemList _mSysItemList;
- public SysItemList MenuSource
- {
- get { return _mSysItemList; }
- set
- {
- Set(() => MenuSource, ref _mSysItemList, value);
- }
- }
- //public void InitTreeview()
- //{
- // var loginService = SimpleIoc.Default.GetInstance<ILoginService>();
- // loginService.QueryAccountFuncMenu(UserManager.CurrentTradeAccount.LoginCode, FuncMenusSuccess, FuncMenusErrorFunc);
- //}
- public void LoadMenus(List<SysMenuItem> funcs)
- {
- ////todo:获取程序集实现了IModuleMenuService
- var account = new AccountMenuService();
- var childMenus = account.GetModuleMenuItems();
- var bankService = new BankMenuService();
- var bankMenus = bankService.GetModuleMenuItems();
- var saleService = new SaleMenuService();
- var saleMenus = saleService.GetModuleMenuItems();
- var reportService = new ReportMenuService();
- var reportMenus = reportService.GetModuleMenuItems();
- //交收管理 Add By DK 20160804
- var deliveryService = new DeliveryMenuService();
- var deliveryMenus = deliveryService.GetModuleMenuItems();
- //var listingService = new ListingMenuService();
- //var listingMenus = listingService.GetModuleMenuItems();
- //todo:修改添加其他模块的菜单
- foreach (var sysMenuItem in bankMenus.Keys)
- {
- childMenus[sysMenuItem] = bankMenus[sysMenuItem];
- }
- foreach (var sysMenuItem in saleMenus.Keys)
- {
- childMenus[sysMenuItem] = saleMenus[sysMenuItem];
- }
- if (reportMenus!=null) //错误 #24677【客户端报表】交易员登录客户端,应不显示报表菜单
- foreach (var sysMenuItem in reportMenus.Keys)
- {
- childMenus[sysMenuItem] = reportMenus[sysMenuItem];
- }
- //交收管理 Add By DK 20160804
- foreach (var sysMenuItem in deliveryMenus.Keys)
- {
- childMenus[sysMenuItem] = deliveryMenus[sysMenuItem];
- }
- //挂牌
- //foreach (var sysMenuItem in listingMenus.Keys)
- //{
- // childMenus[sysMenuItem] = listingMenus[sysMenuItem];
- //}
- //交易菜单
- if (UserManager.TradeFuncMenus != null && UserManager.TradeFuncMenus.Any())
- {
-
- foreach (var sysMenuItem in UserManager.TradeFuncMenus )
- {
- if (childMenus.ContainsKey(sysMenuItem.SysCommandType)) ////交易菜单有默认的命令,使用自身的参数
- {
- childMenus[sysMenuItem.SysCommandType].Title = sysMenuItem.Title;
- sysMenuItem.Command = childMenus[sysMenuItem.SysCommandType].Command;
- }
- else
- {
- childMenus[sysMenuItem.SysCommandType] = sysMenuItem;
- }
- if (sysMenuItem.SysCommandType == MenuCommandType.Cancel) //撤单默认为false
- {
- sysMenuItem.IsEnabled = false;
- }
-
- }
- }
- CreateAccessMenus(funcs, childMenus); ////
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="funcs"></param>
- private void FuncMenusSuccess(List<SysMenuItem> funcs)
- {
- if (funcs == null || !funcs.Any()) return;
- UserManager.AccessFuncMenus = funcs;
- Application.Current.Dispatcher.BeginInvoke(new Action(() =>
- {
- this.LoadMenus(UserManager.AccessFuncMenus); ////树菜单生成
- }));
- }
- /// <summary>
- /// 错误处理
- /// </summary>
- /// <param name="error">错误内容</param>
- private void FuncMenusErrorFunc(ErrorEntity error)
- {
- Application.Current.Dispatcher.BeginInvoke(new Action(() =>
- {
- //todo:查询菜单出错提示
- }));
- }
- /// <summary>
- /// 查找交易汇总节点
- /// </summary>
- /// <param name="funcs"></param>
- /// <returns>如果有,返回true</returns>
- private bool HasTradeSummary(List<SysMenuItem> funcs)
- {
- if (funcs == null)
- {
- return false;
- }
- // 查找交易汇总的节点
- var found = funcs.Find((item) =>
- {
- if (item.Children == null)
- {
- return false;
- }
- // 再查查子类节点
- var foundChild = item.Children.Find((c) =>
- {
- var commandType = UserManager.GetMenuCommandType(c.FuncMenuCode);
- return commandType == MenuCommandType.TradeSum; // 如果有交易汇总tree节点,
- });
- return foundChild != null;
- });
- return found != null;
- }
- /// <summary>
- /// 重新规律tree的节点
- /// </summary>
- /// <param name="funcs"></param>
- /// <returns></returns>
- private List<SysMenuItem> ResetTreeWithLayoutType(List<SysMenuItem> funcs)
- {
- List<SysMenuItem> temps = new List<SysMenuItem>(funcs);
- if (UserManager.GetCurrentLayoutStyle() == LayoutStyle.Tradition) // 不需要显示交易节点
- {
- for (int i = 0; i < temps.Count; i++)
- {
- if (temps[i].SysCommandType == MenuCommandType.Trade)// 不需要交易
- {
- temps.RemoveAt(i);
- break;
- }
- }
- }
- return temps;
- }
- /// <summary>
- /// 生成配置有权限的菜单
- /// </summary>
- /// <param name="funcs"></param>
- /// <param name="menuItems"></param>
- private void CreateAccessMenus(List<SysMenuItem> funcs, Dictionary<MenuCommandType, SysMenuItem> menuItems)
- {
- List<SysMenuItem> temps = ResetTreeWithLayoutType(funcs);
- var menuSource = new SysItemList();
- bool hasTradeSummary = HasTradeSummary(temps);// :是否包含交易汇总,如果有,表示会显示在账户资金前面,那账户资金就不需要高亮了
-
- foreach (var funcMenu in temps)
- {
- var commandType = UserManager.GetMenuCommandType(funcMenu.FuncMenuCode);
- Console.WriteLine(funcMenu.Title + ":" + funcMenu.FuncMenuCode);
- if (menuItems.ContainsKey(commandType))
- {
- RecurMenus(funcMenu, menuItems[commandType], menuItems);
- menuItems[commandType].Title = funcMenu.Title;
- menuSource.Add(menuItems[commandType]);
- if (commandType == MenuCommandType.AccountInfo)
- {
- if (!hasTradeSummary)// 如果没有交易汇总节点,那账户资金就可以高亮了
- {
- menuItems[commandType].IsSelected = true;
- }
- menuItems[commandType].Command.Execute(menuItems[commandType]);
- }
- //if (commandType == MenuCommandType.Trade || commandType==MenuCommandType.Query) //展开交易和查询菜单
- //{
- // menuItems[commandType].IsExpanded = true;
- //}
- menuItems[commandType].IsExpanded = true;
- }
- }
-
- MenuSource = menuSource;
- }
- /// <summary>
- ///生成菜单树
- /// </summary>
- /// <param name="funcMenu">服务端父功能菜单</param>
- /// <param name="parentMenu">本地菜单</param>
- /// <param name="menuItems">菜单字典</param>
- private void RecurMenus(SysMenuItem funcMenu, SysMenuItem parentMenu,
- Dictionary<MenuCommandType, SysMenuItem> menuItems)
- {
- if (parentMenu.Children == null)
- {
- parentMenu.Children = new List<SysMenuItem>();
- }
- var items = new List<SysMenuItem>();
- items.Add(funcMenu);
- bool hasTradeSummary = HasTradeSummary(items);// :是否包含交易汇总,如果有,表示会显示在账户资金前面,那账户资金就不需要高亮了
- // FIXME: 先选择一下资金账号,以便初始化相关对应 @_@!...
- foreach (var childItem in funcMenu.Children)
- {
- var commandType = UserManager.GetMenuCommandType(childItem.FuncMenuCode);
- if (menuItems.ContainsKey(commandType))
- {
- if (commandType == MenuCommandType.AccountInfo)
- {
- if (!hasTradeSummary)// 如果没有交易汇总 那就显示账户资金高亮
- {
- menuItems[commandType].IsSelected = true;
- }
-
- menuItems[commandType].Command.Execute(menuItems[commandType]);
- }
- }
- //if (childItem.Children != null && childItem.Children.Any())
- //{
- // RecurMenus(childItem, menuItems[commandType], menuItems);
- //}
- }
- foreach (var childItem in funcMenu.Children)
- {
- var commandType = UserManager.GetMenuCommandType(childItem.FuncMenuCode);
- Console.WriteLine(childItem.Title + ":" + childItem.FuncMenuCode);
- if ( menuItems.ContainsKey(commandType))
- {
- menuItems[commandType].Title = childItem.Title;
- ////添加父项,以便后面判断
- menuItems[commandType].ParentItem = parentMenu;
- parentMenu.Children.Add(menuItems[commandType]);
- //if (commandType == MenuCommandType.AccountInfo)
- //{
- // menuItems[commandType].IsSelected = true;
- // menuItems[commandType].Command.Execute(menuItems[commandType]);
- //}
- // 默认选择“交易汇总”
- if (commandType == MenuCommandType.TradeSum)
- {
- menuItems[commandType].IsSelected = true;
- menuItems[commandType].Command.Execute(menuItems[commandType]);
- }
- }
- if (childItem.Children != null && childItem.Children.Any())
- {
- RecurMenus(childItem, menuItems[commandType], menuItems);
- }
- }
- }
- /// <summary>
- /// 查找菜单
- /// </summary>
- /// <param name="menuCommandType"></param>
- /// <returns></returns>
- public SysMenuItem FindSystemMenuItem(MenuCommandType menuCommandType)
- {
- if (MenuSource == null || !MenuSource.Any())
- {
- return null;
- }
- foreach (var menuItem in MenuSource)
- {
- if (menuItem.SysCommandType == menuCommandType)
- {
- return menuItem;
- }
- else
- {
- var item = FindTreeViewMenuItem(menuItem, menuCommandType);
- if (item != null)
- {
- return item;
- }
- }
-
- }
- return null;
- }
- /// <summary>
- /// 遍历树查找相应的菜单项
- /// </summary>
- /// <param name="sysItem"></param>
- /// <param name="menuCommandType"></param>
- /// <returns></returns>
- public SysMenuItem FindTreeViewMenuItem(SysMenuItem sysItem, MenuCommandType menuCommandType)
- {
- if (sysItem.Children == null)
- {
- return null;
- }
- foreach (var menuItem in sysItem.Children)
- {
- if (menuItem.SysCommandType == menuCommandType)
- {
- return menuItem;
- }
- else
- {
- var item = FindTreeViewMenuItem(menuItem, menuCommandType);
- if (item != null)
- {
- return item;
- }
- }
- }
- return null;
- }
- }
- public class SysItemList : List<SysMenuItem>
- {
- }
- }
|