update 版本升级

This commit is contained in:
2023-12-26 11:02:04 +08:00
parent f5d2b41415
commit c998795595
20 changed files with 566 additions and 42 deletions

View File

@@ -1,11 +1,15 @@
using Dlhis.Basic.Device.Request;
using Dlhis.Basic.Device.Response;
using Dlhis.Entity.Device.Entities;
using Kurisu.AspNetCore.UnifyResultAndValidation;
using Kurisu.AspNetCore.Utils.Extensions;
using Kurisu.Core.DataAccess.Dto;
using Kurisu.SqlSugar.Extensions;
using Kurisu.SqlSugar.Services;
using Mapster;
using NetTaste;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Dlhis.Basic.Device.Services.Implements;
@@ -30,20 +34,72 @@ public class DeviceService : IDeviceService
public async Task SaveAsync(DeviceSaveRequest request)
{
var template = await CheckTemplateExistsAsync(request.TemplateCode);
await CheckTemplateFieldAsync(request.TemplateCode, request.ConnectionInfo);
if (!request.Id.HasValue)
{
var entity = request.Adapt<DeviceEntity>();
entity.Code = Guid.NewGuid();
entity.Brand = template.Brand;
await _dbContext.InsertAsync(entity);
}
else
{
var device = await _dbContext.Queryable<DeviceEntity>().Where(x => x.Id == request.Id).FirstAsync();
device.ThrowIfNull("设备不存在,无法更新");
var entity = await _dbContext.Queryable<DeviceEntity>().Where(x => x.Id == request.Id).FirstAsync();
entity.ThrowIfNull("设备不存在,无法更新");
request.Adapt(device);
request.Adapt(entity);
entity.Brand = template.Brand;
await _dbContext.UpdateAsync(device);
await _dbContext.UpdateAsync(entity);
}
}
//******************************************************** help method **********************************************************
/// <summary>
/// 检查设备模板是否存在
/// </summary>
/// <param name="templateCode"></param>
/// <returns></returns>
private async Task<DeviceTemplateEntity> CheckTemplateExistsAsync(Guid templateCode)
{
var entity = await _dbContext.Queryable<DeviceTemplateEntity>().Where(x => x.Code == templateCode).FirstAsync();
entity.ThrowIfNull("设备模板不存在");
return entity;
}
/// <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).FirstAsync();
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("连接信息参数格式不正确");
}
catch (Exception)
{
throw;
}
}
}