From 8cb42d81d6a83fdb2230552544778eacb0029a90 Mon Sep 17 00:00:00 2001 From: liguoyang Date: Thu, 14 Nov 2024 15:40:47 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E4=BC=98=E5=8C=96=E6=9D=83=E9=99=90?= =?UTF-8?q?=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/DevicePermissionService.cs | 66 ++++++++++++++----- 1 file changed, 48 insertions(+), 18 deletions(-) diff --git a/Src/Dlhis.Basic/Device/Services/Implements/DevicePermissionService.cs b/Src/Dlhis.Basic/Device/Services/Implements/DevicePermissionService.cs index 3e7f2ce2..333e1507 100644 --- a/Src/Dlhis.Basic/Device/Services/Implements/DevicePermissionService.cs +++ b/Src/Dlhis.Basic/Device/Services/Implements/DevicePermissionService.cs @@ -1,4 +1,5 @@ -using Dlhis.Basic.DataPermissions; +using System.Collections.Concurrent; +using Dlhis.Basic.DataPermissions; using Dlhis.Basic.Device.Dtos; using Dlhis.Basic.Device.Request; using Dlhis.Basic.Device.Response; @@ -16,6 +17,7 @@ using Kurisu.AspNetCore.DataAccess.Dto; using Kurisu.AspNetCore.DataAccess.Entity; using Kurisu.AspNetCore.DataAccess.SqlSugar.Extensions; using Kurisu.AspNetCore.DataAccess.SqlSugar.Services; +using Kurisu.AspNetCore.Scope; using Kurisu.AspNetCore.Utils.Extensions; using SqlSugar; @@ -39,6 +41,8 @@ public class DevicePermissionService : IDevicePermissionService public async Task AssignAsync(DevicePermissionSaveRequest request) { + await CheckAssignTopDoorDeviceGroupAsync(request); + return await GenerateDeviceTasksAsync(async () => { request.AssignValue = request.AssignType == AssignTypeEnumType.Department ? request.TenantId + ":" + request.AssignValue : request.AssignValue; @@ -80,6 +84,22 @@ public class DevicePermissionService : IDevicePermissionService }, null); } + private async Task CheckAssignTopDoorDeviceGroupAsync(DevicePermissionSaveRequest request) + { + if (request.UniqueType == DeviceAssignmentTypeEnumType.Group) + { + await Scoped.Temp.Value.CreateAsync(async sp => + { + var currentDb = sp.GetService(); + await currentDb.IgnoreAsync(async () => + { + var dgs = await currentDb.Queryable().Where(x => request.Codes.Contains(x.Code)).ToListAsync(); + dgs.Any(x => x.TenantId.IsEmpty()).ThrowIfTrue("不可分配顶级分组节点"); + }); + }); + } + } + public async Task AssignToUniqueAsync(DevicePermissionsAssignToDeviceRequest request) { await GenerateDeviceTasksAsync(async () => @@ -196,10 +216,10 @@ public class DevicePermissionService : IDevicePermissionService var current = await GetAssignDeviceCodesInternalAsync(userNos); var devices = await _dbContext.Queryable().Where(x => x.Enable).ToListAsync(); - var waitAddTasks = new List(); - var waitDeleteTasks = new List(); + var waitAddTasks = new ConcurrentBag(); + var waitDeleteTasks = new ConcurrentBag(); - foreach (var userNo in userNos) + Parallel.ForEach(userNos, new ParallelOptions() { MaxDegreeOfParallelism = 100 }, userNo => { before.TryGetValue(userNo, out var beforeTenantDeviceCodes); beforeTenantDeviceCodes ??= new List(); @@ -214,8 +234,8 @@ public class DevicePermissionService : IDevicePermissionService foreach (var tenant in tenants) { - var currentCodes = currentTenantDeviceCodes.FirstOrDefault(x => x.Tenant == tenant)?.Codes ?? new(); - var beforeCodes = beforeTenantDeviceCodes.FirstOrDefault(x => x.Tenant == tenant)?.Codes ?? new(); + var currentCodes = currentTenantDeviceCodes.FirstOrDefault(x => x.Tenant == tenant)?.Codes ?? new List(); + var beforeCodes = beforeTenantDeviceCodes.FirstOrDefault(x => x.Tenant == tenant)?.Codes ?? new List(); //被删除 var deletedItems = beforeCodes.Where(x => !currentCodes.Contains(x)).ToList(); @@ -257,23 +277,27 @@ public class DevicePermissionService : IDevicePermissionService Source = DeviceTaskSourceEnumType.Manually })); } - } + }); if (waitAddTasks.Any()) - { - await _dbContext.InsertAsync(waitAddTasks); - } + await ChunkInsertAsync(waitAddTasks.ToList()); if (waitDeleteTasks.Any()) - { - await _dbContext.InsertAsync(waitDeleteTasks); - } + await ChunkInsertAsync(waitDeleteTasks.ToList()); return new DevicePermissionAssignResponse { AddCount = waitAddTasks.Count, DeleteCount = waitDeleteTasks.Count }; + + async Task ChunkInsertAsync(List batch) where T : class, new() + { + foreach (var tasks in batch.Chunk(500)) + { + await _dbContext.InsertAsync(tasks.ToList()); + } + } } public async Task GetPermissionUsersInfoAsync(List userNos) @@ -424,6 +448,8 @@ public class DevicePermissionService : IDevicePermissionService } } + ids = ids.Distinct().ToList(); + var page = await _dbContext.Queryable() .Where(x => ids.Contains(x.Id)) .WhereIF(request.Search.IsPresent(), x => x.Code == request.Search || x.Name.Contains(request.Search)) @@ -720,8 +746,12 @@ public class DevicePermissionService : IDevicePermissionService var deviceRelationships = await _dbContext.Queryable().ToListAsync(); var departments = await _dbContext.Queryable().ToListAsync(); - var result = new Dictionary>(gainfos.Count, StringComparer.OrdinalIgnoreCase); - foreach (var gainfo in gainfos) + var result = new ConcurrentDictionary>(); + var parallelOptions = new ParallelOptions + { + MaxDegreeOfParallelism = 100, + }; + Parallel.ForEach(gainfos, parallelOptions, gainfo => { var gainfoDto = new GainfoDto { @@ -737,10 +767,10 @@ public class DevicePermissionService : IDevicePermissionService }; var codes = GetAssignDeviceCodesInternal(gainfoDto, gainfoGroupRelationships, devicePermissions, deviceGroups, deviceRelationships, registries, devices, departments); - result.Add(gainfo.No, codes); - } + result.TryAdd(gainfo.No, codes); + }); - return result; + return result.ToDictionary(x => x.Key, x => x.Value); } private static List GetAssignDeviceCodesInternal(