136 lines
6.2 KiB
C#
136 lines
6.2 KiB
C#
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; }
|
|
}
|