using Longbow.Cache; using Longbow.Data; using System.Collections.Generic; using System.Linq; namespace Bootstrap.DataAccess { /// /// /// public static class GroupHelper { public const string RetrieveGroupsDataKey = "GroupHelper-RetrieveGroups"; public const string RetrieveGroupsByUserIdDataKey = "GroupHelper-RetrieveGroupsByUserId"; public const string RetrieveGroupsByRoleIdDataKey = "GroupHelper-RetrieveGroupsByRoleId"; public const string RetrieveGroupsByUserNameDataKey = "BootstrapAdminGroupMiddleware-RetrieveGroupsByUserName"; /// /// 查询所有群组信息 /// /// /// public static IEnumerable RetrieveGroups(int id = 0) { var ret = CacheManager.GetOrAdd(RetrieveGroupsDataKey, key => DbAdapterManager.Create().RetrieveGroups(id)); return id == 0 ? ret : ret.Where(t => id == t.Id); } /// /// 删除群组信息 /// /// public static bool DeleteGroup(IEnumerable value) => DbAdapterManager.Create().DeleteGroup(value); /// /// 保存新建/更新的群组信息 /// /// /// public static bool SaveGroup(Group p) => DbAdapterManager.Create().SaveGroup(p); /// /// 根据用户查询部门信息 /// /// /// public static IEnumerable RetrieveGroupsByUserId(int userId) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveGroupsByUserIdDataKey, userId), k => DbAdapterManager.Create().RetrieveGroupsByUserId(userId), RetrieveGroupsByUserIdDataKey); /// /// 保存用户部门关系 /// /// /// /// public static bool SaveGroupsByUserId(int id, IEnumerable groupIds) => DbAdapterManager.Create().SaveGroupsByUserId(id, groupIds); /// /// 根据角色ID指派部门 /// /// /// public static IEnumerable RetrieveGroupsByRoleId(int roleId) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveGroupsByRoleIdDataKey, roleId), key => DbAdapterManager.Create().RetrieveGroupsByRoleId(roleId), RetrieveGroupsByRoleIdDataKey); /// /// 根据角色ID以及选定的部门ID,保到角色部门表 /// /// /// /// public static bool SaveGroupsByRoleId(int id, IEnumerable groupIds) => DbAdapterManager.Create().SaveGroupsByRoleId(id, groupIds); /// /// /// /// /// public static IEnumerable RetrieveGroupsByUserName(string userName) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveGroupsByUserNameDataKey, userName), r => DbAdapterManager.Create().RetrieveGroupsByUserName(userName), RetrieveGroupsByUserNameDataKey); } }