diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application.Contracts/IServices/IRechargeService.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application.Contracts/IServices/IRechargeService.cs new file mode 100644 index 00000000..6d821715 --- /dev/null +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application.Contracts/IServices/IRechargeService.cs @@ -0,0 +1,9 @@ +namespace Yi.Framework.AiHub.Application.Contracts.IServices; + +public interface IRechargeService +{ + /// + /// 移除用户vip及角色 + /// + Task RemoveVipRoleByExpireAsync(); +} \ No newline at end of file diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application.Contracts/Yi.Framework.AiHub.Application.Contracts.csproj b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application.Contracts/Yi.Framework.AiHub.Application.Contracts.csproj index 1aeef2e7..62169ab8 100644 --- a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application.Contracts/Yi.Framework.AiHub.Application.Contracts.csproj +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application.Contracts/Yi.Framework.AiHub.Application.Contracts.csproj @@ -6,8 +6,5 @@ - - - diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/RechargeService.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/RechargeService.cs index 913ae912..0bc30196 100644 --- a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/RechargeService.cs +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/RechargeService.cs @@ -4,27 +4,33 @@ using Microsoft.AspNetCore.Mvc; using Volo.Abp.Application.Services; using Volo.Abp.Users; using Yi.Framework.AiHub.Application.Contracts.Dtos.Recharge; +using Yi.Framework.AiHub.Application.Contracts.IServices; using Yi.Framework.AiHub.Domain.Entities; +using Yi.Framework.AiHub.Domain.Managers; using Yi.Framework.AiHub.Domain.Shared.Consts; using Yi.Framework.Rbac.Application.Contracts.IServices; using Yi.Framework.SqlSugarCore.Abstractions; namespace Yi.Framework.AiHub.Application.Services { - public class RechargeService : ApplicationService + public class RechargeService : ApplicationService,IRechargeService { private readonly ISqlSugarRepository _repository; private readonly ICurrentUser _currentUser; private readonly IUserService _userService; + private readonly IRoleService _roleService; + private readonly AiRechargeManager _aiMessageManager; public RechargeService( ISqlSugarRepository repository, ICurrentUser currentUser, - IUserService userService) + IUserService userService, IRoleService roleService, AiRechargeManager aiMessageManager) { _repository = repository; _currentUser = currentUser; _userService = userService; + _roleService = roleService; + _aiMessageManager = aiMessageManager; } /// @@ -69,5 +75,18 @@ namespace Yi.Framework.AiHub.Application.Services await _userService.AddUserRoleByRoleCodeAsync(input.UserId, new List() { AiHubConst.VipRole, "default" }); } + + /// + /// 移除用户vip及角色 + /// + [RemoteService(isEnabled: false)] + public async Task RemoveVipRoleByExpireAsync() + { + var expiredUserIds = await _aiMessageManager.RemoveVipByExpireAsync(); + if (expiredUserIds is not null) + { + await _roleService.RemoveUserRoleByRoleCodeAsync(expiredUserIds, AiHubConst.VipRole); + } + } } } \ No newline at end of file diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application.Contracts/Dtos/MessageInputDto.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain.Shared/Dtos/MessageInputDto.cs similarity index 58% rename from Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application.Contracts/Dtos/MessageInputDto.cs rename to Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain.Shared/Dtos/MessageInputDto.cs index 967e1d75..b9fc5854 100644 --- a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application.Contracts/Dtos/MessageInputDto.cs +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain.Shared/Dtos/MessageInputDto.cs @@ -1,8 +1,6 @@ -using SqlSugar; -using Yi.Framework.AiHub.Domain.Shared.Dtos; -using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi; +using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi; -namespace Yi.Framework.AiHub.Application.Contracts.Dtos; +namespace Yi.Framework.AiHub.Domain.Shared.Dtos; public class MessageInputDto { diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Managers/AiGateWayManager.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Managers/AiGateWayManager.cs index 4d0895d3..fe49de86 100644 --- a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Managers/AiGateWayManager.cs +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Managers/AiGateWayManager.cs @@ -9,7 +9,6 @@ using Microsoft.Extensions.Logging; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using Volo.Abp.Domain.Services; -using Yi.Framework.AiHub.Application.Contracts.Dtos; using Yi.Framework.AiHub.Domain.AiGateWay; using Yi.Framework.AiHub.Domain.AiGateWay.Exceptions; using Yi.Framework.AiHub.Domain.Entities.Model; diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Managers/AiMessageManager.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Managers/AiMessageManager.cs index df8e7586..2620c2a2 100644 --- a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Managers/AiMessageManager.cs +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Managers/AiMessageManager.cs @@ -1,8 +1,8 @@ using Volo.Abp.Domain.Services; using Volo.Abp.Users; -using Yi.Framework.AiHub.Application.Contracts.Dtos; using Yi.Framework.AiHub.Domain.Entities; using Yi.Framework.AiHub.Domain.Entities.Chat; +using Yi.Framework.AiHub.Domain.Shared.Dtos; using Yi.Framework.SqlSugarCore.Abstractions; namespace Yi.Framework.AiHub.Domain.Managers; diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Managers/AiRechargeManager.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Managers/AiRechargeManager.cs index 52998cf5..f29d82e9 100644 --- a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Managers/AiRechargeManager.cs +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Managers/AiRechargeManager.cs @@ -2,8 +2,6 @@ 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; @@ -11,21 +9,18 @@ namespace Yi.Framework.AiHub.Domain.Managers; public class AiRechargeManager : DomainService { private readonly ISqlSugarRepository _rechargeRepository; - private readonly IRoleService _roleService; private readonly ISqlSugarRepository _tokenRepository; private readonly ILogger _logger; public AiRechargeManager(ISqlSugarRepository rechargeRepository, - ISqlSugarRepository tokenRepository, ILogger logger, - IRoleService roleService) + ISqlSugarRepository tokenRepository, ILogger logger) { _rechargeRepository = rechargeRepository; _tokenRepository = tokenRepository; _logger = logger; - _roleService = roleService; } - public async Task RemoveVipRoleByExpireAsync() + public async Task?> RemoveVipByExpireAsync() { _logger.LogInformation("开始执行VIP过期自动卸载任务"); @@ -39,7 +34,7 @@ public class AiRechargeManager : DomainService if (!allRecharges.Any()) { _logger.LogInformation("没有找到任何充值记录"); - return; + return null; } // 按用户分组,找出真正过期的用户 @@ -63,18 +58,18 @@ public class AiRechargeManager : DomainService if (!expiredUserIds.Any()) { _logger.LogInformation("没有找到过期的VIP用户"); - return; + return null; } _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} 个过期用户"); + + return expiredUserIds; } } \ No newline at end of file diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Yi.Framework.AiHub.Domain.csproj b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Yi.Framework.AiHub.Domain.csproj index 34f50862..d2e6435b 100644 --- a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Yi.Framework.AiHub.Domain.csproj +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Yi.Framework.AiHub.Domain.csproj @@ -9,7 +9,6 @@ - diff --git a/Yi.Abp.Net8/src/Yi.Abp.Web/Jobs/ai-hub/VipExpireJob.cs b/Yi.Abp.Net8/src/Yi.Abp.Web/Jobs/ai-hub/VipExpireJob.cs index 5576a0d5..7ed5ab44 100644 --- a/Yi.Abp.Net8/src/Yi.Abp.Web/Jobs/ai-hub/VipExpireJob.cs +++ b/Yi.Abp.Net8/src/Yi.Abp.Web/Jobs/ai-hub/VipExpireJob.cs @@ -1,6 +1,7 @@ using Microsoft.Extensions.Logging; using SqlSugar; using Volo.Abp.BackgroundWorkers.Hangfire; +using Yi.Framework.AiHub.Application.Contracts.IServices; using Yi.Framework.AiHub.Domain.Entities; using Yi.Framework.AiHub.Domain.Entities.OpenApi; using Yi.Framework.AiHub.Domain.Managers; @@ -14,11 +15,11 @@ namespace Yi.Abp.Web.Jobs.ai_hub; /// public class VipExpireJob : HangfireBackgroundWorkerBase { - private readonly AiRechargeManager _aiRechargeManager; + private readonly IRechargeService _rechargeService; - public VipExpireJob(AiRechargeManager aiRechargeManager) + public VipExpireJob(IRechargeService rechargeService) { - _aiRechargeManager = aiRechargeManager; + _rechargeService = rechargeService; RecurringJobId = "VIP过期自动卸载"; // 每天凌晨0点执行一次 CronExpression = "0 0 0 * * ?"; @@ -26,6 +27,6 @@ public class VipExpireJob : HangfireBackgroundWorkerBase public override async Task DoWorkAsync(CancellationToken cancellationToken = new CancellationToken()) { - await _aiRechargeManager.RemoveVipRoleByExpireAsync(); + await _rechargeService.RemoveVipRoleByExpireAsync(); } } \ No newline at end of file