fix 优化任务分配速度
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using Dlhis.Basic.Api.Dto;
|
||||
using Dlhis.Basic.Api.Request;
|
||||
using Kurisu.RemoteCall.Attributes;
|
||||
using Kurisu.RemoteCall.Default;
|
||||
|
||||
@@ -25,4 +26,13 @@ public interface IDevicePermissionApi
|
||||
/// <returns></returns>
|
||||
[Post("/api/devicepermissions/re-permission")]
|
||||
Task<DefaultApiResult<int>> RePermissionUsersInfoAsync(PermissionUsersInfoDto beforeDto);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 任务生成
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <returns></returns>
|
||||
[Post("/api/devicepermissions/assignment-generate")]
|
||||
Task<DefaultApiResult> GeneratePermissionsAsync(GeneratePermissionIdRequest request);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Dlhis.Basic.Api.Request
|
||||
{
|
||||
public class GeneratePermissionIdRequest
|
||||
{
|
||||
public long Id { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -32,6 +32,16 @@ public class DevicePermissionsController : ControllerBase
|
||||
await _devicePermissionService.AssignAsync(request);
|
||||
}
|
||||
|
||||
[AllowAnonymous]
|
||||
[HttpPost("assignment-generate")]
|
||||
[Transactional]
|
||||
[IgnoreTenant]
|
||||
public async Task GeneratePermissionsAsync(GeneratePermissionIdRequest request)
|
||||
{
|
||||
await _devicePermissionService.GeneratePermissionsAsync(request.Id);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 当前类型的权限分配
|
||||
/// </summary>
|
||||
@@ -81,4 +91,4 @@ public class DevicePermissionsController : ControllerBase
|
||||
{
|
||||
return await _devicePermissionService.ReGeneratePermissionUsersInfoAsync(beforeDto);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Dlhis.Basic.Device.Request
|
||||
{
|
||||
public class GeneratePermissionIdRequest
|
||||
{
|
||||
public long Id { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -10,9 +10,12 @@ public interface IDevicePermissionService : IScopeDependency
|
||||
/// <summary>
|
||||
/// 保存设备权限
|
||||
/// </summary>
|
||||
/// <param name="reuqest"></param>
|
||||
/// <param name="request"></param>
|
||||
/// <returns></returns>
|
||||
Task AssignAsync(DevicePermissionSaveRequest reuqest);
|
||||
Task AssignAsync(DevicePermissionSaveRequest request);
|
||||
|
||||
|
||||
Task GeneratePermissionsAsync(long id);
|
||||
|
||||
/// <summary>
|
||||
/// 获取分配的权限
|
||||
|
||||
@@ -5,11 +5,14 @@ using Dlhis.Entity.Device.Entities;
|
||||
using Dlhis.Entity.Device.Enums;
|
||||
using Dlhis.Entity.Sys.Entities;
|
||||
using Dlhis.Entity.Sys.Enums;
|
||||
using Dlhis.Scheduled.Api;
|
||||
using Kurisu.AspNetCore.Authentication.Abstractions;
|
||||
using Kurisu.AspNetCore.DataAccess.Entity;
|
||||
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
||||
using Kurisu.AspNetCore.Utils;
|
||||
using Kurisu.AspNetCore.Utils.Extensions;
|
||||
using SqlSugar;
|
||||
using GeneratePermissionIdRequest = Dlhis.Scheduled.Api.Request.GeneratePermissionIdRequest;
|
||||
|
||||
namespace Dlhis.Basic.Device.Services.Implements;
|
||||
|
||||
@@ -17,44 +20,85 @@ public class DevicePermissionService : IDevicePermissionService
|
||||
{
|
||||
private readonly IDbContext _dbContext;
|
||||
private readonly ICurrentUser _currentUser;
|
||||
private readonly RedisCache _redisCache;
|
||||
private readonly IHangfireApi _hangfireApi;
|
||||
|
||||
public DevicePermissionService(IDbContext dbContext, ICurrentUser currentUser)
|
||||
public DevicePermissionService(IDbContext dbContext, ICurrentUser currentUser, RedisCache redisCache, IHangfireApi hangfireApi)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_currentUser = currentUser;
|
||||
_redisCache = redisCache;
|
||||
_hangfireApi = hangfireApi;
|
||||
}
|
||||
|
||||
public async Task AssignAsync(DevicePermissionSaveRequest request)
|
||||
{
|
||||
var currentTenantId = _currentUser.GetStringTenantId();
|
||||
|
||||
await using var handler = await _redisCache.LockAsync($"permission:generate:{currentTenantId}");
|
||||
handler.Acquired.ThrowIfFalse("请勿频繁分配设备权限");
|
||||
|
||||
var tenantId = request.AssignType == AssignTypeEnumType.Department ? request.TenantId : _currentUser.GetStringTenantId();
|
||||
request.TenantId = tenantId;
|
||||
|
||||
await GenerateDeviceTasksAsync(async () =>
|
||||
var json = request.ToJson();
|
||||
var id = await _dbContext.InsertReturnIdentityAsync(new DevicePermissionAssignmentEntity
|
||||
{
|
||||
//删除当前匹配的基地权限
|
||||
var p = await _dbContext.Queryable<DevicePermissionEntity>()
|
||||
.Where(x => x.AssignType == request.AssignType)
|
||||
.Where(x => x.AssignValue == request.AssignValue)
|
||||
.Where(x => x.UniqueType == request.UniqueType)
|
||||
.Where(x => x.TenantId == tenantId)
|
||||
.ToListAsync();
|
||||
Assignment = json,
|
||||
IsHandler = false,
|
||||
Result = null,
|
||||
TenantId = currentTenantId
|
||||
});
|
||||
|
||||
await _dbContext.DeleteReallyAsync(p);
|
||||
await _hangfireApi.GeneratePermissionsAsync(new GeneratePermissionIdRequest()
|
||||
{
|
||||
Id = id
|
||||
});
|
||||
}
|
||||
|
||||
//保存配置的权限
|
||||
if (request.Codes.IsPresent())
|
||||
public async Task GeneratePermissionsAsync(long id)
|
||||
{
|
||||
var assignment = await _dbContext.Queryable<DevicePermissionAssignmentEntity>().SingleAsync(x => x.Id == id);
|
||||
if (assignment == null) return;
|
||||
if (assignment.IsHandler) return;
|
||||
|
||||
try
|
||||
{
|
||||
var request = assignment.Assignment.ToObject<DevicePermissionSaveRequest>();
|
||||
|
||||
await GenerateDeviceTasksAsync(async () =>
|
||||
{
|
||||
var devicePermissions = request.Codes.Select(code => new DevicePermissionEntity
|
||||
{
|
||||
AssignType = request.AssignType,
|
||||
AssignValue = request.AssignValue,
|
||||
UniqueType = request.UniqueType,
|
||||
UniqueValue = code,
|
||||
TenantId = tenantId
|
||||
}).ToList();
|
||||
//删除当前匹配的基地权限
|
||||
var p = await _dbContext.Queryable<DevicePermissionEntity>()
|
||||
.Where(x => x.AssignType == request.AssignType)
|
||||
.Where(x => x.AssignValue == request.AssignValue)
|
||||
.Where(x => x.UniqueType == request.UniqueType)
|
||||
.Where(x => x.TenantId == request.TenantId)
|
||||
.ToListAsync();
|
||||
|
||||
await _dbContext.InsertAsync(devicePermissions);
|
||||
}
|
||||
}, null);
|
||||
await _dbContext.DeleteReallyAsync(p);
|
||||
|
||||
//保存配置的权限
|
||||
if (request.Codes.IsPresent())
|
||||
{
|
||||
var devicePermissions = request.Codes.Select(code => new DevicePermissionEntity
|
||||
{
|
||||
AssignType = request.AssignType,
|
||||
AssignValue = request.AssignValue,
|
||||
UniqueType = request.UniqueType,
|
||||
UniqueValue = code,
|
||||
TenantId = request.TenantId
|
||||
}).ToList();
|
||||
|
||||
await _dbContext.InsertAsync(devicePermissions);
|
||||
}
|
||||
}, null);
|
||||
}
|
||||
finally
|
||||
{
|
||||
assignment.IsHandler = true;
|
||||
await _dbContext.UpdateAsync(assignment);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<List<Guid>> GetAssignmentsAsync(DeviceAssignmentTypeEnumType uniqueType, AssignTypeEnumType assignType, string assignValue)
|
||||
|
||||
@@ -54,20 +54,22 @@ public class SqlSugarPack : BaseAppPack
|
||||
public override void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||
{
|
||||
var isGenerate = Configuration.GetSection("SqlSugarOptions").GetValue<bool>("Generate");
|
||||
if (isGenerate)
|
||||
if (!isGenerate)
|
||||
{
|
||||
using var scope = app.ApplicationServices.CreateScope();
|
||||
var db = scope.ServiceProvider.GetService<IDbContext>();
|
||||
// var tables = App.ActiveTyps.Where(x => x.IsDefined(typeof(SugarTable), false))
|
||||
// .Where(x => !x.IsDefined(typeof(SkipScanAttribute), false))
|
||||
// .ToArray();
|
||||
//
|
||||
// db.Client.DbMaintenance.CreateDatabase();
|
||||
// db.Client.CodeFirst.InitTables(tables);
|
||||
//
|
||||
// var faceDb = db.ChangeDb("face");
|
||||
// faceDb.Client.DbMaintenance.CreateDatabase();
|
||||
// faceDb.Client.CodeFirst.InitTables<GainfoFaceEntity>();
|
||||
return;
|
||||
}
|
||||
|
||||
using var scope = app.ApplicationServices.CreateScope();
|
||||
var db = scope.ServiceProvider.GetService<IDbContext>();
|
||||
var tables = App.ActiveTypes.Where(x => x.IsDefined(typeof(SugarTable), false))
|
||||
.Where(x => !x.IsDefined(typeof(SkipScanAttribute), false))
|
||||
.ToArray();
|
||||
|
||||
db.Client.DbMaintenance.CreateDatabase();
|
||||
db.Client.CodeFirst.InitTables(tables);
|
||||
|
||||
var faceDb = db.ChangeDb("face");
|
||||
faceDb.Client.DbMaintenance.CreateDatabase();
|
||||
faceDb.Client.CodeFirst.InitTables<GainfoFaceEntity>();
|
||||
}
|
||||
}
|
||||
32
Src/Dlhis.Entity/Basic/Entity/DevicePermissionEntity.cs
Normal file
32
Src/Dlhis.Entity/Basic/Entity/DevicePermissionEntity.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using Kurisu.AspNetCore.DataAccess.Entity;
|
||||
using Kurisu.AspNetCore.DataAccess.SqlSugar;
|
||||
using SqlSugar;
|
||||
|
||||
namespace Dlhis.Entity.Basic.Entity;
|
||||
|
||||
/// <summary>
|
||||
/// 设备权限分配
|
||||
/// </summary>
|
||||
[SugarTable(TableName = "DevicePermissionAssignment", TableDescription = "设备权限分配")]
|
||||
public class DevicePermissionAssignmentEntity : SugarBaseEntity, ITenantId
|
||||
{
|
||||
/// <summary>
|
||||
/// 分配json
|
||||
/// </summary>
|
||||
[SugarColumn(IsNullable = false, ColumnDataType = "varchar(1000)", ColumnDescription = "分配json")]
|
||||
public string Assignment { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否处理
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDataType = "bit(1)", ColumnDescription = "是否处理")]
|
||||
public bool IsHandler { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 处理结果
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "处理结果")]
|
||||
public string Result { get; set; }
|
||||
|
||||
public string TenantId { get; set; }
|
||||
}
|
||||
@@ -4,6 +4,7 @@ using Kurisu.RemoteCall.Default;
|
||||
|
||||
namespace Dlhis.Scheduled.Api;
|
||||
|
||||
[RequestLog]
|
||||
[EnableRemoteClient(Name = "hangfire-service", BaseUrl = "${InternalRequest:ScheduledRequestUrl}")]
|
||||
public interface IHangfireApi
|
||||
{
|
||||
@@ -14,4 +15,8 @@ public interface IHangfireApi
|
||||
/// <returns></returns>
|
||||
[Post("api/hangfire/sap/person/no")]
|
||||
Task<DefaultApiResult> SyncSapPersonAsync(SapNoRequest request);
|
||||
}
|
||||
|
||||
[Auth]
|
||||
[Post("api/hangfire/assignment-generate")]
|
||||
Task<DefaultApiResult> GeneratePermissionsAsync(GeneratePermissionIdRequest request);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Dlhis.Scheduled.Api.Request
|
||||
{
|
||||
public class GeneratePermissionIdRequest
|
||||
{
|
||||
public long Id { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using Dlhis.ScheduledTask.Common.Request;
|
||||
using Dlhis.Basic.Api.Request;
|
||||
using Dlhis.ScheduledTask.Common.Request;
|
||||
|
||||
namespace Dlhis.ScheduledTask.Common.Services;
|
||||
|
||||
@@ -20,4 +21,7 @@ public interface IHangfireService : IScopeDependency
|
||||
/// <param name="request"></param>
|
||||
/// <returns></returns>
|
||||
Task CreateSapWerkSyncBackgroundJobAsync(SapNoRequest request);
|
||||
}
|
||||
|
||||
|
||||
void GeneratePermissions(GeneratePermissionIdRequest request);
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
using Dlhis.Entity.Basic.Entity;
|
||||
using Dlhis.Basic.Api;
|
||||
using Dlhis.Basic.Api.Request;
|
||||
using Dlhis.Entity.Basic.Entity;
|
||||
using Dlhis.ScheduledTask.Common.Request;
|
||||
using Hangfire;
|
||||
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
||||
@@ -33,7 +35,7 @@ public class HangfireService : IHangfireService
|
||||
{
|
||||
var jobKey = $"hangfire:background:werk:{request.Code}";
|
||||
|
||||
using var handler = await _redisCache.LockAsync(jobKey);
|
||||
await using var handler = await _redisCache.LockAsync(jobKey);
|
||||
if (handler.Acquired)
|
||||
{
|
||||
var werkInfo = await _dbContext.Queryable<WerkEntity>().Where(x => x.Werk == request.Code).SingleAsync();
|
||||
@@ -44,4 +46,10 @@ public class HangfireService : IHangfireService
|
||||
BackgroundJob.Enqueue<IGainfoUpdateService>(x => x.TodoByWerkAsync(request.Code));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void GeneratePermissions(GeneratePermissionIdRequest request)
|
||||
{
|
||||
BackgroundJob.Enqueue<IDevicePermissionApi>(x => x.GeneratePermissionsAsync(request));
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
using Dlhis.ScheduledTask.Common.Request;
|
||||
using Dlhis.Basic.Api;
|
||||
using Dlhis.Basic.Api.Request;
|
||||
using Dlhis.ScheduledTask.Common.Request;
|
||||
using Dlhis.ScheduledTask.Common.Services;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
@@ -42,4 +44,11 @@ public class HangfireController : ControllerBase
|
||||
{
|
||||
await _hangfireService.CreateSapWerkSyncBackgroundJobAsync(request);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[HttpPost("assignment-generate")]
|
||||
public void GeneratePermissionsAsync(GeneratePermissionIdRequest request)
|
||||
{
|
||||
_hangfireService.GeneratePermissions(request);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user