fix 设备任务下发调整
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
using System.Diagnostics;
|
||||
using Dlhis.Basic.Device.Dtos;
|
||||
using Dlhis.Basic.Device.Request;
|
||||
using Dlhis.Basic.Device.Response;
|
||||
using Dlhis.Entity.Basic.Entity;
|
||||
@@ -8,7 +8,7 @@ using Dlhis.Entity.Sys.Entities;
|
||||
using Dlhis.Entity.Sys.Enums;
|
||||
using Kurisu.AspNetCore.Utils.Extensions;
|
||||
using Kurisu.Core.DataAccess.Dto;
|
||||
using Kurisu.Core.User.Abstractions;
|
||||
using Kurisu.Core.DataAccess.Entity;
|
||||
using Kurisu.SqlSugar.Services;
|
||||
|
||||
namespace Dlhis.Basic.Device.Services.Implements;
|
||||
@@ -16,18 +16,17 @@ namespace Dlhis.Basic.Device.Services.Implements;
|
||||
public class DevicePermissionService : IDevicePermissionService
|
||||
{
|
||||
private readonly IDbContext _dbContext;
|
||||
private readonly ICurrentUser _currentUser;
|
||||
|
||||
public DevicePermissionService(IDbContext dbContext, ICurrentUser currentUser)
|
||||
public DevicePermissionService(IDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_currentUser = currentUser;
|
||||
}
|
||||
|
||||
public async Task AssignAsync(DevicePermissionSaveReuqest reuqest)
|
||||
{
|
||||
await GenerateDeviceTasksAsync(async () =>
|
||||
{
|
||||
//删除当前匹配的基地权限
|
||||
var p = await _dbContext.Queryable<DevicePermissionEntity>()
|
||||
.Where(x => x.AssignType == reuqest.AssignType)
|
||||
.Where(x => x.AssignValue == reuqest.AssignValue)
|
||||
@@ -36,17 +35,20 @@ public class DevicePermissionService : IDevicePermissionService
|
||||
|
||||
await _dbContext.DeleteReallyAsync(p);
|
||||
|
||||
var devicePermissions = reuqest.Codes.Select(grpOrDeviceCode => new DevicePermissionEntity
|
||||
//保存配置的权限
|
||||
if (reuqest.Codes.IsPresent())
|
||||
{
|
||||
AssignType = reuqest.AssignType,
|
||||
AssignValue = reuqest.AssignValue,
|
||||
UniqueType = reuqest.UniqueType,
|
||||
UniqueValue = grpOrDeviceCode,
|
||||
var devicePermissions = reuqest.Codes.Select(code => new DevicePermissionEntity
|
||||
{
|
||||
AssignType = reuqest.AssignType,
|
||||
AssignValue = reuqest.AssignValue,
|
||||
UniqueType = reuqest.UniqueType,
|
||||
UniqueValue = code,
|
||||
|
||||
}).ToList();
|
||||
}).ToList();
|
||||
|
||||
if (devicePermissions.Any())
|
||||
await _dbContext.InsertAsync(devicePermissions);
|
||||
}
|
||||
|
||||
}, null);
|
||||
}
|
||||
@@ -62,86 +64,93 @@ public class DevicePermissionService : IDevicePermissionService
|
||||
|
||||
public async Task GenerateDeviceTasksAsync(Func<Task> func, List<string> userNos)
|
||||
{
|
||||
//var redisKey = "devicePermissions:" + _currentUser.GetStringTenantId();
|
||||
|
||||
//获取当前所有人的权限
|
||||
if (userNos.IsEmpty())
|
||||
userNos = await GetPermissionUserNosAsync();
|
||||
|
||||
var beforeDic = await GetAssignDeviceCodesInternalAsync(userNos); // new Dictionary<string, List<Guid>>();
|
||||
//if (await _redisCache.KeyExistsAsync(redisKey))
|
||||
//{
|
||||
// var cache = await _redisCache.StringGetAsync<Dictionary<string, List<Guid>>>(redisKey);
|
||||
// if (cache?.Any() == true)
|
||||
// {
|
||||
// beforeDic = cache;
|
||||
// }
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// beforeDic = await GetAssignDeviceCodesInternalAsync(userNos);
|
||||
//}
|
||||
|
||||
//业务处理
|
||||
await func();
|
||||
|
||||
//获取业务处理后所有人的权限
|
||||
var nextUserNos = await GetPermissionUserNosAsync();
|
||||
if (nextUserNos.Any())
|
||||
userNos = userNos.Concat(nextUserNos).Distinct().ToList();
|
||||
|
||||
var currentDic = await GetAssignDeviceCodesInternalAsync(userNos);
|
||||
|
||||
var deivces = await _dbContext.Queryable<DeviceEntity>().ToListAsync();
|
||||
var waitTasks = new List<DeviceTaskEntity>();
|
||||
foreach (var userNo in userNos)
|
||||
_dbContext.Client.QueryFilter.ClearAndBackup<ITenantId>();
|
||||
try
|
||||
{
|
||||
beforeDic.TryGetValue(userNo, out var beforeCodes);
|
||||
beforeCodes ??= new List<Guid>();
|
||||
//获取所有人的权限
|
||||
if (userNos.IsEmpty())
|
||||
userNos = await GetPermissionUserNosAsync();
|
||||
|
||||
currentDic.TryGetValue(userNo, out var currentCodes);
|
||||
currentCodes ??= new List<Guid>();
|
||||
//var before = await GetAssignDeviceCodesInternalAsync(userNos);
|
||||
var before = await GetAssignDeviceCodesInternal1Async(userNos);
|
||||
|
||||
//被删除
|
||||
var deletedItems = beforeCodes.Where(x => !currentCodes.Contains(x)).ToList();
|
||||
//新增
|
||||
var addItems = currentCodes.Where(x => !beforeCodes.Contains(x)).ToList();
|
||||
_dbContext.Client.QueryFilter.Restore();
|
||||
//业务处理
|
||||
await func();
|
||||
_dbContext.Client.QueryFilter.ClearAndBackup<ITenantId>();
|
||||
|
||||
if (!deletedItems.Any() && !addItems.Any())
|
||||
//获取业务处理后所有人的权限
|
||||
var nextUserNos = await GetPermissionUserNosAsync();
|
||||
if (nextUserNos.Any())
|
||||
userNos = userNos.Concat(nextUserNos).Distinct().ToList();
|
||||
|
||||
//var current = await GetAssignDeviceCodesInternalAsync(userNos);
|
||||
var current = await GetAssignDeviceCodesInternal1Async(userNos);
|
||||
|
||||
var deivces = await _dbContext.Queryable<DeviceEntity>().ToListAsync();
|
||||
var waitTasks = new List<DeviceTaskEntity>();
|
||||
|
||||
foreach (var userNo in userNos)
|
||||
{
|
||||
continue;
|
||||
before.TryGetValue(userNo, out var beforeTenantDeviceCodes);
|
||||
beforeTenantDeviceCodes ??= new();
|
||||
|
||||
current.TryGetValue(userNo, out var currentTenantDeviceCodes);
|
||||
currentTenantDeviceCodes ??= new();
|
||||
|
||||
foreach (var item in currentTenantDeviceCodes)
|
||||
{
|
||||
var currentCodes = item.Codes;
|
||||
var beforeCodes = beforeTenantDeviceCodes.FirstOrDefault(x => x.Tenant == item.Tenant)?.Codes ?? new();
|
||||
|
||||
//被删除
|
||||
var deletedItems = beforeCodes.Where(x => !currentCodes.Contains(x)).ToList();
|
||||
//新增
|
||||
var addItems = currentCodes.Where(x => !beforeCodes.Contains(x)).ToList();
|
||||
|
||||
if (!deletedItems.Any() && !addItems.Any())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var deviceCodes = deivces.Where(x => deletedItems.Contains(x.Code) || addItems.Contains(x.Code)).Select(x => x.Code).ToList();
|
||||
deletedItems = deletedItems.Where(x => deviceCodes.Contains(x)).ToList();
|
||||
addItems = addItems.Where(x => deviceCodes.Contains(x)).ToList();
|
||||
|
||||
//下发任务创建
|
||||
var toDeleted = deletedItems.Select(x => new DeviceTaskEntity
|
||||
{
|
||||
DeviceUniqueNo = deivces.First(item => item.Code == x).UniqueNo,
|
||||
Status = DeviceTaskStatusEnumType.Wait,
|
||||
Type = DeviceTaskEnumType.Delete,
|
||||
UserNo = userNo,
|
||||
Version = 0,
|
||||
TenantId = item.Tenant,
|
||||
}).ToList();
|
||||
|
||||
var toAdd = addItems.Select(x => new DeviceTaskEntity
|
||||
{
|
||||
DeviceUniqueNo = deivces.First(item => item.Code == x).UniqueNo,
|
||||
Status = DeviceTaskStatusEnumType.Wait,
|
||||
Type = DeviceTaskEnumType.Add,
|
||||
UserNo = userNo,
|
||||
Version = 0,
|
||||
TenantId = item.Tenant,
|
||||
}).ToList();
|
||||
|
||||
waitTasks.AddRange(toDeleted.Concat(toAdd).ToList());
|
||||
}
|
||||
}
|
||||
|
||||
var deviceCodes = deivces.Where(x => deletedItems.Contains(x.Code) || addItems.Contains(x.Code)).Select(x => x.Code).ToList();
|
||||
deletedItems = deletedItems.Where(x => deviceCodes.Contains(x)).ToList();
|
||||
addItems = addItems.Where(x => deviceCodes.Contains(x)).ToList();
|
||||
|
||||
//下发任务创建
|
||||
var toDeleted = deletedItems.Select(x => new DeviceTaskEntity
|
||||
if (waitTasks.Any())
|
||||
{
|
||||
DeviceUniqueNo = deivces.First(item => item.Code == x).UniqueNo,
|
||||
Status = DeviceTaskStatusEnumType.Wait,
|
||||
Type = DeviceTaskEnumType.Delete,
|
||||
UserNo = userNo,
|
||||
Version = 0,
|
||||
}).ToList();
|
||||
|
||||
var toAdd = addItems.Select(x => new DeviceTaskEntity
|
||||
{
|
||||
DeviceUniqueNo = deivces.First(item => item.Code == x).UniqueNo,
|
||||
Status = DeviceTaskStatusEnumType.Wait,
|
||||
Type = DeviceTaskEnumType.Add,
|
||||
UserNo = userNo,
|
||||
Version = 0,
|
||||
}).ToList();
|
||||
|
||||
waitTasks.AddRange(toDeleted.Concat(toAdd).ToList());
|
||||
await _dbContext.InsertAsync(waitTasks);
|
||||
}
|
||||
}
|
||||
|
||||
if (waitTasks.Any())
|
||||
finally
|
||||
{
|
||||
//await _redisCache.StringSetAsync(redisKey, JsonConvert.SerializeObject(currentDic));
|
||||
await _dbContext.InsertAsync(waitTasks);
|
||||
_dbContext.Client.QueryFilter.Restore();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -248,6 +257,62 @@ public class DevicePermissionService : IDevicePermissionService
|
||||
return deviceCodes.Distinct().ToList();
|
||||
}
|
||||
|
||||
public List<UserTenantDeviceCodeDto> GetAssignDeviceCodesInternal1(GainfoEntity gainfo,
|
||||
List<GainfoGroupRelationshipEntity> gainfoGroupRelationships,
|
||||
List<DevicePermissionEntity> devicePermissions,
|
||||
List<DeviceGroupEntity> deviceGroups,
|
||||
List<DeviceGroupRelationshipEntity> deviceGroupRelationships)
|
||||
{
|
||||
var userNo = gainfo.No;
|
||||
var deviceCodes = new List<UserTenantDeviceCodeDto>();
|
||||
var deviceGroupCodes = new List<UserTenantDeviceCodeDto>();
|
||||
|
||||
//获取人员的分组,跨基地
|
||||
var gainfoGroups = gainfoGroupRelationships.Where(x => x.UserNo == userNo).Select(x => x.GroupCode).ToList();
|
||||
|
||||
//按User获取权限
|
||||
var du = GetAssignCodesInternal1(devicePermissions, deviceGroups, DeviceAssignmentTypeEnumType.Device, AssignTypeEnumType.User, userNo);
|
||||
var gu = GetAssignCodesInternal1(devicePermissions, deviceGroups, DeviceAssignmentTypeEnumType.Group, AssignTypeEnumType.User, userNo);
|
||||
deviceCodes.AddRange(du);
|
||||
deviceGroupCodes.AddRange(gu);
|
||||
|
||||
foreach (var groupCode in gainfoGroups)
|
||||
{
|
||||
var dg = GetAssignCodesInternal1(devicePermissions, deviceGroups, DeviceAssignmentTypeEnumType.Device, AssignTypeEnumType.Group, groupCode.ToString());
|
||||
var gg = GetAssignCodesInternal1(devicePermissions, deviceGroups, DeviceAssignmentTypeEnumType.Group, AssignTypeEnumType.Group, groupCode.ToString());
|
||||
deviceCodes.AddRange(dg);
|
||||
deviceGroupCodes.AddRange(gg);
|
||||
}
|
||||
|
||||
var dd = GetAssignCodesInternal1(devicePermissions, deviceGroups, DeviceAssignmentTypeEnumType.Device, AssignTypeEnumType.Department, gainfo.DepartmentNo);
|
||||
var gd = GetAssignCodesInternal1(devicePermissions, deviceGroups, DeviceAssignmentTypeEnumType.Group, AssignTypeEnumType.Department, gainfo.DepartmentNo);
|
||||
deviceCodes.AddRange(dd);
|
||||
deviceGroupCodes.AddRange(gd);
|
||||
|
||||
var dt = GetAssignCodesInternal1(devicePermissions, deviceGroups, DeviceAssignmentTypeEnumType.Device, AssignTypeEnumType.Tenant, gainfo.Factory);
|
||||
var gt = GetAssignCodesInternal1(devicePermissions, deviceGroups, DeviceAssignmentTypeEnumType.Group, AssignTypeEnumType.Tenant, gainfo.Factory);
|
||||
deviceCodes.AddRange(du);
|
||||
deviceGroupCodes.AddRange(gu);
|
||||
|
||||
var groupCodes = deviceGroupCodes.SelectMany(x => x.Codes).Distinct().ToList();
|
||||
var deveiceCodesByGroup = deviceGroupRelationships.Where(x => groupCodes.Contains(x.GroupCode)).GroupBy(x => x.TenantId)
|
||||
.Select(x => new UserTenantDeviceCodeDto
|
||||
{
|
||||
Tenant = x.Key,
|
||||
Codes = x.Select(item => item.DeviceCode).Distinct().ToList()
|
||||
});
|
||||
|
||||
deviceCodes.AddRange(deveiceCodesByGroup);
|
||||
|
||||
return deviceCodes.GroupBy(x => x.Tenant).Select(x => new UserTenantDeviceCodeDto
|
||||
{
|
||||
Tenant = x.Key,
|
||||
Codes = x.SelectMany(item => item.Codes).Distinct().ToList()
|
||||
|
||||
}).ToList();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
@@ -365,17 +430,65 @@ public class DevicePermissionService : IDevicePermissionService
|
||||
}
|
||||
}
|
||||
|
||||
private static List<UserTenantDeviceCodeDto> GetAssignCodesInternal1(
|
||||
List<DevicePermissionEntity> devicePermissions, List<DeviceGroupEntity> deviceGroups,
|
||||
DeviceAssignmentTypeEnumType uniqueType, AssignTypeEnumType assignType, string assignValue)
|
||||
{
|
||||
var tenantDevicePermissions = devicePermissions
|
||||
.Where(x => x.UniqueType == uniqueType)
|
||||
.Where(x => x.AssignType == assignType)
|
||||
.Where(x => x.AssignValue == assignValue)
|
||||
.GroupBy(x => x.TenantId)
|
||||
.Select(x => new UserTenantDeviceCodeDto { Tenant = x.Key, Codes = x.Select(x => x.UniqueValue).ToList() })
|
||||
.ToList();
|
||||
|
||||
//处理获取设备分组的情况
|
||||
if (uniqueType == DeviceAssignmentTypeEnumType.Group)
|
||||
{
|
||||
//开始递归处理
|
||||
var tops = deviceGroups.Where(x => !x.PCode.HasValue).ToList();
|
||||
tops.ForEach(x =>
|
||||
{
|
||||
RecursionGetNext(x, deviceGroups, tenantDevicePermissions, tenantDevicePermissions.SelectMany(x => x.Codes).Contains(x.Code));
|
||||
});
|
||||
}
|
||||
|
||||
return tenantDevicePermissions.GroupBy(x => x.Tenant).Select(x =>
|
||||
{
|
||||
var tenant = x.Key;
|
||||
|
||||
return new UserTenantDeviceCodeDto
|
||||
{
|
||||
Tenant = tenant,
|
||||
Codes = x.SelectMany(item => item.Codes).ToList()
|
||||
};
|
||||
})
|
||||
.Where(x => x.Tenant.IsPresent()).ToList();
|
||||
|
||||
void RecursionGetNext(DeviceGroupEntity current, List<DeviceGroupEntity> deviceGroups, List<UserTenantDeviceCodeDto> result, bool add)
|
||||
{
|
||||
var next = deviceGroups.Where(x => x.PCode == current.Code).ToList();
|
||||
foreach (var item in next)
|
||||
{
|
||||
if (add && item.TenantId.IsPresent())
|
||||
{
|
||||
result.Add(new UserTenantDeviceCodeDto { Tenant = item.TenantId, Codes = new List<Guid> { item.Code } });
|
||||
}
|
||||
RecursionGetNext(item, deviceGroups, result, result.SelectMany(x => x.Codes).Contains(item.Code));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private async Task<Dictionary<string, List<Guid>>> GetAssignDeviceCodesInternalAsync(List<string> userNos)
|
||||
{
|
||||
var sw = Stopwatch.StartNew();
|
||||
var gainfos = await _dbContext.Queryable<GainfoEntity>().Where(x => userNos.Contains(x.No)).ToListAsync();
|
||||
if (!gainfos.Any())
|
||||
{
|
||||
return new Dictionary<string, List<Guid>>(0);
|
||||
return new Dictionary<string, List<Guid>>();
|
||||
}
|
||||
|
||||
var gainfoGroupRelationships = await _dbContext.Queryable<GainfoGroupRelationshipEntity>().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();
|
||||
@@ -390,7 +503,32 @@ public class DevicePermissionService : IDevicePermissionService
|
||||
result.Add(userNo, codes);
|
||||
}
|
||||
|
||||
await Console.Out.WriteLineAsync($"获取所有人员权限耗时:{sw.ElapsedMilliseconds}");
|
||||
return result;
|
||||
}
|
||||
|
||||
private async Task<Dictionary<string, List<UserTenantDeviceCodeDto>>> GetAssignDeviceCodesInternal1Async(List<string> userNos)
|
||||
{
|
||||
var gainfos = await _dbContext.Queryable<GainfoEntity>().Where(x => userNos.Contains(x.No)).ToListAsync();
|
||||
if (!gainfos.Any())
|
||||
{
|
||||
return new Dictionary<string, List<UserTenantDeviceCodeDto>>();
|
||||
}
|
||||
|
||||
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 result = new Dictionary<string, List<UserTenantDeviceCodeDto>>(gainfos.Count);
|
||||
|
||||
foreach (var gainfo in gainfos)
|
||||
{
|
||||
var codes = GetAssignDeviceCodesInternal1(gainfo, gainfoGroupRelationships, devicePermissions, deviceGroups, deviceRelationships);
|
||||
result.Add(gainfo.No, codes);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user