feat(billing): 新增逾期账单自动标记定时任务
1. 新增Hangfire定时任务BillingOverdueProcessJob 2. 修复逾期账单查询条件过宽问题 3. 默认每10分钟执行一次逾期检查
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
using MediatR;
|
||||
using TakeoutSaaS.Application.App.Billings.Commands;
|
||||
using TakeoutSaaS.Domain.Tenants.Repositories;
|
||||
using TakeoutSaaS.Domain.Tenants.Services;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Billings.Handlers;
|
||||
|
||||
@@ -8,42 +8,13 @@ namespace TakeoutSaaS.Application.App.Billings.Handlers;
|
||||
/// 处理逾期账单命令处理器(后台任务)。
|
||||
/// </summary>
|
||||
public sealed class ProcessOverdueBillingsCommandHandler(
|
||||
ITenantBillingRepository billingRepository)
|
||||
IBillingDomainService billingDomainService)
|
||||
: IRequestHandler<ProcessOverdueBillingsCommand, int>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task<int> Handle(ProcessOverdueBillingsCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
// 1. 查询逾期账单(到期日已过且未支付)
|
||||
var overdueBillings = await billingRepository.GetOverdueBillingsAsync(cancellationToken);
|
||||
if (overdueBillings.Count == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 2. (空行后) 标记为逾期并更新通知时间
|
||||
var now = DateTime.UtcNow;
|
||||
var updatedCount = 0;
|
||||
foreach (var billing in overdueBillings)
|
||||
{
|
||||
var before = billing.Status;
|
||||
billing.MarkAsOverdue();
|
||||
|
||||
if (before != billing.Status)
|
||||
{
|
||||
billing.OverdueNotifiedAt ??= now;
|
||||
await billingRepository.UpdateAsync(billing, cancellationToken);
|
||||
updatedCount++;
|
||||
}
|
||||
}
|
||||
|
||||
// 3. (空行后) 持久化
|
||||
if (updatedCount > 0)
|
||||
{
|
||||
await billingRepository.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
|
||||
return updatedCount;
|
||||
// 1. 委托领域服务执行逾期账单处理(Pending && DueDate < Now -> Overdue)
|
||||
return await billingDomainService.ProcessOverdueBillingsAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user