183 lines
5.9 KiB
C#
183 lines
5.9 KiB
C#
using Dlhis.Basic.Device.Request;
|
|
using Dlhis.Basic.Device.Response;
|
|
using Dlhis.Entity.Basic.Entity;
|
|
using Dlhis.Entity.Device.Entities;
|
|
using Dlhis.Entity.Device.Enums;
|
|
using Kurisu.AspNetCore.UnifyResultAndValidation;
|
|
using Kurisu.AspNetCore.Utils.Extensions;
|
|
using Kurisu.Core.DataAccess.Dto;
|
|
using Kurisu.SqlSugar.Attributes;
|
|
using Kurisu.SqlSugar.Extensions;
|
|
using Kurisu.SqlSugar.Services;
|
|
using Mapster;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
namespace Dlhis.Basic.Device.Services.Implements;
|
|
|
|
public class DeviceService : IDeviceService
|
|
{
|
|
private readonly IDbContext _dbContext;
|
|
|
|
public DeviceService(IDbContext dbContext)
|
|
{
|
|
_dbContext = dbContext;
|
|
}
|
|
|
|
public async Task<Pagination<DeviceResponse>> GetPageAsync(DevicePageRequest request)
|
|
{
|
|
var page = await _dbContext.Queryable<DeviceEntity>()
|
|
.Select(device => new DeviceResponse
|
|
{
|
|
FuncsStr = device.Funcs.ToString()
|
|
|
|
}, true)
|
|
.ToPageListAsync(request);
|
|
|
|
return page;
|
|
}
|
|
|
|
[Transactional]
|
|
public async Task SaveAsync(DeviceSaveRequest request)
|
|
{
|
|
await CheckUniqueNoExistsAsync(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);
|
|
//同时保存注册信息
|
|
var registry = await _dbContext.Queryable<DeviceRegistryEntity>().Where(x => x.DeviceUniqueNo == request.UniqueNo).FirstAsync();
|
|
if (registry == null)
|
|
{
|
|
registry = new DeviceRegistryEntity()
|
|
{
|
|
DeviceUniqueNo = request.UniqueNo,
|
|
LastHeartbeatTime = DateTime.Now,
|
|
RegisterTime = DateTime.Now,
|
|
BusinessPlaceCode = null,
|
|
BusinessPlaceCustomCode = null,
|
|
};
|
|
|
|
await _dbContext.InsertAsync(registry);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
var entity = await _dbContext.Queryable<DeviceEntity>().Where(x => x.Id == request.Id).FirstAsync();
|
|
entity.ThrowIfNull("设备不存在,无法更新");
|
|
|
|
request.Adapt(entity);
|
|
entity.Brand = template.Brand;
|
|
|
|
await _dbContext.UpdateAsync(entity);
|
|
}
|
|
}
|
|
|
|
//******************************************************** help methods **********************************************************
|
|
|
|
/// <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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 检查唯一号是否存在
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <returns></returns>
|
|
private async Task CheckUniqueNoExistsAsync(DeviceSaveRequest request)
|
|
{
|
|
var exists = await _dbContext.Queryable<DeviceEntity>().Where(x => x.UniqueNo == request.UniqueNo)
|
|
.WhereIF(request.Id.HasValue, x => x.Id != request.Id)
|
|
.AnyAsync();
|
|
|
|
exists.ThrowIfTrue("唯一号已经存在");
|
|
}
|
|
|
|
[IgnoreTenant]
|
|
public async Task<bool> CheckExistsAsync(string deviceUniqueNo)
|
|
{
|
|
return await _dbContext.Queryable<DeviceEntity>()
|
|
.Where(x => x.UniqueNo == deviceUniqueNo && x.Enable)
|
|
.AnyAsync();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取设备
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <returns></returns>
|
|
public async Task<Pagination<DeviceResponse>> GetDeviceByNoAsync(DevicePageRequest request)
|
|
{
|
|
var page = await _dbContext.Queryable<DeviceEntity>()
|
|
.Where(x => x.UniqueNo == request.UniqueNo)
|
|
.Select(device => new DeviceResponse
|
|
{
|
|
FuncsStr = device.Funcs.ToString()
|
|
|
|
}, true)
|
|
.ToPageListAsync(request);
|
|
|
|
return page;
|
|
}
|
|
|
|
public async Task<List<DeviceResponse>> GetAttendanceDevicesAsync()
|
|
{
|
|
var devices = await _dbContext.Queryable<DeviceEntity>()
|
|
.Where(x => x.Enable)
|
|
.Select(x => new DeviceResponse
|
|
{
|
|
FuncsStr = x.Funcs.ToString()
|
|
}, true)
|
|
.ToListAsync();
|
|
|
|
return devices.Where(x => x.Funcs.Contains(DeviceFuncEnumType.Attendance)).ToList();
|
|
}
|
|
}
|
|
|