feat: 新增规格做法模板管理接口与数据模型
All checks were successful
Build and Deploy TenantApi / build-and-deploy (push) Successful in 45s
All checks were successful
Build and Deploy TenantApi / build-and-deploy (push) Successful in 45s
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
using MediatR;
|
||||
using TakeoutSaaS.Application.App.Products.Dto;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Products.Commands;
|
||||
|
||||
/// <summary>
|
||||
/// 修改规格做法模板状态命令。
|
||||
/// </summary>
|
||||
public sealed class ChangeProductSpecTemplateStatusCommand : IRequest<ProductSpecTemplateItemDto>
|
||||
{
|
||||
/// <summary>
|
||||
/// 门店 ID。
|
||||
/// </summary>
|
||||
public long StoreId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 模板 ID。
|
||||
/// </summary>
|
||||
public long SpecId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 状态(enabled/disabled)。
|
||||
/// </summary>
|
||||
public string Status { get; init; } = "enabled";
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using MediatR;
|
||||
using TakeoutSaaS.Application.App.Products.Dto;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Products.Commands;
|
||||
|
||||
/// <summary>
|
||||
/// 复制规格做法模板命令。
|
||||
/// </summary>
|
||||
public sealed class CopyProductSpecTemplateCommand : IRequest<ProductSpecTemplateItemDto>
|
||||
{
|
||||
/// <summary>
|
||||
/// 门店 ID。
|
||||
/// </summary>
|
||||
public long StoreId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 源模板 ID。
|
||||
/// </summary>
|
||||
public long SpecId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 目标模板名称(可选)。
|
||||
/// </summary>
|
||||
public string? NewName { get; init; }
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using MediatR;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Products.Commands;
|
||||
|
||||
/// <summary>
|
||||
/// 删除规格做法模板命令。
|
||||
/// </summary>
|
||||
public sealed class DeleteProductSpecTemplateCommand : IRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// 门店 ID。
|
||||
/// </summary>
|
||||
public long StoreId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 模板 ID。
|
||||
/// </summary>
|
||||
public long SpecId { get; init; }
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
using MediatR;
|
||||
using TakeoutSaaS.Application.App.Products.Dto;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Products.Commands;
|
||||
|
||||
/// <summary>
|
||||
/// 保存规格做法模板命令。
|
||||
/// </summary>
|
||||
public sealed class SaveProductSpecTemplateCommand : IRequest<ProductSpecTemplateItemDto>
|
||||
{
|
||||
/// <summary>
|
||||
/// 门店 ID。
|
||||
/// </summary>
|
||||
public long StoreId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 模板 ID(编辑时传)。
|
||||
/// </summary>
|
||||
public long? SpecId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 模板名称。
|
||||
/// </summary>
|
||||
public string Name { get; init; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 模板类型(spec/method)。
|
||||
/// </summary>
|
||||
public string Type { get; init; } = "spec";
|
||||
|
||||
/// <summary>
|
||||
/// 选择方式(single/multi)。
|
||||
/// </summary>
|
||||
public string SelectionType { get; init; } = "single";
|
||||
|
||||
/// <summary>
|
||||
/// 是否必选。
|
||||
/// </summary>
|
||||
public bool IsRequired { get; init; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// 排序值。
|
||||
/// </summary>
|
||||
public int Sort { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 状态(enabled/disabled)。
|
||||
/// </summary>
|
||||
public string Status { get; init; } = "enabled";
|
||||
|
||||
/// <summary>
|
||||
/// 关联商品 ID 列表。
|
||||
/// </summary>
|
||||
public IReadOnlyList<long> ProductIds { get; init; } = [];
|
||||
|
||||
/// <summary>
|
||||
/// 模板选项。
|
||||
/// </summary>
|
||||
public IReadOnlyList<SaveProductSpecTemplateValueCommand> Values { get; init; } = [];
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
namespace TakeoutSaaS.Application.App.Products.Commands;
|
||||
|
||||
/// <summary>
|
||||
/// 保存规格做法模板选项命令项。
|
||||
/// </summary>
|
||||
public sealed class SaveProductSpecTemplateValueCommand
|
||||
{
|
||||
/// <summary>
|
||||
/// 选项 ID(编辑时传)。
|
||||
/// </summary>
|
||||
public long? Id { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 选项名称。
|
||||
/// </summary>
|
||||
public string Name { get; init; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 附加价格。
|
||||
/// </summary>
|
||||
public decimal ExtraPrice { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 排序值。
|
||||
/// </summary>
|
||||
public int Sort { get; init; }
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
namespace TakeoutSaaS.Application.App.Products.Dto;
|
||||
|
||||
/// <summary>
|
||||
/// 规格做法模板列表项 DTO。
|
||||
/// </summary>
|
||||
public sealed class ProductSpecTemplateItemDto
|
||||
{
|
||||
/// <summary>
|
||||
/// 模板 ID。
|
||||
/// </summary>
|
||||
public long Id { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 模板名称。
|
||||
/// </summary>
|
||||
public string Name { get; init; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 模板类型(spec/method)。
|
||||
/// </summary>
|
||||
public string Type { get; init; } = "spec";
|
||||
|
||||
/// <summary>
|
||||
/// 选择方式(single/multi)。
|
||||
/// </summary>
|
||||
public string SelectionType { get; init; } = "single";
|
||||
|
||||
/// <summary>
|
||||
/// 是否必选。
|
||||
/// </summary>
|
||||
public bool IsRequired { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 排序值。
|
||||
/// </summary>
|
||||
public int Sort { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 状态(enabled/disabled)。
|
||||
/// </summary>
|
||||
public string Status { get; init; } = "enabled";
|
||||
|
||||
/// <summary>
|
||||
/// 关联商品数量。
|
||||
/// </summary>
|
||||
public int ProductCount { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 关联商品 ID 列表。
|
||||
/// </summary>
|
||||
public IReadOnlyList<long> ProductIds { get; init; } = [];
|
||||
|
||||
/// <summary>
|
||||
/// 模板选项。
|
||||
/// </summary>
|
||||
public IReadOnlyList<ProductSpecTemplateOptionDto> Values { get; init; } = [];
|
||||
|
||||
/// <summary>
|
||||
/// 更新时间。
|
||||
/// </summary>
|
||||
public DateTime UpdatedAt { get; init; }
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
namespace TakeoutSaaS.Application.App.Products.Dto;
|
||||
|
||||
/// <summary>
|
||||
/// 规格做法模板选项 DTO。
|
||||
/// </summary>
|
||||
public sealed class ProductSpecTemplateOptionDto
|
||||
{
|
||||
/// <summary>
|
||||
/// 选项 ID。
|
||||
/// </summary>
|
||||
public long Id { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 选项名称。
|
||||
/// </summary>
|
||||
public string Name { get; init; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 附加价格。
|
||||
/// </summary>
|
||||
public decimal ExtraPrice { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 排序值。
|
||||
/// </summary>
|
||||
public int Sort { get; init; }
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
using MediatR;
|
||||
using TakeoutSaaS.Application.App.Products.Commands;
|
||||
using TakeoutSaaS.Application.App.Products.Dto;
|
||||
using TakeoutSaaS.Domain.Products.Repositories;
|
||||
using TakeoutSaaS.Shared.Abstractions.Constants;
|
||||
using TakeoutSaaS.Shared.Abstractions.Exceptions;
|
||||
using TakeoutSaaS.Shared.Abstractions.Tenancy;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Products.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// 修改规格做法模板状态命令处理器。
|
||||
/// </summary>
|
||||
public sealed class ChangeProductSpecTemplateStatusCommandHandler(
|
||||
IProductRepository productRepository,
|
||||
ITenantProvider tenantProvider)
|
||||
: IRequestHandler<ChangeProductSpecTemplateStatusCommand, ProductSpecTemplateItemDto>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task<ProductSpecTemplateItemDto> Handle(ChangeProductSpecTemplateStatusCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
// 1. 解析状态并校验模板归属。
|
||||
if (!ProductSpecTemplateMapping.TryParseStatus(request.Status, out var isEnabled))
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.BadRequest, "status 参数不合法");
|
||||
}
|
||||
|
||||
var tenantId = tenantProvider.GetCurrentTenantId();
|
||||
var existing = await productRepository.FindSpecTemplateByIdAsync(request.SpecId, tenantId, cancellationToken);
|
||||
if (existing is null || existing.StoreId != request.StoreId)
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.NotFound, "模板不存在");
|
||||
}
|
||||
|
||||
// 2. 保存状态变更。
|
||||
existing.IsEnabled = isEnabled;
|
||||
await productRepository.UpdateSpecTemplateAsync(existing, cancellationToken);
|
||||
await productRepository.SaveChangesAsync(cancellationToken);
|
||||
|
||||
// 3. 返回变更后的完整模板。
|
||||
var options = await productRepository.GetSpecTemplateOptionsByTemplateIdsAsync([existing.Id], tenantId, cancellationToken);
|
||||
var relations = await productRepository.GetSpecTemplateProductsByTemplateIdsAsync([existing.Id], tenantId, request.StoreId, cancellationToken);
|
||||
var optionsLookup = options
|
||||
.GroupBy(x => x.TemplateId)
|
||||
.ToDictionary(group => group.Key, group => group.OrderBy(item => item.SortOrder).ThenBy(item => item.Id).ToList());
|
||||
var productIdsLookup = relations
|
||||
.GroupBy(x => x.TemplateId)
|
||||
.ToDictionary(group => group.Key, group => group.Select(item => item.ProductId).ToList());
|
||||
|
||||
return ProductSpecTemplateDtoFactory.ToDto(existing, optionsLookup, productIdsLookup);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
using MediatR;
|
||||
using TakeoutSaaS.Application.App.Products.Commands;
|
||||
using TakeoutSaaS.Application.App.Products.Dto;
|
||||
using TakeoutSaaS.Domain.Products.Entities;
|
||||
using TakeoutSaaS.Domain.Products.Repositories;
|
||||
using TakeoutSaaS.Shared.Abstractions.Constants;
|
||||
using TakeoutSaaS.Shared.Abstractions.Exceptions;
|
||||
using TakeoutSaaS.Shared.Abstractions.Tenancy;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Products.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// 复制规格做法模板命令处理器。
|
||||
/// </summary>
|
||||
public sealed class CopyProductSpecTemplateCommandHandler(
|
||||
IProductRepository productRepository,
|
||||
ITenantProvider tenantProvider)
|
||||
: IRequestHandler<CopyProductSpecTemplateCommand, ProductSpecTemplateItemDto>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task<ProductSpecTemplateItemDto> Handle(CopyProductSpecTemplateCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
// 1. 读取源模板并校验门店归属。
|
||||
var tenantId = tenantProvider.GetCurrentTenantId();
|
||||
var source = await productRepository.FindSpecTemplateByIdAsync(request.SpecId, tenantId, cancellationToken);
|
||||
if (source is null || source.StoreId != request.StoreId)
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.NotFound, "模板不存在");
|
||||
}
|
||||
|
||||
// 2. 生成不冲突的新模板名称。
|
||||
var isDefaultCopyName = string.IsNullOrWhiteSpace(request.NewName);
|
||||
var baseName = isDefaultCopyName
|
||||
? $"{source.Name}(副本)"
|
||||
: request.NewName!.Trim();
|
||||
if (string.IsNullOrWhiteSpace(baseName))
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.BadRequest, "模板名称不能为空");
|
||||
}
|
||||
|
||||
var targetName = await BuildAvailableNameAsync(
|
||||
tenantId,
|
||||
request.StoreId,
|
||||
baseName,
|
||||
source.Name,
|
||||
isDefaultCopyName,
|
||||
cancellationToken);
|
||||
|
||||
// 3. 创建模板主记录并落库获取 ID。
|
||||
var copiedTemplate = new ProductSpecTemplate
|
||||
{
|
||||
StoreId = request.StoreId,
|
||||
Name = targetName,
|
||||
TemplateType = source.TemplateType,
|
||||
SelectionType = source.SelectionType,
|
||||
IsRequired = source.IsRequired,
|
||||
SortOrder = source.SortOrder,
|
||||
IsEnabled = source.IsEnabled
|
||||
};
|
||||
await productRepository.AddSpecTemplateAsync(copiedTemplate, cancellationToken);
|
||||
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;
|
||||
|
||||
if (sourceOptions.Count > 0)
|
||||
{
|
||||
var copiedOptions = sourceOptions
|
||||
.Select(option => new ProductSpecTemplateOption
|
||||
{
|
||||
TemplateId = copiedTemplate.Id,
|
||||
Name = option.Name,
|
||||
ExtraPrice = option.ExtraPrice,
|
||||
SortOrder = option.SortOrder
|
||||
})
|
||||
.ToList();
|
||||
await productRepository.AddSpecTemplateOptionsAsync(copiedOptions, cancellationToken);
|
||||
}
|
||||
|
||||
if (sourceRelations.Count > 0)
|
||||
{
|
||||
var copiedRelations = sourceRelations
|
||||
.Select(relation => relation.ProductId)
|
||||
.Distinct()
|
||||
.Select(productId => new ProductSpecTemplateProduct
|
||||
{
|
||||
StoreId = request.StoreId,
|
||||
TemplateId = copiedTemplate.Id,
|
||||
ProductId = productId
|
||||
})
|
||||
.ToList();
|
||||
await productRepository.AddSpecTemplateProductsAsync(copiedRelations, cancellationToken);
|
||||
}
|
||||
|
||||
await productRepository.SaveChangesAsync(cancellationToken);
|
||||
|
||||
// 5. 返回复制后的模板快照。
|
||||
var options = await productRepository.GetSpecTemplateOptionsByTemplateIdsAsync([copiedTemplate.Id], tenantId, cancellationToken);
|
||||
var relations = await productRepository.GetSpecTemplateProductsByTemplateIdsAsync([copiedTemplate.Id], tenantId, request.StoreId, cancellationToken);
|
||||
var optionsLookup = options
|
||||
.GroupBy(x => x.TemplateId)
|
||||
.ToDictionary(group => group.Key, group => group.OrderBy(item => item.SortOrder).ThenBy(item => item.Id).ToList());
|
||||
var productIdsLookup = relations
|
||||
.GroupBy(x => x.TemplateId)
|
||||
.ToDictionary(group => group.Key, group => group.Select(item => item.ProductId).ToList());
|
||||
|
||||
return ProductSpecTemplateDtoFactory.ToDto(copiedTemplate, optionsLookup, productIdsLookup);
|
||||
}
|
||||
|
||||
private async Task<string> BuildAvailableNameAsync(
|
||||
long tenantId,
|
||||
long storeId,
|
||||
string baseName,
|
||||
string sourceName,
|
||||
bool isDefaultCopyName,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var candidate = baseName;
|
||||
var conflict = await productRepository.ExistsSpecTemplateNameAsync(tenantId, storeId, candidate, null, cancellationToken);
|
||||
if (!conflict)
|
||||
{
|
||||
return candidate;
|
||||
}
|
||||
|
||||
for (var index = 2; index <= 999; index++)
|
||||
{
|
||||
candidate = isDefaultCopyName
|
||||
? $"{sourceName}(副本{index})"
|
||||
: $"{baseName}({index})";
|
||||
|
||||
conflict = await productRepository.ExistsSpecTemplateNameAsync(tenantId, storeId, candidate, null, cancellationToken);
|
||||
if (!conflict)
|
||||
{
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
|
||||
throw new BusinessException(ErrorCodes.Conflict, "复制失败,模板名称冲突过多");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using MediatR;
|
||||
using TakeoutSaaS.Application.App.Products.Commands;
|
||||
using TakeoutSaaS.Domain.Products.Repositories;
|
||||
using TakeoutSaaS.Shared.Abstractions.Constants;
|
||||
using TakeoutSaaS.Shared.Abstractions.Exceptions;
|
||||
using TakeoutSaaS.Shared.Abstractions.Tenancy;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Products.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// 删除规格做法模板命令处理器。
|
||||
/// </summary>
|
||||
public sealed class DeleteProductSpecTemplateCommandHandler(
|
||||
IProductRepository productRepository,
|
||||
ITenantProvider tenantProvider)
|
||||
: IRequestHandler<DeleteProductSpecTemplateCommand>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task Handle(DeleteProductSpecTemplateCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
// 1. 校验模板是否存在且归属当前门店。
|
||||
var tenantId = tenantProvider.GetCurrentTenantId();
|
||||
var existing = await productRepository.FindSpecTemplateByIdAsync(request.SpecId, tenantId, cancellationToken);
|
||||
if (existing is null || existing.StoreId != request.StoreId)
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.NotFound, "模板不存在");
|
||||
}
|
||||
|
||||
// 2. 删除选项、关联商品与模板主记录。
|
||||
await productRepository.RemoveSpecTemplateOptionsAsync(existing.Id, tenantId, cancellationToken);
|
||||
await productRepository.RemoveSpecTemplateProductsAsync(existing.Id, tenantId, request.StoreId, cancellationToken);
|
||||
await productRepository.DeleteSpecTemplateAsync(existing.Id, tenantId, cancellationToken);
|
||||
await productRepository.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
using MediatR;
|
||||
using TakeoutSaaS.Application.App.Products.Dto;
|
||||
using TakeoutSaaS.Application.App.Products.Queries;
|
||||
using TakeoutSaaS.Domain.Products.Entities;
|
||||
using TakeoutSaaS.Domain.Products.Repositories;
|
||||
using TakeoutSaaS.Shared.Abstractions.Tenancy;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Products.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// 规格做法模板列表查询处理器。
|
||||
/// </summary>
|
||||
public sealed class GetProductSpecTemplateListQueryHandler(
|
||||
IProductRepository productRepository,
|
||||
ITenantProvider tenantProvider)
|
||||
: IRequestHandler<GetProductSpecTemplateListQuery, IReadOnlyList<ProductSpecTemplateItemDto>>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task<IReadOnlyList<ProductSpecTemplateItemDto>> Handle(GetProductSpecTemplateListQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
// 1. 读取门店模板。
|
||||
var tenantId = tenantProvider.GetCurrentTenantId();
|
||||
var templates = await productRepository.GetSpecTemplatesByStoreAsync(tenantId, request.StoreId, cancellationToken);
|
||||
if (templates.Count == 0)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
// 2. 应用状态、类型、关键字筛选。
|
||||
IEnumerable<ProductSpecTemplate> filtered = templates;
|
||||
if (ProductSpecTemplateMapping.TryParseStatus(request.Status, out var parsedStatus))
|
||||
{
|
||||
filtered = filtered.Where(item => item.IsEnabled == parsedStatus);
|
||||
}
|
||||
|
||||
if (ProductSpecTemplateMapping.TryParseTemplateType(request.Type, out var templateType))
|
||||
{
|
||||
filtered = filtered.Where(item => item.TemplateType == templateType);
|
||||
}
|
||||
|
||||
var normalizedKeyword = request.Keyword?.Trim().ToLowerInvariant();
|
||||
if (!string.IsNullOrWhiteSpace(normalizedKeyword))
|
||||
{
|
||||
filtered = filtered.Where(item => item.Name.ToLower().Contains(normalizedKeyword));
|
||||
}
|
||||
|
||||
// 3. 整理结果并批量读取选项与关联商品。
|
||||
var filteredList = filtered
|
||||
.OrderBy(item => item.SortOrder)
|
||||
.ThenBy(item => item.Id)
|
||||
.ToList();
|
||||
if (filteredList.Count == 0)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
// 4. 构建查找字典并映射 DTO。
|
||||
var optionsLookup = (await optionsTask)
|
||||
.GroupBy(x => x.TemplateId)
|
||||
.ToDictionary(group => group.Key, group => group.OrderBy(item => item.SortOrder).ThenBy(item => item.Id).ToList());
|
||||
var productIdsLookup = (await relationsTask)
|
||||
.GroupBy(x => x.TemplateId)
|
||||
.ToDictionary(group => group.Key, group => group.Select(item => item.ProductId).ToList());
|
||||
|
||||
return filteredList
|
||||
.Select(template => ProductSpecTemplateDtoFactory.ToDto(template, optionsLookup, productIdsLookup))
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,197 @@
|
||||
using MediatR;
|
||||
using TakeoutSaaS.Application.App.Products.Commands;
|
||||
using TakeoutSaaS.Application.App.Products.Dto;
|
||||
using TakeoutSaaS.Domain.Products.Entities;
|
||||
using TakeoutSaaS.Domain.Products.Repositories;
|
||||
using TakeoutSaaS.Shared.Abstractions.Constants;
|
||||
using TakeoutSaaS.Shared.Abstractions.Exceptions;
|
||||
using TakeoutSaaS.Shared.Abstractions.Tenancy;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Products.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// 保存规格做法模板命令处理器。
|
||||
/// </summary>
|
||||
public sealed class SaveProductSpecTemplateCommandHandler(
|
||||
IProductRepository productRepository,
|
||||
ITenantProvider tenantProvider)
|
||||
: IRequestHandler<SaveProductSpecTemplateCommand, ProductSpecTemplateItemDto>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task<ProductSpecTemplateItemDto> Handle(SaveProductSpecTemplateCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
// 1. 读取并校验基础输入。
|
||||
var tenantId = tenantProvider.GetCurrentTenantId();
|
||||
var normalizedName = request.Name.Trim();
|
||||
if (string.IsNullOrWhiteSpace(normalizedName))
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.BadRequest, "模板名称不能为空");
|
||||
}
|
||||
|
||||
if (!ProductSpecTemplateMapping.TryParseStatus(request.Status, out var isEnabled))
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.BadRequest, "status 参数不合法");
|
||||
}
|
||||
|
||||
if (!ProductSpecTemplateMapping.TryParseTemplateType(request.Type, out var templateType))
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.BadRequest, "type 参数不合法");
|
||||
}
|
||||
|
||||
if (!ProductSpecTemplateMapping.TryParseSelectionType(request.SelectionType, out var selectionType))
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.BadRequest, "selectionType 参数不合法");
|
||||
}
|
||||
|
||||
var normalizedValues = NormalizeValues(request.Values);
|
||||
if (normalizedValues.Count == 0)
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.BadRequest, "请至少添加一个选项");
|
||||
}
|
||||
|
||||
// 2. 校验同门店模板名称唯一。
|
||||
var isDuplicate = await productRepository.ExistsSpecTemplateNameAsync(
|
||||
tenantId,
|
||||
request.StoreId,
|
||||
normalizedName,
|
||||
request.SpecId,
|
||||
cancellationToken);
|
||||
if (isDuplicate)
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.Conflict, "模板名称已存在");
|
||||
}
|
||||
|
||||
var normalizedProductIds = request.ProductIds
|
||||
.Where(id => id > 0)
|
||||
.Distinct()
|
||||
.ToList();
|
||||
var validProductIds = await productRepository.FilterExistingProductIdsAsync(
|
||||
tenantId,
|
||||
request.StoreId,
|
||||
normalizedProductIds,
|
||||
cancellationToken);
|
||||
|
||||
// 3. 创建或更新模板主记录。
|
||||
ProductSpecTemplate template;
|
||||
var isCreate = !request.SpecId.HasValue;
|
||||
if (isCreate)
|
||||
{
|
||||
template = new ProductSpecTemplate
|
||||
{
|
||||
StoreId = request.StoreId,
|
||||
Name = normalizedName,
|
||||
TemplateType = templateType,
|
||||
SelectionType = selectionType,
|
||||
IsRequired = request.IsRequired,
|
||||
SortOrder = Math.Max(1, request.Sort),
|
||||
IsEnabled = isEnabled
|
||||
};
|
||||
await productRepository.AddSpecTemplateAsync(template, cancellationToken);
|
||||
await productRepository.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
var existing = await productRepository.FindSpecTemplateByIdAsync(
|
||||
request.SpecId!.Value,
|
||||
tenantId,
|
||||
cancellationToken);
|
||||
if (existing is null || existing.StoreId != request.StoreId)
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.NotFound, "模板不存在");
|
||||
}
|
||||
|
||||
template = existing;
|
||||
template.Name = normalizedName;
|
||||
template.TemplateType = templateType;
|
||||
template.SelectionType = selectionType;
|
||||
template.IsRequired = request.IsRequired;
|
||||
template.SortOrder = Math.Max(1, request.Sort);
|
||||
template.IsEnabled = isEnabled;
|
||||
await productRepository.UpdateSpecTemplateAsync(template, cancellationToken);
|
||||
}
|
||||
|
||||
// 4. 替换模板选项与关联商品。
|
||||
await productRepository.RemoveSpecTemplateOptionsAsync(template.Id, tenantId, cancellationToken);
|
||||
await productRepository.RemoveSpecTemplateProductsAsync(template.Id, tenantId, request.StoreId, cancellationToken);
|
||||
|
||||
var optionEntities = normalizedValues
|
||||
.Select(value => new ProductSpecTemplateOption
|
||||
{
|
||||
TemplateId = template.Id,
|
||||
Name = value.Name,
|
||||
ExtraPrice = value.ExtraPrice,
|
||||
SortOrder = value.Sort
|
||||
})
|
||||
.ToList();
|
||||
if (optionEntities.Count > 0)
|
||||
{
|
||||
await productRepository.AddSpecTemplateOptionsAsync(optionEntities, cancellationToken);
|
||||
}
|
||||
|
||||
var relationEntities = validProductIds
|
||||
.Select(productId => new ProductSpecTemplateProduct
|
||||
{
|
||||
StoreId = request.StoreId,
|
||||
TemplateId = template.Id,
|
||||
ProductId = productId
|
||||
})
|
||||
.ToList();
|
||||
if (relationEntities.Count > 0)
|
||||
{
|
||||
await productRepository.AddSpecTemplateProductsAsync(relationEntities, cancellationToken);
|
||||
}
|
||||
|
||||
await productRepository.SaveChangesAsync(cancellationToken);
|
||||
|
||||
// 5. 回读并返回最新快照。
|
||||
var options = await productRepository.GetSpecTemplateOptionsByTemplateIdsAsync([template.Id], tenantId, cancellationToken);
|
||||
var relations = await productRepository.GetSpecTemplateProductsByTemplateIdsAsync([template.Id], tenantId, request.StoreId, cancellationToken);
|
||||
var optionsLookup = options
|
||||
.GroupBy(x => x.TemplateId)
|
||||
.ToDictionary(group => group.Key, group => group.OrderBy(item => item.SortOrder).ThenBy(item => item.Id).ToList());
|
||||
var productIdsLookup = relations
|
||||
.GroupBy(x => x.TemplateId)
|
||||
.ToDictionary(group => group.Key, group => group.Select(item => item.ProductId).ToList());
|
||||
|
||||
return ProductSpecTemplateDtoFactory.ToDto(template, optionsLookup, productIdsLookup);
|
||||
}
|
||||
|
||||
private static IReadOnlyList<NormalizedSpecValue> NormalizeValues(IReadOnlyList<SaveProductSpecTemplateValueCommand>? values)
|
||||
{
|
||||
var source = values ?? [];
|
||||
var normalized = source
|
||||
.Select((item, index) => new NormalizedSpecValue
|
||||
{
|
||||
Name = item.Name.Trim(),
|
||||
ExtraPrice = item.ExtraPrice,
|
||||
Sort = item.Sort > 0 ? item.Sort : index + 1
|
||||
})
|
||||
.Where(item => !string.IsNullOrWhiteSpace(item.Name))
|
||||
.OrderBy(item => item.Sort)
|
||||
.ToList();
|
||||
|
||||
var duplicate = normalized
|
||||
.GroupBy(item => item.Name, StringComparer.OrdinalIgnoreCase)
|
||||
.Any(group => group.Count() > 1);
|
||||
if (duplicate)
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.Conflict, "模板选项名称重复");
|
||||
}
|
||||
|
||||
for (var index = 0; index < normalized.Count; index++)
|
||||
{
|
||||
normalized[index].Sort = index + 1;
|
||||
}
|
||||
|
||||
return normalized;
|
||||
}
|
||||
|
||||
private sealed class NormalizedSpecValue
|
||||
{
|
||||
public decimal ExtraPrice { get; init; }
|
||||
|
||||
public string Name { get; init; } = string.Empty;
|
||||
|
||||
public int Sort { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
using TakeoutSaaS.Application.App.Products.Dto;
|
||||
using TakeoutSaaS.Domain.Products.Entities;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Products;
|
||||
|
||||
/// <summary>
|
||||
/// 规格做法模板 DTO 映射工厂。
|
||||
/// </summary>
|
||||
internal static class ProductSpecTemplateDtoFactory
|
||||
{
|
||||
/// <summary>
|
||||
/// 将模板实体映射为页面 DTO。
|
||||
/// </summary>
|
||||
public static ProductSpecTemplateItemDto ToDto(
|
||||
ProductSpecTemplate template,
|
||||
IReadOnlyDictionary<long, List<ProductSpecTemplateOption>> optionsLookup,
|
||||
IReadOnlyDictionary<long, List<long>> productIdsLookup)
|
||||
{
|
||||
var options = optionsLookup.TryGetValue(template.Id, out var optionList)
|
||||
? optionList
|
||||
: [];
|
||||
|
||||
var productIds = productIdsLookup.TryGetValue(template.Id, out var ids)
|
||||
? ids
|
||||
: [];
|
||||
|
||||
return new ProductSpecTemplateItemDto
|
||||
{
|
||||
Id = template.Id,
|
||||
Name = template.Name,
|
||||
Type = ProductSpecTemplateMapping.ToTemplateTypeText(template.TemplateType),
|
||||
SelectionType = ProductSpecTemplateMapping.ToSelectionTypeText(template.SelectionType),
|
||||
IsRequired = template.IsRequired,
|
||||
Sort = template.SortOrder,
|
||||
Status = ProductSpecTemplateMapping.ToStatusText(template.IsEnabled),
|
||||
ProductCount = productIds.Count,
|
||||
ProductIds = productIds,
|
||||
Values = options
|
||||
.OrderBy(x => x.SortOrder)
|
||||
.ThenBy(x => x.Id)
|
||||
.Select(option => new ProductSpecTemplateOptionDto
|
||||
{
|
||||
Id = option.Id,
|
||||
Name = option.Name,
|
||||
ExtraPrice = option.ExtraPrice,
|
||||
Sort = option.SortOrder
|
||||
})
|
||||
.ToList(),
|
||||
UpdatedAt = template.UpdatedAt ?? template.CreatedAt
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
using TakeoutSaaS.Domain.Products.Enums;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Products;
|
||||
|
||||
/// <summary>
|
||||
/// 规格做法模板映射辅助。
|
||||
/// </summary>
|
||||
internal static class ProductSpecTemplateMapping
|
||||
{
|
||||
/// <summary>
|
||||
/// 解析状态字符串。
|
||||
/// </summary>
|
||||
public static bool TryParseStatus(string? status, out bool isEnabled)
|
||||
{
|
||||
var normalized = NormalizeStatus(status);
|
||||
if (normalized is null)
|
||||
{
|
||||
isEnabled = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
isEnabled = normalized == "enabled";
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 状态转字符串。
|
||||
/// </summary>
|
||||
public static string ToStatusText(bool isEnabled)
|
||||
{
|
||||
return isEnabled ? "enabled" : "disabled";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 解析模板类型。
|
||||
/// </summary>
|
||||
public static bool TryParseTemplateType(string? templateType, out ProductSpecTemplateType parsed)
|
||||
{
|
||||
var normalized = templateType?.Trim().ToLowerInvariant();
|
||||
switch (normalized)
|
||||
{
|
||||
case "spec":
|
||||
parsed = ProductSpecTemplateType.Spec;
|
||||
return true;
|
||||
case "method":
|
||||
parsed = ProductSpecTemplateType.Method;
|
||||
return true;
|
||||
default:
|
||||
parsed = ProductSpecTemplateType.Spec;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 模板类型转字符串。
|
||||
/// </summary>
|
||||
public static string ToTemplateTypeText(ProductSpecTemplateType templateType)
|
||||
{
|
||||
return templateType == ProductSpecTemplateType.Method ? "method" : "spec";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 解析选择类型。
|
||||
/// </summary>
|
||||
public static bool TryParseSelectionType(string? selectionType, out AttributeSelectionType parsed)
|
||||
{
|
||||
var normalized = selectionType?.Trim().ToLowerInvariant();
|
||||
switch (normalized)
|
||||
{
|
||||
case "single":
|
||||
parsed = AttributeSelectionType.Single;
|
||||
return true;
|
||||
case "multi":
|
||||
parsed = AttributeSelectionType.Multiple;
|
||||
return true;
|
||||
default:
|
||||
parsed = AttributeSelectionType.Single;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 选择类型转字符串。
|
||||
/// </summary>
|
||||
public static string ToSelectionTypeText(AttributeSelectionType selectionType)
|
||||
{
|
||||
return selectionType == AttributeSelectionType.Multiple ? "multi" : "single";
|
||||
}
|
||||
|
||||
private static string? NormalizeStatus(string? status)
|
||||
{
|
||||
var normalized = status?.Trim().ToLowerInvariant();
|
||||
return normalized is "enabled" or "disabled" ? normalized : null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using MediatR;
|
||||
using TakeoutSaaS.Application.App.Products.Dto;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Products.Queries;
|
||||
|
||||
/// <summary>
|
||||
/// 查询规格做法模板列表。
|
||||
/// </summary>
|
||||
public sealed class GetProductSpecTemplateListQuery : IRequest<IReadOnlyList<ProductSpecTemplateItemDto>>
|
||||
{
|
||||
/// <summary>
|
||||
/// 门店 ID。
|
||||
/// </summary>
|
||||
public long StoreId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 关键字。
|
||||
/// </summary>
|
||||
public string? Keyword { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 模板类型(spec/method)。
|
||||
/// </summary>
|
||||
public string? Type { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 状态(enabled/disabled)。
|
||||
/// </summary>
|
||||
public string? Status { get; init; }
|
||||
}
|
||||
Reference in New Issue
Block a user