2025-08-09 13:14:15 +08:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
using Volo.Abp.Domain.Services;
|
|
|
|
|
|
using Yi.Framework.AiHub.Domain.Entities;
|
|
|
|
|
|
using Yi.Framework.AiHub.Domain.Entities.OpenApi;
|
2025-10-20 10:18:24 +08:00
|
|
|
|
using Yi.Framework.AiHub.Domain.Shared.Enums;
|
2025-08-09 13:14:15 +08:00
|
|
|
|
using Yi.Framework.SqlSugarCore.Abstractions;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Yi.Framework.AiHub.Domain.Managers;
|
|
|
|
|
|
|
|
|
|
|
|
public class AiRechargeManager : DomainService
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly ISqlSugarRepository<AiRechargeAggregateRoot> _rechargeRepository;
|
|
|
|
|
|
private readonly ISqlSugarRepository<TokenAggregateRoot> _tokenRepository;
|
|
|
|
|
|
private readonly ILogger<AiRechargeManager> _logger;
|
|
|
|
|
|
|
|
|
|
|
|
public AiRechargeManager(ISqlSugarRepository<AiRechargeAggregateRoot> rechargeRepository,
|
2025-08-11 15:51:59 +08:00
|
|
|
|
ISqlSugarRepository<TokenAggregateRoot> tokenRepository, ILogger<AiRechargeManager> logger)
|
2025-08-09 13:14:15 +08:00
|
|
|
|
{
|
|
|
|
|
|
_rechargeRepository = rechargeRepository;
|
|
|
|
|
|
_tokenRepository = tokenRepository;
|
|
|
|
|
|
_logger = logger;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-11 15:51:59 +08:00
|
|
|
|
public async Task<List<Guid>?> RemoveVipByExpireAsync()
|
2025-08-09 13:14:15 +08:00
|
|
|
|
{
|
|
|
|
|
|
_logger.LogInformation("开始执行VIP过期自动卸载任务");
|
|
|
|
|
|
|
|
|
|
|
|
// 获取当前时间
|
|
|
|
|
|
var currentTime = DateTime.Now;
|
|
|
|
|
|
|
2025-08-10 11:53:28 +08:00
|
|
|
|
// 查找所有充值记录,按用户分组
|
2025-10-20 10:18:24 +08:00
|
|
|
|
var allRecharges = await _rechargeRepository._DbQueryable.Where(x => x.RechargeType == RechargeTypeEnum.Vip)
|
2025-08-09 13:14:15 +08:00
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
|
2025-08-10 11:53:28 +08:00
|
|
|
|
if (!allRecharges.Any())
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogInformation("没有找到任何充值记录");
|
2025-08-11 15:51:59 +08:00
|
|
|
|
return null;
|
2025-08-10 11:53:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 按用户分组,找出真正过期的用户
|
|
|
|
|
|
var expiredUserIds = allRecharges
|
|
|
|
|
|
.GroupBy(x => x.UserId)
|
|
|
|
|
|
.Where(group =>
|
|
|
|
|
|
{
|
|
|
|
|
|
// 如果用户有任何一个过期时间为空的记录,说明是永久VIP,不过期
|
|
|
|
|
|
if (group.Any(x => !x.ExpireDateTime.HasValue))
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
// 找到用户最大的过期时间
|
|
|
|
|
|
var maxExpireTime = group.Max(x => x.ExpireDateTime);
|
2025-10-20 10:18:24 +08:00
|
|
|
|
|
2025-08-10 12:07:09 +08:00
|
|
|
|
// 如果最大过期时间小于当前时间,说明用户已过期(比较日期,满足用户最后一天)
|
|
|
|
|
|
return maxExpireTime.HasValue && maxExpireTime.Value.Date < currentTime.Date;
|
2025-08-10 11:53:28 +08:00
|
|
|
|
})
|
|
|
|
|
|
.Select(group => group.Key)
|
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
|
|
if (!expiredUserIds.Any())
|
2025-08-09 13:14:15 +08:00
|
|
|
|
{
|
|
|
|
|
|
_logger.LogInformation("没有找到过期的VIP用户");
|
2025-08-11 15:51:59 +08:00
|
|
|
|
return null;
|
2025-08-09 13:14:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation($"找到 {expiredUserIds.Count} 个过期的VIP用户");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 删除过期用户的Token密钥
|
|
|
|
|
|
var removedTokenCount = await _tokenRepository.DeleteAsync(x => expiredUserIds.Contains(x.UserId));
|
|
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation($"成功删除 {removedTokenCount} 个用户的Token密钥");
|
|
|
|
|
|
_logger.LogInformation($"VIP过期自动卸载任务执行完成,共处理 {expiredUserIds.Count} 个过期用户");
|
2025-08-11 15:51:59 +08:00
|
|
|
|
|
|
|
|
|
|
return expiredUserIds;
|
2025-08-09 13:14:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|