feat: 门店员工与排班管理上线
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
using MediatR;
|
||||
using TakeoutSaaS.Application.App.Stores.Dto;
|
||||
using TakeoutSaaS.Domain.Merchants.Enums;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Stores.Commands;
|
||||
|
||||
/// <summary>
|
||||
/// 创建员工排班命令。
|
||||
/// </summary>
|
||||
public sealed record CreateStoreEmployeeShiftCommand : IRequest<StoreEmployeeShiftDto>
|
||||
{
|
||||
/// <summary>
|
||||
/// 门店 ID。
|
||||
/// </summary>
|
||||
public long StoreId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 员工 ID。
|
||||
/// </summary>
|
||||
public long StaffId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 班次日期。
|
||||
/// </summary>
|
||||
public DateTime ShiftDate { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 开始时间。
|
||||
/// </summary>
|
||||
public TimeSpan StartTime { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 结束时间。
|
||||
/// </summary>
|
||||
public TimeSpan EndTime { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 排班角色。
|
||||
/// </summary>
|
||||
public StaffRoleType RoleType { get; init; } = StaffRoleType.FrontDesk;
|
||||
|
||||
/// <summary>
|
||||
/// 备注。
|
||||
/// </summary>
|
||||
public string? Notes { get; init; }
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using MediatR;
|
||||
using TakeoutSaaS.Application.App.Stores.Dto;
|
||||
using TakeoutSaaS.Domain.Merchants.Enums;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Stores.Commands;
|
||||
|
||||
/// <summary>
|
||||
/// 创建门店员工命令。
|
||||
/// </summary>
|
||||
public sealed record CreateStoreStaffCommand : IRequest<StoreStaffDto>
|
||||
{
|
||||
/// <summary>
|
||||
/// 门店 ID。
|
||||
/// </summary>
|
||||
public long StoreId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 姓名。
|
||||
/// </summary>
|
||||
public string Name { get; init; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 手机号。
|
||||
/// </summary>
|
||||
public string Phone { get; init; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 邮箱。
|
||||
/// </summary>
|
||||
public string? Email { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 角色。
|
||||
/// </summary>
|
||||
public StaffRoleType RoleType { get; init; } = StaffRoleType.FrontDesk;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using MediatR;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Stores.Commands;
|
||||
|
||||
/// <summary>
|
||||
/// 删除员工排班命令。
|
||||
/// </summary>
|
||||
public sealed record DeleteStoreEmployeeShiftCommand : IRequest<bool>
|
||||
{
|
||||
/// <summary>
|
||||
/// 门店 ID。
|
||||
/// </summary>
|
||||
public long StoreId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 排班 ID。
|
||||
/// </summary>
|
||||
public long ShiftId { get; init; }
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using MediatR;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Stores.Commands;
|
||||
|
||||
/// <summary>
|
||||
/// 删除门店员工命令。
|
||||
/// </summary>
|
||||
public sealed record DeleteStoreStaffCommand : IRequest<bool>
|
||||
{
|
||||
/// <summary>
|
||||
/// 门店 ID。
|
||||
/// </summary>
|
||||
public long StoreId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 员工 ID。
|
||||
/// </summary>
|
||||
public long StaffId { get; init; }
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
using MediatR;
|
||||
using TakeoutSaaS.Application.App.Stores.Dto;
|
||||
using TakeoutSaaS.Domain.Merchants.Enums;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Stores.Commands;
|
||||
|
||||
/// <summary>
|
||||
/// 更新员工排班命令。
|
||||
/// </summary>
|
||||
public sealed record UpdateStoreEmployeeShiftCommand : IRequest<StoreEmployeeShiftDto?>
|
||||
{
|
||||
/// <summary>
|
||||
/// 排班 ID。
|
||||
/// </summary>
|
||||
public long ShiftId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 门店 ID。
|
||||
/// </summary>
|
||||
public long StoreId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 员工 ID。
|
||||
/// </summary>
|
||||
public long StaffId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 班次日期。
|
||||
/// </summary>
|
||||
public DateTime ShiftDate { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 开始时间。
|
||||
/// </summary>
|
||||
public TimeSpan StartTime { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 结束时间。
|
||||
/// </summary>
|
||||
public TimeSpan EndTime { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 排班角色。
|
||||
/// </summary>
|
||||
public StaffRoleType RoleType { get; init; } = StaffRoleType.FrontDesk;
|
||||
|
||||
/// <summary>
|
||||
/// 备注。
|
||||
/// </summary>
|
||||
public string? Notes { get; init; }
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
using MediatR;
|
||||
using TakeoutSaaS.Application.App.Stores.Dto;
|
||||
using TakeoutSaaS.Domain.Merchants.Enums;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Stores.Commands;
|
||||
|
||||
/// <summary>
|
||||
/// 更新门店员工命令。
|
||||
/// </summary>
|
||||
public sealed record UpdateStoreStaffCommand : IRequest<StoreStaffDto?>
|
||||
{
|
||||
/// <summary>
|
||||
/// 员工 ID。
|
||||
/// </summary>
|
||||
public long StaffId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 门店 ID。
|
||||
/// </summary>
|
||||
public long StoreId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 姓名。
|
||||
/// </summary>
|
||||
public string Name { get; init; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 手机号。
|
||||
/// </summary>
|
||||
public string Phone { get; init; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 邮箱。
|
||||
/// </summary>
|
||||
public string? Email { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 角色。
|
||||
/// </summary>
|
||||
public StaffRoleType RoleType { get; init; } = StaffRoleType.FrontDesk;
|
||||
|
||||
/// <summary>
|
||||
/// 状态。
|
||||
/// </summary>
|
||||
public StaffStatus Status { get; init; } = StaffStatus.Active;
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
using System.Text.Json.Serialization;
|
||||
using TakeoutSaaS.Domain.Merchants.Enums;
|
||||
using TakeoutSaaS.Shared.Abstractions.Serialization;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Stores.Dto;
|
||||
|
||||
/// <summary>
|
||||
/// 员工排班 DTO。
|
||||
/// </summary>
|
||||
public sealed record StoreEmployeeShiftDto
|
||||
{
|
||||
/// <summary>
|
||||
/// 排班 ID。
|
||||
/// </summary>
|
||||
[JsonConverter(typeof(SnowflakeIdJsonConverter))]
|
||||
public long Id { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 租户 ID。
|
||||
/// </summary>
|
||||
[JsonConverter(typeof(SnowflakeIdJsonConverter))]
|
||||
public long TenantId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 门店 ID。
|
||||
/// </summary>
|
||||
[JsonConverter(typeof(SnowflakeIdJsonConverter))]
|
||||
public long StoreId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 员工 ID。
|
||||
/// </summary>
|
||||
[JsonConverter(typeof(SnowflakeIdJsonConverter))]
|
||||
public long StaffId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 班次日期。
|
||||
/// </summary>
|
||||
public DateTime ShiftDate { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 开始时间。
|
||||
/// </summary>
|
||||
public TimeSpan StartTime { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 结束时间。
|
||||
/// </summary>
|
||||
public TimeSpan EndTime { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 排班角色。
|
||||
/// </summary>
|
||||
public StaffRoleType RoleType { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 备注。
|
||||
/// </summary>
|
||||
public string? Notes { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 创建时间。
|
||||
/// </summary>
|
||||
public DateTime CreatedAt { get; init; }
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
using System.Text.Json.Serialization;
|
||||
using TakeoutSaaS.Domain.Merchants.Enums;
|
||||
using TakeoutSaaS.Shared.Abstractions.Serialization;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Stores.Dto;
|
||||
|
||||
/// <summary>
|
||||
/// 门店员工 DTO。
|
||||
/// </summary>
|
||||
public sealed record StoreStaffDto
|
||||
{
|
||||
/// <summary>
|
||||
/// 员工 ID。
|
||||
/// </summary>
|
||||
[JsonConverter(typeof(SnowflakeIdJsonConverter))]
|
||||
public long Id { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 租户 ID。
|
||||
/// </summary>
|
||||
[JsonConverter(typeof(SnowflakeIdJsonConverter))]
|
||||
public long TenantId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 商户 ID。
|
||||
/// </summary>
|
||||
[JsonConverter(typeof(SnowflakeIdJsonConverter))]
|
||||
public long MerchantId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 门店 ID。
|
||||
/// </summary>
|
||||
[JsonConverter(typeof(SnowflakeIdJsonConverter))]
|
||||
public long? StoreId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 姓名。
|
||||
/// </summary>
|
||||
public string Name { get; init; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 手机号。
|
||||
/// </summary>
|
||||
public string Phone { get; init; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 邮箱。
|
||||
/// </summary>
|
||||
public string? Email { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 角色类型。
|
||||
/// </summary>
|
||||
public StaffRoleType RoleType { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 状态。
|
||||
/// </summary>
|
||||
public StaffStatus Status { get; init; }
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using TakeoutSaaS.Application.App.Stores.Commands;
|
||||
using TakeoutSaaS.Application.App.Stores.Dto;
|
||||
using TakeoutSaaS.Domain.Merchants.Repositories;
|
||||
using TakeoutSaaS.Domain.Stores.Entities;
|
||||
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 CreateStoreEmployeeShiftCommandHandler(
|
||||
IStoreRepository storeRepository,
|
||||
IMerchantRepository merchantRepository,
|
||||
ITenantProvider tenantProvider,
|
||||
ILogger<CreateStoreEmployeeShiftCommandHandler> logger)
|
||||
: IRequestHandler<CreateStoreEmployeeShiftCommand, StoreEmployeeShiftDto>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task<StoreEmployeeShiftDto> Handle(CreateStoreEmployeeShiftCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
// 1. 校验门店存在
|
||||
var tenantId = tenantProvider.GetCurrentTenantId();
|
||||
var store = await storeRepository.FindByIdAsync(request.StoreId, tenantId, cancellationToken);
|
||||
if (store is null)
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.NotFound, "门店不存在");
|
||||
}
|
||||
|
||||
// 2. 校验员工归属与状态
|
||||
var staff = await merchantRepository.FindStaffByIdAsync(request.StaffId, tenantId, cancellationToken);
|
||||
if (staff is null || (staff.StoreId.HasValue && staff.StoreId != request.StoreId))
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.ValidationFailed, "员工不存在或不属于该门店");
|
||||
}
|
||||
|
||||
// 3. 校验日期与冲突
|
||||
var from = request.ShiftDate.Date;
|
||||
var to = request.ShiftDate.Date;
|
||||
var shifts = await storeRepository.GetShiftsAsync(request.StoreId, tenantId, from, to, cancellationToken);
|
||||
var hasConflict = shifts.Any(x => x.StaffId == request.StaffId && x.ShiftDate == request.ShiftDate);
|
||||
if (hasConflict)
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.Conflict, "该员工当日已存在排班");
|
||||
}
|
||||
|
||||
// 4. 构建实体
|
||||
var shift = new StoreEmployeeShift
|
||||
{
|
||||
StoreId = request.StoreId,
|
||||
StaffId = request.StaffId,
|
||||
ShiftDate = request.ShiftDate.Date,
|
||||
StartTime = request.StartTime,
|
||||
EndTime = request.EndTime,
|
||||
RoleType = request.RoleType,
|
||||
Notes = request.Notes?.Trim()
|
||||
};
|
||||
|
||||
// 5. 持久化
|
||||
await storeRepository.AddShiftsAsync(new[] { shift }, cancellationToken);
|
||||
await storeRepository.SaveChangesAsync(cancellationToken);
|
||||
logger.LogInformation("创建排班 {ShiftId} 员工 {StaffId} 门店 {StoreId}", shift.Id, shift.StaffId, shift.StoreId);
|
||||
|
||||
// 6. 返回 DTO
|
||||
return StoreMapping.ToDto(shift);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using TakeoutSaaS.Application.App.Stores.Commands;
|
||||
using TakeoutSaaS.Application.App.Stores.Dto;
|
||||
using TakeoutSaaS.Domain.Merchants.Entities;
|
||||
using TakeoutSaaS.Domain.Merchants.Enums;
|
||||
using TakeoutSaaS.Domain.Merchants.Repositories;
|
||||
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 CreateStoreStaffCommandHandler(
|
||||
IMerchantRepository merchantRepository,
|
||||
IStoreRepository storeRepository,
|
||||
ITenantProvider tenantProvider,
|
||||
ILogger<CreateStoreStaffCommandHandler> logger)
|
||||
: IRequestHandler<CreateStoreStaffCommand, StoreStaffDto>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task<StoreStaffDto> Handle(CreateStoreStaffCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
// 1. 校验门店
|
||||
var tenantId = tenantProvider.GetCurrentTenantId();
|
||||
var store = await storeRepository.FindByIdAsync(request.StoreId, tenantId, cancellationToken);
|
||||
if (store is null)
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.NotFound, "门店不存在");
|
||||
}
|
||||
|
||||
// 2. 组装员工
|
||||
var staff = new MerchantStaff
|
||||
{
|
||||
MerchantId = store.MerchantId,
|
||||
StoreId = request.StoreId,
|
||||
Name = request.Name.Trim(),
|
||||
Phone = request.Phone.Trim(),
|
||||
Email = request.Email?.Trim(),
|
||||
RoleType = request.RoleType,
|
||||
Status = StaffStatus.Active
|
||||
};
|
||||
|
||||
// 3. 持久化
|
||||
await merchantRepository.AddStaffAsync(staff, cancellationToken);
|
||||
await merchantRepository.SaveChangesAsync(cancellationToken);
|
||||
logger.LogInformation("创建门店员工 {StaffId} 门店 {StoreId}", staff.Id, request.StoreId);
|
||||
|
||||
// 4. 返回 DTO
|
||||
return StoreMapping.ToDto(staff);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using TakeoutSaaS.Application.App.Stores.Commands;
|
||||
using TakeoutSaaS.Domain.Stores.Repositories;
|
||||
using TakeoutSaaS.Shared.Abstractions.Tenancy;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Stores.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// 删除排班处理器。
|
||||
/// </summary>
|
||||
public sealed class DeleteStoreEmployeeShiftCommandHandler(
|
||||
IStoreRepository storeRepository,
|
||||
ITenantProvider tenantProvider,
|
||||
ILogger<DeleteStoreEmployeeShiftCommandHandler> logger)
|
||||
: IRequestHandler<DeleteStoreEmployeeShiftCommand, bool>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task<bool> Handle(DeleteStoreEmployeeShiftCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
// 1. 读取排班
|
||||
var tenantId = tenantProvider.GetCurrentTenantId();
|
||||
var shift = await storeRepository.FindShiftByIdAsync(request.ShiftId, tenantId, cancellationToken);
|
||||
if (shift is null || shift.StoreId != request.StoreId)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// 2. 删除
|
||||
await storeRepository.DeleteShiftAsync(request.ShiftId, tenantId, cancellationToken);
|
||||
await storeRepository.SaveChangesAsync(cancellationToken);
|
||||
logger.LogInformation("删除排班 {ShiftId} 门店 {StoreId}", request.ShiftId, request.StoreId);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using TakeoutSaaS.Application.App.Stores.Commands;
|
||||
using TakeoutSaaS.Domain.Merchants.Repositories;
|
||||
using TakeoutSaaS.Shared.Abstractions.Tenancy;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Stores.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// 删除门店员工处理器。
|
||||
/// </summary>
|
||||
public sealed class DeleteStoreStaffCommandHandler(
|
||||
IMerchantRepository merchantRepository,
|
||||
ITenantProvider tenantProvider,
|
||||
ILogger<DeleteStoreStaffCommandHandler> logger)
|
||||
: IRequestHandler<DeleteStoreStaffCommand, bool>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task<bool> Handle(DeleteStoreStaffCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var tenantId = tenantProvider.GetCurrentTenantId();
|
||||
var staff = await merchantRepository.FindStaffByIdAsync(request.StaffId, tenantId, cancellationToken);
|
||||
if (staff is null || staff.StoreId != request.StoreId)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// 逻辑删除未定义,直接物理删除
|
||||
await merchantRepository.DeleteStaffAsync(staff.Id, tenantId, cancellationToken);
|
||||
await merchantRepository.SaveChangesAsync(cancellationToken);
|
||||
logger.LogInformation("删除门店员工 {StaffId} 门店 {StoreId}", request.StaffId, request.StoreId);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using System.Linq;
|
||||
using MediatR;
|
||||
using TakeoutSaaS.Application.App.Stores.Dto;
|
||||
using TakeoutSaaS.Application.App.Stores.Queries;
|
||||
using TakeoutSaaS.Domain.Stores.Repositories;
|
||||
using TakeoutSaaS.Shared.Abstractions.Tenancy;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Stores.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// 排班列表查询处理器。
|
||||
/// </summary>
|
||||
public sealed class ListStoreEmployeeShiftsQueryHandler(
|
||||
IStoreRepository storeRepository,
|
||||
ITenantProvider tenantProvider)
|
||||
: IRequestHandler<ListStoreEmployeeShiftsQuery, IReadOnlyList<StoreEmployeeShiftDto>>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task<IReadOnlyList<StoreEmployeeShiftDto>> Handle(ListStoreEmployeeShiftsQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
// 1. 时间范围
|
||||
var from = request.From ?? DateTime.UtcNow.Date;
|
||||
var to = request.To ?? from.AddDays(7);
|
||||
var tenantId = tenantProvider.GetCurrentTenantId();
|
||||
|
||||
// 2. 查询排班
|
||||
var shifts = await storeRepository.GetShiftsAsync(request.StoreId, tenantId, from, to, cancellationToken);
|
||||
|
||||
if (request.StaffId.HasValue)
|
||||
{
|
||||
shifts = shifts.Where(x => x.StaffId == request.StaffId.Value).ToList();
|
||||
}
|
||||
|
||||
// 3. 映射 DTO
|
||||
return shifts.Select(StoreMapping.ToDto).ToList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
using System.Linq;
|
||||
using MediatR;
|
||||
using TakeoutSaaS.Application.App.Stores.Dto;
|
||||
using TakeoutSaaS.Application.App.Stores.Queries;
|
||||
using TakeoutSaaS.Domain.Merchants.Repositories;
|
||||
using TakeoutSaaS.Domain.Stores.Repositories;
|
||||
using TakeoutSaaS.Shared.Abstractions.Tenancy;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Stores.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// 门店员工列表查询处理器。
|
||||
/// </summary>
|
||||
public sealed class ListStoreStaffQueryHandler(
|
||||
IMerchantRepository merchantRepository,
|
||||
IStoreRepository storeRepository,
|
||||
ITenantProvider tenantProvider)
|
||||
: IRequestHandler<ListStoreStaffQuery, IReadOnlyList<StoreStaffDto>>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task<IReadOnlyList<StoreStaffDto>> Handle(ListStoreStaffQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
// 1. 校验门店存在
|
||||
var tenantId = tenantProvider.GetCurrentTenantId();
|
||||
var store = await storeRepository.FindByIdAsync(request.StoreId, tenantId, cancellationToken);
|
||||
if (store is null)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
// 2. 查询员工
|
||||
var staffs = await merchantRepository.GetStaffByStoreAsync(request.StoreId, tenantId, cancellationToken);
|
||||
|
||||
if (request.RoleType.HasValue)
|
||||
{
|
||||
staffs = staffs.Where(x => x.RoleType == request.RoleType.Value).ToList();
|
||||
}
|
||||
|
||||
if (request.Status.HasValue)
|
||||
{
|
||||
staffs = staffs.Where(x => x.Status == request.Status.Value).ToList();
|
||||
}
|
||||
|
||||
// 3. 映射 DTO
|
||||
return staffs.Select(StoreMapping.ToDto).ToList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using TakeoutSaaS.Application.App.Stores.Commands;
|
||||
using TakeoutSaaS.Application.App.Stores.Dto;
|
||||
using TakeoutSaaS.Domain.Merchants.Repositories;
|
||||
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 UpdateStoreEmployeeShiftCommandHandler(
|
||||
IStoreRepository storeRepository,
|
||||
IMerchantRepository merchantRepository,
|
||||
ITenantProvider tenantProvider,
|
||||
ILogger<UpdateStoreEmployeeShiftCommandHandler> logger)
|
||||
: IRequestHandler<UpdateStoreEmployeeShiftCommand, StoreEmployeeShiftDto?>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task<StoreEmployeeShiftDto?> Handle(UpdateStoreEmployeeShiftCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
// 1. 读取排班
|
||||
var tenantId = tenantProvider.GetCurrentTenantId();
|
||||
var shift = await storeRepository.FindShiftByIdAsync(request.ShiftId, tenantId, cancellationToken);
|
||||
if (shift is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// 2. 校验门店归属
|
||||
if (shift.StoreId != request.StoreId)
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.ValidationFailed, "排班不属于该门店");
|
||||
}
|
||||
|
||||
// 3. 校验员工归属
|
||||
var staff = await merchantRepository.FindStaffByIdAsync(request.StaffId, tenantId, cancellationToken);
|
||||
if (staff is null || (staff.StoreId.HasValue && staff.StoreId != request.StoreId))
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.ValidationFailed, "员工不存在或不属于该门店");
|
||||
}
|
||||
|
||||
// 4. 冲突校验
|
||||
var shifts = await storeRepository.GetShiftsAsync(request.StoreId, tenantId, request.ShiftDate.Date, request.ShiftDate.Date, cancellationToken);
|
||||
var hasConflict = shifts.Any(x => x.Id != request.ShiftId && x.StaffId == request.StaffId && x.ShiftDate == request.ShiftDate);
|
||||
if (hasConflict)
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.Conflict, "该员工当日已存在排班");
|
||||
}
|
||||
|
||||
// 5. 更新字段
|
||||
shift.StaffId = request.StaffId;
|
||||
shift.ShiftDate = request.ShiftDate.Date;
|
||||
shift.StartTime = request.StartTime;
|
||||
shift.EndTime = request.EndTime;
|
||||
shift.RoleType = request.RoleType;
|
||||
shift.Notes = request.Notes?.Trim();
|
||||
|
||||
// 6. 持久化
|
||||
await storeRepository.UpdateShiftAsync(shift, cancellationToken);
|
||||
await storeRepository.SaveChangesAsync(cancellationToken);
|
||||
logger.LogInformation("更新排班 {ShiftId} 员工 {StaffId} 门店 {StoreId}", shift.Id, shift.StaffId, shift.StoreId);
|
||||
|
||||
// 7. 返回 DTO
|
||||
return StoreMapping.ToDto(shift);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using TakeoutSaaS.Application.App.Stores.Commands;
|
||||
using TakeoutSaaS.Application.App.Stores.Dto;
|
||||
using TakeoutSaaS.Domain.Merchants.Repositories;
|
||||
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 UpdateStoreStaffCommandHandler(
|
||||
IMerchantRepository merchantRepository,
|
||||
IStoreRepository storeRepository,
|
||||
ITenantProvider tenantProvider,
|
||||
ILogger<UpdateStoreStaffCommandHandler> logger)
|
||||
: IRequestHandler<UpdateStoreStaffCommand, StoreStaffDto?>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task<StoreStaffDto?> Handle(UpdateStoreStaffCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
// 1. 校验门店
|
||||
var tenantId = tenantProvider.GetCurrentTenantId();
|
||||
var store = await storeRepository.FindByIdAsync(request.StoreId, tenantId, cancellationToken);
|
||||
if (store is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// 2. 读取员工
|
||||
var staff = await merchantRepository.FindStaffByIdAsync(request.StaffId, tenantId, cancellationToken);
|
||||
if (staff is null || staff.StoreId != request.StoreId)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// 3. 更新字段
|
||||
staff.Name = request.Name.Trim();
|
||||
staff.Phone = request.Phone.Trim();
|
||||
staff.Email = request.Email?.Trim();
|
||||
staff.RoleType = request.RoleType;
|
||||
staff.Status = request.Status;
|
||||
|
||||
// 4. 持久化
|
||||
await merchantRepository.SaveChangesAsync(cancellationToken);
|
||||
logger.LogInformation("更新门店员工 {StaffId} 门店 {StoreId}", staff.Id, staff.StoreId);
|
||||
|
||||
// 5. 返回 DTO
|
||||
return StoreMapping.ToDto(staff);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using MediatR;
|
||||
using TakeoutSaaS.Application.App.Stores.Dto;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Stores.Queries;
|
||||
|
||||
/// <summary>
|
||||
/// 员工排班列表查询(支持日期区间)。
|
||||
/// </summary>
|
||||
public sealed record ListStoreEmployeeShiftsQuery : IRequest<IReadOnlyList<StoreEmployeeShiftDto>>
|
||||
{
|
||||
/// <summary>
|
||||
/// 门店 ID。
|
||||
/// </summary>
|
||||
public long StoreId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 开始日期(含),默认今日。
|
||||
/// </summary>
|
||||
public DateTime? From { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 结束日期(含),默认今日+7。
|
||||
/// </summary>
|
||||
public DateTime? To { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 可选员工筛选。
|
||||
/// </summary>
|
||||
public long? StaffId { get; init; }
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using MediatR;
|
||||
using TakeoutSaaS.Application.App.Stores.Dto;
|
||||
using TakeoutSaaS.Domain.Merchants.Enums;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Stores.Queries;
|
||||
|
||||
/// <summary>
|
||||
/// 门店员工列表查询。
|
||||
/// </summary>
|
||||
public sealed record ListStoreStaffQuery : IRequest<IReadOnlyList<StoreStaffDto>>
|
||||
{
|
||||
/// <summary>
|
||||
/// 门店 ID。
|
||||
/// </summary>
|
||||
public long StoreId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 角色筛选。
|
||||
/// </summary>
|
||||
public StaffRoleType? RoleType { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 状态筛选。
|
||||
/// </summary>
|
||||
public StaffStatus? Status { get; init; }
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using TakeoutSaaS.Application.App.Stores.Dto;
|
||||
using TakeoutSaaS.Domain.Merchants.Entities;
|
||||
using TakeoutSaaS.Domain.Stores.Entities;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Stores;
|
||||
@@ -96,4 +97,41 @@ public static class StoreMapping
|
||||
QrCodeUrl = table.QrCodeUrl,
|
||||
CreatedAt = table.CreatedAt
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// 映射排班 DTO。
|
||||
/// </summary>
|
||||
/// <param name="shift">排班实体。</param>
|
||||
/// <returns>DTO。</returns>
|
||||
public static StoreEmployeeShiftDto ToDto(StoreEmployeeShift shift) => new()
|
||||
{
|
||||
Id = shift.Id,
|
||||
TenantId = shift.TenantId,
|
||||
StoreId = shift.StoreId,
|
||||
StaffId = shift.StaffId,
|
||||
ShiftDate = shift.ShiftDate,
|
||||
StartTime = shift.StartTime,
|
||||
EndTime = shift.EndTime,
|
||||
RoleType = shift.RoleType,
|
||||
Notes = shift.Notes,
|
||||
CreatedAt = shift.CreatedAt
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// 映射门店员工 DTO。
|
||||
/// </summary>
|
||||
/// <param name="staff">员工实体。</param>
|
||||
/// <returns>DTO。</returns>
|
||||
public static StoreStaffDto ToDto(MerchantStaff staff) => new()
|
||||
{
|
||||
Id = staff.Id,
|
||||
TenantId = staff.TenantId,
|
||||
MerchantId = staff.MerchantId,
|
||||
StoreId = staff.StoreId,
|
||||
Name = staff.Name,
|
||||
Phone = staff.Phone,
|
||||
Email = staff.Email,
|
||||
RoleType = staff.RoleType,
|
||||
Status = staff.Status
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
using FluentValidation;
|
||||
using TakeoutSaaS.Application.App.Stores.Commands;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Stores.Validators;
|
||||
|
||||
/// <summary>
|
||||
/// 创建排班命令验证器。
|
||||
/// </summary>
|
||||
public sealed class CreateStoreEmployeeShiftCommandValidator : AbstractValidator<CreateStoreEmployeeShiftCommand>
|
||||
{
|
||||
/// <summary>
|
||||
/// 初始化验证规则。
|
||||
/// </summary>
|
||||
public CreateStoreEmployeeShiftCommandValidator()
|
||||
{
|
||||
RuleFor(x => x.StoreId).GreaterThan(0);
|
||||
RuleFor(x => x.StaffId).GreaterThan(0);
|
||||
RuleFor(x => x.ShiftDate).NotEmpty();
|
||||
RuleFor(x => x.StartTime).LessThan(x => x.EndTime).WithMessage("结束时间必须晚于开始时间");
|
||||
RuleFor(x => x.Notes).MaximumLength(256);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using FluentValidation;
|
||||
using TakeoutSaaS.Application.App.Stores.Commands;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Stores.Validators;
|
||||
|
||||
/// <summary>
|
||||
/// 创建门店员工命令验证器。
|
||||
/// </summary>
|
||||
public sealed class CreateStoreStaffCommandValidator : AbstractValidator<CreateStoreStaffCommand>
|
||||
{
|
||||
/// <summary>
|
||||
/// 初始化验证规则。
|
||||
/// </summary>
|
||||
public CreateStoreStaffCommandValidator()
|
||||
{
|
||||
RuleFor(x => x.StoreId).GreaterThan(0);
|
||||
RuleFor(x => x.Name).NotEmpty().MaximumLength(64);
|
||||
RuleFor(x => x.Phone).NotEmpty().MaximumLength(32);
|
||||
RuleFor(x => x.Email).EmailAddress().When(x => !string.IsNullOrWhiteSpace(x.Email));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using FluentValidation;
|
||||
using TakeoutSaaS.Application.App.Stores.Commands;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Stores.Validators;
|
||||
|
||||
/// <summary>
|
||||
/// 更新排班命令验证器。
|
||||
/// </summary>
|
||||
public sealed class UpdateStoreEmployeeShiftCommandValidator : AbstractValidator<UpdateStoreEmployeeShiftCommand>
|
||||
{
|
||||
/// <summary>
|
||||
/// 初始化验证规则。
|
||||
/// </summary>
|
||||
public UpdateStoreEmployeeShiftCommandValidator()
|
||||
{
|
||||
RuleFor(x => x.ShiftId).GreaterThan(0);
|
||||
RuleFor(x => x.StoreId).GreaterThan(0);
|
||||
RuleFor(x => x.StaffId).GreaterThan(0);
|
||||
RuleFor(x => x.ShiftDate).NotEmpty();
|
||||
RuleFor(x => x.StartTime).LessThan(x => x.EndTime).WithMessage("结束时间必须晚于开始时间");
|
||||
RuleFor(x => x.Notes).MaximumLength(256);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using FluentValidation;
|
||||
using TakeoutSaaS.Application.App.Stores.Commands;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Stores.Validators;
|
||||
|
||||
/// <summary>
|
||||
/// 更新门店员工命令验证器。
|
||||
/// </summary>
|
||||
public sealed class UpdateStoreStaffCommandValidator : AbstractValidator<UpdateStoreStaffCommand>
|
||||
{
|
||||
/// <summary>
|
||||
/// 初始化验证规则。
|
||||
/// </summary>
|
||||
public UpdateStoreStaffCommandValidator()
|
||||
{
|
||||
RuleFor(x => x.StaffId).GreaterThan(0);
|
||||
RuleFor(x => x.StoreId).GreaterThan(0);
|
||||
RuleFor(x => x.Name).NotEmpty().MaximumLength(64);
|
||||
RuleFor(x => x.Phone).NotEmpty().MaximumLength(32);
|
||||
RuleFor(x => x.Email).EmailAddress().When(x => !string.IsNullOrWhiteSpace(x.Email));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user