fix 优化任务状态

This commit is contained in:
2024-03-14 20:36:14 +08:00
parent efbb72105c
commit 7a6bedf3f4
6 changed files with 157 additions and 2 deletions

View File

@@ -0,0 +1,135 @@
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)
.Select(x => new IdVersion { Id = x.Id, Version = x.Version }).ToList();
ignoreData.ForEach(p =>
{
where = where.Or(x => x.Id == p.Id && x.Version == p.Version && x.Status == DeviceTaskStatusEnumType.Wait);
});
}
else if (item.Type == DeviceTaskEnumType.Delete && item.Status == DeviceTaskStatusEnumType.Wait)
{
ignoreData = data.Where(x => (x.Type == DeviceTaskEnumType.Add || x.Type == DeviceTaskEnumType.Delete))
.Where(x => x.Status == DeviceTaskStatusEnumType.Wait)
.Where(x => x.Id != item.Id)
.Select(x => new IdVersion { Id = x.Id, Version = x.Version }).ToList();
ignoreData.ForEach(p =>
{
where = where.Or(x => x.Id == p.Id && x.Version == p.Version && x.Status == DeviceTaskStatusEnumType.Wait);
});
}
if (ignoreData.Any())
{
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; }
}

View File

@@ -1,4 +1,5 @@
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;
@@ -6,6 +7,7 @@ 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;
@@ -18,11 +20,13 @@ public class DevicePermissionService : IDevicePermissionService
{
private readonly IDbContext _dbContext;
private readonly ICurrentUser _currentUser;
private readonly IEventBus _eventBus;
public DevicePermissionService(IDbContext dbContext, ICurrentUser currentUser)
public DevicePermissionService(IDbContext dbContext, ICurrentUser currentUser, IEventBus eventBus)
{
_dbContext = dbContext;
_currentUser = currentUser;
_eventBus = eventBus;
}
public async Task AssignAsync(DevicePermissionSaveReuqest reuqest)
@@ -153,6 +157,7 @@ public class DevicePermissionService : IDevicePermissionService
if (waitTasks.Any())
{
await _dbContext.InsertAsync(waitTasks);
await _eventBus.PublishAsync(new CancelDuplicationTaskMessage());
}
}
finally

View File

@@ -17,7 +17,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Kurisu.AspNetCore" Version="0.6.1" />
<PackageReference Include="Kurisu.AspNetCore" Version="0.7.1" />
</ItemGroup>
<ItemGroup>

View File

@@ -1,3 +1,4 @@
using Kurisu.AspNetCore.EventBus.Extensions;
using Kurisu.AspNetCore.Startup;
using Kurisu.AspNetCore.Utils.Extensions;
using Kurisu.Startup;
@@ -22,5 +23,6 @@ public class Startup : DefaultStartup
{
base.ConfigureServices(services);
services.AddRedis();
services.AddEventBus();
}
}

View File

@@ -47,5 +47,11 @@ public class DeviceTaskEntity : SugarBaseEntity, ITenantId, IVersion
[SugarColumn(ColumnDataType = "longtext")]
public string ResultMessage { get; set; }
/// <summary>
/// 取消id
/// </summary>
public long CancellationId { get; set; }
public string TenantId { get; set; }
}

View File

@@ -26,6 +26,13 @@ public enum DeviceTaskStatusEnumType
[Description("下发完成")]
Success = 10,
/// <summary>
/// 取消
/// </summary>
[Description("取消")]
Cancel = 95,
/// <summary>
/// 异常
/// </summary>