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; /// /// 客户余额变动与流水写入。 /// 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); } /// /// 运营人工调整余额(正数入账、负数扣减),写入账户流水。 /// 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 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("账户流水号生成失败"); } }