feat: 新增加料管理接口与模板能力
All checks were successful
Build and Deploy TenantApi / build-and-deploy (push) Successful in 43s
All checks were successful
Build and Deploy TenantApi / build-and-deploy (push) Successful in 43s
This commit is contained in:
@@ -0,0 +1,279 @@
|
|||||||
|
namespace TakeoutSaaS.TenantApi.Contracts.Product;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 加料组列表查询请求。
|
||||||
|
/// </summary>
|
||||||
|
public sealed class ProductAddonGroupListRequest
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 门店 ID。
|
||||||
|
/// </summary>
|
||||||
|
public string StoreId { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 关键字。
|
||||||
|
/// </summary>
|
||||||
|
public string? Keyword { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 状态(enabled/disabled)。
|
||||||
|
/// </summary>
|
||||||
|
public string? Status { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 保存加料组请求。
|
||||||
|
/// </summary>
|
||||||
|
public sealed class SaveProductAddonGroupRequest
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 门店 ID。
|
||||||
|
/// </summary>
|
||||||
|
public string StoreId { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 加料组 ID(编辑时传)。
|
||||||
|
/// </summary>
|
||||||
|
public string? Id { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 加料组名称。
|
||||||
|
/// </summary>
|
||||||
|
public string Name { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 加料组描述。
|
||||||
|
/// </summary>
|
||||||
|
public string Description { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否必选。
|
||||||
|
/// </summary>
|
||||||
|
public bool Required { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 最小可选数。
|
||||||
|
/// </summary>
|
||||||
|
public int MinSelect { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 最大可选数。
|
||||||
|
/// </summary>
|
||||||
|
public int MaxSelect { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 排序值。
|
||||||
|
/// </summary>
|
||||||
|
public int Sort { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 状态(enabled/disabled)。
|
||||||
|
/// </summary>
|
||||||
|
public string Status { get; set; } = "enabled";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 关联商品 ID 列表。
|
||||||
|
/// </summary>
|
||||||
|
public List<string> ProductIds { get; set; } = [];
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 加料项列表。
|
||||||
|
/// </summary>
|
||||||
|
public List<SaveProductAddonItemRequest> Items { get; set; } = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 保存加料项请求。
|
||||||
|
/// </summary>
|
||||||
|
public sealed class SaveProductAddonItemRequest
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 加料项 ID(编辑时传)。
|
||||||
|
/// </summary>
|
||||||
|
public string? Id { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 加料项名称。
|
||||||
|
/// </summary>
|
||||||
|
public string Name { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 加价金额。
|
||||||
|
/// </summary>
|
||||||
|
public decimal Price { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 库存数量。
|
||||||
|
/// </summary>
|
||||||
|
public int Stock { get; set; } = 999;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 排序值。
|
||||||
|
/// </summary>
|
||||||
|
public int Sort { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 状态(enabled/disabled)。
|
||||||
|
/// </summary>
|
||||||
|
public string Status { get; set; } = "enabled";
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 删除加料组请求。
|
||||||
|
/// </summary>
|
||||||
|
public sealed class DeleteProductAddonGroupRequest
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 门店 ID。
|
||||||
|
/// </summary>
|
||||||
|
public string StoreId { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 加料组 ID。
|
||||||
|
/// </summary>
|
||||||
|
public string GroupId { get; set; } = string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 修改加料组状态请求。
|
||||||
|
/// </summary>
|
||||||
|
public sealed class ChangeProductAddonGroupStatusRequest
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 门店 ID。
|
||||||
|
/// </summary>
|
||||||
|
public string StoreId { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 加料组 ID。
|
||||||
|
/// </summary>
|
||||||
|
public string GroupId { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 状态(enabled/disabled)。
|
||||||
|
/// </summary>
|
||||||
|
public string Status { get; set; } = "enabled";
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 绑定加料组商品请求。
|
||||||
|
/// </summary>
|
||||||
|
public sealed class BindProductAddonGroupProductsRequest
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 门店 ID。
|
||||||
|
/// </summary>
|
||||||
|
public string StoreId { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 加料组 ID。
|
||||||
|
/// </summary>
|
||||||
|
public string GroupId { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 商品 ID 列表。
|
||||||
|
/// </summary>
|
||||||
|
public List<string> ProductIds { get; set; } = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 加料项响应。
|
||||||
|
/// </summary>
|
||||||
|
public sealed class ProductAddonItemResponse
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 加料项 ID。
|
||||||
|
/// </summary>
|
||||||
|
public string Id { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 加料项名称。
|
||||||
|
/// </summary>
|
||||||
|
public string Name { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 加价金额。
|
||||||
|
/// </summary>
|
||||||
|
public decimal Price { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 库存数量。
|
||||||
|
/// </summary>
|
||||||
|
public int Stock { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 排序值。
|
||||||
|
/// </summary>
|
||||||
|
public int Sort { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 状态(enabled/disabled)。
|
||||||
|
/// </summary>
|
||||||
|
public string Status { get; set; } = "enabled";
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 加料组响应。
|
||||||
|
/// </summary>
|
||||||
|
public sealed class ProductAddonGroupItemResponse
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 加料组 ID。
|
||||||
|
/// </summary>
|
||||||
|
public string Id { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 加料组名称。
|
||||||
|
/// </summary>
|
||||||
|
public string Name { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 描述。
|
||||||
|
/// </summary>
|
||||||
|
public string Description { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否必选。
|
||||||
|
/// </summary>
|
||||||
|
public bool Required { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 最小可选数。
|
||||||
|
/// </summary>
|
||||||
|
public int MinSelect { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 最大可选数。
|
||||||
|
/// </summary>
|
||||||
|
public int MaxSelect { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 排序值。
|
||||||
|
/// </summary>
|
||||||
|
public int Sort { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 状态(enabled/disabled)。
|
||||||
|
/// </summary>
|
||||||
|
public string Status { get; set; } = "enabled";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 关联商品数量。
|
||||||
|
/// </summary>
|
||||||
|
public int ProductCount { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 关联商品 ID 列表。
|
||||||
|
/// </summary>
|
||||||
|
public List<string> ProductIds { get; set; } = [];
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 加料项列表。
|
||||||
|
/// </summary>
|
||||||
|
public List<ProductAddonItemResponse> Items { get; set; } = [];
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 更新时间。
|
||||||
|
/// </summary>
|
||||||
|
public string UpdatedAt { get; set; } = string.Empty;
|
||||||
|
}
|
||||||
@@ -0,0 +1,204 @@
|
|||||||
|
using MediatR;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using TakeoutSaaS.Application.App.Products.Commands;
|
||||||
|
using TakeoutSaaS.Application.App.Products.Dto;
|
||||||
|
using TakeoutSaaS.Application.App.Products.Queries;
|
||||||
|
using TakeoutSaaS.Application.App.Stores.Services;
|
||||||
|
using TakeoutSaaS.Infrastructure.App.Persistence;
|
||||||
|
using TakeoutSaaS.Shared.Abstractions.Results;
|
||||||
|
using TakeoutSaaS.Shared.Web.Api;
|
||||||
|
using TakeoutSaaS.TenantApi.Contracts.Product;
|
||||||
|
|
||||||
|
namespace TakeoutSaaS.TenantApi.Controllers;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 租户端加料管理。
|
||||||
|
/// </summary>
|
||||||
|
[ApiVersion("1.0")]
|
||||||
|
[Authorize]
|
||||||
|
[Route("api/tenant/v{version:apiVersion}/product")]
|
||||||
|
public sealed class ProductAddonController(
|
||||||
|
IMediator mediator,
|
||||||
|
TakeoutAppDbContext dbContext,
|
||||||
|
StoreContextService storeContextService) : BaseApiController
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 加料组列表。
|
||||||
|
/// </summary>
|
||||||
|
[HttpGet("addon/group/list")]
|
||||||
|
[ProducesResponseType(typeof(ApiResponse<IReadOnlyList<ProductAddonGroupItemResponse>>), StatusCodes.Status200OK)]
|
||||||
|
public async Task<ApiResponse<IReadOnlyList<ProductAddonGroupItemResponse>>> GetAddonGroupList(
|
||||||
|
[FromQuery] ProductAddonGroupListRequest request,
|
||||||
|
CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
// 1. 校验门店访问权限。
|
||||||
|
var storeId = StoreApiHelpers.ParseRequiredSnowflake(request.StoreId, nameof(request.StoreId));
|
||||||
|
await EnsureStoreAccessibleAsync(storeId, cancellationToken);
|
||||||
|
|
||||||
|
// 2. 查询并返回列表。
|
||||||
|
var result = await mediator.Send(new GetProductAddonGroupListQuery
|
||||||
|
{
|
||||||
|
StoreId = storeId,
|
||||||
|
Keyword = request.Keyword,
|
||||||
|
Status = request.Status
|
||||||
|
}, cancellationToken);
|
||||||
|
|
||||||
|
return ApiResponse<IReadOnlyList<ProductAddonGroupItemResponse>>.Ok(result.Select(MapAddonGroupItem).ToList());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 保存加料组。
|
||||||
|
/// </summary>
|
||||||
|
[HttpPost("addon/group/save")]
|
||||||
|
[ProducesResponseType(typeof(ApiResponse<ProductAddonGroupItemResponse>), StatusCodes.Status200OK)]
|
||||||
|
public async Task<ApiResponse<ProductAddonGroupItemResponse>> SaveAddonGroup(
|
||||||
|
[FromBody] SaveProductAddonGroupRequest request,
|
||||||
|
CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
// 1. 校验门店访问权限。
|
||||||
|
var storeId = StoreApiHelpers.ParseRequiredSnowflake(request.StoreId, nameof(request.StoreId));
|
||||||
|
await EnsureStoreAccessibleAsync(storeId, cancellationToken);
|
||||||
|
|
||||||
|
// 2. 提交保存命令。
|
||||||
|
var result = await mediator.Send(new SaveProductAddonGroupCommand
|
||||||
|
{
|
||||||
|
StoreId = storeId,
|
||||||
|
GroupId = StoreApiHelpers.ParseSnowflakeOrNull(request.Id),
|
||||||
|
Name = request.Name,
|
||||||
|
Description = request.Description,
|
||||||
|
Required = request.Required,
|
||||||
|
MinSelect = request.MinSelect,
|
||||||
|
MaxSelect = request.MaxSelect,
|
||||||
|
Sort = request.Sort,
|
||||||
|
Status = request.Status,
|
||||||
|
ProductIds = StoreApiHelpers.ParseSnowflakeList(request.ProductIds),
|
||||||
|
Items = (request.Items ?? [])
|
||||||
|
.Select(item => new SaveProductAddonItemCommand
|
||||||
|
{
|
||||||
|
Id = StoreApiHelpers.ParseSnowflakeOrNull(item.Id),
|
||||||
|
Name = item.Name,
|
||||||
|
Price = item.Price,
|
||||||
|
Stock = item.Stock,
|
||||||
|
Sort = item.Sort,
|
||||||
|
Status = item.Status
|
||||||
|
})
|
||||||
|
.ToList()
|
||||||
|
}, cancellationToken);
|
||||||
|
|
||||||
|
return ApiResponse<ProductAddonGroupItemResponse>.Ok(MapAddonGroupItem(result));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 删除加料组。
|
||||||
|
/// </summary>
|
||||||
|
[HttpPost("addon/group/delete")]
|
||||||
|
[ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status200OK)]
|
||||||
|
public async Task<ApiResponse<object>> DeleteAddonGroup(
|
||||||
|
[FromBody] DeleteProductAddonGroupRequest request,
|
||||||
|
CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
// 1. 校验门店访问权限。
|
||||||
|
var storeId = StoreApiHelpers.ParseRequiredSnowflake(request.StoreId, nameof(request.StoreId));
|
||||||
|
await EnsureStoreAccessibleAsync(storeId, cancellationToken);
|
||||||
|
|
||||||
|
// 2. 提交删除命令。
|
||||||
|
await mediator.Send(new DeleteProductAddonGroupCommand
|
||||||
|
{
|
||||||
|
StoreId = storeId,
|
||||||
|
GroupId = StoreApiHelpers.ParseRequiredSnowflake(request.GroupId, nameof(request.GroupId))
|
||||||
|
}, cancellationToken);
|
||||||
|
|
||||||
|
return ApiResponse<object>.Ok(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 修改加料组状态。
|
||||||
|
/// </summary>
|
||||||
|
[HttpPost("addon/group/status")]
|
||||||
|
[ProducesResponseType(typeof(ApiResponse<ProductAddonGroupItemResponse>), StatusCodes.Status200OK)]
|
||||||
|
public async Task<ApiResponse<ProductAddonGroupItemResponse>> ChangeAddonGroupStatus(
|
||||||
|
[FromBody] ChangeProductAddonGroupStatusRequest request,
|
||||||
|
CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
// 1. 校验门店访问权限。
|
||||||
|
var storeId = StoreApiHelpers.ParseRequiredSnowflake(request.StoreId, nameof(request.StoreId));
|
||||||
|
await EnsureStoreAccessibleAsync(storeId, cancellationToken);
|
||||||
|
|
||||||
|
// 2. 提交状态命令并返回更新后的快照。
|
||||||
|
var result = await mediator.Send(new ChangeProductAddonGroupStatusCommand
|
||||||
|
{
|
||||||
|
StoreId = storeId,
|
||||||
|
GroupId = StoreApiHelpers.ParseRequiredSnowflake(request.GroupId, nameof(request.GroupId)),
|
||||||
|
Status = request.Status
|
||||||
|
}, cancellationToken);
|
||||||
|
|
||||||
|
return ApiResponse<ProductAddonGroupItemResponse>.Ok(MapAddonGroupItem(result));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 绑定加料组商品。
|
||||||
|
/// </summary>
|
||||||
|
[HttpPost("addon/group/products/bind")]
|
||||||
|
[ProducesResponseType(typeof(ApiResponse<ProductAddonGroupItemResponse>), StatusCodes.Status200OK)]
|
||||||
|
public async Task<ApiResponse<ProductAddonGroupItemResponse>> BindAddonGroupProducts(
|
||||||
|
[FromBody] BindProductAddonGroupProductsRequest request,
|
||||||
|
CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
// 1. 校验门店访问权限。
|
||||||
|
var storeId = StoreApiHelpers.ParseRequiredSnowflake(request.StoreId, nameof(request.StoreId));
|
||||||
|
await EnsureStoreAccessibleAsync(storeId, cancellationToken);
|
||||||
|
|
||||||
|
// 2. 提交绑定命令并返回更新后的快照。
|
||||||
|
var result = await mediator.Send(new BindProductAddonGroupProductsCommand
|
||||||
|
{
|
||||||
|
StoreId = storeId,
|
||||||
|
GroupId = StoreApiHelpers.ParseRequiredSnowflake(request.GroupId, nameof(request.GroupId)),
|
||||||
|
ProductIds = StoreApiHelpers.ParseSnowflakeList(request.ProductIds)
|
||||||
|
}, cancellationToken);
|
||||||
|
|
||||||
|
return ApiResponse<ProductAddonGroupItemResponse>.Ok(MapAddonGroupItem(result));
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task EnsureStoreAccessibleAsync(long storeId, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
// 1. 读取当前租户上下文。
|
||||||
|
var (tenantId, merchantId) = StoreApiHelpers.GetTenantMerchantContext(storeContextService);
|
||||||
|
|
||||||
|
// 2. 校验门店是否属于当前租户商户。
|
||||||
|
await StoreApiHelpers.EnsureStoreAccessibleAsync(dbContext, tenantId, merchantId, storeId, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ProductAddonGroupItemResponse MapAddonGroupItem(ProductAddonTemplateItemDto source)
|
||||||
|
{
|
||||||
|
// 1. 映射加料项列表。
|
||||||
|
var items = source.Items
|
||||||
|
.Select(item => new ProductAddonItemResponse
|
||||||
|
{
|
||||||
|
Id = item.Id.ToString(),
|
||||||
|
Name = item.Name,
|
||||||
|
Price = item.Price,
|
||||||
|
Stock = item.Stock,
|
||||||
|
Sort = item.Sort,
|
||||||
|
Status = item.Status
|
||||||
|
})
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
// 2. 映射加料组响应。
|
||||||
|
return new ProductAddonGroupItemResponse
|
||||||
|
{
|
||||||
|
Id = source.Id.ToString(),
|
||||||
|
Name = source.Name,
|
||||||
|
Description = source.Description,
|
||||||
|
Required = source.Required,
|
||||||
|
MinSelect = source.MinSelect,
|
||||||
|
MaxSelect = source.MaxSelect,
|
||||||
|
Sort = source.Sort,
|
||||||
|
Status = source.Status,
|
||||||
|
ProductCount = source.ProductCount,
|
||||||
|
ProductIds = source.ProductIds.Select(item => item.ToString()).ToList(),
|
||||||
|
Items = items,
|
||||||
|
UpdatedAt = source.UpdatedAt.ToString("yyyy-MM-dd HH:mm:ss")
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
using MediatR;
|
||||||
|
using TakeoutSaaS.Application.App.Products.Dto;
|
||||||
|
|
||||||
|
namespace TakeoutSaaS.Application.App.Products.Commands;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 绑定加料组商品命令。
|
||||||
|
/// </summary>
|
||||||
|
public sealed class BindProductAddonGroupProductsCommand : IRequest<ProductAddonTemplateItemDto>
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 门店 ID。
|
||||||
|
/// </summary>
|
||||||
|
public long StoreId { get; init; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 加料组 ID。
|
||||||
|
/// </summary>
|
||||||
|
public long GroupId { get; init; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 商品 ID 列表。
|
||||||
|
/// </summary>
|
||||||
|
public IReadOnlyList<long> ProductIds { get; init; } = [];
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
using MediatR;
|
||||||
|
using TakeoutSaaS.Application.App.Products.Dto;
|
||||||
|
|
||||||
|
namespace TakeoutSaaS.Application.App.Products.Commands;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 修改加料组状态命令。
|
||||||
|
/// </summary>
|
||||||
|
public sealed class ChangeProductAddonGroupStatusCommand : IRequest<ProductAddonTemplateItemDto>
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 门店 ID。
|
||||||
|
/// </summary>
|
||||||
|
public long StoreId { get; init; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 加料组 ID。
|
||||||
|
/// </summary>
|
||||||
|
public long GroupId { get; init; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 状态(enabled/disabled)。
|
||||||
|
/// </summary>
|
||||||
|
public string Status { get; init; } = "enabled";
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
using MediatR;
|
||||||
|
|
||||||
|
namespace TakeoutSaaS.Application.App.Products.Commands;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 删除加料组命令。
|
||||||
|
/// </summary>
|
||||||
|
public sealed class DeleteProductAddonGroupCommand : IRequest
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 门店 ID。
|
||||||
|
/// </summary>
|
||||||
|
public long StoreId { get; init; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 加料组 ID。
|
||||||
|
/// </summary>
|
||||||
|
public long GroupId { get; init; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
using MediatR;
|
||||||
|
using TakeoutSaaS.Application.App.Products.Dto;
|
||||||
|
|
||||||
|
namespace TakeoutSaaS.Application.App.Products.Commands;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 保存加料组命令。
|
||||||
|
/// </summary>
|
||||||
|
public sealed class SaveProductAddonGroupCommand : IRequest<ProductAddonTemplateItemDto>
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 门店 ID。
|
||||||
|
/// </summary>
|
||||||
|
public long StoreId { get; init; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 加料组 ID(编辑时传)。
|
||||||
|
/// </summary>
|
||||||
|
public long? GroupId { get; init; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 加料组名称。
|
||||||
|
/// </summary>
|
||||||
|
public string Name { get; init; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 描述。
|
||||||
|
/// </summary>
|
||||||
|
public string Description { get; init; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否必选。
|
||||||
|
/// </summary>
|
||||||
|
public bool Required { get; init; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 最小可选数量。
|
||||||
|
/// </summary>
|
||||||
|
public int MinSelect { get; init; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 最大可选数量。
|
||||||
|
/// </summary>
|
||||||
|
public int MaxSelect { get; init; }
|
||||||
|
|
||||||
|
/// <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<SaveProductAddonItemCommand> Items { get; init; } = [];
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
namespace TakeoutSaaS.Application.App.Products.Commands;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 保存加料项命令。
|
||||||
|
/// </summary>
|
||||||
|
public sealed class SaveProductAddonItemCommand
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 加料项 ID(编辑时可传)。
|
||||||
|
/// </summary>
|
||||||
|
public long? Id { get; init; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 加料项名称。
|
||||||
|
/// </summary>
|
||||||
|
public string Name { get; init; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 加价金额。
|
||||||
|
/// </summary>
|
||||||
|
public decimal Price { get; init; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 库存数量。
|
||||||
|
/// </summary>
|
||||||
|
public int Stock { get; init; } = 999;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 排序值。
|
||||||
|
/// </summary>
|
||||||
|
public int Sort { get; init; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 状态(enabled/disabled)。
|
||||||
|
/// </summary>
|
||||||
|
public string Status { get; init; } = "enabled";
|
||||||
|
}
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
namespace TakeoutSaaS.Application.App.Products.Dto;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 加料模板列表项 DTO。
|
||||||
|
/// </summary>
|
||||||
|
public sealed class ProductAddonTemplateItemDto
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 模板 ID。
|
||||||
|
/// </summary>
|
||||||
|
public long Id { get; init; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 模板名称。
|
||||||
|
/// </summary>
|
||||||
|
public string Name { get; init; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 模板描述。
|
||||||
|
/// </summary>
|
||||||
|
public string Description { get; init; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否必选。
|
||||||
|
/// </summary>
|
||||||
|
public bool Required { get; init; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 最小可选数。
|
||||||
|
/// </summary>
|
||||||
|
public int MinSelect { get; init; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 最大可选数。
|
||||||
|
/// </summary>
|
||||||
|
public int MaxSelect { 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<ProductAddonTemplateOptionDto> Items { get; init; } = [];
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 更新时间。
|
||||||
|
/// </summary>
|
||||||
|
public DateTime UpdatedAt { get; init; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
namespace TakeoutSaaS.Application.App.Products.Dto;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 加料模板选项 DTO。
|
||||||
|
/// </summary>
|
||||||
|
public sealed class ProductAddonTemplateOptionDto
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 选项 ID。
|
||||||
|
/// </summary>
|
||||||
|
public long Id { get; init; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 选项名称。
|
||||||
|
/// </summary>
|
||||||
|
public string Name { get; init; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 加价金额。
|
||||||
|
/// </summary>
|
||||||
|
public decimal Price { get; init; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 库存数量。
|
||||||
|
/// </summary>
|
||||||
|
public int Stock { get; init; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 排序值。
|
||||||
|
/// </summary>
|
||||||
|
public int Sort { get; init; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 状态(enabled/disabled)。
|
||||||
|
/// </summary>
|
||||||
|
public string Status { get; init; } = "enabled";
|
||||||
|
}
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
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 BindProductAddonGroupProductsCommandHandler(
|
||||||
|
IProductRepository productRepository,
|
||||||
|
ITenantProvider tenantProvider)
|
||||||
|
: IRequestHandler<BindProductAddonGroupProductsCommand, ProductAddonTemplateItemDto>
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
public async Task<ProductAddonTemplateItemDto> Handle(BindProductAddonGroupProductsCommand request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
// 1. 校验加料组是否存在且归属当前门店。
|
||||||
|
var tenantId = tenantProvider.GetCurrentTenantId();
|
||||||
|
var existing = await productRepository.FindAddonTemplateByIdAsync(request.GroupId, tenantId, cancellationToken);
|
||||||
|
if (existing is null || existing.StoreId != request.StoreId)
|
||||||
|
{
|
||||||
|
throw new BusinessException(ErrorCodes.NotFound, "加料组不存在");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 过滤有效商品并替换关联关系。
|
||||||
|
var normalizedProductIds = request.ProductIds
|
||||||
|
.Where(item => item > 0)
|
||||||
|
.Distinct()
|
||||||
|
.ToList();
|
||||||
|
var validProductIds = await productRepository.FilterExistingProductIdsAsync(
|
||||||
|
tenantId,
|
||||||
|
request.StoreId,
|
||||||
|
normalizedProductIds,
|
||||||
|
cancellationToken);
|
||||||
|
|
||||||
|
await productRepository.RemoveSpecTemplateProductsAsync(existing.Id, tenantId, request.StoreId, cancellationToken);
|
||||||
|
|
||||||
|
var relations = validProductIds
|
||||||
|
.Select(productId => new ProductSpecTemplateProduct
|
||||||
|
{
|
||||||
|
StoreId = request.StoreId,
|
||||||
|
TemplateId = existing.Id,
|
||||||
|
ProductId = productId
|
||||||
|
})
|
||||||
|
.ToList();
|
||||||
|
if (relations.Count > 0)
|
||||||
|
{
|
||||||
|
await productRepository.AddSpecTemplateProductsAsync(relations, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
await productRepository.SaveChangesAsync(cancellationToken);
|
||||||
|
|
||||||
|
// 3. 返回最新快照。
|
||||||
|
var options = await productRepository.GetSpecTemplateOptionsByTemplateIdsAsync([existing.Id], tenantId, cancellationToken);
|
||||||
|
var latestRelations = 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 = latestRelations
|
||||||
|
.GroupBy(x => x.TemplateId)
|
||||||
|
.ToDictionary(group => group.Key, group => group.Select(item => item.ProductId).ToList());
|
||||||
|
|
||||||
|
return ProductAddonTemplateDtoFactory.ToDto(existing, optionsLookup, productIdsLookup);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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 ChangeProductAddonGroupStatusCommandHandler(
|
||||||
|
IProductRepository productRepository,
|
||||||
|
ITenantProvider tenantProvider)
|
||||||
|
: IRequestHandler<ChangeProductAddonGroupStatusCommand, ProductAddonTemplateItemDto>
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
public async Task<ProductAddonTemplateItemDto> Handle(ChangeProductAddonGroupStatusCommand request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
// 1. 解析状态并验证加料组归属。
|
||||||
|
if (!ProductAddonTemplateMapping.TryParseStatus(request.Status, out var isEnabled))
|
||||||
|
{
|
||||||
|
throw new BusinessException(ErrorCodes.BadRequest, "status 参数不合法");
|
||||||
|
}
|
||||||
|
|
||||||
|
var tenantId = tenantProvider.GetCurrentTenantId();
|
||||||
|
var existing = await productRepository.FindAddonTemplateByIdAsync(request.GroupId, 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 ProductAddonTemplateDtoFactory.ToDto(existing, optionsLookup, productIdsLookup);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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 DeleteProductAddonGroupCommandHandler(
|
||||||
|
IProductRepository productRepository,
|
||||||
|
ITenantProvider tenantProvider)
|
||||||
|
: IRequestHandler<DeleteProductAddonGroupCommand>
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
public async Task Handle(DeleteProductAddonGroupCommand request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
// 1. 校验加料组是否存在且归属当前门店。
|
||||||
|
var tenantId = tenantProvider.GetCurrentTenantId();
|
||||||
|
var existing = await productRepository.FindAddonTemplateByIdAsync(request.GroupId, 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,75 @@
|
|||||||
|
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 GetProductAddonGroupListQueryHandler(
|
||||||
|
IProductRepository productRepository,
|
||||||
|
ITenantProvider tenantProvider)
|
||||||
|
: IRequestHandler<GetProductAddonGroupListQuery, IReadOnlyList<ProductAddonTemplateItemDto>>
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
public async Task<IReadOnlyList<ProductAddonTemplateItemDto>> Handle(GetProductAddonGroupListQuery request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
// 1. 读取门店下的加料模板。
|
||||||
|
var tenantId = tenantProvider.GetCurrentTenantId();
|
||||||
|
var templates = await productRepository.GetAddonTemplatesByStoreAsync(tenantId, request.StoreId, cancellationToken);
|
||||||
|
if (templates.Count == 0)
|
||||||
|
{
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 按状态与关键字过滤。
|
||||||
|
IEnumerable<ProductSpecTemplate> filtered = templates;
|
||||||
|
if (!string.IsNullOrWhiteSpace(request.Status))
|
||||||
|
{
|
||||||
|
if (!ProductAddonTemplateMapping.TryParseStatus(request.Status, out var isEnabled))
|
||||||
|
{
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
filtered = filtered.Where(item => item.IsEnabled == isEnabled);
|
||||||
|
}
|
||||||
|
|
||||||
|
var normalizedKeyword = request.Keyword?.Trim().ToLowerInvariant();
|
||||||
|
if (!string.IsNullOrWhiteSpace(normalizedKeyword))
|
||||||
|
{
|
||||||
|
filtered = filtered.Where(item =>
|
||||||
|
item.Name.ToLower().Contains(normalizedKeyword) ||
|
||||||
|
item.Description.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 options = await productRepository.GetSpecTemplateOptionsByTemplateIdsAsync(templateIds, tenantId, cancellationToken);
|
||||||
|
var relations = await productRepository.GetSpecTemplateProductsByTemplateIdsAsync(templateIds, tenantId, request.StoreId, cancellationToken);
|
||||||
|
|
||||||
|
// 4. 构建字典并映射 DTO。
|
||||||
|
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 filteredList
|
||||||
|
.Select(template => ProductAddonTemplateDtoFactory.ToDto(template, optionsLookup, productIdsLookup))
|
||||||
|
.ToList();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,241 @@
|
|||||||
|
using MediatR;
|
||||||
|
using TakeoutSaaS.Application.App.Products.Commands;
|
||||||
|
using TakeoutSaaS.Application.App.Products.Dto;
|
||||||
|
using TakeoutSaaS.Domain.Products.Entities;
|
||||||
|
using TakeoutSaaS.Domain.Products.Enums;
|
||||||
|
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 SaveProductAddonGroupCommandHandler(
|
||||||
|
IProductRepository productRepository,
|
||||||
|
ITenantProvider tenantProvider)
|
||||||
|
: IRequestHandler<SaveProductAddonGroupCommand, ProductAddonTemplateItemDto>
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
public async Task<ProductAddonTemplateItemDto> Handle(SaveProductAddonGroupCommand request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
// 1. 校验基础输入并归一化参数。
|
||||||
|
var tenantId = tenantProvider.GetCurrentTenantId();
|
||||||
|
var normalizedName = request.Name.Trim();
|
||||||
|
if (string.IsNullOrWhiteSpace(normalizedName))
|
||||||
|
{
|
||||||
|
throw new BusinessException(ErrorCodes.BadRequest, "加料组名称不能为空");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!ProductAddonTemplateMapping.TryParseStatus(request.Status, out var isEnabled))
|
||||||
|
{
|
||||||
|
throw new BusinessException(ErrorCodes.BadRequest, "status 参数不合法");
|
||||||
|
}
|
||||||
|
|
||||||
|
var normalizedDescription = request.Description.Trim();
|
||||||
|
var normalizedMinSelect = Math.Max(0, request.MinSelect);
|
||||||
|
var normalizedMaxSelect = Math.Max(1, request.MaxSelect);
|
||||||
|
if (normalizedMaxSelect < normalizedMinSelect)
|
||||||
|
{
|
||||||
|
normalizedMaxSelect = normalizedMinSelect;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.Required && normalizedMinSelect == 0)
|
||||||
|
{
|
||||||
|
normalizedMinSelect = 1;
|
||||||
|
if (normalizedMaxSelect < 1)
|
||||||
|
{
|
||||||
|
normalizedMaxSelect = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var normalizedItems = NormalizeItems(request.Items);
|
||||||
|
if (normalizedItems.Count == 0)
|
||||||
|
{
|
||||||
|
throw new BusinessException(ErrorCodes.BadRequest, "请至少添加一个加料项");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 校验同门店名称唯一。
|
||||||
|
var isDuplicate = await productRepository.ExistsAddonTemplateNameAsync(
|
||||||
|
tenantId,
|
||||||
|
request.StoreId,
|
||||||
|
normalizedName,
|
||||||
|
request.GroupId,
|
||||||
|
cancellationToken);
|
||||||
|
if (isDuplicate)
|
||||||
|
{
|
||||||
|
throw new BusinessException(ErrorCodes.Conflict, "加料组名称已存在");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. 过滤有效商品并创建或更新模板主记录。
|
||||||
|
var normalizedProductIds = request.ProductIds
|
||||||
|
.Where(id => id > 0)
|
||||||
|
.Distinct()
|
||||||
|
.ToList();
|
||||||
|
var validProductIds = await productRepository.FilterExistingProductIdsAsync(
|
||||||
|
tenantId,
|
||||||
|
request.StoreId,
|
||||||
|
normalizedProductIds,
|
||||||
|
cancellationToken);
|
||||||
|
|
||||||
|
ProductSpecTemplate template;
|
||||||
|
var isCreate = !request.GroupId.HasValue;
|
||||||
|
if (isCreate)
|
||||||
|
{
|
||||||
|
template = new ProductSpecTemplate
|
||||||
|
{
|
||||||
|
StoreId = request.StoreId,
|
||||||
|
Name = normalizedName,
|
||||||
|
Description = normalizedDescription,
|
||||||
|
TemplateType = ProductSpecTemplateType.Addon,
|
||||||
|
SelectionType = ProductAddonTemplateMapping.ToSelectionType(normalizedMaxSelect),
|
||||||
|
IsRequired = request.Required,
|
||||||
|
MinSelect = normalizedMinSelect,
|
||||||
|
MaxSelect = normalizedMaxSelect,
|
||||||
|
SortOrder = Math.Max(1, request.Sort),
|
||||||
|
IsEnabled = isEnabled
|
||||||
|
};
|
||||||
|
|
||||||
|
await productRepository.AddSpecTemplateAsync(template, cancellationToken);
|
||||||
|
await productRepository.SaveChangesAsync(cancellationToken);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var groupId = request.GroupId
|
||||||
|
?? throw new BusinessException(ErrorCodes.BadRequest, "加料组标识不能为空");
|
||||||
|
|
||||||
|
var existing = await productRepository.FindAddonTemplateByIdAsync(
|
||||||
|
groupId,
|
||||||
|
tenantId,
|
||||||
|
cancellationToken);
|
||||||
|
if (existing is null || existing.StoreId != request.StoreId)
|
||||||
|
{
|
||||||
|
throw new BusinessException(ErrorCodes.NotFound, "加料组不存在");
|
||||||
|
}
|
||||||
|
|
||||||
|
template = existing;
|
||||||
|
template.Name = normalizedName;
|
||||||
|
template.Description = normalizedDescription;
|
||||||
|
template.SelectionType = ProductAddonTemplateMapping.ToSelectionType(normalizedMaxSelect);
|
||||||
|
template.IsRequired = request.Required;
|
||||||
|
template.MinSelect = normalizedMinSelect;
|
||||||
|
template.MaxSelect = normalizedMaxSelect;
|
||||||
|
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 = normalizedItems
|
||||||
|
.Select(item => new ProductSpecTemplateOption
|
||||||
|
{
|
||||||
|
TemplateId = template.Id,
|
||||||
|
Name = item.Name,
|
||||||
|
ExtraPrice = item.Price,
|
||||||
|
Stock = item.Stock,
|
||||||
|
IsEnabled = item.IsEnabled,
|
||||||
|
SortOrder = item.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. 回读并返回最新快照。
|
||||||
|
return await LoadAddonTemplateSnapshotAsync(template, tenantId, request.StoreId, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static IReadOnlyList<NormalizedAddonItem> NormalizeItems(IReadOnlyList<SaveProductAddonItemCommand>? items)
|
||||||
|
{
|
||||||
|
var source = items ?? [];
|
||||||
|
var normalized = source
|
||||||
|
.Select((item, index) =>
|
||||||
|
{
|
||||||
|
if (!ProductAddonTemplateMapping.TryParseStatus(item.Status, out var isEnabled))
|
||||||
|
{
|
||||||
|
throw new BusinessException(ErrorCodes.BadRequest, "加料项状态不合法");
|
||||||
|
}
|
||||||
|
|
||||||
|
return new NormalizedAddonItem
|
||||||
|
{
|
||||||
|
Name = item.Name.Trim(),
|
||||||
|
Price = item.Price,
|
||||||
|
Stock = Math.Max(0, item.Stock),
|
||||||
|
Sort = item.Sort > 0 ? item.Sort : index + 1,
|
||||||
|
IsEnabled = isEnabled
|
||||||
|
};
|
||||||
|
})
|
||||||
|
.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 async Task<ProductAddonTemplateItemDto> LoadAddonTemplateSnapshotAsync(
|
||||||
|
ProductSpecTemplate template,
|
||||||
|
long tenantId,
|
||||||
|
long storeId,
|
||||||
|
CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var options = await productRepository.GetSpecTemplateOptionsByTemplateIdsAsync([template.Id], tenantId, cancellationToken);
|
||||||
|
var relations = await productRepository.GetSpecTemplateProductsByTemplateIdsAsync([template.Id], tenantId, 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 ProductAddonTemplateDtoFactory.ToDto(template, optionsLookup, productIdsLookup);
|
||||||
|
}
|
||||||
|
|
||||||
|
private sealed class NormalizedAddonItem
|
||||||
|
{
|
||||||
|
public bool IsEnabled { get; init; }
|
||||||
|
|
||||||
|
public string Name { get; init; } = string.Empty;
|
||||||
|
|
||||||
|
public decimal Price { get; init; }
|
||||||
|
|
||||||
|
public int Sort { get; set; }
|
||||||
|
|
||||||
|
public int Stock { get; init; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ using MediatR;
|
|||||||
using TakeoutSaaS.Application.App.Products.Commands;
|
using TakeoutSaaS.Application.App.Products.Commands;
|
||||||
using TakeoutSaaS.Application.App.Products.Dto;
|
using TakeoutSaaS.Application.App.Products.Dto;
|
||||||
using TakeoutSaaS.Domain.Products.Entities;
|
using TakeoutSaaS.Domain.Products.Entities;
|
||||||
|
using TakeoutSaaS.Domain.Products.Enums;
|
||||||
using TakeoutSaaS.Domain.Products.Repositories;
|
using TakeoutSaaS.Domain.Products.Repositories;
|
||||||
using TakeoutSaaS.Shared.Abstractions.Constants;
|
using TakeoutSaaS.Shared.Abstractions.Constants;
|
||||||
using TakeoutSaaS.Shared.Abstractions.Exceptions;
|
using TakeoutSaaS.Shared.Abstractions.Exceptions;
|
||||||
@@ -49,6 +50,11 @@ public sealed class SaveProductSpecTemplateCommandHandler(
|
|||||||
throw new BusinessException(ErrorCodes.BadRequest, "请至少添加一个选项");
|
throw new BusinessException(ErrorCodes.BadRequest, "请至少添加一个选项");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var normalizedMinSelect = request.IsRequired ? 1 : 0;
|
||||||
|
var normalizedMaxSelect = selectionType == AttributeSelectionType.Single
|
||||||
|
? 1
|
||||||
|
: Math.Max(1, normalizedValues.Count);
|
||||||
|
|
||||||
// 2. 校验同门店模板名称唯一。
|
// 2. 校验同门店模板名称唯一。
|
||||||
var isDuplicate = await productRepository.ExistsSpecTemplateNameAsync(
|
var isDuplicate = await productRepository.ExistsSpecTemplateNameAsync(
|
||||||
tenantId,
|
tenantId,
|
||||||
@@ -80,9 +86,12 @@ public sealed class SaveProductSpecTemplateCommandHandler(
|
|||||||
{
|
{
|
||||||
StoreId = request.StoreId,
|
StoreId = request.StoreId,
|
||||||
Name = normalizedName,
|
Name = normalizedName,
|
||||||
|
Description = string.Empty,
|
||||||
TemplateType = templateType,
|
TemplateType = templateType,
|
||||||
SelectionType = selectionType,
|
SelectionType = selectionType,
|
||||||
IsRequired = request.IsRequired,
|
IsRequired = request.IsRequired,
|
||||||
|
MinSelect = normalizedMinSelect,
|
||||||
|
MaxSelect = normalizedMaxSelect,
|
||||||
SortOrder = Math.Max(1, request.Sort),
|
SortOrder = Math.Max(1, request.Sort),
|
||||||
IsEnabled = isEnabled
|
IsEnabled = isEnabled
|
||||||
};
|
};
|
||||||
@@ -105,6 +114,8 @@ public sealed class SaveProductSpecTemplateCommandHandler(
|
|||||||
template.TemplateType = templateType;
|
template.TemplateType = templateType;
|
||||||
template.SelectionType = selectionType;
|
template.SelectionType = selectionType;
|
||||||
template.IsRequired = request.IsRequired;
|
template.IsRequired = request.IsRequired;
|
||||||
|
template.MinSelect = normalizedMinSelect;
|
||||||
|
template.MaxSelect = normalizedMaxSelect;
|
||||||
template.SortOrder = Math.Max(1, request.Sort);
|
template.SortOrder = Math.Max(1, request.Sort);
|
||||||
template.IsEnabled = isEnabled;
|
template.IsEnabled = isEnabled;
|
||||||
await productRepository.UpdateSpecTemplateAsync(template, cancellationToken);
|
await productRepository.UpdateSpecTemplateAsync(template, cancellationToken);
|
||||||
@@ -120,6 +131,8 @@ public sealed class SaveProductSpecTemplateCommandHandler(
|
|||||||
TemplateId = template.Id,
|
TemplateId = template.Id,
|
||||||
Name = value.Name,
|
Name = value.Name,
|
||||||
ExtraPrice = value.ExtraPrice,
|
ExtraPrice = value.ExtraPrice,
|
||||||
|
Stock = 999,
|
||||||
|
IsEnabled = true,
|
||||||
SortOrder = value.Sort
|
SortOrder = value.Sort
|
||||||
})
|
})
|
||||||
.ToList();
|
.ToList();
|
||||||
|
|||||||
@@ -0,0 +1,55 @@
|
|||||||
|
using TakeoutSaaS.Application.App.Products.Dto;
|
||||||
|
using TakeoutSaaS.Domain.Products.Entities;
|
||||||
|
|
||||||
|
namespace TakeoutSaaS.Application.App.Products;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 加料模板 DTO 映射工厂。
|
||||||
|
/// </summary>
|
||||||
|
internal static class ProductAddonTemplateDtoFactory
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 将模板实体映射为页面 DTO。
|
||||||
|
/// </summary>
|
||||||
|
public static ProductAddonTemplateItemDto 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 ProductAddonTemplateItemDto
|
||||||
|
{
|
||||||
|
Id = template.Id,
|
||||||
|
Name = template.Name,
|
||||||
|
Description = template.Description,
|
||||||
|
Required = template.IsRequired,
|
||||||
|
MinSelect = template.MinSelect,
|
||||||
|
MaxSelect = template.MaxSelect,
|
||||||
|
Sort = template.SortOrder,
|
||||||
|
Status = ProductAddonTemplateMapping.ToStatusText(template.IsEnabled),
|
||||||
|
ProductCount = productIds.Count,
|
||||||
|
ProductIds = productIds,
|
||||||
|
Items = options
|
||||||
|
.OrderBy(x => x.SortOrder)
|
||||||
|
.ThenBy(x => x.Id)
|
||||||
|
.Select(option => new ProductAddonTemplateOptionDto
|
||||||
|
{
|
||||||
|
Id = option.Id,
|
||||||
|
Name = option.Name,
|
||||||
|
Price = option.ExtraPrice,
|
||||||
|
Stock = option.Stock,
|
||||||
|
Sort = option.SortOrder,
|
||||||
|
Status = ProductAddonTemplateMapping.ToStatusText(option.IsEnabled)
|
||||||
|
})
|
||||||
|
.ToList(),
|
||||||
|
UpdatedAt = template.UpdatedAt ?? template.CreatedAt
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
using TakeoutSaaS.Domain.Products.Enums;
|
||||||
|
|
||||||
|
namespace TakeoutSaaS.Application.App.Products;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 加料模板映射辅助。
|
||||||
|
/// </summary>
|
||||||
|
internal static class ProductAddonTemplateMapping
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 解析状态字符串。
|
||||||
|
/// </summary>
|
||||||
|
public static bool TryParseStatus(string? status, out bool isEnabled)
|
||||||
|
{
|
||||||
|
var normalized = status?.Trim().ToLowerInvariant();
|
||||||
|
switch (normalized)
|
||||||
|
{
|
||||||
|
case "enabled":
|
||||||
|
isEnabled = true;
|
||||||
|
return true;
|
||||||
|
case "disabled":
|
||||||
|
isEnabled = false;
|
||||||
|
return true;
|
||||||
|
default:
|
||||||
|
isEnabled = true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 状态转字符串。
|
||||||
|
/// </summary>
|
||||||
|
public static string ToStatusText(bool isEnabled)
|
||||||
|
{
|
||||||
|
return isEnabled ? "enabled" : "disabled";
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 依据最大可选数推导选择类型。
|
||||||
|
/// </summary>
|
||||||
|
public static AttributeSelectionType ToSelectionType(int maxSelect)
|
||||||
|
{
|
||||||
|
return maxSelect <= 1 ? AttributeSelectionType.Single : AttributeSelectionType.Multiple;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -56,7 +56,12 @@ internal static class ProductSpecTemplateMapping
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static string ToTemplateTypeText(ProductSpecTemplateType templateType)
|
public static string ToTemplateTypeText(ProductSpecTemplateType templateType)
|
||||||
{
|
{
|
||||||
return templateType == ProductSpecTemplateType.Method ? "method" : "spec";
|
return templateType switch
|
||||||
|
{
|
||||||
|
ProductSpecTemplateType.Method => "method",
|
||||||
|
ProductSpecTemplateType.Addon => "addon",
|
||||||
|
_ => "spec"
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
using MediatR;
|
||||||
|
using TakeoutSaaS.Application.App.Products.Dto;
|
||||||
|
|
||||||
|
namespace TakeoutSaaS.Application.App.Products.Queries;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 查询加料组列表。
|
||||||
|
/// </summary>
|
||||||
|
public sealed class GetProductAddonGroupListQuery : IRequest<IReadOnlyList<ProductAddonTemplateItemDto>>
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 门店 ID。
|
||||||
|
/// </summary>
|
||||||
|
public long StoreId { get; init; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 关键字。
|
||||||
|
/// </summary>
|
||||||
|
public string? Keyword { get; init; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 状态(enabled/disabled)。
|
||||||
|
/// </summary>
|
||||||
|
public string? Status { get; init; }
|
||||||
|
}
|
||||||
@@ -18,6 +18,11 @@ public sealed class ProductSpecTemplate : MultiTenantEntityBase
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public string Name { get; set; } = string.Empty;
|
public string Name { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 模板描述。
|
||||||
|
/// </summary>
|
||||||
|
public string Description { get; set; } = string.Empty;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 模板类型。
|
/// 模板类型。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -33,6 +38,16 @@ public sealed class ProductSpecTemplate : MultiTenantEntityBase
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public bool IsRequired { get; set; } = true;
|
public bool IsRequired { get; set; } = true;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 最小可选数。
|
||||||
|
/// </summary>
|
||||||
|
public int MinSelect { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 最大可选数。
|
||||||
|
/// </summary>
|
||||||
|
public int MaxSelect { get; set; } = 1;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 排序值。
|
/// 排序值。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -22,6 +22,16 @@ public sealed class ProductSpecTemplateOption : MultiTenantEntityBase
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public decimal ExtraPrice { get; set; }
|
public decimal ExtraPrice { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 库存数量。
|
||||||
|
/// </summary>
|
||||||
|
public int Stock { get; set; } = 999;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否启用。
|
||||||
|
/// </summary>
|
||||||
|
public bool IsEnabled { get; set; } = true;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 排序值。
|
/// 排序值。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -13,5 +13,10 @@ public enum ProductSpecTemplateType
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 做法模板。
|
/// 做法模板。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
Method = 1
|
Method = 1,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 加料模板。
|
||||||
|
/// </summary>
|
||||||
|
Addon = 2
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,16 +48,31 @@ public interface IProductRepository
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
Task<IReadOnlyList<ProductSpecTemplate>> GetSpecTemplatesByStoreAsync(long tenantId, long storeId, CancellationToken cancellationToken = default);
|
Task<IReadOnlyList<ProductSpecTemplate>> GetSpecTemplatesByStoreAsync(long tenantId, long storeId, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 按门店读取加料模板。
|
||||||
|
/// </summary>
|
||||||
|
Task<IReadOnlyList<ProductSpecTemplate>> GetAddonTemplatesByStoreAsync(long tenantId, long storeId, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 依据标识读取规格做法模板。
|
/// 依据标识读取规格做法模板。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
Task<ProductSpecTemplate?> FindSpecTemplateByIdAsync(long templateId, long tenantId, CancellationToken cancellationToken = default);
|
Task<ProductSpecTemplate?> FindSpecTemplateByIdAsync(long templateId, long tenantId, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 依据标识读取加料模板。
|
||||||
|
/// </summary>
|
||||||
|
Task<ProductSpecTemplate?> FindAddonTemplateByIdAsync(long templateId, long tenantId, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 判断门店内模板名称是否已存在。
|
/// 判断门店内模板名称是否已存在。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
Task<bool> ExistsSpecTemplateNameAsync(long tenantId, long storeId, string name, long? excludeTemplateId = null, CancellationToken cancellationToken = default);
|
Task<bool> ExistsSpecTemplateNameAsync(long tenantId, long storeId, string name, long? excludeTemplateId = null, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 判断门店内加料模板名称是否已存在。
|
||||||
|
/// </summary>
|
||||||
|
Task<bool> ExistsAddonTemplateNameAsync(long tenantId, long storeId, string name, long? excludeTemplateId = null, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 按模板读取规格做法选项。
|
/// 按模板读取规格做法选项。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -1185,12 +1185,15 @@ public sealed class TakeoutAppDbContext(
|
|||||||
builder.HasKey(x => x.Id);
|
builder.HasKey(x => x.Id);
|
||||||
builder.Property(x => x.StoreId).IsRequired();
|
builder.Property(x => x.StoreId).IsRequired();
|
||||||
builder.Property(x => x.Name).HasMaxLength(64).IsRequired();
|
builder.Property(x => x.Name).HasMaxLength(64).IsRequired();
|
||||||
|
builder.Property(x => x.Description).HasMaxLength(256).IsRequired();
|
||||||
builder.Property(x => x.TemplateType).HasConversion<int>();
|
builder.Property(x => x.TemplateType).HasConversion<int>();
|
||||||
builder.Property(x => x.SelectionType).HasConversion<int>();
|
builder.Property(x => x.SelectionType).HasConversion<int>();
|
||||||
|
builder.Property(x => x.MinSelect).IsRequired();
|
||||||
|
builder.Property(x => x.MaxSelect).IsRequired();
|
||||||
builder.Property(x => x.SortOrder).IsRequired();
|
builder.Property(x => x.SortOrder).IsRequired();
|
||||||
builder.Property(x => x.IsEnabled).IsRequired();
|
builder.Property(x => x.IsEnabled).IsRequired();
|
||||||
builder.Property(x => x.IsRequired).IsRequired();
|
builder.Property(x => x.IsRequired).IsRequired();
|
||||||
builder.HasIndex(x => new { x.TenantId, x.StoreId, x.Name }).IsUnique();
|
builder.HasIndex(x => new { x.TenantId, x.StoreId, x.TemplateType, x.Name }).IsUnique();
|
||||||
builder.HasIndex(x => new { x.TenantId, x.StoreId, x.TemplateType, x.IsEnabled });
|
builder.HasIndex(x => new { x.TenantId, x.StoreId, x.TemplateType, x.IsEnabled });
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1201,6 +1204,8 @@ public sealed class TakeoutAppDbContext(
|
|||||||
builder.Property(x => x.TemplateId).IsRequired();
|
builder.Property(x => x.TemplateId).IsRequired();
|
||||||
builder.Property(x => x.Name).HasMaxLength(64).IsRequired();
|
builder.Property(x => x.Name).HasMaxLength(64).IsRequired();
|
||||||
builder.Property(x => x.ExtraPrice).HasPrecision(18, 2);
|
builder.Property(x => x.ExtraPrice).HasPrecision(18, 2);
|
||||||
|
builder.Property(x => x.Stock).IsRequired();
|
||||||
|
builder.Property(x => x.IsEnabled).IsRequired();
|
||||||
builder.Property(x => x.SortOrder).IsRequired();
|
builder.Property(x => x.SortOrder).IsRequired();
|
||||||
builder.HasIndex(x => new { x.TenantId, x.TemplateId, x.Name }).IsUnique();
|
builder.HasIndex(x => new { x.TenantId, x.TemplateId, x.Name }).IsUnique();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -138,7 +138,24 @@ public sealed class EfProductRepository(TakeoutAppDbContext context) : IProductR
|
|||||||
{
|
{
|
||||||
return await context.ProductSpecTemplates
|
return await context.ProductSpecTemplates
|
||||||
.AsNoTracking()
|
.AsNoTracking()
|
||||||
.Where(x => x.TenantId == tenantId && x.StoreId == storeId)
|
.Where(x =>
|
||||||
|
x.TenantId == tenantId &&
|
||||||
|
x.StoreId == storeId &&
|
||||||
|
x.TemplateType != ProductSpecTemplateType.Addon)
|
||||||
|
.OrderBy(x => x.SortOrder)
|
||||||
|
.ThenBy(x => x.Id)
|
||||||
|
.ToListAsync(cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public async Task<IReadOnlyList<ProductSpecTemplate>> GetAddonTemplatesByStoreAsync(long tenantId, long storeId, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
return await context.ProductSpecTemplates
|
||||||
|
.AsNoTracking()
|
||||||
|
.Where(x =>
|
||||||
|
x.TenantId == tenantId &&
|
||||||
|
x.StoreId == storeId &&
|
||||||
|
x.TemplateType == ProductSpecTemplateType.Addon)
|
||||||
.OrderBy(x => x.SortOrder)
|
.OrderBy(x => x.SortOrder)
|
||||||
.ThenBy(x => x.Id)
|
.ThenBy(x => x.Id)
|
||||||
.ToListAsync(cancellationToken);
|
.ToListAsync(cancellationToken);
|
||||||
@@ -148,7 +165,21 @@ public sealed class EfProductRepository(TakeoutAppDbContext context) : IProductR
|
|||||||
public Task<ProductSpecTemplate?> FindSpecTemplateByIdAsync(long templateId, long tenantId, CancellationToken cancellationToken = default)
|
public Task<ProductSpecTemplate?> FindSpecTemplateByIdAsync(long templateId, long tenantId, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
return context.ProductSpecTemplates
|
return context.ProductSpecTemplates
|
||||||
.Where(x => x.TenantId == tenantId && x.Id == templateId)
|
.Where(x =>
|
||||||
|
x.TenantId == tenantId &&
|
||||||
|
x.Id == templateId &&
|
||||||
|
x.TemplateType != ProductSpecTemplateType.Addon)
|
||||||
|
.FirstOrDefaultAsync(cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public Task<ProductSpecTemplate?> FindAddonTemplateByIdAsync(long templateId, long tenantId, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
return context.ProductSpecTemplates
|
||||||
|
.Where(x =>
|
||||||
|
x.TenantId == tenantId &&
|
||||||
|
x.Id == templateId &&
|
||||||
|
x.TemplateType == ProductSpecTemplateType.Addon)
|
||||||
.FirstOrDefaultAsync(cancellationToken);
|
.FirstOrDefaultAsync(cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -162,6 +193,28 @@ public sealed class EfProductRepository(TakeoutAppDbContext context) : IProductR
|
|||||||
.Where(x =>
|
.Where(x =>
|
||||||
x.TenantId == tenantId &&
|
x.TenantId == tenantId &&
|
||||||
x.StoreId == storeId &&
|
x.StoreId == storeId &&
|
||||||
|
x.TemplateType != ProductSpecTemplateType.Addon &&
|
||||||
|
x.Name.ToLower() == normalizedLower);
|
||||||
|
|
||||||
|
if (excludeTemplateId.HasValue)
|
||||||
|
{
|
||||||
|
query = query.Where(x => x.Id != excludeTemplateId.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return query.AnyAsync(cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public Task<bool> ExistsAddonTemplateNameAsync(long tenantId, long storeId, string name, long? excludeTemplateId = null, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
var normalizedName = (name ?? string.Empty).Trim();
|
||||||
|
var normalizedLower = normalizedName.ToLowerInvariant();
|
||||||
|
var query = context.ProductSpecTemplates
|
||||||
|
.AsNoTracking()
|
||||||
|
.Where(x =>
|
||||||
|
x.TenantId == tenantId &&
|
||||||
|
x.StoreId == storeId &&
|
||||||
|
x.TemplateType == ProductSpecTemplateType.Addon &&
|
||||||
x.Name.ToLower() == normalizedLower);
|
x.Name.ToLower() == normalizedLower);
|
||||||
|
|
||||||
if (excludeTemplateId.HasValue)
|
if (excludeTemplateId.HasValue)
|
||||||
|
|||||||
@@ -0,0 +1,99 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace TakeoutSaaS.Infrastructure.Migrations
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
public partial class ExtendProductSpecTemplateForAddonGroups : Migration
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.DropIndex(
|
||||||
|
name: "IX_product_spec_templates_TenantId_StoreId_Name",
|
||||||
|
table: "product_spec_templates");
|
||||||
|
|
||||||
|
migrationBuilder.AddColumn<string>(
|
||||||
|
name: "Description",
|
||||||
|
table: "product_spec_templates",
|
||||||
|
type: "character varying(256)",
|
||||||
|
maxLength: 256,
|
||||||
|
nullable: false,
|
||||||
|
defaultValue: "",
|
||||||
|
comment: "模板描述。");
|
||||||
|
|
||||||
|
migrationBuilder.AddColumn<int>(
|
||||||
|
name: "MaxSelect",
|
||||||
|
table: "product_spec_templates",
|
||||||
|
type: "integer",
|
||||||
|
nullable: false,
|
||||||
|
defaultValue: 1,
|
||||||
|
comment: "最大可选数。");
|
||||||
|
|
||||||
|
migrationBuilder.AddColumn<int>(
|
||||||
|
name: "MinSelect",
|
||||||
|
table: "product_spec_templates",
|
||||||
|
type: "integer",
|
||||||
|
nullable: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
comment: "最小可选数。");
|
||||||
|
|
||||||
|
migrationBuilder.AddColumn<bool>(
|
||||||
|
name: "IsEnabled",
|
||||||
|
table: "product_spec_template_options",
|
||||||
|
type: "boolean",
|
||||||
|
nullable: false,
|
||||||
|
defaultValue: true,
|
||||||
|
comment: "是否启用。");
|
||||||
|
|
||||||
|
migrationBuilder.AddColumn<int>(
|
||||||
|
name: "Stock",
|
||||||
|
table: "product_spec_template_options",
|
||||||
|
type: "integer",
|
||||||
|
nullable: false,
|
||||||
|
defaultValue: 999,
|
||||||
|
comment: "库存数量。");
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "IX_product_spec_templates_TenantId_StoreId_TemplateType_Name",
|
||||||
|
table: "product_spec_templates",
|
||||||
|
columns: new[] { "TenantId", "StoreId", "TemplateType", "Name" },
|
||||||
|
unique: true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.DropIndex(
|
||||||
|
name: "IX_product_spec_templates_TenantId_StoreId_TemplateType_Name",
|
||||||
|
table: "product_spec_templates");
|
||||||
|
|
||||||
|
migrationBuilder.DropColumn(
|
||||||
|
name: "Description",
|
||||||
|
table: "product_spec_templates");
|
||||||
|
|
||||||
|
migrationBuilder.DropColumn(
|
||||||
|
name: "MaxSelect",
|
||||||
|
table: "product_spec_templates");
|
||||||
|
|
||||||
|
migrationBuilder.DropColumn(
|
||||||
|
name: "MinSelect",
|
||||||
|
table: "product_spec_templates");
|
||||||
|
|
||||||
|
migrationBuilder.DropColumn(
|
||||||
|
name: "IsEnabled",
|
||||||
|
table: "product_spec_template_options");
|
||||||
|
|
||||||
|
migrationBuilder.DropColumn(
|
||||||
|
name: "Stock",
|
||||||
|
table: "product_spec_template_options");
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "IX_product_spec_templates_TenantId_StoreId_Name",
|
||||||
|
table: "product_spec_templates",
|
||||||
|
columns: new[] { "TenantId", "StoreId", "Name" },
|
||||||
|
unique: true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4689,6 +4689,12 @@ namespace TakeoutSaaS.Infrastructure.Migrations
|
|||||||
.HasColumnType("bigint")
|
.HasColumnType("bigint")
|
||||||
.HasComment("删除人用户标识(软删除),未删除时为 null。");
|
.HasComment("删除人用户标识(软删除),未删除时为 null。");
|
||||||
|
|
||||||
|
b.Property<string>("Description")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(256)
|
||||||
|
.HasColumnType("character varying(256)")
|
||||||
|
.HasComment("模板描述。");
|
||||||
|
|
||||||
b.Property<bool>("IsEnabled")
|
b.Property<bool>("IsEnabled")
|
||||||
.HasColumnType("boolean")
|
.HasColumnType("boolean")
|
||||||
.HasComment("是否启用。");
|
.HasComment("是否启用。");
|
||||||
@@ -4697,12 +4703,20 @@ namespace TakeoutSaaS.Infrastructure.Migrations
|
|||||||
.HasColumnType("boolean")
|
.HasColumnType("boolean")
|
||||||
.HasComment("是否必选。");
|
.HasComment("是否必选。");
|
||||||
|
|
||||||
|
b.Property<int>("MaxSelect")
|
||||||
|
.HasColumnType("integer")
|
||||||
|
.HasComment("最大可选数。");
|
||||||
|
|
||||||
b.Property<string>("Name")
|
b.Property<string>("Name")
|
||||||
.IsRequired()
|
.IsRequired()
|
||||||
.HasMaxLength(64)
|
.HasMaxLength(64)
|
||||||
.HasColumnType("character varying(64)")
|
.HasColumnType("character varying(64)")
|
||||||
.HasComment("模板名称。");
|
.HasComment("模板名称。");
|
||||||
|
|
||||||
|
b.Property<int>("MinSelect")
|
||||||
|
.HasColumnType("integer")
|
||||||
|
.HasComment("最小可选数。");
|
||||||
|
|
||||||
b.Property<int>("SelectionType")
|
b.Property<int>("SelectionType")
|
||||||
.HasColumnType("integer")
|
.HasColumnType("integer")
|
||||||
.HasComment("选择方式。");
|
.HasComment("选择方式。");
|
||||||
@@ -4733,7 +4747,7 @@ namespace TakeoutSaaS.Infrastructure.Migrations
|
|||||||
|
|
||||||
b.HasKey("Id");
|
b.HasKey("Id");
|
||||||
|
|
||||||
b.HasIndex("TenantId", "StoreId", "Name")
|
b.HasIndex("TenantId", "StoreId", "TemplateType", "Name")
|
||||||
.IsUnique();
|
.IsUnique();
|
||||||
|
|
||||||
b.HasIndex("TenantId", "StoreId", "TemplateType", "IsEnabled");
|
b.HasIndex("TenantId", "StoreId", "TemplateType", "IsEnabled");
|
||||||
@@ -4774,6 +4788,10 @@ namespace TakeoutSaaS.Infrastructure.Migrations
|
|||||||
.HasColumnType("numeric(18,2)")
|
.HasColumnType("numeric(18,2)")
|
||||||
.HasComment("附加价格。");
|
.HasComment("附加价格。");
|
||||||
|
|
||||||
|
b.Property<bool>("IsEnabled")
|
||||||
|
.HasColumnType("boolean")
|
||||||
|
.HasComment("是否启用。");
|
||||||
|
|
||||||
b.Property<string>("Name")
|
b.Property<string>("Name")
|
||||||
.IsRequired()
|
.IsRequired()
|
||||||
.HasMaxLength(64)
|
.HasMaxLength(64)
|
||||||
@@ -4784,6 +4802,10 @@ namespace TakeoutSaaS.Infrastructure.Migrations
|
|||||||
.HasColumnType("integer")
|
.HasColumnType("integer")
|
||||||
.HasComment("排序值。");
|
.HasComment("排序值。");
|
||||||
|
|
||||||
|
b.Property<int>("Stock")
|
||||||
|
.HasColumnType("integer")
|
||||||
|
.HasComment("库存数量。");
|
||||||
|
|
||||||
b.Property<long>("TemplateId")
|
b.Property<long>("TemplateId")
|
||||||
.HasColumnType("bigint")
|
.HasColumnType("bigint")
|
||||||
.HasComment("模板 ID。");
|
.HasComment("模板 ID。");
|
||||||
|
|||||||
Reference in New Issue
Block a user