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