fix 逻辑调整
This commit is contained in:
@@ -34,8 +34,7 @@ public class DeviceGroupsController : ControllerBase
|
|||||||
[DataPermission]
|
[DataPermission]
|
||||||
public async Task<List<DeviceGroupTreeResponse>> GetTreeAsync(
|
public async Task<List<DeviceGroupTreeResponse>> GetTreeAsync(
|
||||||
[EnumDataType(typeof(DeviceTypeEnumType), ErrorMessage = "请选择正确的类型")]
|
[EnumDataType(typeof(DeviceTypeEnumType), ErrorMessage = "请选择正确的类型")]
|
||||||
DeviceTypeEnumType type
|
DeviceTypeEnumType type)
|
||||||
)
|
|
||||||
{
|
{
|
||||||
return await _deviceGroupService.GetTreeAsync(type);
|
return await _deviceGroupService.GetTreeAsync(type);
|
||||||
}
|
}
|
||||||
|
|||||||
35
Src/Dlhis.Basic/Device/Dtos/DevicePermissionDto.cs
Normal file
35
Src/Dlhis.Basic/Device/Dtos/DevicePermissionDto.cs
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
using Dlhis.Entity.Device.Enums;
|
||||||
|
using Dlhis.Entity.Sys.Enums;
|
||||||
|
|
||||||
|
namespace Dlhis.Basic.Device.Dtos;
|
||||||
|
|
||||||
|
public class DevicePermissionDto
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 分配权限类型(User,Role,Group...)
|
||||||
|
/// </summary>
|
||||||
|
public AssignTypeEnumType AssignType { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 分配值
|
||||||
|
/// </summary>
|
||||||
|
public string AssignValue { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 设备权限类型(Device/DeviceGroup)
|
||||||
|
/// </summary>
|
||||||
|
public DeviceAssignmentTypeEnumType UniqueType { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 类型值
|
||||||
|
/// </summary>
|
||||||
|
public Guid UniqueValue { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 设备数据类型
|
||||||
|
/// </summary>
|
||||||
|
public DeviceTypeEnumType Type { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
public string TenantId { get; set; }
|
||||||
|
}
|
||||||
@@ -26,9 +26,19 @@ public class AssignTypeDeviceCode
|
|||||||
public AssignTypeEnumType AssignType { get; init; }
|
public AssignTypeEnumType AssignType { get; init; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 设备权限
|
/// 分配值
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IEnumerable<Guid> Codes { get; init; }
|
public string AssignValue { get; init; }
|
||||||
|
|
||||||
|
public DeviceAssignmentTypeEnumType UniqueType { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 权限
|
||||||
|
/// </summary>
|
||||||
|
public IEnumerable<Guid> Codes { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
public IEnumerable<Guid> OriginalCodes { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class UserTenantDeviceCodeDto
|
public class UserTenantDeviceCodeDto
|
||||||
|
|||||||
@@ -13,10 +13,14 @@ namespace Dlhis.Basic.Device.Services;
|
|||||||
public interface IDevicePermissionAnalyzeService : IScopeDependency
|
public interface IDevicePermissionAnalyzeService : IScopeDependency
|
||||||
{
|
{
|
||||||
Task<List<DevicePermissionEntity>> GetDevicePermissions(DevicePermissionSaveRequest request);
|
Task<List<DevicePermissionEntity>> GetDevicePermissions(DevicePermissionSaveRequest request);
|
||||||
|
|
||||||
Task<DevicePermissionAssignResponse> GenerateDeviceTasksAsync(Func<Task> func, List<string> userNos);
|
Task<DevicePermissionAssignResponse> GenerateDeviceTasksAsync(Func<Task> func, List<string> userNos);
|
||||||
|
|
||||||
Task<Dictionary<string, IEnumerable<TenantAssignTypeDeviceCode>>> GetUserAssignDeviceCodesWithTypeInTenantAsync(List<string> userNos, params AssignTypeEnumType[] ignoreAssignTypes);
|
Task<Dictionary<string, IEnumerable<TenantAssignTypeDeviceCode>>> GetUserAssignDeviceCodesWithTypeInTenantAsync(
|
||||||
|
List<string> userNos,
|
||||||
|
string ignoreAssignValue = null,
|
||||||
|
params AssignTypeEnumType[] ignoreAssignTypes
|
||||||
|
);
|
||||||
|
|
||||||
Task<List<Guid>> GetAssignmentsAsync(DeviceGetAssignmentsRequest request);
|
Task<List<Guid>> GetAssignmentsAsync(DeviceGetAssignmentsRequest request);
|
||||||
|
|
||||||
|
|||||||
@@ -51,14 +51,19 @@ public abstract class BasePermissionAnalyzeService : IPermissionAnalyzeService
|
|||||||
|
|
||||||
var gainfoNos = await GetGainfoNosAsync(request.GetActualAssignValue());
|
var gainfoNos = await GetGainfoNosAsync(request.GetActualAssignValue());
|
||||||
|
|
||||||
var userTenantDeviceCodes =
|
var userTenantDeviceCodes = devicesToDelete.IsPresent() && request.AssignType != AssignTypeEnumType.Tenant
|
||||||
devicesToDelete.IsPresent() && request.AssignType != AssignTypeEnumType.Tenant
|
? await _devicePermissionAnalyzeService.GetUserAssignDeviceCodesWithTypeInTenantAsync(gainfoNos, request.GetActualAssignValue(), request.AssignType)
|
||||||
? await _devicePermissionAnalyzeService.GetUserAssignDeviceCodesWithTypeInTenantAsync(gainfoNos, request.AssignType)
|
: new Dictionary<string, IEnumerable<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)
|
||||||
{
|
{
|
||||||
|
if (userTenantDeviceCodes.Any())
|
||||||
|
{
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
tasks.AddRange(devicesToAdd.Select(x => new DeviceTaskEntity
|
tasks.AddRange(devicesToAdd.Select(x => new DeviceTaskEntity
|
||||||
{
|
{
|
||||||
Type = DeviceTaskEnumType.Add,
|
Type = DeviceTaskEnumType.Add,
|
||||||
@@ -75,12 +80,6 @@ public abstract class BasePermissionAnalyzeService : IPermissionAnalyzeService
|
|||||||
TenantId = x.TenantId,
|
TenantId = x.TenantId,
|
||||||
}).ToList());
|
}).ToList());
|
||||||
|
|
||||||
if (userTenantDeviceCodes.Any())
|
|
||||||
{
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
tasks.AddRange(devicesToDelete.Select(x => new DeviceTaskEntity
|
tasks.AddRange(devicesToDelete.Select(x => new DeviceTaskEntity
|
||||||
{
|
{
|
||||||
Type = DeviceTaskEnumType.Delete,
|
Type = DeviceTaskEnumType.Delete,
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
using Dlhis.Basic.DataPermissions;
|
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;
|
||||||
|
using Dlhis.Entity.Basic.Enums;
|
||||||
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.Entities;
|
||||||
@@ -18,20 +19,17 @@ public class DeviceGroupService : IDeviceGroupService
|
|||||||
{
|
{
|
||||||
private readonly IDbContext _dbContext;
|
private readonly IDbContext _dbContext;
|
||||||
private readonly ICurrentUser _currentUser;
|
private readonly ICurrentUser _currentUser;
|
||||||
private readonly INamedResolver _namedResolver;
|
private readonly IDevicePermissionAnalyzeService _devicePermissionAnalyzeService;
|
||||||
private readonly IMyDataPermissionService _myDataPermissionService;
|
|
||||||
private readonly ITenantApi _tenantApi;
|
private readonly ITenantApi _tenantApi;
|
||||||
|
|
||||||
public DeviceGroupService(IDbContext dbContext,
|
public DeviceGroupService(IDbContext dbContext,
|
||||||
ICurrentUser currentUser,
|
ICurrentUser currentUser,
|
||||||
INamedResolver namedResolver,
|
IDevicePermissionAnalyzeService devicePermissionAnalyzeService,
|
||||||
IMyDataPermissionService myDataPermissionService,
|
|
||||||
ITenantApi tenantApi)
|
ITenantApi tenantApi)
|
||||||
{
|
{
|
||||||
_dbContext = dbContext;
|
_dbContext = dbContext;
|
||||||
_currentUser = currentUser;
|
_currentUser = currentUser;
|
||||||
_namedResolver = namedResolver;
|
_devicePermissionAnalyzeService = devicePermissionAnalyzeService;
|
||||||
_myDataPermissionService = myDataPermissionService;
|
|
||||||
_tenantApi = tenantApi;
|
_tenantApi = tenantApi;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,8 +108,10 @@ public class DeviceGroupService : IDeviceGroupService
|
|||||||
await CheckGroupTypeMatchAsync(request, group);
|
await CheckGroupTypeMatchAsync(request, group);
|
||||||
|
|
||||||
var relationships = await _dbContext.Queryable<DeviceGroupRelationshipEntity>().Where(x => x.GroupCode == request.GroupCode).ToListAsync();
|
var relationships = await _dbContext.Queryable<DeviceGroupRelationshipEntity>().Where(x => x.GroupCode == request.GroupCode).ToListAsync();
|
||||||
await _dbContext.DeleteAsync(relationships);
|
|
||||||
|
|
||||||
|
var r = await PermissionResetAsync(request.GroupCode, relationships.Select(x => x.DeviceCode).Distinct().ToList(), request.DeviceCodes);
|
||||||
|
|
||||||
|
await _dbContext.DeleteAsync(relationships);
|
||||||
var entities = request.DeviceCodes.Select(x => new DeviceGroupRelationshipEntity
|
var entities = request.DeviceCodes.Select(x => new DeviceGroupRelationshipEntity
|
||||||
{
|
{
|
||||||
GroupCode = request.GroupCode,
|
GroupCode = request.GroupCode,
|
||||||
@@ -122,11 +122,18 @@ public class DeviceGroupService : IDeviceGroupService
|
|||||||
|
|
||||||
await _dbContext.InsertAsync(entities);
|
await _dbContext.InsertAsync(entities);
|
||||||
|
|
||||||
return await PermissionResetAsync(request.GroupCode);
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<DevicePermissionAssignResponse> PermissionResetAsync(Guid groupCode)
|
private async Task<DevicePermissionAssignResponse> PermissionResetAsync(Guid groupCode, List<Guid> beforeDeviceCodes, List<Guid> presentDeviceCodes)
|
||||||
{
|
{
|
||||||
|
var codesToAdd = presentDeviceCodes.Except(beforeDeviceCodes).ToList();
|
||||||
|
var codesToDelete = beforeDeviceCodes.Except(presentDeviceCodes).ToList();
|
||||||
|
if (codesToAdd.IsEmpty() && codesToDelete.IsEmpty())
|
||||||
|
{
|
||||||
|
return new DevicePermissionAssignResponse(0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
var deviceGroups = await _dbContext.Queryable<DeviceGroupEntity>().ToListAsync();
|
var deviceGroups = await _dbContext.Queryable<DeviceGroupEntity>().ToListAsync();
|
||||||
var deviceGroupCodes = RecursionHelper.HandlerDeviceGroupPreviousCodes(groupCode, deviceGroups);
|
var deviceGroupCodes = RecursionHelper.HandlerDeviceGroupPreviousCodes(groupCode, deviceGroups);
|
||||||
|
|
||||||
@@ -135,38 +142,151 @@ public class DeviceGroupService : IDeviceGroupService
|
|||||||
.Where(x => deviceGroupCodes.Contains(x.UniqueValue))
|
.Where(x => deviceGroupCodes.Contains(x.UniqueValue))
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
|
|
||||||
var result = new List<DevicePermissionAssignResponse>();
|
if (!dps.Any())
|
||||||
|
|
||||||
if (dps.Any())
|
|
||||||
{
|
{
|
||||||
var type = dps.First().Type;
|
return new DevicePermissionAssignResponse(0, 0);
|
||||||
|
|
||||||
foreach (var devicePermissionGroup in dps.GroupBy(x => new { x.AssignType, x.AssignValue, x.TenantId }))
|
|
||||||
{
|
|
||||||
var assignType = devicePermissionGroup.Key.AssignType;
|
|
||||||
var assignValue = devicePermissionGroup.Key.AssignValue;
|
|
||||||
var tenantId = devicePermissionGroup.Key.TenantId;
|
|
||||||
var analyzeService = _namedResolver.GetService<IPermissionAnalyzeService>(PermissionAnalyzeConstants.GetTypeName(assignType));
|
|
||||||
|
|
||||||
var codes = devicePermissionGroup.Select(x => x.UniqueValue).Distinct().ToList();
|
|
||||||
|
|
||||||
var r = await analyzeService.AnalyzeAsync(new DevicePermissionSaveRequest
|
|
||||||
{
|
|
||||||
AssignType = assignType,
|
|
||||||
AssignValue = assignValue,
|
|
||||||
UniqueType = DeviceAssignmentTypeEnumType.Group,
|
|
||||||
Codes = codes,
|
|
||||||
TenantId = tenantId,
|
|
||||||
Type = type
|
|
||||||
});
|
|
||||||
|
|
||||||
result.Add(r);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new DevicePermissionAssignResponse(result.Sum(x => x.AddCount), result.Sum(x => x.DeleteCount));
|
var gainfoNos = await GetPermissionUserNosAsync(dps);
|
||||||
|
|
||||||
|
|
||||||
|
var userTenantDeviceCodes = codesToDelete.IsPresent() && dps.Exists(x => x.AssignType != AssignTypeEnumType.Tenant)
|
||||||
|
? await _devicePermissionAnalyzeService.GetUserAssignDeviceCodesWithTypeInTenantAsync(gainfoNos)
|
||||||
|
: new Dictionary<string, IEnumerable<TenantAssignTypeDeviceCode>>();
|
||||||
|
|
||||||
|
|
||||||
|
var devicesToAdd = await _dbContext.Queryable<DeviceEntity>().Where(x => codesToAdd.Contains(x.Code)).ToListAsync();
|
||||||
|
var devicesToDelete = await _dbContext.Queryable<DeviceEntity>().Where(x => codesToDelete.Contains(x.Code)).ToListAsync();
|
||||||
|
|
||||||
|
var tasks = new List<DeviceTaskEntity>();
|
||||||
|
foreach (var gainfoNo in gainfoNos)
|
||||||
|
{
|
||||||
|
if (userTenantDeviceCodes.Any())
|
||||||
|
{
|
||||||
|
var myDeviceCodes = userTenantDeviceCodes[gainfoNo]
|
||||||
|
.SelectMany(x => x.AssignTypes
|
||||||
|
.Where(p =>
|
||||||
|
{
|
||||||
|
if (p.UniqueType == DeviceAssignmentTypeEnumType.Device)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return p.OriginalCodes.IsPresent() && p.OriginalCodes.First() != groupCode;
|
||||||
|
})
|
||||||
|
)
|
||||||
|
.SelectMany(x => x.Codes);
|
||||||
|
|
||||||
|
devicesToDelete = devicesToDelete.Where(p => devicesToDelete.Select(x => x.Code).Except(myDeviceCodes).Contains(p.Code)).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.AddRange(devicesToAdd.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(devicesToDelete.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());
|
||||||
|
}
|
||||||
|
|
||||||
|
await _dbContext.InsertAsync(tasks);
|
||||||
|
|
||||||
|
return new DevicePermissionAssignResponse(gainfoNos.Count * devicesToAdd.Count, gainfoNos.Count * devicesToDelete.Count);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取当前基地有权限的用户
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
private async Task<List<string>> GetPermissionUserNosAsync(List<DevicePermissionEntity> permissions)
|
||||||
|
{
|
||||||
|
//User
|
||||||
|
var userNos = permissions.Where(x => x.AssignType == AssignTypeEnumType.User)
|
||||||
|
.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();
|
||||||
|
|
||||||
|
//Department
|
||||||
|
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>();
|
||||||
|
foreach (var d in departmentNos)
|
||||||
|
{
|
||||||
|
departmentNosToSearch.AddRange(RecursionHelper.HandlerDepartmentNextCodes(d, departments));
|
||||||
|
}
|
||||||
|
|
||||||
|
departmentNosToSearch = departmentNosToSearch.Distinct().ToList();
|
||||||
|
|
||||||
|
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 => x.Type != GainfoUserTypeEnumType.External)
|
||||||
|
.Where(x => tenants.Contains(x.Factory))
|
||||||
|
.Select(x => x.No).ToListAsync();
|
||||||
|
|
||||||
|
return userNos.Concat(userNosByGroup).Concat(userNosByDepartment).Concat(userNosByWerk).Distinct().ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public async Task<List<DeviceUnderGroupResponse>> GetDevicesByGroupCodeAsync(Guid code)
|
public async Task<List<DeviceUnderGroupResponse>> GetDevicesByGroupCodeAsync(Guid code)
|
||||||
{
|
{
|
||||||
var relationships = await _dbContext.Queryable<DeviceGroupRelationshipEntity>()
|
var relationships = await _dbContext.Queryable<DeviceGroupRelationshipEntity>()
|
||||||
|
|||||||
@@ -132,7 +132,10 @@ public class DevicePermissionAnalyzeService : IDevicePermissionAnalyzeService
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public async Task<Dictionary<string, IEnumerable<TenantAssignTypeDeviceCode>>> GetUserAssignDeviceCodesWithTypeInTenantAsync(List<string> userNos, params AssignTypeEnumType[] ignoreAssignTypes)
|
public async Task<Dictionary<string, IEnumerable<TenantAssignTypeDeviceCode>>> GetUserAssignDeviceCodesWithTypeInTenantAsync(
|
||||||
|
List<string> userNos,
|
||||||
|
string ignoreAssignValue = null,
|
||||||
|
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()) return new Dictionary<string, IEnumerable<TenantAssignTypeDeviceCode>>();
|
if (!gainfos.Any()) return new Dictionary<string, IEnumerable<TenantAssignTypeDeviceCode>>();
|
||||||
@@ -144,7 +147,10 @@ public class DevicePermissionAnalyzeService : IDevicePermissionAnalyzeService
|
|||||||
var gainfoGroupRelationships = await _dbContext.Queryable<GainfoGroupRelationshipEntity>().Where(x => userNos.Contains(x.UserNo)).ToListAsync();
|
var gainfoGroupRelationships = await _dbContext.Queryable<GainfoGroupRelationshipEntity>().Where(x => userNos.Contains(x.UserNo)).ToListAsync();
|
||||||
var gainfoGroups = await _dbContext.Queryable<GainfoGroupEntity>().ToListAsync();
|
var gainfoGroups = await _dbContext.Queryable<GainfoGroupEntity>().ToListAsync();
|
||||||
|
|
||||||
var devicePermissions = await _dbContext.Queryable<DevicePermissionEntity>().WhereIF(ignoreAssignTypes.IsPresent(), x => !ignoreAssignTypes.Contains(x.AssignType)).ToListAsync();
|
var devicePermissions = await _dbContext.Queryable<DevicePermissionEntity>()
|
||||||
|
.WhereIF(ignoreAssignTypes.IsPresent(), x => !ignoreAssignTypes.Contains(x.AssignType) && x.AssignValue != ignoreAssignValue)
|
||||||
|
.Select<DevicePermissionDto>()
|
||||||
|
.ToListAsync();
|
||||||
var deviceGroups = await _dbContext.Queryable<DeviceGroupEntity>().ToListAsync();
|
var deviceGroups = await _dbContext.Queryable<DeviceGroupEntity>().ToListAsync();
|
||||||
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();
|
||||||
@@ -167,6 +173,7 @@ public class DevicePermissionAnalyzeService : IDevicePermissionAnalyzeService
|
|||||||
UserFactory = gainfo.Factory,
|
UserFactory = gainfo.Factory,
|
||||||
UserGainfoGroupCodes = gainfoGroupRelationships.Where(x => x.UserNo == gainfo.No).Select(x => x.GroupCode),
|
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), //外包人员不允许餐饮
|
DeviceCodes = devices.WhereIf(gainfo.Type == GainfoUserTypeEnumType.Outsource, x => x.Type != DeviceTypeEnumType.Meal).Select(x => x.Code), //外包人员不允许餐饮
|
||||||
|
IgnoreAssignValue = ignoreAssignValue,
|
||||||
IgnoreAssignTypeTypes = internalIgnoreAssignTypes
|
IgnoreAssignTypeTypes = internalIgnoreAssignTypes
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -187,13 +194,12 @@ public class DevicePermissionAnalyzeService : IDevicePermissionAnalyzeService
|
|||||||
private static IEnumerable<TenantAssignTypeDeviceCode> GetAssignDeviceCodesInternal(
|
private static IEnumerable<TenantAssignTypeDeviceCode> GetAssignDeviceCodesInternal(
|
||||||
AnalyzePackDto analyzePack,
|
AnalyzePackDto analyzePack,
|
||||||
List<GainfoGroupEntity> gainfoGroups,
|
List<GainfoGroupEntity> gainfoGroups,
|
||||||
List<DevicePermissionEntity> devicePermissions,
|
List<DevicePermissionDto> devicePermissions,
|
||||||
List<DeviceGroupEntity> deviceGroups,
|
List<DeviceGroupEntity> deviceGroups,
|
||||||
List<DeviceGroupRelationshipEntity> deviceGroupRelationships,
|
List<DeviceGroupRelationshipEntity> deviceGroupRelationships,
|
||||||
List<DepartmentEntity> departments)
|
List<DepartmentEntity> departments)
|
||||||
{
|
{
|
||||||
var deviceDtos = new List<TenantAssignTypeDeviceCode>();
|
var deviceDtos = new List<TenantAssignTypeDeviceCode>();
|
||||||
var deviceGroupDtos = new List<TenantAssignTypeDeviceCode>();
|
|
||||||
|
|
||||||
foreach (var assignType in new[] { AssignTypeEnumType.User, AssignTypeEnumType.Group, AssignTypeEnumType.Department, AssignTypeEnumType.Tenant })
|
foreach (var assignType in new[] { AssignTypeEnumType.User, AssignTypeEnumType.Group, AssignTypeEnumType.Department, AssignTypeEnumType.Tenant })
|
||||||
{
|
{
|
||||||
@@ -207,40 +213,32 @@ public class DevicePermissionAnalyzeService : IDevicePermissionAnalyzeService
|
|||||||
_ => throw new ArgumentOutOfRangeException()
|
_ => throw new ArgumentOutOfRangeException()
|
||||||
};
|
};
|
||||||
|
|
||||||
if (analyzePack.IgnoreAssignTypeTypes.Contains(assignType))
|
foreach (var assignValue in assignValues.WhereIf(analyzePack.IgnoreAssignTypeTypes.Contains(assignType), x => x != analyzePack.IgnoreAssignValue))
|
||||||
{
|
{
|
||||||
continue;
|
deviceDtos.AddRange(GetCurrentAssignTypePermissions(devicePermissions, gainfoGroups, departments,
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var assignValue in assignValues)
|
|
||||||
{
|
|
||||||
deviceDtos.AddRange(GetMyTypeDevicePermissionAsync(devicePermissions, gainfoGroups, deviceGroups, departments,
|
|
||||||
DeviceAssignmentTypeEnumType.Device, assignType, assignValue));
|
DeviceAssignmentTypeEnumType.Device, assignType, assignValue));
|
||||||
deviceGroupDtos.AddRange(GetMyTypeDevicePermissionAsync(devicePermissions, gainfoGroups, deviceGroups, departments,
|
|
||||||
DeviceAssignmentTypeEnumType.Group, assignType, assignValue));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//设备组转设备
|
var groupItems = GetCurrentAssignTypePermissions(devicePermissions, gainfoGroups, departments,
|
||||||
deviceDtos.AddRange(deviceGroupDtos.Select(x =>
|
DeviceAssignmentTypeEnumType.Group, assignType, assignValue);
|
||||||
{
|
|
||||||
var tenant = x.Tenant;
|
|
||||||
|
|
||||||
var deviceGroupCodes = x.AssignTypes.SelectMany(item => item.Codes);
|
foreach (var groupItem in groupItems)
|
||||||
var type = x.AssignTypes.ElementAt(0).AssignType;
|
|
||||||
return new TenantAssignTypeDeviceCode
|
|
||||||
{
|
|
||||||
Tenant = tenant,
|
|
||||||
AssignTypes = new[]
|
|
||||||
{
|
{
|
||||||
new AssignTypeDeviceCode
|
foreach (var assignTypeDeviceCodeItem in groupItem.AssignTypes)
|
||||||
{
|
{
|
||||||
AssignType = type,
|
var r = new List<Guid>();
|
||||||
Codes = deviceGroupRelationships.Where(item => deviceGroupCodes.Contains(item.GroupCode)).Select(item => item.DeviceCode)
|
foreach (var code in assignTypeDeviceCodeItem.Codes)
|
||||||
|
{
|
||||||
|
r.AddRange(RecursionHelper.HandlerDeviceGroupNextCodes(code, deviceGroups));
|
||||||
|
}
|
||||||
|
|
||||||
|
assignTypeDeviceCodeItem.OriginalCodes = r;
|
||||||
|
assignTypeDeviceCodeItem.Codes = deviceGroupRelationships.Where(x => r.Contains(x.GroupCode)).Select(x => x.DeviceCode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
|
||||||
}));
|
deviceDtos.AddRange(groupItems);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return deviceDtos.GroupBy(x => x.Tenant).Select(x =>
|
return deviceDtos.GroupBy(x => x.Tenant).Select(x =>
|
||||||
{
|
{
|
||||||
@@ -248,11 +246,14 @@ public class DevicePermissionAnalyzeService : IDevicePermissionAnalyzeService
|
|||||||
{
|
{
|
||||||
Tenant = x.Key,
|
Tenant = x.Key,
|
||||||
AssignTypes = x.SelectMany(p => p.AssignTypes)
|
AssignTypes = x.SelectMany(p => p.AssignTypes)
|
||||||
.GroupBy(p => p.AssignType)
|
.GroupBy(p => new { p.AssignType, p.AssignValue, p.UniqueType })
|
||||||
.Select(p => new AssignTypeDeviceCode
|
.Select(p => new AssignTypeDeviceCode
|
||||||
{
|
{
|
||||||
AssignType = p.Key,
|
AssignType = p.Key.AssignType,
|
||||||
Codes = p.SelectMany(item => analyzePack.DeviceCodes.Where(it => item.Codes.Contains(it)))
|
AssignValue = p.Key.AssignValue,
|
||||||
|
UniqueType = p.Key.UniqueType,
|
||||||
|
Codes = p.SelectMany(xx => xx.Codes),
|
||||||
|
OriginalCodes = p.Where(xx => xx.OriginalCodes.IsPresent()).SelectMany(xx => xx.OriginalCodes)
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -260,14 +261,16 @@ public class DevicePermissionAnalyzeService : IDevicePermissionAnalyzeService
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private static List<UserTenantDeviceCodeDto> GetCurrentAssignTypePermissions(
|
private static List<TenantAssignTypeDeviceCode> GetCurrentAssignTypePermissions(
|
||||||
List<DevicePermissionEntity> devicePermissions,
|
List<DevicePermissionDto> devicePermissions,
|
||||||
List<GainfoGroupEntity> gainfoGroups,
|
List<GainfoGroupEntity> gainfoGroups,
|
||||||
List<DepartmentEntity> departments,
|
List<DepartmentEntity> departments,
|
||||||
DeviceAssignmentTypeEnumType uniqueType,
|
DeviceAssignmentTypeEnumType uniqueType,
|
||||||
AssignTypeEnumType assignType,
|
AssignTypeEnumType assignType,
|
||||||
string assignValue)
|
string assignValue)
|
||||||
{
|
{
|
||||||
|
var assignValues = new List<string> { assignValue };
|
||||||
|
|
||||||
if (assignType == AssignTypeEnumType.Department)
|
if (assignType == AssignTypeEnumType.Department)
|
||||||
{
|
{
|
||||||
// 2401:00001
|
// 2401:00001
|
||||||
@@ -276,86 +279,33 @@ public class DevicePermissionAnalyzeService : IDevicePermissionAnalyzeService
|
|||||||
var tenantId = arr[0];
|
var tenantId = arr[0];
|
||||||
var currentDepartmentNo = arr[1];
|
var currentDepartmentNo = arr[1];
|
||||||
|
|
||||||
var departmentNos = RecursionHelper.HandlerDepartmentPreviousCodes(currentDepartmentNo, departments).Select(x => tenantId + ":" + x).ToList();
|
assignValues = RecursionHelper.HandlerDepartmentPreviousCodes(currentDepartmentNo, departments).Select(x => tenantId + ":" + x).ToList();
|
||||||
|
|
||||||
return 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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (assignType == AssignTypeEnumType.Group)
|
if (assignType == AssignTypeEnumType.Group)
|
||||||
{
|
{
|
||||||
var gainfoGroupCodes = RecursionHelper.HandlerGainfoGroupPreviousCodes(Guid.Parse(assignValue), gainfoGroups).Select(x => x.ToString());
|
assignValues = RecursionHelper.HandlerGainfoGroupPreviousCodes(Guid.Parse(assignValue), gainfoGroups).Select(x => x.ToString()).ToList();
|
||||||
return devicePermissions
|
|
||||||
.Where(x => x.UniqueType == uniqueType)
|
|
||||||
.Where(x => x.AssignType == assignType)
|
|
||||||
.Where(x => gainfoGroupCodes.Contains(x.AssignValue))
|
|
||||||
.GroupBy(x => x.TenantId)
|
|
||||||
.Select(x => new UserTenantDeviceCodeDto { Tenant = x.Key, Codes = x.Select(p => p.UniqueValue) })
|
|
||||||
.ToList();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//user / tenant
|
|
||||||
return devicePermissions
|
return devicePermissions
|
||||||
.Where(x => x.UniqueType == uniqueType
|
.Where(x => x.UniqueType == uniqueType)
|
||||||
&& x.AssignType == assignType
|
.Where(x => x.AssignType == assignType)
|
||||||
&& x.AssignValue == assignValue)
|
.Where(x => assignValues.Contains(x.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 TenantAssignTypeDeviceCode
|
||||||
|
{
|
||||||
|
Tenant = x.Key,
|
||||||
|
AssignTypes = x.GroupBy(xx => new { xx.AssignType, xx.AssignValue }).Select(xx => new AssignTypeDeviceCode
|
||||||
|
{
|
||||||
|
AssignType = xx.Key.AssignType,
|
||||||
|
AssignValue = xx.Key.AssignValue,
|
||||||
|
UniqueType = uniqueType,
|
||||||
|
Codes = xx.Select(xxx => xxx.UniqueValue)
|
||||||
|
})
|
||||||
|
})
|
||||||
.ToList();
|
.ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static IEnumerable<TenantAssignTypeDeviceCode> GetMyTypeDevicePermissionAsync(
|
|
||||||
List<DevicePermissionEntity> devicePermissions,
|
|
||||||
List<GainfoGroupEntity> gainfoGroups,
|
|
||||||
List<DeviceGroupEntity> deviceGroups,
|
|
||||||
List<DepartmentEntity> departments,
|
|
||||||
DeviceAssignmentTypeEnumType uniqueType, AssignTypeEnumType assignType, string assignValue)
|
|
||||||
{
|
|
||||||
var tenantDevicePermissions = GetCurrentAssignTypePermissions(devicePermissions, gainfoGroups, departments, uniqueType, assignType, assignValue);
|
|
||||||
|
|
||||||
//处理获取设备分组的情况,递归向下查询子分组
|
|
||||||
if (uniqueType == DeviceAssignmentTypeEnumType.Group)
|
|
||||||
{
|
|
||||||
foreach (var tenantDevicePermission in tenantDevicePermissions)
|
|
||||||
{
|
|
||||||
var deviceGroupCodes = tenantDevicePermission.Codes;
|
|
||||||
var r = tenantDevicePermission.Codes.ToList();
|
|
||||||
|
|
||||||
foreach (var deviceGroupCode in deviceGroupCodes)
|
|
||||||
{
|
|
||||||
r.AddRange(RecursionHelper.HandlerDeviceGroupNextCodes(deviceGroupCode, deviceGroups));
|
|
||||||
}
|
|
||||||
|
|
||||||
tenantDevicePermission.Codes = r.Distinct().ToList();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return tenantDevicePermissions.GroupBy(x => x.Tenant).Select(x =>
|
|
||||||
{
|
|
||||||
var tenant = x.Key;
|
|
||||||
|
|
||||||
return new TenantAssignTypeDeviceCode
|
|
||||||
{
|
|
||||||
Tenant = tenant,
|
|
||||||
AssignTypes = new[]
|
|
||||||
{
|
|
||||||
new AssignTypeDeviceCode
|
|
||||||
{
|
|
||||||
AssignType = assignType,
|
|
||||||
Codes = x.SelectMany(item => item.Codes)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private async Task ChunkInsertAsync<T>(List<T> batch) where T : class, new()
|
private async Task ChunkInsertAsync<T>(List<T> batch) where T : class, new()
|
||||||
{
|
{
|
||||||
foreach (var tasks in batch.Chunk(500))
|
foreach (var tasks in batch.Chunk(500))
|
||||||
@@ -365,9 +315,9 @@ public class DevicePermissionAnalyzeService : IDevicePermissionAnalyzeService
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public async Task<Dictionary<string, IEnumerable<UserTenantDeviceCodeDto>>> GetUserAssignDeviceCodesInTenantAsync(List<string> userNos, params AssignTypeEnumType[] ignoreAssignTypes)
|
public async Task<Dictionary<string, IEnumerable<UserTenantDeviceCodeDto>>> GetUserAssignDeviceCodesInTenantAsync(List<string> userNos, string ignoreAssignValue = null, params AssignTypeEnumType[] ignoreAssignTypes)
|
||||||
{
|
{
|
||||||
var result = await GetUserAssignDeviceCodesWithTypeInTenantAsync(userNos, ignoreAssignTypes);
|
var result = await GetUserAssignDeviceCodesWithTypeInTenantAsync(userNos, ignoreAssignValue, ignoreAssignTypes);
|
||||||
|
|
||||||
return result.ToDictionary(x => x.Key, x => x.Value.GroupBy(p => p.Tenant).Select(item =>
|
return result.ToDictionary(x => x.Key, x => x.Value.GroupBy(p => p.Tenant).Select(item =>
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -21,8 +21,12 @@ public class GainfoDto
|
|||||||
|
|
||||||
public class AnalyzePackDto
|
public class AnalyzePackDto
|
||||||
{
|
{
|
||||||
|
public string IgnoreAssignValue { get; set; }
|
||||||
|
|
||||||
public string UserNo { get; set; }
|
public string UserNo { get; set; }
|
||||||
|
|
||||||
public string UserDepartment { get; set; }
|
public string UserDepartment { get; set; }
|
||||||
|
|
||||||
public string UserFactory { get; set; }
|
public string UserFactory { get; set; }
|
||||||
|
|
||||||
public IEnumerable<Guid> UserGainfoGroupCodes { get; set; }
|
public IEnumerable<Guid> UserGainfoGroupCodes { get; set; }
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Kurisu.AspNetCore" Version="0.9.20" />
|
<PackageReference Include="Kurisu.AspNetCore" Version="0.9.35" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
Reference in New Issue
Block a user