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(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(); // loginService.QueryAccountFuncMenu(UserManager.CurrentTradeAccount.LoginCode, FuncMenusSuccess, FuncMenusErrorFunc); //} public void LoadMenus(List 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); //// } /// /// /// /// private void FuncMenusSuccess(List funcs) { if (funcs == null || !funcs.Any()) return; UserManager.AccessFuncMenus = funcs; Application.Current.Dispatcher.BeginInvoke(new Action(() => { this.LoadMenus(UserManager.AccessFuncMenus); ////树菜单生成 })); } /// /// 错误处理 /// /// 错误内容 private void FuncMenusErrorFunc(ErrorEntity error) { Application.Current.Dispatcher.BeginInvoke(new Action(() => { //todo:查询菜单出错提示 })); } /// /// 查找交易汇总节点 /// /// /// 如果有,返回true private bool HasTradeSummary(List 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; } /// /// 重新规律tree的节点 /// /// /// private List ResetTreeWithLayoutType(List funcs) { List temps = new List(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; } /// /// 生成配置有权限的菜单 /// /// /// private void CreateAccessMenus(List funcs, Dictionary menuItems) { List 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; } /// ///生成菜单树 /// /// 服务端父功能菜单 /// 本地菜单 /// 菜单字典 private void RecurMenus(SysMenuItem funcMenu, SysMenuItem parentMenu, Dictionary menuItems) { if (parentMenu.Children == null) { parentMenu.Children = new List(); } var items = new List(); 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); } } } /// /// 查找菜单 /// /// /// 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; } /// /// 遍历树查找相应的菜单项 /// /// /// /// 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 { } }