fix 调整为调度执行
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
using Dlhis.Basic.Device.Response;
|
||||
using Dlhis.Basic.Device.Services;
|
||||
using Kurisu.AspNetCore.DataAccess.SqlSugar.Aop;
|
||||
using Kurisu.AspNetCore.EventBus.Abstractions;
|
||||
using Kurisu.AspNetCore.Scope;
|
||||
using Kurisu.Core.DataAccess.Dto;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
@@ -218,5 +220,4 @@ public class DevicesController : ControllerBase
|
||||
{
|
||||
return await _deviceService.BulkUploadsAsync(request);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,170 +0,0 @@
|
||||
using System.Linq.Expressions;
|
||||
using Dlhis.Entity.Device.Entities;
|
||||
using Dlhis.Entity.Device.Enums;
|
||||
using Kurisu.AspNetCore.EventBus.Abstractions;
|
||||
using Kurisu.AspNetCore.EventBus.Abstractions.Handler;
|
||||
using Kurisu.AspNetCore.Utils.Extensions;
|
||||
using Kurisu.Core.DataAccess.Entity;
|
||||
using Kurisu.SqlSugar.Services;
|
||||
using SqlSugar;
|
||||
|
||||
namespace Dlhis.Basic.Device.EventBusHandler;
|
||||
|
||||
public class CancelDuplicationTaskHandler : IAsyncChannelHandler<CancelDuplicationTaskMessage>
|
||||
{
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
private readonly ILogger<CancelDuplicationTaskHandler> _logger;
|
||||
|
||||
public CancelDuplicationTaskHandler(IServiceProvider serviceProvider, ILogger<CancelDuplicationTaskHandler> logger)
|
||||
{
|
||||
_serviceProvider = serviceProvider;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task InvokeAsync(CancelDuplicationTaskMessage message)
|
||||
{
|
||||
_logger.LogInformation("抵消任务开始处理");
|
||||
|
||||
using (var scope = _serviceProvider.CreateScope())
|
||||
{
|
||||
var optionService = scope.ServiceProvider.GetService<ISqlSugarOptionsService>();
|
||||
var db = scope.ServiceProvider.GetService<IDbContext>();
|
||||
|
||||
db.Client.QueryFilter.ClearAndBackup<ITenantId>();
|
||||
optionService.IgnoreTenant = true;
|
||||
try
|
||||
{
|
||||
//人员下发任务相关
|
||||
var taskTypes = new List<DeviceTaskEnumType>() { DeviceTaskEnumType.Add, DeviceTaskEnumType.Delete, DeviceTaskEnumType.Update };
|
||||
|
||||
var deviceTasks = await db.Queryable<DeviceTaskEntity>()
|
||||
.Where(x => x.Status == DeviceTaskStatusEnumType.Wait)//没有被获取
|
||||
.Where(x => taskTypes.Contains(x.Type))
|
||||
.OrderBy(x => x.CreateTime, OrderByType.Desc)
|
||||
.ToListAsync();
|
||||
|
||||
var groupData = deviceTasks.GroupBy(x => new { x.TenantId, x.UserNo, x.DeviceUniqueNo });
|
||||
foreach (var deviceTaskPack in groupData)
|
||||
{
|
||||
var uniqueInfo = deviceTaskPack.Key;
|
||||
var data = deviceTaskPack.ToList();
|
||||
|
||||
|
||||
foreach (var item in data)
|
||||
{
|
||||
try
|
||||
{
|
||||
Expression<Func<DeviceTaskEntity, bool>> where = LinqExpressionExtension.False<DeviceTaskEntity>();
|
||||
var ignoreData = new List<IdVersion>();
|
||||
//最新一条更新人员信息
|
||||
if (item.Type == DeviceTaskEnumType.Update && item.Status == DeviceTaskStatusEnumType.Wait)
|
||||
{
|
||||
//获取过往的更新人员任务
|
||||
ignoreData = data.Where(x => x.Type == DeviceTaskEnumType.Update)
|
||||
.Where(x => x.Status == DeviceTaskStatusEnumType.Wait)
|
||||
.Where(x => x.Id != item.Id)
|
||||
.Where(x => item.CreateTime > x.CreateTime)
|
||||
.Select(x => new IdVersion { Id = x.Id, Version = x.Version }).ToList();
|
||||
}
|
||||
//最新一条删除人员信息
|
||||
else if (item.Type == DeviceTaskEnumType.Delete && item.Status == DeviceTaskStatusEnumType.Wait)
|
||||
{
|
||||
//存在成功下发的任务,此处可以直接把其他任务取消
|
||||
if (await db.Queryable<DeviceTaskEntity>().Where(x => x.UserNo == item.UserNo)
|
||||
.Where(x => x.DeviceUniqueNo == item.DeviceUniqueNo)
|
||||
.Where(x => x.TenantId == item.TenantId).Where(x => x.Status == DeviceTaskStatusEnumType.Success).AnyAsync())
|
||||
{
|
||||
ignoreData.AddRange(data.Where(x => x.Type == DeviceTaskEnumType.Add
|
||||
|| x.Type == DeviceTaskEnumType.Delete
|
||||
|| x.Type == DeviceTaskEnumType.Update)
|
||||
.Where(x => x.Status == DeviceTaskStatusEnumType.Wait)
|
||||
.Where(x => x.Id != item.Id)
|
||||
.Where(x => item.CreateTime > x.CreateTime)
|
||||
.Select(x => new IdVersion { Id = x.Id, Version = x.Version })
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
var currentIds = new List<long> { item.Id };
|
||||
var beforeTask = data.Where(x => x.Type == DeviceTaskEnumType.Add || x.Type == DeviceTaskEnumType.Update)
|
||||
.Where(x => !currentIds.Contains(x.Id))
|
||||
.Where(x => x.Status == DeviceTaskStatusEnumType.Wait)
|
||||
.Where(x => item.CreateTime > x.CreateTime)
|
||||
.FirstOrDefault();
|
||||
|
||||
//逻辑正确,最终beforeTask.type == DeviceTaskEnumType.Add
|
||||
while (beforeTask != null && beforeTask.Type == DeviceTaskEnumType.Update)
|
||||
{
|
||||
ignoreData.Add(new IdVersion() { Id = beforeTask.Id, Version = beforeTask.Version });
|
||||
currentIds.Add(beforeTask.Id);
|
||||
|
||||
beforeTask = data.Where(x => x.Type == DeviceTaskEnumType.Add || x.Type == DeviceTaskEnumType.Update)
|
||||
.Where(x => !currentIds.Contains(x.Id))
|
||||
.Where(x => x.Status == DeviceTaskStatusEnumType.Wait)
|
||||
.Where(x => item.CreateTime > x.CreateTime)
|
||||
.FirstOrDefault();
|
||||
}
|
||||
|
||||
if (beforeTask != null)
|
||||
{
|
||||
ignoreData.Add(new IdVersion() { Id = beforeTask.Id, Version = beforeTask.Version });
|
||||
ignoreData.Add(new IdVersion() { Id = item.Id, Version = item.Version });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (ignoreData.Any())
|
||||
{
|
||||
ignoreData.ForEach(p =>
|
||||
{
|
||||
where = where.Or(x => x.Id == p.Id && x.Version == p.Version && x.Status == DeviceTaskStatusEnumType.Wait);
|
||||
});
|
||||
|
||||
await db.Updateable<DeviceTaskEntity>()
|
||||
.SetColumns(x => x.Version == x.Version + 1)
|
||||
.SetColumns(x => x.Status == DeviceTaskStatusEnumType.Cancel)
|
||||
.SetColumns(x => x.CancellationId == item.Id)
|
||||
.SetColumns(x => x.ModifiedTime == DateTime.Now)
|
||||
.Where(where)
|
||||
.ExecuteCommandAsync();
|
||||
|
||||
data.ForEach(x =>
|
||||
{
|
||||
if (ignoreData.Any(p => p.Id == x.Id))
|
||||
x.Status = DeviceTaskStatusEnumType.Cancel;
|
||||
});
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "抵消处理异常:{message}", ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "抵消处理异常:{message}", ex.Message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
db.Client.QueryFilter.Restore();
|
||||
optionService.IgnoreTenant = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
_logger.LogInformation("抵消任务处理完成");
|
||||
}
|
||||
}
|
||||
|
||||
public class CancelDuplicationTaskMessage : IAsyncChannelMessage
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public class IdVersion
|
||||
{
|
||||
public long Id { get; set; }
|
||||
public int Version { get; set; }
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
using Dlhis.Basic.Device.Dtos;
|
||||
using Dlhis.Basic.Device.EventBusHandler;
|
||||
using Dlhis.Basic.Device.Request;
|
||||
using Dlhis.Basic.Device.Response;
|
||||
using Dlhis.Entity.Basic.Entity;
|
||||
@@ -7,7 +6,6 @@ using Dlhis.Entity.Device.Entities;
|
||||
using Dlhis.Entity.Device.Enums;
|
||||
using Dlhis.Entity.Sys.Entities;
|
||||
using Dlhis.Entity.Sys.Enums;
|
||||
using Kurisu.AspNetCore.EventBus.Abstractions;
|
||||
using Kurisu.AspNetCore.Utils.Extensions;
|
||||
using Kurisu.Core.DataAccess.Dto;
|
||||
using Kurisu.Core.DataAccess.Entity;
|
||||
@@ -20,13 +18,11 @@ public class DevicePermissionService : IDevicePermissionService
|
||||
{
|
||||
private readonly IDbContext _dbContext;
|
||||
private readonly ICurrentUser _currentUser;
|
||||
private readonly IEventBus _eventBus;
|
||||
|
||||
public DevicePermissionService(IDbContext dbContext, ICurrentUser currentUser, IEventBus eventBus)
|
||||
public DevicePermissionService(IDbContext dbContext, ICurrentUser currentUser)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_currentUser = currentUser;
|
||||
_eventBus = eventBus;
|
||||
}
|
||||
|
||||
public async Task AssignAsync(DevicePermissionSaveReuqest reuqest)
|
||||
@@ -158,8 +154,6 @@ public class DevicePermissionService : IDevicePermissionService
|
||||
{
|
||||
await _dbContext.InsertAsync(waitTasks);
|
||||
}
|
||||
|
||||
await _eventBus.PublishAsync(new CancelDuplicationTaskMessage());
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
using Kurisu.AspNetCore.EventBus.Extensions;
|
||||
using Kurisu.AspNetCore.Startup;
|
||||
using Kurisu.AspNetCore.Utils.Extensions;
|
||||
using Kurisu.Startup;
|
||||
@@ -23,6 +22,5 @@ public class Startup : DefaultStartup
|
||||
{
|
||||
base.ConfigureServices(services);
|
||||
services.AddRedis();
|
||||
services.AddEventBus();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user