feat: 门店员工与排班管理上线

This commit is contained in:
2025-12-04 09:32:03 +08:00
parent 1a5209a8b1
commit 19422df0f1
31 changed files with 1265 additions and 7 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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