312 lines
14 KiB
C#
312 lines
14 KiB
C#
using System.Collections.Concurrent;
|
|
using Dlhis.Basic.DataPermissions;
|
|
using Dlhis.Basic.Device.Dtos;
|
|
using Dlhis.Basic.Device.Request;
|
|
using Dlhis.Basic.Organization.Dto;
|
|
using Dlhis.Entity.Basic.Entity;
|
|
using Dlhis.Entity.Basic.Enums;
|
|
using Dlhis.Entity.Device.Entities;
|
|
using Dlhis.Entity.Device.Enums;
|
|
using Dlhis.Entity.Sys.Entities;
|
|
using Dlhis.Entity.Sys.Enums;
|
|
using Kurisu.AspNetCore.Authentication.Abstractions;
|
|
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
|
using Kurisu.AspNetCore.Utils.Extensions;
|
|
|
|
namespace Dlhis.Basic.Device.Services.Implements;
|
|
|
|
public class DevicePermissionAnalyzeService : IDevicePermissionAnalyzeService
|
|
{
|
|
private readonly IDbContext _dbContext;
|
|
private readonly ICurrentUser _currentUser;
|
|
private readonly IDataPermissionService _myDataPermissionService;
|
|
|
|
public DevicePermissionAnalyzeService(IDbContext dbContext,
|
|
ICurrentUser currentUser,
|
|
IDataPermissionService myDataPermissionService)
|
|
{
|
|
_dbContext = dbContext;
|
|
_currentUser = currentUser;
|
|
_myDataPermissionService = myDataPermissionService;
|
|
}
|
|
|
|
public async Task<List<DevicePermissionEntity>> GetDevicePermissionsAsync(DevicePermissionSaveRequest request)
|
|
{
|
|
var currentTenantId = await GetCurrentFactoryAsync(request.UniqueType, request.Codes);
|
|
|
|
//获取当前查询的配置
|
|
var setting = _dbContext.GetQueryableSetting();
|
|
|
|
var query = _dbContext.Queryable<DevicePermissionEntity>().Where(x => x.AssignType == request.AssignType
|
|
&& x.AssignValue == request.GetActualAssignValue()
|
|
&& x.UniqueType == request.UniqueType
|
|
&& x.Type == request.Type
|
|
&& x.TenantId == currentTenantId);
|
|
|
|
//根据数据权限进行过滤
|
|
if (setting.GetEnableDataPermission<DevicePermissionEntity>())
|
|
{
|
|
(bool enableDeviceFilter, List<Guid> deviceCodes) = _myDataPermissionService.GetMyDeviceCodes();
|
|
(bool enableDeviceGroupFilter, List<Guid> deviceGroupCodes) = _myDataPermissionService.GetMyDeviceGroupCodes();
|
|
|
|
query = query
|
|
.WhereIF(enableDeviceFilter && request.UniqueType == DeviceAssignmentTypeEnumType.Device, x => deviceCodes.Contains(x.UniqueValue))
|
|
.WhereIF(enableDeviceGroupFilter && request.UniqueType == DeviceAssignmentTypeEnumType.Group, x => deviceGroupCodes.Contains(x.UniqueValue));
|
|
}
|
|
|
|
return await query.ToListAsync();
|
|
|
|
|
|
//获取当前的操作人的基地
|
|
async Task<string> GetCurrentFactoryAsync(DeviceAssignmentTypeEnumType uniqueType, List<Guid> codes)
|
|
{
|
|
var internalCurrentTenantId = _currentUser.GetStringTenantId();
|
|
|
|
var r = uniqueType switch
|
|
{
|
|
DeviceAssignmentTypeEnumType.Device => await _dbContext.Queryable<DeviceEntity>().Where(x => codes.Contains(x.Code)).Where(x => x.TenantId != internalCurrentTenantId).AnyAsync(),
|
|
DeviceAssignmentTypeEnumType.Group => await _dbContext.Queryable<DeviceGroupEntity>().Where(x => codes.Contains(x.Code)).Where(x => x.TenantId != internalCurrentTenantId).AnyAsync(),
|
|
_ => false
|
|
};
|
|
|
|
r.ThrowIfTrue("存在设备或者设备组不属于当前厂区,操作失败!");
|
|
|
|
return internalCurrentTenantId;
|
|
}
|
|
}
|
|
|
|
public async Task<List<Guid>> GetUserPermissionDeviceCodesAsync(string userNo)
|
|
{
|
|
var result = await GetUserAssignDeviceCodesWithTypeInTenantAsync(new List<string> { userNo });
|
|
return result.Count == 0
|
|
? new List<Guid>()
|
|
: result[userNo].SelectMany(x => x.AssignTypes.SelectMany(p => p.Codes)).Distinct().ToList();
|
|
}
|
|
|
|
|
|
public async Task<Dictionary<string, List<TenantAssignTypeDeviceCode>>> GetUserAssignDeviceCodesWithTypeInTenantAsync(
|
|
List<string> userNos,
|
|
DeviceAssignmentTypeEnumType? ignoreUniqueType = null,
|
|
string ignoreAssignValue = null, AssignTypeEnumType? ignoreAssignType = null)
|
|
{
|
|
var gainfos = await _dbContext.Queryable<GainfoEntity>().WhereIF(userNos.IsPresent(), x => userNos.Contains(x.No)).ToListAsync();
|
|
if (!gainfos.Any()) return new Dictionary<string, List<TenantAssignTypeDeviceCode>>();
|
|
|
|
var devices = await _dbContext.Queryable<DeviceEntity>().ToListAsync();
|
|
|
|
var enableFactoryPermissions = await _dbContext.Queryable<FactorySettingEntity>().ToListAsync();
|
|
|
|
var gainfoGroupRelationships = await _dbContext.Queryable<GainfoGroupRelationshipEntity>().Where(x => userNos.Contains(x.UserNo)).ToListAsync();
|
|
var gainfoGroups = await _dbContext.Queryable<GainfoGroupEntity>().ToListAsync();
|
|
|
|
var devicePermissions = await _dbContext.Queryable<DevicePermissionEntity>()
|
|
.Select<DevicePermissionDto>()
|
|
.ToListAsync();
|
|
|
|
var deviceGroups = await _dbContext.Queryable<DeviceGroupEntity>().ToListAsync();
|
|
var deviceRelationships = await _dbContext.Queryable<DeviceGroupRelationshipEntity>().ToListAsync();
|
|
var departments = await _dbContext.Queryable<DepartmentEntity>().ToListAsync();
|
|
|
|
var result = new ConcurrentDictionary<string, List<TenantAssignTypeDeviceCode>>();
|
|
|
|
gainfos.ParallelForEach(gainfo =>
|
|
{
|
|
var internalIgnoreAssignTypes = new List<AssignTypeEnumType>();
|
|
|
|
if (ignoreAssignType.HasValue)
|
|
internalIgnoreAssignTypes.Add(ignoreAssignType.Value);
|
|
|
|
if (gainfo.Type == GainfoUserTypeEnumType.External ||
|
|
(gainfo.Type == GainfoUserTypeEnumType.Outsource && (enableFactoryPermissions.FirstOrDefault(x => x.TenantId == gainfo.Factory)?.EnableOutsourceDown ?? true) == false))
|
|
{
|
|
internalIgnoreAssignTypes.Add(AssignTypeEnumType.Tenant);
|
|
}
|
|
|
|
var analyzePack = new AnalyzePackDto
|
|
{
|
|
UserNo = gainfo.No,
|
|
UserDepartment = gainfo.DepartmentNo,
|
|
UserFactory = gainfo.Factory,
|
|
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), //外包人员不允许餐饮
|
|
IgnoreAssignValue = ignoreAssignValue,
|
|
IgnoreAssignTypes = internalIgnoreAssignTypes,
|
|
IgnoreUniqueType = ignoreUniqueType
|
|
};
|
|
|
|
var codes = GetAssignDeviceCodesPermissions(analyzePack,
|
|
gainfoGroups,
|
|
devicePermissions,
|
|
deviceGroups,
|
|
deviceRelationships,
|
|
departments);
|
|
|
|
result.TryAdd(gainfo.No, codes);
|
|
});
|
|
|
|
return result.ToDictionary(x => x.Key, x => x.Value);
|
|
}
|
|
|
|
|
|
private static List<TenantAssignTypeDeviceCode> GetAssignDeviceCodesPermissions(
|
|
AnalyzePackDto analyzePack,
|
|
List<GainfoGroupEntity> gainfoGroups,
|
|
List<DevicePermissionDto> devicePermissions,
|
|
List<DeviceGroupEntity> deviceGroups,
|
|
List<DeviceGroupRelationshipEntity> deviceGroupRelationships,
|
|
List<DepartmentEntity> departments)
|
|
{
|
|
var deviceDtos = new List<TenantAssignTypeDeviceCode>();
|
|
|
|
var uniqueTypes = new[] { DeviceAssignmentTypeEnumType.Device, DeviceAssignmentTypeEnumType.Group };
|
|
foreach (var assignType in new[] { AssignTypeEnumType.User, AssignTypeEnumType.Group, AssignTypeEnumType.Department, AssignTypeEnumType.Tenant })
|
|
{
|
|
if (analyzePack.IgnoreAssignTypes.Contains(assignType) && analyzePack.IgnoreAssignValue.IsEmpty())
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (assignType == AssignTypeEnumType.Group)
|
|
{
|
|
var tempUserGroupCodes = analyzePack.UserGainfoGroupCodes.ToList();
|
|
foreach (Guid analyzePackUserGainfoGroupCode in analyzePack.UserGainfoGroupCodes)
|
|
{
|
|
tempUserGroupCodes.AddRange(RecursionHelper.HandlerGainfoGroupPreviousCodes(analyzePackUserGainfoGroupCode, gainfoGroups));
|
|
}
|
|
|
|
analyzePack.UserGainfoGroupCodes = tempUserGroupCodes.Distinct().ToList();
|
|
}
|
|
|
|
var assignValues = assignType switch
|
|
{
|
|
AssignTypeEnumType.User => new List<string> { analyzePack.UserNo },
|
|
AssignTypeEnumType.Group => analyzePack.UserGainfoGroupCodes.Select(x => x.ToString()),
|
|
AssignTypeEnumType.Department => RecursionHelper.HandlerDepartmentPreviousCodes(analyzePack.UserDepartment, departments).Select(x => analyzePack.UserFactory + ":" + x).ToList(),
|
|
AssignTypeEnumType.Tenant => new List<string> { analyzePack.UserFactory },
|
|
AssignTypeEnumType.Role => throw new ArgumentOutOfRangeException(),
|
|
_ => throw new ArgumentOutOfRangeException()
|
|
};
|
|
|
|
var data = assignValues.SelectMany(x => uniqueTypes.Select(p => new { assignValue = x, uniqueType = p })).ToList();
|
|
var needRemove = data.Where(x => x.assignValue == analyzePack.IgnoreAssignValue && x.uniqueType == analyzePack.IgnoreUniqueType);
|
|
data = data.ExceptBy(needRemove, x => new { x.assignValue, x.uniqueType }).ToList();
|
|
|
|
foreach (var dataPack in data.GroupBy(x => x.uniqueType))
|
|
{
|
|
var currentUniqueType = dataPack.Key;
|
|
var currentAssignValues = dataPack.Select(x => x.assignValue).ToList();
|
|
|
|
deviceDtos.AddRange(
|
|
SearchActualAssignPermissions(
|
|
devicePermissions,
|
|
deviceGroups, deviceGroupRelationships,
|
|
currentUniqueType, assignType, currentAssignValues
|
|
)
|
|
);
|
|
}
|
|
}
|
|
|
|
return deviceDtos.GroupBy(x => x.Tenant).Select(x =>
|
|
{
|
|
var dto = new TenantAssignTypeDeviceCode
|
|
{
|
|
Tenant = x.Key,
|
|
AssignTypes = x.SelectMany(p => p.AssignTypes)
|
|
.GroupBy(p => new { p.AssignType, p.AssignValue, p.UniqueType })
|
|
.Select(p => new AssignTypeDeviceCode
|
|
{
|
|
AssignType = p.Key.AssignType,
|
|
AssignValue = p.Key.AssignValue,
|
|
UniqueType = p.Key.UniqueType,
|
|
Codes = p.SelectMany(xx => xx.Codes.Where(xxx => analyzePack.DeviceCodes.Contains(xxx))).ToList(),
|
|
OriginalCodes = p.Where(xx => xx.OriginalCodes.IsPresent()).SelectMany(xx => xx.OriginalCodes).ToList()
|
|
}).ToList()
|
|
};
|
|
|
|
return dto;
|
|
}).ToList();
|
|
}
|
|
|
|
private static List<TenantAssignTypeDeviceCode> SearchActualAssignPermissions(
|
|
List<DevicePermissionDto> devicePermissions,
|
|
List<DeviceGroupEntity> deviceGroups,
|
|
List<DeviceGroupRelationshipEntity> deviceGroupRelationships,
|
|
DeviceAssignmentTypeEnumType uniqueType,
|
|
AssignTypeEnumType assignType,
|
|
List<string> assignValues)
|
|
{
|
|
var r = devicePermissions
|
|
.Where(x => x.UniqueType == uniqueType)
|
|
.Where(x => x.AssignType == assignType)
|
|
.Where(x => assignValues.Contains(x.AssignValue))
|
|
.GroupBy(x => x.TenantId)
|
|
.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()
|
|
})
|
|
.ToList();
|
|
|
|
if (uniqueType == DeviceAssignmentTypeEnumType.Group)
|
|
{
|
|
foreach (var groupItem in r)
|
|
{
|
|
foreach (var assignTypeDeviceCodeItem in groupItem.AssignTypes)
|
|
{
|
|
var nextItems = new List<Guid>();
|
|
foreach (var code in assignTypeDeviceCodeItem.Codes)
|
|
{
|
|
nextItems.AddRange(RecursionHelper.HandlerDeviceGroupNextCodes(code, deviceGroups));
|
|
}
|
|
|
|
nextItems = nextItems.Distinct().ToList();
|
|
assignTypeDeviceCodeItem.OriginalCodes = nextItems;
|
|
assignTypeDeviceCodeItem.Codes = deviceGroupRelationships.Where(x => nextItems.Contains(x.GroupCode)).Select(x => x.DeviceCode).ToList();
|
|
}
|
|
}
|
|
}
|
|
|
|
return r;
|
|
}
|
|
|
|
public async Task<List<Guid>> GetAssignmentsAsync(DeviceGetAssignmentsRequest request)
|
|
{
|
|
var r = await GetDevicePermissionsAsync(new DevicePermissionSaveRequest
|
|
{
|
|
AssignType = request.AssignType,
|
|
AssignValue = request.AssignValue,
|
|
UniqueType = request.UniqueType,
|
|
Codes = null,
|
|
TenantId = request.TenantId,
|
|
Type = request.Type
|
|
});
|
|
|
|
return r.Select(x => x.UniqueValue).ToList();
|
|
}
|
|
|
|
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();
|
|
}
|
|
} |