Files
DMS/DMS.Infrastructure/Services/DeviceService.cs

84 lines
3.0 KiB
C#
Raw Normal View History

using AutoMapper;
using DMS.Core.Helper;
using DMS.Core.Models;
using DMS.Infrastructure.Data;
using DMS.Infrastructure.Entities;
using DMS.Infrastructure.Repositories;
using DMS.Infrastructure.Interfaces;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
2025-07-19 19:55:42 +08:00
using System.Collections.Concurrent;
namespace DMS.Infrastructure.Services
{
2025-07-19 22:29:50 +08:00
public class DeviceService :BaseService<Device, DbDevice, DeviceRepository>
{
2025-07-19 22:29:50 +08:00
private readonly IDeviceRepository _deviceRepository;
private readonly IMenuRepository _menuRepository;
private readonly IVarTableRepository _varTableRepository;
private readonly IMapper _mapper;
2025-07-19 19:55:42 +08:00
2025-07-19 22:29:50 +08:00
public DeviceService(IMapper mapper, DeviceRepository repository) : base(mapper, repository)
{
2025-07-19 22:29:50 +08:00
_deviceRepository = repository;
_mapper = mapper;
}
2025-07-19 19:55:42 +08:00
public async Task<List<Device>> GetAllAsync()
{
2025-07-19 19:55:42 +08:00
var dbDevices = await _deviceRepository.GetAllAsync();
2025-07-19 22:29:50 +08:00
return _mapper.Map<List<Device>>(dbDevices);
}
2025-07-19 19:55:42 +08:00
public async Task<Device> AddAsync(Device device)
{
2025-07-19 19:55:42 +08:00
Device resDevice = null;
2025-07-19 22:29:50 +08:00
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
var dbList = await GetAllAsync();
//查询设备的名字是否存在
// if (dbList.Any(d => d.Name == device.Name || (d.Ip == device.Ip && d.Prot == device.Prot) || d.OpcUaEndpointUrl == device.OpcUaEndpointUrl))
// {
// NlogHelper.Warn("设备的名称Ip:端口OpcUrl,不可以重复。");
// return resDevice;
// }
2025-07-19 22:29:50 +08:00
// 2. 将设备添加到数据库
var addDevice = await _deviceRepository.AddAsync(_mapper.Map<DbDevice>(device));
2025-07-19 19:55:42 +08:00
2025-07-19 22:29:50 +08:00
//判断判断是否添加默认变量表
//if (device.IsAddDefVarTable)
//{
// DbVariableTable dbVariableTable = new DbVariableTable();
// dbVariableTable.Name = "默认变量表";
// dbVariableTable.Description = "默认变量表";
// dbVariableTable.DeviceId = addDevice.Id;
// dbVariableTable.ProtocolType = addDevice.ProtocolType;
// var dbAddVarTable= await _varTableRepository.AddAsync(dbVariableTable);
// if (addDevice.VariableTables==null)
// {
// addDevice.VariableTables= new List<DbVariableTable>();
// }
2025-07-19 19:55:42 +08:00
2025-07-19 22:29:50 +08:00
// addDevice.VariableTables.Add(dbAddVarTable);
//}
2025-07-19 22:29:50 +08:00
// 4. 为新设备添加菜单
//var addDeviceMenuId = await _menuRepository.AddAsync(addDevice);
resDevice = _mapper.Map<Device>(addDevice);
2025-07-19 22:29:50 +08:00
stopwatch.Stop();
NlogHelper.Info($"添加设备 '{device.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
return resDevice;
}
}
}