fix 并发开始配置
This commit is contained in:
6
Src/Dlhis.Basic/Device/Services/IParallelService.cs
Normal file
6
Src/Dlhis.Basic/Device/Services/IParallelService.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace Dlhis.Basic.Device.Services;
|
||||
|
||||
public interface IParallelService : IScopeDependency
|
||||
{
|
||||
public bool Enable { get; set; }
|
||||
}
|
||||
@@ -17,17 +17,18 @@ public class DeviceGroupService : IDeviceGroupService
|
||||
private readonly IDevicePermissionService _devicePermissionService;
|
||||
private readonly ICurrentUser _currentUser;
|
||||
private readonly ITenantApi _tenantApi;
|
||||
private readonly IParallelService _parallelService;
|
||||
|
||||
public DeviceGroupService(IDbContext dbContext,
|
||||
IDevicePermissionService devicePermissionService,
|
||||
ICurrentUser currentUser,
|
||||
ITenantApi tenantApi
|
||||
)
|
||||
ITenantApi tenantApi, IParallelService parallelService)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_devicePermissionService = devicePermissionService;
|
||||
_currentUser = currentUser;
|
||||
_tenantApi = tenantApi;
|
||||
_parallelService = parallelService;
|
||||
}
|
||||
|
||||
|
||||
@@ -104,6 +105,7 @@ public class DeviceGroupService : IDeviceGroupService
|
||||
await CheckNoNextGroupAsync(group.Code);
|
||||
await CheckGroupTypeMatchAsync(request, group);
|
||||
|
||||
_parallelService.Enable = true;
|
||||
//下发任务
|
||||
await _devicePermissionService.GenerateDeviceTasksAsync(async () =>
|
||||
{
|
||||
@@ -120,6 +122,7 @@ public class DeviceGroupService : IDeviceGroupService
|
||||
|
||||
await _dbContext.InsertAsync(entities);
|
||||
}, null);
|
||||
_parallelService.Enable = false;
|
||||
}
|
||||
|
||||
public async Task<List<DeviceUnderGroupResponse>> GetDevicesByGroupCodeAsync(Guid code)
|
||||
|
||||
@@ -29,14 +29,19 @@ public class DevicePermissionService : IDevicePermissionService
|
||||
private readonly IDbContext _dbContext;
|
||||
private readonly IMyDataPermissionService _myDataPermissionService;
|
||||
private readonly ITenantApi _tenantApi;
|
||||
private readonly IParallelService _parallelService;
|
||||
private readonly ICurrentUser _currentUser;
|
||||
|
||||
public DevicePermissionService(IDbContext dbContext, IMyDataPermissionService myDataPermissionService, ITenantApi tenantApi,
|
||||
public DevicePermissionService(IDbContext dbContext,
|
||||
IMyDataPermissionService myDataPermissionService,
|
||||
ITenantApi tenantApi,
|
||||
IParallelService parallelService,
|
||||
ICurrentUser currentUser)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_myDataPermissionService = myDataPermissionService;
|
||||
_tenantApi = tenantApi;
|
||||
_parallelService = parallelService;
|
||||
_currentUser = currentUser;
|
||||
}
|
||||
|
||||
@@ -172,7 +177,7 @@ public class DevicePermissionService : IDevicePermissionService
|
||||
|
||||
var device = await _dbContext.Queryable<DeviceEntity>().Where(x => x.UniqueNo == deviceNo).SingleAsync();
|
||||
device.ThrowIfNull("设备不存在");
|
||||
|
||||
|
||||
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
|
||||
using (var package = new ExcelPackage(file.OpenReadStream()))
|
||||
{
|
||||
@@ -203,7 +208,7 @@ public class DevicePermissionService : IDevicePermissionService
|
||||
}).ToList();
|
||||
|
||||
await _dbContext.InsertAsync(permissions);
|
||||
|
||||
|
||||
var tasks = permissions.Select(x => new DeviceTaskEntity
|
||||
{
|
||||
Type = DeviceTaskEnumType.Add,
|
||||
@@ -548,12 +553,18 @@ public class DevicePermissionService : IDevicePermissionService
|
||||
var permissions = await _dbContext.Queryable<DevicePermissionEntity>().ToListAsync();
|
||||
|
||||
//User
|
||||
var userNos = permissions.Where(x => x.AssignType == AssignTypeEnumType.User).Select(x => x.AssignValue).ToList();
|
||||
var userNos = permissions.Where(x => x.AssignType == AssignTypeEnumType.User)
|
||||
.Select(x => x.AssignValue)
|
||||
.Distinct()
|
||||
.ToList();
|
||||
|
||||
//Group
|
||||
var groupCode = permissions.Where(x => x.AssignType == AssignTypeEnumType.Group).Select(x => Guid.Parse(x.AssignValue)).ToList();
|
||||
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 => groupCode.Contains(x.GroupCode))
|
||||
.Where(x => groupCodes.Contains(x.GroupCode))
|
||||
.Select(x => x.UserNo)
|
||||
.ToListAsync();
|
||||
|
||||
@@ -707,49 +718,55 @@ public class DevicePermissionService : IDevicePermissionService
|
||||
var deviceRelationships = await _dbContext.Queryable<DeviceGroupRelationshipEntity>().ToListAsync();
|
||||
var departments = await _dbContext.Queryable<DepartmentEntity>().ToListAsync();
|
||||
|
||||
//var result = new ConcurrentDictionary<string, List<UserTenantDeviceCodeDto>>();
|
||||
|
||||
// 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);
|
||||
// });
|
||||
//
|
||||
// return result.ToDictionary(x => x.Key, x => x.Value);
|
||||
var result = new Dictionary<string, List<UserTenantDeviceCodeDto>>();
|
||||
foreach (var gainfo in gainfos)
|
||||
if (_parallelService.Enable)
|
||||
{
|
||||
var gainfoDto = new GainfoDto
|
||||
var result = new ConcurrentDictionary<string, List<UserTenantDeviceCodeDto>>();
|
||||
|
||||
Parallel.ForEach(gainfos, new ParallelOptions { MaxDegreeOfParallelism = 4 }, gainfo =>
|
||||
{
|
||||
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 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);
|
||||
var codes = GetAssignDeviceCodesInternal(gainfoDto, gainfoGroupRelationships, devicePermissions, deviceGroups, deviceRelationships, registries, devices, departments);
|
||||
result.TryAdd(gainfo.No, codes);
|
||||
});
|
||||
|
||||
return result.ToDictionary(x => x.Key, x => x.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
var result = new Dictionary<string, List<UserTenantDeviceCodeDto>>();
|
||||
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
|
||||
};
|
||||
|
||||
return result;
|
||||
var codes = GetAssignDeviceCodesInternal(gainfoDto, gainfoGroupRelationships, devicePermissions, deviceGroups, deviceRelationships, registries, devices, departments);
|
||||
result.Add(gainfo.No, codes);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Dlhis.Basic.Device.Services.Implements;
|
||||
|
||||
public class ParallelService : IParallelService
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public bool Enable { get; set; }
|
||||
}
|
||||
Reference in New Issue
Block a user