using Bootstrap.Security; using Bootstrap.Security.SQLServer; using Longbow.Cache; using Longbow.Data; using System.Collections.Generic; using System.Linq; namespace Bootstrap.DataAccess { /// /// /// public static class MenuHelper { /// /// /// public const string RetrieveMenusByRoleIdDataKey = "MenuHelper-RetrieveMenusByRoleId"; /// /// /// public const string RetrieveMenusAll = "BootstrapMenu-RetrieveMenus"; /// /// /// /// /// public static bool SaveMenu(BootstrapMenu value) => DbAdapterManager.Create().SaveMenu(value); /// /// /// /// /// public static bool DeleteMenu(IEnumerable value) => DbAdapterManager.Create().DeleteMenu(value); /// /// 通过用户名获得所有菜单 /// /// /// public static IEnumerable RetrieveMenusByUserName(string userName) => RetrieveAllMenus(userName); /// /// /// /// /// public static IEnumerable RetrieveMenusByRoleId(int roleId) => CacheManager.GetOrAdd($"{RetrieveMenusByRoleIdDataKey}-{roleId}", k => DbAdapterManager.Create().RetrieveMenusByRoleId(roleId), RetrieveMenusByRoleIdDataKey); /// /// /// /// /// /// public static bool SaveMenusByRoleId(int id, IEnumerable value) => DbAdapterManager.Create().SaveMenusByRoleId(id, value); /// /// /// /// /// /// public static IEnumerable RetrieveAppMenus(string appId, string userName, string activeUrl) { var menus = RetrieveAllMenus(userName).Where(m => m.Category == "1" && m.IsResource == 0); if (appId != "0") menus = menus.Where(m => m.ApplicationCode == appId); BASQLHelper.ActiveMenu(null, menus, activeUrl); var root = menus.Where(m => m.ParentId == 0).OrderBy(m => m.ApplicationCode).ThenBy(m => m.Order); BASQLHelper.CascadeMenus(menus, root); return root; } /// /// 通过当前用户名获得后台菜单,层次化后集合 /// /// /// 当前登陆的用户名 /// 当前访问菜单 /// 连接字符串名称,默认为ba /// public static IEnumerable RetrieveSystemMenus(string userName, string activeUrl = null) { var menus = RetrieveAllMenus(userName).Where(m => m.Category == "0" && m.IsResource == 0); BASQLHelper.ActiveMenu(null, menus, activeUrl); var root = menus.Where(m => m.ParentId == 0).OrderBy(m => m.ApplicationCode).ThenBy(m => m.Order); BASQLHelper.CascadeMenus(menus, root); return root; } /// /// 通过当前用户名获得所有菜单,层次化后集合 /// /// /// public static IEnumerable RetrieveMenus(string userName) { var menus = RetrieveAllMenus(userName); var root = menus.Where(m => m.ParentId == 0).OrderBy(m => m.ApplicationCode).ThenBy(m => m.Order); BASQLHelper.CascadeMenus(menus, root); return root; } /// /// 通过用户获得所有菜单 /// /// /// private static IEnumerable RetrieveAllMenus(string userName) => CacheManager.GetOrAdd($"{RetrieveMenusAll}-{userName}", key => DbAdapterManager.Create().RetrieveAllMenus(userName), RetrieveMenusAll); } }