
// 








using NetTaste;
using SimpleRedis;

namespace SafeCampus.System;

/// <summary>
/// 用户模块事件总线
/// </summary>
public class UserEventSubscriber : IEventSubscriber, ISingleton
{
    private readonly IServiceProvider _services;
    private readonly ISimpleCacheService _simpleCacheService;


    public UserEventSubscriber(IServiceProvider services, ISimpleCacheService simpleCacheService)
    {
        _services = services;
        _simpleCacheService = simpleCacheService;
    }

    /// <summary>
    /// 根据角色ID列表清除用户缓存
    /// </summary>
    /// <param name="context"></param>
    /// <returns></returns>
    [EventSubscribe(EventSubscriberConst.CLEAR_USER_CACHE)]
    public async Task DeleteUserCacheByRoleIds(EventHandlerExecutingContext context)
    {
        var roleIds = (List<long>)context.Source.Payload;//获取角色ID

        // 创建新的作用域
        using var scope = _services.CreateScope();
        // 解析角色服务
        var relationService = scope.ServiceProvider.GetRequiredService<IRelationService>();
        //获取用户和角色关系
        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<ISysUserService>();
            //从redis中删除
            userService.DeleteUserFromRedis(userIds);
        }
    }

    /// <summary>
    /// 根据机构ID列表清除用户token
    /// </summary>
    /// <param name="context"></param>
    [EventSubscribe(EventSubscriberConst.CLEAR_ORG_TOKEN)]
    public async Task DeleteUserTokenByOrgIds(EventHandlerExecutingContext context)
    {
        var orgIds = (List<long>)context.Source.Payload;//获取机构ID
        // 创建新的作用域
        using var scope = _services.CreateScope();
        // 获取用户ID列表
        var userIds = await DbContext.DB.QueryableWithAttr<SysUser>().Where(it => orgIds.Contains(it.OrgId)).Select(it => it.Id).ToListAsync();
        //从redis中删除所属机构的用户token
        _simpleCacheService.HashDel<List<Token>>(CacheConst.CACHE_USER_TOKEN, userIds.Select(it => it.ToString()).ToArray());
    }

    /// <summary>
    /// 根据模块ID列表清除用户token
    /// </summary>
    /// <param name="context"></param>
    [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<IRelationService>();
        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<ISysUserService>();
        //从redis中删除用户信息
        userService.DeleteUserFromRedis(userIds);
        //从redis中删除用户token
        _simpleCacheService.HashDel<List<Token>>(CacheConst.CACHE_USER_TOKEN, userIds.Select(it => it.ToString()).ToArray());
    }
}
