fix: 修复员工排班保存唯一键冲突
All checks were successful
Build and Deploy TenantApi / build-and-deploy (push) Successful in 43s

This commit is contained in:
2026-02-20 09:12:21 +08:00
parent 069d7de05e
commit 669db14f64

View File

@@ -313,12 +313,8 @@ public sealed class StoreStaffController(
? CreateOffWeekShifts()
: NormalizeShifts(request.Shifts, fallback, template);
dbContext.StoreStaffWeeklySchedules.RemoveRange(existingRows);
await dbContext.StoreStaffWeeklySchedules.AddRangeAsync(
ToWeeklyEntities(parsedStoreId, parsedStaffId, shifts, template),
cancellationToken);
await dbContext.SaveChangesAsync(cancellationToken);
var nextRows = ToWeeklyEntities(parsedStoreId, parsedStaffId, shifts, template).ToList();
await ReplaceWeeklySchedulesAsync(existingRows, nextRows, cancellationToken);
return ApiResponse<StaffScheduleDto>.Ok(new StaffScheduleDto
{
@@ -404,12 +400,10 @@ public sealed class StoreStaffController(
});
}
dbContext.StoreStaffWeeklySchedules.RemoveRange(existingRows);
var entities = finalSchedules
.SelectMany(x => ToWeeklyEntities(parsedStoreId, long.Parse(x.StaffId), x.Shifts, template))
.ToList();
await dbContext.StoreStaffWeeklySchedules.AddRangeAsync(entities, cancellationToken);
await dbContext.SaveChangesAsync(cancellationToken);
await ReplaceWeeklySchedulesAsync(existingRows, entities, cancellationToken);
return ApiResponse<StoreStaffScheduleDto>.Ok(new StoreStaffScheduleDto
{
@@ -513,11 +507,11 @@ public sealed class StoreStaffController(
targetTemplateEntity.FullStartTime = StoreApiHelpers.ParseRequiredTime(sourceTemplate.Full.StartTime, "templates.full.startTime");
targetTemplateEntity.FullEndTime = StoreApiHelpers.ParseRequiredTime(sourceTemplate.Full.EndTime, "templates.full.endTime");
}
await dbContext.SaveChangesAsync(cancellationToken);
var targetRows = await dbContext.StoreStaffWeeklySchedules
.Where(x => x.TenantId == tenantId && accessibleTargetIds.Contains(x.StoreId))
.ToListAsync(cancellationToken);
dbContext.StoreStaffWeeklySchedules.RemoveRange(targetRows);
var targetStaffs = await dbContext.MerchantStaff
.AsNoTracking()
@@ -547,12 +541,7 @@ public sealed class StoreStaffController(
}
}
if (entities.Count > 0)
{
await dbContext.StoreStaffWeeklySchedules.AddRangeAsync(entities, cancellationToken);
}
await dbContext.SaveChangesAsync(cancellationToken);
await ReplaceWeeklySchedulesAsync(targetRows, entities, cancellationToken);
return ApiResponse<CopyStoreStaffScheduleResult>.Ok(new CopyStoreStaffScheduleResult
{
@@ -774,7 +763,11 @@ public sealed class StoreStaffController(
IEnumerable<StaffDayShiftDto> shifts,
StoreShiftTemplatesDto template)
{
foreach (var shift in shifts)
foreach (var shift in shifts
.Where(x => x.DayOfWeek is >= 0 and <= 6)
.GroupBy(x => x.DayOfWeek)
.Select(x => x.Last())
.OrderBy(x => x.DayOfWeek))
{
var shiftType = StoreApiHelpers.ToShiftType(shift.ShiftType);
var (defaultStart, defaultEnd) = ResolveShiftTimeRange(shiftType, template);
@@ -801,6 +794,26 @@ public sealed class StoreStaffController(
}
}
private async Task ReplaceWeeklySchedulesAsync(
IReadOnlyCollection<StoreStaffWeeklySchedule> existingRows,
IReadOnlyCollection<StoreStaffWeeklySchedule> nextRows,
CancellationToken cancellationToken)
{
if (existingRows.Count > 0)
{
dbContext.StoreStaffWeeklySchedules.RemoveRange(existingRows);
await dbContext.SaveChangesAsync(cancellationToken);
}
if (nextRows.Count == 0)
{
return;
}
await dbContext.StoreStaffWeeklySchedules.AddRangeAsync(nextRows, cancellationToken);
await dbContext.SaveChangesAsync(cancellationToken);
}
private static List<StaffDayShiftDto> CreateOffWeekShifts()
{
return Enumerable.Range(0, 7).Select(day => new StaffDayShiftDto