feat: 租户冻结解冻与订阅延期接口
This commit is contained in:
@@ -188,6 +188,50 @@ public sealed class TenantsController(IMediator mediator) : BaseApiController
|
||||
return ApiResponse<TenantReviewClaimDto?>.Ok(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 冻结租户(暂停服务)。
|
||||
/// </summary>
|
||||
/// <returns>冻结后的租户信息。</returns>
|
||||
[HttpPost("{tenantId:long}/freeze")]
|
||||
[PermissionAuthorize("tenant:review")]
|
||||
[ProducesResponseType(typeof(ApiResponse<TenantDto>), StatusCodes.Status200OK)]
|
||||
public async Task<ApiResponse<TenantDto>> Freeze(
|
||||
long tenantId,
|
||||
[FromBody] FreezeTenantCommand body,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
// 1. 合并路由参数
|
||||
var command = body with { TenantId = tenantId };
|
||||
|
||||
// 2. 执行冻结
|
||||
var result = await mediator.Send(command, cancellationToken);
|
||||
|
||||
// 3. 返回冻结结果
|
||||
return ApiResponse<TenantDto>.Ok(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 解冻租户(恢复服务)。
|
||||
/// </summary>
|
||||
/// <returns>解冻后的租户信息。</returns>
|
||||
[HttpPost("{tenantId:long}/unfreeze")]
|
||||
[PermissionAuthorize("tenant:review")]
|
||||
[ProducesResponseType(typeof(ApiResponse<TenantDto>), StatusCodes.Status200OK)]
|
||||
public async Task<ApiResponse<TenantDto>> Unfreeze(
|
||||
long tenantId,
|
||||
[FromBody] UnfreezeTenantCommand body,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
// 1. 合并路由参数
|
||||
var command = body with { TenantId = tenantId };
|
||||
|
||||
// 2. 执行解冻
|
||||
var result = await mediator.Send(command, cancellationToken);
|
||||
|
||||
// 3. 返回解冻结果
|
||||
return ApiResponse<TenantDto>.Ok(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建或续费租户订阅。
|
||||
/// </summary>
|
||||
@@ -208,6 +252,28 @@ public sealed class TenantsController(IMediator mediator) : BaseApiController
|
||||
return ApiResponse<TenantSubscriptionDto>.Ok(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 延期/赠送租户订阅时长(按当前订阅套餐续费)。
|
||||
/// </summary>
|
||||
/// <returns>续费后的订阅信息。</returns>
|
||||
[HttpPost("{tenantId:long}/subscriptions/extend")]
|
||||
[PermissionAuthorize("tenant:subscription")]
|
||||
[ProducesResponseType(typeof(ApiResponse<TenantSubscriptionDto>), StatusCodes.Status200OK)]
|
||||
public async Task<ApiResponse<TenantSubscriptionDto>> ExtendSubscription(
|
||||
long tenantId,
|
||||
[FromBody] ExtendTenantSubscriptionCommand body,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
// 1. 合并租户标识
|
||||
var command = body with { TenantId = tenantId };
|
||||
|
||||
// 2. 执行延期/赠送
|
||||
var result = await mediator.Send(command, cancellationToken);
|
||||
|
||||
// 3. 返回订阅结果
|
||||
return ApiResponse<TenantSubscriptionDto>.Ok(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 套餐升降配。
|
||||
/// </summary>
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
using MediatR;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using TakeoutSaaS.Application.App.Tenants.Dto;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Tenants.Commands;
|
||||
|
||||
/// <summary>
|
||||
/// 延期/赠送租户订阅时长(按当前订阅套餐续费)。
|
||||
/// </summary>
|
||||
public sealed record ExtendTenantSubscriptionCommand : IRequest<TenantSubscriptionDto>
|
||||
{
|
||||
/// <summary>
|
||||
/// 租户 ID(雪花算法)。
|
||||
/// </summary>
|
||||
[Required]
|
||||
public long TenantId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 赠送/延期时长(月)。
|
||||
/// </summary>
|
||||
[Range(1, 120)]
|
||||
public int DurationMonths { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 备注信息。
|
||||
/// </summary>
|
||||
[MaxLength(256)]
|
||||
public string? Notes { get; init; }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
using MediatR;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using TakeoutSaaS.Application.App.Tenants.Dto;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Tenants.Commands;
|
||||
|
||||
/// <summary>
|
||||
/// 冻结租户(将租户状态置为暂停)。
|
||||
/// </summary>
|
||||
public sealed record FreezeTenantCommand : IRequest<TenantDto>
|
||||
{
|
||||
/// <summary>
|
||||
/// 租户 ID(雪花算法)。
|
||||
/// </summary>
|
||||
[Required]
|
||||
public long TenantId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 冻结原因。
|
||||
/// </summary>
|
||||
[Required]
|
||||
[MaxLength(256)]
|
||||
public string Reason { get; init; } = string.Empty;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
using MediatR;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using TakeoutSaaS.Application.App.Tenants.Dto;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Tenants.Commands;
|
||||
|
||||
/// <summary>
|
||||
/// 解冻租户(恢复租户状态)。
|
||||
/// </summary>
|
||||
public sealed record UnfreezeTenantCommand : IRequest<TenantDto>
|
||||
{
|
||||
/// <summary>
|
||||
/// 租户 ID(雪花算法)。
|
||||
/// </summary>
|
||||
[Required]
|
||||
public long TenantId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 解冻备注(可选)。
|
||||
/// </summary>
|
||||
[MaxLength(256)]
|
||||
public string? Reason { get; init; }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
using MediatR;
|
||||
using TakeoutSaaS.Application.App.Tenants.Commands;
|
||||
using TakeoutSaaS.Application.App.Tenants.Dto;
|
||||
using TakeoutSaaS.Domain.Tenants.Entities;
|
||||
using TakeoutSaaS.Domain.Tenants.Enums;
|
||||
using TakeoutSaaS.Domain.Tenants.Repositories;
|
||||
using TakeoutSaaS.Shared.Abstractions.Constants;
|
||||
using TakeoutSaaS.Shared.Abstractions.Exceptions;
|
||||
using TakeoutSaaS.Shared.Abstractions.Ids;
|
||||
using TakeoutSaaS.Shared.Abstractions.Security;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Tenants.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// 延期/赠送订阅处理器(按当前订阅套餐续费)。
|
||||
/// </summary>
|
||||
public sealed class ExtendTenantSubscriptionCommandHandler(
|
||||
ITenantRepository tenantRepository,
|
||||
IIdGenerator idGenerator,
|
||||
ICurrentUserAccessor currentUserAccessor)
|
||||
: IRequestHandler<ExtendTenantSubscriptionCommand, TenantSubscriptionDto>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task<TenantSubscriptionDto> Handle(ExtendTenantSubscriptionCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
// 1. 校验时长
|
||||
if (request.DurationMonths <= 0)
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.BadRequest, "延期/赠送时长必须大于 0");
|
||||
}
|
||||
|
||||
// 2. 获取租户与当前订阅
|
||||
var tenant = await tenantRepository.FindByIdAsync(request.TenantId, cancellationToken)
|
||||
?? throw new BusinessException(ErrorCodes.NotFound, "租户不存在");
|
||||
|
||||
var current = await tenantRepository.GetActiveSubscriptionAsync(request.TenantId, cancellationToken)
|
||||
?? throw new BusinessException(ErrorCodes.BadRequest, "订阅不存在,无法延期/赠送");
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
var effectiveFrom = current.EffectiveTo > now ? current.EffectiveTo : now;
|
||||
var effectiveTo = effectiveFrom.AddMonths(request.DurationMonths);
|
||||
|
||||
var previousStatus = tenant.Status;
|
||||
var actorName = currentUserAccessor.IsAuthenticated
|
||||
? $"user:{currentUserAccessor.UserId}"
|
||||
: "system";
|
||||
|
||||
// 3. (空行后) 创建续费订阅
|
||||
var subscription = new TenantSubscription
|
||||
{
|
||||
Id = idGenerator.NextId(),
|
||||
TenantId = tenant.Id,
|
||||
TenantPackageId = current.TenantPackageId,
|
||||
EffectiveFrom = effectiveFrom,
|
||||
EffectiveTo = effectiveTo,
|
||||
NextBillingDate = effectiveTo,
|
||||
Status = SubscriptionStatus.Active,
|
||||
AutoRenew = current.AutoRenew,
|
||||
Notes = request.Notes
|
||||
};
|
||||
|
||||
await tenantRepository.AddSubscriptionAsync(subscription, cancellationToken);
|
||||
await tenantRepository.AddSubscriptionHistoryAsync(new TenantSubscriptionHistory
|
||||
{
|
||||
Id = idGenerator.NextId(),
|
||||
TenantId = tenant.Id,
|
||||
TenantSubscriptionId = subscription.Id,
|
||||
FromPackageId = current.TenantPackageId,
|
||||
ToPackageId = current.TenantPackageId,
|
||||
ChangeType = SubscriptionChangeType.Renew,
|
||||
EffectiveFrom = effectiveFrom,
|
||||
EffectiveTo = effectiveTo,
|
||||
Amount = null,
|
||||
Currency = null,
|
||||
Notes = request.Notes
|
||||
}, cancellationToken);
|
||||
|
||||
// 4. (空行后) 若租户处于到期状态则恢复为正常(冻结状态需先解冻)
|
||||
if (tenant.Status == TenantStatus.Expired)
|
||||
{
|
||||
tenant.Status = TenantStatus.Active;
|
||||
}
|
||||
|
||||
tenant.EffectiveFrom = subscription.EffectiveFrom;
|
||||
tenant.EffectiveTo = subscription.EffectiveTo;
|
||||
|
||||
await tenantRepository.UpdateTenantAsync(tenant, cancellationToken);
|
||||
|
||||
// 5. (空行后) 记录审计
|
||||
await tenantRepository.AddAuditLogAsync(new TenantAuditLog
|
||||
{
|
||||
TenantId = tenant.Id,
|
||||
Action = TenantAuditAction.SubscriptionUpdated,
|
||||
Title = "延期/赠送时长",
|
||||
Description = $"续费 {request.DurationMonths} 月,到期时间:{current.EffectiveTo:yyyy-MM-dd HH:mm:ss} -> {subscription.EffectiveTo:yyyy-MM-dd HH:mm:ss}",
|
||||
OperatorId = currentUserAccessor.UserId == 0 ? null : currentUserAccessor.UserId,
|
||||
OperatorName = actorName,
|
||||
PreviousStatus = previousStatus,
|
||||
CurrentStatus = tenant.Status
|
||||
}, cancellationToken);
|
||||
|
||||
// 6. 保存并返回
|
||||
await tenantRepository.SaveChangesAsync(cancellationToken);
|
||||
return subscription.ToSubscriptionDto()
|
||||
?? throw new BusinessException(ErrorCodes.InternalServerError, "订阅生成失败");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
using MediatR;
|
||||
using TakeoutSaaS.Application.App.Tenants.Commands;
|
||||
using TakeoutSaaS.Application.App.Tenants.Dto;
|
||||
using TakeoutSaaS.Domain.Tenants.Entities;
|
||||
using TakeoutSaaS.Domain.Tenants.Enums;
|
||||
using TakeoutSaaS.Domain.Tenants.Repositories;
|
||||
using TakeoutSaaS.Shared.Abstractions.Constants;
|
||||
using TakeoutSaaS.Shared.Abstractions.Exceptions;
|
||||
using TakeoutSaaS.Shared.Abstractions.Security;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Tenants.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// 冻结租户处理器。
|
||||
/// </summary>
|
||||
public sealed class FreezeTenantCommandHandler(
|
||||
ITenantRepository tenantRepository,
|
||||
ICurrentUserAccessor currentUserAccessor)
|
||||
: IRequestHandler<FreezeTenantCommand, TenantDto>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task<TenantDto> Handle(FreezeTenantCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
// 1. 获取租户与订阅
|
||||
var tenant = await tenantRepository.FindByIdAsync(request.TenantId, cancellationToken)
|
||||
?? throw new BusinessException(ErrorCodes.NotFound, "租户不存在");
|
||||
|
||||
var subscription = await tenantRepository.GetActiveSubscriptionAsync(request.TenantId, cancellationToken);
|
||||
var verification = await tenantRepository.GetVerificationProfileAsync(request.TenantId, cancellationToken);
|
||||
|
||||
var previousStatus = tenant.Status;
|
||||
if (tenant.Status == TenantStatus.Closed)
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.BadRequest, "已注销租户不可冻结");
|
||||
}
|
||||
|
||||
if (tenant.Status == TenantStatus.Suspended)
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.Conflict, "租户已被冻结");
|
||||
}
|
||||
|
||||
// 2. 更新租户状态
|
||||
tenant.Status = TenantStatus.Suspended;
|
||||
tenant.SuspendedAt = DateTime.UtcNow;
|
||||
tenant.SuspensionReason = request.Reason;
|
||||
|
||||
// 3. (空行后) 同步暂停订阅
|
||||
if (subscription != null)
|
||||
{
|
||||
subscription.Status = SubscriptionStatus.Suspended;
|
||||
await tenantRepository.UpdateSubscriptionAsync(subscription, cancellationToken);
|
||||
}
|
||||
|
||||
await tenantRepository.UpdateTenantAsync(tenant, cancellationToken);
|
||||
|
||||
// 4. (空行后) 记录审计
|
||||
var actorName = currentUserAccessor.IsAuthenticated
|
||||
? $"user:{currentUserAccessor.UserId}"
|
||||
: "system";
|
||||
|
||||
await tenantRepository.AddAuditLogAsync(new TenantAuditLog
|
||||
{
|
||||
TenantId = tenant.Id,
|
||||
Action = TenantAuditAction.StatusChanged,
|
||||
Title = "冻结租户",
|
||||
Description = request.Reason,
|
||||
OperatorId = currentUserAccessor.UserId == 0 ? null : currentUserAccessor.UserId,
|
||||
OperatorName = actorName,
|
||||
PreviousStatus = previousStatus,
|
||||
CurrentStatus = tenant.Status
|
||||
}, cancellationToken);
|
||||
|
||||
// 5. 保存并返回
|
||||
await tenantRepository.SaveChangesAsync(cancellationToken);
|
||||
return TenantMapping.ToDto(tenant, subscription, verification);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
using MediatR;
|
||||
using TakeoutSaaS.Application.App.Tenants.Commands;
|
||||
using TakeoutSaaS.Application.App.Tenants.Dto;
|
||||
using TakeoutSaaS.Domain.Tenants.Entities;
|
||||
using TakeoutSaaS.Domain.Tenants.Enums;
|
||||
using TakeoutSaaS.Domain.Tenants.Repositories;
|
||||
using TakeoutSaaS.Shared.Abstractions.Constants;
|
||||
using TakeoutSaaS.Shared.Abstractions.Exceptions;
|
||||
using TakeoutSaaS.Shared.Abstractions.Security;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Tenants.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// 解冻租户处理器。
|
||||
/// </summary>
|
||||
public sealed class UnfreezeTenantCommandHandler(
|
||||
ITenantRepository tenantRepository,
|
||||
ICurrentUserAccessor currentUserAccessor)
|
||||
: IRequestHandler<UnfreezeTenantCommand, TenantDto>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task<TenantDto> Handle(UnfreezeTenantCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
// 1. 获取租户与订阅
|
||||
var tenant = await tenantRepository.FindByIdAsync(request.TenantId, cancellationToken)
|
||||
?? throw new BusinessException(ErrorCodes.NotFound, "租户不存在");
|
||||
|
||||
var subscription = await tenantRepository.GetActiveSubscriptionAsync(request.TenantId, cancellationToken);
|
||||
var verification = await tenantRepository.GetVerificationProfileAsync(request.TenantId, cancellationToken);
|
||||
|
||||
var previousStatus = tenant.Status;
|
||||
if (tenant.Status != TenantStatus.Suspended)
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.BadRequest, "当前租户未处于冻结状态");
|
||||
}
|
||||
|
||||
// 2. 计算恢复状态(到期则回到到期状态)
|
||||
var now = DateTime.UtcNow;
|
||||
var isExpired = subscription != null && subscription.EffectiveTo <= now;
|
||||
|
||||
tenant.Status = isExpired ? TenantStatus.Expired : TenantStatus.Active;
|
||||
tenant.SuspendedAt = null;
|
||||
tenant.SuspensionReason = null;
|
||||
|
||||
// 3. (空行后) 同步订阅状态
|
||||
if (subscription != null)
|
||||
{
|
||||
subscription.Status = isExpired ? SubscriptionStatus.GracePeriod : SubscriptionStatus.Active;
|
||||
await tenantRepository.UpdateSubscriptionAsync(subscription, cancellationToken);
|
||||
}
|
||||
|
||||
await tenantRepository.UpdateTenantAsync(tenant, cancellationToken);
|
||||
|
||||
// 4. (空行后) 记录审计
|
||||
var actorName = currentUserAccessor.IsAuthenticated
|
||||
? $"user:{currentUserAccessor.UserId}"
|
||||
: "system";
|
||||
|
||||
await tenantRepository.AddAuditLogAsync(new TenantAuditLog
|
||||
{
|
||||
TenantId = tenant.Id,
|
||||
Action = TenantAuditAction.StatusChanged,
|
||||
Title = "解冻租户",
|
||||
Description = request.Reason,
|
||||
OperatorId = currentUserAccessor.UserId == 0 ? null : currentUserAccessor.UserId,
|
||||
OperatorName = actorName,
|
||||
PreviousStatus = previousStatus,
|
||||
CurrentStatus = tenant.Status
|
||||
}, cancellationToken);
|
||||
|
||||
// 5. 保存并返回
|
||||
await tenantRepository.SaveChangesAsync(cancellationToken);
|
||||
return TenantMapping.ToDto(tenant, subscription, verification);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user