fix: 运单列表 enrichment 改为顺序查询,避免 MySqlConnection 并发复用
All checks were successful
Build and Deploy / deploy (push) Successful in 1m12s

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
zzh 2026-10-24 11:31:33 +08:00
parent d22fe7220d
commit ccb9c055a5

View File

@ -298,7 +298,7 @@ public partial class OrderService
}
/// <summary>
/// 列表页专用映射:不加载申报明细/费用明细,并行 enrichment,减小 JSON 与 DB 开销。
/// 列表页专用映射:不加载申报明细/费用明细,批量 enrichment,减小 JSON 与 DB 开销。
/// </summary>
private async Task<List<OrderOutput>> MapOrderListOutputsAsync(IReadOnlyCollection<Order> orders)
{
@ -335,58 +335,32 @@ public partial class OrderService
.Distinct()
.ToArray();
var sendersTask = OrderRepository.GetSendersByOrderIdsAsync(orderIds);
var recipientsTask = OrderRepository.GetRecipientsByOrderIdsAsync(orderIds);
var weightsTask = OrderRepository.GetWeightsByOrderIdsAsync(orderIds);
var receivableSumTask = OrderRepository.GetReceivableSumByOrderIdsAsync(orderIds);
var warehousesTask = warehouseIds.Length == 0
? Task.FromResult(new List<WarehouseAddress>())
: WarehouseAddressRepository.GetListAsync(x => warehouseIds.Contains(x.Id));
var productsTask = productCodes.Length == 0
? Task.FromResult(new List<SalesProduct>())
: SalesProductRepository.GetListAsync(x => productCodes.Contains(x.ProductCode));
var channelsTask = channelCodes.Length == 0
? Task.FromResult(new List<ServiceChannel>())
: ServiceChannelRepository.GetListAsync(x => channelCodes.Contains(x.ChannelCode));
var countriesTask = countryCodes.Length == 0
? Task.FromResult(new List<Country>())
: CountryRepository.GetListAsync(x => countryCodes.Contains(x.CountryCode));
var auditUserNameTask = UserDisplayNameResolver.ResolveManyAsync(
// SqlSugarScope 共用同一 MySqlConnection,不能 Task.WhenAll 并发查库(会报 already in use)
var senders = await OrderRepository.GetSendersByOrderIdsAsync(orderIds);
var recipients = await OrderRepository.GetRecipientsByOrderIdsAsync(orderIds);
var weights = await OrderRepository.GetWeightsByOrderIdsAsync(orderIds);
var receivableSumMap = await OrderRepository.GetReceivableSumByOrderIdsAsync(orderIds);
var warehouses = warehouseIds.Length == 0
? []
: await WarehouseAddressRepository.GetListAsync(x => warehouseIds.Contains(x.Id));
var products = productCodes.Length == 0
? []
: await SalesProductRepository.GetListAsync(x => productCodes.Contains(x.ProductCode));
var channels = channelCodes.Length == 0
? []
: await ServiceChannelRepository.GetListAsync(x => channelCodes.Contains(x.ChannelCode));
var countries = countryCodes.Length == 0
? []
: await CountryRepository.GetListAsync(x => countryCodes.Contains(x.CountryCode));
var auditUserNameMap = await UserDisplayNameResolver.ResolveManyAsync(
orders.SelectMany(x => new[] { x.CreateUserId, x.UpdateUserId }));
var customersTask = customerIds.Length == 0
? Task.FromResult(new List<Customer>())
: CustomerRepository.GetListAsync(x => customerIds.Contains(x.Id));
var pickupOrdersTask = pickupOrderIds.Length == 0
? Task.FromResult(new List<PickupOrder>())
: PickupOrderRepository.GetListAsync(x => pickupOrderIds.Contains(x.Id));
var bindSummaryTask = LoadStockBindListSummariesAsync(orderIds);
await Task.WhenAll(
sendersTask,
recipientsTask,
weightsTask,
receivableSumTask,
warehousesTask,
productsTask,
channelsTask,
countriesTask,
auditUserNameTask,
customersTask,
pickupOrdersTask,
bindSummaryTask);
var senders = await sendersTask;
var recipients = await recipientsTask;
var weights = await weightsTask;
var receivableSumMap = await receivableSumTask;
var warehouses = await warehousesTask;
var products = await productsTask;
var channels = await channelsTask;
var countries = await countriesTask;
var auditUserNameMap = await auditUserNameTask;
var customers = await customersTask;
var pickupOrders = await pickupOrdersTask;
var bindSummaryMap = await bindSummaryTask;
var customers = customerIds.Length == 0
? []
: await CustomerRepository.GetListAsync(x => customerIds.Contains(x.Id));
var pickupOrders = pickupOrderIds.Length == 0
? []
: await PickupOrderRepository.GetListAsync(x => pickupOrderIds.Contains(x.Id));
var bindSummaryMap = await LoadStockBindListSummariesAsync(orderIds);
var senderMap = senders.GroupBy(x => x.OrderId).ToDictionary(x => x.Key, x => x.First());
var recipientMap = recipients.GroupBy(x => x.OrderId).ToDictionary(x => x.Key, x => x.First());