feat(billing): 新增逾期账单自动标记定时任务

1. 新增Hangfire定时任务BillingOverdueProcessJob
2. 修复逾期账单查询条件过宽问题
3. 默认每10分钟执行一次逾期检查
This commit is contained in:
2025-12-18 12:14:01 +08:00
parent a5abd6ef90
commit 15a35d8e40
7 changed files with 73 additions and 40 deletions

View File

@@ -102,14 +102,13 @@ public sealed class TenantBillingRepository(TakeoutAppDbContext context) : ITena
// 1. 以当前 UTC 时间作为逾期判断基准
var now = DateTime.UtcNow;
// 2. (空行后) 查询逾期且未结清/未取消的账单
// 2. (空行后) 查询逾期且仍处于待支付的账单(仅 Pending 才允许自动切换为 Overdue
return await context.TenantBillingStatements
.IgnoreQueryFilters()
.AsNoTracking()
.Where(x => x.DeletedAt == null
&& x.DueDate < now
&& x.Status != TenantBillingStatus.Paid
&& x.Status != TenantBillingStatus.Cancelled)
&& x.Status == TenantBillingStatus.Pending)
.OrderBy(x => x.DueDate)
.ToListAsync(cancellationToken);
}

View File

@@ -147,14 +147,14 @@ public sealed class BillingDomainService(
/// <inheritdoc />
public async Task<int> ProcessOverdueBillingsAsync(CancellationToken cancellationToken = default)
{
// 1. 查询当前已逾期且未支付/未取消的账单(由仓储按 DueDate 筛选)
// 1. 查询当前已超过到期日且仍处于待支付的账单(由仓储按 DueDate + Status 筛选)
var overdueBillings = await billingRepository.GetOverdueBillingsAsync(cancellationToken);
if (overdueBillings.Count == 0)
{
return 0;
}
// 2. (空行后) 批量标记逾期(仅处理 Pending
// 2. (空行后) 批量标记逾期(防御性:再次判断 Pending
var processedAt = DateTime.UtcNow;
var updated = 0;
foreach (var billing in overdueBillings)