fix 设备注册自动新增

This commit is contained in:
2024-01-10 17:33:27 +08:00
parent 2e51a13c3d
commit 54835e6cae
6 changed files with 47 additions and 8 deletions

View File

@@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
using Npgsql.Replication.PgOutput;
namespace Dlhis.Basic.Device.Request;
@@ -10,6 +11,11 @@ public class DeviceResigterRequest
[Required(ErrorMessage = "设备唯一编码不能为空")]
public string UniqueNo { get; set; }
/// <summary>
/// 设备部署位置
/// </summary>
public string DeployPlace { get; set; }
/// <summary>
/// 营业点手工号; 可为空,后续在后台中设置
/// </summary>

View File

@@ -23,10 +23,11 @@ public class DeviceConnectService : IDeviceConnectService
[IgnoreTenant]
public async Task<string> ResigterAsync(DeviceResigterRequest request)
{
var device = await GetEnableDeviceAsync(request.UniqueNo);
var registry = await _dbContext.Queryable<DeviceRegistryEntity>().Where(x => x.DeviceUniqueNo == request.UniqueNo).FirstAsync();
var businessPlace = await _dbContext.Queryable<CanteenBusinessPlaceEntity>().Where(x => x.CustomCode == request.BusinessCustomCode).FirstAsync();
businessPlace.ThrowIfNull("营业点不存在");
var device = await GetEnableDeviceAsync(request.UniqueNo, false, request.DeployPlace, businessPlace.TenantId);
var registry = await _dbContext.Queryable<DeviceRegistryEntity>().Where(x => x.DeviceUniqueNo == request.UniqueNo).FirstAsync();
if (registry == null)
{
@@ -92,15 +93,45 @@ public class DeviceConnectService : IDeviceConnectService
/// 获取启用的设备
/// </summary>
/// <param name="uniqueNo"></param>
/// <param name="checkExists"></param>
/// <param name="deployPlace"></param>
/// <param name="tenantId"></param>
/// <returns></returns>
private async Task<DeviceEntity> GetEnableDeviceAsync(string uniqueNo)
private async Task<DeviceEntity> GetEnableDeviceAsync(string uniqueNo, bool checkExists = true, string deployPlace = "", string tenantId = "")
{
var device = await _dbContext.Queryable<DeviceEntity>()
.Where(x => x.UniqueNo == uniqueNo)
.FirstAsync();
device.ThrowIfNull($"未知的设备唯一编码{uniqueNo}");
device.Enable.ThrowIfFalse("设备尚未启用");
if (checkExists)
{
device.ThrowIfNull($"未知的设备唯一编码{uniqueNo}");
device.Enable.ThrowIfFalse("设备尚未启用");
}
else
{
if (device == null)
{
device = new DeviceEntity()
{
Code = Guid.NewGuid(),
Brand = string.Empty,
ConnectionInfo = string.Empty,
TemplateCode = null,
UniqueNo = uniqueNo,
TenantId = tenantId,
Supplier = string.Empty,
Funcs = new List<DeviceFuncEnumType>(),
DeployPlace = deployPlace,
HeartbeatInterval = 0,
DeployDirection = string.Empty,
Enable = true,
DeployPosition = string.Empty,
};
await _dbContext.InsertAsync(device);
}
}
return device;
}