diff --git a/DMS.Infrastructure/Interfaces/IDeviceRepository.cs b/DMS.Infrastructure/Interfaces/IDeviceRepository.cs index 3ab30b3..76ea551 100644 --- a/DMS.Infrastructure/Interfaces/IDeviceRepository.cs +++ b/DMS.Infrastructure/Interfaces/IDeviceRepository.cs @@ -6,10 +6,10 @@ namespace DMS.Infrastructure.Interfaces { public interface IDeviceRepository { - Task AddAsync(Device model); - Task UpdateAsync(Device model); - Task DeleteAsync(Device model); - Task> GetAllAsync(); - Task GetByIdAsync(int id); + Task AddAsync(DbDevice model); + Task UpdateAsync(DbDevice model); + Task DeleteAsync(DbDevice model); + Task> GetAllAsync(); + Task GetByIdAsync(int id); } } \ No newline at end of file diff --git a/DMS.Infrastructure/Interfaces/IDeviceService.cs b/DMS.Infrastructure/Interfaces/IDeviceService.cs index 4fbfd97..1c46af8 100644 --- a/DMS.Infrastructure/Interfaces/IDeviceService.cs +++ b/DMS.Infrastructure/Interfaces/IDeviceService.cs @@ -6,7 +6,7 @@ namespace DMS.Infrastructure.Interfaces { public interface IDeviceService { - Task DeleteAsync(Device device, List menus); - Task AddAsync(Device device); + Task AddAsync(Device device); + Task> GetAllAsync(); } } \ No newline at end of file diff --git a/DMS.Infrastructure/Interfaces/IVarTableRepository.cs b/DMS.Infrastructure/Interfaces/IVarTableRepository.cs index f0fe545..c81cc65 100644 --- a/DMS.Infrastructure/Interfaces/IVarTableRepository.cs +++ b/DMS.Infrastructure/Interfaces/IVarTableRepository.cs @@ -1,17 +1,17 @@ using System.Collections.Generic; using System.Threading.Tasks; using DMS.Core.Models; +using DMS.Infrastructure.Entities; namespace DMS.Infrastructure.Interfaces { public interface IVarTableRepository { - Task AddAsync(VariableTable varTable); - Task AddAsync(VariableTable variableTable, ITransaction db); - Task UpdateAsync(VariableTable variableTable); - Task UpdateAsync(VariableTable variableTable, ITransaction db); - Task DeleteAsync(VariableTable variableTable); - Task DeleteAsync(VariableTable varTable, ITransaction db); - + Task AddAsync(DbVariableTable varTable); + Task UpdateAsync(DbVariableTable variableTable); + Task DeleteAsync(DbVariableTable variableTable); + Task> GetAllAsync(); + Task GetByIdAsync(int id); + } } \ No newline at end of file diff --git a/DMS.Infrastructure/Repositories/BaseRepository.cs b/DMS.Infrastructure/Repositories/BaseRepository.cs index 1c2f3b4..1036c4d 100644 --- a/DMS.Infrastructure/Repositories/BaseRepository.cs +++ b/DMS.Infrastructure/Repositories/BaseRepository.cs @@ -8,18 +8,40 @@ using System.Linq.Expressions; namespace DMS.Infrastructure.Repositories; +/// +/// 通用仓储基类,封装了对实体对象的常用 CRUD 操作。 +/// +/// 实体类型,必须是引用类型且具有无参构造函数。 public abstract class BaseRepository where TEntity : class, new() { private readonly ITransaction _transaction; + /// + /// 获取当前事务的 SqlSugarClient 实例,用于数据库操作。 + /// protected SqlSugarClient Db => _transaction.GetInstance(); + /// + /// 获取到当前的事务 + /// + /// + public ITransaction GetTransaction() => _transaction; + + /// + /// 初始化 BaseRepository 的新实例。 + /// + /// 事务管理对象,通过依赖注入提供。 protected BaseRepository(ITransaction transaction) { this._transaction = transaction; } + /// + /// 异步添加一个新实体。 + /// + /// 要添加的实体对象。 + /// 返回已添加的实体对象(可能包含数据库生成的主键等信息)。 public virtual async Task AddAsync(TEntity entity) { Stopwatch stopwatch = new Stopwatch(); @@ -30,6 +52,11 @@ public abstract class BaseRepository return result; } + /// + /// 异步更新一个现有实体。 + /// + /// 要更新的实体对象。 + /// 返回受影响的行数。 public virtual async Task UpdateAsync(TEntity entity) { Stopwatch stopwatch = new Stopwatch(); @@ -40,6 +67,11 @@ public abstract class BaseRepository return result; } + /// + /// 异步删除一个实体。 + /// + /// 要删除的实体对象。 + /// 返回受影响的行数。 public virtual async Task DeleteAsync(TEntity entity) { Stopwatch stopwatch = new Stopwatch(); @@ -50,6 +82,10 @@ public abstract class BaseRepository return result; } + /// + /// 异步获取所有实体。 + /// + /// 返回包含所有实体的列表。 public virtual async Task> GetAllAsync() { Stopwatch stopwatch = new Stopwatch(); @@ -60,6 +96,11 @@ public abstract class BaseRepository return entities; } + /// + /// 异步根据主键 ID 获取单个实体。 + /// + /// 实体的主键 ID。 + /// 返回找到的实体,如果未找到则返回 null。 public virtual async Task GetByIdAsync(int id) { Stopwatch stopwatch = new Stopwatch(); @@ -70,6 +111,11 @@ public abstract class BaseRepository return entity; } + /// + /// 异步根据指定条件获取单个实体。 + /// + /// 查询条件的 Lambda 表达式。 + /// 返回满足条件的第一个实体,如果未找到则返回 null。 public virtual async Task GetByConditionAsync(Expression> expression) { Stopwatch stopwatch = new Stopwatch(); diff --git a/DMS.Infrastructure/Repositories/DeviceRepository.cs b/DMS.Infrastructure/Repositories/DeviceRepository.cs index f219317..9ca8179 100644 --- a/DMS.Infrastructure/Repositories/DeviceRepository.cs +++ b/DMS.Infrastructure/Repositories/DeviceRepository.cs @@ -10,7 +10,7 @@ using DMS.Infrastructure.Interfaces; namespace DMS.Infrastructure.Repositories; -public class DeviceRepository : BaseRepository +public class DeviceRepository : BaseRepository,IDeviceRepository { public DeviceRepository(ITransaction transaction) @@ -19,5 +19,4 @@ public class DeviceRepository : BaseRepository } - } \ No newline at end of file diff --git a/DMS.Infrastructure/Repositories/VarTableRepository.cs b/DMS.Infrastructure/Repositories/VarTableRepository.cs index d16d647..e4cce35 100644 --- a/DMS.Infrastructure/Repositories/VarTableRepository.cs +++ b/DMS.Infrastructure/Repositories/VarTableRepository.cs @@ -7,79 +7,12 @@ using DMS.Infrastructure.Data; namespace DMS.Infrastructure.Repositories; -public class VarTableRepository : BaseRepository +public class VarTableRepository : BaseRepository, IVarTableRepository { public VarTableRepository(SqlSugarDbContext dbContext) : base(dbContext) { } - /// - /// 添加变量表 - /// - /// - /// 变量表的ID - public override async Task 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; - } - - /// - /// 编辑变量表 - /// - /// - /// - public override async Task 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; - } - - /// - /// 删除变量表 - /// - /// - /// - public override async Task 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; - } - - /// - /// 删除变量表支持事务 - /// - /// - /// - public async Task DeleteAsync(IEnumerable deviceVariableTables) - { - if (deviceVariableTables == null || deviceVariableTables.Count() == 0) - return; - // 转换对象 - await Db.Deleteable(deviceVariableTables) - .ExecuteCommandAsync(); - } - - } \ No newline at end of file diff --git a/DMS.Infrastructure/Services/DeviceService.cs b/DMS.Infrastructure/Services/DeviceService.cs index 09281e9..8f8755d 100644 --- a/DMS.Infrastructure/Services/DeviceService.cs +++ b/DMS.Infrastructure/Services/DeviceService.cs @@ -11,6 +11,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Threading.Tasks; +using System.Collections.Concurrent; namespace DMS.Infrastructure.Services { @@ -20,7 +21,8 @@ namespace DMS.Infrastructure.Services private readonly IMenuRepository _menuRepository; private readonly IVarTableRepository _varTableRepository; private readonly IMapper _mapper; - private readonly SqlSugarClient Db; // Assuming DbContext is accessible or passed + + private ConcurrentDictionary _devicesDic; public DeviceService(DeviceRepository deviceRepository, IMenuRepository menuRepository, IVarTableRepository varTableRepository, IMapper mapper, SqlSugarDbContext dbContext) { @@ -28,44 +30,72 @@ namespace DMS.Infrastructure.Services _menuRepository = menuRepository; _varTableRepository = varTableRepository; _mapper = mapper; - Db = dbContext.GetInstance(); + _devicesDic = new ConcurrentDictionary(); } - public async Task DeleteAsync(Device device, List menus) + public async Task> GetAllAsync() { - Stopwatch stopwatch = new Stopwatch(); - stopwatch.Start(); - var result = await Db.Deleteable(new DbDevice { Id = device.Id }) - .ExecuteCommandAsync(); - // 删除变量表 - //await _varTableRepository.DeleteAsync(device.VariableTables); + var dbDevices = await _deviceRepository.GetAllAsync(); - stopwatch.Stop(); - NlogHelper.Info($"删除设备:{device.Name},耗时:{stopwatch.ElapsedMilliseconds}ms"); - return result; + var deviceDic = _mapper.Map>(dbDevices).ToDictionary(d => d.Id); + _devicesDic = new ConcurrentDictionary(deviceDic); + + return deviceDic.Values.ToList(); } - public async Task AddAsync(Device device) + + public async Task AddAsync(Device device) { - Stopwatch stopwatch = new Stopwatch(); - stopwatch.Start(); + Device resDevice = null; + try + { + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); - //查询设备的名字是否存在 - var exist = await Db.Queryable() - .Where(d => d.Name == device.Name) - .FirstAsync(); - if (exist != null) - throw new InvalidOperationException("设备名称已经存在。"); + //查询设备的名字是否存在 + if (_devicesDic.Values.Any(d => d.Name == device.Name || (d.Ip == device.Ip && d.Prot == device.Prot) || d.OpcUaEndpointUrl == device.OpcUaEndpointUrl)) + { + NlogHelper.Warn("设备的名称,Ip:端口,OpcUrl,不可以重复。"); + return resDevice; + } + await _deviceRepository.GetTransaction().BeginTranAsync(); + // 2. 将设备添加到数据库 + var addDevice = await _deviceRepository.AddAsync(_mapper.Map(device)); - // 2. 将设备添加到数据库 - var addDevice = await Db.Insertable(_mapper.Map(device)) - .ExecuteReturnEntityAsync(); + //判断判断是否添加默认变量表 + 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(); + } - // 4. 为新设备添加菜单 - //var addDeviceMenuId = await _menuRepository.AddAsync(addDevice); + addDevice.VariableTables.Add(dbAddVarTable); + } + + // 4. 为新设备添加菜单 + //var addDeviceMenuId = await _menuRepository.AddAsync(addDevice); + resDevice = _mapper.Map(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"); } } }