refactor: 排班替换改为事务内先删后增
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:
@@ -305,6 +305,7 @@ public sealed class StoreStaffController(
|
||||
throw new BusinessException(ErrorCodes.BadRequest, "请先配置班次模板");
|
||||
}
|
||||
var existingRows = await dbContext.StoreStaffWeeklySchedules
|
||||
.AsNoTracking()
|
||||
.Where(x => x.TenantId == tenantId && x.StoreId == parsedStoreId && x.StaffId == parsedStaffId)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
@@ -314,7 +315,14 @@ public sealed class StoreStaffController(
|
||||
: NormalizeShifts(request.Shifts, fallback, template);
|
||||
|
||||
var nextRows = ToWeeklyEntities(parsedStoreId, parsedStaffId, shifts, template).ToList();
|
||||
await ReplaceWeeklySchedulesAsync(existingRows, nextRows, cancellationToken);
|
||||
await ReplaceWeeklySchedulesAsync(
|
||||
dbContext.StoreStaffWeeklySchedules
|
||||
.Where(x =>
|
||||
x.TenantId == tenantId &&
|
||||
x.StoreId == parsedStoreId &&
|
||||
x.StaffId == parsedStaffId),
|
||||
nextRows,
|
||||
cancellationToken);
|
||||
|
||||
return ApiResponse<StaffScheduleDto>.Ok(new StaffScheduleDto
|
||||
{
|
||||
@@ -350,6 +358,7 @@ public sealed class StoreStaffController(
|
||||
throw new BusinessException(ErrorCodes.BadRequest, "请先配置班次模板");
|
||||
}
|
||||
var existingRows = await dbContext.StoreStaffWeeklySchedules
|
||||
.AsNoTracking()
|
||||
.Where(x => x.TenantId == tenantId && x.StoreId == parsedStoreId)
|
||||
.ToListAsync(cancellationToken);
|
||||
var existingMap = existingRows
|
||||
@@ -403,7 +412,13 @@ public sealed class StoreStaffController(
|
||||
var entities = finalSchedules
|
||||
.SelectMany(x => ToWeeklyEntities(parsedStoreId, long.Parse(x.StaffId), x.Shifts, template))
|
||||
.ToList();
|
||||
await ReplaceWeeklySchedulesAsync(existingRows, entities, cancellationToken);
|
||||
await ReplaceWeeklySchedulesAsync(
|
||||
dbContext.StoreStaffWeeklySchedules
|
||||
.Where(x =>
|
||||
x.TenantId == tenantId &&
|
||||
x.StoreId == parsedStoreId),
|
||||
entities,
|
||||
cancellationToken);
|
||||
|
||||
return ApiResponse<StoreStaffScheduleDto>.Ok(new StoreStaffScheduleDto
|
||||
{
|
||||
@@ -509,10 +524,6 @@ public sealed class StoreStaffController(
|
||||
}
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
|
||||
var targetRows = await dbContext.StoreStaffWeeklySchedules
|
||||
.Where(x => x.TenantId == tenantId && accessibleTargetIds.Contains(x.StoreId))
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
var targetStaffs = await dbContext.MerchantStaff
|
||||
.AsNoTracking()
|
||||
.Where(x => x.TenantId == tenantId && x.StoreId.HasValue && accessibleTargetIds.Contains(x.StoreId.Value))
|
||||
@@ -541,7 +552,13 @@ public sealed class StoreStaffController(
|
||||
}
|
||||
}
|
||||
|
||||
await ReplaceWeeklySchedulesAsync(targetRows, entities, cancellationToken);
|
||||
await ReplaceWeeklySchedulesAsync(
|
||||
dbContext.StoreStaffWeeklySchedules
|
||||
.Where(x =>
|
||||
x.TenantId == tenantId &&
|
||||
accessibleTargetIds.Contains(x.StoreId)),
|
||||
entities,
|
||||
cancellationToken);
|
||||
|
||||
return ApiResponse<CopyStoreStaffScheduleResult>.Ok(new CopyStoreStaffScheduleResult
|
||||
{
|
||||
@@ -795,23 +812,20 @@ public sealed class StoreStaffController(
|
||||
}
|
||||
|
||||
private async Task ReplaceWeeklySchedulesAsync(
|
||||
IReadOnlyCollection<StoreStaffWeeklySchedule> existingRows,
|
||||
IQueryable<StoreStaffWeeklySchedule> deleteQuery,
|
||||
IReadOnlyCollection<StoreStaffWeeklySchedule> nextRows,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
if (existingRows.Count > 0)
|
||||
await using var transaction = await dbContext.Database.BeginTransactionAsync(cancellationToken);
|
||||
await deleteQuery.ExecuteDeleteAsync(cancellationToken);
|
||||
|
||||
if (nextRows.Count > 0)
|
||||
{
|
||||
dbContext.StoreStaffWeeklySchedules.RemoveRange(existingRows);
|
||||
await dbContext.StoreStaffWeeklySchedules.AddRangeAsync(nextRows, cancellationToken);
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
|
||||
if (nextRows.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await dbContext.StoreStaffWeeklySchedules.AddRangeAsync(nextRows, cancellationToken);
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
await transaction.CommitAsync(cancellationToken);
|
||||
}
|
||||
|
||||
private static List<StaffDayShiftDto> CreateOffWeekShifts()
|
||||
|
||||
Reference in New Issue
Block a user