feat 优化权限完成
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
using Dlhis.Basic.Device.Dtos;
|
using Dlhis.Basic.Device.Filters;
|
||||||
using Dlhis.Basic.Device.Filters;
|
|
||||||
using Dlhis.Basic.Device.Request;
|
using Dlhis.Basic.Device.Request;
|
||||||
using Dlhis.Basic.Device.Response;
|
using Dlhis.Basic.Device.Response;
|
||||||
using Dlhis.Basic.Device.Services;
|
using Dlhis.Basic.Device.Services;
|
||||||
@@ -108,12 +107,4 @@ public class DevicePermissionsController : ControllerBase
|
|||||||
{
|
{
|
||||||
return await _devicePermissionService.GetAssignDeviceCodesAsync(userCode);
|
return await _devicePermissionService.GetAssignDeviceCodesAsync(userCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
[AllowAnonymous]
|
|
||||||
[HttpGet("test")]
|
|
||||||
[IgnoreTenant]
|
|
||||||
public async Task<Dictionary<string, List<UserTenantDeviceCodeDto>>> GetUserDevicePermissionsAsync(string code)
|
|
||||||
{
|
|
||||||
return await _devicePermissionService.GetUserDevicePermissionsAsync(code);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
namespace Dlhis.Basic.Device.Dtos
|
|
||||||
{
|
|
||||||
public class PermissionUsersInfoDto
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 拥有权限的用户
|
|
||||||
/// </summary>
|
|
||||||
public List<string> UserNos { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 用户对应的 [ 厂区--设备Code ]
|
|
||||||
/// </summary>
|
|
||||||
public Dictionary<string, List<UserTenantDeviceCodeDto>> PermissionInfos { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -2,6 +2,35 @@
|
|||||||
|
|
||||||
namespace Dlhis.Basic.Device.Dtos;
|
namespace Dlhis.Basic.Device.Dtos;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 厂区对应分配类型的设备权限
|
||||||
|
/// </summary>
|
||||||
|
public class TenantAssignTypeDeviceCode
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 租户
|
||||||
|
/// </summary>
|
||||||
|
public string Tenant { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 设备权限
|
||||||
|
/// </summary>
|
||||||
|
public IEnumerable<AssignTypeDeviceCode> AssignTypes { get; init; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class AssignTypeDeviceCode
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 分配类型
|
||||||
|
/// </summary>
|
||||||
|
public AssignTypeEnumType AssignType { get; init; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 设备权限
|
||||||
|
/// </summary>
|
||||||
|
public IEnumerable<Guid> Codes { get; init; }
|
||||||
|
}
|
||||||
|
|
||||||
public class UserTenantDeviceCodeDto
|
public class UserTenantDeviceCodeDto
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -9,32 +38,6 @@ public class UserTenantDeviceCodeDto
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public string Tenant { get; set; }
|
public string Tenant { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 设备权限
|
|
||||||
/// </summary>
|
|
||||||
public IEnumerable<Guid> Codes { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public class TenantAssignTypeDeviceCode
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 租户
|
|
||||||
/// </summary>
|
|
||||||
public string Tenant { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 设备权限
|
|
||||||
/// </summary>
|
|
||||||
public IEnumerable<AssignTypeDeviceCode> Codes { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public class AssignTypeDeviceCode
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 分配类型
|
|
||||||
/// </summary>
|
|
||||||
public AssignTypeEnumType AssignType { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 设备权限
|
/// 设备权限
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
using Kurisu.AspNetCore.Authentication.Abstractions;
|
using Kurisu.AspNetCore.Authentication.Abstractions;
|
||||||
using Kurisu.AspNetCore.Utils;
|
using Kurisu.AspNetCore.Cache;
|
||||||
using Kurisu.AspNetCore.Utils.Extensions;
|
using Kurisu.AspNetCore.Utils.Extensions;
|
||||||
using Microsoft.AspNetCore.Mvc.Filters;
|
using Microsoft.AspNetCore.Mvc.Filters;
|
||||||
|
|
||||||
@@ -11,12 +11,11 @@ public class AssignPermissionLimitAttribute : Attribute, IAsyncActionFilter
|
|||||||
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
|
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
|
||||||
{
|
{
|
||||||
var currentUser = context.HttpContext.RequestServices.GetRequiredService<ICurrentUser>();
|
var currentUser = context.HttpContext.RequestServices.GetRequiredService<ICurrentUser>();
|
||||||
//var userId = currentUser.GetIntSubjectId();
|
|
||||||
var redisCache = context.HttpContext.RequestServices.GetRequiredService<RedisCache>();
|
var redisCache = context.HttpContext.RequestServices.GetRequiredService<RedisCache>();
|
||||||
|
|
||||||
var currentTenantId = currentUser.GetStringTenantId();
|
var currentTenantId = currentUser.GetStringTenantId();
|
||||||
|
|
||||||
await using var handler = await redisCache.LockAsync($"permission:generate:{currentTenantId}");
|
await using var handler = await redisCache.LockAsync($"permission:generate:{currentTenantId}", expiry: TimeSpan.FromMinutes(3));
|
||||||
handler.Acquired.ThrowIfFalse("请勿频繁分配");
|
handler.Acquired.ThrowIfFalse("请勿频繁分配");
|
||||||
await next();
|
await next();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ using Dlhis.Entity.Sys.Enums;
|
|||||||
|
|
||||||
namespace Dlhis.Basic.Device;
|
namespace Dlhis.Basic.Device;
|
||||||
|
|
||||||
public class PermissionAnalyzeConstants
|
public static class PermissionAnalyzeConstants
|
||||||
{
|
{
|
||||||
private const string _verb = "PermissionAnalyze";
|
private const string _verb = "PermissionAnalyze";
|
||||||
public const string User = nameof(AssignTypeEnumType.User) + _verb;
|
public const string User = nameof(AssignTypeEnumType.User) + _verb;
|
||||||
@@ -10,7 +10,6 @@ public class PermissionAnalyzeConstants
|
|||||||
public const string Department = nameof(AssignTypeEnumType.Department) + _verb;
|
public const string Department = nameof(AssignTypeEnumType.Department) + _verb;
|
||||||
public const string Tenant = nameof(AssignTypeEnumType.Tenant) + _verb;
|
public const string Tenant = nameof(AssignTypeEnumType.Tenant) + _verb;
|
||||||
|
|
||||||
|
|
||||||
public static string GetTypeName(AssignTypeEnumType assignTypeEnumType)
|
public static string GetTypeName(AssignTypeEnumType assignTypeEnumType)
|
||||||
{
|
{
|
||||||
return assignTypeEnumType + _verb;
|
return assignTypeEnumType + _verb;
|
||||||
|
|||||||
@@ -13,5 +13,5 @@ public class DeviceAssignRequest
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 设备code
|
/// 设备code
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public List<Guid> DeviceCodes { get; set; } = new List<Guid>();
|
public List<Guid> DeviceCodes { get; set; } = new();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -61,4 +61,13 @@ public class DevicePermissionSaveRequest : IValidatableObject
|
|||||||
yield return new ValidationResult("暂不支持角色分配", new[] { nameof(AssignType) });
|
yield return new ValidationResult("暂不支持角色分配", new[] { nameof(AssignType) });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取实际分配的权限值
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public string GetActualAssignValue()
|
||||||
|
{
|
||||||
|
return AssignType == AssignTypeEnumType.Department ? TenantId + ":" + AssignValue : AssignValue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -2,6 +2,12 @@ namespace Dlhis.Basic.Device.Response;
|
|||||||
|
|
||||||
public class DevicePermissionAssignResponse
|
public class DevicePermissionAssignResponse
|
||||||
{
|
{
|
||||||
|
public DevicePermissionAssignResponse(int addCount, int deleteCount)
|
||||||
|
{
|
||||||
|
AddCount = addCount;
|
||||||
|
DeleteCount = deleteCount;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 添加数量
|
/// 添加数量
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -13,14 +13,11 @@ public interface IDevicePermissionAnalyzeService : IScopeDependency
|
|||||||
{
|
{
|
||||||
Task<DevicePermissionAssignResponse> GenerateDeviceTasksAsync(Func<Task> func, List<string> userNos);
|
Task<DevicePermissionAssignResponse> GenerateDeviceTasksAsync(Func<Task> func, List<string> userNos);
|
||||||
|
|
||||||
Task<Dictionary<string, List<UserTenantDeviceCodeDto>>> GetAssignDeviceCodesInternalAsync(List<string> userNos);
|
Task<Dictionary<string, IEnumerable<TenantAssignTypeDeviceCode>>> GetUserAssignDeviceCodesWithTypeInTenantAsync(List<string> userNos, params AssignTypeEnumType[] ignoreAssignTypes);
|
||||||
|
|
||||||
Task<Dictionary<string, List<TenantAssignTypeDeviceCode>>> GetAssignDeviceCodesInternalAsync(List<string> userNos, params AssignTypeEnumType[] ignoreAssignTypes);
|
|
||||||
|
|
||||||
Task<List<Guid>> GetAssignmentsAsync(DeviceGetAssignmentsRequest request);
|
Task<List<Guid>> GetAssignmentsAsync(DeviceGetAssignmentsRequest request);
|
||||||
|
|
||||||
Task<List<GainfoDto>> GetAssignmentUniqueAsync(DeviceGetAssignmentUniquesRequest request);
|
Task<List<GainfoDto>> GetAssignmentUniqueAsync(DeviceGetAssignmentUniquesRequest request);
|
||||||
|
|
||||||
|
|
||||||
Task<List<Guid>> GetAssignDeviceCodesAsync(string userCode);
|
Task<List<Guid>> GetAssignDeviceCodesAsync(string userCode);
|
||||||
}
|
}
|
||||||
@@ -27,7 +27,7 @@ public interface IDevicePermissionService : IScopeDependency
|
|||||||
Task AssignToUniqueImportAsync(IFormFile file);
|
Task AssignToUniqueImportAsync(IFormFile file);
|
||||||
|
|
||||||
|
|
||||||
Task<Dictionary<string, List<UserTenantDeviceCodeDto>>> GetUserDevicePermissionsAsync(string code);
|
Task<Dictionary<string, IEnumerable<UserTenantDeviceCodeDto>>> GetUserDevicePermissionsAsync(string code);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 生成下发任务(主动生成)
|
/// 生成下发任务(主动生成)
|
||||||
|
|||||||
@@ -1,6 +0,0 @@
|
|||||||
namespace Dlhis.Basic.Device.Services;
|
|
||||||
|
|
||||||
public interface IParallelService : IScopeDependency
|
|
||||||
{
|
|
||||||
public bool Enable { get; set; }
|
|
||||||
}
|
|
||||||
@@ -1,8 +1,17 @@
|
|||||||
using Dlhis.Basic.Device.Request;
|
using Dlhis.Basic.Device.Request;
|
||||||
|
using Dlhis.Basic.Device.Response;
|
||||||
|
|
||||||
namespace Dlhis.Basic.Device.Services;
|
namespace Dlhis.Basic.Device.Services;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 权限分析
|
||||||
|
/// </summary>
|
||||||
public interface IPermissionAnalyzeService
|
public interface IPermissionAnalyzeService
|
||||||
{
|
{
|
||||||
Task AnalyzeAsync(DevicePermissionSaveRequest request);
|
/// <summary>
|
||||||
|
/// 分析操作
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="request"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<DevicePermissionAssignResponse> AnalyzeAsync(DevicePermissionSaveRequest request);
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
using Dlhis.Basic.DataPermissions;
|
using Dlhis.Basic.DataPermissions;
|
||||||
using Dlhis.Basic.Device.Dtos;
|
using Dlhis.Basic.Device.Dtos;
|
||||||
using Dlhis.Basic.Device.Request;
|
using Dlhis.Basic.Device.Request;
|
||||||
|
using Dlhis.Basic.Device.Response;
|
||||||
using Dlhis.Entity.Basic.Entity;
|
using Dlhis.Entity.Basic.Entity;
|
||||||
using Dlhis.Entity.Device.Entities;
|
using Dlhis.Entity.Device.Entities;
|
||||||
using Dlhis.Entity.Device.Enums;
|
using Dlhis.Entity.Device.Enums;
|
||||||
@@ -15,50 +16,52 @@ using SqlSugar;
|
|||||||
|
|
||||||
namespace Dlhis.Basic.Device.Services.Implements;
|
namespace Dlhis.Basic.Device.Services.Implements;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 权限分析基类
|
||||||
|
/// </summary>
|
||||||
public abstract class BasePermissionAnalyzeService : IPermissionAnalyzeService
|
public abstract class BasePermissionAnalyzeService : IPermissionAnalyzeService
|
||||||
{
|
{
|
||||||
private readonly ICurrentUser _currentUser;
|
private readonly ICurrentUser _currentUser;
|
||||||
private readonly IDevicePermissionAnalyzeService _devicePermissionAnalyzeService;
|
|
||||||
private readonly IMyDataPermissionService _myDataPermissionService;
|
private readonly IMyDataPermissionService _myDataPermissionService;
|
||||||
|
private readonly IDevicePermissionAnalyzeService _devicePermissionAnalyzeService;
|
||||||
|
|
||||||
protected IDbContext DbContext { get; }
|
protected IDbContext DbContext { get; }
|
||||||
|
|
||||||
protected BasePermissionAnalyzeService(IServiceProvider serviceProvider)
|
protected BasePermissionAnalyzeService(IServiceProvider serviceProvider)
|
||||||
{
|
{
|
||||||
_currentUser = serviceProvider.GetService<ICurrentUser>();
|
_currentUser = serviceProvider.GetService<ICurrentUser>();
|
||||||
_devicePermissionAnalyzeService = serviceProvider.GetService<IDevicePermissionAnalyzeService>();
|
|
||||||
_myDataPermissionService = serviceProvider.GetService<IMyDataPermissionService>();
|
_myDataPermissionService = serviceProvider.GetService<IMyDataPermissionService>();
|
||||||
DbContext = serviceProvider.GetService<IDbContext>();
|
DbContext = serviceProvider.GetService<IDbContext>();
|
||||||
CurrentTenantId = _currentUser.GetStringTenantId();
|
CurrentTenantId = _currentUser.GetStringTenantId();
|
||||||
|
_devicePermissionAnalyzeService = serviceProvider.GetService<IDevicePermissionAnalyzeService>();
|
||||||
}
|
}
|
||||||
|
|
||||||
private string CurrentTenantId { get; }
|
private string CurrentTenantId { get; }
|
||||||
|
|
||||||
public ISugarQueryable<DevicePermissionEntity> DevicePermission => DbContext.Queryable<DevicePermissionEntity>();
|
protected ISugarQueryable<DevicePermissionEntity> DevicePermission => DbContext.Queryable<DevicePermissionEntity>();
|
||||||
|
|
||||||
|
public async Task<DevicePermissionAssignResponse> AnalyzeAsync(DevicePermissionSaveRequest request)
|
||||||
public async Task AnalyzeAsync(DevicePermissionSaveRequest request)
|
|
||||||
{
|
{
|
||||||
var currentAssignValue = request.AssignType == AssignTypeEnumType.Department ? request.TenantId + ":" + request.AssignValue : request.AssignValue;
|
await CheckWhetherAssignToTopDeviceGroupAsync(request);
|
||||||
|
await CheckAssignedToTenantAsync(request);
|
||||||
await CheckAssignTopDoorDeviceGroupAsync(request);
|
|
||||||
|
|
||||||
var dps = await GetAssignedDevicePermissionsAsync(request);
|
var dps = await GetAssignedDevicePermissionsAsync(request);
|
||||||
|
|
||||||
(List<Guid> codesToAdd, List<Guid> codesToDelete) = await GetDiffAsync(request.UniqueType, dps, request.Codes);
|
(List<Guid> codesToAdd, List<Guid> codesToDelete) = await GetDiffAsync(request.UniqueType, dps, request.Codes);
|
||||||
if (codesToAdd.IsEmpty() && codesToDelete.IsEmpty())
|
if (codesToAdd.IsEmpty() && codesToDelete.IsEmpty())
|
||||||
{
|
{
|
||||||
return;
|
return new DevicePermissionAssignResponse(0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
var devicesToAdd = await DbContext.Queryable<DeviceEntity>().Where(x => codesToAdd.Contains(x.Code)).Distinct().ToListAsync();
|
var devicesToAdd = await DbContext.Queryable<DeviceEntity>().Where(x => codesToAdd.Contains(x.Code)).Distinct().ToListAsync();
|
||||||
var devicesToDelete = await DbContext.Queryable<DeviceEntity>().Where(x => codesToDelete.Contains(x.Code)).Distinct().ToListAsync();
|
var devicesToDelete = await DbContext.Queryable<DeviceEntity>().Where(x => codesToDelete.Contains(x.Code)).Distinct().ToListAsync();
|
||||||
|
|
||||||
var gainfoNos = await GetGainfoNosAsync(currentAssignValue);
|
var gainfoNos = await GetGainfoNosAsync(request.GetActualAssignValue());
|
||||||
|
|
||||||
var userTenantDeviceCodes =
|
var userTenantDeviceCodes =
|
||||||
devicesToDelete.IsPresent() && request.AssignType != AssignTypeEnumType.Tenant
|
devicesToDelete.IsPresent() && request.AssignType != AssignTypeEnumType.Tenant
|
||||||
? await _devicePermissionAnalyzeService.GetAssignDeviceCodesInternalAsync(gainfoNos, request.AssignType)
|
? await _devicePermissionAnalyzeService.GetUserAssignDeviceCodesWithTypeInTenantAsync(gainfoNos, request.AssignType)
|
||||||
: new Dictionary<string, List<TenantAssignTypeDeviceCode>>();
|
: new Dictionary<string, IEnumerable<TenantAssignTypeDeviceCode>>();
|
||||||
|
|
||||||
var tasks = new List<DeviceTaskEntity>();
|
var tasks = new List<DeviceTaskEntity>();
|
||||||
foreach (var gainfoNo in gainfoNos)
|
foreach (var gainfoNo in gainfoNos)
|
||||||
@@ -81,7 +84,7 @@ public abstract class BasePermissionAnalyzeService : IPermissionAnalyzeService
|
|||||||
|
|
||||||
if (userTenantDeviceCodes.Any())
|
if (userTenantDeviceCodes.Any())
|
||||||
{
|
{
|
||||||
var myDeviceCodes = userTenantDeviceCodes[gainfoNo].SelectMany(x => x.Codes).SelectMany(x => x.Codes);
|
var myDeviceCodes = userTenantDeviceCodes[gainfoNo].SelectMany(x => x.AssignTypes).SelectMany(x => x.Codes);
|
||||||
devicesToDelete = devicesToDelete.Where(p => devicesToDelete.Select(x => x.Code).Except(myDeviceCodes).Contains(p.Code)).ToList();
|
devicesToDelete = devicesToDelete.Where(p => devicesToDelete.Select(x => x.Code).Except(myDeviceCodes).Contains(p.Code)).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,13 +107,58 @@ public abstract class BasePermissionAnalyzeService : IPermissionAnalyzeService
|
|||||||
|
|
||||||
await ChunkInsertAsync(tasks);
|
await ChunkInsertAsync(tasks);
|
||||||
await PersistDataAsync(dps, request);
|
await PersistDataAsync(dps, request);
|
||||||
|
|
||||||
|
return new DevicePermissionAssignResponse(devicesToAdd.Count, devicesToDelete.Count);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task ChunkInsertAsync<T>(List<T> batch) where T : class, new()
|
|
||||||
|
public async Task CheckAssignedToTenantAsync(DevicePermissionSaveRequest request)
|
||||||
{
|
{
|
||||||
foreach (var tasks in batch.Chunk(500))
|
if (request.AssignType != AssignTypeEnumType.Tenant)
|
||||||
{
|
{
|
||||||
await DbContext.InsertAsync(tasks);
|
(request.Type == DeviceTypeEnumType.Meal).ThrowIfTrue("餐饮设备需分配到厂区级别");
|
||||||
|
|
||||||
|
if (request.UniqueType == DeviceAssignmentTypeEnumType.Device)
|
||||||
|
{
|
||||||
|
if (await DbContext.Queryable<DevicePermissionEntity>()
|
||||||
|
.Where(x => x.UniqueType == DeviceAssignmentTypeEnumType.Device)
|
||||||
|
.Where(x => x.AssignType == AssignTypeEnumType.Tenant)
|
||||||
|
.Where(x => request.Codes.Contains(x.UniqueValue))
|
||||||
|
.AnyAsync())
|
||||||
|
{
|
||||||
|
true.ThrowIfTrue("存在设备分配到厂区中");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.UniqueType == DeviceAssignmentTypeEnumType.Group)
|
||||||
|
{
|
||||||
|
var result = new List<Guid>();
|
||||||
|
var deviceGroups = await DbContext.Queryable<DeviceGroupEntity>().ToListAsync();
|
||||||
|
request.Codes.ForEach(groupCode => result.AddRange(RecursionHelper.HandlerDeviceGroupNextCodes(groupCode, deviceGroups)));
|
||||||
|
|
||||||
|
if (await DbContext.Queryable<DevicePermissionEntity>()
|
||||||
|
.Where(x => x.UniqueType == DeviceAssignmentTypeEnumType.Group)
|
||||||
|
.Where(x => x.AssignType == AssignTypeEnumType.Tenant)
|
||||||
|
.Where(x => result.Contains(x.UniqueValue))
|
||||||
|
.AnyAsync())
|
||||||
|
{
|
||||||
|
true.ThrowIfTrue("存在设备组分配到厂区中");
|
||||||
|
}
|
||||||
|
|
||||||
|
var deviceCodes = await DbContext.Queryable<DeviceGroupRelationshipEntity>()
|
||||||
|
.Where(x => result.Contains(x.GroupCode))
|
||||||
|
.Select(x => x.DeviceCode).Distinct()
|
||||||
|
.ToListAsync();
|
||||||
|
|
||||||
|
if (await DbContext.Queryable<DevicePermissionEntity>()
|
||||||
|
.Where(x => x.UniqueType == DeviceAssignmentTypeEnumType.Device)
|
||||||
|
.Where(x => x.AssignType == AssignTypeEnumType.Tenant)
|
||||||
|
.Where(x => deviceCodes.Contains(x.UniqueValue))
|
||||||
|
.AnyAsync())
|
||||||
|
{
|
||||||
|
true.ThrowIfTrue("设备组存在关联设备已分配到厂区中");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -124,7 +172,19 @@ public abstract class BasePermissionAnalyzeService : IPermissionAnalyzeService
|
|||||||
|
|
||||||
// --------------------------------------------------------------------------------------------------------------------- //
|
// --------------------------------------------------------------------------------------------------------------------- //
|
||||||
|
|
||||||
private async Task CheckAssignTopDoorDeviceGroupAsync(DevicePermissionSaveRequest request)
|
private async Task ChunkInsertAsync<T>(List<T> batch) where T : class, new()
|
||||||
|
{
|
||||||
|
foreach (var tasks in batch.Chunk(500))
|
||||||
|
{
|
||||||
|
await DbContext.InsertAsync(tasks);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 检查是否分配顶级的设备分组
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="request"></param>
|
||||||
|
private static async Task CheckWhetherAssignToTopDeviceGroupAsync(DevicePermissionSaveRequest request)
|
||||||
{
|
{
|
||||||
if (request.UniqueType == DeviceAssignmentTypeEnumType.Group)
|
if (request.UniqueType == DeviceAssignmentTypeEnumType.Group)
|
||||||
{
|
{
|
||||||
@@ -140,18 +200,21 @@ public abstract class BasePermissionAnalyzeService : IPermissionAnalyzeService
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 持久化数据
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="dps"></param>
|
||||||
|
/// <param name="request"></param>
|
||||||
private async Task PersistDataAsync(List<DevicePermissionEntity> dps, DevicePermissionSaveRequest request)
|
private async Task PersistDataAsync(List<DevicePermissionEntity> dps, DevicePermissionSaveRequest request)
|
||||||
{
|
{
|
||||||
await DbContext.DeleteAsync(dps);
|
await DbContext.DeleteAsync(dps);
|
||||||
//保存配置的权限
|
//保存配置的权限
|
||||||
if (request.Codes.IsPresent())
|
if (request.Codes.IsPresent())
|
||||||
{
|
{
|
||||||
var currentAssignValue = request.AssignType == AssignTypeEnumType.Department ? request.TenantId + ":" + request.AssignValue : request.AssignValue;
|
|
||||||
var devicePermissions = request.Codes.Select(code => new DevicePermissionEntity
|
var devicePermissions = request.Codes.Select(code => new DevicePermissionEntity
|
||||||
{
|
{
|
||||||
AssignType = request.AssignType,
|
AssignType = request.AssignType,
|
||||||
AssignValue = currentAssignValue,
|
AssignValue = request.GetActualAssignValue(),
|
||||||
UniqueType = request.UniqueType,
|
UniqueType = request.UniqueType,
|
||||||
UniqueValue = code,
|
UniqueValue = code,
|
||||||
Type = request.Type,
|
Type = request.Type,
|
||||||
@@ -165,7 +228,6 @@ public abstract class BasePermissionAnalyzeService : IPermissionAnalyzeService
|
|||||||
private List<Guid> GetMyUniqueValues(DeviceAssignmentTypeEnumType uniqueType)
|
private List<Guid> GetMyUniqueValues(DeviceAssignmentTypeEnumType uniqueType)
|
||||||
{
|
{
|
||||||
var searchAll = _currentUser.GetRole() is "admin" or "tenant-admin";
|
var searchAll = _currentUser.GetRole() is "admin" or "tenant-admin";
|
||||||
//_myDataPermissionService
|
|
||||||
if (!searchAll && uniqueType == DeviceAssignmentTypeEnumType.Device)
|
if (!searchAll && uniqueType == DeviceAssignmentTypeEnumType.Device)
|
||||||
{
|
{
|
||||||
return _myDataPermissionService.GetMyDeviceCodes();
|
return _myDataPermissionService.GetMyDeviceCodes();
|
||||||
@@ -174,13 +236,17 @@ public abstract class BasePermissionAnalyzeService : IPermissionAnalyzeService
|
|||||||
return new List<Guid>();
|
return new List<Guid>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取当前分配对应的现有权限
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="request"></param>
|
||||||
|
/// <returns></returns>
|
||||||
private async Task<List<DevicePermissionEntity>> GetAssignedDevicePermissionsAsync(DevicePermissionSaveRequest request)
|
private async Task<List<DevicePermissionEntity>> GetAssignedDevicePermissionsAsync(DevicePermissionSaveRequest request)
|
||||||
{
|
{
|
||||||
var myData = GetMyUniqueValues(request.UniqueType);
|
var myData = GetMyUniqueValues(request.UniqueType);
|
||||||
|
|
||||||
var currentAssignValue = request.AssignType == AssignTypeEnumType.Department ? request.TenantId + ":" + request.AssignValue : request.AssignValue;
|
|
||||||
return await DevicePermission.Where(x => x.AssignType == request.AssignType
|
return await DevicePermission.Where(x => x.AssignType == request.AssignType
|
||||||
&& x.AssignValue == currentAssignValue
|
&& x.AssignValue == request.GetActualAssignValue()
|
||||||
&& x.UniqueType == request.UniqueType
|
&& x.UniqueType == request.UniqueType
|
||||||
&& x.Type == request.Type
|
&& x.Type == request.Type
|
||||||
&& x.TenantId == CurrentTenantId)
|
&& x.TenantId == CurrentTenantId)
|
||||||
@@ -188,6 +254,13 @@ public abstract class BasePermissionAnalyzeService : IPermissionAnalyzeService
|
|||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取现在权限与当前的分配之间的deviceCode差异
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="type"></param>
|
||||||
|
/// <param name="dps"></param>
|
||||||
|
/// <param name="uniqueValues"></param>
|
||||||
|
/// <returns></returns>
|
||||||
private async Task<(List<Guid>, List<Guid>)> GetDiffAsync(DeviceAssignmentTypeEnumType type, List<DevicePermissionEntity> dps, List<Guid> uniqueValues)
|
private async Task<(List<Guid>, List<Guid>)> GetDiffAsync(DeviceAssignmentTypeEnumType type, List<DevicePermissionEntity> dps, List<Guid> uniqueValues)
|
||||||
{
|
{
|
||||||
var beforeCodes = dps.Select(x => x.UniqueValue).ToList();
|
var beforeCodes = dps.Select(x => x.UniqueValue).ToList();
|
||||||
@@ -196,8 +269,8 @@ public abstract class BasePermissionAnalyzeService : IPermissionAnalyzeService
|
|||||||
if (type == DeviceAssignmentTypeEnumType.Group)
|
if (type == DeviceAssignmentTypeEnumType.Group)
|
||||||
{
|
{
|
||||||
var deviceGroups = await DbContext.Queryable<DeviceGroupEntity>().ToListAsync();
|
var deviceGroups = await DbContext.Queryable<DeviceGroupEntity>().ToListAsync();
|
||||||
beforeCodes = await GetDeviceCodeByGroupAsync(deviceGroups, beforeCodes);
|
beforeCodes = await GetDeviceCodeByGroupAsync(beforeCodes, deviceGroups);
|
||||||
presentCodes = await GetDeviceCodeByGroupAsync(deviceGroups, presentCodes);
|
presentCodes = await GetDeviceCodeByGroupAsync(presentCodes, deviceGroups);
|
||||||
}
|
}
|
||||||
|
|
||||||
var codesToAdd = presentCodes.Except(beforeCodes).ToList();
|
var codesToAdd = presentCodes.Except(beforeCodes).ToList();
|
||||||
@@ -207,13 +280,16 @@ public abstract class BasePermissionAnalyzeService : IPermissionAnalyzeService
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private async Task<List<Guid>> GetDeviceCodeByGroupAsync(List<DeviceGroupEntity> deviceGroups, List<Guid> codes)
|
/// <summary>
|
||||||
|
/// 获取分组下的deviceCode
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="codes"></param>
|
||||||
|
/// <param name="deviceGroups"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
private async Task<List<Guid>> GetDeviceCodeByGroupAsync(List<Guid> codes, List<DeviceGroupEntity> deviceGroups)
|
||||||
{
|
{
|
||||||
var result = new List<Guid>();
|
var result = new List<Guid>();
|
||||||
foreach (var groupCode in codes)
|
codes.ForEach(groupCode => result.AddRange(RecursionHelper.HandlerDeviceGroupNextCodes(groupCode, deviceGroups)));
|
||||||
{
|
|
||||||
result.AddRange(RecursionHelper.HandlerDeviceGroupNextCodes(groupCode, deviceGroups));
|
|
||||||
}
|
|
||||||
|
|
||||||
result = result.Distinct().ToList();
|
result = result.Distinct().ToList();
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ using Dlhis.Basic.Device.Response;
|
|||||||
using Dlhis.Entity.Basic.Entity;
|
using Dlhis.Entity.Basic.Entity;
|
||||||
using Dlhis.Entity.Device.Entities;
|
using Dlhis.Entity.Device.Entities;
|
||||||
using Dlhis.Entity.Device.Enums;
|
using Dlhis.Entity.Device.Enums;
|
||||||
|
using Dlhis.Entity.Sys.Entities;
|
||||||
|
using Dlhis.Entity.Sys.Enums;
|
||||||
using Dlhis.Sys.Api;
|
using Dlhis.Sys.Api;
|
||||||
using Kurisu.AspNetCore.Authentication.Abstractions;
|
using Kurisu.AspNetCore.Authentication.Abstractions;
|
||||||
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
||||||
@@ -14,21 +16,19 @@ namespace Dlhis.Basic.Device.Services.Implements;
|
|||||||
public class DeviceGroupService : IDeviceGroupService
|
public class DeviceGroupService : IDeviceGroupService
|
||||||
{
|
{
|
||||||
private readonly IDbContext _dbContext;
|
private readonly IDbContext _dbContext;
|
||||||
private readonly IDevicePermissionAnalyzeService _devicePermissionAnalyzeService;
|
|
||||||
private readonly ICurrentUser _currentUser;
|
private readonly ICurrentUser _currentUser;
|
||||||
|
private readonly INamedResolver _namedResolver;
|
||||||
private readonly ITenantApi _tenantApi;
|
private readonly ITenantApi _tenantApi;
|
||||||
private readonly IParallelService _parallelService;
|
|
||||||
|
|
||||||
public DeviceGroupService(IDbContext dbContext,
|
public DeviceGroupService(IDbContext dbContext,
|
||||||
IDevicePermissionAnalyzeService devicePermissionAnalyzeService,
|
|
||||||
ICurrentUser currentUser,
|
ICurrentUser currentUser,
|
||||||
ITenantApi tenantApi, IParallelService parallelService)
|
INamedResolver namedResolver,
|
||||||
|
ITenantApi tenantApi)
|
||||||
{
|
{
|
||||||
_dbContext = dbContext;
|
_dbContext = dbContext;
|
||||||
_devicePermissionAnalyzeService = devicePermissionAnalyzeService;
|
|
||||||
_currentUser = currentUser;
|
_currentUser = currentUser;
|
||||||
|
_namedResolver = namedResolver;
|
||||||
_tenantApi = tenantApi;
|
_tenantApi = tenantApi;
|
||||||
_parallelService = parallelService;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -105,24 +105,57 @@ public class DeviceGroupService : IDeviceGroupService
|
|||||||
await CheckNoNextGroupAsync(group.Code);
|
await CheckNoNextGroupAsync(group.Code);
|
||||||
await CheckGroupTypeMatchAsync(request, group);
|
await CheckGroupTypeMatchAsync(request, group);
|
||||||
|
|
||||||
_parallelService.Enable = true;
|
var relationships = await _dbContext.Queryable<DeviceGroupRelationshipEntity>().Where(x => x.GroupCode == request.GroupCode).ToListAsync();
|
||||||
//下发任务
|
await _dbContext.DeleteAsync(relationships);
|
||||||
await _devicePermissionAnalyzeService.GenerateDeviceTasksAsync(async () =>
|
|
||||||
|
var entities = request.DeviceCodes.Select(x => new DeviceGroupRelationshipEntity
|
||||||
{
|
{
|
||||||
var relationships = await _dbContext.Queryable<DeviceGroupRelationshipEntity>().Where(x => x.GroupCode == request.GroupCode).ToListAsync();
|
GroupCode = request.GroupCode,
|
||||||
await _dbContext.DeleteAsync(relationships);
|
DeviceCode = x,
|
||||||
|
|
||||||
var entities = request.DeviceCodes.Select(x => new DeviceGroupRelationshipEntity
|
TenantId = _currentUser.GetStringTenantId(),
|
||||||
|
}).ToList();
|
||||||
|
|
||||||
|
await _dbContext.InsertAsync(entities);
|
||||||
|
|
||||||
|
await PermissionResetAsync(request.GroupCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task PermissionResetAsync(Guid groupCode)
|
||||||
|
{
|
||||||
|
var deviceGroups = RecursionHelper.HandlerDeviceGroupPreviousCodes(groupCode, await _dbContext.Queryable<DeviceGroupEntity>().ToListAsync());
|
||||||
|
|
||||||
|
var dps = await _dbContext.Queryable<DevicePermissionEntity>()
|
||||||
|
.Where(x => x.UniqueType == DeviceAssignmentTypeEnumType.Group)
|
||||||
|
.Where(x => deviceGroups.Contains(x.UniqueValue))
|
||||||
|
.ToListAsync();
|
||||||
|
|
||||||
|
if (dps.Any())
|
||||||
|
{
|
||||||
|
var type = dps.First().Type;
|
||||||
|
|
||||||
|
foreach (var devicePermissionGroup in dps.GroupBy(x => new { x.AssignType, x.AssignValue, x.TenantId }))
|
||||||
{
|
{
|
||||||
GroupCode = request.GroupCode,
|
var assignType = devicePermissionGroup.Key.AssignType;
|
||||||
DeviceCode = x,
|
var assignValue = devicePermissionGroup.Key.AssignValue;
|
||||||
|
var tenantId = devicePermissionGroup.Key.TenantId;
|
||||||
|
var analyzeService = _namedResolver.GetService<IPermissionAnalyzeService>(PermissionAnalyzeConstants.GetTypeName(assignType));
|
||||||
|
|
||||||
TenantId = _currentUser.GetStringTenantId(),
|
var codes = devicePermissionGroup.Select(x => x.UniqueValue).Distinct().ToList();
|
||||||
}).ToList();
|
|
||||||
|
|
||||||
await _dbContext.InsertAsync(entities);
|
await analyzeService.AnalyzeAsync(new DevicePermissionSaveRequest
|
||||||
}, null);
|
{
|
||||||
_parallelService.Enable = false;
|
AssignType = assignType,
|
||||||
|
AssignValue = assignValue,
|
||||||
|
UniqueType = DeviceAssignmentTypeEnumType.Group,
|
||||||
|
Codes = codes,
|
||||||
|
TenantId = tenantId,
|
||||||
|
Type = type
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<List<DeviceUnderGroupResponse>> GetDevicesByGroupCodeAsync(Guid code)
|
public async Task<List<DeviceUnderGroupResponse>> GetDevicesByGroupCodeAsync(Guid code)
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
using Dlhis.Basic.DataPermissions;
|
using Dlhis.Basic.DataPermissions;
|
||||||
using Dlhis.Basic.Device.Dtos;
|
using Dlhis.Basic.Device.Dtos;
|
||||||
using Dlhis.Basic.Device.Request;
|
using Dlhis.Basic.Device.Request;
|
||||||
@@ -15,55 +14,34 @@ using Kurisu.AspNetCore.Authentication.Abstractions;
|
|||||||
using Kurisu.AspNetCore.DataAccess.Entity;
|
using Kurisu.AspNetCore.DataAccess.Entity;
|
||||||
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
||||||
using Kurisu.AspNetCore.Utils.Extensions;
|
using Kurisu.AspNetCore.Utils.Extensions;
|
||||||
using SqlSugar;
|
|
||||||
|
|
||||||
namespace Dlhis.Basic.Device.Services.Implements;
|
namespace Dlhis.Basic.Device.Services.Implements;
|
||||||
|
|
||||||
public class DevicePermissionAnalyzeService : IDevicePermissionAnalyzeService
|
public class DevicePermissionAnalyzeService : IDevicePermissionAnalyzeService
|
||||||
{
|
{
|
||||||
private readonly IDbContext _dbContext;
|
private readonly IDbContext _dbContext;
|
||||||
private readonly IParallelService _parallelService;
|
|
||||||
private readonly ICurrentUser _currentUser;
|
private readonly ICurrentUser _currentUser;
|
||||||
private readonly ILogger<DevicePermissionAnalyzeService> _logger;
|
|
||||||
private readonly IMyDataPermissionService _myDataPermissionService;
|
private readonly IMyDataPermissionService _myDataPermissionService;
|
||||||
|
|
||||||
public DevicePermissionAnalyzeService(IDbContext dbContext,
|
public DevicePermissionAnalyzeService(IDbContext dbContext,
|
||||||
IParallelService parallelService,
|
|
||||||
ICurrentUser currentUser,
|
ICurrentUser currentUser,
|
||||||
IMyDataPermissionService myDataPermissionService,
|
IMyDataPermissionService myDataPermissionService)
|
||||||
ILogger<DevicePermissionAnalyzeService> logger)
|
|
||||||
{
|
{
|
||||||
_dbContext = dbContext;
|
_dbContext = dbContext;
|
||||||
_parallelService = parallelService;
|
|
||||||
_currentUser = currentUser;
|
_currentUser = currentUser;
|
||||||
_logger = logger;
|
|
||||||
_myDataPermissionService = myDataPermissionService;
|
_myDataPermissionService = myDataPermissionService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<DevicePermissionAssignResponse> GenerateDeviceTasksAsync(Func<Task> func, List<string> userNos)
|
public async Task<DevicePermissionAssignResponse> GenerateDeviceTasksAsync(Func<Task> func, List<string> userNos)
|
||||||
{
|
{
|
||||||
var getAllUser = userNos.IsEmpty();
|
var before = await GetUserAssignDeviceCodesInTenantAsync(userNos);
|
||||||
|
|
||||||
//获取所有人的权限
|
|
||||||
if (getAllUser)
|
|
||||||
userNos = await GetPermissionUserNosAsync();
|
|
||||||
|
|
||||||
var before = await GetAssignDeviceCodesInternalAsync(userNos);
|
|
||||||
|
|
||||||
//业务处理
|
//业务处理
|
||||||
_dbContext.Client.QueryFilter.Restore();
|
_dbContext.Client.QueryFilter.Restore();
|
||||||
await func();
|
await func();
|
||||||
_dbContext.Client.QueryFilter.ClearAndBackup<ITenantId>();
|
_dbContext.Client.QueryFilter.ClearAndBackup<ITenantId>();
|
||||||
|
|
||||||
//获取业务处理后所有人的权限
|
var current = await GetUserAssignDeviceCodesInTenantAsync(userNos);
|
||||||
if (getAllUser)
|
|
||||||
{
|
|
||||||
var nextUserNos = await GetPermissionUserNosAsync();
|
|
||||||
if (nextUserNos.Any())
|
|
||||||
userNos = userNos.Concat(nextUserNos).Distinct().ToList();
|
|
||||||
}
|
|
||||||
|
|
||||||
var current = await GetAssignDeviceCodesInternalAsync(userNos);
|
|
||||||
|
|
||||||
var devices = await _dbContext.Queryable<DeviceEntity>().Where(x => x.Enable).ToListAsync();
|
var devices = await _dbContext.Queryable<DeviceEntity>().Where(x => x.Enable).ToListAsync();
|
||||||
|
|
||||||
@@ -72,10 +50,8 @@ public class DevicePermissionAnalyzeService : IDevicePermissionAnalyzeService
|
|||||||
|
|
||||||
foreach (var userNo in userNos)
|
foreach (var userNo in userNos)
|
||||||
{
|
{
|
||||||
before.TryGetValue(userNo, out var beforeTenantDeviceCodes);
|
var beforeTenantDeviceCodes = before[userNo].ToList();
|
||||||
beforeTenantDeviceCodes ??= new List<UserTenantDeviceCodeDto>();
|
var currentTenantDeviceCodes = current[userNo].ToList();
|
||||||
current.TryGetValue(userNo, out var currentTenantDeviceCodes);
|
|
||||||
currentTenantDeviceCodes ??= new List<UserTenantDeviceCodeDto>();
|
|
||||||
|
|
||||||
var tenants = new HashSet<string>();
|
var tenants = new HashSet<string>();
|
||||||
foreach (var item in beforeTenantDeviceCodes)
|
foreach (var item in beforeTenantDeviceCodes)
|
||||||
@@ -133,107 +109,14 @@ public class DevicePermissionAnalyzeService : IDevicePermissionAnalyzeService
|
|||||||
await ChunkInsertAsync(waitAddTasks);
|
await ChunkInsertAsync(waitAddTasks);
|
||||||
await ChunkInsertAsync(waitDeleteTasks);
|
await ChunkInsertAsync(waitDeleteTasks);
|
||||||
|
|
||||||
return new DevicePermissionAssignResponse
|
return new DevicePermissionAssignResponse(waitAddTasks.Count, waitDeleteTasks.Count);
|
||||||
{
|
|
||||||
AddCount = waitAddTasks.Count,
|
|
||||||
DeleteCount = waitDeleteTasks.Count
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public async Task<DevicePermissionAssignResponse> GenerateDeviceTasksV2Async(Func<Task> func, List<string> userNos)
|
public async Task<Dictionary<string, IEnumerable<TenantAssignTypeDeviceCode>>> GetUserAssignDeviceCodesWithTypeInTenantAsync(List<string> userNos, params AssignTypeEnumType[] ignoreAssignTypes)
|
||||||
{
|
|
||||||
var before = await GetAssignDeviceCodesInternalAsync(userNos);
|
|
||||||
//业务处理
|
|
||||||
await _dbContext.IgnoreAsync<ITenantId>(async () => { await func(); });
|
|
||||||
|
|
||||||
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>();
|
|
||||||
|
|
||||||
foreach (var userNo in userNos)
|
|
||||||
{
|
|
||||||
before.TryGetValue(userNo, out var beforeTenantDeviceCodes);
|
|
||||||
beforeTenantDeviceCodes ??= new List<UserTenantDeviceCodeDto>(0);
|
|
||||||
current.TryGetValue(userNo, out var currentTenantDeviceCodes);
|
|
||||||
currentTenantDeviceCodes ??= new List<UserTenantDeviceCodeDto>(0);
|
|
||||||
|
|
||||||
var tenants = new HashSet<string>();
|
|
||||||
foreach (var item in beforeTenantDeviceCodes)
|
|
||||||
tenants.Add(item.Tenant);
|
|
||||||
foreach (var item in currentTenantDeviceCodes)
|
|
||||||
tenants.Add(item.Tenant);
|
|
||||||
|
|
||||||
foreach (var tenant in tenants)
|
|
||||||
{
|
|
||||||
var currentCodes = currentTenantDeviceCodes.FirstOrDefault(x => x.Tenant == tenant)?.Codes?.ToList() ?? new List<Guid>();
|
|
||||||
var beforeCodes = beforeTenantDeviceCodes.FirstOrDefault(x => x.Tenant == tenant)?.Codes?.ToList() ?? new List<Guid>();
|
|
||||||
|
|
||||||
var addItems = currentCodes.Except(beforeCodes).ToList();
|
|
||||||
var deletedItems = beforeCodes.Except(currentCodes).ToList();
|
|
||||||
|
|
||||||
if (!deletedItems.Any() && !addItems.Any())
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
//下发任务创建
|
|
||||||
deletedItems.ForEach(x => waitDeleteTasks.Add(new DeviceTaskEntity
|
|
||||||
{
|
|
||||||
DeviceUniqueNo = devices.First(item => item.Code == x).UniqueNo,
|
|
||||||
Status = DeviceTaskStatusEnumType.Wait,
|
|
||||||
Type = DeviceTaskEnumType.Delete,
|
|
||||||
CancellationId = 0,
|
|
||||||
DeviceType = devices.First(item => item.Code == x).Type,
|
|
||||||
UserNo = userNo,
|
|
||||||
Version = 0,
|
|
||||||
TenantId = tenant,
|
|
||||||
Source = DeviceTaskSourceEnumType.Manually
|
|
||||||
}));
|
|
||||||
|
|
||||||
addItems.ForEach(x => waitAddTasks.Add(new DeviceTaskEntity
|
|
||||||
{
|
|
||||||
DeviceUniqueNo = devices.First(item => item.Code == x).UniqueNo,
|
|
||||||
Status = DeviceTaskStatusEnumType.Wait,
|
|
||||||
Type = DeviceTaskEnumType.Add,
|
|
||||||
DeviceType = devices.First(item => item.Code == x).Type,
|
|
||||||
UserNo = userNo,
|
|
||||||
Version = 0,
|
|
||||||
TenantId = tenant,
|
|
||||||
Source = DeviceTaskSourceEnumType.Manually
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
await ChunkInsertAsync(waitAddTasks);
|
|
||||||
await ChunkInsertAsync(waitDeleteTasks);
|
|
||||||
|
|
||||||
return new DevicePermissionAssignResponse
|
|
||||||
{
|
|
||||||
AddCount = waitAddTasks.Count,
|
|
||||||
DeleteCount = waitDeleteTasks.Count
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
private 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<Dictionary<string, List<TenantAssignTypeDeviceCode>>> GetAssignDeviceCodesInternalAsync(List<string> userNos, params AssignTypeEnumType[] ignoreAssignTypes)
|
|
||||||
{
|
{
|
||||||
var gainfos = await _dbContext.Queryable<GainfoEntity>().Where(x => userNos.Contains(x.No)).ToListAsync();
|
var gainfos = await _dbContext.Queryable<GainfoEntity>().Where(x => userNos.Contains(x.No)).ToListAsync();
|
||||||
if (!gainfos.Any())
|
if (!gainfos.Any()) return new Dictionary<string, IEnumerable<TenantAssignTypeDeviceCode>>();
|
||||||
{
|
|
||||||
return new Dictionary<string, List<TenantAssignTypeDeviceCode>>();
|
|
||||||
}
|
|
||||||
|
|
||||||
var devices = await _dbContext.Queryable<DeviceEntity>().Where(x => x.Enable).ToListAsync();
|
var devices = await _dbContext.Queryable<DeviceEntity>().Where(x => x.Enable).ToListAsync();
|
||||||
|
|
||||||
@@ -247,318 +130,75 @@ public class DevicePermissionAnalyzeService : IDevicePermissionAnalyzeService
|
|||||||
var deviceRelationships = await _dbContext.Queryable<DeviceGroupRelationshipEntity>().ToListAsync();
|
var deviceRelationships = await _dbContext.Queryable<DeviceGroupRelationshipEntity>().ToListAsync();
|
||||||
var departments = await _dbContext.Queryable<DepartmentEntity>().ToListAsync();
|
var departments = await _dbContext.Queryable<DepartmentEntity>().ToListAsync();
|
||||||
|
|
||||||
var result = new ConcurrentDictionary<string, List<TenantAssignTypeDeviceCode>>();
|
var result = new ConcurrentDictionary<string, IEnumerable<TenantAssignTypeDeviceCode>>();
|
||||||
//var total = gainfos.Count;
|
|
||||||
|
|
||||||
gainfos.ParallelForEach(gainfo =>
|
gainfos.ParallelForEach(gainfo =>
|
||||||
{
|
{
|
||||||
var gainfoDto = new GainfoDto
|
var internalIgnoreAssignTypes = new List<AssignTypeEnumType>();
|
||||||
|
internalIgnoreAssignTypes.AddRange(ignoreAssignTypes);
|
||||||
|
if (gainfo.Type == GainfoUserTypeEnumType.Outsource && (enableFactoryPermissions.FirstOrDefault(x => x.TenantId == gainfo.Factory)?.EnableOutsourceDown ?? true) == false)
|
||||||
{
|
{
|
||||||
Id = gainfo.Id,
|
internalIgnoreAssignTypes.Add(AssignTypeEnumType.Tenant);
|
||||||
No = gainfo.No,
|
}
|
||||||
Code = gainfo.Code,
|
|
||||||
Name = gainfo.Name,
|
var analyzePack = new AnalyzePackDto
|
||||||
Workplace = gainfo.Workplace,
|
{
|
||||||
Factory = gainfo.Factory,
|
UserNo = gainfo.No,
|
||||||
DepartmentNo = gainfo.DepartmentNo,
|
UserDepartment = gainfo.Factory + ":" + gainfo.DepartmentNo,
|
||||||
IsOutsource = gainfo.Type == GainfoUserTypeEnumType.Outsource,
|
UserFactory = gainfo.Factory,
|
||||||
EnableFactoryPermission = enableFactoryPermissions.FirstOrDefault(x => x.TenantId == gainfo.Factory)?.EnableOutsourceDown ?? true
|
UserGainfoGroupCodes = gainfoGroupRelationships.Where(x => x.UserNo == gainfo.No).Select(x => x.GroupCode),
|
||||||
|
DeviceCodes = devices.WhereIf(gainfo.Type == GainfoUserTypeEnumType.Outsource, x => x.Type != DeviceTypeEnumType.Meal).Select(x => x.Code), //外包人员不允许餐饮
|
||||||
|
IgnoreAssignTypeTypes = internalIgnoreAssignTypes
|
||||||
};
|
};
|
||||||
|
|
||||||
var codes = GetAssignDeviceCodesInternal2(gainfoDto, gainfoGroups, gainfoGroupRelationships, devicePermissions, deviceGroups, deviceRelationships, devices, departments, ignoreAssignTypes);
|
var codes = GetAssignDeviceCodesInternal(analyzePack,
|
||||||
result.TryAdd(gainfo.No, codes);
|
gainfoGroups,
|
||||||
|
devicePermissions,
|
||||||
|
deviceGroups,
|
||||||
|
deviceRelationships,
|
||||||
|
departments);
|
||||||
|
|
||||||
// Interlocked.Decrement(ref total);
|
result.TryAdd(gainfo.No, codes);
|
||||||
// _logger.LogInformation("剩余:{count}", total);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return result.ToDictionary(x => x.Key, x => x.Value);
|
return result.ToDictionary(x => x.Key, x => x.Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public async Task<Dictionary<string, List<UserTenantDeviceCodeDto>>> GetAssignDeviceCodesInternalAsync(List<string> userNos)
|
private static IEnumerable<TenantAssignTypeDeviceCode> GetAssignDeviceCodesInternal(
|
||||||
{
|
AnalyzePackDto analyzePack,
|
||||||
var devices = await _dbContext.Queryable<DeviceEntity>().Where(x => x.Enable).ToListAsync();
|
|
||||||
var deviceUniqueNos = devices.Select(x => x.UniqueNo).ToList();
|
|
||||||
var registries = await _dbContext.Queryable<DeviceRegistryEntity>()
|
|
||||||
.Where(x => deviceUniqueNos.Contains(x.DeviceUniqueNo))
|
|
||||||
.Where(x => !SqlFunc.IsNullOrEmpty(x.BusinessPlaceCode))
|
|
||||||
.ToListAsync();
|
|
||||||
|
|
||||||
var gainfos = await _dbContext.Queryable<GainfoEntity>().Where(x => userNos.Contains(x.No)).ToListAsync();
|
|
||||||
if (!gainfos.Any())
|
|
||||||
{
|
|
||||||
return new Dictionary<string, List<UserTenantDeviceCodeDto>>();
|
|
||||||
}
|
|
||||||
|
|
||||||
var enableFactoryPermissions = await _dbContext.Queryable<FactorySettingEntity>().ToListAsync();
|
|
||||||
|
|
||||||
var gainfoGroupRelationships = await _dbContext.Queryable<GainfoGroupRelationshipEntity>().Where(x => userNos.Contains(x.UserNo)).ToListAsync();
|
|
||||||
var devicePermissions = await _dbContext.Queryable<DevicePermissionEntity>().ToListAsync();
|
|
||||||
var deviceGroups = await _dbContext.Queryable<DeviceGroupEntity>().ToListAsync();
|
|
||||||
var deviceRelationships = await _dbContext.Queryable<DeviceGroupRelationshipEntity>().ToListAsync();
|
|
||||||
var departments = await _dbContext.Queryable<DepartmentEntity>().ToListAsync();
|
|
||||||
|
|
||||||
if (_parallelService.Enable)
|
|
||||||
{
|
|
||||||
var result = new ConcurrentDictionary<string, List<UserTenantDeviceCodeDto>>();
|
|
||||||
var total = gainfos.Count;
|
|
||||||
Parallel.ForEach(gainfos, new ParallelOptions { MaxDegreeOfParallelism = 4 }, gainfo =>
|
|
||||||
{
|
|
||||||
var gainfoDto = new GainfoDto
|
|
||||||
{
|
|
||||||
Id = gainfo.Id,
|
|
||||||
No = gainfo.No,
|
|
||||||
Code = gainfo.Code,
|
|
||||||
Name = gainfo.Name,
|
|
||||||
Workplace = gainfo.Workplace,
|
|
||||||
Factory = gainfo.Factory,
|
|
||||||
DepartmentNo = gainfo.DepartmentNo,
|
|
||||||
IsOutsource = gainfo.Type == GainfoUserTypeEnumType.Outsource,
|
|
||||||
EnableFactoryPermission = enableFactoryPermissions.FirstOrDefault(x => x.TenantId == gainfo.Factory)?.EnableOutsourceDown ?? true
|
|
||||||
};
|
|
||||||
|
|
||||||
var codes = GetAssignDeviceCodesInternal(gainfoDto, gainfoGroupRelationships, devicePermissions, deviceGroups, deviceRelationships, registries, devices, departments);
|
|
||||||
result.TryAdd(gainfo.No, codes);
|
|
||||||
|
|
||||||
Interlocked.Decrement(ref total);
|
|
||||||
_logger.LogInformation("剩余:{count}", total);
|
|
||||||
});
|
|
||||||
|
|
||||||
return result.ToDictionary(x => x.Key, x => x.Value);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
var result = new Dictionary<string, List<UserTenantDeviceCodeDto>>();
|
|
||||||
var total = gainfos.Count;
|
|
||||||
foreach (var gainfo in gainfos)
|
|
||||||
{
|
|
||||||
var gainfoDto = new GainfoDto
|
|
||||||
{
|
|
||||||
Id = gainfo.Id,
|
|
||||||
No = gainfo.No,
|
|
||||||
Code = gainfo.Code,
|
|
||||||
Name = gainfo.Name,
|
|
||||||
Workplace = gainfo.Workplace,
|
|
||||||
Factory = gainfo.Factory,
|
|
||||||
DepartmentNo = gainfo.DepartmentNo,
|
|
||||||
IsOutsource = gainfo.Type == GainfoUserTypeEnumType.Outsource,
|
|
||||||
EnableFactoryPermission = enableFactoryPermissions.FirstOrDefault(x => x.TenantId == gainfo.Factory)?.EnableOutsourceDown ?? true
|
|
||||||
};
|
|
||||||
|
|
||||||
var codes = GetAssignDeviceCodesInternal(gainfoDto, gainfoGroupRelationships, devicePermissions, deviceGroups, deviceRelationships, registries, devices, departments);
|
|
||||||
result.Add(gainfo.No, codes);
|
|
||||||
total--;
|
|
||||||
_logger.LogInformation("剩余:{count}", total);
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<List<Guid>> GetAssignmentsAsync(DeviceGetAssignmentsRequest request)
|
|
||||||
{
|
|
||||||
request.AssignValue = request.AssignType == AssignTypeEnumType.Department ? request.TenantId + ":" + request.AssignValue : request.AssignValue;
|
|
||||||
var role = _currentUser.GetRole();
|
|
||||||
var isAll = role is "admin" or "tenant-admin";
|
|
||||||
var deviceCodes = _myDataPermissionService.GetMyDeviceCodes();
|
|
||||||
|
|
||||||
return await _dbContext.Queryable<DevicePermissionEntity>()
|
|
||||||
.Where(x => x.AssignType == request.AssignType)
|
|
||||||
.Where(x => x.AssignValue == request.AssignValue)
|
|
||||||
.Where(x => x.UniqueType == request.UniqueType)
|
|
||||||
.WhereIF(request.UniqueType == DeviceAssignmentTypeEnumType.Device && !isAll, x => deviceCodes.Contains(x.UniqueValue))
|
|
||||||
.Where(x => x.Type == request.Type)
|
|
||||||
.Select(x => x.UniqueValue)
|
|
||||||
.ToListAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<List<GainfoDto>> GetAssignmentUniqueAsync(DeviceGetAssignmentUniquesRequest request)
|
|
||||||
{
|
|
||||||
var nos = await _dbContext.Queryable<DevicePermissionEntity>()
|
|
||||||
.Where(x => x.UniqueType == request.UniqueType)
|
|
||||||
.Where(x => x.UniqueValue == request.UniqueValue)
|
|
||||||
.Where(x => x.AssignType == AssignTypeEnumType.User)
|
|
||||||
.Where(x => x.Type == request.Type)
|
|
||||||
.Select(x => x.AssignValue)
|
|
||||||
.ToListAsync();
|
|
||||||
|
|
||||||
return await _dbContext.Queryable<GainfoEntity>().Where(x => nos.Contains(x.No)).Select(x => new GainfoDto
|
|
||||||
{
|
|
||||||
Id = x.Id,
|
|
||||||
Code = x.Code,
|
|
||||||
Name = x.Name,
|
|
||||||
Workplace = x.Workplace,
|
|
||||||
Factory = x.Factory
|
|
||||||
}).ToListAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<List<Guid>> GetAssignDeviceCodesAsync(string userCode)
|
|
||||||
{
|
|
||||||
var userNo = await _dbContext.Queryable<GainfoEntity>().Where(x => x.Code == userCode || x.No == userCode).Select(x => x.No).SingleAsync();
|
|
||||||
var result = await GetAssignDeviceCodesInternalAsync(new List<string> { userNo });
|
|
||||||
return result.First().Value.SelectMany(x => x.Codes).ToList();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static List<UserTenantDeviceCodeDto> GetAssignDeviceCodesInternal(
|
|
||||||
GainfoDto gainfo,
|
|
||||||
List<GainfoGroupRelationshipEntity> gainfoGroupRelationships,
|
|
||||||
List<DevicePermissionEntity> devicePermissions,
|
|
||||||
List<DeviceGroupEntity> deviceGroups,
|
|
||||||
List<DeviceGroupRelationshipEntity> deviceGroupRelationships,
|
|
||||||
List<DeviceRegistryEntity> deviceRegistries,
|
|
||||||
List<DeviceEntity> devices, List<DepartmentEntity> departments)
|
|
||||||
{
|
|
||||||
if (gainfo.IsOutsource)
|
|
||||||
{
|
|
||||||
//外包人员不允许餐饮
|
|
||||||
devices = devices.Where(x => x.Type != DeviceTypeEnumType.Meal).ToList();
|
|
||||||
}
|
|
||||||
|
|
||||||
var userNo = gainfo.No;
|
|
||||||
|
|
||||||
var deviceDtos = new List<UserTenantDeviceCodeDto>();
|
|
||||||
var deviceGroupDtos = new List<UserTenantDeviceCodeDto>();
|
|
||||||
var businessPlaceDtos = new List<UserTenantDeviceCodeDto>();
|
|
||||||
|
|
||||||
//获取人员的分组,跨基地
|
|
||||||
var gainfoGroups = gainfoGroupRelationships.Where(x => x.UserNo == userNo).Select(x => x.GroupCode).ToList();
|
|
||||||
|
|
||||||
//按用户
|
|
||||||
var du = GetAssignCodesInternal(devicePermissions, deviceGroups, departments, DeviceAssignmentTypeEnumType.Device, AssignTypeEnumType.User, userNo);
|
|
||||||
var gu = GetAssignCodesInternal(devicePermissions, deviceGroups, departments, DeviceAssignmentTypeEnumType.Group, AssignTypeEnumType.User, userNo);
|
|
||||||
var pu = GetAssignCodesInternal(devicePermissions, deviceGroups, departments, DeviceAssignmentTypeEnumType.BusinessPlace, AssignTypeEnumType.User, userNo);
|
|
||||||
deviceDtos.AddRange(du);
|
|
||||||
deviceGroupDtos.AddRange(gu);
|
|
||||||
businessPlaceDtos.AddRange(pu);
|
|
||||||
|
|
||||||
//按用户组
|
|
||||||
foreach (var groupCode in gainfoGroups)
|
|
||||||
{
|
|
||||||
var dg = GetAssignCodesInternal(devicePermissions, deviceGroups, departments, DeviceAssignmentTypeEnumType.Device, AssignTypeEnumType.Group, groupCode.ToString());
|
|
||||||
var gg = GetAssignCodesInternal(devicePermissions, deviceGroups, departments, DeviceAssignmentTypeEnumType.Group, AssignTypeEnumType.Group, groupCode.ToString());
|
|
||||||
var pg = GetAssignCodesInternal(devicePermissions, deviceGroups, departments, DeviceAssignmentTypeEnumType.BusinessPlace, AssignTypeEnumType.Group, groupCode.ToString());
|
|
||||||
deviceDtos.AddRange(dg);
|
|
||||||
deviceGroupDtos.AddRange(gg);
|
|
||||||
businessPlaceDtos.AddRange(pg);
|
|
||||||
}
|
|
||||||
|
|
||||||
//按部门
|
|
||||||
var currentAssignValue = gainfo.Factory + ":" + gainfo.DepartmentNo;
|
|
||||||
var dd = GetAssignCodesInternal(devicePermissions, deviceGroups, departments, DeviceAssignmentTypeEnumType.Device, AssignTypeEnumType.Department, currentAssignValue);
|
|
||||||
var gd = GetAssignCodesInternal(devicePermissions, deviceGroups, departments, DeviceAssignmentTypeEnumType.Group, AssignTypeEnumType.Department, currentAssignValue);
|
|
||||||
var pd = GetAssignCodesInternal(devicePermissions, deviceGroups, departments, DeviceAssignmentTypeEnumType.BusinessPlace, AssignTypeEnumType.Department, currentAssignValue);
|
|
||||||
deviceDtos.AddRange(dd);
|
|
||||||
deviceGroupDtos.AddRange(gd);
|
|
||||||
businessPlaceDtos.AddRange(pd);
|
|
||||||
|
|
||||||
//按厂区
|
|
||||||
if (!gainfo.IsOutsource || gainfo.EnableFactoryPermission)
|
|
||||||
{
|
|
||||||
var dt = GetAssignCodesInternal(devicePermissions, deviceGroups, departments, DeviceAssignmentTypeEnumType.Device, AssignTypeEnumType.Tenant, gainfo.Factory);
|
|
||||||
var gt = GetAssignCodesInternal(devicePermissions, deviceGroups, departments, DeviceAssignmentTypeEnumType.Group, AssignTypeEnumType.Tenant, gainfo.Factory);
|
|
||||||
var pt = GetAssignCodesInternal(devicePermissions, deviceGroups, departments, DeviceAssignmentTypeEnumType.BusinessPlace, AssignTypeEnumType.Tenant, gainfo.Factory);
|
|
||||||
deviceDtos.AddRange(dt);
|
|
||||||
deviceGroupDtos.AddRange(gt);
|
|
||||||
businessPlaceDtos.AddRange(pt);
|
|
||||||
}
|
|
||||||
|
|
||||||
var deviceGroupCodes = deviceGroupDtos.SelectMany(x => x.Codes).Distinct().ToList();
|
|
||||||
var businessPlaceCodes = businessPlaceDtos.SelectMany(x => x.Codes).Distinct().ToList();
|
|
||||||
|
|
||||||
var deviceCodesByGroup = deviceGroupRelationships.Where(x => deviceGroupCodes.Contains(x.GroupCode))
|
|
||||||
.GroupBy(x => x.TenantId)
|
|
||||||
.Select(x => new UserTenantDeviceCodeDto
|
|
||||||
{
|
|
||||||
Tenant = x.Key,
|
|
||||||
Codes = x.Select(item => item.DeviceCode).Distinct().ToList()
|
|
||||||
});
|
|
||||||
|
|
||||||
var placeGroup = deviceRegistries.Where(x => businessPlaceCodes.Contains(x.BusinessPlaceCode!.Value))
|
|
||||||
.GroupBy(x => x.TenantId)
|
|
||||||
.Select(x =>
|
|
||||||
{
|
|
||||||
var currentDeviceUniqueNos = x.Select(item => item.DeviceUniqueNo).ToList();
|
|
||||||
|
|
||||||
return new UserTenantDeviceCodeDto
|
|
||||||
{
|
|
||||||
Tenant = x.Key,
|
|
||||||
Codes = devices.Where(d => currentDeviceUniqueNos.Contains(d.UniqueNo)).Select(d => d.Code).Distinct().ToList()
|
|
||||||
};
|
|
||||||
}).ToList();
|
|
||||||
|
|
||||||
deviceDtos.AddRange(deviceCodesByGroup);
|
|
||||||
deviceDtos.AddRange(placeGroup);
|
|
||||||
|
|
||||||
return deviceDtos.GroupBy(x => x.Tenant).Select(x =>
|
|
||||||
{
|
|
||||||
var dto = new UserTenantDeviceCodeDto
|
|
||||||
{
|
|
||||||
Tenant = x.Key,
|
|
||||||
Codes = x.SelectMany(item => item.Codes).Distinct().ToList()
|
|
||||||
};
|
|
||||||
|
|
||||||
dto.Codes = dto.Codes.Where(p => devices.Any(item => item.Code == p)).ToList();
|
|
||||||
|
|
||||||
return dto;
|
|
||||||
}).ToList();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static List<TenantAssignTypeDeviceCode> GetAssignDeviceCodesInternal2(
|
|
||||||
GainfoDto gainfo,
|
|
||||||
List<GainfoGroupEntity> gainfoGroups,
|
List<GainfoGroupEntity> gainfoGroups,
|
||||||
List<GainfoGroupRelationshipEntity> gainfoGroupRelationships,
|
|
||||||
List<DevicePermissionEntity> devicePermissions,
|
List<DevicePermissionEntity> devicePermissions,
|
||||||
List<DeviceGroupEntity> deviceGroups,
|
List<DeviceGroupEntity> deviceGroups,
|
||||||
List<DeviceGroupRelationshipEntity> deviceGroupRelationships,
|
List<DeviceGroupRelationshipEntity> deviceGroupRelationships,
|
||||||
List<DeviceEntity> devices, List<DepartmentEntity> departments, params AssignTypeEnumType[] ignoreAssignTypes)
|
List<DepartmentEntity> departments)
|
||||||
{
|
{
|
||||||
if (gainfo.IsOutsource)
|
|
||||||
{
|
|
||||||
//外包人员不允许餐饮
|
|
||||||
devices = devices.Where(x => x.Type != DeviceTypeEnumType.Meal).ToList();
|
|
||||||
}
|
|
||||||
|
|
||||||
var deviceCodes = devices.Select(x => x.Code).ToList();
|
|
||||||
|
|
||||||
var deviceDtos = new List<TenantAssignTypeDeviceCode>();
|
var deviceDtos = new List<TenantAssignTypeDeviceCode>();
|
||||||
var deviceGroupDtos = new List<TenantAssignTypeDeviceCode>();
|
var deviceGroupDtos = new List<TenantAssignTypeDeviceCode>();
|
||||||
|
|
||||||
//获取人员的分组,跨基地
|
foreach (var assignType in new[] { AssignTypeEnumType.User, AssignTypeEnumType.Group, AssignTypeEnumType.Department, AssignTypeEnumType.Tenant })
|
||||||
var myGainfoGroups = gainfoGroupRelationships.Where(x => x.UserNo == gainfo.No).Select(x => x.GroupCode).ToList();
|
|
||||||
|
|
||||||
if (ignoreAssignTypes.IsEmpty() || !ignoreAssignTypes.Contains(AssignTypeEnumType.User))
|
|
||||||
{
|
{
|
||||||
//按用户
|
var assignValues = assignType switch
|
||||||
deviceDtos.AddRange(GetMyTypeDevicePermissionAsync(devicePermissions, gainfoGroups, deviceGroups, departments, DeviceAssignmentTypeEnumType.Device, AssignTypeEnumType.User, gainfo.No));
|
|
||||||
deviceGroupDtos.AddRange(GetMyTypeDevicePermissionAsync(devicePermissions, gainfoGroups, deviceGroups, departments, DeviceAssignmentTypeEnumType.Group, AssignTypeEnumType.User, gainfo.No));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ignoreAssignTypes.IsEmpty() || !ignoreAssignTypes.Contains(AssignTypeEnumType.Group))
|
|
||||||
{
|
|
||||||
//按用户组
|
|
||||||
foreach (var groupCode in myGainfoGroups)
|
|
||||||
{
|
{
|
||||||
deviceDtos.AddRange(GetMyTypeDevicePermissionAsync(devicePermissions, gainfoGroups, deviceGroups, departments, DeviceAssignmentTypeEnumType.Device, AssignTypeEnumType.Group, groupCode.ToString()));
|
AssignTypeEnumType.User => new[] { analyzePack.UserNo },
|
||||||
deviceGroupDtos.AddRange(GetMyTypeDevicePermissionAsync(devicePermissions, gainfoGroups, deviceGroups, departments, DeviceAssignmentTypeEnumType.Group, AssignTypeEnumType.Group, groupCode.ToString()));
|
AssignTypeEnumType.Group => analyzePack.UserGainfoGroupCodes.Select(x => x.ToString()).ToArray(),
|
||||||
|
AssignTypeEnumType.Department => new[] { analyzePack.UserDepartment },
|
||||||
|
AssignTypeEnumType.Tenant => new[] { analyzePack.UserFactory },
|
||||||
|
AssignTypeEnumType.Role => throw new ArgumentOutOfRangeException(),
|
||||||
|
_ => throw new ArgumentOutOfRangeException()
|
||||||
|
};
|
||||||
|
|
||||||
|
if (analyzePack.IgnoreAssignTypeTypes.Contains(assignType))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (ignoreAssignTypes.IsEmpty() || !ignoreAssignTypes.Contains(AssignTypeEnumType.Department))
|
foreach (var assignValue in assignValues)
|
||||||
{
|
|
||||||
//按部门
|
|
||||||
var currentAssignValue = gainfo.Factory + ":" + gainfo.DepartmentNo;
|
|
||||||
deviceDtos.AddRange(GetMyTypeDevicePermissionAsync(devicePermissions, gainfoGroups, deviceGroups, departments, DeviceAssignmentTypeEnumType.Device, AssignTypeEnumType.Department, currentAssignValue));
|
|
||||||
deviceGroupDtos.AddRange(GetMyTypeDevicePermissionAsync(devicePermissions, gainfoGroups, deviceGroups, departments, DeviceAssignmentTypeEnumType.Group, AssignTypeEnumType.Department, currentAssignValue));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ignoreAssignTypes.IsEmpty() || !ignoreAssignTypes.Contains(AssignTypeEnumType.Tenant))
|
|
||||||
{
|
|
||||||
//按厂区
|
|
||||||
if (!gainfo.IsOutsource || gainfo.EnableFactoryPermission)
|
|
||||||
{
|
{
|
||||||
deviceDtos.AddRange(GetMyTypeDevicePermissionAsync(devicePermissions, gainfoGroups, deviceGroups, departments, DeviceAssignmentTypeEnumType.Device, AssignTypeEnumType.Tenant, gainfo.Factory));
|
deviceDtos.AddRange(GetMyTypeDevicePermissionAsync(devicePermissions, gainfoGroups, deviceGroups, departments,
|
||||||
deviceGroupDtos.AddRange(GetMyTypeDevicePermissionAsync(devicePermissions, gainfoGroups, deviceGroups, departments, DeviceAssignmentTypeEnumType.Group, AssignTypeEnumType.Tenant, gainfo.Factory));
|
DeviceAssignmentTypeEnumType.Device, assignType, assignValue));
|
||||||
|
deviceGroupDtos.AddRange(GetMyTypeDevicePermissionAsync(devicePermissions, gainfoGroups, deviceGroups, departments,
|
||||||
|
DeviceAssignmentTypeEnumType.Group, assignType, assignValue));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -567,17 +207,17 @@ public class DevicePermissionAnalyzeService : IDevicePermissionAnalyzeService
|
|||||||
{
|
{
|
||||||
var tenant = x.Tenant;
|
var tenant = x.Tenant;
|
||||||
|
|
||||||
var deviceGroupCodes = x.Codes.SelectMany(item => item.Codes);
|
var deviceGroupCodes = x.AssignTypes.SelectMany(item => item.Codes);
|
||||||
var type = x.Codes.ElementAt(0).AssignType;
|
var type = x.AssignTypes.ElementAt(0).AssignType;
|
||||||
return new TenantAssignTypeDeviceCode
|
return new TenantAssignTypeDeviceCode
|
||||||
{
|
{
|
||||||
Tenant = tenant,
|
Tenant = tenant,
|
||||||
Codes = new List<AssignTypeDeviceCode>
|
AssignTypes = new[]
|
||||||
{
|
{
|
||||||
new()
|
new AssignTypeDeviceCode
|
||||||
{
|
{
|
||||||
AssignType = type,
|
AssignType = type,
|
||||||
Codes = deviceGroupRelationships.Where(item => deviceGroupCodes.Contains(item.GroupCode)).Select(item => item.DeviceCode).ToList()
|
Codes = deviceGroupRelationships.Where(item => deviceGroupCodes.Contains(item.GroupCode)).Select(item => item.DeviceCode)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -588,69 +228,17 @@ public class DevicePermissionAnalyzeService : IDevicePermissionAnalyzeService
|
|||||||
var dto = new TenantAssignTypeDeviceCode
|
var dto = new TenantAssignTypeDeviceCode
|
||||||
{
|
{
|
||||||
Tenant = x.Key,
|
Tenant = x.Key,
|
||||||
Codes = x.SelectMany(p => p.Codes)
|
AssignTypes = x.SelectMany(p => p.AssignTypes)
|
||||||
.GroupBy(p => p.AssignType)
|
.GroupBy(p => p.AssignType)
|
||||||
.Select(p => new AssignTypeDeviceCode
|
.Select(p => new AssignTypeDeviceCode
|
||||||
{
|
{
|
||||||
AssignType = p.Key,
|
AssignType = p.Key,
|
||||||
Codes = p.SelectMany(item => deviceCodes.Where(it => item.Codes.Contains(it)))
|
Codes = p.SelectMany(item => analyzePack.DeviceCodes.Where(it => item.Codes.Contains(it)))
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
|
|
||||||
return dto;
|
return dto;
|
||||||
}).ToList();
|
});
|
||||||
}
|
|
||||||
|
|
||||||
private static List<UserTenantDeviceCodeDto> GetAssignCodesInternal(
|
|
||||||
List<DevicePermissionEntity> devicePermissions, List<DeviceGroupEntity> deviceGroups, List<DepartmentEntity> departments,
|
|
||||||
DeviceAssignmentTypeEnumType uniqueType, AssignTypeEnumType assignType, string assignValue)
|
|
||||||
{
|
|
||||||
var tenantDevicePermissions = devicePermissions
|
|
||||||
.Where(x => x.UniqueType == uniqueType
|
|
||||||
&& x.AssignType == assignType
|
|
||||||
&& x.AssignValue == assignValue)
|
|
||||||
.GroupBy(x => x.TenantId)
|
|
||||||
.Select(x => new UserTenantDeviceCodeDto { Tenant = x.Key, Codes = x.Select(p => p.UniqueValue) })
|
|
||||||
.ToList();
|
|
||||||
|
|
||||||
//处理获取设备分组的情况
|
|
||||||
if (uniqueType == DeviceAssignmentTypeEnumType.Group)
|
|
||||||
{
|
|
||||||
//开始递归处理
|
|
||||||
var tops = deviceGroups.Where(x => !x.PCode.HasValue).ToList();
|
|
||||||
tops.ForEach(x => { DeviceGroupRecursionGetNext(x, deviceGroups, tenantDevicePermissions, tenantDevicePermissions.SelectMany(item => item.Codes).Contains(x.Code)); });
|
|
||||||
}
|
|
||||||
|
|
||||||
if (assignType == AssignTypeEnumType.Department)
|
|
||||||
{
|
|
||||||
//递归向上获取 assignValue = department.No
|
|
||||||
var arr = assignValue.Split(":");
|
|
||||||
var tenantId = arr.First();
|
|
||||||
var currentDepartmentNo = arr.Last();
|
|
||||||
var departmentNos = new List<string>(12) { currentDepartmentNo };
|
|
||||||
DepartmentRecursionGetPNo(currentDepartmentNo, departments, departmentNos);
|
|
||||||
|
|
||||||
departmentNos = departmentNos.Select(x => tenantId + ":" + x).ToList();
|
|
||||||
|
|
||||||
tenantDevicePermissions = devicePermissions
|
|
||||||
.Where(x => x.UniqueType == uniqueType)
|
|
||||||
.Where(x => x.AssignType == assignType)
|
|
||||||
.Where(x => departmentNos.Contains(x.AssignValue))
|
|
||||||
.GroupBy(x => x.TenantId)
|
|
||||||
.Select(x => new UserTenantDeviceCodeDto { Tenant = x.Key, Codes = x.Select(p => p.UniqueValue) })
|
|
||||||
.ToList();
|
|
||||||
}
|
|
||||||
|
|
||||||
return tenantDevicePermissions.GroupBy(x => x.Tenant).Select(x =>
|
|
||||||
{
|
|
||||||
var tenant = x.Key;
|
|
||||||
|
|
||||||
return new UserTenantDeviceCodeDto
|
|
||||||
{
|
|
||||||
Tenant = tenant,
|
|
||||||
Codes = x.SelectMany(item => item.Codes).ToList()
|
|
||||||
};
|
|
||||||
}).ToList();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static List<UserTenantDeviceCodeDto> GetCurrentAssignTypePermissions(
|
private static List<UserTenantDeviceCodeDto> GetCurrentAssignTypePermissions(
|
||||||
@@ -661,7 +249,6 @@ public class DevicePermissionAnalyzeService : IDevicePermissionAnalyzeService
|
|||||||
AssignTypeEnumType assignType,
|
AssignTypeEnumType assignType,
|
||||||
string assignValue)
|
string assignValue)
|
||||||
{
|
{
|
||||||
//处理分配到部门
|
|
||||||
if (assignType == AssignTypeEnumType.Department)
|
if (assignType == AssignTypeEnumType.Department)
|
||||||
{
|
{
|
||||||
// 2401:00001
|
// 2401:00001
|
||||||
@@ -695,15 +282,16 @@ public class DevicePermissionAnalyzeService : IDevicePermissionAnalyzeService
|
|||||||
|
|
||||||
//user / tenant
|
//user / tenant
|
||||||
return devicePermissions
|
return devicePermissions
|
||||||
.Where(x => x.UniqueType == uniqueType //设备/设备组
|
.Where(x => x.UniqueType == uniqueType
|
||||||
&& x.AssignType == assignType // user/group/department/tenant
|
&& x.AssignType == assignType
|
||||||
&& x.AssignValue == assignValue)
|
&& x.AssignValue == assignValue)
|
||||||
.GroupBy(x => x.TenantId)
|
.GroupBy(x => x.TenantId)
|
||||||
.Select(x => new UserTenantDeviceCodeDto { Tenant = x.Key, Codes = x.Select(p => p.UniqueValue) })
|
.Select(x => new UserTenantDeviceCodeDto { Tenant = x.Key, Codes = x.Select(p => p.UniqueValue) })
|
||||||
.ToList();
|
.ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static List<TenantAssignTypeDeviceCode> GetMyTypeDevicePermissionAsync(
|
|
||||||
|
private static IEnumerable<TenantAssignTypeDeviceCode> GetMyTypeDevicePermissionAsync(
|
||||||
List<DevicePermissionEntity> devicePermissions,
|
List<DevicePermissionEntity> devicePermissions,
|
||||||
List<GainfoGroupEntity> gainfoGroups,
|
List<GainfoGroupEntity> gainfoGroups,
|
||||||
List<DeviceGroupEntity> deviceGroups,
|
List<DeviceGroupEntity> deviceGroups,
|
||||||
@@ -736,120 +324,83 @@ public class DevicePermissionAnalyzeService : IDevicePermissionAnalyzeService
|
|||||||
return new TenantAssignTypeDeviceCode
|
return new TenantAssignTypeDeviceCode
|
||||||
{
|
{
|
||||||
Tenant = tenant,
|
Tenant = tenant,
|
||||||
Codes = new List<AssignTypeDeviceCode>
|
AssignTypes = new[]
|
||||||
{
|
{
|
||||||
new()
|
new AssignTypeDeviceCode
|
||||||
{
|
{
|
||||||
AssignType = assignType,
|
AssignType = assignType,
|
||||||
Codes = x.SelectMany(item => item.Codes),
|
Codes = x.SelectMany(item => item.Codes)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}).ToList();
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
private async Task ChunkInsertAsync<T>(List<T> batch) where T : class, new()
|
||||||
/// 获取当前基地有权限的用户
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
private async Task<List<string>> GetPermissionUserNosAsync()
|
|
||||||
{
|
{
|
||||||
var permissions = await _dbContext.Queryable<DevicePermissionEntity>().ToListAsync();
|
foreach (var tasks in batch.Chunk(500))
|
||||||
|
{
|
||||||
|
await _dbContext.InsertAsync(tasks.ToList());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//User
|
|
||||||
var userNos = permissions.Where(x => x.AssignType == AssignTypeEnumType.User)
|
public async Task<Dictionary<string, IEnumerable<UserTenantDeviceCodeDto>>> GetUserAssignDeviceCodesInTenantAsync(List<string> userNos, params AssignTypeEnumType[] ignoreAssignTypes)
|
||||||
|
{
|
||||||
|
var result = await GetUserAssignDeviceCodesWithTypeInTenantAsync(userNos, ignoreAssignTypes);
|
||||||
|
|
||||||
|
return result.ToDictionary(x => x.Key, x => x.Value.GroupBy(p => p.Tenant).Select(item =>
|
||||||
|
{
|
||||||
|
return new UserTenantDeviceCodeDto
|
||||||
|
{
|
||||||
|
Tenant = item.Key,
|
||||||
|
Codes = item.SelectMany(p => p.AssignTypes).SelectMany(p => p.Codes).Distinct().ToList(),
|
||||||
|
};
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public async Task<List<Guid>> GetAssignmentsAsync(DeviceGetAssignmentsRequest request)
|
||||||
|
{
|
||||||
|
var currentAssignValue = request.AssignType == AssignTypeEnumType.Department ? request.TenantId + ":" + request.AssignValue : request.AssignValue;
|
||||||
|
|
||||||
|
var deviceCodes = _currentUser.GetRole() is "admin" or "tenant-admin" ? new List<Guid>() : _myDataPermissionService.GetMyDeviceCodes();
|
||||||
|
|
||||||
|
return await _dbContext.Queryable<DevicePermissionEntity>()
|
||||||
|
.Where(x => x.AssignType == request.AssignType)
|
||||||
|
.Where(x => x.AssignValue == currentAssignValue)
|
||||||
|
.Where(x => x.UniqueType == request.UniqueType)
|
||||||
|
.WhereIF(deviceCodes.Any() && request.UniqueType == DeviceAssignmentTypeEnumType.Device, x => deviceCodes.Contains(x.UniqueValue))
|
||||||
|
.Where(x => x.Type == request.Type)
|
||||||
|
.Select(x => x.UniqueValue)
|
||||||
|
.ToListAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<List<GainfoDto>> GetAssignmentUniqueAsync(DeviceGetAssignmentUniquesRequest request)
|
||||||
|
{
|
||||||
|
var nos = await _dbContext.Queryable<DevicePermissionEntity>()
|
||||||
|
.Where(x => x.UniqueType == request.UniqueType)
|
||||||
|
.Where(x => x.UniqueValue == request.UniqueValue)
|
||||||
|
.Where(x => x.AssignType == AssignTypeEnumType.User)
|
||||||
|
.Where(x => x.Type == request.Type)
|
||||||
.Select(x => x.AssignValue)
|
.Select(x => x.AssignValue)
|
||||||
.Distinct()
|
|
||||||
.ToList();
|
|
||||||
|
|
||||||
//Group
|
|
||||||
var groupCodes = permissions.Where(x => x.AssignType == AssignTypeEnumType.Group)
|
|
||||||
.Select(x => Guid.Parse(x.AssignValue))
|
|
||||||
.Distinct()
|
|
||||||
.ToList();
|
|
||||||
var userNosByGroup = await _dbContext.Queryable<GainfoGroupRelationshipEntity>()
|
|
||||||
.Where(x => groupCodes.Contains(x.GroupCode))
|
|
||||||
.Select(x => x.UserNo)
|
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
|
|
||||||
//Department
|
return await _dbContext.Queryable<GainfoEntity>().Where(x => nos.Contains(x.No)).Select(x => new GainfoDto
|
||||||
var tenantDepartmentNos = permissions.Where(x => x.AssignType == AssignTypeEnumType.Department)
|
|
||||||
.Where(x => x.AssignValue.Contains(':'))
|
|
||||||
.Select(x =>
|
|
||||||
{
|
|
||||||
var arr = x.AssignValue.Split(':');
|
|
||||||
return new
|
|
||||||
{
|
|
||||||
Factory = arr[0], DepartmentNo = arr[1]
|
|
||||||
};
|
|
||||||
})
|
|
||||||
.GroupBy(x => x.Factory)
|
|
||||||
.ToDictionary(x => x.Key, x => x.Select(p => p.DepartmentNo).Distinct().ToList());
|
|
||||||
var departments = await _dbContext.Queryable<DepartmentEntity>().ToListAsync();
|
|
||||||
|
|
||||||
var gainfoByDeptWhere = LinqExpressionExtensions.False<GainfoEntity>();
|
|
||||||
foreach ((string factory, List<string> departmentNos) in tenantDepartmentNos)
|
|
||||||
{
|
{
|
||||||
var departmentNosToSearch = new List<string>();
|
Id = x.Id,
|
||||||
foreach (var d in departmentNos)
|
Code = x.Code,
|
||||||
{
|
Name = x.Name,
|
||||||
departmentNosToSearch.Add(d);
|
Workplace = x.Workplace,
|
||||||
RecursionGetNextAllDepartment(d, departments, departmentNosToSearch);
|
Factory = x.Factory
|
||||||
}
|
}).ToListAsync();
|
||||||
|
|
||||||
gainfoByDeptWhere = gainfoByDeptWhere.Or(x => x.Factory == factory && departmentNosToSearch.Contains(x.DepartmentNo));
|
|
||||||
}
|
|
||||||
|
|
||||||
var userNosByDepartment = await _dbContext.Queryable<GainfoEntity>()
|
|
||||||
.Where(gainfoByDeptWhere)
|
|
||||||
.Select(x => x.No)
|
|
||||||
.ToListAsync();
|
|
||||||
|
|
||||||
|
|
||||||
//Tenant
|
|
||||||
var tenants = permissions.Where(x => x.AssignType == AssignTypeEnumType.Tenant).Select(x => x.AssignValue).Distinct().ToList();
|
|
||||||
var userNosByWerk = await _dbContext.Queryable<GainfoEntity>().Where(x => tenants.Contains(x.Factory)).Select(x => x.No).ToListAsync();
|
|
||||||
|
|
||||||
return userNos.Concat(userNosByGroup).Concat(userNosByDepartment).Concat(userNosByWerk).Distinct().ToList();
|
|
||||||
|
|
||||||
void RecursionGetNextAllDepartment(string current, List<DepartmentEntity> data, List<string> result)
|
|
||||||
{
|
|
||||||
var next = data.Where(x => x.PNo == current);
|
|
||||||
foreach (var item in next.Select(x => x.No))
|
|
||||||
{
|
|
||||||
result.Add(item);
|
|
||||||
RecursionGetNextAllDepartment(item, data, result);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<List<Guid>> GetAssignDeviceCodesAsync(string userCode)
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
static void DepartmentRecursionGetPNo(string current, List<DepartmentEntity> data, List<string> result)
|
|
||||||
{
|
{
|
||||||
var p = data.FirstOrDefault(x => x.No == current)?.PNo;
|
var userNo = await _dbContext.Queryable<GainfoEntity>().Where(x => x.Code == userCode || x.No == userCode).Select(x => x.No).SingleAsync();
|
||||||
if (!p.IsPresent())
|
var result = await GetUserAssignDeviceCodesInTenantAsync(new List<string> { userNo });
|
||||||
{
|
return result.First().Value.SelectMany(x => x.Codes).ToList();
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
result.Add(p);
|
|
||||||
DepartmentRecursionGetPNo(p, data, result);
|
|
||||||
}
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
static void DeviceGroupRecursionGetNext(DeviceGroupEntity current, List<DeviceGroupEntity> currentDeviceGroups, List<UserTenantDeviceCodeDto> result, bool add)
|
|
||||||
{
|
|
||||||
var next = currentDeviceGroups.Where(x => x.PCode == current.Code).ToList();
|
|
||||||
foreach (var item in next)
|
|
||||||
{
|
|
||||||
if (add)
|
|
||||||
{
|
|
||||||
result.Add(new UserTenantDeviceCodeDto { Tenant = item.TenantId, Codes = new List<Guid> { item.Code } });
|
|
||||||
}
|
|
||||||
|
|
||||||
DeviceGroupRecursionGetNext(item, currentDeviceGroups, result, result.SelectMany(x => x.Codes).Contains(item.Code));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
using Dlhis.Basic.DataPermissions;
|
using Dlhis.Basic.Device.Dtos;
|
||||||
using Dlhis.Basic.Device.Dtos;
|
|
||||||
using Dlhis.Basic.Device.Request;
|
using Dlhis.Basic.Device.Request;
|
||||||
using Dlhis.Basic.Device.Response;
|
using Dlhis.Basic.Device.Response;
|
||||||
using Dlhis.Entity.Basic.Entity;
|
using Dlhis.Entity.Basic.Entity;
|
||||||
@@ -11,10 +10,8 @@ using Dlhis.Entity.Visitor.Entity;
|
|||||||
using Dlhis.Sys.Api;
|
using Dlhis.Sys.Api;
|
||||||
using Kurisu.AspNetCore.Authentication.Abstractions;
|
using Kurisu.AspNetCore.Authentication.Abstractions;
|
||||||
using Kurisu.AspNetCore.DataAccess.Dto;
|
using Kurisu.AspNetCore.DataAccess.Dto;
|
||||||
using Kurisu.AspNetCore.DataAccess.Entity;
|
|
||||||
using Kurisu.AspNetCore.DataAccess.SqlSugar.Extensions;
|
using Kurisu.AspNetCore.DataAccess.SqlSugar.Extensions;
|
||||||
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
||||||
using Kurisu.AspNetCore.Scope;
|
|
||||||
using Kurisu.AspNetCore.Utils.Extensions;
|
using Kurisu.AspNetCore.Utils.Extensions;
|
||||||
using OfficeOpenXml;
|
using OfficeOpenXml;
|
||||||
using SqlSugar;
|
using SqlSugar;
|
||||||
@@ -24,25 +21,19 @@ namespace Dlhis.Basic.Device.Services.Implements;
|
|||||||
public class DevicePermissionService : IDevicePermissionService
|
public class DevicePermissionService : IDevicePermissionService
|
||||||
{
|
{
|
||||||
private readonly IDbContext _dbContext;
|
private readonly IDbContext _dbContext;
|
||||||
private readonly IMyDataPermissionService _myDataPermissionService;
|
|
||||||
private readonly ITenantApi _tenantApi;
|
private readonly ITenantApi _tenantApi;
|
||||||
private readonly IParallelService _parallelService;
|
|
||||||
private readonly INamedResolver _namedResolver;
|
private readonly INamedResolver _namedResolver;
|
||||||
private readonly IDevicePermissionAnalyzeService _devicePermissionAnalyzeService;
|
private readonly IDevicePermissionAnalyzeService _devicePermissionAnalyzeService;
|
||||||
private readonly ICurrentUser _currentUser;
|
private readonly ICurrentUser _currentUser;
|
||||||
|
|
||||||
public DevicePermissionService(IDbContext dbContext,
|
public DevicePermissionService(IDbContext dbContext,
|
||||||
IMyDataPermissionService myDataPermissionService,
|
|
||||||
ITenantApi tenantApi,
|
ITenantApi tenantApi,
|
||||||
IParallelService parallelService,
|
|
||||||
INamedResolver namedResolver,
|
INamedResolver namedResolver,
|
||||||
IDevicePermissionAnalyzeService devicePermissionAnalyzeService,
|
IDevicePermissionAnalyzeService devicePermissionAnalyzeService,
|
||||||
ICurrentUser currentUser)
|
ICurrentUser currentUser)
|
||||||
{
|
{
|
||||||
_dbContext = dbContext;
|
_dbContext = dbContext;
|
||||||
_myDataPermissionService = myDataPermissionService;
|
|
||||||
_tenantApi = tenantApi;
|
_tenantApi = tenantApi;
|
||||||
_parallelService = parallelService;
|
|
||||||
_namedResolver = namedResolver;
|
_namedResolver = namedResolver;
|
||||||
_devicePermissionAnalyzeService = devicePermissionAnalyzeService;
|
_devicePermissionAnalyzeService = devicePermissionAnalyzeService;
|
||||||
_currentUser = currentUser;
|
_currentUser = currentUser;
|
||||||
@@ -50,252 +41,9 @@ public class DevicePermissionService : IDevicePermissionService
|
|||||||
|
|
||||||
public async Task<DevicePermissionAssignResponse> AssignAsync(DevicePermissionSaveRequest request)
|
public async Task<DevicePermissionAssignResponse> AssignAsync(DevicePermissionSaveRequest request)
|
||||||
{
|
{
|
||||||
await _namedResolver.GetService<IPermissionAnalyzeService>(PermissionAnalyzeConstants.GetTypeName(request.AssignType)).AnalyzeAsync(request);
|
return await _namedResolver
|
||||||
|
.GetService<IPermissionAnalyzeService>(PermissionAnalyzeConstants.GetTypeName(request.AssignType))
|
||||||
return null;
|
.AnalyzeAsync(request);
|
||||||
await CheckAssignTopDoorDeviceGroupAsync(request);
|
|
||||||
|
|
||||||
var currentTenantId = _currentUser.GetStringTenantId();
|
|
||||||
var role = _currentUser.GetRole();
|
|
||||||
var isAll = role is "admin" or "tenant-admin";
|
|
||||||
var deviceCodes = isAll ? new List<Guid>() : _myDataPermissionService.GetMyDeviceCodes();
|
|
||||||
|
|
||||||
_parallelService.Enable = true;
|
|
||||||
return await _devicePermissionAnalyzeService.GenerateDeviceTasksAsync(async () =>
|
|
||||||
{
|
|
||||||
request.AssignValue = request.AssignType == AssignTypeEnumType.Department ? request.TenantId + ":" + request.AssignValue : request.AssignValue;
|
|
||||||
|
|
||||||
await _dbContext.IgnoreAsync<ITenantId>(async () =>
|
|
||||||
{
|
|
||||||
//删除当前匹配的基地权限
|
|
||||||
var p = await _dbContext.Queryable<DevicePermissionEntity>()
|
|
||||||
.Where(x => x.AssignType == request.AssignType)
|
|
||||||
.Where(x => x.AssignValue == request.AssignValue)
|
|
||||||
.Where(x => x.UniqueType == request.UniqueType)
|
|
||||||
.WhereIF(request.UniqueType == DeviceAssignmentTypeEnumType.Device && !isAll, x => deviceCodes.Contains(x.UniqueValue))
|
|
||||||
.Where(x => x.Type == request.Type)
|
|
||||||
.Where(x => x.TenantId == currentTenantId)
|
|
||||||
.ToListAsync();
|
|
||||||
|
|
||||||
await _dbContext.DeleteAsync(p);
|
|
||||||
|
|
||||||
//保存配置的权限
|
|
||||||
if (request.Codes.IsPresent())
|
|
||||||
{
|
|
||||||
var devicePermissions = request.Codes.Select(code => new DevicePermissionEntity
|
|
||||||
{
|
|
||||||
AssignType = request.AssignType,
|
|
||||||
AssignValue = request.AssignValue,
|
|
||||||
UniqueType = request.UniqueType,
|
|
||||||
UniqueValue = code,
|
|
||||||
Type = request.Type,
|
|
||||||
TenantId = currentTenantId
|
|
||||||
}).ToList();
|
|
||||||
|
|
||||||
await _dbContext.InsertAsync(devicePermissions);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}, 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<DevicePermissionAssignResponse> GenerateTasksAsync(List<Guid> addDeviceCodes, List<Guid> deleteDeviceCodes, List<string> gainfoNos)
|
|
||||||
{
|
|
||||||
var allDeviceCodes = addDeviceCodes.Concat(deleteDeviceCodes).ToList();
|
|
||||||
var devices = await _dbContext.Queryable<DeviceEntity>()
|
|
||||||
.Where(x => allDeviceCodes.Contains(x.Code))
|
|
||||||
.Select(x => new { x.Code, x.UniqueNo, x.Type, x.TenantId })
|
|
||||||
.ToListAsync();
|
|
||||||
|
|
||||||
var tasks = new List<DeviceTaskEntity>();
|
|
||||||
foreach (var gainfoNo in gainfoNos)
|
|
||||||
{
|
|
||||||
tasks.AddRange(addDeviceCodes.Select(x =>
|
|
||||||
{
|
|
||||||
var d = devices.First(item => item.Code == x);
|
|
||||||
return new DeviceTaskEntity
|
|
||||||
{
|
|
||||||
Type = DeviceTaskEnumType.Add,
|
|
||||||
DeviceType = d.Type,
|
|
||||||
UserId = null,
|
|
||||||
UserNo = gainfoNo,
|
|
||||||
DeviceUniqueNo = d.UniqueNo,
|
|
||||||
Status = DeviceTaskStatusEnumType.Wait,
|
|
||||||
Version = 0,
|
|
||||||
ResultMessage = null,
|
|
||||||
CancellationId = 0,
|
|
||||||
Source = DeviceTaskSourceEnumType.Manually,
|
|
||||||
Priority = 0,
|
|
||||||
TenantId = d.TenantId,
|
|
||||||
};
|
|
||||||
}).ToList());
|
|
||||||
|
|
||||||
tasks.AddRange(deleteDeviceCodes.Select(x =>
|
|
||||||
{
|
|
||||||
var d = devices.First(item => item.Code == x);
|
|
||||||
return new DeviceTaskEntity
|
|
||||||
{
|
|
||||||
Type = DeviceTaskEnumType.Delete,
|
|
||||||
DeviceType = d.Type,
|
|
||||||
UserId = null,
|
|
||||||
UserNo = gainfoNo,
|
|
||||||
DeviceUniqueNo = d.UniqueNo,
|
|
||||||
Status = DeviceTaskStatusEnumType.Wait,
|
|
||||||
Version = 0,
|
|
||||||
ResultMessage = null,
|
|
||||||
CancellationId = 0,
|
|
||||||
Source = DeviceTaskSourceEnumType.Manually,
|
|
||||||
Priority = 0,
|
|
||||||
TenantId = d.TenantId,
|
|
||||||
};
|
|
||||||
}).ToList());
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var items in tasks.Where(x => x.Type == DeviceTaskEnumType.Add).Chunk(500))
|
|
||||||
{
|
|
||||||
await _dbContext.InsertAsync(items.ToList());
|
|
||||||
}
|
|
||||||
|
|
||||||
return new DevicePermissionAssignResponse
|
|
||||||
{
|
|
||||||
AddCount = addDeviceCodes.Count * gainfoNos.Count,
|
|
||||||
DeleteCount = deleteDeviceCodes.Count * gainfoNos.Count
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public async Task<DevicePermissionAssignResponse> AssignDeviceGroupToTenantAsync(string currentTenantId, DevicePermissionSaveRequest request)
|
|
||||||
{
|
|
||||||
var groupData = await _dbContext.Queryable<DevicePermissionEntity>()
|
|
||||||
.Where(x => x.AssignType == AssignTypeEnumType.Tenant)
|
|
||||||
.Where(x => x.AssignValue == request.AssignValue)
|
|
||||||
.Where(x => x.UniqueType == request.UniqueType)
|
|
||||||
.Where(x => x.Type == request.Type)
|
|
||||||
.Where(x => x.TenantId == currentTenantId)
|
|
||||||
.ToListAsync();
|
|
||||||
|
|
||||||
var gainfoNos = await _dbContext.Queryable<GainfoEntity>().Where(x => x.Factory == request.AssignValue).Select(x => x.No).ToListAsync();
|
|
||||||
|
|
||||||
var beforeDeviceCodes = groupData.Select(x => x.UniqueValue).ToList();
|
|
||||||
var presentDeviceCodes = request.Codes.IsPresent() ? request.Codes : new List<Guid>();
|
|
||||||
|
|
||||||
var addDeviceGroupCodes = presentDeviceCodes.Except(beforeDeviceCodes).ToList();
|
|
||||||
var deleteDeviceGroupCodes = beforeDeviceCodes.Except(presentDeviceCodes).ToList();
|
|
||||||
|
|
||||||
await _dbContext.DeleteAsync(groupData);
|
|
||||||
//保存配置的权限
|
|
||||||
if (request.Codes.IsPresent())
|
|
||||||
{
|
|
||||||
var devicePermissions = request.Codes.Select(code => new DevicePermissionEntity
|
|
||||||
{
|
|
||||||
AssignType = request.AssignType,
|
|
||||||
AssignValue = request.AssignValue,
|
|
||||||
UniqueType = request.UniqueType,
|
|
||||||
UniqueValue = code,
|
|
||||||
Type = request.Type,
|
|
||||||
TenantId = currentTenantId
|
|
||||||
}).ToList();
|
|
||||||
|
|
||||||
await _dbContext.InsertAsync(devicePermissions);
|
|
||||||
}
|
|
||||||
|
|
||||||
var deviceGroups = await _dbContext.Queryable<DeviceGroupEntity>().ToListAsync();
|
|
||||||
|
|
||||||
var addDevices = await GetDevicesByGroupsAsync(deviceGroups, deviceGroups.Where(x => addDeviceGroupCodes.Contains(x.Code)).ToList());
|
|
||||||
var deleteDevices = await GetDevicesByGroupsAsync(deviceGroups, deviceGroups.Where(x => deleteDeviceGroupCodes.Contains(x.Code)).ToList());
|
|
||||||
|
|
||||||
var tasks = new List<DeviceTaskEntity>();
|
|
||||||
foreach (var gainfoNo in gainfoNos)
|
|
||||||
{
|
|
||||||
tasks.AddRange(addDevices.Select(x => new DeviceTaskEntity
|
|
||||||
{
|
|
||||||
Type = DeviceTaskEnumType.Add,
|
|
||||||
DeviceType = x.Type,
|
|
||||||
UserId = null,
|
|
||||||
UserNo = gainfoNo,
|
|
||||||
DeviceUniqueNo = x.UniqueNo,
|
|
||||||
Status = DeviceTaskStatusEnumType.Wait,
|
|
||||||
Version = 0,
|
|
||||||
ResultMessage = null,
|
|
||||||
CancellationId = 0,
|
|
||||||
Source = DeviceTaskSourceEnumType.Manually,
|
|
||||||
Priority = 0,
|
|
||||||
TenantId = x.TenantId,
|
|
||||||
}).ToList());
|
|
||||||
|
|
||||||
tasks.AddRange(deleteDevices.Select(x => new DeviceTaskEntity
|
|
||||||
{
|
|
||||||
Type = DeviceTaskEnumType.Delete,
|
|
||||||
DeviceType = x.Type,
|
|
||||||
UserId = null,
|
|
||||||
UserNo = gainfoNo,
|
|
||||||
DeviceUniqueNo = x.UniqueNo,
|
|
||||||
Status = DeviceTaskStatusEnumType.Wait,
|
|
||||||
Version = 0,
|
|
||||||
ResultMessage = null,
|
|
||||||
CancellationId = 0,
|
|
||||||
Source = DeviceTaskSourceEnumType.Manually,
|
|
||||||
Priority = 0,
|
|
||||||
TenantId = x.TenantId,
|
|
||||||
}).ToList());
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var items in tasks.Where(x => x.Type == DeviceTaskEnumType.Add).Chunk(500))
|
|
||||||
{
|
|
||||||
await _dbContext.InsertAsync(items.ToList());
|
|
||||||
}
|
|
||||||
|
|
||||||
return new DevicePermissionAssignResponse
|
|
||||||
{
|
|
||||||
AddCount = addDevices.Count * gainfoNos.Count,
|
|
||||||
DeleteCount = deleteDevices.Count * gainfoNos.Count
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void RecursionGetNextDeviceGroup(List<DeviceGroupEntity> allData, DeviceGroupEntity current, List<Guid> result)
|
|
||||||
{
|
|
||||||
var nextItems = allData.Where(x => x.PCode == current.Code).ToList();
|
|
||||||
foreach (var item in nextItems)
|
|
||||||
{
|
|
||||||
result.Add(item.Code);
|
|
||||||
RecursionGetNextDeviceGroup(allData, item, result);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task<List<DeviceEntity>> GetDevicesByGroupsAsync(List<DeviceGroupEntity> allData, List<DeviceGroupEntity> groups)
|
|
||||||
{
|
|
||||||
var result = new List<Guid>();
|
|
||||||
foreach (var group in groups)
|
|
||||||
{
|
|
||||||
result.Add(group.Code);
|
|
||||||
RecursionGetNextDeviceGroup(allData, group, result);
|
|
||||||
}
|
|
||||||
|
|
||||||
var deviceCodes = await _dbContext.Queryable<DeviceGroupRelationshipEntity>()
|
|
||||||
.Where(x => result.Contains(x.GroupCode))
|
|
||||||
.Select(x => x.DeviceCode)
|
|
||||||
.ToListAsync();
|
|
||||||
|
|
||||||
return await _dbContext.Queryable<DeviceEntity>()
|
|
||||||
.Where(x => deviceCodes.Contains(x.Code))
|
|
||||||
.ToListAsync();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task AssignToUniqueAsync(DevicePermissionsAssignToDeviceRequest request)
|
public async Task AssignToUniqueAsync(DevicePermissionsAssignToDeviceRequest request)
|
||||||
@@ -475,10 +223,19 @@ public class DevicePermissionService : IDevicePermissionService
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public async Task<Dictionary<string, List<UserTenantDeviceCodeDto>>> GetUserDevicePermissionsAsync(string code)
|
public async Task<Dictionary<string, IEnumerable<UserTenantDeviceCodeDto>>> GetUserDevicePermissionsAsync(string code)
|
||||||
{
|
{
|
||||||
var userNo = await _dbContext.Queryable<GainfoEntity>().Where(x => x.Code == code).Select(x => x.No).SingleAsync();
|
var userNo = await _dbContext.Queryable<GainfoEntity>().Where(x => x.Code == code).Select(x => x.No).SingleAsync();
|
||||||
return await _devicePermissionAnalyzeService.GetAssignDeviceCodesInternalAsync(new List<string> { userNo });
|
var result = await _devicePermissionAnalyzeService.GetUserAssignDeviceCodesWithTypeInTenantAsync(new List<string> { userNo });
|
||||||
|
|
||||||
|
return result.ToDictionary(x => x.Key, x => x.Value.GroupBy(p => p.Tenant).Select(item =>
|
||||||
|
{
|
||||||
|
return new UserTenantDeviceCodeDto
|
||||||
|
{
|
||||||
|
Tenant = item.Key,
|
||||||
|
Codes = item.SelectMany(p => p.AssignTypes).SelectMany(p => p.Codes).Distinct().ToList(),
|
||||||
|
};
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -701,7 +458,7 @@ public class DevicePermissionService : IDevicePermissionService
|
|||||||
public async Task<List<Guid>> GetAssignDeviceCodesAsync(string userCode)
|
public async Task<List<Guid>> GetAssignDeviceCodesAsync(string userCode)
|
||||||
{
|
{
|
||||||
var userNo = await _dbContext.Queryable<GainfoEntity>().Where(x => x.Code == userCode || x.No == userCode).Select(x => x.No).SingleAsync();
|
var userNo = await _dbContext.Queryable<GainfoEntity>().Where(x => x.Code == userCode || x.No == userCode).Select(x => x.No).SingleAsync();
|
||||||
var result = await _devicePermissionAnalyzeService.GetAssignDeviceCodesInternalAsync(new List<string> { userNo });
|
var result = await _devicePermissionAnalyzeService.GetUserAssignDeviceCodesWithTypeInTenantAsync(new List<string> { userNo });
|
||||||
return result.First().Value.SelectMany(x => x.Codes).ToList();
|
return result.First().Value.SelectMany(x => x.AssignTypes.SelectMany(p => p.Codes)).Distinct().ToList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
namespace Dlhis.Basic.Device.Services.Implements;
|
|
||||||
|
|
||||||
public class ParallelService : IParallelService
|
|
||||||
{
|
|
||||||
/// <inheritdoc />
|
|
||||||
public bool Enable { get; set; }
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
using System.Security.Claims;
|
using System.Security.Claims;
|
||||||
using Kurisu.AspNetCore;
|
using Kurisu.AspNetCore;
|
||||||
using Kurisu.AspNetCore.Authentication.Abstractions;
|
using Kurisu.AspNetCore.Authentication.Abstractions;
|
||||||
using Kurisu.AspNetCore.Utils;
|
using Kurisu.AspNetCore.Cache;
|
||||||
using Kurisu.AspNetCore.Utils.Converters;
|
using Kurisu.AspNetCore.Utils.Converters;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
namespace Dlhis.Basic.Organization.Dto;
|
using Dlhis.Entity.Sys.Enums;
|
||||||
|
|
||||||
|
namespace Dlhis.Basic.Organization.Dto;
|
||||||
|
|
||||||
public class GainfoDto
|
public class GainfoDto
|
||||||
{
|
{
|
||||||
@@ -15,4 +17,17 @@ public class GainfoDto
|
|||||||
|
|
||||||
public bool IsOutsource { get; set; }
|
public bool IsOutsource { get; set; }
|
||||||
public bool EnableFactoryPermission { get; set; }
|
public bool EnableFactoryPermission { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class AnalyzePackDto
|
||||||
|
{
|
||||||
|
public string UserNo { get; set; }
|
||||||
|
public string UserDepartment { get; set; }
|
||||||
|
public string UserFactory { get; set; }
|
||||||
|
|
||||||
|
public IEnumerable<Guid> UserGainfoGroupCodes { get; set; }
|
||||||
|
|
||||||
|
public IEnumerable<Guid> DeviceCodes { get; set; }
|
||||||
|
|
||||||
|
public IEnumerable<AssignTypeEnumType> IgnoreAssignTypeTypes { get; set; }
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,11 @@
|
|||||||
using Dlhis.Basic.Device.Services;
|
using Dlhis.Basic.Device;
|
||||||
|
using Dlhis.Basic.Device.Request;
|
||||||
|
using Dlhis.Basic.Device.Services;
|
||||||
using Dlhis.Basic.Organization.Request;
|
using Dlhis.Basic.Organization.Request;
|
||||||
using Dlhis.Basic.Organization.Response;
|
using Dlhis.Basic.Organization.Response;
|
||||||
using Dlhis.Entity.Basic.Entity;
|
using Dlhis.Entity.Basic.Entity;
|
||||||
|
using Dlhis.Entity.Sys.Entities;
|
||||||
|
using Dlhis.Entity.Sys.Enums;
|
||||||
using Kurisu.AspNetCore.Authentication.Abstractions;
|
using Kurisu.AspNetCore.Authentication.Abstractions;
|
||||||
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
||||||
using Kurisu.AspNetCore.Utils.Extensions;
|
using Kurisu.AspNetCore.Utils.Extensions;
|
||||||
@@ -13,13 +17,13 @@ public class GainfoGroupService : IGainfoGroupService
|
|||||||
{
|
{
|
||||||
private readonly IDbContext _dbContext;
|
private readonly IDbContext _dbContext;
|
||||||
private readonly ICurrentUser _currentUser;
|
private readonly ICurrentUser _currentUser;
|
||||||
private readonly IDevicePermissionAnalyzeService _devicePermissionAnalyzeService;
|
private readonly INamedResolver _namedResolver;
|
||||||
|
|
||||||
public GainfoGroupService(IDbContext dbContext, ICurrentUser currentUser, IDevicePermissionAnalyzeService devicePermissionAnalyzeService)
|
public GainfoGroupService(IDbContext dbContext, ICurrentUser currentUser, INamedResolver namedResolver)
|
||||||
{
|
{
|
||||||
_dbContext = dbContext;
|
_dbContext = dbContext;
|
||||||
_currentUser = currentUser;
|
_currentUser = currentUser;
|
||||||
_devicePermissionAnalyzeService = devicePermissionAnalyzeService;
|
_namedResolver = namedResolver;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<List<GainfoGroupTreeResponse>> GetTreeAsync()
|
public async Task<List<GainfoGroupTreeResponse>> GetTreeAsync()
|
||||||
@@ -82,28 +86,60 @@ public class GainfoGroupService : IGainfoGroupService
|
|||||||
var existsNextGroup = await _dbContext.Queryable<GainfoGroupEntity>().Where(x => x.PCode == request.GroupCode).AnyAsync();
|
var existsNextGroup = await _dbContext.Queryable<GainfoGroupEntity>().Where(x => x.PCode == request.GroupCode).AnyAsync();
|
||||||
existsNextGroup.ThrowIfTrue("存在下级分组,无法分配人员");
|
existsNextGroup.ThrowIfTrue("存在下级分组,无法分配人员");
|
||||||
|
|
||||||
await _devicePermissionAnalyzeService.GenerateDeviceTasksAsync(async () =>
|
var relations = await _dbContext.Queryable<GainfoGroupRelationshipEntity>()
|
||||||
|
.Where(x => x.GroupCode == request.GroupCode)
|
||||||
|
.ToListAsync();
|
||||||
|
await _dbContext.DeleteAsync(relations);
|
||||||
|
|
||||||
|
var userNos = await _dbContext.Queryable<GainfoEntity>()
|
||||||
|
.Where(x => request.UserNos.Contains(x.No) || request.UserNos.Contains(x.Code)) //NO和工号匹配
|
||||||
|
.Select(x => x.No)
|
||||||
|
.ToListAsync();
|
||||||
|
|
||||||
|
//(userNos.Count != request.UserNos.Count).ThrowIfTrue("存在权限外人员,无法添加");
|
||||||
|
|
||||||
|
var entities = userNos.Select(x => new GainfoGroupRelationshipEntity
|
||||||
{
|
{
|
||||||
var relations = await _dbContext.Queryable<GainfoGroupRelationshipEntity>()
|
GroupCode = request.GroupCode,
|
||||||
.Where(x => x.GroupCode == request.GroupCode)
|
UserNo = x
|
||||||
.ToListAsync();
|
}).ToList();
|
||||||
await _dbContext.DeleteAsync(relations);
|
|
||||||
|
|
||||||
var userNos = await _dbContext.Queryable<GainfoEntity>()
|
await _dbContext.InsertAsync(entities);
|
||||||
.Where(x => request.UserNos.Contains(x.No) || request.UserNos.Contains(x.Code)) //NO和工号匹配
|
|
||||||
.Select(x => x.No)
|
|
||||||
.ToListAsync();
|
|
||||||
|
|
||||||
//(userNos.Count != request.UserNos.Count).ThrowIfTrue("存在权限外人员,无法添加");
|
await PermissionResetAsync(request.GroupCode);
|
||||||
|
}
|
||||||
|
|
||||||
var entities = userNos.Select(x => new GainfoGroupRelationshipEntity
|
|
||||||
|
private async Task PermissionResetAsync(Guid groupCode)
|
||||||
|
{
|
||||||
|
var gainfoGroups = RecursionHelper.HandlerGainfoGroupPreviousCodes(groupCode, await _dbContext.Queryable<GainfoGroupEntity>().ToListAsync()).Select(x => x.ToString());
|
||||||
|
|
||||||
|
var dps = await _dbContext.Queryable<DevicePermissionEntity>()
|
||||||
|
.Where(x => x.AssignType == AssignTypeEnumType.Group)
|
||||||
|
.Where(x => gainfoGroups.Contains(x.AssignValue))
|
||||||
|
.ToListAsync();
|
||||||
|
|
||||||
|
var analyzeService = _namedResolver.GetService<IPermissionAnalyzeService>(PermissionAnalyzeConstants.GetTypeName(AssignTypeEnumType.Group));
|
||||||
|
|
||||||
|
foreach (var devicePermissionGroup in dps.GroupBy(x => new { x.AssignValue, x.TenantId, x.UniqueType, x.Type }))
|
||||||
|
{
|
||||||
|
var assignValue = devicePermissionGroup.Key.AssignValue;
|
||||||
|
var tenantId = devicePermissionGroup.Key.TenantId;
|
||||||
|
var uniqueType = devicePermissionGroup.Key.UniqueType;
|
||||||
|
var type = devicePermissionGroup.Key.Type;
|
||||||
|
|
||||||
|
var codes = devicePermissionGroup.Select(x => x.UniqueValue).Distinct().ToList();
|
||||||
|
|
||||||
|
await analyzeService.AnalyzeAsync(new DevicePermissionSaveRequest
|
||||||
{
|
{
|
||||||
GroupCode = request.GroupCode,
|
AssignType = AssignTypeEnumType.Group,
|
||||||
UserNo = x
|
AssignValue = assignValue,
|
||||||
}).ToList();
|
UniqueType = uniqueType,
|
||||||
|
Codes = codes,
|
||||||
await _dbContext.InsertAsync(entities);
|
TenantId = tenantId,
|
||||||
}, null);
|
Type = type
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ using Dlhis.Basic.Organization.Response;
|
|||||||
using Dlhis.Entity.Basic.Entity;
|
using Dlhis.Entity.Basic.Entity;
|
||||||
using Dlhis.Entity.Basic.Enums;
|
using Dlhis.Entity.Basic.Enums;
|
||||||
using Dlhis.Entity.Device.Entities;
|
using Dlhis.Entity.Device.Entities;
|
||||||
|
using Kurisu.AspNetCore.Cache;
|
||||||
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
||||||
using Kurisu.AspNetCore.Utils;
|
|
||||||
using Kurisu.AspNetCore.Utils.Extensions;
|
using Kurisu.AspNetCore.Utils.Extensions;
|
||||||
using Mapster;
|
using Mapster;
|
||||||
using SqlSugar;
|
using SqlSugar;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
|
using Kurisu.AspNetCore.Cache.Extensions;
|
||||||
using Kurisu.AspNetCore.Startup;
|
using Kurisu.AspNetCore.Startup;
|
||||||
using Kurisu.AspNetCore.Utils.Extensions;
|
|
||||||
using Microsoft.IdentityModel.Logging;
|
using Microsoft.IdentityModel.Logging;
|
||||||
|
|
||||||
class Program
|
class Program
|
||||||
|
|||||||
@@ -106,4 +106,25 @@ public static class RecursionHelper
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static List<Guid> HandlerDeviceGroupPreviousCodes(Guid current, List<DeviceGroupEntity> data)
|
||||||
|
{
|
||||||
|
var result = new List<Guid> { current };
|
||||||
|
|
||||||
|
InternalHandlerCodes(current, data, result);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
|
||||||
|
static void InternalHandlerCodes(Guid internalCurrent, List<DeviceGroupEntity> internalData, List<Guid> internalResult)
|
||||||
|
{
|
||||||
|
var pno = internalData.FirstOrDefault(x => x.Code == internalCurrent)?.PCode;
|
||||||
|
if (!pno.HasValue)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
internalResult.Add(pno.Value);
|
||||||
|
InternalHandlerCodes(pno.Value, internalData, internalResult);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -12,6 +12,7 @@ using Dlhis.Entity.Device.Enums;
|
|||||||
using Dlhis.Sys.Api;
|
using Dlhis.Sys.Api;
|
||||||
using Kurisu.AspNetCore;
|
using Kurisu.AspNetCore;
|
||||||
using Kurisu.AspNetCore.Authentication.Abstractions;
|
using Kurisu.AspNetCore.Authentication.Abstractions;
|
||||||
|
using Kurisu.AspNetCore.Cache;
|
||||||
using Kurisu.AspNetCore.CustomClass;
|
using Kurisu.AspNetCore.CustomClass;
|
||||||
using Kurisu.AspNetCore.DataAccess.Dto;
|
using Kurisu.AspNetCore.DataAccess.Dto;
|
||||||
using Kurisu.AspNetCore.DataAccess.SqlSugar.Extensions;
|
using Kurisu.AspNetCore.DataAccess.SqlSugar.Extensions;
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ using Dlhis.Canteen.Order.Request;
|
|||||||
using Dlhis.Entity.Device;
|
using Dlhis.Entity.Device;
|
||||||
using Dlhis.Entity.Device.Enums;
|
using Dlhis.Entity.Device.Enums;
|
||||||
using Kurisu.AspNetCore.Authentication.Abstractions;
|
using Kurisu.AspNetCore.Authentication.Abstractions;
|
||||||
|
using Kurisu.AspNetCore.Cache;
|
||||||
using Kurisu.AspNetCore.CustomClass;
|
using Kurisu.AspNetCore.CustomClass;
|
||||||
using Kurisu.AspNetCore.DataAccess.Dto;
|
using Kurisu.AspNetCore.DataAccess.Dto;
|
||||||
using Kurisu.AspNetCore.DataAccess.SqlSugar.Extensions;
|
using Kurisu.AspNetCore.DataAccess.SqlSugar.Extensions;
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
using Kurisu.AspNetCore.Cache.Extensions;
|
||||||
using Kurisu.AspNetCore.Startup;
|
using Kurisu.AspNetCore.Startup;
|
||||||
using Kurisu.AspNetCore.Utils.Extensions;
|
using Kurisu.AspNetCore.Utils.Extensions;
|
||||||
using Microsoft.IdentityModel.Logging;
|
using Microsoft.IdentityModel.Logging;
|
||||||
|
|||||||
@@ -6,12 +6,12 @@
|
|||||||
<Nullable>disable</Nullable>
|
<Nullable>disable</Nullable>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<PackageReference Include="Kurisu.AspNetCore" Version="0.9.25" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\Dlhis.DataPermission.Model\Dlhis.DataPermission.Model.csproj" />
|
<ProjectReference Include="..\Dlhis.DataPermission.Model\Dlhis.DataPermission.Model.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Kurisu.AspNetCore" Version="0.9.35" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ public class SqlSugarPack : BaseAppPack
|
|||||||
{
|
{
|
||||||
public override void ConfigureServices(IServiceCollection services)
|
public override void ConfigureServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
services.AddSqlSugar(sp =>
|
services.AddSqlSugar(DbType.MySqlConnector, sp =>
|
||||||
{
|
{
|
||||||
var configuration = sp.GetService<IConfiguration>();
|
var configuration = sp.GetService<IConfiguration>();
|
||||||
|
|
||||||
@@ -80,7 +80,7 @@ public class SqlSugarPack : BaseAppPack
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
public override void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||||
{
|
{
|
||||||
var isGenerate = Configuration.GetSection("SqlSugarOptions").GetValue<bool>("Generate");
|
var isGenerate = Configuration.GetSection("SqlSugarOptions").GetValue<bool>("Generate");
|
||||||
|
|||||||
@@ -3,9 +3,9 @@ using Dlhis.Entity.Basic.Enums;
|
|||||||
using Dlhis.Entity.Visitor.Entity;
|
using Dlhis.Entity.Visitor.Entity;
|
||||||
using Dlhis.Entity.Visitor.Enums;
|
using Dlhis.Entity.Visitor.Enums;
|
||||||
using Dlhis.External.Appointment.Request;
|
using Dlhis.External.Appointment.Request;
|
||||||
|
using Kurisu.AspNetCore.Cache;
|
||||||
using Kurisu.AspNetCore.CustomClass;
|
using Kurisu.AspNetCore.CustomClass;
|
||||||
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
||||||
using Kurisu.AspNetCore.Utils;
|
|
||||||
using Kurisu.AspNetCore.Utils.Extensions;
|
using Kurisu.AspNetCore.Utils.Extensions;
|
||||||
using SqlSugar;
|
using SqlSugar;
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
using Kurisu.AspNetCore.DataAccess.SqlSugar.Extensions;
|
using Kurisu.AspNetCore.Cache.Extensions;
|
||||||
using Kurisu.AspNetCore.Startup;
|
using Kurisu.AspNetCore.Startup;
|
||||||
using Kurisu.AspNetCore.Utils.Extensions;
|
|
||||||
using Microsoft.IdentityModel.Logging;
|
using Microsoft.IdentityModel.Logging;
|
||||||
|
|
||||||
class Program
|
class Program
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ public class OrderService : IOrderService
|
|||||||
|
|
||||||
var message = "离线消费:";
|
var message = "离线消费:";
|
||||||
|
|
||||||
gainfo.ThrowIfNull("无消费权限");
|
gainfo.ThrowIfNull(message + "无消费权限");
|
||||||
|
|
||||||
var isError = false;
|
var isError = false;
|
||||||
try
|
try
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using Dlhis.ScheduledTask.Attributes;
|
using Dlhis.ScheduledTask.Attributes;
|
||||||
|
using Kurisu.AspNetCore.Cache;
|
||||||
using Kurisu.AspNetCore.DataAccess.Entity;
|
using Kurisu.AspNetCore.DataAccess.Entity;
|
||||||
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
||||||
using Kurisu.AspNetCore.Utils;
|
|
||||||
using Kurisu.Scheduled.Abstractions;
|
using Kurisu.Scheduled.Abstractions;
|
||||||
|
|
||||||
namespace Dlhis.ScheduledTask;
|
namespace Dlhis.ScheduledTask;
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
using Dlhis.Jip.Api;
|
using Kurisu.AspNetCore.Cache.Extensions;
|
||||||
using Dlhis.Jip.Api.Request;
|
|
||||||
using Kurisu.AspNetCore.Startup;
|
using Kurisu.AspNetCore.Startup;
|
||||||
using Kurisu.AspNetCore.Utils.Extensions;
|
|
||||||
|
|
||||||
class Program
|
class Program
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ using Dlhis.Jip.Api;
|
|||||||
using Dlhis.Jip.Api.Request;
|
using Dlhis.Jip.Api.Request;
|
||||||
using Dlhis.Jip.Api.Requests;
|
using Dlhis.Jip.Api.Requests;
|
||||||
using Hangfire;
|
using Hangfire;
|
||||||
using Kurisu.AspNetCore.Utils;
|
using Kurisu.AspNetCore.Cache;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
|||||||
@@ -2,11 +2,11 @@
|
|||||||
using Dlhis.Sys.Identity.Request;
|
using Dlhis.Sys.Identity.Request;
|
||||||
using Dlhis.Sys.Identity.Response;
|
using Dlhis.Sys.Identity.Response;
|
||||||
using Kurisu.AspNetCore.Authentication.Abstractions;
|
using Kurisu.AspNetCore.Authentication.Abstractions;
|
||||||
|
using Kurisu.AspNetCore.Cache;
|
||||||
using Kurisu.AspNetCore.CustomClass;
|
using Kurisu.AspNetCore.CustomClass;
|
||||||
using Kurisu.AspNetCore.DataAccess.Dto;
|
using Kurisu.AspNetCore.DataAccess.Dto;
|
||||||
using Kurisu.AspNetCore.DataAccess.SqlSugar.Extensions;
|
using Kurisu.AspNetCore.DataAccess.SqlSugar.Extensions;
|
||||||
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
||||||
using Kurisu.AspNetCore.Utils;
|
|
||||||
using Kurisu.AspNetCore.Utils.Extensions;
|
using Kurisu.AspNetCore.Utils.Extensions;
|
||||||
|
|
||||||
namespace Dlhis.Sys.Identity.Services.Implements;
|
namespace Dlhis.Sys.Identity.Services.Implements;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
|
using Kurisu.AspNetCore.Cache.Extensions;
|
||||||
using Kurisu.AspNetCore.Startup;
|
using Kurisu.AspNetCore.Startup;
|
||||||
using Kurisu.AspNetCore.Utils.Extensions;
|
|
||||||
using Microsoft.IdentityModel.Logging;
|
using Microsoft.IdentityModel.Logging;
|
||||||
|
|
||||||
namespace Dlhis.Sys;
|
namespace Dlhis.Sys;
|
||||||
|
|||||||
Reference in New Issue
Block a user