using Longbow.Cache;
using Longbow.Data;
using System.Collections.Generic;
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 = "GroupHelper-RetrieveGroupsByUserName";
///
/// 查询所有群组信息
///
///
public static IEnumerable Retrieves() => CacheManager.GetOrAdd(RetrieveGroupsDataKey, key => DbContextManager.Create().Retrieves());
///
/// 删除群组信息
///
///
public static bool Delete(IEnumerable values)
{
var ret = DbContextManager.Create().Delete(values);
if (ret) CacheCleanUtility.ClearCache(groupIds: values);
return ret;
}
///
/// 保存新建/更新的群组信息
///
///
///
public static bool Save(Group p)
{
var ret = DbContextManager.Create().Save(p);
if (ret) CacheCleanUtility.ClearCache(groupIds: string.IsNullOrEmpty(p.Id) ? new List() : new List() { p.Id });
return ret;
}
///
/// 根据用户查询部门信息
///
///
///
public static IEnumerable RetrievesByUserId(string userId) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveGroupsByUserIdDataKey, userId), k => DbContextManager.Create().RetrievesByUserId(userId), RetrieveGroupsByUserIdDataKey);
///
/// 保存用户部门关系
///
///
///
///
public static bool SaveByUserId(string userId, IEnumerable groupIds)
{
var ret = DbContextManager.Create().SaveByUserId(userId, groupIds);
if (ret) CacheCleanUtility.ClearCache(groupIds: groupIds, userIds: new List() { userId });
return ret;
}
///
/// 根据角色ID指派部门
///
///
///
public static IEnumerable RetrievesByRoleId(string roleId) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveGroupsByRoleIdDataKey, roleId), key => DbContextManager.Create().RetrievesByRoleId(roleId), RetrieveGroupsByRoleIdDataKey);
///
/// 根据角色ID以及选定的部门ID,保到角色部门表
///
///
///
///
public static bool SaveByRoleId(string roleId, IEnumerable groupIds)
{
var ret = DbContextManager.Create().SaveByRoleId(roleId, groupIds);
if (ret) CacheCleanUtility.ClearCache(groupIds: groupIds, roleIds: new List() { roleId });
return ret;
}
///
///
///
///
///
public static IEnumerable RetrievesByUserName(string userName) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveGroupsByUserNameDataKey, userName), r => DbContextManager.Create().RetrievesByUserName(userName), RetrieveGroupsByUserNameDataKey);
}
}