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,254 @@
|
|||||||
|
namespace TakeoutSaaS.TenantApi.Contracts.Product;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 规格做法列表查询请求。
|
||||||
|
/// </summary>
|
||||||
|
public sealed class ProductSpecListRequest
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 门店 ID。
|
||||||
|
/// </summary>
|
||||||
|
public string StoreId { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 关键字。
|
||||||
|
/// </summary>
|
||||||
|
public string? Keyword { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 模板类型(spec/method)。
|
||||||
|
/// </summary>
|
||||||
|
public string? Type { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 状态(enabled/disabled)。
|
||||||
|
/// </summary>
|
||||||
|
public string? Status { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 保存规格做法模板请求。
|
||||||
|
/// </summary>
|
||||||
|
public sealed class SaveProductSpecRequest
|
||||||
|
{
|
||||||
|
/// <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>
|
||||||
|
/// 模板类型(spec/method)。
|
||||||
|
/// </summary>
|
||||||
|
public string Type { get; set; } = "spec";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 选择方式(single/multi)。
|
||||||
|
/// </summary>
|
||||||
|
public string SelectionType { get; set; } = "single";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否必选。
|
||||||
|
/// </summary>
|
||||||
|
public bool IsRequired { get; set; } = true;
|
||||||
|
|
||||||
|
/// <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<SaveProductSpecValueRequest> Values { get; set; } = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 保存规格做法模板选项请求。
|
||||||
|
/// </summary>
|
||||||
|
public sealed class SaveProductSpecValueRequest
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 选项 ID(编辑时传)。
|
||||||
|
/// </summary>
|
||||||
|
public string? Id { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 选项名称。
|
||||||
|
/// </summary>
|
||||||
|
public string Name { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 附加价格。
|
||||||
|
/// </summary>
|
||||||
|
public decimal ExtraPrice { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 排序值。
|
||||||
|
/// </summary>
|
||||||
|
public int Sort { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 删除规格做法模板请求。
|
||||||
|
/// </summary>
|
||||||
|
public sealed class DeleteProductSpecRequest
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 门店 ID。
|
||||||
|
/// </summary>
|
||||||
|
public string StoreId { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 模板 ID。
|
||||||
|
/// </summary>
|
||||||
|
public string SpecId { get; set; } = string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 规格做法模板状态变更请求。
|
||||||
|
/// </summary>
|
||||||
|
public sealed class ChangeProductSpecStatusRequest
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 门店 ID。
|
||||||
|
/// </summary>
|
||||||
|
public string StoreId { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 模板 ID。
|
||||||
|
/// </summary>
|
||||||
|
public string SpecId { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 状态(enabled/disabled)。
|
||||||
|
/// </summary>
|
||||||
|
public string Status { get; set; } = "enabled";
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 复制规格做法模板请求。
|
||||||
|
/// </summary>
|
||||||
|
public sealed class CopyProductSpecRequest
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 门店 ID。
|
||||||
|
/// </summary>
|
||||||
|
public string StoreId { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 模板 ID。
|
||||||
|
/// </summary>
|
||||||
|
public string SpecId { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 新模板名称(可选)。
|
||||||
|
/// </summary>
|
||||||
|
public string? NewName { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 规格做法模板选项响应。
|
||||||
|
/// </summary>
|
||||||
|
public sealed class ProductSpecValueResponse
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 选项 ID。
|
||||||
|
/// </summary>
|
||||||
|
public string Id { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 选项名称。
|
||||||
|
/// </summary>
|
||||||
|
public string Name { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 附加价格。
|
||||||
|
/// </summary>
|
||||||
|
public decimal ExtraPrice { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 排序值。
|
||||||
|
/// </summary>
|
||||||
|
public int Sort { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 规格做法模板列表项响应。
|
||||||
|
/// </summary>
|
||||||
|
public sealed class ProductSpecItemResponse
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 模板 ID。
|
||||||
|
/// </summary>
|
||||||
|
public string Id { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 模板名称。
|
||||||
|
/// </summary>
|
||||||
|
public string Name { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 模板类型(spec/method)。
|
||||||
|
/// </summary>
|
||||||
|
public string Type { get; set; } = "spec";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 选择方式(single/multi)。
|
||||||
|
/// </summary>
|
||||||
|
public string SelectionType { get; set; } = "single";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否必选。
|
||||||
|
/// </summary>
|
||||||
|
public bool IsRequired { 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<ProductSpecValueResponse> Values { get; set; } = [];
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 更新时间。
|
||||||
|
/// </summary>
|
||||||
|
public string UpdatedAt { get; set; } = string.Empty;
|
||||||
|
}
|
||||||
@@ -0,0 +1,182 @@
|
|||||||
|
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 ProductSpecController(
|
||||||
|
IMediator mediator,
|
||||||
|
TakeoutAppDbContext dbContext,
|
||||||
|
StoreContextService storeContextService) : BaseApiController
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 规格做法模板列表。
|
||||||
|
/// </summary>
|
||||||
|
[HttpGet("spec/list")]
|
||||||
|
[ProducesResponseType(typeof(ApiResponse<IReadOnlyList<ProductSpecItemResponse>>), StatusCodes.Status200OK)]
|
||||||
|
public async Task<ApiResponse<IReadOnlyList<ProductSpecItemResponse>>> GetSpecList(
|
||||||
|
[FromQuery] ProductSpecListRequest request,
|
||||||
|
CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var storeId = StoreApiHelpers.ParseRequiredSnowflake(request.StoreId, nameof(request.StoreId));
|
||||||
|
await EnsureStoreAccessibleAsync(storeId, cancellationToken);
|
||||||
|
|
||||||
|
var result = await mediator.Send(new GetProductSpecTemplateListQuery
|
||||||
|
{
|
||||||
|
StoreId = storeId,
|
||||||
|
Keyword = request.Keyword,
|
||||||
|
Type = request.Type,
|
||||||
|
Status = request.Status
|
||||||
|
}, cancellationToken);
|
||||||
|
|
||||||
|
return ApiResponse<IReadOnlyList<ProductSpecItemResponse>>.Ok(result.Select(MapSpecItem).ToList());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 保存规格做法模板。
|
||||||
|
/// </summary>
|
||||||
|
[HttpPost("spec/save")]
|
||||||
|
[ProducesResponseType(typeof(ApiResponse<ProductSpecItemResponse>), StatusCodes.Status200OK)]
|
||||||
|
public async Task<ApiResponse<ProductSpecItemResponse>> SaveSpec(
|
||||||
|
[FromBody] SaveProductSpecRequest request,
|
||||||
|
CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var storeId = StoreApiHelpers.ParseRequiredSnowflake(request.StoreId, nameof(request.StoreId));
|
||||||
|
await EnsureStoreAccessibleAsync(storeId, cancellationToken);
|
||||||
|
|
||||||
|
var result = await mediator.Send(new SaveProductSpecTemplateCommand
|
||||||
|
{
|
||||||
|
StoreId = storeId,
|
||||||
|
SpecId = StoreApiHelpers.ParseSnowflakeOrNull(request.Id),
|
||||||
|
Name = request.Name,
|
||||||
|
Type = request.Type,
|
||||||
|
SelectionType = request.SelectionType,
|
||||||
|
IsRequired = request.IsRequired,
|
||||||
|
Sort = request.Sort,
|
||||||
|
Status = request.Status,
|
||||||
|
ProductIds = StoreApiHelpers.ParseSnowflakeList(request.ProductIds),
|
||||||
|
Values = (request.Values ?? [])
|
||||||
|
.Select(item => new SaveProductSpecTemplateValueCommand
|
||||||
|
{
|
||||||
|
Id = StoreApiHelpers.ParseSnowflakeOrNull(item.Id),
|
||||||
|
Name = item.Name,
|
||||||
|
ExtraPrice = item.ExtraPrice,
|
||||||
|
Sort = item.Sort
|
||||||
|
})
|
||||||
|
.ToList()
|
||||||
|
}, cancellationToken);
|
||||||
|
|
||||||
|
return ApiResponse<ProductSpecItemResponse>.Ok(MapSpecItem(result));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 删除规格做法模板。
|
||||||
|
/// </summary>
|
||||||
|
[HttpPost("spec/delete")]
|
||||||
|
[ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status200OK)]
|
||||||
|
public async Task<ApiResponse<object>> DeleteSpec(
|
||||||
|
[FromBody] DeleteProductSpecRequest request,
|
||||||
|
CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var storeId = StoreApiHelpers.ParseRequiredSnowflake(request.StoreId, nameof(request.StoreId));
|
||||||
|
await EnsureStoreAccessibleAsync(storeId, cancellationToken);
|
||||||
|
|
||||||
|
await mediator.Send(new DeleteProductSpecTemplateCommand
|
||||||
|
{
|
||||||
|
StoreId = storeId,
|
||||||
|
SpecId = StoreApiHelpers.ParseRequiredSnowflake(request.SpecId, nameof(request.SpecId))
|
||||||
|
}, cancellationToken);
|
||||||
|
|
||||||
|
return ApiResponse<object>.Ok(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 修改规格做法模板状态。
|
||||||
|
/// </summary>
|
||||||
|
[HttpPost("spec/status")]
|
||||||
|
[ProducesResponseType(typeof(ApiResponse<ProductSpecItemResponse>), StatusCodes.Status200OK)]
|
||||||
|
public async Task<ApiResponse<ProductSpecItemResponse>> ChangeSpecStatus(
|
||||||
|
[FromBody] ChangeProductSpecStatusRequest request,
|
||||||
|
CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var storeId = StoreApiHelpers.ParseRequiredSnowflake(request.StoreId, nameof(request.StoreId));
|
||||||
|
await EnsureStoreAccessibleAsync(storeId, cancellationToken);
|
||||||
|
|
||||||
|
var result = await mediator.Send(new ChangeProductSpecTemplateStatusCommand
|
||||||
|
{
|
||||||
|
StoreId = storeId,
|
||||||
|
SpecId = StoreApiHelpers.ParseRequiredSnowflake(request.SpecId, nameof(request.SpecId)),
|
||||||
|
Status = request.Status
|
||||||
|
}, cancellationToken);
|
||||||
|
|
||||||
|
return ApiResponse<ProductSpecItemResponse>.Ok(MapSpecItem(result));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 复制规格做法模板。
|
||||||
|
/// </summary>
|
||||||
|
[HttpPost("spec/copy")]
|
||||||
|
[ProducesResponseType(typeof(ApiResponse<ProductSpecItemResponse>), StatusCodes.Status200OK)]
|
||||||
|
public async Task<ApiResponse<ProductSpecItemResponse>> CopySpec(
|
||||||
|
[FromBody] CopyProductSpecRequest request,
|
||||||
|
CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var storeId = StoreApiHelpers.ParseRequiredSnowflake(request.StoreId, nameof(request.StoreId));
|
||||||
|
await EnsureStoreAccessibleAsync(storeId, cancellationToken);
|
||||||
|
|
||||||
|
var result = await mediator.Send(new CopyProductSpecTemplateCommand
|
||||||
|
{
|
||||||
|
StoreId = storeId,
|
||||||
|
SpecId = StoreApiHelpers.ParseRequiredSnowflake(request.SpecId, nameof(request.SpecId)),
|
||||||
|
NewName = request.NewName
|
||||||
|
}, cancellationToken);
|
||||||
|
|
||||||
|
return ApiResponse<ProductSpecItemResponse>.Ok(MapSpecItem(result));
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task EnsureStoreAccessibleAsync(long storeId, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var (tenantId, merchantId) = StoreApiHelpers.GetTenantMerchantContext(storeContextService);
|
||||||
|
await StoreApiHelpers.EnsureStoreAccessibleAsync(dbContext, tenantId, merchantId, storeId, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ProductSpecItemResponse MapSpecItem(ProductSpecTemplateItemDto source)
|
||||||
|
{
|
||||||
|
return new ProductSpecItemResponse
|
||||||
|
{
|
||||||
|
Id = source.Id.ToString(),
|
||||||
|
Name = source.Name,
|
||||||
|
Type = source.Type,
|
||||||
|
SelectionType = source.SelectionType,
|
||||||
|
IsRequired = source.IsRequired,
|
||||||
|
Sort = source.Sort,
|
||||||
|
Status = source.Status,
|
||||||
|
ProductCount = source.ProductCount,
|
||||||
|
ProductIds = source.ProductIds.Select(item => item.ToString()).ToList(),
|
||||||
|
Values = source.Values
|
||||||
|
.Select(item => new ProductSpecValueResponse
|
||||||
|
{
|
||||||
|
Id = item.Id.ToString(),
|
||||||
|
Name = item.Name,
|
||||||
|
ExtraPrice = item.ExtraPrice,
|
||||||
|
Sort = item.Sort
|
||||||
|
})
|
||||||
|
.ToList(),
|
||||||
|
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 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; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
using TakeoutSaaS.Domain.Products.Enums;
|
||||||
|
using TakeoutSaaS.Shared.Abstractions.Entities;
|
||||||
|
|
||||||
|
namespace TakeoutSaaS.Domain.Products.Entities;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 门店规格做法模板。
|
||||||
|
/// </summary>
|
||||||
|
public sealed class ProductSpecTemplate : MultiTenantEntityBase
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 所属门店。
|
||||||
|
/// </summary>
|
||||||
|
public long StoreId { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 模板名称。
|
||||||
|
/// </summary>
|
||||||
|
public string Name { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 模板类型。
|
||||||
|
/// </summary>
|
||||||
|
public ProductSpecTemplateType TemplateType { get; set; } = ProductSpecTemplateType.Spec;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 选择方式。
|
||||||
|
/// </summary>
|
||||||
|
public AttributeSelectionType SelectionType { get; set; } = AttributeSelectionType.Single;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否必选。
|
||||||
|
/// </summary>
|
||||||
|
public bool IsRequired { get; set; } = true;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 排序值。
|
||||||
|
/// </summary>
|
||||||
|
public int SortOrder { get; set; } = 100;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否启用。
|
||||||
|
/// </summary>
|
||||||
|
public bool IsEnabled { get; set; } = true;
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
using TakeoutSaaS.Shared.Abstractions.Entities;
|
||||||
|
|
||||||
|
namespace TakeoutSaaS.Domain.Products.Entities;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 规格做法模板选项。
|
||||||
|
/// </summary>
|
||||||
|
public sealed class ProductSpecTemplateOption : MultiTenantEntityBase
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 模板 ID。
|
||||||
|
/// </summary>
|
||||||
|
public long TemplateId { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 选项名称。
|
||||||
|
/// </summary>
|
||||||
|
public string Name { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 附加价格。
|
||||||
|
/// </summary>
|
||||||
|
public decimal ExtraPrice { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 排序值。
|
||||||
|
/// </summary>
|
||||||
|
public int SortOrder { get; set; } = 100;
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
using TakeoutSaaS.Shared.Abstractions.Entities;
|
||||||
|
|
||||||
|
namespace TakeoutSaaS.Domain.Products.Entities;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 规格做法模板与商品关联。
|
||||||
|
/// </summary>
|
||||||
|
public sealed class ProductSpecTemplateProduct : MultiTenantEntityBase
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 所属门店。
|
||||||
|
/// </summary>
|
||||||
|
public long StoreId { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 模板 ID。
|
||||||
|
/// </summary>
|
||||||
|
public long TemplateId { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 商品 ID。
|
||||||
|
/// </summary>
|
||||||
|
public long ProductId { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
namespace TakeoutSaaS.Domain.Products.Enums;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 规格做法模板类型。
|
||||||
|
/// </summary>
|
||||||
|
public enum ProductSpecTemplateType
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 规格模板。
|
||||||
|
/// </summary>
|
||||||
|
Spec = 0,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 做法模板。
|
||||||
|
/// </summary>
|
||||||
|
Method = 1
|
||||||
|
}
|
||||||
@@ -43,6 +43,36 @@ public interface IProductRepository
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
Task<Dictionary<long, int>> CountProductsByCategoryIdsAsync(long tenantId, long storeId, IReadOnlyCollection<long> categoryIds, CancellationToken cancellationToken = default);
|
Task<Dictionary<long, int>> CountProductsByCategoryIdsAsync(long tenantId, long storeId, IReadOnlyCollection<long> categoryIds, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 按门店读取规格做法模板。
|
||||||
|
/// </summary>
|
||||||
|
Task<IReadOnlyList<ProductSpecTemplate>> GetSpecTemplatesByStoreAsync(long tenantId, long storeId, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 依据标识读取规格做法模板。
|
||||||
|
/// </summary>
|
||||||
|
Task<ProductSpecTemplate?> FindSpecTemplateByIdAsync(long templateId, long tenantId, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 判断门店内模板名称是否已存在。
|
||||||
|
/// </summary>
|
||||||
|
Task<bool> ExistsSpecTemplateNameAsync(long tenantId, long storeId, string name, long? excludeTemplateId = null, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 按模板读取规格做法选项。
|
||||||
|
/// </summary>
|
||||||
|
Task<IReadOnlyList<ProductSpecTemplateOption>> GetSpecTemplateOptionsByTemplateIdsAsync(IReadOnlyCollection<long> templateIds, long tenantId, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 按模板读取模板关联商品。
|
||||||
|
/// </summary>
|
||||||
|
Task<IReadOnlyList<ProductSpecTemplateProduct>> GetSpecTemplateProductsByTemplateIdsAsync(IReadOnlyCollection<long> templateIds, long tenantId, long storeId, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 过滤门店内真实存在的商品 ID。
|
||||||
|
/// </summary>
|
||||||
|
Task<IReadOnlyList<long>> FilterExistingProductIdsAsync(long tenantId, long storeId, IReadOnlyCollection<long> productIds, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 查询商品选择器列表。
|
/// 查询商品选择器列表。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -160,6 +190,21 @@ public interface IProductRepository
|
|||||||
/// <returns>异步任务。</returns>
|
/// <returns>异步任务。</returns>
|
||||||
Task AddCategoryAsync(ProductCategory category, CancellationToken cancellationToken = default);
|
Task AddCategoryAsync(ProductCategory category, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 新增规格做法模板。
|
||||||
|
/// </summary>
|
||||||
|
Task AddSpecTemplateAsync(ProductSpecTemplate template, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 新增规格做法模板选项。
|
||||||
|
/// </summary>
|
||||||
|
Task AddSpecTemplateOptionsAsync(IEnumerable<ProductSpecTemplateOption> options, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 新增规格做法模板关联商品。
|
||||||
|
/// </summary>
|
||||||
|
Task AddSpecTemplateProductsAsync(IEnumerable<ProductSpecTemplateProduct> relations, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 新增商品。
|
/// 新增商品。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -242,6 +287,11 @@ public interface IProductRepository
|
|||||||
/// <returns>异步任务。</returns>
|
/// <returns>异步任务。</returns>
|
||||||
Task UpdateCategoryAsync(ProductCategory category, CancellationToken cancellationToken = default);
|
Task UpdateCategoryAsync(ProductCategory category, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 更新规格做法模板。
|
||||||
|
/// </summary>
|
||||||
|
Task UpdateSpecTemplateAsync(ProductSpecTemplate template, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 删除分类。
|
/// 删除分类。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -251,6 +301,11 @@ public interface IProductRepository
|
|||||||
/// <returns>异步任务。</returns>
|
/// <returns>异步任务。</returns>
|
||||||
Task DeleteCategoryAsync(long categoryId, long tenantId, CancellationToken cancellationToken = default);
|
Task DeleteCategoryAsync(long categoryId, long tenantId, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 删除规格做法模板。
|
||||||
|
/// </summary>
|
||||||
|
Task DeleteSpecTemplateAsync(long templateId, long tenantId, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 删除商品下的 SKU。
|
/// 删除商品下的 SKU。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -260,6 +315,16 @@ public interface IProductRepository
|
|||||||
/// <returns>异步任务。</returns>
|
/// <returns>异步任务。</returns>
|
||||||
Task RemoveSkusAsync(long productId, long tenantId, CancellationToken cancellationToken = default);
|
Task RemoveSkusAsync(long productId, long tenantId, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 删除模板下的规格做法选项。
|
||||||
|
/// </summary>
|
||||||
|
Task RemoveSpecTemplateOptionsAsync(long templateId, long tenantId, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 删除模板关联商品。
|
||||||
|
/// </summary>
|
||||||
|
Task RemoveSpecTemplateProductsAsync(long templateId, long tenantId, long storeId, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 删除商品下的加料组及选项。
|
/// 删除商品下的加料组及选项。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -201,6 +201,18 @@ public sealed class TakeoutAppDbContext(
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public DbSet<ProductAttributeOption> ProductAttributeOptions => Set<ProductAttributeOption>();
|
public DbSet<ProductAttributeOption> ProductAttributeOptions => Set<ProductAttributeOption>();
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
/// 门店规格做法模板。
|
||||||
|
/// </summary>
|
||||||
|
public DbSet<ProductSpecTemplate> ProductSpecTemplates => Set<ProductSpecTemplate>();
|
||||||
|
/// <summary>
|
||||||
|
/// 门店规格做法模板选项。
|
||||||
|
/// </summary>
|
||||||
|
public DbSet<ProductSpecTemplateOption> ProductSpecTemplateOptions => Set<ProductSpecTemplateOption>();
|
||||||
|
/// <summary>
|
||||||
|
/// 门店规格做法模板关联商品。
|
||||||
|
/// </summary>
|
||||||
|
public DbSet<ProductSpecTemplateProduct> ProductSpecTemplateProducts => Set<ProductSpecTemplateProduct>();
|
||||||
|
/// <summary>
|
||||||
/// SKU 实体。
|
/// SKU 实体。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public DbSet<ProductSku> ProductSkus => Set<ProductSku>();
|
public DbSet<ProductSku> ProductSkus => Set<ProductSku>();
|
||||||
@@ -441,6 +453,9 @@ public sealed class TakeoutAppDbContext(
|
|||||||
ConfigureProduct(modelBuilder.Entity<Product>());
|
ConfigureProduct(modelBuilder.Entity<Product>());
|
||||||
ConfigureProductAttributeGroup(modelBuilder.Entity<ProductAttributeGroup>());
|
ConfigureProductAttributeGroup(modelBuilder.Entity<ProductAttributeGroup>());
|
||||||
ConfigureProductAttributeOption(modelBuilder.Entity<ProductAttributeOption>());
|
ConfigureProductAttributeOption(modelBuilder.Entity<ProductAttributeOption>());
|
||||||
|
ConfigureProductSpecTemplate(modelBuilder.Entity<ProductSpecTemplate>());
|
||||||
|
ConfigureProductSpecTemplateOption(modelBuilder.Entity<ProductSpecTemplateOption>());
|
||||||
|
ConfigureProductSpecTemplateProduct(modelBuilder.Entity<ProductSpecTemplateProduct>());
|
||||||
ConfigureProductSku(modelBuilder.Entity<ProductSku>());
|
ConfigureProductSku(modelBuilder.Entity<ProductSku>());
|
||||||
ConfigureProductAddonGroup(modelBuilder.Entity<ProductAddonGroup>());
|
ConfigureProductAddonGroup(modelBuilder.Entity<ProductAddonGroup>());
|
||||||
ConfigureProductAddonOption(modelBuilder.Entity<ProductAddonOption>());
|
ConfigureProductAddonOption(modelBuilder.Entity<ProductAddonOption>());
|
||||||
@@ -1164,6 +1179,43 @@ public sealed class TakeoutAppDbContext(
|
|||||||
builder.HasIndex(x => new { x.TenantId, x.AttributeGroupId, x.Name }).IsUnique();
|
builder.HasIndex(x => new { x.TenantId, x.AttributeGroupId, x.Name }).IsUnique();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void ConfigureProductSpecTemplate(EntityTypeBuilder<ProductSpecTemplate> builder)
|
||||||
|
{
|
||||||
|
builder.ToTable("product_spec_templates");
|
||||||
|
builder.HasKey(x => x.Id);
|
||||||
|
builder.Property(x => x.StoreId).IsRequired();
|
||||||
|
builder.Property(x => x.Name).HasMaxLength(64).IsRequired();
|
||||||
|
builder.Property(x => x.TemplateType).HasConversion<int>();
|
||||||
|
builder.Property(x => x.SelectionType).HasConversion<int>();
|
||||||
|
builder.Property(x => x.SortOrder).IsRequired();
|
||||||
|
builder.Property(x => x.IsEnabled).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.IsEnabled });
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ConfigureProductSpecTemplateOption(EntityTypeBuilder<ProductSpecTemplateOption> builder)
|
||||||
|
{
|
||||||
|
builder.ToTable("product_spec_template_options");
|
||||||
|
builder.HasKey(x => x.Id);
|
||||||
|
builder.Property(x => x.TemplateId).IsRequired();
|
||||||
|
builder.Property(x => x.Name).HasMaxLength(64).IsRequired();
|
||||||
|
builder.Property(x => x.ExtraPrice).HasPrecision(18, 2);
|
||||||
|
builder.Property(x => x.SortOrder).IsRequired();
|
||||||
|
builder.HasIndex(x => new { x.TenantId, x.TemplateId, x.Name }).IsUnique();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ConfigureProductSpecTemplateProduct(EntityTypeBuilder<ProductSpecTemplateProduct> builder)
|
||||||
|
{
|
||||||
|
builder.ToTable("product_spec_template_products");
|
||||||
|
builder.HasKey(x => x.Id);
|
||||||
|
builder.Property(x => x.StoreId).IsRequired();
|
||||||
|
builder.Property(x => x.TemplateId).IsRequired();
|
||||||
|
builder.Property(x => x.ProductId).IsRequired();
|
||||||
|
builder.HasIndex(x => new { x.TenantId, x.StoreId, x.TemplateId, x.ProductId }).IsUnique();
|
||||||
|
builder.HasIndex(x => new { x.TenantId, x.StoreId, x.ProductId });
|
||||||
|
}
|
||||||
|
|
||||||
private static void ConfigureProductSku(EntityTypeBuilder<ProductSku> builder)
|
private static void ConfigureProductSku(EntityTypeBuilder<ProductSku> builder)
|
||||||
{
|
{
|
||||||
builder.ToTable("product_skus");
|
builder.ToTable("product_skus");
|
||||||
|
|||||||
@@ -133,6 +133,98 @@ public sealed class EfProductRepository(TakeoutAppDbContext context) : IProductR
|
|||||||
.ToDictionaryAsync(item => item.CategoryId, item => item.Count, cancellationToken);
|
.ToDictionaryAsync(item => item.CategoryId, item => item.Count, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public async Task<IReadOnlyList<ProductSpecTemplate>> GetSpecTemplatesByStoreAsync(long tenantId, long storeId, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
return await context.ProductSpecTemplates
|
||||||
|
.AsNoTracking()
|
||||||
|
.Where(x => x.TenantId == tenantId && x.StoreId == storeId)
|
||||||
|
.OrderBy(x => x.SortOrder)
|
||||||
|
.ThenBy(x => x.Id)
|
||||||
|
.ToListAsync(cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public Task<ProductSpecTemplate?> FindSpecTemplateByIdAsync(long templateId, long tenantId, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
return context.ProductSpecTemplates
|
||||||
|
.Where(x => x.TenantId == tenantId && x.Id == templateId)
|
||||||
|
.FirstOrDefaultAsync(cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public Task<bool> ExistsSpecTemplateNameAsync(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.Name.ToLower() == normalizedLower);
|
||||||
|
|
||||||
|
if (excludeTemplateId.HasValue)
|
||||||
|
{
|
||||||
|
query = query.Where(x => x.Id != excludeTemplateId.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return query.AnyAsync(cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public async Task<IReadOnlyList<ProductSpecTemplateOption>> GetSpecTemplateOptionsByTemplateIdsAsync(IReadOnlyCollection<long> templateIds, long tenantId, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
if (templateIds.Count == 0)
|
||||||
|
{
|
||||||
|
return Array.Empty<ProductSpecTemplateOption>();
|
||||||
|
}
|
||||||
|
|
||||||
|
return await context.ProductSpecTemplateOptions
|
||||||
|
.AsNoTracking()
|
||||||
|
.Where(x => x.TenantId == tenantId && templateIds.Contains(x.TemplateId))
|
||||||
|
.OrderBy(x => x.SortOrder)
|
||||||
|
.ThenBy(x => x.Id)
|
||||||
|
.ToListAsync(cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public async Task<IReadOnlyList<ProductSpecTemplateProduct>> GetSpecTemplateProductsByTemplateIdsAsync(IReadOnlyCollection<long> templateIds, long tenantId, long storeId, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
if (templateIds.Count == 0)
|
||||||
|
{
|
||||||
|
return Array.Empty<ProductSpecTemplateProduct>();
|
||||||
|
}
|
||||||
|
|
||||||
|
return await context.ProductSpecTemplateProducts
|
||||||
|
.AsNoTracking()
|
||||||
|
.Where(x =>
|
||||||
|
x.TenantId == tenantId &&
|
||||||
|
x.StoreId == storeId &&
|
||||||
|
templateIds.Contains(x.TemplateId))
|
||||||
|
.OrderBy(x => x.TemplateId)
|
||||||
|
.ThenBy(x => x.ProductId)
|
||||||
|
.ToListAsync(cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public async Task<IReadOnlyList<long>> FilterExistingProductIdsAsync(long tenantId, long storeId, IReadOnlyCollection<long> productIds, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
if (productIds.Count == 0)
|
||||||
|
{
|
||||||
|
return Array.Empty<long>();
|
||||||
|
}
|
||||||
|
|
||||||
|
return await context.Products
|
||||||
|
.AsNoTracking()
|
||||||
|
.Where(x =>
|
||||||
|
x.TenantId == tenantId &&
|
||||||
|
x.StoreId == storeId &&
|
||||||
|
productIds.Contains(x.Id))
|
||||||
|
.Select(x => x.Id)
|
||||||
|
.ToListAsync(cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public async Task<IReadOnlyList<Product>> SearchPickerAsync(long tenantId, long storeId, long? categoryId, string? keyword, int limit, CancellationToken cancellationToken = default)
|
public async Task<IReadOnlyList<Product>> SearchPickerAsync(long tenantId, long storeId, long? categoryId, string? keyword, int limit, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
@@ -422,6 +514,24 @@ public sealed class EfProductRepository(TakeoutAppDbContext context) : IProductR
|
|||||||
return context.ProductCategories.AddAsync(category, cancellationToken).AsTask();
|
return context.ProductCategories.AddAsync(category, cancellationToken).AsTask();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public Task AddSpecTemplateAsync(ProductSpecTemplate template, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
return context.ProductSpecTemplates.AddAsync(template, cancellationToken).AsTask();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public Task AddSpecTemplateOptionsAsync(IEnumerable<ProductSpecTemplateOption> options, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
return context.ProductSpecTemplateOptions.AddRangeAsync(options, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public Task AddSpecTemplateProductsAsync(IEnumerable<ProductSpecTemplateProduct> relations, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
return context.ProductSpecTemplateProducts.AddRangeAsync(relations, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public Task AddProductAsync(Product product, CancellationToken cancellationToken = default)
|
public Task AddProductAsync(Product product, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
@@ -503,6 +613,13 @@ public sealed class EfProductRepository(TakeoutAppDbContext context) : IProductR
|
|||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public Task UpdateSpecTemplateAsync(ProductSpecTemplate template, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
context.ProductSpecTemplates.Update(template);
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public async Task DeleteCategoryAsync(long categoryId, long tenantId, CancellationToken cancellationToken = default)
|
public async Task DeleteCategoryAsync(long categoryId, long tenantId, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
@@ -518,6 +635,23 @@ public sealed class EfProductRepository(TakeoutAppDbContext context) : IProductR
|
|||||||
context.ProductCategories.Remove(existing);
|
context.ProductCategories.Remove(existing);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public async Task DeleteSpecTemplateAsync(long templateId, long tenantId, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
var existing = await context.ProductSpecTemplates
|
||||||
|
.Where(x => x.TenantId == tenantId && x.Id == templateId)
|
||||||
|
.FirstOrDefaultAsync(cancellationToken);
|
||||||
|
|
||||||
|
if (existing is null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await context.ProductSpecTemplates
|
||||||
|
.Where(x => x.TenantId == tenantId && x.Id == templateId)
|
||||||
|
.ExecuteDeleteAsync(cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public async Task RemoveSkusAsync(long productId, long tenantId, CancellationToken cancellationToken = default)
|
public async Task RemoveSkusAsync(long productId, long tenantId, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
@@ -533,6 +667,25 @@ public sealed class EfProductRepository(TakeoutAppDbContext context) : IProductR
|
|||||||
context.ProductSkus.RemoveRange(skus);
|
context.ProductSkus.RemoveRange(skus);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public async Task RemoveSpecTemplateOptionsAsync(long templateId, long tenantId, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
await context.ProductSpecTemplateOptions
|
||||||
|
.Where(x => x.TenantId == tenantId && x.TemplateId == templateId)
|
||||||
|
.ExecuteDeleteAsync(cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public async Task RemoveSpecTemplateProductsAsync(long templateId, long tenantId, long storeId, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
await context.ProductSpecTemplateProducts
|
||||||
|
.Where(x =>
|
||||||
|
x.TenantId == tenantId &&
|
||||||
|
x.StoreId == storeId &&
|
||||||
|
x.TemplateId == templateId)
|
||||||
|
.ExecuteDeleteAsync(cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public async Task RemoveAddonGroupsAsync(long productId, long tenantId, CancellationToken cancellationToken = default)
|
public async Task RemoveAddonGroupsAsync(long productId, long tenantId, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
|
|||||||
8075
src/Infrastructure/TakeoutSaaS.Infrastructure/Migrations/20260220112948_AddProductSpecTemplates.Designer.cs
generated
Normal file
8075
src/Infrastructure/TakeoutSaaS.Infrastructure/Migrations/20260220112948_AddProductSpecTemplates.Designer.cs
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,131 @@
|
|||||||
|
using System;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace TakeoutSaaS.Infrastructure.Migrations
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
public partial class AddProductSpecTemplates : Migration
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.CreateTable(
|
||||||
|
name: "product_spec_template_options",
|
||||||
|
columns: table => new
|
||||||
|
{
|
||||||
|
Id = table.Column<long>(type: "bigint", nullable: false, comment: "实体唯一标识。")
|
||||||
|
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
|
||||||
|
TemplateId = table.Column<long>(type: "bigint", nullable: false, comment: "模板 ID。"),
|
||||||
|
Name = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false, comment: "选项名称。"),
|
||||||
|
ExtraPrice = table.Column<decimal>(type: "numeric(18,2)", precision: 18, scale: 2, nullable: false, comment: "附加价格。"),
|
||||||
|
SortOrder = table.Column<int>(type: "integer", nullable: false, comment: "排序值。"),
|
||||||
|
CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false, comment: "创建时间(UTC)。"),
|
||||||
|
UpdatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "最近一次更新时间(UTC),从未更新时为 null。"),
|
||||||
|
DeletedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "软删除时间(UTC),未删除时为 null。"),
|
||||||
|
CreatedBy = table.Column<long>(type: "bigint", nullable: true, comment: "创建人用户标识,匿名或系统操作时为 null。"),
|
||||||
|
UpdatedBy = table.Column<long>(type: "bigint", nullable: true, comment: "最后更新人用户标识,匿名或系统操作时为 null。"),
|
||||||
|
DeletedBy = table.Column<long>(type: "bigint", nullable: true, comment: "删除人用户标识(软删除),未删除时为 null。"),
|
||||||
|
TenantId = table.Column<long>(type: "bigint", nullable: false, comment: "所属租户 ID。")
|
||||||
|
},
|
||||||
|
constraints: table =>
|
||||||
|
{
|
||||||
|
table.PrimaryKey("PK_product_spec_template_options", x => x.Id);
|
||||||
|
},
|
||||||
|
comment: "规格做法模板选项。");
|
||||||
|
|
||||||
|
migrationBuilder.CreateTable(
|
||||||
|
name: "product_spec_template_products",
|
||||||
|
columns: table => new
|
||||||
|
{
|
||||||
|
Id = table.Column<long>(type: "bigint", nullable: false, comment: "实体唯一标识。")
|
||||||
|
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
|
||||||
|
StoreId = table.Column<long>(type: "bigint", nullable: false, comment: "所属门店。"),
|
||||||
|
TemplateId = table.Column<long>(type: "bigint", nullable: false, comment: "模板 ID。"),
|
||||||
|
ProductId = table.Column<long>(type: "bigint", nullable: false, comment: "商品 ID。"),
|
||||||
|
CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false, comment: "创建时间(UTC)。"),
|
||||||
|
UpdatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "最近一次更新时间(UTC),从未更新时为 null。"),
|
||||||
|
DeletedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "软删除时间(UTC),未删除时为 null。"),
|
||||||
|
CreatedBy = table.Column<long>(type: "bigint", nullable: true, comment: "创建人用户标识,匿名或系统操作时为 null。"),
|
||||||
|
UpdatedBy = table.Column<long>(type: "bigint", nullable: true, comment: "最后更新人用户标识,匿名或系统操作时为 null。"),
|
||||||
|
DeletedBy = table.Column<long>(type: "bigint", nullable: true, comment: "删除人用户标识(软删除),未删除时为 null。"),
|
||||||
|
TenantId = table.Column<long>(type: "bigint", nullable: false, comment: "所属租户 ID。")
|
||||||
|
},
|
||||||
|
constraints: table =>
|
||||||
|
{
|
||||||
|
table.PrimaryKey("PK_product_spec_template_products", x => x.Id);
|
||||||
|
},
|
||||||
|
comment: "规格做法模板与商品关联。");
|
||||||
|
|
||||||
|
migrationBuilder.CreateTable(
|
||||||
|
name: "product_spec_templates",
|
||||||
|
columns: table => new
|
||||||
|
{
|
||||||
|
Id = table.Column<long>(type: "bigint", nullable: false, comment: "实体唯一标识。")
|
||||||
|
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
|
||||||
|
StoreId = table.Column<long>(type: "bigint", nullable: false, comment: "所属门店。"),
|
||||||
|
Name = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false, comment: "模板名称。"),
|
||||||
|
TemplateType = table.Column<int>(type: "integer", nullable: false, comment: "模板类型。"),
|
||||||
|
SelectionType = table.Column<int>(type: "integer", nullable: false, comment: "选择方式。"),
|
||||||
|
IsRequired = table.Column<bool>(type: "boolean", nullable: false, comment: "是否必选。"),
|
||||||
|
SortOrder = table.Column<int>(type: "integer", nullable: false, comment: "排序值。"),
|
||||||
|
IsEnabled = table.Column<bool>(type: "boolean", nullable: false, comment: "是否启用。"),
|
||||||
|
CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false, comment: "创建时间(UTC)。"),
|
||||||
|
UpdatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "最近一次更新时间(UTC),从未更新时为 null。"),
|
||||||
|
DeletedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "软删除时间(UTC),未删除时为 null。"),
|
||||||
|
CreatedBy = table.Column<long>(type: "bigint", nullable: true, comment: "创建人用户标识,匿名或系统操作时为 null。"),
|
||||||
|
UpdatedBy = table.Column<long>(type: "bigint", nullable: true, comment: "最后更新人用户标识,匿名或系统操作时为 null。"),
|
||||||
|
DeletedBy = table.Column<long>(type: "bigint", nullable: true, comment: "删除人用户标识(软删除),未删除时为 null。"),
|
||||||
|
TenantId = table.Column<long>(type: "bigint", nullable: false, comment: "所属租户 ID。")
|
||||||
|
},
|
||||||
|
constraints: table =>
|
||||||
|
{
|
||||||
|
table.PrimaryKey("PK_product_spec_templates", x => x.Id);
|
||||||
|
},
|
||||||
|
comment: "门店规格做法模板。");
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "IX_product_spec_template_options_TenantId_TemplateId_Name",
|
||||||
|
table: "product_spec_template_options",
|
||||||
|
columns: new[] { "TenantId", "TemplateId", "Name" },
|
||||||
|
unique: true);
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "IX_product_spec_template_products_TenantId_StoreId_ProductId",
|
||||||
|
table: "product_spec_template_products",
|
||||||
|
columns: new[] { "TenantId", "StoreId", "ProductId" });
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "IX_product_spec_template_products_TenantId_StoreId_TemplateId_~",
|
||||||
|
table: "product_spec_template_products",
|
||||||
|
columns: new[] { "TenantId", "StoreId", "TemplateId", "ProductId" },
|
||||||
|
unique: true);
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "IX_product_spec_templates_TenantId_StoreId_Name",
|
||||||
|
table: "product_spec_templates",
|
||||||
|
columns: new[] { "TenantId", "StoreId", "Name" },
|
||||||
|
unique: true);
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "IX_product_spec_templates_TenantId_StoreId_TemplateType_IsEnab~",
|
||||||
|
table: "product_spec_templates",
|
||||||
|
columns: new[] { "TenantId", "StoreId", "TemplateType", "IsEnabled" });
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.DropTable(
|
||||||
|
name: "product_spec_template_options");
|
||||||
|
|
||||||
|
migrationBuilder.DropTable(
|
||||||
|
name: "product_spec_template_products");
|
||||||
|
|
||||||
|
migrationBuilder.DropTable(
|
||||||
|
name: "product_spec_templates");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4664,6 +4664,215 @@ namespace TakeoutSaaS.Infrastructure.Migrations
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("TakeoutSaaS.Domain.Products.Entities.ProductSpecTemplate", b =>
|
||||||
|
{
|
||||||
|
b.Property<long>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("bigint")
|
||||||
|
.HasComment("实体唯一标识。");
|
||||||
|
|
||||||
|
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<long>("Id"));
|
||||||
|
|
||||||
|
b.Property<DateTime>("CreatedAt")
|
||||||
|
.HasColumnType("timestamp with time zone")
|
||||||
|
.HasComment("创建时间(UTC)。");
|
||||||
|
|
||||||
|
b.Property<long?>("CreatedBy")
|
||||||
|
.HasColumnType("bigint")
|
||||||
|
.HasComment("创建人用户标识,匿名或系统操作时为 null。");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("DeletedAt")
|
||||||
|
.HasColumnType("timestamp with time zone")
|
||||||
|
.HasComment("软删除时间(UTC),未删除时为 null。");
|
||||||
|
|
||||||
|
b.Property<long?>("DeletedBy")
|
||||||
|
.HasColumnType("bigint")
|
||||||
|
.HasComment("删除人用户标识(软删除),未删除时为 null。");
|
||||||
|
|
||||||
|
b.Property<bool>("IsEnabled")
|
||||||
|
.HasColumnType("boolean")
|
||||||
|
.HasComment("是否启用。");
|
||||||
|
|
||||||
|
b.Property<bool>("IsRequired")
|
||||||
|
.HasColumnType("boolean")
|
||||||
|
.HasComment("是否必选。");
|
||||||
|
|
||||||
|
b.Property<string>("Name")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(64)
|
||||||
|
.HasColumnType("character varying(64)")
|
||||||
|
.HasComment("模板名称。");
|
||||||
|
|
||||||
|
b.Property<int>("SelectionType")
|
||||||
|
.HasColumnType("integer")
|
||||||
|
.HasComment("选择方式。");
|
||||||
|
|
||||||
|
b.Property<int>("SortOrder")
|
||||||
|
.HasColumnType("integer")
|
||||||
|
.HasComment("排序值。");
|
||||||
|
|
||||||
|
b.Property<long>("StoreId")
|
||||||
|
.HasColumnType("bigint")
|
||||||
|
.HasComment("所属门店。");
|
||||||
|
|
||||||
|
b.Property<int>("TemplateType")
|
||||||
|
.HasColumnType("integer")
|
||||||
|
.HasComment("模板类型。");
|
||||||
|
|
||||||
|
b.Property<long>("TenantId")
|
||||||
|
.HasColumnType("bigint")
|
||||||
|
.HasComment("所属租户 ID。");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("UpdatedAt")
|
||||||
|
.HasColumnType("timestamp with time zone")
|
||||||
|
.HasComment("最近一次更新时间(UTC),从未更新时为 null。");
|
||||||
|
|
||||||
|
b.Property<long?>("UpdatedBy")
|
||||||
|
.HasColumnType("bigint")
|
||||||
|
.HasComment("最后更新人用户标识,匿名或系统操作时为 null。");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("TenantId", "StoreId", "Name")
|
||||||
|
.IsUnique();
|
||||||
|
|
||||||
|
b.HasIndex("TenantId", "StoreId", "TemplateType", "IsEnabled");
|
||||||
|
|
||||||
|
b.ToTable("product_spec_templates", null, t =>
|
||||||
|
{
|
||||||
|
t.HasComment("门店规格做法模板。");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("TakeoutSaaS.Domain.Products.Entities.ProductSpecTemplateOption", b =>
|
||||||
|
{
|
||||||
|
b.Property<long>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("bigint")
|
||||||
|
.HasComment("实体唯一标识。");
|
||||||
|
|
||||||
|
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<long>("Id"));
|
||||||
|
|
||||||
|
b.Property<DateTime>("CreatedAt")
|
||||||
|
.HasColumnType("timestamp with time zone")
|
||||||
|
.HasComment("创建时间(UTC)。");
|
||||||
|
|
||||||
|
b.Property<long?>("CreatedBy")
|
||||||
|
.HasColumnType("bigint")
|
||||||
|
.HasComment("创建人用户标识,匿名或系统操作时为 null。");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("DeletedAt")
|
||||||
|
.HasColumnType("timestamp with time zone")
|
||||||
|
.HasComment("软删除时间(UTC),未删除时为 null。");
|
||||||
|
|
||||||
|
b.Property<long?>("DeletedBy")
|
||||||
|
.HasColumnType("bigint")
|
||||||
|
.HasComment("删除人用户标识(软删除),未删除时为 null。");
|
||||||
|
|
||||||
|
b.Property<decimal>("ExtraPrice")
|
||||||
|
.HasPrecision(18, 2)
|
||||||
|
.HasColumnType("numeric(18,2)")
|
||||||
|
.HasComment("附加价格。");
|
||||||
|
|
||||||
|
b.Property<string>("Name")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(64)
|
||||||
|
.HasColumnType("character varying(64)")
|
||||||
|
.HasComment("选项名称。");
|
||||||
|
|
||||||
|
b.Property<int>("SortOrder")
|
||||||
|
.HasColumnType("integer")
|
||||||
|
.HasComment("排序值。");
|
||||||
|
|
||||||
|
b.Property<long>("TemplateId")
|
||||||
|
.HasColumnType("bigint")
|
||||||
|
.HasComment("模板 ID。");
|
||||||
|
|
||||||
|
b.Property<long>("TenantId")
|
||||||
|
.HasColumnType("bigint")
|
||||||
|
.HasComment("所属租户 ID。");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("UpdatedAt")
|
||||||
|
.HasColumnType("timestamp with time zone")
|
||||||
|
.HasComment("最近一次更新时间(UTC),从未更新时为 null。");
|
||||||
|
|
||||||
|
b.Property<long?>("UpdatedBy")
|
||||||
|
.HasColumnType("bigint")
|
||||||
|
.HasComment("最后更新人用户标识,匿名或系统操作时为 null。");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("TenantId", "TemplateId", "Name")
|
||||||
|
.IsUnique();
|
||||||
|
|
||||||
|
b.ToTable("product_spec_template_options", null, t =>
|
||||||
|
{
|
||||||
|
t.HasComment("规格做法模板选项。");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("TakeoutSaaS.Domain.Products.Entities.ProductSpecTemplateProduct", b =>
|
||||||
|
{
|
||||||
|
b.Property<long>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("bigint")
|
||||||
|
.HasComment("实体唯一标识。");
|
||||||
|
|
||||||
|
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<long>("Id"));
|
||||||
|
|
||||||
|
b.Property<DateTime>("CreatedAt")
|
||||||
|
.HasColumnType("timestamp with time zone")
|
||||||
|
.HasComment("创建时间(UTC)。");
|
||||||
|
|
||||||
|
b.Property<long?>("CreatedBy")
|
||||||
|
.HasColumnType("bigint")
|
||||||
|
.HasComment("创建人用户标识,匿名或系统操作时为 null。");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("DeletedAt")
|
||||||
|
.HasColumnType("timestamp with time zone")
|
||||||
|
.HasComment("软删除时间(UTC),未删除时为 null。");
|
||||||
|
|
||||||
|
b.Property<long?>("DeletedBy")
|
||||||
|
.HasColumnType("bigint")
|
||||||
|
.HasComment("删除人用户标识(软删除),未删除时为 null。");
|
||||||
|
|
||||||
|
b.Property<long>("ProductId")
|
||||||
|
.HasColumnType("bigint")
|
||||||
|
.HasComment("商品 ID。");
|
||||||
|
|
||||||
|
b.Property<long>("StoreId")
|
||||||
|
.HasColumnType("bigint")
|
||||||
|
.HasComment("所属门店。");
|
||||||
|
|
||||||
|
b.Property<long>("TemplateId")
|
||||||
|
.HasColumnType("bigint")
|
||||||
|
.HasComment("模板 ID。");
|
||||||
|
|
||||||
|
b.Property<long>("TenantId")
|
||||||
|
.HasColumnType("bigint")
|
||||||
|
.HasComment("所属租户 ID。");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("UpdatedAt")
|
||||||
|
.HasColumnType("timestamp with time zone")
|
||||||
|
.HasComment("最近一次更新时间(UTC),从未更新时为 null。");
|
||||||
|
|
||||||
|
b.Property<long?>("UpdatedBy")
|
||||||
|
.HasColumnType("bigint")
|
||||||
|
.HasComment("最后更新人用户标识,匿名或系统操作时为 null。");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("TenantId", "StoreId", "ProductId");
|
||||||
|
|
||||||
|
b.HasIndex("TenantId", "StoreId", "TemplateId", "ProductId")
|
||||||
|
.IsUnique();
|
||||||
|
|
||||||
|
b.ToTable("product_spec_template_products", null, t =>
|
||||||
|
{
|
||||||
|
t.HasComment("规格做法模板与商品关联。");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
modelBuilder.Entity("TakeoutSaaS.Domain.Queues.Entities.QueueTicket", b =>
|
modelBuilder.Entity("TakeoutSaaS.Domain.Queues.Entities.QueueTicket", b =>
|
||||||
{
|
{
|
||||||
b.Property<long>("Id")
|
b.Property<long>("Id")
|
||||||
|
|||||||
Reference in New Issue
Block a user