fix 优化权限分析

This commit is contained in:
2024-11-14 15:40:47 +08:00
parent ee8784195f
commit 8cb42d81d6

View File

@@ -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<DevicePermissionAssignResponse> 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<IDbContext>();
await currentDb.IgnoreAsync<ITenantId>(async () =>
{
var dgs = await currentDb.Queryable<DeviceGroupEntity>().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<DeviceEntity>().Where(x => x.Enable).ToListAsync();
var waitAddTasks = new List<DeviceTaskEntity>();
var waitDeleteTasks = new List<DeviceTaskEntity>();
var waitAddTasks = new ConcurrentBag<DeviceTaskEntity>();
var waitDeleteTasks = new ConcurrentBag<DeviceTaskEntity>();
foreach (var userNo in userNos)
Parallel.ForEach(userNos, new ParallelOptions() { MaxDegreeOfParallelism = 100 }, userNo =>
{
before.TryGetValue(userNo, out var beforeTenantDeviceCodes);
beforeTenantDeviceCodes ??= new List<UserTenantDeviceCodeDto>();
@@ -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<Guid>();
var beforeCodes = beforeTenantDeviceCodes.FirstOrDefault(x => x.Tenant == tenant)?.Codes ?? new List<Guid>();
//被删除
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<T>(List<T> batch) where T : class, new()
{
foreach (var tasks in batch.Chunk(500))
{
await _dbContext.InsertAsync(tasks.ToList());
}
}
}
public async Task<PermissionUsersInfoDto> GetPermissionUsersInfoAsync(List<string> userNos)
@@ -424,6 +448,8 @@ public class DevicePermissionService : IDevicePermissionService
}
}
ids = ids.Distinct().ToList();
var page = await _dbContext.Queryable<GainfoEntity>()
.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<DeviceGroupRelationshipEntity>().ToListAsync();
var departments = await _dbContext.Queryable<DepartmentEntity>().ToListAsync();
var result = new Dictionary<string, List<UserTenantDeviceCodeDto>>(gainfos.Count, StringComparer.OrdinalIgnoreCase);
foreach (var gainfo in gainfos)
var result = new ConcurrentDictionary<string, List<UserTenantDeviceCodeDto>>();
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<UserTenantDeviceCodeDto> GetAssignDeviceCodesInternal(