feat 梯控相关

This commit is contained in:
2026-03-13 15:42:36 +08:00
parent 9d0600ebb8
commit f1cb0813db
34 changed files with 1112 additions and 320 deletions

View File

@@ -130,7 +130,7 @@ public class DevicePermissionsController : ControllerBase
[IgnoreTenant]
public async Task<List<Guid>> GetAssignDeviceCodesAsync(string userCode)
{
return await _devicePermissionService.GetUserDeviceCodesByNoAsync(userCode);
return await _devicePermissionService.GetUserDeviceCodesByNoOrCodeAsync(userCode);
}

View File

@@ -76,7 +76,7 @@ public class DevicesConnectController : ControllerBase
/// <param name="brand"></param>
/// <param name="factory"></param>
/// <returns></returns>
[DisableApiLog]
[DisableApiLog]
[HttpGet("brand-devices")]
[IgnoreTenant]
public async Task<List<DeviceUniqueWithConnectionInfoDto>> GetDeviceUniqueInfoByBrandAsync(

View File

@@ -0,0 +1,48 @@
using Dlhis.Basic.Device.Request;
using Dlhis.Basic.Device.Response;
using Dlhis.Basic.Device.Services;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Aop;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Dlhis.Basic.Device.Controllers;
/// <summary>
/// 梯控设备
/// </summary>
[Authorize]
[Route("api/[controller]")]
[ApiController]
public class ElevatorAcsController : ControllerBase
{
private readonly IElevatorAcsService _elevatorAcsService;
public ElevatorAcsController(IElevatorAcsService elevatorAcsService)
{
_elevatorAcsService = elevatorAcsService;
}
/// <summary>
/// 设备保存
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
[HttpPost("save")]
[Transactional]
public async Task SaveAsync(ElevatorAcsSaveRequest request)
{
await _elevatorAcsService.SaveAsync(request);
}
/// <summary>
/// 获取设备详情
/// </summary>
/// <param name="deviceUniqueNo"></param>
/// <returns></returns>
[HttpGet("detail")]
public async Task<ElevatorAcsDetailResponse> GetDetailsAsync(string deviceUniqueNo)
{
return await _elevatorAcsService.GetDetailsAsync(deviceUniqueNo);
}
}

View File

@@ -23,4 +23,9 @@ public class DevicePageRequest : PageDto
/// 启用状态
/// </summary>
public bool? Enable { get; set; }
/// <summary>
/// 是否为梯控
/// </summary>
public bool IsElevatorAcs { get; set; }
}

View File

@@ -1,7 +1,5 @@
using System.ComponentModel.DataAnnotations;
using System.Linq.Expressions;
using Dlhis.Entity.Device.Enums;
using Dlhis.Entity.Sys.Entities;
using Dlhis.Entity.Sys.Enums;
using Kurisu.AspNetCore.Utils.Extensions;

View File

@@ -0,0 +1,78 @@
using System.ComponentModel.DataAnnotations;
using Dlhis.Entity.Device.Enums;
namespace Dlhis.Basic.Device.Request;
public class ElevatorAcsSaveRequest
{
public bool IsEdit { get; set; }
/// <summary>
/// 设备唯一号
/// </summary>
[Required(ErrorMessage = "设备唯一号不能为空")]
public string UniqueNo { get; set; }
/// <summary>
/// 模板code
/// </summary>
[Required(ErrorMessage = "模板不能为空")]
public Guid TemplateCode { get; set; }
/// <summary>
/// 供应商信息
/// </summary>
[Required(ErrorMessage = "供应商信息不能为空")]
public string Supplier { get; set; }
/// <summary>
/// 设备功能 DeviceFuncEnumType
/// </summary>
[MinLength(1, ErrorMessage = "设备功能不能为空")]
[Required(ErrorMessage = "设备功能不能为空")]
public List<DeviceFuncEnumType> Funcs { get; set; }
/// <summary>
/// 心跳间隔
/// </summary>
public int HeartbeatInterval { get; set; }
/// <summary>
/// 部署点
/// </summary>
public string DeployPlace { get; set; }
/// <summary>
/// 部署坐标
/// </summary>
public string DeployPosition { get; set; }
/// <summary>
/// 部署方向 DeviceDeployDirectionEnumType
/// </summary>
public DeviceDeployDirectionEnumType? DeployDirection { get; set; }
/// <summary>
/// 连接信息
/// </summary>
[Required(ErrorMessage = "连接信息不能为空")]
public string ConnectionInfo { get; set; }
/// <summary>
/// 是否启用
/// </summary>
public bool Enable { get; set; }
/// <summary>
/// 楼层列表
/// </summary>
public List<FloorInfo> Floors { get; set; }
}
public class FloorInfo
{
/// <summary>
/// 楼层名称
/// </summary>
public string FloorName { get; set; }
}

View File

@@ -102,6 +102,33 @@ public class DevicePageResponse
/// </summary>
public bool EnableTimePeriod { get; set; }
/// <summary>
/// 是否作为梯控
/// </summary>
public bool IsElevatorAcs { get; set; }
/// <summary>
/// 同源标记
/// </summary>
public int SourceFlag
{
get
{
if (string.IsNullOrEmpty(RealDeviceUniqueNo))
{
return UniqueNo.GetHashCode();
}
return RealDeviceUniqueNo.GetHashCode();
}
}
/// <summary>
///
/// </summary>
[JsonIgnore]
public string RealDeviceUniqueNo { get; set; }
/// <summary>
/// 同步标志

View File

@@ -0,0 +1,112 @@
using Dlhis.DataPermission.Model;
using Dlhis.Entity.Device.Enums;
using Mapster;
using SqlSugar;
using SqlSugar.DbConvert;
namespace Dlhis.Basic.Device.Response;
public class ElevatorAcsDetailResponse
{
/// <summary>
/// 设备code
/// </summary>
[DeviceCode]
[SugarColumn(IsNullable = false, ColumnDescription = "设备code")]
public Guid Code { get; set; }
/// <summary>
/// 设备唯一码,IP/MAC/SNO
/// </summary>
[AdaptIgnore(MemberSide.Destination)]
[SugarColumn(IsNullable = false, ColumnDescription = "设备唯一码", UniqueGroupNameList = new[] { "Idx_Device_UniqueNo" })]
public string UniqueNo { get; set; }
/// <summary>
/// 心跳间隔
/// </summary>
[SugarColumn(IsNullable = false, ColumnDescription = "心跳检查间隔")]
public int HeartbeatInterval { get; set; } = 30;
/// <summary>
/// 供应商
/// </summary>
[SugarColumn(IsNullable = true, ColumnDescription = "供应商")]
public string Supplier { get; set; }
/// <summary>
/// 设备功能
/// </summary>
[SugarColumn(IsNullable = false, ColumnDataType = "varchar(150)", IsJson = true, ColumnDescription = "设备功能", DefaultValue = "[]")]
public List<DeviceFuncEnumType> Funcs { get; set; }
/// <summary>
/// 设备类型
/// </summary>
[SugarColumn(IsNullable = false, ColumnDescription = "设备类型", ColumnDataType = "varchar(50)", SqlParameterDbType = typeof(EnumToStringConvert))]
public DeviceTypeEnumType Type { get; set; }
/// <summary>
/// 部署点
/// </summary>
[SugarColumn(IsNullable = true, ColumnDescription = "部署点")]
public string DeployPlace { get; set; }
/// <summary>
/// 部署坐标
/// </summary>
[SugarColumn(IsNullable = true, ColumnDescription = "部署坐标")]
public string DeployPosition { get; set; }
/// <summary>
/// 部署方向
/// </summary>
[SugarColumn(IsNullable = true, ColumnDescription = "部署方向", ColumnDataType = "varchar(50)", SqlParameterDbType = typeof(EnumToStringConvert))]
public DeviceDeployDirectionEnumType? DeployDirection { get; set; }
/// <summary>
/// 模板code
/// </summary>
[SugarColumn(IsNullable = false, ColumnDescription = "模板code")]
public Guid TemplateCode { get; set; }
/// <summary>
/// 连接信息
/// </summary>
[SugarColumn(IsNullable = false, ColumnDescription = "连接信息")]
public string ConnectionInfo { get; set; }
/// <summary>
/// 是否启用
/// </summary>
[SugarColumn(IsNullable = false, ColumnDataType = "bit", ColumnDescription = "是否启用")]
public bool Enable { get; set; }
/// <summary>
/// 是否作为考勤设备
/// </summary>
[SugarColumn(IsNullable = false, ColumnDataType = "bit", ColumnDescription = "是否作为考勤设备")]
public bool AsAttendance { get; set; }
/// <summary>
/// 是否启用时段码
/// </summary>
[SugarColumn(IsNullable = false, ColumnDataType = "bit", ColumnDescription = "是否启用时段码", DefaultValue = "0")]
public bool EnableTimePeriod { get; set; } = false;
/// <summary>
/// 是否为电梯门禁设备
/// </summary>
[SugarColumn(IsNullable = false, ColumnDataType = "bit", ColumnDescription = "是否为电梯门禁设备", DefaultValue = "0")]
public bool IsElevatorAcs { get; set; }
public List<ElevatorAcsFloorResponse> Floors { get; set; }
}
public class ElevatorAcsFloorResponse
{
public string FloorName { get; set; }
public string AcsValue { get; set; }
}

View File

@@ -27,18 +27,18 @@ public interface IDevicePermissionService : IScopeDependency
/// <summary>
/// 生成下发任务(主动生成)
/// </summary>
/// <param name="userNo"></param>
/// <param name="userNoOrCode"></param>
/// <param name="taskType"></param>
/// <param name="source"></param>
/// <returns></returns>
Task GenerateDeviceTasksAsync(string userNo, DeviceTaskEnumType taskType, string source);
Task GenerateDeviceTasksAsync(string userNoOrCode, DeviceTaskEnumType taskType, string source);
/// <summary>
/// 获取用户所有设备权限
/// </summary>
/// <param name="userNo"></param>
/// <param name="userNoOrCode"></param>
/// <returns></returns>
Task<List<Guid>> GetUserDeviceCodesByNoAsync(string userNo);
Task<List<Guid>> GetUserDeviceCodesByNoOrCodeAsync(string userNoOrCode);
Task<Pagination<DeviceGainfoResponse>> GetGainfoPageByDeviceAsync(DeviceWithGainfoRequest request);
@@ -64,5 +64,6 @@ public interface IDevicePermissionService : IScopeDependency
Task<int> GenerateDeleteDeviceTasksAsync(string deviceUniqueNo);
[Obsolete("不建议使用,建议使用GetUserDeviceCodesByNoOrCodeAsync方法")]
Task<List<TempC>> GetUserDeviceCodesByNoAsync(List<string> userCodes);
}

View File

@@ -0,0 +1,10 @@
using Dlhis.Basic.Device.Request;
using Dlhis.Basic.Device.Response;
namespace Dlhis.Basic.Device.Services;
public interface IElevatorAcsService : IScopeDependency
{
Task SaveAsync(ElevatorAcsSaveRequest request);
Task<ElevatorAcsDetailResponse> GetDetailsAsync(string deviceUniqueNo);
}

View File

@@ -139,15 +139,35 @@ public class DeviceConnectService : IDeviceConnectService
{
var factories = factory.Split(",").ToList();
return await _dbContext.Queryable<DeviceEntity>()
.LeftJoin<DeviceTemplateEntity>((d, dt) => d.TemplateCode == dt.Code)
.Where((d, dt) => dt.Brand == brand && factories.Contains(d.TenantId))
.Select(d => new DeviceUniqueWithConnectionInfoDto
{
DeviceUnique = d.UniqueNo,
ConnectionInfo = d.ConnectionInfo
})
.ToListAsync();
var results = await _dbContext.Queryable<DeviceEntity>()
.LeftJoin<DeviceTemplateEntity>((d, dt) => d.TemplateCode == dt.Code)
.Where((d, dt) => dt.Brand == brand && factories.Contains(d.TenantId))
.Where(d => d.IsElevatorAcs == false)//先排除梯控
.Where(d => d.Enable == true)
.Select(d => new DeviceUniqueWithConnectionInfoDto
{
DeviceUnique = d.UniqueNo,
ConnectionInfo = d.ConnectionInfo
})
.ToListAsync();
var elevatorAcses = await _dbContext.Queryable<DeviceEntity>()
.LeftJoin<DeviceTemplateEntity>((d, dt) => d.TemplateCode == dt.Code)
.LeftJoin<ElevatorAcsFloorEntity>((d, dt, f) => d.UniqueNo == f.VirtualDeviceUniqueNo)
.Where((d, dt) => dt.Brand == brand && factories.Contains(d.TenantId))
.Where(d => d.IsElevatorAcs == true)
.Where(d => d.Enable == true)
.Select((d, dt, f) => new DeviceUniqueWithConnectionInfoDto
{
DeviceUnique = f.RealDeviceUniqueNo,
ConnectionInfo = d.ConnectionInfo
})
.ToListAsync();
results.AddRange(elevatorAcses.DistinctBy(x => x.DeviceUnique).ToList());
return results;
}

View File

@@ -298,9 +298,9 @@ public class DevicePermissionService : IDevicePermissionService
}
public async Task GenerateDeviceTasksAsync(string userNo, DeviceTaskEnumType taskType, string source)
public async Task GenerateDeviceTasksAsync(string userNoOrCode, DeviceTaskEnumType taskType, string source)
{
var deviceCodes = await GetUserDeviceCodesByNoAsync(userNo);
var deviceCodes = await GetUserDeviceCodesByNoOrCodeAsync(userNoOrCode);
if (deviceCodes.IsEmpty()) return;
var devices = await _dbContext.Queryable<DeviceEntity>()
@@ -308,13 +308,33 @@ public class DevicePermissionService : IDevicePermissionService
.Where(x => deviceCodes.Contains(x.Code))
.ToListAsync();
var gainfo = await _dbContext.Queryable<GainfoEntity>()
.Where(x => x.No == userNoOrCode || x.Code == userNoOrCode)
.Select(x => new
{
UserId = x.Id,
UserNo = x.No,
UserCode = x.Code
})
.SingleAsync()
?? await _dbContext.Queryable<GainfoLeaveEntity>()
.Where(x => x.No == userNoOrCode || x.Code == userNoOrCode)
.Select(x => new
{
UserId = (long)x.IdentityUserId,
UserNo = x.No,
UserCode = x.Code
})
.SingleAsync();
await _dbContext.InsertAsync(devices.Select(x => new DeviceTaskEntity
{
DeviceUniqueNo = x.UniqueNo,
Status = DeviceTaskStatusEnumType.Wait,
Type = taskType,
DeviceType = x.Type,
UserNo = userNo,
UserNo = gainfo.UserNo,
UserId = gainfo.UserId,
Priority = 0,
Version = 0,
TenantId = x.TenantId,
@@ -326,11 +346,14 @@ public class DevicePermissionService : IDevicePermissionService
/// <summary>
/// 获取用户所有设备权限
/// </summary>
/// <param name="userNo"></param>
/// <param name="userNoOrCode"></param>
/// <returns></returns>
public async Task<List<Guid>> GetUserDeviceCodesByNoAsync(string userNo)
public async Task<List<Guid>> GetUserDeviceCodesByNoOrCodeAsync(string userNoOrCode)
{
var no = await _dbContext.Queryable<GainfoEntity>().Where(x => x.No == userNo || x.Code == userNo).Select(x => x.No).SingleAsync();
var no = await _dbContext.Queryable<GainfoEntity>().Where(x => x.No == userNoOrCode || x.Code == userNoOrCode).Select(x => x.No).SingleAsync()
??
await _dbContext.Queryable<GainfoLeaveEntity>().Where(x => x.No == userNoOrCode || x.Code == userNoOrCode).Select(x => x.No).FirstAsync();
var result = await _devicePermissionAnalyzeService.GetUserAssignDeviceCodesWithTypeInTenantAsync(new List<string> { no });
var deviceCodes = result.First().Value.SelectMany(x => x.AssignTypes.SelectMany(p => p.Codes)).Distinct().ToList();

View File

@@ -38,24 +38,29 @@ public class DeviceService : IDeviceService
public async Task<Pagination<DevicePageResponse>> GetPageAsync(DevicePageRequest request)
{
var page = await _dbContext.Queryable<DeviceEntity>()
.LeftJoin<DeviceTemplateEntity>((d, dt) => d.TemplateCode == dt.Code)
.LeftJoin<DeviceRegistryEntity>((d, dt, dr) => d.UniqueNo == dr.DeviceUniqueNo)
.LeftJoin<BusinessPlaceEntity>((d, dt, dr, bp) => dr.BusinessPlaceCode == bp.Code)
.Where((d, dt) => d.Type == request.Type)
.WhereIF(request.Enable.HasValue, (d, dt) => d.Enable == request.Enable)
.WhereIF(request.Search.IsPresent(), d => d.UniqueNo == request.Search || d.DeployPlace.Contains(request.Search))
.WhereIF(request.Supplier.IsPresent(), d => d.Supplier.Contains(request.Supplier))
.Select((d, dt, dr, bp) => new DevicePageResponse
{
FuncsStr = d.Funcs.ToString(),
Brand = dt.Brand,
Type = d.Type,
BusinessPlaceCode = bp.Code,
BusinessPlaceName = bp.Name,
LastHeartbeatTime = dr.LastHeartbeatTime,
Factory = d.TenantId
}, true)
.ToPageListAsync(request);
.LeftJoin<DeviceTemplateEntity>((d, dt) => d.TemplateCode == dt.Code)
.LeftJoin<DeviceRegistryEntity>((d, dt, dr) => d.UniqueNo == dr.DeviceUniqueNo)
.LeftJoin<BusinessPlaceEntity>((d, dt, dr, bp) => dr.BusinessPlaceCode == bp.Code)
.LeftJoin<ElevatorAcsFloorEntity>((d, dt, dr, bp, ef) => d.UniqueNo == ef.VirtualDeviceUniqueNo)
.Where((d, dt) => d.Type == request.Type)
.Where(d => d.IsElevatorAcs == request.IsElevatorAcs)
.WhereIF(request.Enable.HasValue, (d, dt) => d.Enable == request.Enable)
.WhereIF(request.Search.IsPresent(), d => d.UniqueNo == request.Search || d.DeployPlace.Contains(request.Search))
.WhereIF(request.Supplier.IsPresent(), d => d.Supplier.Contains(request.Supplier))
.Select((d, dt, dr, bp, ef) => new DevicePageResponse
{
IsElevatorAcs = d.IsElevatorAcs,
FuncsStr = d.Funcs.ToString(),
Brand = dt.Brand,
Type = d.Type,
BusinessPlaceCode = bp.Code,
BusinessPlaceName = bp.Name,
LastHeartbeatTime = dr.LastHeartbeatTime,
RealDeviceUniqueNo = ef.RealDeviceUniqueNo,
Factory = d.TenantId,
}, true)
.ToPageListAsync(request);
//获取当前用户角色
var role = _currentUser.GetRole();
@@ -77,7 +82,6 @@ public class DeviceService : IDeviceService
}
}
var deviceUniqueNos = page.Data.Select(x => x.UniqueNo).ToList();
var deviceUsers = await _dbContext.Queryable<DeviceUserEntity>()
.Where(x => deviceUniqueNos.Contains(x.DeviceUniqueNo))

View File

@@ -0,0 +1,216 @@
using Dlhis.Basic.Device.Request;
using Dlhis.Basic.Device.Response;
using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Device.Enums;
using Dlhis.Sys.Api;
using Kurisu.AspNetCore.Authentication.Abstractions;
using Kurisu.AspNetCore.CustomClass;
using Kurisu.AspNetCore.DataAccess.Entity;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
using Kurisu.AspNetCore.Utils.Extensions;
using Mapster;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Dlhis.Basic.Device.Services.Implements;
public class ElevatorAcsService : IElevatorAcsService
{
private readonly IDbContext _dbContext;
private readonly ICurrentUser _currentUser;
private readonly ITenantApi _tenantApi;
private const string DefaultPassword = "********";
private const string DefaultPasswordKey = "password";
public ElevatorAcsService(IDbContext dbContext, ICurrentUser currentUser, ITenantApi tenantApi)
{
_dbContext = dbContext;
_currentUser = currentUser;
_tenantApi = tenantApi;
}
public async Task SaveAsync(ElevatorAcsSaveRequest request)
{
await CheckUniqueNoExistsAsync(request);
await CheckTemplateExistsAsync(request.TemplateCode);
await CheckTemplateFieldAsync(request.TemplateCode, request.ConnectionInfo);
if (!request.IsEdit)
{
var password = GetJsonValue(request.ConnectionInfo, DefaultPasswordKey);
if (password.IsPresent())
{
(password == DefaultPassword).ThrowIfTrue("不支持错误的密码类型");
}
for (int i = 0; i < request.Floors.Count; i++)
{
var floor = request.Floors[i];
DeviceEntity entity = request.Adapt<DeviceEntity>();
entity.Code = Guid.NewGuid();
entity.Type = DeviceTypeEnumType.Door;
entity.IsElevatorAcs = true;
entity.Enable = true;
var virtualDeviceUniqueNo = $"{request.UniqueNo}-EACS-{floor.FloorName}";
entity.UniqueNo = virtualDeviceUniqueNo;
var elevatorAcsFloor = new ElevatorAcsFloorEntity
{
RealDeviceUniqueNo = request.UniqueNo,
FloorName = floor.FloorName,
AcsValue = i,
VirtualDeviceUniqueNo = virtualDeviceUniqueNo,
};
await _dbContext.InsertAsync(entity);
await _dbContext.InsertAsync(elevatorAcsFloor);
}
}
else
{
var prefix = request.UniqueNo + "-EACS-";
List<DeviceEntity> entities = await _dbContext.Queryable<DeviceEntity>().Where(x => x.UniqueNo.StartsWith(prefix)).ToListAsync();
foreach (var entity in entities)
{
//检查更新的密码字段是否为默认密码,如果是,则将原有密码从数据库实体中取出并替换到请求的连接信息中,防止默认密码被保存。
// 这样可以避免用户在未修改密码时,前端传递默认密码导致密码被覆盖为不可用的默认值。
var updatePassword = GetJsonValue(request.ConnectionInfo, DefaultPasswordKey);
if (updatePassword.IsPresent())
{
if (updatePassword == DefaultPassword)
{
var jObj = JObject.Parse(request.ConnectionInfo);
var password = GetJsonValue(entity.ConnectionInfo, DefaultPasswordKey);
jObj[DefaultPasswordKey] = password;
request.ConnectionInfo = jObj.ToString();
}
}
request.Adapt(entity);
await _dbContext.UpdateAsync(entity);
}
}
}
public async Task<ElevatorAcsDetailResponse> GetDetailsAsync(string deviceUniqueNo)
{
var floor = await _dbContext.Queryable<ElevatorAcsFloorEntity>().Where(x => x.VirtualDeviceUniqueNo == deviceUniqueNo).SingleAsync();
var floors = await _dbContext.Queryable<ElevatorAcsFloorEntity>().Where(x => x.RealDeviceUniqueNo == floor.RealDeviceUniqueNo).ToListAsync();
var deviceUniqueNos = floors.Select(x => x.VirtualDeviceUniqueNo).ToList();
var device = await _dbContext.Queryable<DeviceEntity>().Where(x => x.UniqueNo == deviceUniqueNo).SingleAsync();
var result = device.Adapt<ElevatorAcsDetailResponse>();
result.UniqueNo = floor.RealDeviceUniqueNo;
result.Floors = floors.Adapt<List<ElevatorAcsFloorResponse>>();
return result;
}
/// <summary>
/// 获取json的字段值
/// </summary>
/// <param name="json"></param>
/// <param name="key"></param>
/// <returns></returns>
/// <exception cref="UserFriendlyException"></exception>
private static string GetJsonValue(string json, string key)
{
try
{
var jObj = JObject.Parse(json);
return jObj.TryGetValue(key, out var value)
? value.Value<string>()
: string.Empty;
}
catch (JsonReaderException)
{
throw new UserFriendlyException("连接信息参数格式不正确");
}
}
/// <summary>
/// 检查设备模板是否存在
/// </summary>
/// <param name="templateCode"></param>
/// <returns></returns>
private async Task CheckTemplateExistsAsync(Guid templateCode)
{
var entity = await _dbContext.Queryable<DeviceTemplateEntity>().Where(x => x.Code == templateCode).FirstAsync();
entity.ThrowIfNull("设备模板不存在");
}
/// <summary>
/// 检查设备连接参数是否与模板设定一致
/// </summary>
/// <param name="templateCode"></param>
/// <param name="jsonContent"></param>
/// <returns></returns>
private async Task CheckTemplateFieldAsync(Guid templateCode, string jsonContent)
{
var template = await _dbContext.Queryable<DeviceTemplateEntity>().Where(x => x.Code == templateCode).SingleAsync();
template.ThrowIfNull("设备模板不存在");
try
{
var templateJObj = JObject.Parse(template.Template);
var jObj = JObject.Parse(jsonContent);
(templateJObj.Count == jObj.Count).ThrowIfFalse("连接参数字段数不一致");
foreach (var item in templateJObj)
{
jObj.ContainsKey(item.Key).ThrowIfFalse($"设备连接参数需要和模板一致.缺失{item.Key}");
}
}
catch (Exception ex) when (ex is JsonReaderException)
{
throw new UserFriendlyException("连接信息参数格式不正确");
}
}
/// <summary>
/// 检查唯一号是否存在
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
private async Task CheckUniqueNoExistsAsync(ElevatorAcsSaveRequest request)
{
await _dbContext.IgnoreAsync<ITenantId>(async () =>
{
var currentTenantId = _currentUser.GetStringTenantId();
var prefix = request.UniqueNo + "-EACS-";
//查询设备是否存在
var device = await _dbContext.Queryable<DeviceEntity>()
.Where(x => x.UniqueNo == request.UniqueNo)
.WhereIF(!request.IsEdit, x => x.UniqueNo.StartsWith(prefix))
.SingleAsync();
if (device != null)
{
(device.TenantId == currentTenantId).ThrowIfTrue("设备已经存在,请检查");
var factory = device.TenantId;
try
{
var tenant = await _tenantApi.GetTenantByCodeAsync(device.TenantId);
factory = tenant?.Name ?? string.Empty;
}
catch (Exception)
{
//ignore
}
true.ThrowIfTrue($"设备已添加{factory}厂区");
}
});
}
}