fix 时段变更生成下发任务

This commit is contained in:
2024-07-17 18:37:33 +08:00
parent a981baa296
commit 3c9e3261c8
7 changed files with 235 additions and 56 deletions

View File

@@ -1,6 +1,8 @@
using Dlhis.Basic.Device.Request;
using System.ComponentModel.DataAnnotations;
using Dlhis.Basic.Device.Request;
using Dlhis.Basic.Device.Response;
using Dlhis.Basic.Device.Services;
using Dlhis.Entity.Device.Enums;
using Kurisu.AspNetCore.DataAccess.Dto;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
@@ -21,16 +23,40 @@ public class DeviceTimePeriodsController : ControllerBase
{
_deviceTimePeriodService = deviceTimePeriodService;
}
/// <summary>
/// 时段码分页
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
[HttpGet("page")]
public async Task<Pagination<DeviceTimePeriodPageResponse>> GetPageAsync([FromQuery] DeviceTimePeriodPageRequest request)
public async Task<Pagination<DeviceTimePeriodResponse>> GetPageAsync([FromQuery] DeviceTimePeriodPageRequest request)
{
return await _deviceTimePeriodService.GetPageAsync(request);
}
/// <summary>
/// 保存时段码设置
/// </summary>
/// <param name="request"></param>
[HttpPost("save")]
public async Task SaveAsync(DeviceTimePeriodSaveRequest request)
{
await _deviceTimePeriodService.SaveAsync(request);
}
/// <summary>
/// 获取类型时段列表
/// </summary>
/// <param name="assignType"></param>
/// <param name="value"></param>
/// <returns></returns>
[HttpGet("list")]
public async Task<List<DeviceTimePeriodResponse>> GetListAsync(
[EnumDataType(typeof(DeviceTimePeriodAssignEnumType))]
DeviceTimePeriodAssignEnumType assignType,
[Required] Guid value)
{
return await _deviceTimePeriodService.GetListAsync(assignType, value);
}
}

View File

@@ -0,0 +1,11 @@
namespace Dlhis.Basic.Device.Dtos;
public class DeviceTimePeriodDto
{
public int CardType { get; set; }
public TimeOnly OnStart { get; set; }
public TimeOnly OnEnd { get; set; }
public TimeOnly OffStart { get; set; }
public TimeOnly OffEnd { get; set; }
}

View File

@@ -2,7 +2,7 @@ using Dlhis.Entity.Device.Enums;
namespace Dlhis.Basic.Device.Response;
public class DeviceTimePeriodPageResponse
public class DeviceTimePeriodResponse
{
public long Id { get; set; }

View File

@@ -1,12 +1,17 @@
using Dlhis.Basic.Device.Request;
using Dlhis.Basic.Device.Response;
using Dlhis.Entity.Device.Enums;
using Kurisu.AspNetCore.DataAccess.Dto;
namespace Dlhis.Basic.Device.Services;
public interface IDeviceTimePeriodService : IScopeDependency
{
Task<Pagination<DeviceTimePeriodPageResponse>> GetPageAsync(DeviceTimePeriodPageRequest request);
Task<Pagination<DeviceTimePeriodResponse>> GetPageAsync(DeviceTimePeriodPageRequest request);
Task SaveAsync(DeviceTimePeriodSaveRequest request);
Task<List<DeviceTimePeriodResponse>> GetListAsync(DeviceTimePeriodAssignEnumType assignType, Guid value);
Task GenerateDeviceTimePeriodTasksAsync(Func<Task> func);
}

View File

@@ -22,12 +22,14 @@ public class DeviceService : IDeviceService
private readonly IDbContext _dbContext;
private readonly ICurrentUser _currentUser;
private readonly ITenantApi _tenantApi;
private readonly IDeviceTimePeriodService _deviceTimePeriodService;
public DeviceService(IDbContext dbContext, ICurrentUser currentUser, ITenantApi tenantApi)
public DeviceService(IDbContext dbContext, ICurrentUser currentUser, ITenantApi tenantApi, IDeviceTimePeriodService deviceTimePeriodService)
{
_dbContext = dbContext;
_currentUser = currentUser;
_tenantApi = tenantApi;
_deviceTimePeriodService = deviceTimePeriodService;
}
public async Task<Pagination<DevicePageResponse>> GetPageAsync(DevicePageRequest request)
@@ -63,50 +65,54 @@ public class DeviceService : IDeviceService
await CheckUniqueNoExistsAsync(request);
await CheckTemplateExistsAsync(request.TemplateCode);
await CheckTemplateFieldAsync(request.TemplateCode, request.ConnectionInfo);
DeviceEntity entity;
if (!request.Id.HasValue)
await _deviceTimePeriodService.GenerateDeviceTimePeriodTasksAsync(async () =>
{
entity = request.Adapt<DeviceEntity>();
entity.UniqueNo = request.UniqueNo;
entity.Code = Guid.NewGuid();
await _dbContext.InsertAsync(entity);
//同时保存注册信息
var registry = await _dbContext.Queryable<DeviceRegistryEntity>().Where(x => x.DeviceUniqueNo == request.UniqueNo).FirstAsync();
if (registry == null)
DeviceEntity entity;
if (!request.Id.HasValue)
{
registry = new DeviceRegistryEntity
entity = request.Adapt<DeviceEntity>();
entity.UniqueNo = request.UniqueNo;
entity.Code = Guid.NewGuid();
await _dbContext.InsertAsync(entity);
//同时保存注册信息
var registry = await _dbContext.Queryable<DeviceRegistryEntity>().Where(x => x.DeviceUniqueNo == request.UniqueNo).FirstAsync();
if (registry == null)
{
DeviceUniqueNo = request.UniqueNo,
LastHeartbeatTime = DateTime.Now,
RegisterTime = DateTime.Now,
BusinessPlaceCode = null,
};
registry = new DeviceRegistryEntity
{
DeviceUniqueNo = request.UniqueNo,
LastHeartbeatTime = DateTime.Now,
RegisterTime = DateTime.Now,
BusinessPlaceCode = null,
};
await _dbContext.InsertAsync(registry);
await _dbContext.InsertAsync(registry);
}
}
}
else
{
entity = await _dbContext.Queryable<DeviceEntity>().Where(x => x.Id == request.Id).FirstAsync();
entity.ThrowIfNull("设备不存在,无法更新");
//分类发生变化,检查是否存在分组
if (request.Type != entity.Type)
else
{
var existsG = await _dbContext.Queryable<DeviceGroupRelationshipEntity>()
.LeftJoin<DeviceGroupEntity>((dgr, dg) => dgr.GroupCode == dg.Code)
.Where((dgr, dg) => dgr.DeviceCode == entity.Code)
.AnyAsync();
entity = await _dbContext.Queryable<DeviceEntity>().Where(x => x.Id == request.Id).FirstAsync();
entity.ThrowIfNull("设备不存在,无法更新");
existsG.ThrowIfTrue("存在设备分组,不可修改设备分类");
//分类发生变化,检查是否存在分组
if (request.Type != entity.Type)
{
var existsG = await _dbContext.Queryable<DeviceGroupRelationshipEntity>()
.LeftJoin<DeviceGroupEntity>((dgr, dg) => dgr.GroupCode == dg.Code)
.Where((dgr, dg) => dgr.DeviceCode == entity.Code)
.AnyAsync();
existsG.ThrowIfTrue("存在设备分组,不可修改设备分类");
}
request.Adapt(entity);
await _dbContext.UpdateAsync(entity);
}
request.Adapt(entity);
await _dbContext.UpdateAsync(entity);
}
await GenerateUploadRecordLogForBeginTimeAsync(entity);
await GenerateUploadRecordLogForBeginTimeAsync(entity);
});
}
/// <summary>

View File

@@ -1,3 +1,4 @@
using Dlhis.Basic.Device.Dtos;
using Dlhis.Basic.Device.Request;
using Dlhis.Basic.Device.Response;
using Dlhis.Entity.Basic.Entity;
@@ -8,6 +9,7 @@ using Kurisu.AspNetCore.DataAccess.SqlSugar.Extensions;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
using Kurisu.AspNetCore.Utils.Extensions;
using Mapster;
using SqlSugar;
namespace Dlhis.Basic.Device.Services.Implements;
@@ -21,11 +23,11 @@ public class DeviceTimePeriodService : IDeviceTimePeriodService
}
/// <inheritdoc />
public async Task<Pagination<DeviceTimePeriodPageResponse>> GetPageAsync(DeviceTimePeriodPageRequest request)
public async Task<Pagination<DeviceTimePeriodResponse>> GetPageAsync(DeviceTimePeriodPageRequest request)
{
var page = await _dbContext.Queryable<DeviceTimePeriodEntity>()
.Where(x => x.AssignType == request.AssignType)
.Select<DeviceTimePeriodPageResponse>()
.Select<DeviceTimePeriodResponse>()
.ToPageListAsync(request);
var values = page.Data.Select(x => x.Value);
@@ -54,23 +56,149 @@ public class DeviceTimePeriodService : IDeviceTimePeriodService
.Where(x => x.CardType == request.CardType)
.Where(x => x.AssignType == request.AssignType)
.Where(x => x.Value == request.Value)
.WhereIF(request.Id.HasValue, x => x.Id != request.Id)
.AnyAsync()).ThrowIfTrue("请勿重复添加");
if (!request.Id.HasValue)
await GenerateDeviceTimePeriodTasksAsync(async () =>
{
var entity = request.Adapt<DeviceTimePeriodEntity>();
entity.CardType = request.CardType;
entity.AssignType = request.AssignType;
entity.Value = request.Value;
await _dbContext.InsertAsync(entity);
}
else
{
var entity = await _dbContext.Queryable<DeviceTimePeriodEntity>().Where(x => x.Id == request.Id).SingleAsync();
request.Adapt(entity);
if (!request.Id.HasValue)
{
var entity = request.Adapt<DeviceTimePeriodEntity>();
entity.CardType = request.CardType;
entity.AssignType = request.AssignType;
entity.Value = request.Value;
await _dbContext.InsertAsync(entity);
}
else
{
var entity = await _dbContext.Queryable<DeviceTimePeriodEntity>().Where(x => x.Id == request.Id).SingleAsync();
request.Adapt(entity);
await _dbContext.UpdateAsync(entity);
await _dbContext.UpdateAsync(entity);
}
});
}
/// <inheritdoc />
public async Task<List<DeviceTimePeriodResponse>> GetListAsync(DeviceTimePeriodAssignEnumType assignType, Guid value)
{
return await _dbContext.Queryable<DeviceTimePeriodEntity>().Where(x => x.AssignType == assignType && x.Value == value).Select<DeviceTimePeriodResponse>().ToListAsync();
}
public async Task GenerateDeviceTimePeriodTasksAsync(Func<Task> func)
{
var cardTypes = await _dbContext.Queryable<BasicTypeEntity>().Where(x => x.Type == "timeperiod").Select(x => SqlFunc.ToInt32(x.Code)).ToListAsync();
var before = await GetSettingsAsync();
await func();
var current = await GetSettingsAsync();
var tasks = new List<DeviceTaskEntity>();
var deviceUniqueNos = before.Keys.ToList().Concat(current.Keys).Distinct().ToList();
var devices = await _dbContext.Queryable<DeviceEntity>().Where(x => deviceUniqueNos.Contains(x.UniqueNo)).ToListAsync();
foreach (var device in devices)
{
var deviceUniqueNo = device.UniqueNo;
before.TryGetValue(deviceUniqueNo, out var beforeDtos);
beforeDtos ??= new List<DeviceTimePeriodDto>(0);
current.TryGetValue(deviceUniqueNo, out var currentDtos);
currentDtos ??= new List<DeviceTimePeriodDto>(0);
if (beforeDtos.Count != currentDtos.Count)
{
tasks.Add(new DeviceTaskEntity
{
Type = beforeDtos.Count < currentDtos.Count ? DeviceTaskEnumType.TimePeriodAdd : DeviceTaskEnumType.TimePeriodDelete,
DeviceType = device.Type,
UserId = null,
UserNo = null,
DeviceUniqueNo = deviceUniqueNo,
Status = DeviceTaskStatusEnumType.Wait,
Version = 0,
CancellationId = 0,
Source = null,
Priority = 90,
TenantId = device.TenantId
});
continue;
}
foreach (var cardType in cardTypes)
{
var b = beforeDtos.FirstOrDefault(x => x.CardType == cardType);
if (b == null)
{
continue;
}
var c = currentDtos.First(x => x.CardType == cardType);
if (b.OnStart != c.OnStart || b.OnEnd != c.OnEnd || b.OffStart != c.OffStart || b.OffEnd != c.OffEnd)
{
tasks.Add(new DeviceTaskEntity
{
Type = DeviceTaskEnumType.TimePeriodAdd,
DeviceType = device.Type,
UserId = null,
UserNo = null,
DeviceUniqueNo = deviceUniqueNo,
Status = DeviceTaskStatusEnumType.Wait,
Version = 0,
CancellationId = 0,
Source = null,
Priority = 90,
TenantId = device.TenantId
});
}
}
}
await _dbContext.InsertAsync(tasks);
}
private async Task<Dictionary<string, List<DeviceTimePeriodDto>>> GetSettingsAsync()
{
var timePeriods = await _dbContext.Queryable<DeviceTimePeriodEntity>().ToListAsync();
var templateCodes = timePeriods.Where(x => x.AssignType == DeviceTimePeriodAssignEnumType.Template).Select(x => x.Value).Distinct().ToList();
var deviceCodes = timePeriods.Where(x => x.AssignType == DeviceTimePeriodAssignEnumType.Device).Select(x => x.Value).ToList();
var devices = await _dbContext.Queryable<DeviceEntity>()
.Where(x => !SqlFunc.IsNullOrEmpty(x.TemplateCode))
.Where(x => templateCodes.Contains(x.TemplateCode.Value) || deviceCodes.Contains(x.Code))
.Where(x => x.Enable && x.EnableTimePeriod)
.ToListAsync();
return devices.Select(x =>
{
var tSettings = timePeriods
.Where(item => item.AssignType == DeviceTimePeriodAssignEnumType.Template && item.Value == x.TemplateCode)
.Adapt<List<DeviceTimePeriodDto>>();
var dSettings = timePeriods
.Where(item => item.AssignType == DeviceTimePeriodAssignEnumType.Device && item.Value == x.Code)
.Adapt<List<DeviceTimePeriodDto>>();
foreach (var setting in dSettings)
{
var s = tSettings.FirstOrDefault(item => item.CardType == setting.CardType);
if (s != null)
{
tSettings.Remove(s);
}
tSettings.Add(setting);
}
return new
{
x.UniqueNo, tSettings
};
}).ToDictionary(x => x.UniqueNo, x => x.tSettings);
}
}

View File

@@ -6,9 +6,12 @@ namespace Dlhis.Entity.Sap;
[SugarTable(TableName = "SapDataSync")]
public class SapDataSyncEntity : SugarBaseEntity
{
[SugarColumn(UniqueGroupNameList = new[] { "unique_SapDataSync_key-type" })]
public string Key { get; set; }
[SugarColumn(UniqueGroupNameList = new[] { "unique_SapDataSync_key-type" })]
public string Type { get; set; }
[SugarColumn(ColumnDataType = "bit")]
public bool IsSync { get; set; }
}