using System.Collections.Generic; using System; using System.Data; using System.Linq; using System.Xml; namespace BLL { public static class SysMenuService { private const string NavigationChangeGroup = "MenuNavigationChanges"; public static bool IsRemovedCompanySection(string menuType) { return string.Equals(menuType, Const.Menu_DigData, StringComparison.OrdinalIgnoreCase) || string.Equals(menuType, Const.Menu_SmartSite, StringComparison.OrdinalIgnoreCase) || string.Equals(menuType, Const.Menu_TeamWork, StringComparison.OrdinalIgnoreCase) || string.Equals(menuType, Const.Menu_Notice, StringComparison.OrdinalIgnoreCase); } public static void TrackNavigationChanges(Model.SGGLDB db, string menuType, List menuIds) { var trackedIds = new HashSet(db.Sys_Const .Where(x => x.GroupId == NavigationChangeGroup && x.ConstValue == menuType) .Select(x => x.ConstText).ToList(), StringComparer.OrdinalIgnoreCase); foreach (var id in menuIds.Where(id => !trackedIds.Contains(id))) { db.Sys_Const.InsertOnSubmit(new Model.Sys_Const { ID = SQLHelper.GetNewID(), GroupId = NavigationChangeGroup, ConstText = id, ConstValue = menuType }); } } public static bool MoveMenu(string menuId, string sourceType, string targetType, string targetParent, bool isOffice) { using (var db = new Model.SGGLDB(Funs.ConnString)) { db.Connection.Open(); using (var transaction = db.Connection.BeginTransaction(IsolationLevel.Serializable)) { db.Transaction = transaction; var source = db.Sys_Menu.Where(x => x.MenuType == sourceType).ToList(); var menu = source.Find(x => string.Equals(x.MenuId, menuId, StringComparison.OrdinalIgnoreCase)); if (menu == null || (targetParent != "0" && !db.Sys_Menu.Any(x => x.MenuId == targetParent && x.MenuType == targetType && x.IsEnd != true))) { return false; } var children = source.ToLookup(x => x.SuperMenu, StringComparer.OrdinalIgnoreCase); var subtree = new List { menu }; for (int i = 0; i < subtree.Count; i++) { subtree.AddRange(children[subtree[i].MenuId]); } var ids = subtree.Select(x => x.MenuId).ToList(); if (ids.Contains(targetParent, StringComparer.OrdinalIgnoreCase)) { return false; } menu.SuperMenu = targetParent; if (sourceType != targetType) { foreach (var item in subtree) { item.MenuType = targetType; item.IsOffice = isOffice; } foreach (var power in db.Sys_RolePower.Where(x => ids.Contains(x.MenuId))) { power.MenuType = targetType; power.IsOffice = isOffice; } TrackNavigationChanges(db, targetType, ids); } TrackNavigationChanges(db, sourceType, sourceType == targetType ? new List { menuId } : ids); db.SubmitChanges(); transaction.Commit(); return true; } } } public static void ApplyNavigationChanges(XmlDocument document, string menuType) { using (var db = new Model.SGGLDB(Funs.ConnString)) { // 仅叠加通过设置页保存的菜单,保留现有 XML 中其他菜单的显示和顺序。 var changedIds = db.Sys_Const.Where(x => x.GroupId == NavigationChangeGroup && x.ConstValue == menuType) .Select(x => x.ConstText).ToList(); if (changedIds.Count == 0) return; var menus = db.Sys_Menu.Where(x => x.MenuType == menuType).ToList(); ApplyNavigationChanges(document, changedIds, menus); } } internal static void ApplyNavigationChanges(XmlDocument document, List changedIds, List menus) { var changed = new HashSet(changedIds, StringComparer.OrdinalIgnoreCase); var byId = menus.ToDictionary(x => x.MenuId, StringComparer.OrdinalIgnoreCase); var nodes = document.SelectNodes("//TreeNode[@id]").Cast() .ToLookup(x => x.GetAttribute("id"), StringComparer.OrdinalIgnoreCase); var placed = new Dictionary(StringComparer.OrdinalIgnoreCase); foreach (var group in nodes) { if (changed.Contains(group.Key)) { foreach (var node in group) node.ParentNode.RemoveChild(node); } else { placed.Add(group.Key, group.First()); } } foreach (var menu in menus.Where(x => changed.Contains(x.MenuId) && x.IsUsed == true).OrderBy(x => x.SortIndex)) { PlaceNavigationNode(document, menu, byId, nodes, placed); } } private static XmlElement PlaceNavigationNode(XmlDocument document, Model.Sys_Menu menu, Dictionary menus, ILookup originalNodes, Dictionary placed) { XmlElement node; if (placed.TryGetValue(menu.MenuId, out node)) return node; if (menu.IsUsed != true) return null; XmlElement parent = document.DocumentElement; if (menu.SuperMenu != "0" && !placed.TryGetValue(menu.SuperMenu ?? "", out parent)) { Model.Sys_Menu parentMenu; if (!menus.TryGetValue(menu.SuperMenu ?? "", out parentMenu)) return null; parent = PlaceNavigationNode(document, parentMenu, menus, originalNodes, placed); } if (parent == null) return null; node = originalNodes[menu.MenuId].FirstOrDefault() ?? document.CreateElement("TreeNode"); node.SetAttribute("id", menu.MenuId); node.SetAttribute("Text", menu.MenuName); node.SetAttribute("NavigateUrl", menu.Url); var next = parent.ChildNodes.OfType().FirstOrDefault(sibling => { Model.Sys_Menu siblingMenu; return menus.TryGetValue(sibling.GetAttribute("id"), out siblingMenu) && (siblingMenu.SortIndex ?? int.MinValue) > (menu.SortIndex ?? int.MinValue); }); parent.InsertBefore(node, next); placed.Add(menu.MenuId, node); return node; } /// /// 根据MenuId获取菜单名称项 /// /// /// public static List GetSupMenuListBySuperMenu(string superMenu) { var list = (from x in Funs.DB.Sys_Menu where x.SuperMenu == superMenu orderby x.SortIndex select x).ToList(); return list; } /// /// 根据MenuId获取菜单名称项 /// /// /// public static Model.Sys_Menu GetSupMenuBySuperMenu(string superMenu) { return Funs.DB.Sys_Menu.FirstOrDefault(x => x.SuperMenu == superMenu); } /// /// 根据MenuId获取菜单名称项 /// /// /// public static Model.Sys_Menu GetSysMenuByMenuId(string menuId) { return Funs.DB.Sys_Menu.FirstOrDefault(x => x.MenuId == menuId); } /// /// 根据MenuType获取菜单集合 /// /// /// public static List GetMenuListByMenuType(string menuType) { var list = (from x in Funs.DB.Sys_Menu where x.MenuType == menuType orderby x.SortIndex select x).ToList(); return list; } /// /// 根据MenuType获取菜单集合 /// /// /// public static List GetIsUsedMenuListByMenuType(string menuType) { var list = (from x in Funs.DB.Sys_Menu where (x.MenuType == menuType || menuType == null) && x.IsUsed == true orderby x.SortIndex select x).Distinct().ToList(); return list; } /// /// 根据MenuType获取菜单集合 /// /// /// public static List GetIsUsedMenuListBySupType(string menuType) { List lists = new List(); if (menuType == "MenuType_S") { lists = (from x in Funs.DB.Sys_Menu where x.IsOffice == true && x.IsUsed == true orderby x.SortIndex select x).Distinct().ToList(); } else { lists = (from x in Funs.DB.Sys_Menu where x.IsOffice == false && x.IsUsed == true orderby x.SortIndex select x).Distinct().ToList(); } return lists; } } }