refactor 优化,注释

This commit is contained in:
2025-01-10 16:19:49 +08:00
parent fe6667f602
commit 41c560e8c7
16 changed files with 172 additions and 298 deletions

View File

@@ -2,7 +2,6 @@ using System.Collections.Concurrent;
using Dlhis.Basic.DataPermissions;
using Dlhis.Basic.Device.Dtos;
using Dlhis.Basic.Device.Request;
using Dlhis.Basic.Device.Response;
using Dlhis.Basic.Organization.Dto;
using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Basic.Enums;
@@ -11,7 +10,6 @@ using Dlhis.Entity.Device.Enums;
using Dlhis.Entity.Sys.Entities;
using Dlhis.Entity.Sys.Enums;
using Kurisu.AspNetCore.Authentication.Abstractions;
using Kurisu.AspNetCore.DataAccess.Entity;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
using Kurisu.AspNetCore.Utils.Extensions;
@@ -34,8 +32,9 @@ public class DevicePermissionAnalyzeService : IDevicePermissionAnalyzeService
public async Task<List<DevicePermissionEntity>> GetDevicePermissionsAsync(DevicePermissionSaveRequest request)
{
var currentTenantId = await GetCurrentFactoryAsync(request);
var currentTenantId = await GetCurrentFactoryAsync(request.UniqueType, request.Codes);
//获取当前查询的配置
var setting = _dbContext.GetQueryableSetting();
var query = _dbContext.Queryable<DevicePermissionEntity>().Where(x => x.AssignType == request.AssignType
@@ -43,6 +42,8 @@ public class DevicePermissionAnalyzeService : IDevicePermissionAnalyzeService
&& x.UniqueType == request.UniqueType
&& x.Type == request.Type
&& x.TenantId == currentTenantId);
//根据数据权限进行过滤
if (setting.GetEnableDataPermission<DevicePermissionEntity>())
{
(bool enableDeviceFilter, List<Guid> deviceCodes) = _myDataPermissionService.GetMyDeviceCodes();
@@ -54,102 +55,32 @@ public class DevicePermissionAnalyzeService : IDevicePermissionAnalyzeService
}
return await query.ToListAsync();
}
private async Task<string> GetCurrentFactoryAsync(DevicePermissionSaveRequest request)
{
var currentTenantId = _currentUser.GetStringTenantId();
var r = request.UniqueType switch
//获取当前的操作人的基地
async Task<string> GetCurrentFactoryAsync(DeviceAssignmentTypeEnumType uniqueType, List<Guid> codes)
{
DeviceAssignmentTypeEnumType.Device => await _dbContext.Queryable<DeviceEntity>().Where(x => request.Codes.Contains(x.Code)).Where(x => x.TenantId != currentTenantId).AnyAsync(),
DeviceAssignmentTypeEnumType.Group => await _dbContext.Queryable<DeviceGroupEntity>().Where(x => request.Codes.Contains(x.Code)).Where(x => x.TenantId != currentTenantId).AnyAsync(),
_ => false
};
var internalCurrentTenantId = _currentUser.GetStringTenantId();
r.ThrowIfTrue("存在设备或者设备组不属于当前厂区,操作失败!");
return currentTenantId;
}
public async Task<DevicePermissionAssignResponse> GenerateDeviceTasksAsync(Func<Task> func, List<string> userNos, string source)
{
var before = await GetUserAssignDeviceCodesInTenantAsync(userNos);
//业务处理
_dbContext.Client.QueryFilter.Restore();
await func();
_dbContext.Client.QueryFilter.ClearAndBackup<ITenantId>();
var current = await GetUserAssignDeviceCodesInTenantAsync(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)
{
var beforeTenantDeviceCodes = before[userNo].ToList();
var currentTenantDeviceCodes = current[userNo].ToList();
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 r = uniqueType switch
{
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>();
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
};
//被删除
var deletedItems = beforeCodes.Where(x => !currentCodes.Contains(x)).ToList();
//新增
var addItems = currentCodes.Where(x => !beforeCodes.Contains(x)).ToList();
r.ThrowIfTrue("存在设备或者设备组不属于当前厂区,操作失败!");
if (!deletedItems.Any() && !addItems.Any())
{
continue;
}
var deviceCodes = devices.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();
//下发任务创建
deletedItems.ForEach(x => waitDeleteTasks.Add(new DeviceTaskEntity
{
DeviceUniqueNo = devices.First(item => item.Code == x).UniqueNo,
Status = DeviceTaskStatusEnumType.Wait,
Type = DeviceTaskEnumType.Delete,
DeviceType = devices.First(item => item.Code == x).Type,
UserNo = userNo,
Version = 0,
TenantId = tenant,
Source = source
}));
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 = source
}));
}
return internalCurrentTenantId;
}
}
await ChunkInsertAsync(waitAddTasks);
await ChunkInsertAsync(waitDeleteTasks);
return new DevicePermissionAssignResponse(waitAddTasks.Count, waitDeleteTasks.Count);
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();
}
@@ -344,33 +275,6 @@ public class DevicePermissionAnalyzeService : IDevicePermissionAnalyzeService
return r;
}
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<UserTenantDeviceCodeDto>>> GetUserAssignDeviceCodesInTenantAsync(
List<string> userNos,
DeviceAssignmentTypeEnumType? ignoreUniqueType = null,
string ignoreAssignValue = null, AssignTypeEnumType? ignoreAssignType = null)
{
var result = await GetUserAssignDeviceCodesWithTypeInTenantAsync(userNos, ignoreUniqueType, ignoreAssignValue, ignoreAssignType);
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(),
};
}).ToList());
}
public async Task<List<Guid>> GetAssignmentsAsync(DeviceGetAssignmentsRequest request)
{
var r = await GetDevicePermissionsAsync(new DevicePermissionSaveRequest
@@ -405,10 +309,4 @@ public class DevicePermissionAnalyzeService : IDevicePermissionAnalyzeService
Factory = x.Factory
}).ToListAsync();
}
public async Task<List<Guid>> GetAssignDeviceCodesAsync(string userNo)
{
var result = await GetUserAssignDeviceCodesInTenantAsync(new List<string> { userNo });
return result.First().Value.SelectMany(x => x.Codes).ToList();
}
}