feat(product): add product schedule management api
All checks were successful
Build and Deploy TenantApi / build-and-deploy (push) Successful in 46s
All checks were successful
Build and Deploy TenantApi / build-and-deploy (push) Successful in 46s
This commit is contained in:
@@ -0,0 +1,156 @@
|
||||
namespace TakeoutSaaS.TenantApi.Contracts.Product;
|
||||
|
||||
/// <summary>
|
||||
/// 商品时段规则列表查询请求。
|
||||
/// </summary>
|
||||
public sealed class ProductScheduleListRequest
|
||||
{
|
||||
/// <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 SaveProductScheduleRequest
|
||||
{
|
||||
/// <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>
|
||||
/// 开始时间(HH:mm)。
|
||||
/// </summary>
|
||||
public string StartTime { get; set; } = "00:00";
|
||||
|
||||
/// <summary>
|
||||
/// 结束时间(HH:mm)。
|
||||
/// </summary>
|
||||
public string EndTime { get; set; } = "00:00";
|
||||
|
||||
/// <summary>
|
||||
/// 适用星期(1-7)。
|
||||
/// </summary>
|
||||
public List<int> WeekDays { get; set; } = [];
|
||||
|
||||
/// <summary>
|
||||
/// 关联商品 ID 列表。
|
||||
/// </summary>
|
||||
public List<string> ProductIds { get; set; } = [];
|
||||
|
||||
/// <summary>
|
||||
/// 状态(enabled/disabled)。
|
||||
/// </summary>
|
||||
public string Status { get; set; } = "enabled";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除商品时段规则请求。
|
||||
/// </summary>
|
||||
public sealed class DeleteProductScheduleRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// 门店 ID。
|
||||
/// </summary>
|
||||
public string StoreId { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 规则 ID。
|
||||
/// </summary>
|
||||
public string ScheduleId { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 修改商品时段规则状态请求。
|
||||
/// </summary>
|
||||
public sealed class ChangeProductScheduleStatusRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// 门店 ID。
|
||||
/// </summary>
|
||||
public string StoreId { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 规则 ID。
|
||||
/// </summary>
|
||||
public string ScheduleId { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 状态(enabled/disabled)。
|
||||
/// </summary>
|
||||
public string Status { get; set; } = "enabled";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 商品时段规则列表项响应。
|
||||
/// </summary>
|
||||
public sealed class ProductScheduleItemResponse
|
||||
{
|
||||
/// <summary>
|
||||
/// 规则 ID。
|
||||
/// </summary>
|
||||
public string Id { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 规则名称。
|
||||
/// </summary>
|
||||
public string Name { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 开始时间(HH:mm)。
|
||||
/// </summary>
|
||||
public string StartTime { get; set; } = "00:00";
|
||||
|
||||
/// <summary>
|
||||
/// 结束时间(HH:mm)。
|
||||
/// </summary>
|
||||
public string EndTime { get; set; } = "00:00";
|
||||
|
||||
/// <summary>
|
||||
/// 适用星期(1-7)。
|
||||
/// </summary>
|
||||
public List<int> WeekDays { 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 string UpdatedAt { get; set; } = string.Empty;
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
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 ProductScheduleController(
|
||||
IMediator mediator,
|
||||
TakeoutAppDbContext dbContext,
|
||||
StoreContextService storeContextService) : BaseApiController
|
||||
{
|
||||
/// <summary>
|
||||
/// 商品时段规则列表。
|
||||
/// </summary>
|
||||
[HttpGet("schedule/list")]
|
||||
[ProducesResponseType(typeof(ApiResponse<IReadOnlyList<ProductScheduleItemResponse>>), StatusCodes.Status200OK)]
|
||||
public async Task<ApiResponse<IReadOnlyList<ProductScheduleItemResponse>>> GetScheduleList(
|
||||
[FromQuery] ProductScheduleListRequest request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var storeId = StoreApiHelpers.ParseRequiredSnowflake(request.StoreId, nameof(request.StoreId));
|
||||
await EnsureStoreAccessibleAsync(storeId, cancellationToken);
|
||||
|
||||
var result = await mediator.Send(new GetProductScheduleListQuery
|
||||
{
|
||||
StoreId = storeId,
|
||||
Keyword = request.Keyword,
|
||||
Status = request.Status
|
||||
}, cancellationToken);
|
||||
|
||||
return ApiResponse<IReadOnlyList<ProductScheduleItemResponse>>.Ok(result.Select(MapScheduleItem).ToList());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 保存商品时段规则。
|
||||
/// </summary>
|
||||
[HttpPost("schedule/save")]
|
||||
[ProducesResponseType(typeof(ApiResponse<ProductScheduleItemResponse>), StatusCodes.Status200OK)]
|
||||
public async Task<ApiResponse<ProductScheduleItemResponse>> SaveSchedule(
|
||||
[FromBody] SaveProductScheduleRequest request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var storeId = StoreApiHelpers.ParseRequiredSnowflake(request.StoreId, nameof(request.StoreId));
|
||||
await EnsureStoreAccessibleAsync(storeId, cancellationToken);
|
||||
|
||||
var result = await mediator.Send(new SaveProductScheduleCommand
|
||||
{
|
||||
StoreId = storeId,
|
||||
ScheduleId = StoreApiHelpers.ParseSnowflakeOrNull(request.Id),
|
||||
Name = request.Name,
|
||||
StartTime = request.StartTime,
|
||||
EndTime = request.EndTime,
|
||||
WeekDays = request.WeekDays ?? [],
|
||||
ProductIds = StoreApiHelpers.ParseSnowflakeList(request.ProductIds),
|
||||
Status = request.Status
|
||||
}, cancellationToken);
|
||||
|
||||
return ApiResponse<ProductScheduleItemResponse>.Ok(MapScheduleItem(result));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除商品时段规则。
|
||||
/// </summary>
|
||||
[HttpPost("schedule/delete")]
|
||||
[ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status200OK)]
|
||||
public async Task<ApiResponse<object>> DeleteSchedule(
|
||||
[FromBody] DeleteProductScheduleRequest request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var storeId = StoreApiHelpers.ParseRequiredSnowflake(request.StoreId, nameof(request.StoreId));
|
||||
await EnsureStoreAccessibleAsync(storeId, cancellationToken);
|
||||
|
||||
await mediator.Send(new DeleteProductScheduleCommand
|
||||
{
|
||||
StoreId = storeId,
|
||||
ScheduleId = StoreApiHelpers.ParseRequiredSnowflake(request.ScheduleId, nameof(request.ScheduleId))
|
||||
}, cancellationToken);
|
||||
|
||||
return ApiResponse<object>.Ok(null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 修改商品时段规则状态。
|
||||
/// </summary>
|
||||
[HttpPost("schedule/status")]
|
||||
[ProducesResponseType(typeof(ApiResponse<ProductScheduleItemResponse>), StatusCodes.Status200OK)]
|
||||
public async Task<ApiResponse<ProductScheduleItemResponse>> ChangeScheduleStatus(
|
||||
[FromBody] ChangeProductScheduleStatusRequest request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var storeId = StoreApiHelpers.ParseRequiredSnowflake(request.StoreId, nameof(request.StoreId));
|
||||
await EnsureStoreAccessibleAsync(storeId, cancellationToken);
|
||||
|
||||
var result = await mediator.Send(new ChangeProductScheduleStatusCommand
|
||||
{
|
||||
StoreId = storeId,
|
||||
ScheduleId = StoreApiHelpers.ParseRequiredSnowflake(request.ScheduleId, nameof(request.ScheduleId)),
|
||||
Status = request.Status
|
||||
}, cancellationToken);
|
||||
|
||||
return ApiResponse<ProductScheduleItemResponse>.Ok(MapScheduleItem(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 ProductScheduleItemResponse MapScheduleItem(ProductScheduleItemDto source)
|
||||
{
|
||||
return new ProductScheduleItemResponse
|
||||
{
|
||||
Id = source.Id.ToString(),
|
||||
Name = source.Name,
|
||||
StartTime = source.StartTime,
|
||||
EndTime = source.EndTime,
|
||||
WeekDays = source.WeekDays.ToList(),
|
||||
Status = source.Status,
|
||||
ProductCount = source.ProductCount,
|
||||
ProductIds = source.ProductIds.Select(item => item.ToString()).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 ChangeProductScheduleStatusCommand : IRequest<ProductScheduleItemDto>
|
||||
{
|
||||
/// <summary>
|
||||
/// 门店 ID。
|
||||
/// </summary>
|
||||
public long StoreId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 规则 ID。
|
||||
/// </summary>
|
||||
public long ScheduleId { 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 DeleteProductScheduleCommand : IRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// 门店 ID。
|
||||
/// </summary>
|
||||
public long StoreId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 规则 ID。
|
||||
/// </summary>
|
||||
public long ScheduleId { get; init; }
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
using MediatR;
|
||||
using TakeoutSaaS.Application.App.Products.Dto;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Products.Commands;
|
||||
|
||||
/// <summary>
|
||||
/// 保存商品时段规则命令。
|
||||
/// </summary>
|
||||
public sealed class SaveProductScheduleCommand : IRequest<ProductScheduleItemDto>
|
||||
{
|
||||
/// <summary>
|
||||
/// 门店 ID。
|
||||
/// </summary>
|
||||
public long StoreId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 规则 ID(编辑时传)。
|
||||
/// </summary>
|
||||
public long? ScheduleId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 规则名称。
|
||||
/// </summary>
|
||||
public string Name { get; init; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 开始时间(HH:mm)。
|
||||
/// </summary>
|
||||
public string StartTime { get; init; } = "00:00";
|
||||
|
||||
/// <summary>
|
||||
/// 结束时间(HH:mm)。
|
||||
/// </summary>
|
||||
public string EndTime { get; init; } = "00:00";
|
||||
|
||||
/// <summary>
|
||||
/// 星期列表(1-7)。
|
||||
/// </summary>
|
||||
public IReadOnlyList<int> WeekDays { get; init; } = [];
|
||||
|
||||
/// <summary>
|
||||
/// 关联商品 ID 列表。
|
||||
/// </summary>
|
||||
public IReadOnlyList<long> ProductIds { get; init; } = [];
|
||||
|
||||
/// <summary>
|
||||
/// 状态(enabled/disabled)。
|
||||
/// </summary>
|
||||
public string Status { get; init; } = "enabled";
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
namespace TakeoutSaaS.Application.App.Products.Dto;
|
||||
|
||||
/// <summary>
|
||||
/// 商品时段规则列表项 DTO。
|
||||
/// </summary>
|
||||
public sealed class ProductScheduleItemDto
|
||||
{
|
||||
/// <summary>
|
||||
/// 规则 ID。
|
||||
/// </summary>
|
||||
public long Id { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 规则名称。
|
||||
/// </summary>
|
||||
public string Name { get; init; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 开始时间(HH:mm)。
|
||||
/// </summary>
|
||||
public string StartTime { get; init; } = "00:00";
|
||||
|
||||
/// <summary>
|
||||
/// 结束时间(HH:mm)。
|
||||
/// </summary>
|
||||
public string EndTime { get; init; } = "00:00";
|
||||
|
||||
/// <summary>
|
||||
/// 星期列表(1-7)。
|
||||
/// </summary>
|
||||
public IReadOnlyList<int> WeekDays { 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 DateTime UpdatedAt { 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 ChangeProductScheduleStatusCommandHandler(
|
||||
IProductRepository productRepository,
|
||||
ITenantProvider tenantProvider)
|
||||
: IRequestHandler<ChangeProductScheduleStatusCommand, ProductScheduleItemDto>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task<ProductScheduleItemDto> Handle(ChangeProductScheduleStatusCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
// 1. 解析状态并校验规则归属。
|
||||
if (!ProductScheduleMapping.TryParseStatus(request.Status, out var isEnabled))
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.BadRequest, "status 参数不合法");
|
||||
}
|
||||
|
||||
var tenantId = tenantProvider.GetCurrentTenantId();
|
||||
var existing = await productRepository.FindScheduleByIdAsync(request.ScheduleId, tenantId, cancellationToken);
|
||||
if (existing is null || existing.StoreId != request.StoreId)
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.NotFound, "时段规则不存在");
|
||||
}
|
||||
|
||||
// 2. 保存状态变更。
|
||||
existing.IsEnabled = isEnabled;
|
||||
await productRepository.UpdateScheduleAsync(existing, cancellationToken);
|
||||
await productRepository.SaveChangesAsync(cancellationToken);
|
||||
|
||||
// 3. 返回最新快照。
|
||||
var relations = await productRepository.GetScheduleProductsByScheduleIdsAsync(
|
||||
[existing.Id],
|
||||
tenantId,
|
||||
request.StoreId,
|
||||
cancellationToken);
|
||||
var productIdsLookup = relations
|
||||
.GroupBy(x => x.ScheduleId)
|
||||
.ToDictionary(group => group.Key, group => group.Select(item => item.ProductId).ToList());
|
||||
|
||||
return ProductScheduleDtoFactory.ToDto(existing, productIdsLookup);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
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 DeleteProductScheduleCommandHandler(
|
||||
IProductRepository productRepository,
|
||||
ITenantProvider tenantProvider)
|
||||
: IRequestHandler<DeleteProductScheduleCommand>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task Handle(DeleteProductScheduleCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
// 1. 校验规则存在且归属当前门店。
|
||||
var tenantId = tenantProvider.GetCurrentTenantId();
|
||||
var existing = await productRepository.FindScheduleByIdAsync(request.ScheduleId, tenantId, cancellationToken);
|
||||
if (existing is null || existing.StoreId != request.StoreId)
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.NotFound, "时段规则不存在");
|
||||
}
|
||||
|
||||
// 2. 删除关联和主记录。
|
||||
await productRepository.RemoveScheduleProductsAsync(existing.Id, tenantId, request.StoreId, cancellationToken);
|
||||
await productRepository.DeleteScheduleAsync(existing.Id, tenantId, cancellationToken);
|
||||
await productRepository.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
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 GetProductScheduleListQueryHandler(
|
||||
IProductRepository productRepository,
|
||||
ITenantProvider tenantProvider)
|
||||
: IRequestHandler<GetProductScheduleListQuery, IReadOnlyList<ProductScheduleItemDto>>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task<IReadOnlyList<ProductScheduleItemDto>> Handle(GetProductScheduleListQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
// 1. 读取门店时段规则。
|
||||
var tenantId = tenantProvider.GetCurrentTenantId();
|
||||
var schedules = await productRepository.GetSchedulesByStoreAsync(tenantId, request.StoreId, cancellationToken);
|
||||
if (schedules.Count == 0)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
// 2. 按状态与关键字过滤。
|
||||
IEnumerable<ProductSchedule> filtered = schedules;
|
||||
if (!string.IsNullOrWhiteSpace(request.Status))
|
||||
{
|
||||
if (!ProductScheduleMapping.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));
|
||||
}
|
||||
|
||||
var filteredList = filtered
|
||||
.OrderBy(item => item.Name)
|
||||
.ThenBy(item => item.Id)
|
||||
.ToList();
|
||||
if (filteredList.Count == 0)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
// 3. 批量读取关联商品并映射 DTO。
|
||||
var scheduleIds = filteredList.Select(item => item.Id).ToList();
|
||||
var relations = await productRepository.GetScheduleProductsByScheduleIdsAsync(
|
||||
scheduleIds,
|
||||
tenantId,
|
||||
request.StoreId,
|
||||
cancellationToken);
|
||||
var productIdsLookup = relations
|
||||
.GroupBy(x => x.ScheduleId)
|
||||
.ToDictionary(group => group.Key, group => group.Select(item => item.ProductId).ToList());
|
||||
|
||||
return filteredList
|
||||
.Select(schedule => ProductScheduleDtoFactory.ToDto(schedule, productIdsLookup))
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
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 SaveProductScheduleCommandHandler(
|
||||
IProductRepository productRepository,
|
||||
ITenantProvider tenantProvider)
|
||||
: IRequestHandler<SaveProductScheduleCommand, ProductScheduleItemDto>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task<ProductScheduleItemDto> Handle(SaveProductScheduleCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
// 1. 归一化并校验输入。
|
||||
var tenantId = tenantProvider.GetCurrentTenantId();
|
||||
var normalizedName = request.Name.Trim();
|
||||
if (string.IsNullOrWhiteSpace(normalizedName))
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.BadRequest, "规则名称不能为空");
|
||||
}
|
||||
|
||||
if (!ProductScheduleMapping.TryParseStatus(request.Status, out var isEnabled))
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.BadRequest, "status 参数不合法");
|
||||
}
|
||||
|
||||
if (!ProductScheduleMapping.TryParseTime(request.StartTime, out var startTime))
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.BadRequest, "开始时间格式必须为 HH:mm");
|
||||
}
|
||||
|
||||
if (!ProductScheduleMapping.TryParseTime(request.EndTime, out var endTime))
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.BadRequest, "结束时间格式必须为 HH:mm");
|
||||
}
|
||||
|
||||
if (startTime == endTime)
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.BadRequest, "开始和结束时间不能相同");
|
||||
}
|
||||
|
||||
if (!ProductScheduleMapping.TryNormalizeWeekDays(request.WeekDays, out var normalizedWeekDays))
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.BadRequest, "请至少选择一个适用星期");
|
||||
}
|
||||
|
||||
var normalizedProductIds = request.ProductIds
|
||||
.Where(item => item > 0)
|
||||
.Distinct()
|
||||
.ToList();
|
||||
var validProductIds = await productRepository.FilterExistingProductIdsAsync(
|
||||
tenantId,
|
||||
request.StoreId,
|
||||
normalizedProductIds,
|
||||
cancellationToken);
|
||||
if (validProductIds.Count == 0)
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.BadRequest, "请至少关联一个商品");
|
||||
}
|
||||
|
||||
// 2. 校验门店内名称唯一。
|
||||
var isDuplicate = await productRepository.ExistsScheduleNameAsync(
|
||||
tenantId,
|
||||
request.StoreId,
|
||||
normalizedName,
|
||||
request.ScheduleId,
|
||||
cancellationToken);
|
||||
if (isDuplicate)
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.Conflict, "规则名称已存在");
|
||||
}
|
||||
|
||||
// 3. 创建或更新规则主记录。
|
||||
ProductSchedule schedule;
|
||||
if (!request.ScheduleId.HasValue)
|
||||
{
|
||||
schedule = new ProductSchedule
|
||||
{
|
||||
StoreId = request.StoreId,
|
||||
Name = normalizedName,
|
||||
StartTime = startTime,
|
||||
EndTime = endTime,
|
||||
WeekDaysMask = ProductScheduleMapping.ToWeekDaysMask(normalizedWeekDays),
|
||||
IsEnabled = isEnabled
|
||||
};
|
||||
|
||||
await productRepository.AddScheduleAsync(schedule, cancellationToken);
|
||||
await productRepository.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
var existing = await productRepository.FindScheduleByIdAsync(request.ScheduleId.Value, tenantId, cancellationToken);
|
||||
if (existing is null || existing.StoreId != request.StoreId)
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.NotFound, "时段规则不存在");
|
||||
}
|
||||
|
||||
existing.Name = normalizedName;
|
||||
existing.StartTime = startTime;
|
||||
existing.EndTime = endTime;
|
||||
existing.WeekDaysMask = ProductScheduleMapping.ToWeekDaysMask(normalizedWeekDays);
|
||||
existing.IsEnabled = isEnabled;
|
||||
schedule = existing;
|
||||
|
||||
await productRepository.UpdateScheduleAsync(schedule, cancellationToken);
|
||||
}
|
||||
|
||||
// 4. 替换规则关联商品。
|
||||
await productRepository.RemoveScheduleProductsAsync(schedule.Id, tenantId, request.StoreId, cancellationToken);
|
||||
var relations = validProductIds
|
||||
.Select(productId => new ProductScheduleProduct
|
||||
{
|
||||
StoreId = request.StoreId,
|
||||
ScheduleId = schedule.Id,
|
||||
ProductId = productId
|
||||
})
|
||||
.ToList();
|
||||
if (relations.Count > 0)
|
||||
{
|
||||
await productRepository.AddScheduleProductsAsync(relations, cancellationToken);
|
||||
}
|
||||
|
||||
await productRepository.SaveChangesAsync(cancellationToken);
|
||||
|
||||
// 5. 回读关联并返回最新快照。
|
||||
var latestRelations = await productRepository.GetScheduleProductsByScheduleIdsAsync(
|
||||
[schedule.Id],
|
||||
tenantId,
|
||||
request.StoreId,
|
||||
cancellationToken);
|
||||
var productIdsLookup = latestRelations
|
||||
.GroupBy(x => x.ScheduleId)
|
||||
.ToDictionary(group => group.Key, group => group.Select(item => item.ProductId).ToList());
|
||||
|
||||
return ProductScheduleDtoFactory.ToDto(schedule, productIdsLookup);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using TakeoutSaaS.Application.App.Products.Dto;
|
||||
using TakeoutSaaS.Domain.Products.Entities;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Products;
|
||||
|
||||
/// <summary>
|
||||
/// 商品时段规则 DTO 映射工厂。
|
||||
/// </summary>
|
||||
internal static class ProductScheduleDtoFactory
|
||||
{
|
||||
/// <summary>
|
||||
/// 将时段规则实体映射为页面 DTO。
|
||||
/// </summary>
|
||||
public static ProductScheduleItemDto ToDto(
|
||||
ProductSchedule schedule,
|
||||
IReadOnlyDictionary<long, List<long>> productIdsLookup)
|
||||
{
|
||||
var productIds = productIdsLookup.TryGetValue(schedule.Id, out var ids)
|
||||
? ids
|
||||
: [];
|
||||
|
||||
return new ProductScheduleItemDto
|
||||
{
|
||||
Id = schedule.Id,
|
||||
Name = schedule.Name,
|
||||
StartTime = ProductScheduleMapping.ToTimeText(schedule.StartTime),
|
||||
EndTime = ProductScheduleMapping.ToTimeText(schedule.EndTime),
|
||||
WeekDays = ProductScheduleMapping.FromWeekDaysMask(schedule.WeekDaysMask),
|
||||
Status = ProductScheduleMapping.ToStatusText(schedule.IsEnabled),
|
||||
ProductCount = productIds.Count,
|
||||
ProductIds = productIds,
|
||||
UpdatedAt = schedule.UpdatedAt ?? schedule.CreatedAt
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
using System.Globalization;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Products;
|
||||
|
||||
/// <summary>
|
||||
/// 商品时段规则映射辅助。
|
||||
/// </summary>
|
||||
internal static class ProductScheduleMapping
|
||||
{
|
||||
/// <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 bool TryParseTime(string? value, out TimeSpan parsed)
|
||||
{
|
||||
return TimeSpan.TryParseExact(
|
||||
(value ?? string.Empty).Trim(),
|
||||
"hh\\:mm",
|
||||
CultureInfo.InvariantCulture,
|
||||
out parsed);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 时间转字符串。
|
||||
/// </summary>
|
||||
public static string ToTimeText(TimeSpan value)
|
||||
{
|
||||
return value.ToString("hh\\:mm", CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 归一化星期列表(1-7)。
|
||||
/// </summary>
|
||||
public static bool TryNormalizeWeekDays(IEnumerable<int>? weekDays, out List<int> normalized)
|
||||
{
|
||||
normalized = (weekDays ?? [])
|
||||
.Distinct()
|
||||
.Where(day => day is >= 1 and <= 7)
|
||||
.OrderBy(day => day)
|
||||
.ToList();
|
||||
|
||||
return normalized.Count > 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 星期列表转位掩码。
|
||||
/// </summary>
|
||||
public static int ToWeekDaysMask(IReadOnlyCollection<int> weekDays)
|
||||
{
|
||||
var mask = 0;
|
||||
foreach (var day in weekDays)
|
||||
{
|
||||
mask |= 1 << (day - 1);
|
||||
}
|
||||
|
||||
return mask;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 位掩码转星期列表(1-7)。
|
||||
/// </summary>
|
||||
public static IReadOnlyList<int> FromWeekDaysMask(int mask)
|
||||
{
|
||||
var days = new List<int>(7);
|
||||
for (var day = 1; day <= 7; day++)
|
||||
{
|
||||
if ((mask & (1 << (day - 1))) != 0)
|
||||
{
|
||||
days.Add(day);
|
||||
}
|
||||
}
|
||||
|
||||
return days;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using MediatR;
|
||||
using TakeoutSaaS.Application.App.Products.Dto;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Products.Queries;
|
||||
|
||||
/// <summary>
|
||||
/// 查询商品时段规则列表。
|
||||
/// </summary>
|
||||
public sealed class GetProductScheduleListQuery : IRequest<IReadOnlyList<ProductScheduleItemDto>>
|
||||
{
|
||||
/// <summary>
|
||||
/// 门店 ID。
|
||||
/// </summary>
|
||||
public long StoreId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 关键字。
|
||||
/// </summary>
|
||||
public string? Keyword { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 状态(enabled/disabled)。
|
||||
/// </summary>
|
||||
public string? Status { get; init; }
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
using TakeoutSaaS.Shared.Abstractions.Entities;
|
||||
|
||||
namespace TakeoutSaaS.Domain.Products.Entities;
|
||||
|
||||
/// <summary>
|
||||
/// 商品时段规则(门店维度)。
|
||||
/// </summary>
|
||||
public sealed class ProductSchedule : MultiTenantEntityBase
|
||||
{
|
||||
/// <summary>
|
||||
/// 所属门店。
|
||||
/// </summary>
|
||||
public long StoreId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 规则名称。
|
||||
/// </summary>
|
||||
public string Name { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 开始时间。
|
||||
/// </summary>
|
||||
public TimeSpan StartTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 结束时间。
|
||||
/// </summary>
|
||||
public TimeSpan EndTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 星期位掩码(周一到周日)。
|
||||
/// </summary>
|
||||
public int WeekDaysMask { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否启用。
|
||||
/// </summary>
|
||||
public bool IsEnabled { get; set; } = true;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using TakeoutSaaS.Shared.Abstractions.Entities;
|
||||
|
||||
namespace TakeoutSaaS.Domain.Products.Entities;
|
||||
|
||||
/// <summary>
|
||||
/// 时段规则与商品关联关系。
|
||||
/// </summary>
|
||||
public sealed class ProductScheduleProduct : MultiTenantEntityBase
|
||||
{
|
||||
/// <summary>
|
||||
/// 所属门店。
|
||||
/// </summary>
|
||||
public long StoreId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 时段规则 ID。
|
||||
/// </summary>
|
||||
public long ScheduleId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 商品 ID。
|
||||
/// </summary>
|
||||
public long ProductId { get; set; }
|
||||
}
|
||||
@@ -58,6 +58,11 @@ public interface IProductRepository
|
||||
/// </summary>
|
||||
Task<IReadOnlyList<ProductLabel>> GetLabelsByStoreAsync(long tenantId, long storeId, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// 按门店读取商品时段规则。
|
||||
/// </summary>
|
||||
Task<IReadOnlyList<ProductSchedule>> GetSchedulesByStoreAsync(long tenantId, long storeId, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// 依据标识读取规格做法模板。
|
||||
/// </summary>
|
||||
@@ -73,6 +78,11 @@ public interface IProductRepository
|
||||
/// </summary>
|
||||
Task<ProductLabel?> FindLabelByIdAsync(long labelId, long tenantId, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// 依据标识读取商品时段规则。
|
||||
/// </summary>
|
||||
Task<ProductSchedule?> FindScheduleByIdAsync(long scheduleId, long tenantId, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// 判断门店内模板名称是否已存在。
|
||||
/// </summary>
|
||||
@@ -88,6 +98,11 @@ public interface IProductRepository
|
||||
/// </summary>
|
||||
Task<bool> ExistsLabelNameAsync(long tenantId, long storeId, string name, long? excludeLabelId = null, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// 判断门店内时段规则名称是否已存在。
|
||||
/// </summary>
|
||||
Task<bool> ExistsScheduleNameAsync(long tenantId, long storeId, string name, long? excludeScheduleId = null, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// 按模板读取规格做法选项。
|
||||
/// </summary>
|
||||
@@ -103,6 +118,11 @@ public interface IProductRepository
|
||||
/// </summary>
|
||||
Task<Dictionary<long, int>> CountLabelProductsByLabelIdsAsync(long tenantId, long storeId, IReadOnlyCollection<long> labelIds, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// 读取时段规则关联商品。
|
||||
/// </summary>
|
||||
Task<IReadOnlyList<ProductScheduleProduct>> GetScheduleProductsByScheduleIdsAsync(IReadOnlyCollection<long> scheduleIds, long tenantId, long storeId, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// 过滤门店内真实存在的商品 ID。
|
||||
/// </summary>
|
||||
@@ -245,6 +265,16 @@ public interface IProductRepository
|
||||
/// </summary>
|
||||
Task AddLabelAsync(ProductLabel label, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// 新增时段规则。
|
||||
/// </summary>
|
||||
Task AddScheduleAsync(ProductSchedule schedule, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// 新增时段规则关联商品。
|
||||
/// </summary>
|
||||
Task AddScheduleProductsAsync(IEnumerable<ProductScheduleProduct> relations, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// 新增商品。
|
||||
/// </summary>
|
||||
@@ -337,6 +367,11 @@ public interface IProductRepository
|
||||
/// </summary>
|
||||
Task UpdateLabelAsync(ProductLabel label, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// 更新时段规则。
|
||||
/// </summary>
|
||||
Task UpdateScheduleAsync(ProductSchedule schedule, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// 删除分类。
|
||||
/// </summary>
|
||||
@@ -356,6 +391,11 @@ public interface IProductRepository
|
||||
/// </summary>
|
||||
Task DeleteLabelAsync(long labelId, long tenantId, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// 删除时段规则。
|
||||
/// </summary>
|
||||
Task DeleteScheduleAsync(long scheduleId, long tenantId, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// 删除商品下的 SKU。
|
||||
/// </summary>
|
||||
@@ -380,6 +420,11 @@ public interface IProductRepository
|
||||
/// </summary>
|
||||
Task RemoveLabelProductsAsync(long labelId, long tenantId, long storeId, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// 删除时段规则关联商品。
|
||||
/// </summary>
|
||||
Task RemoveScheduleProductsAsync(long scheduleId, long tenantId, long storeId, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// 删除商品下的加料组及选项。
|
||||
/// </summary>
|
||||
|
||||
@@ -221,6 +221,14 @@ public sealed class TakeoutAppDbContext(
|
||||
/// </summary>
|
||||
public DbSet<ProductLabelProduct> ProductLabelProducts => Set<ProductLabelProduct>();
|
||||
/// <summary>
|
||||
/// 商品时段规则。
|
||||
/// </summary>
|
||||
public DbSet<ProductSchedule> ProductSchedules => Set<ProductSchedule>();
|
||||
/// <summary>
|
||||
/// 时段规则与商品关联。
|
||||
/// </summary>
|
||||
public DbSet<ProductScheduleProduct> ProductScheduleProducts => Set<ProductScheduleProduct>();
|
||||
/// <summary>
|
||||
/// SKU 实体。
|
||||
/// </summary>
|
||||
public DbSet<ProductSku> ProductSkus => Set<ProductSku>();
|
||||
@@ -466,6 +474,8 @@ public sealed class TakeoutAppDbContext(
|
||||
ConfigureProductSpecTemplateProduct(modelBuilder.Entity<ProductSpecTemplateProduct>());
|
||||
ConfigureProductLabel(modelBuilder.Entity<ProductLabel>());
|
||||
ConfigureProductLabelProduct(modelBuilder.Entity<ProductLabelProduct>());
|
||||
ConfigureProductSchedule(modelBuilder.Entity<ProductSchedule>());
|
||||
ConfigureProductScheduleProduct(modelBuilder.Entity<ProductScheduleProduct>());
|
||||
ConfigureProductSku(modelBuilder.Entity<ProductSku>());
|
||||
ConfigureProductAddonGroup(modelBuilder.Entity<ProductAddonGroup>());
|
||||
ConfigureProductAddonOption(modelBuilder.Entity<ProductAddonOption>());
|
||||
@@ -1255,6 +1265,31 @@ public sealed class TakeoutAppDbContext(
|
||||
builder.HasIndex(x => new { x.TenantId, x.StoreId, x.ProductId });
|
||||
}
|
||||
|
||||
private static void ConfigureProductSchedule(EntityTypeBuilder<ProductSchedule> builder)
|
||||
{
|
||||
builder.ToTable("product_schedules");
|
||||
builder.HasKey(x => x.Id);
|
||||
builder.Property(x => x.StoreId).IsRequired();
|
||||
builder.Property(x => x.Name).HasMaxLength(64).IsRequired();
|
||||
builder.Property(x => x.StartTime).IsRequired();
|
||||
builder.Property(x => x.EndTime).IsRequired();
|
||||
builder.Property(x => x.WeekDaysMask).IsRequired();
|
||||
builder.Property(x => x.IsEnabled).IsRequired();
|
||||
builder.HasIndex(x => new { x.TenantId, x.StoreId, x.Name }).IsUnique();
|
||||
builder.HasIndex(x => new { x.TenantId, x.StoreId, x.IsEnabled });
|
||||
}
|
||||
|
||||
private static void ConfigureProductScheduleProduct(EntityTypeBuilder<ProductScheduleProduct> builder)
|
||||
{
|
||||
builder.ToTable("product_schedule_products");
|
||||
builder.HasKey(x => x.Id);
|
||||
builder.Property(x => x.StoreId).IsRequired();
|
||||
builder.Property(x => x.ScheduleId).IsRequired();
|
||||
builder.Property(x => x.ProductId).IsRequired();
|
||||
builder.HasIndex(x => new { x.TenantId, x.StoreId, x.ScheduleId, x.ProductId }).IsUnique();
|
||||
builder.HasIndex(x => new { x.TenantId, x.StoreId, x.ProductId });
|
||||
}
|
||||
|
||||
private static void ConfigureProductSku(EntityTypeBuilder<ProductSku> builder)
|
||||
{
|
||||
builder.ToTable("product_skus");
|
||||
|
||||
@@ -172,6 +172,17 @@ public sealed class EfProductRepository(TakeoutAppDbContext context) : IProductR
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IReadOnlyList<ProductSchedule>> GetSchedulesByStoreAsync(long tenantId, long storeId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await context.ProductSchedules
|
||||
.AsNoTracking()
|
||||
.Where(x => x.TenantId == tenantId && x.StoreId == storeId)
|
||||
.OrderBy(x => x.Name)
|
||||
.ThenBy(x => x.Id)
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<ProductSpecTemplate?> FindSpecTemplateByIdAsync(long templateId, long tenantId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
@@ -202,6 +213,14 @@ public sealed class EfProductRepository(TakeoutAppDbContext context) : IProductR
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<ProductSchedule?> FindScheduleByIdAsync(long scheduleId, long tenantId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return context.ProductSchedules
|
||||
.Where(x => x.TenantId == tenantId && x.Id == scheduleId)
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<bool> ExistsSpecTemplateNameAsync(long tenantId, long storeId, string name, long? excludeTemplateId = null, CancellationToken cancellationToken = default)
|
||||
{
|
||||
@@ -264,6 +283,26 @@ public sealed class EfProductRepository(TakeoutAppDbContext context) : IProductR
|
||||
return query.AnyAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<bool> ExistsScheduleNameAsync(long tenantId, long storeId, string name, long? excludeScheduleId = null, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var normalizedName = (name ?? string.Empty).Trim();
|
||||
var normalizedLower = normalizedName.ToLowerInvariant();
|
||||
var query = context.ProductSchedules
|
||||
.AsNoTracking()
|
||||
.Where(x =>
|
||||
x.TenantId == tenantId &&
|
||||
x.StoreId == storeId &&
|
||||
x.Name.ToLower() == normalizedLower);
|
||||
|
||||
if (excludeScheduleId.HasValue)
|
||||
{
|
||||
query = query.Where(x => x.Id != excludeScheduleId.Value);
|
||||
}
|
||||
|
||||
return query.AnyAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IReadOnlyList<ProductSpecTemplateOption>> GetSpecTemplateOptionsByTemplateIdsAsync(IReadOnlyCollection<long> templateIds, long tenantId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
@@ -318,6 +357,25 @@ public sealed class EfProductRepository(TakeoutAppDbContext context) : IProductR
|
||||
.ToDictionaryAsync(item => item.LabelId, item => item.Count, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IReadOnlyList<ProductScheduleProduct>> GetScheduleProductsByScheduleIdsAsync(IReadOnlyCollection<long> scheduleIds, long tenantId, long storeId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (scheduleIds.Count == 0)
|
||||
{
|
||||
return Array.Empty<ProductScheduleProduct>();
|
||||
}
|
||||
|
||||
return await context.ProductScheduleProducts
|
||||
.AsNoTracking()
|
||||
.Where(x =>
|
||||
x.TenantId == tenantId &&
|
||||
x.StoreId == storeId &&
|
||||
scheduleIds.Contains(x.ScheduleId))
|
||||
.OrderBy(x => x.ScheduleId)
|
||||
.ThenBy(x => x.ProductId)
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IReadOnlyList<long>> FilterExistingProductIdsAsync(long tenantId, long storeId, IReadOnlyCollection<long> productIds, CancellationToken cancellationToken = default)
|
||||
{
|
||||
@@ -649,6 +707,18 @@ public sealed class EfProductRepository(TakeoutAppDbContext context) : IProductR
|
||||
return context.ProductLabels.AddAsync(label, cancellationToken).AsTask();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task AddScheduleAsync(ProductSchedule schedule, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return context.ProductSchedules.AddAsync(schedule, cancellationToken).AsTask();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task AddScheduleProductsAsync(IEnumerable<ProductScheduleProduct> relations, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return context.ProductScheduleProducts.AddRangeAsync(relations, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task AddProductAsync(Product product, CancellationToken cancellationToken = default)
|
||||
{
|
||||
@@ -744,6 +814,13 @@ public sealed class EfProductRepository(TakeoutAppDbContext context) : IProductR
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task UpdateScheduleAsync(ProductSchedule schedule, CancellationToken cancellationToken = default)
|
||||
{
|
||||
context.ProductSchedules.Update(schedule);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task DeleteCategoryAsync(long categoryId, long tenantId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
@@ -793,6 +870,23 @@ public sealed class EfProductRepository(TakeoutAppDbContext context) : IProductR
|
||||
.ExecuteDeleteAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task DeleteScheduleAsync(long scheduleId, long tenantId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var existing = await context.ProductSchedules
|
||||
.Where(x => x.TenantId == tenantId && x.Id == scheduleId)
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
|
||||
if (existing is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await context.ProductSchedules
|
||||
.Where(x => x.TenantId == tenantId && x.Id == scheduleId)
|
||||
.ExecuteDeleteAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task RemoveSkusAsync(long productId, long tenantId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
@@ -838,6 +932,17 @@ public sealed class EfProductRepository(TakeoutAppDbContext context) : IProductR
|
||||
.ExecuteDeleteAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task RemoveScheduleProductsAsync(long scheduleId, long tenantId, long storeId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
await context.ProductScheduleProducts
|
||||
.Where(x =>
|
||||
x.TenantId == tenantId &&
|
||||
x.StoreId == storeId &&
|
||||
x.ScheduleId == scheduleId)
|
||||
.ExecuteDeleteAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task RemoveAddonGroupsAsync(long productId, long tenantId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
|
||||
8371
src/Infrastructure/TakeoutSaaS.Infrastructure/Migrations/20260221032856_AddProductSchedules.Designer.cs
generated
Normal file
8371
src/Infrastructure/TakeoutSaaS.Infrastructure/Migrations/20260221032856_AddProductSchedules.Designer.cs
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,97 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace TakeoutSaaS.Infrastructure.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class AddProductSchedules : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "product_schedule_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: "所属门店。"),
|
||||
ScheduleId = 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_schedule_products", x => x.Id);
|
||||
},
|
||||
comment: "时段规则与商品关联关系。");
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "product_schedules",
|
||||
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: "规则名称。"),
|
||||
StartTime = table.Column<TimeSpan>(type: "interval", nullable: false, comment: "开始时间。"),
|
||||
EndTime = table.Column<TimeSpan>(type: "interval", nullable: false, comment: "结束时间。"),
|
||||
WeekDaysMask = 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_schedules", x => x.Id);
|
||||
},
|
||||
comment: "商品时段规则(门店维度)。");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_product_schedule_products_TenantId_StoreId_ProductId",
|
||||
table: "product_schedule_products",
|
||||
columns: new[] { "TenantId", "StoreId", "ProductId" });
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_product_schedule_products_TenantId_StoreId_ScheduleId_Produ~",
|
||||
table: "product_schedule_products",
|
||||
columns: new[] { "TenantId", "StoreId", "ScheduleId", "ProductId" },
|
||||
unique: true);
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_product_schedules_TenantId_StoreId_IsEnabled",
|
||||
table: "product_schedules",
|
||||
columns: new[] { "TenantId", "StoreId", "IsEnabled" });
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_product_schedules_TenantId_StoreId_Name",
|
||||
table: "product_schedules",
|
||||
columns: new[] { "TenantId", "StoreId", "Name" },
|
||||
unique: true);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "product_schedule_products");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "product_schedules");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4709,6 +4709,144 @@ namespace TakeoutSaaS.Infrastructure.Migrations
|
||||
});
|
||||
});
|
||||
|
||||
modelBuilder.Entity("TakeoutSaaS.Domain.Products.Entities.ProductSchedule", 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<TimeSpan>("EndTime")
|
||||
.HasColumnType("interval")
|
||||
.HasComment("结束时间。");
|
||||
|
||||
b.Property<bool>("IsEnabled")
|
||||
.HasColumnType("boolean")
|
||||
.HasComment("是否启用。");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasMaxLength(64)
|
||||
.HasColumnType("character varying(64)")
|
||||
.HasComment("规则名称。");
|
||||
|
||||
b.Property<TimeSpan>("StartTime")
|
||||
.HasColumnType("interval")
|
||||
.HasComment("开始时间。");
|
||||
|
||||
b.Property<long>("StoreId")
|
||||
.HasColumnType("bigint")
|
||||
.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.Property<int>("WeekDaysMask")
|
||||
.HasColumnType("integer")
|
||||
.HasComment("星期位掩码(周一到周日)。");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("TenantId", "StoreId", "IsEnabled");
|
||||
|
||||
b.HasIndex("TenantId", "StoreId", "Name")
|
||||
.IsUnique();
|
||||
|
||||
b.ToTable("product_schedules", null, t =>
|
||||
{
|
||||
t.HasComment("商品时段规则(门店维度)。");
|
||||
});
|
||||
});
|
||||
|
||||
modelBuilder.Entity("TakeoutSaaS.Domain.Products.Entities.ProductScheduleProduct", 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>("ScheduleId")
|
||||
.HasColumnType("bigint")
|
||||
.HasComment("时段规则 ID。");
|
||||
|
||||
b.Property<long>("StoreId")
|
||||
.HasColumnType("bigint")
|
||||
.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", "ProductId");
|
||||
|
||||
b.HasIndex("TenantId", "StoreId", "ScheduleId", "ProductId")
|
||||
.IsUnique();
|
||||
|
||||
b.ToTable("product_schedule_products", null, t =>
|
||||
{
|
||||
t.HasComment("时段规则与商品关联关系。");
|
||||
});
|
||||
});
|
||||
|
||||
modelBuilder.Entity("TakeoutSaaS.Domain.Products.Entities.ProductSku", b =>
|
||||
{
|
||||
b.Property<long>("Id")
|
||||
|
||||
Reference in New Issue
Block a user