修改添加设备部分功能

This commit is contained in:
2025-07-19 19:55:42 +08:00
parent 76e46614cc
commit 5cf25dd9ac
7 changed files with 120 additions and 112 deletions

View File

@@ -6,10 +6,10 @@ namespace DMS.Infrastructure.Interfaces
{ {
public interface IDeviceRepository public interface IDeviceRepository
{ {
Task<int> AddAsync(Device model); Task<DbDevice> AddAsync(DbDevice model);
Task<int> UpdateAsync(Device model); Task<int> UpdateAsync(DbDevice model);
Task<int> DeleteAsync(Device model); Task<int> DeleteAsync(DbDevice model);
Task<List<Device>> GetAllAsync(); Task<List<DbDevice>> GetAllAsync();
Task<Device> GetByIdAsync(int id); Task<DbDevice> GetByIdAsync(int id);
} }
} }

View File

@@ -6,7 +6,7 @@ namespace DMS.Infrastructure.Interfaces
{ {
public interface IDeviceService public interface IDeviceService
{ {
Task<int> DeleteAsync(Device device, List<MenuBean> menus); Task<Device> AddAsync(Device device);
Task AddAsync(Device device); Task<List<Device>> GetAllAsync();
} }
} }

View File

@@ -1,17 +1,17 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using DMS.Core.Models; using DMS.Core.Models;
using DMS.Infrastructure.Entities;
namespace DMS.Infrastructure.Interfaces namespace DMS.Infrastructure.Interfaces
{ {
public interface IVarTableRepository public interface IVarTableRepository
{ {
Task<VariableTable> AddAsync(VariableTable varTable); Task<DbVariableTable> AddAsync(DbVariableTable varTable);
Task<VariableTable> AddAsync(VariableTable variableTable, ITransaction db); Task<int> UpdateAsync(DbVariableTable variableTable);
Task<int> UpdateAsync(VariableTable variableTable); Task<int> DeleteAsync(DbVariableTable variableTable);
Task<int> UpdateAsync(VariableTable variableTable, ITransaction db); Task<List<DbVariableTable>> GetAllAsync();
Task<int> DeleteAsync(VariableTable variableTable); Task<DbVariableTable> GetByIdAsync(int id);
Task<int> DeleteAsync(VariableTable varTable, ITransaction db);
} }
} }

View File

@@ -8,18 +8,40 @@ using System.Linq.Expressions;
namespace DMS.Infrastructure.Repositories; namespace DMS.Infrastructure.Repositories;
/// <summary>
/// 通用仓储基类,封装了对实体对象的常用 CRUD 操作。
/// </summary>
/// <typeparam name="TEntity">实体类型,必须是引用类型且具有无参构造函数。</typeparam>
public abstract class BaseRepository<TEntity> public abstract class BaseRepository<TEntity>
where TEntity : class, new() where TEntity : class, new()
{ {
private readonly ITransaction _transaction; private readonly ITransaction _transaction;
/// <summary>
/// 获取当前事务的 SqlSugarClient 实例,用于数据库操作。
/// </summary>
protected SqlSugarClient Db => _transaction.GetInstance(); protected SqlSugarClient Db => _transaction.GetInstance();
/// <summary>
/// 获取到当前的事务
/// </summary>
/// <returns></returns>
public ITransaction GetTransaction() => _transaction;
/// <summary>
/// 初始化 BaseRepository 的新实例。
/// </summary>
/// <param name="transaction">事务管理对象,通过依赖注入提供。</param>
protected BaseRepository(ITransaction transaction) protected BaseRepository(ITransaction transaction)
{ {
this._transaction = transaction; this._transaction = transaction;
} }
/// <summary>
/// 异步添加一个新实体。
/// </summary>
/// <param name="entity">要添加的实体对象。</param>
/// <returns>返回已添加的实体对象(可能包含数据库生成的主键等信息)。</returns>
public virtual async Task<TEntity> AddAsync(TEntity entity) public virtual async Task<TEntity> AddAsync(TEntity entity)
{ {
Stopwatch stopwatch = new Stopwatch(); Stopwatch stopwatch = new Stopwatch();
@@ -30,6 +52,11 @@ public abstract class BaseRepository<TEntity>
return result; return result;
} }
/// <summary>
/// 异步更新一个现有实体。
/// </summary>
/// <param name="entity">要更新的实体对象。</param>
/// <returns>返回受影响的行数。</returns>
public virtual async Task<int> UpdateAsync(TEntity entity) public virtual async Task<int> UpdateAsync(TEntity entity)
{ {
Stopwatch stopwatch = new Stopwatch(); Stopwatch stopwatch = new Stopwatch();
@@ -40,6 +67,11 @@ public abstract class BaseRepository<TEntity>
return result; return result;
} }
/// <summary>
/// 异步删除一个实体。
/// </summary>
/// <param name="entity">要删除的实体对象。</param>
/// <returns>返回受影响的行数。</returns>
public virtual async Task<int> DeleteAsync(TEntity entity) public virtual async Task<int> DeleteAsync(TEntity entity)
{ {
Stopwatch stopwatch = new Stopwatch(); Stopwatch stopwatch = new Stopwatch();
@@ -50,6 +82,10 @@ public abstract class BaseRepository<TEntity>
return result; return result;
} }
/// <summary>
/// 异步获取所有实体。
/// </summary>
/// <returns>返回包含所有实体的列表。</returns>
public virtual async Task<List<TEntity>> GetAllAsync() public virtual async Task<List<TEntity>> GetAllAsync()
{ {
Stopwatch stopwatch = new Stopwatch(); Stopwatch stopwatch = new Stopwatch();
@@ -60,6 +96,11 @@ public abstract class BaseRepository<TEntity>
return entities; return entities;
} }
/// <summary>
/// 异步根据主键 ID 获取单个实体。
/// </summary>
/// <param name="id">实体的主键 ID。</param>
/// <returns>返回找到的实体,如果未找到则返回 null。</returns>
public virtual async Task<TEntity> GetByIdAsync(int id) public virtual async Task<TEntity> GetByIdAsync(int id)
{ {
Stopwatch stopwatch = new Stopwatch(); Stopwatch stopwatch = new Stopwatch();
@@ -70,6 +111,11 @@ public abstract class BaseRepository<TEntity>
return entity; return entity;
} }
/// <summary>
/// 异步根据指定条件获取单个实体。
/// </summary>
/// <param name="expression">查询条件的 Lambda 表达式。</param>
/// <returns>返回满足条件的第一个实体,如果未找到则返回 null。</returns>
public virtual async Task<TEntity> GetByConditionAsync(Expression<Func<TEntity, bool>> expression) public virtual async Task<TEntity> GetByConditionAsync(Expression<Func<TEntity, bool>> expression)
{ {
Stopwatch stopwatch = new Stopwatch(); Stopwatch stopwatch = new Stopwatch();

View File

@@ -10,7 +10,7 @@ using DMS.Infrastructure.Interfaces;
namespace DMS.Infrastructure.Repositories; namespace DMS.Infrastructure.Repositories;
public class DeviceRepository : BaseRepository<DbDevice> public class DeviceRepository : BaseRepository<DbDevice>,IDeviceRepository
{ {
public DeviceRepository(ITransaction transaction) public DeviceRepository(ITransaction transaction)
@@ -19,5 +19,4 @@ public class DeviceRepository : BaseRepository<DbDevice>
} }
} }

View File

@@ -7,79 +7,12 @@ using DMS.Infrastructure.Data;
namespace DMS.Infrastructure.Repositories; namespace DMS.Infrastructure.Repositories;
public class VarTableRepository : BaseRepository<DbVariableTable> public class VarTableRepository : BaseRepository<DbVariableTable>, IVarTableRepository
{ {
public VarTableRepository(SqlSugarDbContext dbContext) public VarTableRepository(SqlSugarDbContext dbContext)
: base(dbContext) : base(dbContext)
{ {
} }
/// <summary>
/// 添加变量表
/// </summary>
/// <param name="varTable"></param>
/// <returns>变量表的ID</returns>
public override async Task<DbVariableTable> AddAsync(DbVariableTable entity)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
var addVarTabel = await Db.Insertable(entity)
.ExecuteReturnEntityAsync();
stopwatch.Stop();
//NlogHelper.Info($"添加变量表 '{entity.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
return addVarTabel;
}
/// <summary>
/// 编辑变量表
/// </summary>
/// <param name="variableTable"></param>
/// <returns></returns>
public override async Task<int> UpdateAsync(DbVariableTable entity)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
var result = await Db.Updateable(entity)
.ExecuteCommandAsync();
stopwatch.Stop();
//NlogHelper.Info($"编辑变量表 '{entity.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
/// <summary>
/// 删除变量表
/// </summary>
/// <param name="variableTable"></param>
/// <returns></returns>
public override async Task<int> DeleteAsync(DbVariableTable entity)
{
if (entity == null )
return 0;
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
// 转换对象
var res= await Db.Deleteable(entity)
.ExecuteCommandAsync();
stopwatch.Stop();
//NlogHelper.Info($"删除变量表 '{entity.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
return res;
}
/// <summary>
/// 删除变量表支持事务
/// </summary>
/// <param name="deviceVariableTables"></param>
/// <param name="db"></param>
public async Task DeleteAsync(IEnumerable<DbVariableTable> deviceVariableTables)
{
if (deviceVariableTables == null || deviceVariableTables.Count() == 0)
return;
// 转换对象
await Db.Deleteable<DbVariableTable>(deviceVariableTables)
.ExecuteCommandAsync();
}
} }

View File

@@ -11,6 +11,7 @@ using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Collections.Concurrent;
namespace DMS.Infrastructure.Services namespace DMS.Infrastructure.Services
{ {
@@ -20,7 +21,8 @@ namespace DMS.Infrastructure.Services
private readonly IMenuRepository _menuRepository; private readonly IMenuRepository _menuRepository;
private readonly IVarTableRepository _varTableRepository; private readonly IVarTableRepository _varTableRepository;
private readonly IMapper _mapper; private readonly IMapper _mapper;
private readonly SqlSugarClient Db; // Assuming DbContext is accessible or passed
private ConcurrentDictionary<int, Device> _devicesDic;
public DeviceService(DeviceRepository deviceRepository, IMenuRepository menuRepository, IVarTableRepository varTableRepository, IMapper mapper, SqlSugarDbContext dbContext) public DeviceService(DeviceRepository deviceRepository, IMenuRepository menuRepository, IVarTableRepository varTableRepository, IMapper mapper, SqlSugarDbContext dbContext)
{ {
@@ -28,44 +30,72 @@ namespace DMS.Infrastructure.Services
_menuRepository = menuRepository; _menuRepository = menuRepository;
_varTableRepository = varTableRepository; _varTableRepository = varTableRepository;
_mapper = mapper; _mapper = mapper;
Db = dbContext.GetInstance(); _devicesDic = new ConcurrentDictionary<int, Device>();
} }
public async Task<int> DeleteAsync(Device device, List<MenuBean> menus) public async Task<List<Device>> GetAllAsync()
{ {
Stopwatch stopwatch = new Stopwatch(); var dbDevices = await _deviceRepository.GetAllAsync();
stopwatch.Start();
var result = await Db.Deleteable<DbDevice>(new DbDevice { Id = device.Id })
.ExecuteCommandAsync();
// 删除变量表
//await _varTableRepository.DeleteAsync(device.VariableTables);
stopwatch.Stop(); var deviceDic = _mapper.Map<List<Device>>(dbDevices).ToDictionary(d => d.Id);
NlogHelper.Info($"删除设备:{device.Name},耗时:{stopwatch.ElapsedMilliseconds}ms"); _devicesDic = new ConcurrentDictionary<int, Device>(deviceDic);
return result;
return deviceDic.Values.ToList();
} }
public async Task AddAsync(Device device)
public async Task<Device> AddAsync(Device device)
{ {
Stopwatch stopwatch = new Stopwatch(); Device resDevice = null;
stopwatch.Start(); try
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
//查询设备的名字是否存在 //查询设备的名字是否存在
var exist = await Db.Queryable<DbDevice>() if (_devicesDic.Values.Any(d => d.Name == device.Name || (d.Ip == device.Ip && d.Prot == device.Prot) || d.OpcUaEndpointUrl == device.OpcUaEndpointUrl))
.Where(d => d.Name == device.Name) {
.FirstAsync(); NlogHelper.Warn("设备的名称Ip:端口OpcUrl,不可以重复。");
if (exist != null) return resDevice;
throw new InvalidOperationException("设备名称已经存在。"); }
await _deviceRepository.GetTransaction().BeginTranAsync();
// 2. 将设备添加到数据库
var addDevice = await _deviceRepository.AddAsync(_mapper.Map<DbDevice>(device));
// 2. 将设备添加到数据库 //判断判断是否添加默认变量表
var addDevice = await Db.Insertable<DbDevice>(_mapper.Map<DbDevice>(device)) if (device.IsAddDefVarTable)
.ExecuteReturnEntityAsync(); {
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>();
}
// 4. 为新设备添加菜单 addDevice.VariableTables.Add(dbAddVarTable);
//var addDeviceMenuId = await _menuRepository.AddAsync(addDevice); }
// 4. 为新设备添加菜单
//var addDeviceMenuId = await _menuRepository.AddAsync(addDevice);
resDevice = _mapper.Map<Device>(addDevice);
await _deviceRepository.GetTransaction().CommitTranAsync();
stopwatch.Stop();
NlogHelper.Info($"添加设备 '{device.Name}' 及相关菜单耗时:{stopwatch.ElapsedMilliseconds}ms");
return resDevice;
}
catch (Exception e)
{
await _deviceRepository.GetTransaction().RollbackTranAsync();
throw;
}
stopwatch.Stop();
NlogHelper.Info($"添加设备 '{device.Name}' 及相关菜单耗时:{stopwatch.ElapsedMilliseconds}ms");
} }
} }
} }