fix: 修复规格模板查询并发DbContext异常
All checks were successful
Build and Deploy TenantApi / build-and-deploy (push) Successful in 44s

This commit is contained in:
2026-02-21 07:42:02 +08:00
parent 392d9f03a1
commit 848778b8b5
2 changed files with 20 additions and 11 deletions

View File

@@ -61,12 +61,15 @@ public sealed class CopyProductSpecTemplateCommandHandler(
await productRepository.SaveChangesAsync(cancellationToken);
// 4. 复制选项与关联商品。
var sourceOptionsTask = productRepository.GetSpecTemplateOptionsByTemplateIdsAsync([source.Id], tenantId, cancellationToken);
var sourceRelationsTask = productRepository.GetSpecTemplateProductsByTemplateIdsAsync([source.Id], tenantId, request.StoreId, cancellationToken);
await Task.WhenAll(sourceOptionsTask, sourceRelationsTask);
var sourceOptions = await sourceOptionsTask;
var sourceRelations = await sourceRelationsTask;
var sourceOptions = await productRepository.GetSpecTemplateOptionsByTemplateIdsAsync(
[source.Id],
tenantId,
cancellationToken);
var sourceRelations = await productRepository.GetSpecTemplateProductsByTemplateIdsAsync(
[source.Id],
tenantId,
request.StoreId,
cancellationToken);
if (sourceOptions.Count > 0)
{

View File

@@ -55,15 +55,21 @@ public sealed class GetProductSpecTemplateListQueryHandler(
}
var templateIds = filteredList.Select(item => item.Id).ToList();
var optionsTask = productRepository.GetSpecTemplateOptionsByTemplateIdsAsync(templateIds, tenantId, cancellationToken);
var relationsTask = productRepository.GetSpecTemplateProductsByTemplateIdsAsync(templateIds, tenantId, request.StoreId, cancellationToken);
await Task.WhenAll(optionsTask, relationsTask);
var options = await productRepository.GetSpecTemplateOptionsByTemplateIdsAsync(
templateIds,
tenantId,
cancellationToken);
var relations = await productRepository.GetSpecTemplateProductsByTemplateIdsAsync(
templateIds,
tenantId,
request.StoreId,
cancellationToken);
// 4. 构建查找字典并映射 DTO。
var optionsLookup = (await optionsTask)
var optionsLookup = options
.GroupBy(x => x.TemplateId)
.ToDictionary(group => group.Key, group => group.OrderBy(item => item.SortOrder).ThenBy(item => item.Id).ToList());
var productIdsLookup = (await relationsTask)
var productIdsLookup = relations
.GroupBy(x => x.TemplateId)
.ToDictionary(group => group.Key, group => group.Select(item => item.ProductId).ToList());