//
using NetTaste;
using SimpleRedis;
namespace SafeCampus.System;
///
/// 用户模块事件总线
///
public class UserEventSubscriber : IEventSubscriber, ISingleton
{
private readonly IServiceProvider _services;
private readonly ISimpleCacheService _simpleCacheService;
public UserEventSubscriber(IServiceProvider services, ISimpleCacheService simpleCacheService)
{
_services = services;
_simpleCacheService = simpleCacheService;
}
///
/// 根据角色ID列表清除用户缓存
///
///
///
[EventSubscribe(EventSubscriberConst.CLEAR_USER_CACHE)]
public async Task DeleteUserCacheByRoleIds(EventHandlerExecutingContext context)
{
var roleIds = (List)context.Source.Payload;//获取角色ID
// 创建新的作用域
using var scope = _services.CreateScope();
// 解析角色服务
var relationService = scope.ServiceProvider.GetRequiredService();
//获取用户和角色关系
var relations = await relationService.GetRelationListByTargetIdListAndCategory(roleIds.Select(it => it.ToString()).ToList(),
CateGoryConst.RELATION_SYS_USER_HAS_ROLE);
if (relations.Count > 0)
{
var userIds = relations.Select(it => it.ObjectId).ToList();//用户ID列表
// 解析用户服务
var userService = scope.ServiceProvider.GetRequiredService();
//从redis中删除
userService.DeleteUserFromRedis(userIds);
}
}
///
/// 根据机构ID列表清除用户token
///
///
[EventSubscribe(EventSubscriberConst.CLEAR_ORG_TOKEN)]
public async Task DeleteUserTokenByOrgIds(EventHandlerExecutingContext context)
{
var orgIds = (List)context.Source.Payload;//获取机构ID
// 创建新的作用域
using var scope = _services.CreateScope();
// 获取用户ID列表
var userIds = await DbContext.DB.QueryableWithAttr().Where(it => orgIds.Contains(it.OrgId)).Select(it => it.Id).ToListAsync();
//从redis中删除所属机构的用户token
_simpleCacheService.HashDel>(CacheConst.CACHE_USER_TOKEN, userIds.Select(it => it.ToString()).ToArray());
}
///
/// 根据模块ID列表清除用户token
///
///
[EventSubscribe(EventSubscriberConst.CLEAR_MODULE_TOKEN)]
public async Task DeleteUserTokenByModuleId(EventHandlerExecutingContext context)
{
var moduleId = (long)context.Source.Payload;//获取机构ID
// 创建新的作用域
using var scope = _services.CreateScope();
// 解析关系服务
var relationService = scope.ServiceProvider.GetRequiredService();
var roleModuleRelations =
await relationService.GetRelationListByTargetIdAndCategory(moduleId.ToString(), CateGoryConst.RELATION_SYS_ROLE_HAS_MODULE);//角色模块关系
var userModuleRelations =
await relationService.GetRelationListByTargetIdAndCategory(moduleId.ToString(), CateGoryConst.RELATION_SYS_USER_HAS_MODULE);//用户模块关系
var userIds = userModuleRelations.Select(it => it.ObjectId).ToList();//用户ID列表
var roleIds = roleModuleRelations.Select(it => it.ObjectId).ToList();//角色ID列表
var userRoleRelations = await relationService.GetRelationListByTargetIdListAndCategory(roleIds.Select(it => it.ToString()).ToList(),
CateGoryConst.RELATION_SYS_USER_HAS_ROLE);//用户角色关系
userIds.AddRange(userRoleRelations.Select(it => it.ObjectId).ToList());//添加用户ID列表
// 解析用户服务
var userService = scope.ServiceProvider.GetRequiredService();
//从redis中删除用户信息
userService.DeleteUserFromRedis(userIds);
//从redis中删除用户token
_simpleCacheService.HashDel>(CacheConst.CACHE_USER_TOKEN, userIds.Select(it => it.ToString()).ToArray());
}
}