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 RetrieveGroups() => CacheManager.GetOrAdd(RetrieveGroupsDataKey, key => DbAdapterManager.Create().RetrieveGroups());
///
/// 删除群组信息
///
///
public static bool DeleteGroup(IEnumerable value)
{
var ret = DbAdapterManager.Create().DeleteGroup(value);
if (ret) CacheCleanUtility.ClearCache(groupIds: value);
return ret;
}
///
/// 保存新建/更新的群组信息
///
///
///
public static bool SaveGroup(Group p)
{
var ret = DbAdapterManager.Create().SaveGroup(p);
if (ret) CacheCleanUtility.ClearCache(groupIds: string.IsNullOrEmpty(p.Id) ? new List() : new List() { p.Id });
return ret;
}
///
/// 根据用户查询部门信息
///
///
///
public static IEnumerable RetrieveGroupsByUserId(string userId) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveGroupsByUserIdDataKey, userId), k => DbAdapterManager.Create().RetrieveGroupsByUserId(userId), RetrieveGroupsByUserIdDataKey);
///
/// 保存用户部门关系
///
///
///
///
public static bool SaveGroupsByUserId(string userId, IEnumerable groupIds)
{
var ret = DbAdapterManager.Create().SaveGroupsByUserId(userId, groupIds);
if (ret) CacheCleanUtility.ClearCache(groupIds: groupIds, userIds: new List() { userId });
return ret;
}
///
/// 根据角色ID指派部门
///
///
///
public static IEnumerable RetrieveGroupsByRoleId(string roleId) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveGroupsByRoleIdDataKey, roleId), key => DbAdapterManager.Create().RetrieveGroupsByRoleId(roleId), RetrieveGroupsByRoleIdDataKey);
///
/// 根据角色ID以及选定的部门ID,保到角色部门表
///
///
///
///
public static bool SaveGroupsByRoleId(string roleId, IEnumerable groupIds)
{
var ret = DbAdapterManager.Create().SaveGroupsByRoleId(roleId, groupIds);
if (ret) CacheCleanUtility.ClearCache(groupIds: groupIds, roleIds: new List() { roleId });
return ret;
}
///
///
///
///
///
public static IEnumerable RetrieveGroupsByUserName(string userName) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveGroupsByUserNameDataKey, userName), r => DbAdapterManager.Create().RetrieveGroupsByUserName(userName), RetrieveGroupsByUserNameDataKey);
}
}