Files
Yi.Framework/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Managers/AiRechargeManager.cs
ccnetcore e6e4829164 feat: 新增VIP过期自动卸载功能
- 新增`AiRechargeManager`类,实现VIP过期用户的自动卸载逻辑。
- 新增`AiHubConst`常量类,统一管理角色名称。
- 在`IRoleService`中添加`RemoveUserRoleByRoleCodeAsync`方法,用于移除指定用户的角色。
- 在`RoleManager`中实现`RemoveUserRoleByRoleCodeAsync`方法。
- 优化`CurrentExtensions`中VIP角色判断逻辑,使用常量替代硬编码。
- 调整`YiAbpWebModule`中部分代码格式,提升可读性。
2025-08-09 13:14:15 +08:00

59 lines
2.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Microsoft.Extensions.Logging;
using Volo.Abp.Domain.Services;
using Yi.Framework.AiHub.Domain.Entities;
using Yi.Framework.AiHub.Domain.Entities.OpenApi;
using Yi.Framework.AiHub.Domain.Shared.Consts;
using Yi.Framework.Rbac.Application.Contracts.IServices;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.AiHub.Domain.Managers;
public class AiRechargeManager : DomainService
{
private readonly ISqlSugarRepository<AiRechargeAggregateRoot> _rechargeRepository;
private readonly IRoleService _roleService;
private readonly ISqlSugarRepository<TokenAggregateRoot> _tokenRepository;
private readonly ILogger<AiRechargeManager> _logger;
public AiRechargeManager(ISqlSugarRepository<AiRechargeAggregateRoot> rechargeRepository,
ISqlSugarRepository<TokenAggregateRoot> tokenRepository, ILogger<AiRechargeManager> logger,
IRoleService roleService)
{
_rechargeRepository = rechargeRepository;
_tokenRepository = tokenRepository;
_logger = logger;
_roleService = roleService;
}
public async Task RemoveVipRoleByExpireAsync()
{
_logger.LogInformation("开始执行VIP过期自动卸载任务");
// 获取当前时间
var currentTime = DateTime.Now;
// 查找过期的充值记录
var expiredRecharges = await _rechargeRepository._DbQueryable
.Where(x => x.ExpireDateTime.HasValue && x.ExpireDateTime.Value < currentTime)
.ToListAsync();
if (!expiredRecharges.Any())
{
_logger.LogInformation("没有找到过期的VIP用户");
return;
}
// 获取过期用户的ID列表
var expiredUserIds = expiredRecharges.Select(x => x.UserId).Distinct().ToList();
_logger.LogInformation($"找到 {expiredUserIds.Count} 个过期的VIP用户");
// 获取YiXinAi-Vip角色ID
await _roleService.RemoveUserRoleByRoleCodeAsync(expiredUserIds, AiHubConst.VipRole);
// 删除过期用户的Token密钥
var removedTokenCount = await _tokenRepository.DeleteAsync(x => expiredUserIds.Contains(x.UserId));
_logger.LogInformation($"成功删除 {removedTokenCount} 个用户的Token密钥");
_logger.LogInformation($"VIP过期自动卸载任务执行完成共处理 {expiredUserIds.Count} 个过期用户");
}
}