diff --git a/src/Api/TakeoutSaaS.TenantApi/Controllers/StoreStaffController.cs b/src/Api/TakeoutSaaS.TenantApi/Controllers/StoreStaffController.cs index 28a8d2f..e49312c 100644 --- a/src/Api/TakeoutSaaS.TenantApi/Controllers/StoreStaffController.cs +++ b/src/Api/TakeoutSaaS.TenantApi/Controllers/StoreStaffController.cs @@ -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.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.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.Ok(new CopyStoreStaffScheduleResult { @@ -795,23 +812,20 @@ public sealed class StoreStaffController( } private async Task ReplaceWeeklySchedulesAsync( - IReadOnlyCollection existingRows, + IQueryable deleteQuery, IReadOnlyCollection 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 CreateOffWeekShifts()