feat(product): add product schedule management api
All checks were successful
Build and Deploy TenantApi / build-and-deploy (push) Successful in 46s

This commit is contained in:
2026-02-21 11:46:55 +08:00
parent ad65ef3bf6
commit d41f69045f
21 changed files with 9760 additions and 0 deletions

View File

@@ -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";
}

View File

@@ -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; }
}

View File

@@ -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";
}

View File

@@ -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; }
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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();
}
}

View File

@@ -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);
}
}

View File

@@ -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
};
}
}

View File

@@ -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;
}
}

View File

@@ -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; }
}