fix: 修复员工排班保存唯一键冲突
All checks were successful
Build and Deploy TenantApi / build-and-deploy (push) Successful in 43s
All checks were successful
Build and Deploy TenantApi / build-and-deploy (push) Successful in 43s
This commit is contained in:
@@ -313,12 +313,8 @@ public sealed class StoreStaffController(
|
|||||||
? CreateOffWeekShifts()
|
? CreateOffWeekShifts()
|
||||||
: NormalizeShifts(request.Shifts, fallback, template);
|
: NormalizeShifts(request.Shifts, fallback, template);
|
||||||
|
|
||||||
dbContext.StoreStaffWeeklySchedules.RemoveRange(existingRows);
|
var nextRows = ToWeeklyEntities(parsedStoreId, parsedStaffId, shifts, template).ToList();
|
||||||
await dbContext.StoreStaffWeeklySchedules.AddRangeAsync(
|
await ReplaceWeeklySchedulesAsync(existingRows, nextRows, cancellationToken);
|
||||||
ToWeeklyEntities(parsedStoreId, parsedStaffId, shifts, template),
|
|
||||||
cancellationToken);
|
|
||||||
|
|
||||||
await dbContext.SaveChangesAsync(cancellationToken);
|
|
||||||
|
|
||||||
return ApiResponse<StaffScheduleDto>.Ok(new StaffScheduleDto
|
return ApiResponse<StaffScheduleDto>.Ok(new StaffScheduleDto
|
||||||
{
|
{
|
||||||
@@ -404,12 +400,10 @@ public sealed class StoreStaffController(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
dbContext.StoreStaffWeeklySchedules.RemoveRange(existingRows);
|
|
||||||
var entities = finalSchedules
|
var entities = finalSchedules
|
||||||
.SelectMany(x => ToWeeklyEntities(parsedStoreId, long.Parse(x.StaffId), x.Shifts, template))
|
.SelectMany(x => ToWeeklyEntities(parsedStoreId, long.Parse(x.StaffId), x.Shifts, template))
|
||||||
.ToList();
|
.ToList();
|
||||||
await dbContext.StoreStaffWeeklySchedules.AddRangeAsync(entities, cancellationToken);
|
await ReplaceWeeklySchedulesAsync(existingRows, entities, cancellationToken);
|
||||||
await dbContext.SaveChangesAsync(cancellationToken);
|
|
||||||
|
|
||||||
return ApiResponse<StoreStaffScheduleDto>.Ok(new StoreStaffScheduleDto
|
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.FullStartTime = StoreApiHelpers.ParseRequiredTime(sourceTemplate.Full.StartTime, "templates.full.startTime");
|
||||||
targetTemplateEntity.FullEndTime = StoreApiHelpers.ParseRequiredTime(sourceTemplate.Full.EndTime, "templates.full.endTime");
|
targetTemplateEntity.FullEndTime = StoreApiHelpers.ParseRequiredTime(sourceTemplate.Full.EndTime, "templates.full.endTime");
|
||||||
}
|
}
|
||||||
|
await dbContext.SaveChangesAsync(cancellationToken);
|
||||||
|
|
||||||
var targetRows = await dbContext.StoreStaffWeeklySchedules
|
var targetRows = await dbContext.StoreStaffWeeklySchedules
|
||||||
.Where(x => x.TenantId == tenantId && accessibleTargetIds.Contains(x.StoreId))
|
.Where(x => x.TenantId == tenantId && accessibleTargetIds.Contains(x.StoreId))
|
||||||
.ToListAsync(cancellationToken);
|
.ToListAsync(cancellationToken);
|
||||||
dbContext.StoreStaffWeeklySchedules.RemoveRange(targetRows);
|
|
||||||
|
|
||||||
var targetStaffs = await dbContext.MerchantStaff
|
var targetStaffs = await dbContext.MerchantStaff
|
||||||
.AsNoTracking()
|
.AsNoTracking()
|
||||||
@@ -547,12 +541,7 @@ public sealed class StoreStaffController(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (entities.Count > 0)
|
await ReplaceWeeklySchedulesAsync(targetRows, entities, cancellationToken);
|
||||||
{
|
|
||||||
await dbContext.StoreStaffWeeklySchedules.AddRangeAsync(entities, cancellationToken);
|
|
||||||
}
|
|
||||||
|
|
||||||
await dbContext.SaveChangesAsync(cancellationToken);
|
|
||||||
|
|
||||||
return ApiResponse<CopyStoreStaffScheduleResult>.Ok(new CopyStoreStaffScheduleResult
|
return ApiResponse<CopyStoreStaffScheduleResult>.Ok(new CopyStoreStaffScheduleResult
|
||||||
{
|
{
|
||||||
@@ -774,7 +763,11 @@ public sealed class StoreStaffController(
|
|||||||
IEnumerable<StaffDayShiftDto> shifts,
|
IEnumerable<StaffDayShiftDto> shifts,
|
||||||
StoreShiftTemplatesDto template)
|
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 shiftType = StoreApiHelpers.ToShiftType(shift.ShiftType);
|
||||||
var (defaultStart, defaultEnd) = ResolveShiftTimeRange(shiftType, template);
|
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()
|
private static List<StaffDayShiftDto> CreateOffWeekShifts()
|
||||||
{
|
{
|
||||||
return Enumerable.Range(0, 7).Select(day => new StaffDayShiftDto
|
return Enumerable.Range(0, 7).Select(day => new StaffDayShiftDto
|
||||||
|
|||||||
Reference in New Issue
Block a user