217 lines
7.9 KiB
C#
217 lines
7.9 KiB
C#
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}厂区");
|
|
}
|
|
});
|
|
}
|
|
}
|