using Longbow.Cache; using Longbow.Data; using System.Collections.Generic; namespace Bootstrap.DataAccess { /// /// /// public static class RoleHelper { public const string RetrieveRolesDataKey = "RoleHelper-RetrieveRoles"; public const string RetrieveRolesByUserIdDataKey = "RoleHelper-RetrieveRolesByUserId"; public const string RetrieveRolesByMenuIdDataKey = "RoleHelper-RetrieveRolesByMenuId"; public const string RetrieveRolesByGroupIdDataKey = "RoleHelper-RetrieveRolesByGroupId"; public const string RetrieveRolesByUserNameDataKey = "RoleHelper-RetrieveRolesByUserName"; public const string RetrieveRolesByUrlDataKey = "RoleHelper-RetrieveRolesByUrl"; /// /// 查询所有角色 /// /// /// public static IEnumerable Retrieves() => CacheManager.GetOrAdd(RetrieveRolesDataKey, key => DbContextManager.Create().Retrieves()); /// /// 保存用户角色关系 /// /// /// /// public static bool SaveByUserId(string userId, IEnumerable roleIds) { var ret = DbContextManager.Create().SaveByUserId(userId, roleIds); if (ret) CacheCleanUtility.ClearCache(userIds: new List() { userId }, roleIds: roleIds); return ret; } /// /// 查询某个用户所拥有的角色 /// /// public static IEnumerable RetrievesByUserId(string userId) => CacheManager.GetOrAdd($"{RetrieveRolesByUserIdDataKey}-{userId}", key => DbContextManager.Create().RetrievesByUserId(userId), RetrieveRolesByUserIdDataKey); /// /// 删除角色表 /// /// public static bool Delete(IEnumerable value) { var ret = DbContextManager.Create().Delete(value); if (ret) CacheCleanUtility.ClearCache(roleIds: value); return ret; } /// /// 保存新建/更新的角色信息 /// /// /// public static bool Save(Role p) { var ret = DbContextManager.Create().Save(p); if (ret) CacheCleanUtility.ClearCache(roleIds: string.IsNullOrEmpty(p.Id) ? new List() : new List { p.Id }); return ret; } /// /// 查询某个菜单所拥有的角色 /// /// /// public static IEnumerable RetrievesByMenuId(string menuId) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveRolesByMenuIdDataKey, menuId), key => DbContextManager.Create().RetrievesByMenuId(menuId), RetrieveRolesByMenuIdDataKey); /// /// /// /// /// /// public static bool SavaByMenuId(string menuId, IEnumerable roleIds) { var ret = DbContextManager.Create().SavaByMenuId(menuId, roleIds); if (ret) CacheCleanUtility.ClearCache(roleIds: roleIds, menuIds: new List() { menuId }); return ret; } /// /// 根据GroupId查询和该Group有关的所有Roles /// /// /// public static IEnumerable RetrievesByGroupId(string groupId) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveRolesByGroupIdDataKey, groupId), key => DbContextManager.Create().RetrievesByGroupId(groupId), RetrieveRolesByGroupIdDataKey); /// /// 根据GroupId更新Roles信息,删除旧的Roles信息,插入新的Roles信息 /// /// /// /// public static bool SaveByGroupId(string groupId, IEnumerable roleIds) { var ret = DbContextManager.Create().SaveByGroupId(groupId, roleIds); if (ret) CacheCleanUtility.ClearCache(roleIds: roleIds, groupIds: new List() { groupId }); return ret; } /// /// /// /// /// public static IEnumerable RetrieveRolesByUserName(string userName) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveRolesByUserNameDataKey, userName), key => DbContextManager.Create().RetrieveRolesByUserName(userName), RetrieveRolesByUserNameDataKey); /// /// /// /// /// public static IEnumerable RetrieveRolesByUrl(string url) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveRolesByUrlDataKey, url), key => DbContextManager.Create().RetrieveRolesByUrl(url), RetrieveRolesByUrlDataKey); } }