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; /// /// 客户账户流水 API。 /// [ApiExplorerSettings(GroupName = "OMS-Customer")] public class CustomerAccountFlowService : ApplicationService { private ICustomerAccountFlowRepository FlowRepository => LazyServiceProvider.GetRequiredService(); private ICustomerRepository CustomerRepository => LazyServiceProvider.GetRequiredService(); /// /// 分页查询客户账户流水。 /// [HttpGet("/customer/account-flow/list")] public async Task> 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 { Items = items, Total = total }; } private static Expression> 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 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 { [nameof(CustomerAccountFlow.Amount)] = asc, [nameof(CustomerAccountFlow.Id)] = false }; } return new Dictionary { [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 }; } }