feat: Mini 桌码扫码上下文接口
This commit is contained in:
35
src/Api/TakeoutSaaS.MiniApi/Controllers/TablesController.cs
Normal file
35
src/Api/TakeoutSaaS.MiniApi/Controllers/TablesController.cs
Normal file
@@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// 桌码上下文。
|
||||
/// </summary>
|
||||
[ApiVersion("1.0")]
|
||||
[Authorize]
|
||||
[Route("api/mini/v{version:apiVersion}/tables")]
|
||||
public sealed class TablesController(IMediator mediator) : BaseApiController
|
||||
{
|
||||
/// <summary>
|
||||
/// 解析桌码并返回上下文。
|
||||
/// </summary>
|
||||
[HttpGet("{code}/context")]
|
||||
[ProducesResponseType(typeof(ApiResponse<StoreTableContextDto>), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status404NotFound)]
|
||||
public async Task<ApiResponse<StoreTableContextDto>> GetContext(string code, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = await mediator.Send(new GetStoreTableContextQuery { TableCode = code }, cancellationToken);
|
||||
return result is null
|
||||
? ApiResponse<StoreTableContextDto>.Error(ErrorCodes.NotFound, "桌码不存在")
|
||||
: ApiResponse<StoreTableContextDto>.Ok(result);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// 桌码上下文 DTO。
|
||||
/// </summary>
|
||||
public sealed record StoreTableContextDto
|
||||
{
|
||||
/// <summary>
|
||||
/// 门店 ID。
|
||||
/// </summary>
|
||||
[JsonConverter(typeof(SnowflakeIdJsonConverter))]
|
||||
public long StoreId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 门店名称。
|
||||
/// </summary>
|
||||
public string StoreName { get; init; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 门店公告。
|
||||
/// </summary>
|
||||
public string? Announcement { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 门店标签。
|
||||
/// </summary>
|
||||
public string? Tags { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 桌台 ID。
|
||||
/// </summary>
|
||||
[JsonConverter(typeof(SnowflakeIdJsonConverter))]
|
||||
public long TableId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 桌码。
|
||||
/// </summary>
|
||||
public string TableCode { get; init; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 区域 ID。
|
||||
/// </summary>
|
||||
[JsonConverter(typeof(SnowflakeIdJsonConverter))]
|
||||
public long? AreaId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 容量。
|
||||
/// </summary>
|
||||
public int Capacity { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 标签。
|
||||
/// </summary>
|
||||
public string? TableTags { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 状态。
|
||||
/// </summary>
|
||||
public StoreTableStatus Status { get; init; }
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// 桌码上下文查询处理器。
|
||||
/// </summary>
|
||||
public sealed class GetStoreTableContextQueryHandler(
|
||||
IStoreRepository storeRepository,
|
||||
ITenantProvider tenantProvider,
|
||||
ILogger<GetStoreTableContextQueryHandler> logger)
|
||||
: IRequestHandler<GetStoreTableContextQuery, StoreTableContextDto?>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task<StoreTableContextDto?> 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
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using MediatR;
|
||||
using TakeoutSaaS.Application.App.Stores.Dto;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Stores.Queries;
|
||||
|
||||
/// <summary>
|
||||
/// 桌码上下文查询。
|
||||
/// </summary>
|
||||
public sealed record GetStoreTableContextQuery : IRequest<StoreTableContextDto?>
|
||||
{
|
||||
/// <summary>
|
||||
/// 桌码。
|
||||
/// </summary>
|
||||
public string TableCode { get; init; } = string.Empty;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using FluentValidation;
|
||||
using TakeoutSaaS.Application.App.Stores.Queries;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Stores.Validators;
|
||||
|
||||
/// <summary>
|
||||
/// 桌码上下文查询验证器。
|
||||
/// </summary>
|
||||
public sealed class GetStoreTableContextQueryValidator : AbstractValidator<GetStoreTableContextQuery>
|
||||
{
|
||||
/// <summary>
|
||||
/// 初始化验证规则。
|
||||
/// </summary>
|
||||
public GetStoreTableContextQueryValidator()
|
||||
{
|
||||
RuleFor(x => x.TableCode).NotEmpty().MaximumLength(32);
|
||||
}
|
||||
}
|
||||
@@ -71,6 +71,11 @@ public interface IStoreRepository
|
||||
/// </summary>
|
||||
Task<StoreTable?> FindTableByIdAsync(long tableId, long tenantId, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// 依据桌码获取桌台。
|
||||
/// </summary>
|
||||
Task<StoreTable?> FindTableByCodeAsync(string tableCode, long tenantId, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// 获取门店员工排班(可选时间范围)。
|
||||
/// </summary>
|
||||
|
||||
@@ -146,6 +146,14 @@ public sealed class EfStoreRepository(TakeoutAppDbContext context) : IStoreRepos
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<StoreTable?> FindTableByCodeAsync(string tableCode, long tenantId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return context.StoreTables
|
||||
.Where(x => x.TenantId == tenantId && x.TableCode == tableCode)
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IReadOnlyList<StoreEmployeeShift>> GetShiftsAsync(long storeId, long tenantId, DateTime? from = null, DateTime? to = null, CancellationToken cancellationToken = default)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user