refactor: 移除AiHub Domain层对Application.Contracts的循环依赖

移除Yi.Framework.AiHub.Domain项目中对Yi.Framework.AiHub.Application.Contracts的项目引用,解决领域层和应用层之间的循环依赖问题,符合DDD架构分层原则。
This commit is contained in:
chenchun
2025-08-11 15:51:59 +08:00
parent 42d537a68b
commit c17c9000a8
9 changed files with 44 additions and 27 deletions

View File

@@ -0,0 +1,9 @@
namespace Yi.Framework.AiHub.Application.Contracts.IServices;
public interface IRechargeService
{
/// <summary>
/// 移除用户vip及角色
/// </summary>
Task RemoveVipRoleByExpireAsync();
}

View File

@@ -6,8 +6,5 @@
<ProjectReference Include="..\..\rbac\Yi.Framework.Rbac.Application.Contracts\Yi.Framework.Rbac.Application.Contracts.csproj" />
<ProjectReference Include="..\Yi.Framework.AiHub.Domain.Shared\Yi.Framework.AiHub.Domain.Shared.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="IServices\" />
</ItemGroup>
</Project>

View File

@@ -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<AiRechargeAggregateRoot> _repository;
private readonly ICurrentUser _currentUser;
private readonly IUserService _userService;
private readonly IRoleService _roleService;
private readonly AiRechargeManager _aiMessageManager;
public RechargeService(
ISqlSugarRepository<AiRechargeAggregateRoot> repository,
ICurrentUser currentUser,
IUserService userService)
IUserService userService, IRoleService roleService, AiRechargeManager aiMessageManager)
{
_repository = repository;
_currentUser = currentUser;
_userService = userService;
_roleService = roleService;
_aiMessageManager = aiMessageManager;
}
/// <summary>
@@ -69,5 +75,18 @@ namespace Yi.Framework.AiHub.Application.Services
await _userService.AddUserRoleByRoleCodeAsync(input.UserId,
new List<string>() { AiHubConst.VipRole, "default" });
}
/// <summary>
/// 移除用户vip及角色
/// </summary>
[RemoteService(isEnabled: false)]
public async Task RemoveVipRoleByExpireAsync()
{
var expiredUserIds = await _aiMessageManager.RemoveVipByExpireAsync();
if (expiredUserIds is not null)
{
await _roleService.RemoveUserRoleByRoleCodeAsync(expiredUserIds, AiHubConst.VipRole);
}
}
}
}

View File

@@ -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
{

View File

@@ -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;

View File

@@ -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;

View File

@@ -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<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)
ISqlSugarRepository<TokenAggregateRoot> tokenRepository, ILogger<AiRechargeManager> logger)
{
_rechargeRepository = rechargeRepository;
_tokenRepository = tokenRepository;
_logger = logger;
_roleService = roleService;
}
public async Task RemoveVipRoleByExpireAsync()
public async Task<List<Guid>?> 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;
}
}

View File

@@ -9,7 +9,6 @@
<ItemGroup>
<ProjectReference Include="..\..\..\framework\Yi.Framework.Mapster\Yi.Framework.Mapster.csproj" />
<ProjectReference Include="..\..\..\framework\Yi.Framework.SqlSugarCore.Abstractions\Yi.Framework.SqlSugarCore.Abstractions.csproj" />
<ProjectReference Include="..\Yi.Framework.AiHub.Application.Contracts\Yi.Framework.AiHub.Application.Contracts.csproj" />
<ProjectReference Include="..\Yi.Framework.AiHub.Domain.Shared\Yi.Framework.AiHub.Domain.Shared.csproj" />
</ItemGroup>

View File

@@ -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;
/// </summary>
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();
}
}