admin/src/Oms.Application/Shared/CustomerBalanceMutator.cs
zzh 8a4a0787cd
All checks were successful
Build and Deploy / deploy (push) Successful in 1m17s
feat: 客户余额人工调整写入账户流水
新增 /customer/adjust-balance;更新接口忽略直接改余额,避免无痕调整。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-11 17:34:45 +08:00

224 lines
6.0 KiB
C#

using Fake.DependencyInjection;
using Fake.Application;
using Oms.Domain.CustomersAggregate.Consts;
using Oms.Domain.CustomersAggregate.Entities;
using Oms.Domain.CustomersAggregate.Repositories;
namespace Oms.Application.Shared;
/// <summary>
/// 客户余额变动与流水写入。
/// </summary>
public class CustomerBalanceMutator : IScopedDependency
{
private readonly ICustomerRepository _customerRepository;
private readonly ICustomerAccountFlowRepository _flowRepository;
public CustomerBalanceMutator(
ICustomerRepository customerRepository,
ICustomerAccountFlowRepository flowRepository)
{
_customerRepository = customerRepository;
_flowRepository = flowRepository;
}
public Task CreditRechargeAsync(
Customer customer,
decimal amount,
int rechargeRecordId,
string rechargeSerialNumber,
string? remark,
CancellationToken cancellationToken = default)
{
return ApplyAsync(
customer,
amount,
CustomerAccountFlowBizTypes.Recharge,
rechargeRecordId,
rechargeSerialNumber,
remark,
cancellationToken);
}
public Task DebitOrderPayAsync(
Customer customer,
decimal amount,
int orderId,
string serviceNo,
string? remark,
CancellationToken cancellationToken = default)
{
if (amount <= 0)
{
throw new BusinessException("支付金额必须大于 0");
}
return ApplyAsync(
customer,
-amount,
CustomerAccountFlowBizTypes.OrderPay,
orderId,
serviceNo,
remark,
cancellationToken);
}
public Task CreditReferralRewardAsync(
Customer customer,
decimal amount,
int detailId,
string? orderNo,
string? remark,
CancellationToken cancellationToken = default)
{
if (amount <= 0)
{
throw new BusinessException("奖励金额必须大于 0");
}
return ApplyAsync(
customer,
amount,
CustomerAccountFlowBizTypes.ReferralCommission,
detailId,
orderNo,
remark,
cancellationToken);
}
public Task DebitReferralClawbackAsync(
Customer customer,
decimal amount,
int detailId,
string? orderNo,
string? remark,
CancellationToken cancellationToken = default)
{
if (amount <= 0)
{
return Task.CompletedTask;
}
var debit = Math.Min(amount, customer.Balance);
if (debit <= 0)
{
return Task.CompletedTask;
}
return ApplyAsync(
customer,
-debit,
CustomerAccountFlowBizTypes.ReferralCommission,
detailId,
orderNo,
remark,
cancellationToken);
}
public Task DebitPickupPayAsync(
Customer customer,
decimal amount,
int pickupOrderId,
string pickupNo,
string? remark,
CancellationToken cancellationToken = default)
{
if (amount <= 0)
{
throw new BusinessException("支付金额必须大于 0");
}
return ApplyAsync(
customer,
-amount,
CustomerAccountFlowBizTypes.PickupPay,
pickupOrderId,
pickupNo,
remark,
cancellationToken);
}
/// <summary>
/// 运营人工调整余额(正数入账、负数扣减),写入账户流水。
/// </summary>
public Task AdjustManualAsync(
Customer customer,
decimal amount,
string remark,
CancellationToken cancellationToken = default)
{
if (amount == 0)
{
throw new BusinessException("调整金额不能为 0");
}
if (string.IsNullOrWhiteSpace(remark))
{
throw new BusinessException("调整原因不能为空");
}
return ApplyAsync(
customer,
amount,
CustomerAccountFlowBizTypes.ManualAdjust,
null,
null,
remark.Trim(),
cancellationToken);
}
private async Task ApplyAsync(
Customer customer,
decimal signedAmount,
string bizType,
int? bizId,
string? bizNo,
string? remark,
CancellationToken cancellationToken = default)
{
if (signedAmount == 0)
{
throw new BusinessException("流水金额不能为 0");
}
var balanceBefore = customer.Balance;
var balanceAfter = balanceBefore + signedAmount;
if (balanceAfter < 0)
{
throw new BusinessException("账户余额不足");
}
customer.Balance = balanceAfter;
await _customerRepository.UpdateAsync(customer, cancellationToken);
var flow = new CustomerAccountFlow
{
CustomerId = customer.Id,
SerialNumber = await GenerateSerialNumberAsync(cancellationToken),
BizType = bizType,
BizId = bizId,
BizNo = bizNo,
Amount = signedAmount,
BalanceBefore = balanceBefore,
BalanceAfter = balanceAfter,
Currency = "CNY",
Remark = remark
};
await _flowRepository.InsertAsync(flow, cancellationToken);
}
private async Task<string> GenerateSerialNumberAsync(CancellationToken cancellationToken)
{
for (var i = 0; i < 5; i++)
{
var serialNumber = $"FLOW{DateTime.Now:yyyyMMddHHmmss}{Random.Shared.Next(100000, 999999)}";
if (!await _flowRepository.ExistsSerialNumberAsync(serialNumber, cancellationToken))
{
return serialNumber;
}
}
throw new BusinessException("账户流水号生成失败");
}
}