admin/src/Oms.Application/Services/Customers/CustomerAccountFlowService.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

153 lines
6.0 KiB
C#

using System.Linq.Expressions;
using Fake.Application;
using Microsoft.AspNetCore.Mvc;
using Oms.Application.Dtos.Customers;
using Oms.Application.Dtos.Shared;
using Oms.Application.Shared;
using Oms.Domain.CustomersAggregate.Consts;
using Oms.Domain.CustomersAggregate.Entities;
using Oms.Domain.CustomersAggregate.Repositories;
namespace Oms.Application.Services.Customers;
/// <summary>
/// 客户账户流水 API。
/// </summary>
[ApiExplorerSettings(GroupName = "OMS-Customer")]
public class CustomerAccountFlowService : ApplicationService
{
private ICustomerAccountFlowRepository FlowRepository =>
LazyServiceProvider.GetRequiredService<ICustomerAccountFlowRepository>();
private ICustomerRepository CustomerRepository =>
LazyServiceProvider.GetRequiredService<ICustomerRepository>();
/// <summary>
/// 分页查询客户账户流水。
/// </summary>
[HttpGet("/customer/account-flow/list")]
public async Task<OmsPageResult<CustomerAccountFlowOutput>> GetPaginatedListAsync(
[FromQuery] CustomerAccountFlowPaginatedInput input)
{
var customerId = await OmsSessionHelper.ResolveListCustomerIdAsync(
CustomerRepository,
CurrentUser,
input.CustomerId);
var predicate = BuildPaginatedPredicate(input, customerId);
var pageIndex = OmsDocHelper.NormalizePage(input.Page);
var pageSize = OmsDocHelper.NormalizePageSize(input.PageSize);
var entities = await FlowRepository.GetPagedListAsync(
predicate,
pageIndex,
pageSize,
BuildAccountFlowSortDictionary(input));
var total = await FlowRepository.CountAsync(predicate);
var customerIds = entities.Select(x => x.CustomerId).Distinct().ToArray();
var customers = customerIds.Length == 0
? []
: await CustomerRepository.GetListAsync(x => customerIds.Contains(x.Id));
var customerMap = customers.ToDictionary(x => x.Id);
var items = entities.Select(entity =>
{
customerMap.TryGetValue(entity.CustomerId, out var customer);
return new CustomerAccountFlowOutput
{
Id = entity.Id,
CustomerId = entity.CustomerId,
CustomerCode = customer?.CustomerCode,
SerialNumber = entity.SerialNumber,
BizType = entity.BizType,
BizTypeName = GetBizTypeName(entity.BizType),
BizId = entity.BizId,
BizNo = entity.BizNo,
Amount = entity.Amount,
BalanceBefore = entity.BalanceBefore,
BalanceAfter = entity.BalanceAfter,
Currency = entity.Currency,
Remark = entity.Remark,
CreateTime = entity.CreateTime
};
}).ToList();
return new OmsPageResult<CustomerAccountFlowOutput>
{
Items = items,
Total = total
};
}
private static Expression<Func<CustomerAccountFlow, bool>> BuildPaginatedPredicate(
CustomerAccountFlowPaginatedInput input,
int? customerId)
{
var keyword = input.Keyword?.Trim();
var bizType = input.BizType?.Trim();
var hasKeyword = !string.IsNullOrWhiteSpace(keyword);
var hasBizType = !string.IsNullOrWhiteSpace(bizType)
&& !string.Equals(bizType, "all", StringComparison.OrdinalIgnoreCase);
var hasCustomerFilter = customerId.HasValue;
var customerIdValue = customerId.GetValueOrDefault();
var hasStart = input.StartTime.HasValue;
var hasEnd = input.EndTime.HasValue;
var start = input.StartTime.GetValueOrDefault();
var end = input.EndTime.GetValueOrDefault();
var hasAmountMin = input.AmountMin.HasValue;
var hasAmountMax = input.AmountMax.HasValue;
var amountMin = input.AmountMin.GetValueOrDefault();
var amountMax = input.AmountMax.GetValueOrDefault();
return flow =>
(!hasCustomerFilter || flow.CustomerId == customerIdValue) &&
(!hasBizType || flow.BizType == bizType) &&
(!hasKeyword ||
flow.SerialNumber.Contains(keyword!) ||
(flow.BizNo ?? string.Empty).Contains(keyword!)) &&
(!hasStart || flow.CreateTime >= start) &&
(!hasEnd || flow.CreateTime <= end) &&
(!hasAmountMin || flow.Amount >= amountMin) &&
(!hasAmountMax || flow.Amount <= amountMax);
}
private static Dictionary<string, bool> BuildAccountFlowSortDictionary(
CustomerAccountFlowPaginatedInput input)
{
var field = input.SortField?.Trim();
var asc = string.Equals(input.SortOrder, "asc", StringComparison.OrdinalIgnoreCase);
if (string.Equals(field, "amount", StringComparison.OrdinalIgnoreCase))
{
return new Dictionary<string, bool>
{
[nameof(CustomerAccountFlow.Amount)] = asc,
[nameof(CustomerAccountFlow.Id)] = false
};
}
return new Dictionary<string, bool>
{
[nameof(CustomerAccountFlow.CreateTime)] = string.Equals(field, "createTime", StringComparison.OrdinalIgnoreCase)
? asc
: false,
[nameof(CustomerAccountFlow.Id)] = false
};
}
private static string GetBizTypeName(string bizType)
{
return bizType switch
{
CustomerAccountFlowBizTypes.Recharge => "充值入账",
CustomerAccountFlowBizTypes.OrderPay => "订单支付",
CustomerAccountFlowBizTypes.PickupPay => "揽收支付",
CustomerAccountFlowBizTypes.ReferralCommission => "推荐佣金",
CustomerAccountFlowBizTypes.CommissionToBalance => "佣金转入余额",
CustomerAccountFlowBizTypes.CommissionWithdraw => "佣金提现",
CustomerAccountFlowBizTypes.MarketingAdjust => "营销调整",
CustomerAccountFlowBizTypes.ManualAdjust => "人工调整",
_ => bizType
};
}
}