✨ feat: 完成账单管理模块后端功能开发及API优化
核心功能:
- 账单CRUD操作(创建、查询、详情、更新状态、删除)
- 支付记录管理(创建支付、审核支付)
- 批量操作支持(批量更新账单状态)
- 统计分析功能(账单统计、逾期账单查询)
- 导出功能(Excel/PDF/CSV)
API端点 (16个):
- GET /api/admin/v1/billings - 账单列表(分页、筛选、排序)
- POST /api/admin/v1/billings - 创建账单
- GET /api/admin/v1/billings/{id} - 账单详情
- DELETE /api/admin/v1/billings/{id} - 删除账单
- PUT /api/admin/v1/billings/{id}/status - 更新状态
- POST /api/admin/v1/billings/batch/status - 批量更新
- GET /api/admin/v1/billings/{id}/payments - 支付记录
- POST /api/admin/v1/billings/{id}/payments - 创建支付
- PUT /api/admin/v1/billings/payments/{paymentId}/verify - 审核支付
- GET /api/admin/v1/billings/statistics - 统计数据
- GET /api/admin/v1/billings/overdue - 逾期账单
- POST /api/admin/v1/billings/export - 导出账单
架构优化:
- 采用CQRS模式分离读写(MediatR + Dapper + EF Core)
- 完整的领域模型设计(TenantBillingStatement, TenantPayment等)
- FluentValidation请求验证
- 状态机管理账单和支付状态流转
API设计优化 (三项改进):
1. 导出API响应Content-Type改为application/octet-stream
2. 支付审核API添加Approved和Notes可选参数,支持通过/拒绝
3. 移除TenantBillings API中重复的TenantId参数
数据库变更:
- 新增账单相关表及关系
- 支持Snowflake ID主键
- 完整的审计字段支持
🤖 Generated with Claude Code
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
using MediatR;
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
using TakeoutSaaS.Application.App.Billings.Dto;
|
||||
using TakeoutSaaS.Application.App.Billings.Queries;
|
||||
using TakeoutSaaS.Shared.Abstractions.Constants;
|
||||
using TakeoutSaaS.Shared.Abstractions.Data;
|
||||
using TakeoutSaaS.Shared.Abstractions.Exceptions;
|
||||
using TakeoutSaaS.Domain.Tenants.Enums;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Billings.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// 查询账单支付记录处理器。
|
||||
/// </summary>
|
||||
public sealed class GetBillingPaymentsQueryHandler(
|
||||
IDapperExecutor dapperExecutor)
|
||||
: IRequestHandler<GetBillingPaymentsQuery, List<PaymentRecordDto>>
|
||||
{
|
||||
/// <summary>
|
||||
/// 处理查询账单支付记录请求。
|
||||
/// </summary>
|
||||
/// <param name="request">查询命令。</param>
|
||||
/// <param name="cancellationToken">取消标记。</param>
|
||||
/// <returns>支付记录列表 DTO。</returns>
|
||||
public async Task<List<PaymentRecordDto>> Handle(GetBillingPaymentsQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
// 1. 校验账单是否存在
|
||||
return await dapperExecutor.QueryAsync(
|
||||
DatabaseConstants.AppDataSource,
|
||||
DatabaseConnectionRole.Read,
|
||||
async (connection, token) =>
|
||||
{
|
||||
// 1.1 校验账单存在
|
||||
var exists = await ExecuteScalarIntAsync(
|
||||
connection,
|
||||
"""
|
||||
select 1
|
||||
from public.tenant_billing_statements b
|
||||
where b."DeletedAt" is null
|
||||
and b."Id" = @billingId
|
||||
limit 1;
|
||||
""",
|
||||
[
|
||||
("billingId", request.BillingId)
|
||||
],
|
||||
token);
|
||||
|
||||
if (exists == 0)
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.NotFound, "账单不存在");
|
||||
}
|
||||
|
||||
// 1.2 (空行后) 查询支付记录
|
||||
await using var command = CreateCommand(
|
||||
connection,
|
||||
"""
|
||||
select
|
||||
p."Id",
|
||||
p."TenantId",
|
||||
p."BillingStatementId",
|
||||
p."Amount",
|
||||
p."Method",
|
||||
p."Status",
|
||||
p."TransactionNo",
|
||||
p."ProofUrl",
|
||||
p."Notes",
|
||||
p."VerifiedBy",
|
||||
p."VerifiedAt",
|
||||
p."RefundReason",
|
||||
p."RefundedAt",
|
||||
p."PaidAt",
|
||||
p."CreatedAt"
|
||||
from public.tenant_payments p
|
||||
where p."DeletedAt" is null
|
||||
and p."BillingStatementId" = @billingId
|
||||
order by p."CreatedAt" desc;
|
||||
""",
|
||||
[
|
||||
("billingId", request.BillingId)
|
||||
]);
|
||||
|
||||
await using var reader = await command.ExecuteReaderAsync(token);
|
||||
var results = new List<PaymentRecordDto>();
|
||||
while (await reader.ReadAsync(token))
|
||||
{
|
||||
results.Add(new PaymentRecordDto
|
||||
{
|
||||
Id = reader.GetInt64(0),
|
||||
TenantId = reader.GetInt64(1),
|
||||
BillingId = reader.GetInt64(2),
|
||||
Amount = reader.GetDecimal(3),
|
||||
Method = (TenantPaymentMethod)reader.GetInt32(4),
|
||||
Status = (TenantPaymentStatus)reader.GetInt32(5),
|
||||
TransactionNo = reader.IsDBNull(6) ? null : reader.GetString(6),
|
||||
ProofUrl = reader.IsDBNull(7) ? null : reader.GetString(7),
|
||||
Notes = reader.IsDBNull(8) ? null : reader.GetString(8),
|
||||
VerifiedBy = reader.IsDBNull(9) ? null : reader.GetInt64(9),
|
||||
VerifiedAt = reader.IsDBNull(10) ? null : reader.GetDateTime(10),
|
||||
RefundReason = reader.IsDBNull(11) ? null : reader.GetString(11),
|
||||
RefundedAt = reader.IsDBNull(12) ? null : reader.GetDateTime(12),
|
||||
PaidAt = reader.IsDBNull(13) ? null : reader.GetDateTime(13),
|
||||
IsVerified = !reader.IsDBNull(10),
|
||||
CreatedAt = reader.GetDateTime(14)
|
||||
});
|
||||
}
|
||||
|
||||
return results;
|
||||
},
|
||||
cancellationToken);
|
||||
}
|
||||
|
||||
private static async Task<int> ExecuteScalarIntAsync(
|
||||
IDbConnection connection,
|
||||
string sql,
|
||||
(string Name, object? Value)[] parameters,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
await using var command = CreateCommand(connection, sql, parameters);
|
||||
var result = await command.ExecuteScalarAsync(cancellationToken);
|
||||
return result is null or DBNull ? 0 : Convert.ToInt32(result);
|
||||
}
|
||||
|
||||
private static DbCommand CreateCommand(IDbConnection connection, string sql, (string Name, object? Value)[] parameters)
|
||||
{
|
||||
var command = connection.CreateCommand();
|
||||
command.CommandText = sql;
|
||||
|
||||
foreach (var (name, value) in parameters)
|
||||
{
|
||||
var p = command.CreateParameter();
|
||||
p.ParameterName = name;
|
||||
p.Value = value ?? DBNull.Value;
|
||||
command.Parameters.Add(p);
|
||||
}
|
||||
|
||||
return (DbCommand)command;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user