diff --git a/Document/12_BusinessTodo.md b/Document/12_BusinessTodo.md index 870b3e7..455b188 100644 --- a/Document/12_BusinessTodo.md +++ b/Document/12_BusinessTodo.md @@ -20,8 +20,8 @@ - 进展:新增桌台区域/桌码 DTO、命令、查询、验证与处理器,支持批量生成桌码、区域绑定和更新;Admin API 增加桌台区域与桌码 CRUD 及二维码 ZIP 导出端点,使用 QRCoder 生成 SVG 并打包下载;仓储补齐桌台/区域的查找、更新、删除。 - [x] 员工排班:创建员工、绑定门店角色、维护 StoreEmployeeShift,可查询未来 7 日排班。 - 进展:新增门店员工 DTO/命令/查询/验证与处理器,支持员工创建/更新/删除及按门店查询;新增排班 CRUD(默认查询未来 7 天),校验员工归属、时间冲突;Admin API 增加员工与排班控制器及权限种子,仓储补充排班查询/更新/删除。 -- [ ] 桌码扫码入口:Mini 端解析二维码,GET /api/mini/tables/{code}/context 返回门店、桌台、公告。 - - 当前:MiniApi 无桌码相关接口,未实现桌码解析与上下文返回。 +- [x] 桌码扫码入口:Mini 端解析二维码,GET /api/mini/tables/{code}/context 返回门店、桌台、公告。 + - 进展:新增桌码上下文查询 DTO/验证/处理器,可按桌码解析返回门店名称/公告/标签及桌台信息;MiniApi 增加 `TablesController` 提供 `/context` 端点,仓储支持按桌码查询。 - [ ] 菜品建模:分类、SPU、SKU、规格/加料组、价格策略、媒资 CRUD + 上下架流程;Mini 端可拉取完整 JSON。 - 当前:Admin 仅有基础商品 CRUD(Product 级),未覆盖 SKU/规格/加料组、价格策略、媒资与上下架流程,Mini 端也未提供完整商品 JSON 拉取接口。 - [ ] 库存体系:SKU 库存、批次、调整、售罄管理,支持预售/档期锁定并在订单中扣减/释放。 diff --git a/src/Api/TakeoutSaaS.MiniApi/Controllers/TablesController.cs b/src/Api/TakeoutSaaS.MiniApi/Controllers/TablesController.cs new file mode 100644 index 0000000..504e35a --- /dev/null +++ b/src/Api/TakeoutSaaS.MiniApi/Controllers/TablesController.cs @@ -0,0 +1,35 @@ +using System.Collections.Generic; +using MediatR; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using TakeoutSaaS.Application.App.Stores.Dto; +using TakeoutSaaS.Application.App.Stores.Queries; +using TakeoutSaaS.Shared.Abstractions.Constants; +using TakeoutSaaS.Shared.Abstractions.Results; +using TakeoutSaaS.Shared.Web.Api; + +namespace TakeoutSaaS.MiniApi.Controllers; + +/// +/// 桌码上下文。 +/// +[ApiVersion("1.0")] +[Authorize] +[Route("api/mini/v{version:apiVersion}/tables")] +public sealed class TablesController(IMediator mediator) : BaseApiController +{ + /// + /// 解析桌码并返回上下文。 + /// + [HttpGet("{code}/context")] + [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status200OK)] + [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status404NotFound)] + public async Task> GetContext(string code, CancellationToken cancellationToken) + { + var result = await mediator.Send(new GetStoreTableContextQuery { TableCode = code }, cancellationToken); + return result is null + ? ApiResponse.Error(ErrorCodes.NotFound, "桌码不存在") + : ApiResponse.Ok(result); + } +} diff --git a/src/Application/TakeoutSaaS.Application/App/Stores/Dto/StoreTableContextDto.cs b/src/Application/TakeoutSaaS.Application/App/Stores/Dto/StoreTableContextDto.cs new file mode 100644 index 0000000..4ce0e86 --- /dev/null +++ b/src/Application/TakeoutSaaS.Application/App/Stores/Dto/StoreTableContextDto.cs @@ -0,0 +1,64 @@ +using System.Text.Json.Serialization; +using TakeoutSaaS.Domain.Stores.Enums; +using TakeoutSaaS.Shared.Abstractions.Serialization; + +namespace TakeoutSaaS.Application.App.Stores.Dto; + +/// +/// 桌码上下文 DTO。 +/// +public sealed record StoreTableContextDto +{ + /// + /// 门店 ID。 + /// + [JsonConverter(typeof(SnowflakeIdJsonConverter))] + public long StoreId { get; init; } + + /// + /// 门店名称。 + /// + public string StoreName { get; init; } = string.Empty; + + /// + /// 门店公告。 + /// + public string? Announcement { get; init; } + + /// + /// 门店标签。 + /// + public string? Tags { get; init; } + + /// + /// 桌台 ID。 + /// + [JsonConverter(typeof(SnowflakeIdJsonConverter))] + public long TableId { get; init; } + + /// + /// 桌码。 + /// + public string TableCode { get; init; } = string.Empty; + + /// + /// 区域 ID。 + /// + [JsonConverter(typeof(SnowflakeIdJsonConverter))] + public long? AreaId { get; init; } + + /// + /// 容量。 + /// + public int Capacity { get; init; } + + /// + /// 标签。 + /// + public string? TableTags { get; init; } + + /// + /// 状态。 + /// + public StoreTableStatus Status { get; init; } +} diff --git a/src/Application/TakeoutSaaS.Application/App/Stores/Handlers/GetStoreTableContextQueryHandler.cs b/src/Application/TakeoutSaaS.Application/App/Stores/Handlers/GetStoreTableContextQueryHandler.cs new file mode 100644 index 0000000..8ef1bea --- /dev/null +++ b/src/Application/TakeoutSaaS.Application/App/Stores/Handlers/GetStoreTableContextQueryHandler.cs @@ -0,0 +1,55 @@ +using MediatR; +using Microsoft.Extensions.Logging; +using TakeoutSaaS.Application.App.Stores.Dto; +using TakeoutSaaS.Application.App.Stores.Queries; +using TakeoutSaaS.Domain.Stores.Repositories; +using TakeoutSaaS.Shared.Abstractions.Constants; +using TakeoutSaaS.Shared.Abstractions.Exceptions; +using TakeoutSaaS.Shared.Abstractions.Tenancy; + +namespace TakeoutSaaS.Application.App.Stores.Handlers; + +/// +/// 桌码上下文查询处理器。 +/// +public sealed class GetStoreTableContextQueryHandler( + IStoreRepository storeRepository, + ITenantProvider tenantProvider, + ILogger logger) + : IRequestHandler +{ + /// + public async Task Handle(GetStoreTableContextQuery request, CancellationToken cancellationToken) + { + // 1. 查询桌码 + var tenantId = tenantProvider.GetCurrentTenantId(); + var table = await storeRepository.FindTableByCodeAsync(request.TableCode, tenantId, cancellationToken); + if (table is null) + { + logger.LogWarning("未找到桌码 {TableCode}", request.TableCode); + return null; + } + + // 2. 查询门店 + var store = await storeRepository.FindByIdAsync(table.StoreId, tenantId, cancellationToken); + if (store is null) + { + throw new BusinessException(ErrorCodes.NotFound, "门店不存在"); + } + + // 3. 组装上下文 + return new StoreTableContextDto + { + StoreId = store.Id, + StoreName = store.Name, + Announcement = store.Announcement, + Tags = store.Tags, + TableId = table.Id, + TableCode = table.TableCode, + AreaId = table.AreaId, + Capacity = table.Capacity, + TableTags = table.Tags, + Status = table.Status + }; + } +} diff --git a/src/Application/TakeoutSaaS.Application/App/Stores/Queries/GetStoreTableContextQuery.cs b/src/Application/TakeoutSaaS.Application/App/Stores/Queries/GetStoreTableContextQuery.cs new file mode 100644 index 0000000..2ed641e --- /dev/null +++ b/src/Application/TakeoutSaaS.Application/App/Stores/Queries/GetStoreTableContextQuery.cs @@ -0,0 +1,15 @@ +using MediatR; +using TakeoutSaaS.Application.App.Stores.Dto; + +namespace TakeoutSaaS.Application.App.Stores.Queries; + +/// +/// 桌码上下文查询。 +/// +public sealed record GetStoreTableContextQuery : IRequest +{ + /// + /// 桌码。 + /// + public string TableCode { get; init; } = string.Empty; +} diff --git a/src/Application/TakeoutSaaS.Application/App/Stores/Validators/GetStoreTableContextQueryValidator.cs b/src/Application/TakeoutSaaS.Application/App/Stores/Validators/GetStoreTableContextQueryValidator.cs new file mode 100644 index 0000000..bb80b34 --- /dev/null +++ b/src/Application/TakeoutSaaS.Application/App/Stores/Validators/GetStoreTableContextQueryValidator.cs @@ -0,0 +1,18 @@ +using FluentValidation; +using TakeoutSaaS.Application.App.Stores.Queries; + +namespace TakeoutSaaS.Application.App.Stores.Validators; + +/// +/// 桌码上下文查询验证器。 +/// +public sealed class GetStoreTableContextQueryValidator : AbstractValidator +{ + /// + /// 初始化验证规则。 + /// + public GetStoreTableContextQueryValidator() + { + RuleFor(x => x.TableCode).NotEmpty().MaximumLength(32); + } +} diff --git a/src/Domain/TakeoutSaaS.Domain/Stores/Repositories/IStoreRepository.cs b/src/Domain/TakeoutSaaS.Domain/Stores/Repositories/IStoreRepository.cs index a4d0146..2cdbfac 100644 --- a/src/Domain/TakeoutSaaS.Domain/Stores/Repositories/IStoreRepository.cs +++ b/src/Domain/TakeoutSaaS.Domain/Stores/Repositories/IStoreRepository.cs @@ -71,6 +71,11 @@ public interface IStoreRepository /// Task FindTableByIdAsync(long tableId, long tenantId, CancellationToken cancellationToken = default); + /// + /// 依据桌码获取桌台。 + /// + Task FindTableByCodeAsync(string tableCode, long tenantId, CancellationToken cancellationToken = default); + /// /// 获取门店员工排班(可选时间范围)。 /// diff --git a/src/Infrastructure/TakeoutSaaS.Infrastructure/App/Repositories/EfStoreRepository.cs b/src/Infrastructure/TakeoutSaaS.Infrastructure/App/Repositories/EfStoreRepository.cs index 5fb6ebf..0722b3c 100644 --- a/src/Infrastructure/TakeoutSaaS.Infrastructure/App/Repositories/EfStoreRepository.cs +++ b/src/Infrastructure/TakeoutSaaS.Infrastructure/App/Repositories/EfStoreRepository.cs @@ -146,6 +146,14 @@ public sealed class EfStoreRepository(TakeoutAppDbContext context) : IStoreRepos .FirstOrDefaultAsync(cancellationToken); } + /// + public Task FindTableByCodeAsync(string tableCode, long tenantId, CancellationToken cancellationToken = default) + { + return context.StoreTables + .Where(x => x.TenantId == tenantId && x.TableCode == tableCode) + .FirstOrDefaultAsync(cancellationToken); + } + /// public async Task> GetShiftsAsync(long storeId, long tenantId, DateTime? from = null, DateTime? to = null, CancellationToken cancellationToken = default) {