diff --git a/DMS.Application/Services/DeviceAppService.cs b/DMS.Application/Services/DeviceService.cs
similarity index 97%
rename from DMS.Application/Services/DeviceAppService.cs
rename to DMS.Application/Services/DeviceService.cs
index ae0a40e..0fa01db 100644
--- a/DMS.Application/Services/DeviceAppService.cs
+++ b/DMS.Application/Services/DeviceService.cs
@@ -10,7 +10,7 @@ namespace DMS.Application.Services;
///
/// 实现设备管理的应用服务。
///
-public class DeviceAppService : IDeviceAppService
+public class DeviceService : IDeviceAppService
{
private readonly IRepositoryManager _repoManager;
private readonly IMapper _mapper;
@@ -18,7 +18,7 @@ public class DeviceAppService : IDeviceAppService
///
/// 构造函数,通过依赖注入获取仓储管理器和AutoMapper实例。
///
- public DeviceAppService(IRepositoryManager repoManager, IMapper mapper)
+ public DeviceService(IRepositoryManager repoManager, IMapper mapper)
{
_repoManager = repoManager;
_mapper = mapper;
diff --git a/DMS.Core/DMS.Core.csproj b/DMS.Core/DMS.Core.csproj
index e0c93f7..4bbeeee 100644
--- a/DMS.Core/DMS.Core.csproj
+++ b/DMS.Core/DMS.Core.csproj
@@ -23,6 +23,10 @@
+
+
+
+
diff --git a/DMS.Core/Interfaces/IDatabaseService.cs b/DMS.Core/Interfaces/IDatabaseService.cs
index 6d0d713..834ab85 100644
--- a/DMS.Core/Interfaces/IDatabaseService.cs
+++ b/DMS.Core/Interfaces/IDatabaseService.cs
@@ -2,7 +2,8 @@ namespace DMS.Core.Interfaces
{
public interface IDatabaseService
{
- void InitializeDataBase();
- Task InitializeMenu();
+ void InitializeTables();
+ void InitializeTableIndex();
+ void InitializeMenus();
}
}
\ No newline at end of file
diff --git a/DMS.Core/Interfaces/Repositories/IBaseRepository.cs b/DMS.Core/Interfaces/Repositories/IBaseRepository.cs
index dedf0e9..718e814 100644
--- a/DMS.Core/Interfaces/Repositories/IBaseRepository.cs
+++ b/DMS.Core/Interfaces/Repositories/IBaseRepository.cs
@@ -36,4 +36,10 @@ public interface IBaseRepository where T : class
///
/// 要删除的实体的主键ID。
Task DeleteAsync(T entity);
+
+ ///
+ /// 异步根据ID删除一个实体。
+ ///
+ /// 要删除的实体的主键ID。
+ Task DeleteAsync(int id);
}
\ No newline at end of file
diff --git a/DMS.Infrastructure.UnitTests/Repositories_Test/DeviceRepository_Test.cs b/DMS.Infrastructure.UnitTests/Repositories_Test/DeviceRepository_Test.cs
deleted file mode 100644
index f751849..0000000
--- a/DMS.Infrastructure.UnitTests/Repositories_Test/DeviceRepository_Test.cs
+++ /dev/null
@@ -1,48 +0,0 @@
-using DMS.Config;
-using DMS.Infrastructure.Data;
-using DMS.Infrastructure.Entities;
-using DMS.Infrastructure.Repositories;
-
-namespace DMS.Infrastructure.UnitTests.Repositories_Test;
-
-public class DeviceRepository_Test
-{
- private readonly DeviceRepository _deviceRepository;
-
- public DeviceRepository_Test()
- {
- AppSettings appSettings = new AppSettings();
- appSettings.Database.Database = "dms_test";
- var dbContext = new SqlSugarDbContext(appSettings);
- dbContext.GetInstance()
- .DbMaintenance.CreateDatabase();
- dbContext.GetInstance()
- .CodeFirst.InitTables();
- // dbContext.GetInstance()
- // .DbMaintenance.CreateIndex("Devices", new[] { "name" }, true);
- _deviceRepository = new DeviceRepository(dbContext);
- }
-
- [Fact]
- public async Task AddAsync_Test()
- {
- var dbDevice = FakerHelper.FakeDbDevice();
- var resDevice = await _deviceRepository.AddAsync(dbDevice);
- var res = await _deviceRepository.GetByIdAsync(resDevice.Id);
-
- Assert.NotNull(res);
- }
-
- [Fact]
- public async Task UpdateAsync_Test()
- {
- var dbDevice = FakerHelper.FakeDbDevice();
- var resDevice = await _deviceRepository.AddAsync(dbDevice);
-
- var res2 = await _deviceRepository.GetByIdAsync(resDevice.Id);
- res2.Name = "HaHa";
- var res = await _deviceRepository.UpdateAsync(res2);
-
- Assert.Equal(res, 1);
- }
-}
\ No newline at end of file
diff --git a/DMS.Infrastructure/Entities/DbDevice.cs b/DMS.Infrastructure/Entities/DbDevice.cs
index be72383..b20131f 100644
--- a/DMS.Infrastructure/Entities/DbDevice.cs
+++ b/DMS.Infrastructure/Entities/DbDevice.cs
@@ -7,7 +7,6 @@ namespace DMS.Infrastructure.Entities;
///
/// 设备实体类,对应数据库中的 Devices 表。
///
-[SugarTable("Devices")]
public class DbDevice
{
///
diff --git a/DMS.Infrastructure/Entities/DbMenu.cs b/DMS.Infrastructure/Entities/DbMenu.cs
index 3704adc..8cc4197 100644
--- a/DMS.Infrastructure/Entities/DbMenu.cs
+++ b/DMS.Infrastructure/Entities/DbMenu.cs
@@ -2,7 +2,6 @@ using SqlSugar;
namespace DMS.Infrastructure.Entities;
-[SugarTable("Menus")]
public class DbMenu
{
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
diff --git a/DMS.Infrastructure/Entities/DbMqttServer.cs b/DMS.Infrastructure/Entities/DbMqttServer.cs
index 83f5753..186f28b 100644
--- a/DMS.Infrastructure/Entities/DbMqttServer.cs
+++ b/DMS.Infrastructure/Entities/DbMqttServer.cs
@@ -2,7 +2,6 @@ using SqlSugar;
namespace DMS.Infrastructure.Entities;
-[SugarTable("MqttServers")]
public class DbMqttServer
{
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
diff --git a/DMS.Infrastructure/Entities/DbNlog.cs b/DMS.Infrastructure/Entities/DbNlog.cs
index 5a29a13..61ab977 100644
--- a/DMS.Infrastructure/Entities/DbNlog.cs
+++ b/DMS.Infrastructure/Entities/DbNlog.cs
@@ -6,7 +6,6 @@ namespace DMS.Infrastructure.Entities;
///
/// 数据库实体:对应数据库中的 Logs 表,用于存储应用程序日志。
///
-[SugarTable("Logs")]
public class DbNlog
{
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
diff --git a/DMS.Infrastructure/Entities/DbUser.cs b/DMS.Infrastructure/Entities/DbUser.cs
index 3e7b356..a214742 100644
--- a/DMS.Infrastructure/Entities/DbUser.cs
+++ b/DMS.Infrastructure/Entities/DbUser.cs
@@ -2,7 +2,6 @@
namespace DMS.Infrastructure.Entities;
-[SugarTable("Users")]
public class DbUser
{
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
diff --git a/DMS.Infrastructure/Entities/DbVariable.cs b/DMS.Infrastructure/Entities/DbVariable.cs
index 59b8c1c..b9dae05 100644
--- a/DMS.Infrastructure/Entities/DbVariable.cs
+++ b/DMS.Infrastructure/Entities/DbVariable.cs
@@ -2,7 +2,6 @@
namespace DMS.Infrastructure.Entities;
-[SugarTable("Variables")]
public class DbVariable
{
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
@@ -13,6 +12,7 @@ public class DbVariable
public int PollLevel { get; set; } // 对应 PollLevelType 枚举
public bool IsActive { get; set; }
public int VariableTableId { get; set; }
+ public string S7Address { get; set; }
public string OpcUaNodeId { get; set; }
public bool IsHistoryEnabled { get; set; }
public double HistoryDeadband { get; set; }
diff --git a/DMS.Infrastructure/Entities/DbVariableHistory.cs b/DMS.Infrastructure/Entities/DbVariableHistory.cs
index b477d00..248cca8 100644
--- a/DMS.Infrastructure/Entities/DbVariableHistory.cs
+++ b/DMS.Infrastructure/Entities/DbVariableHistory.cs
@@ -2,7 +2,7 @@ using SqlSugar;
namespace DMS.Infrastructure.Entities;
-[SugarTable("VariableHistories")]
+
public class DbVariableHistory
{
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
diff --git a/DMS.Infrastructure/Entities/DbVariableMqttAlias.cs b/DMS.Infrastructure/Entities/DbVariableMqttAlias.cs
index 545dd40..46dcbb9 100644
--- a/DMS.Infrastructure/Entities/DbVariableMqttAlias.cs
+++ b/DMS.Infrastructure/Entities/DbVariableMqttAlias.cs
@@ -5,7 +5,6 @@ namespace DMS.Infrastructure.Entities;
///
/// 数据库实体:对应数据库中的 VariableMqttAliases 表。
///
-[SugarTable("VariableMqttAliases")]
public class DbVariableMqttAlias
{
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
diff --git a/DMS.Infrastructure/Entities/DbVariableTable.cs b/DMS.Infrastructure/Entities/DbVariableTable.cs
index 932d6c2..1c67962 100644
--- a/DMS.Infrastructure/Entities/DbVariableTable.cs
+++ b/DMS.Infrastructure/Entities/DbVariableTable.cs
@@ -2,7 +2,6 @@
namespace DMS.Infrastructure.Entities;
-[SugarTable("VariableTables")]
public class DbVariableTable
{
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
diff --git a/DMS.Infrastructure/Repositories/BaseRepository.cs b/DMS.Infrastructure/Repositories/BaseRepository.cs
index f7359b2..a2a80eb 100644
--- a/DMS.Infrastructure/Repositories/BaseRepository.cs
+++ b/DMS.Infrastructure/Repositories/BaseRepository.cs
@@ -80,6 +80,7 @@ public abstract class BaseRepository
NlogHelper.Info($"Delete {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
+
///
/// 异步获取所有实体。
diff --git a/DMS.Infrastructure/Repositories/DeviceRepository.cs b/DMS.Infrastructure/Repositories/DeviceRepository.cs
index c33d243..91912bd 100644
--- a/DMS.Infrastructure/Repositories/DeviceRepository.cs
+++ b/DMS.Infrastructure/Repositories/DeviceRepository.cs
@@ -1,4 +1,6 @@
+using System.Diagnostics;
using AutoMapper;
+using DMS.Core.Helper;
using DMS.Core.Interfaces.Repositories;
using DMS.Core.Models;
using DMS.Infrastructure.Data;
@@ -38,4 +40,15 @@ public class DeviceRepository : BaseRepository, IDeviceRepository
public async Task DeleteAsync(Device model) => await base.DeleteAsync(_mapper.Map(model));
+
+ public async Task DeleteAsync(int id)
+ {
+ var stopwatch = new Stopwatch();
+ stopwatch.Start();
+ var result = await Db.Deleteable(new DbDevice() { Id = id })
+ .ExecuteCommandAsync();
+ stopwatch.Stop();
+ NlogHelper.Info($"Delete {typeof(DbDevice)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms");
+ return result;
+ }
}
\ No newline at end of file
diff --git a/DMS.Infrastructure/Repositories/MenuRepository.cs b/DMS.Infrastructure/Repositories/MenuRepository.cs
index 6732bfa..7b65493 100644
--- a/DMS.Infrastructure/Repositories/MenuRepository.cs
+++ b/DMS.Infrastructure/Repositories/MenuRepository.cs
@@ -31,13 +31,37 @@ public class MenuRepository : BaseRepository, IMenuRepository
}
- public async Task GetByIdAsync(int id) => throw new NotImplementedException();
+ public async Task GetByIdAsync(int id)
+ {
+ var dbMenu = await base.GetByIdAsync(id);
+ return _mapper.Map(dbMenu);
+ }
- public async Task> GetAllAsync() => throw new NotImplementedException();
+ public async Task> GetAllAsync()
+ {
+ var dbList = await base.GetAllAsync();
+ return _mapper.Map>(dbList);
+ }
- public async Task AddAsync(MenuBean entity) => throw new NotImplementedException();
+ public async Task AddAsync(MenuBean entity)
+ {
+ var dbMenu = await base.AddAsync(_mapper.Map(entity));
+ return _mapper.Map(dbMenu, entity);
+ }
- public async Task UpdateAsync(MenuBean entity) => throw new NotImplementedException();
+ public async Task UpdateAsync(MenuBean entity) => await base.UpdateAsync(_mapper.Map(entity));
- public async Task DeleteAsync(MenuBean entity) => throw new NotImplementedException();
+
+ public async Task DeleteAsync(MenuBean entity) => await base.DeleteAsync(_mapper.Map(entity));
+
+ public async Task DeleteAsync(int id)
+ {
+ var stopwatch = new Stopwatch();
+ stopwatch.Start();
+ var result = await Db.Deleteable(new DbMenu { Id = id })
+ .ExecuteCommandAsync();
+ stopwatch.Stop();
+ NlogHelper.Info($"Delete {typeof(DbMenu)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms");
+ return result;
+ }
}
\ No newline at end of file
diff --git a/DMS.Infrastructure/Repositories/MqttServerRepository.cs b/DMS.Infrastructure/Repositories/MqttServerRepository.cs
index 65d82f6..2e1b7da 100644
--- a/DMS.Infrastructure/Repositories/MqttServerRepository.cs
+++ b/DMS.Infrastructure/Repositories/MqttServerRepository.cs
@@ -1,4 +1,5 @@
using System.Diagnostics;
+using AutoMapper;
using DMS.Core.Helper;
using DMS.Core.Interfaces.Repositories;
using DMS.Core.Models;
@@ -12,18 +13,44 @@ namespace DMS.Infrastructure.Repositories;
///
public class MqttServerRepository : BaseRepository, IMqttServerRepository
{
- public MqttServerRepository(SqlSugarDbContext dbContext)
+ private readonly IMapper _mapper;
+
+ public MqttServerRepository(IMapper mapper, SqlSugarDbContext dbContext)
: base(dbContext)
{
+ _mapper = mapper;
}
- public async Task GetByIdAsync(int id) => throw new NotImplementedException();
+ public async Task GetByIdAsync(int id)
+ {
+ var dbMqttServer = await base.GetByIdAsync(id);
+ return _mapper.Map(dbMqttServer);
+ }
- public async Task> GetAllAsync() => throw new NotImplementedException();
+ public async Task> GetAllAsync()
+ {
+ var dbList = await base.GetAllAsync();
+ return _mapper.Map>(dbList);
+ }
- public async Task AddAsync(MqttServer entity) => throw new NotImplementedException();
+ public async Task AddAsync(MqttServer entity)
+ {
+ var dbMqttServer = await base.AddAsync(_mapper.Map(entity));
+ return _mapper.Map(dbMqttServer, entity);
+ }
- public async Task UpdateAsync(MqttServer entity) => throw new NotImplementedException();
+ public async Task UpdateAsync(MqttServer entity) => await base.UpdateAsync(_mapper.Map(entity));
- public async Task DeleteAsync(MqttServer entity) => throw new NotImplementedException();
+ public async Task DeleteAsync(MqttServer entity) => await base.DeleteAsync(_mapper.Map(entity));
+
+ public async Task DeleteAsync(int id)
+ {
+ var stopwatch = new Stopwatch();
+ stopwatch.Start();
+ var result = await Db.Deleteable(new MqttServer() { Id = id })
+ .ExecuteCommandAsync();
+ stopwatch.Stop();
+ NlogHelper.Info($"Delete {typeof(MqttServer)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms");
+ return result;
+ }
}
\ No newline at end of file
diff --git a/DMS.Infrastructure/Repositories/RepositoryManager.cs b/DMS.Infrastructure/Repositories/RepositoryManager.cs
index fb90adf..9ad2412 100644
--- a/DMS.Infrastructure/Repositories/RepositoryManager.cs
+++ b/DMS.Infrastructure/Repositories/RepositoryManager.cs
@@ -1,3 +1,4 @@
+using AutoMapper;
using DMS.Core.Interfaces;
using DMS.Core.Interfaces.Repositories;
using DMS.Infrastructure.Data;
@@ -8,21 +9,23 @@ namespace DMS.Infrastructure.Repositories;
public class RepositoryManager : IRepositoryManager
{
private readonly SqlSugarClient _db;
+ private readonly IMapper _mapper;
private readonly SqlSugarDbContext _dbContext;
- public RepositoryManager(SqlSugarDbContext dbContext)
+ public RepositoryManager(IMapper mapper, SqlSugarDbContext dbContext)
{
+ _mapper = mapper;
_dbContext = dbContext;
_db = dbContext.GetInstance();
- Devices = new DeviceRepository(dbContext);
- VariableTables = new VariableTableRepository(dbContext);
- Variables = new VariableRepository(dbContext);
- MqttServers = new MqttServerRepository(dbContext);
- VariableMqttAliases = new VariableMqttAliasRepository(dbContext);
- Menus = new MenuRepository(dbContext);
- VariableHistories = new VariableHistoryRepository(dbContext);
- Users = new UserRepository(dbContext);
+ Devices = new DeviceRepository(mapper, dbContext);
+ VariableTables = new VariableTableRepository(mapper, dbContext);
+ Variables = new VariableRepository(mapper, dbContext);
+ MqttServers = new MqttServerRepository(mapper, dbContext);
+ VariableMqttAliases = new VariableMqttAliasRepository(mapper, dbContext);
+ Menus = new MenuRepository(mapper, dbContext);
+ VariableHistories = new VariableHistoryRepository(mapper, dbContext);
+ Users = new UserRepository(mapper, dbContext);
}
public void Dispose()
diff --git a/DMS.Infrastructure/Repositories/UserRepository.cs b/DMS.Infrastructure/Repositories/UserRepository.cs
index 024207c..955e4ed 100644
--- a/DMS.Infrastructure/Repositories/UserRepository.cs
+++ b/DMS.Infrastructure/Repositories/UserRepository.cs
@@ -1,3 +1,16 @@
+using System.Diagnostics;
+using DMS.Core.Interfaces;
+using DMS.Core.Interfaces.Repositories;
+using DMS.Core.Models;
+using DMS.Infrastructure.Data;
+using DMS.Infrastructure.Entities;
+
+
+///
+/// 用户仓储类,用于操作DbUser实体
+///
+using AutoMapper;
+using DMS.Core.Helper;
using DMS.Core.Interfaces;
using DMS.Core.Interfaces.Repositories;
using DMS.Core.Models;
@@ -11,18 +24,44 @@ namespace DMS.Infrastructure.Repositories;
///
public class UserRepository : BaseRepository, IUserRepository
{
- public UserRepository(SqlSugarDbContext dbContext)
+ private readonly IMapper _mapper;
+
+ public UserRepository(IMapper mapper, SqlSugarDbContext dbContext)
: base(dbContext)
{
+ _mapper = mapper;
}
- public async Task GetByIdAsync(int id) => throw new NotImplementedException();
+ public async Task GetByIdAsync(int id)
+ {
+ var dbUser = await base.GetByIdAsync(id);
+ return _mapper.Map(dbUser);
+ }
- public async Task> GetAllAsync() => throw new NotImplementedException();
+ public async Task> GetAllAsync()
+ {
+ var dbList = await base.GetAllAsync();
+ return _mapper.Map>(dbList);
+ }
- public async Task AddAsync(User entity) => throw new NotImplementedException();
+ public async Task AddAsync(User entity)
+ {
+ var dbUser = await base.AddAsync(_mapper.Map(entity));
+ return _mapper.Map(dbUser, entity);
+ }
- public async Task UpdateAsync(User entity) => throw new NotImplementedException();
+ public async Task UpdateAsync(User entity) => await base.UpdateAsync(_mapper.Map(entity));
- public async Task DeleteAsync(User entity) => throw new NotImplementedException();
+ public async Task DeleteAsync(User entity) => await base.DeleteAsync(_mapper.Map(entity));
+
+ public async Task DeleteAsync(int id)
+ {
+ var stopwatch = new Stopwatch();
+ stopwatch.Start();
+ var result = await Db.Deleteable(new User() { Id = id })
+ .ExecuteCommandAsync();
+ stopwatch.Stop();
+ NlogHelper.Info($"Delete {typeof(DbMenu)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms");
+ return result;
+ }
}
\ No newline at end of file
diff --git a/DMS.Infrastructure/Repositories/VariableHistoryRepository.cs b/DMS.Infrastructure/Repositories/VariableHistoryRepository.cs
index 7150ed8..1b21a79 100644
--- a/DMS.Infrastructure/Repositories/VariableHistoryRepository.cs
+++ b/DMS.Infrastructure/Repositories/VariableHistoryRepository.cs
@@ -1,3 +1,12 @@
+using System.Diagnostics;
+using DMS.Core.Interfaces.Repositories;
+using DMS.Core.Models;
+using DMS.Infrastructure.Data;
+using DMS.Infrastructure.Entities;
+
+
+using AutoMapper;
+using DMS.Core.Helper;
using DMS.Core.Interfaces.Repositories;
using DMS.Core.Models;
using DMS.Infrastructure.Data;
@@ -7,18 +16,44 @@ namespace DMS.Infrastructure.Repositories;
public class VariableHistoryRepository : BaseRepository, IVariableHistoryRepository
{
- public VariableHistoryRepository(SqlSugarDbContext dbContext)
+ private readonly IMapper _mapper;
+
+ public VariableHistoryRepository(IMapper mapper, SqlSugarDbContext dbContext)
: base(dbContext)
{
+ _mapper = mapper;
}
- public async Task GetByIdAsync(int id) => throw new NotImplementedException();
+ public async Task GetByIdAsync(int id)
+ {
+ var dbVariableHistory = await base.GetByIdAsync(id);
+ return _mapper.Map(dbVariableHistory);
+ }
- public async Task> GetAllAsync() => throw new NotImplementedException();
+ public async Task> GetAllAsync()
+ {
+ var dbList = await base.GetAllAsync();
+ return _mapper.Map>(dbList);
+ }
- public async Task AddAsync(VariableHistory entity) => throw new NotImplementedException();
+ public async Task AddAsync(VariableHistory entity)
+ {
+ var dbVariableHistory = await base.AddAsync(_mapper.Map(entity));
+ return _mapper.Map(dbVariableHistory, entity);
+ }
- public async Task UpdateAsync(VariableHistory entity) => throw new NotImplementedException();
+ public async Task UpdateAsync(VariableHistory entity) => await base.UpdateAsync(_mapper.Map(entity));
- public async Task DeleteAsync(VariableHistory entity) => throw new NotImplementedException();
+ public async Task DeleteAsync(VariableHistory entity) => await base.DeleteAsync(_mapper.Map(entity));
+
+ public async Task DeleteAsync(int id)
+ {
+ var stopwatch = new Stopwatch();
+ stopwatch.Start();
+ var result = await Db.Deleteable(new Variable() { Id = id })
+ .ExecuteCommandAsync();
+ stopwatch.Stop();
+ NlogHelper.Info($"Delete {typeof(DbMenu)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms");
+ return result;
+ }
}
\ No newline at end of file
diff --git a/DMS.Infrastructure/Repositories/VariableMqttAliasRepository.cs b/DMS.Infrastructure/Repositories/VariableMqttAliasRepository.cs
index 93e6854..bfddf4b 100644
--- a/DMS.Infrastructure/Repositories/VariableMqttAliasRepository.cs
+++ b/DMS.Infrastructure/Repositories/VariableMqttAliasRepository.cs
@@ -1,7 +1,19 @@
+using System.Diagnostics;
using DMS.Core.Interfaces.Repositories;
using DMS.Infrastructure.Data;
using DMS.Infrastructure.Entities;
+
+///
+/// 变量与MQTT服务器别名关联的数据仓库。
+///
+using AutoMapper;
+using DMS.Core.Helper;
+using DMS.Core.Interfaces.Repositories;
+using DMS.Core.Models;
+using DMS.Infrastructure.Data;
+using DMS.Infrastructure.Entities;
+
namespace DMS.Infrastructure.Repositories;
///
@@ -9,18 +21,44 @@ namespace DMS.Infrastructure.Repositories;
///
public class VariableMqttAliasRepository : BaseRepository, IVariableMqttAliasRepository
{
- public VariableMqttAliasRepository(SqlSugarDbContext dbContext)
+ private readonly IMapper _mapper;
+
+ public VariableMqttAliasRepository(IMapper mapper, SqlSugarDbContext dbContext)
: base(dbContext)
{
+ _mapper = mapper;
}
- public async Task GetByIdAsync(int id) => throw new NotImplementedException();
+ public async Task GetByIdAsync(int id)
+ {
+ var dbVariableMqttAlias = await base.GetByIdAsync(id);
+ return _mapper.Map(dbVariableMqttAlias);
+ }
- public async Task> GetAllAsync() => throw new NotImplementedException();
+ public async Task> GetAllAsync()
+ {
+ var dbList = await base.GetAllAsync();
+ return _mapper.Map>(dbList);
+ }
- public async Task AddAsync(VariableMqttAlias entity) => throw new NotImplementedException();
+ public async Task AddAsync(VariableMqttAlias entity)
+ {
+ var dbVariableMqttAlias = await base.AddAsync(_mapper.Map(entity));
+ return _mapper.Map(dbVariableMqttAlias, entity);
+ }
- public async Task UpdateAsync(VariableMqttAlias entity) => throw new NotImplementedException();
+ public async Task UpdateAsync(VariableMqttAlias entity) => await base.UpdateAsync(_mapper.Map(entity));
- public async Task DeleteAsync(VariableMqttAlias entity) => throw new NotImplementedException();
+ public async Task DeleteAsync(VariableMqttAlias entity) => await base.DeleteAsync(_mapper.Map(entity));
+
+ public async Task DeleteAsync(int id)
+ {
+ var stopwatch = new Stopwatch();
+ stopwatch.Start();
+ var result = await Db.Deleteable(new VariableMqttAlias() { Id = id })
+ .ExecuteCommandAsync();
+ stopwatch.Stop();
+ NlogHelper.Info($"Delete {typeof(DbMenu)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms");
+ return result;
+ }
}
\ No newline at end of file
diff --git a/DMS.Infrastructure/Repositories/VariableRepository.cs b/DMS.Infrastructure/Repositories/VariableRepository.cs
index 8f7e49a..4d65782 100644
--- a/DMS.Infrastructure/Repositories/VariableRepository.cs
+++ b/DMS.Infrastructure/Repositories/VariableRepository.cs
@@ -1,4 +1,6 @@
using System.Diagnostics;
+using AutoMapper;
+using DMS.Core.Helper;
using DMS.Core.Interfaces.Repositories;
using DMS.Core.Models;
using DMS.Infrastructure.Data;
@@ -11,9 +13,12 @@ namespace DMS.Infrastructure.Repositories;
///
public class VariableRepository : BaseRepository, IVariableRepository
{
- public VariableRepository(SqlSugarDbContext dbContext)
+ private readonly IMapper _mapper;
+
+ public VariableRepository(IMapper mapper, SqlSugarDbContext dbContext)
: base(dbContext)
{
+ _mapper = mapper;
}
@@ -99,13 +104,37 @@ public class VariableRepository : BaseRepository, IVariableRepositor
}
}
*/
- public async Task GetByIdAsync(int id) => throw new NotImplementedException();
+ public async Task GetByIdAsync(int id)
+ {
+ var dbVariable = await base.GetByIdAsync(id);
+ return _mapper.Map(dbVariable);
+ }
- public async Task> GetAllAsync() => throw new NotImplementedException();
+ public async Task> GetAllAsync()
+ {
+ var dbList = await base.GetAllAsync();
+ return _mapper.Map>(dbList);
+ }
- public async Task AddAsync(Variable entity) => throw new NotImplementedException();
+ public async Task AddAsync(Variable entity)
+ {
+ var dbVariable = await base.AddAsync(_mapper.Map(entity));
+ return _mapper.Map(dbVariable, entity);
+ }
- public async Task UpdateAsync(Variable entity) => throw new NotImplementedException();
+ public async Task UpdateAsync(Variable entity) => await base.UpdateAsync(_mapper.Map(entity));
- public async Task DeleteAsync(Variable entity) => throw new NotImplementedException();
+ public async Task DeleteAsync(Variable entity) => await base.DeleteAsync(_mapper.Map(entity));
+
+
+ public async Task DeleteAsync(int id)
+ {
+ var stopwatch = new Stopwatch();
+ stopwatch.Start();
+ var result = await Db.Deleteable(new Variable() { Id = id })
+ .ExecuteCommandAsync();
+ stopwatch.Stop();
+ NlogHelper.Info($"Delete {typeof(DbMenu)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms");
+ return result;
+ }
}
\ No newline at end of file
diff --git a/DMS.Infrastructure/Repositories/VariableTableRepository.cs b/DMS.Infrastructure/Repositories/VariableTableRepository.cs
index b5d8d66..fe7c98b 100644
--- a/DMS.Infrastructure/Repositories/VariableTableRepository.cs
+++ b/DMS.Infrastructure/Repositories/VariableTableRepository.cs
@@ -1,3 +1,12 @@
+using System.Diagnostics;
+using DMS.Core.Interfaces.Repositories;
+using DMS.Core.Models;
+using DMS.Infrastructure.Data;
+using DMS.Infrastructure.Entities;
+
+
+using AutoMapper;
+using DMS.Core.Helper;
using DMS.Core.Interfaces.Repositories;
using DMS.Core.Models;
using DMS.Infrastructure.Data;
@@ -7,18 +16,44 @@ namespace DMS.Infrastructure.Repositories;
public class VariableTableRepository : BaseRepository, IVariableTableRepository
{
- public VariableTableRepository(SqlSugarDbContext dbContext)
+ private readonly IMapper _mapper;
+
+ public VariableTableRepository(IMapper mapper, SqlSugarDbContext dbContext)
: base(dbContext)
{
+ _mapper = mapper;
}
- public async Task GetByIdAsync(int id) => throw new NotImplementedException();
+ public async Task GetByIdAsync(int id)
+ {
+ var dbVariableTable = await base.GetByIdAsync(id);
+ return _mapper.Map(dbVariableTable);
+ }
- public async Task> GetAllAsync() => throw new NotImplementedException();
+ public async Task> GetAllAsync()
+ {
+ var dbList = await base.GetAllAsync();
+ return _mapper.Map>(dbList);
+ }
- public async Task AddAsync(VariableTable entity) => throw new NotImplementedException();
+ public async Task AddAsync(VariableTable entity)
+ {
+ var dbVariableTable = await base.AddAsync(_mapper.Map(entity));
+ return _mapper.Map(dbVariableTable, entity);
+ }
- public async Task UpdateAsync(VariableTable entity) => throw new NotImplementedException();
+ public async Task UpdateAsync(VariableTable entity) => await base.UpdateAsync(_mapper.Map(entity));
- public async Task DeleteAsync(VariableTable entity) => throw new NotImplementedException();
+ public async Task DeleteAsync(VariableTable entity) => await base.DeleteAsync(_mapper.Map(entity));
+
+ public async Task DeleteAsync(int id)
+ {
+ var stopwatch = new Stopwatch();
+ stopwatch.Start();
+ var result = await Db.Deleteable(new VariableTable() { Id = id })
+ .ExecuteCommandAsync();
+ stopwatch.Stop();
+ NlogHelper.Info($"Delete {typeof(DbMenu)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms");
+ return result;
+ }
}
\ No newline at end of file
diff --git a/DMS.Infrastructure/Services/BaseService.cs b/DMS.Infrastructure/Services/BaseService.cs
index 1aa8822..efcd595 100644
--- a/DMS.Infrastructure/Services/BaseService.cs
+++ b/DMS.Infrastructure/Services/BaseService.cs
@@ -1,6 +1,7 @@
using AutoMapper;
using DMS.Infrastructure.Repositories;
using System.Threading.Tasks;
+using DMS.Core.Interfaces.Repositories;
namespace DMS.Infrastructure.Services
{
@@ -10,11 +11,10 @@ namespace DMS.Infrastructure.Services
/// 业务逻辑模型类型。
/// 数据库实体类型。
/// 与实体对应的仓储类型。
- public abstract class BaseService
- where TEntity : class, new()
- where TRepository : BaseRepository
+ public abstract class BaseService
+ where TModel : class, new()
+ where TRepository : IBaseRepository
{
- protected readonly IMapper _mapper;
protected readonly TRepository ServerRepository;
///
@@ -22,9 +22,8 @@ namespace DMS.Infrastructure.Services
///
/// AutoMapper 实例,用于对象映射。
/// 仓储实例,用于数据访问。
- protected BaseService(IMapper mapper, TRepository serverRepository)
+ protected BaseService( TRepository serverRepository)
{
- _mapper = mapper;
ServerRepository = serverRepository;
}
@@ -33,10 +32,9 @@ namespace DMS.Infrastructure.Services
///
/// 要添加的业务模型对象。
/// 返回添加后的数据库实体。
- public virtual async Task AddAsync(TModel model)
+ public virtual async Task AddAsync(TModel model)
{
- var entity = _mapper.Map(model);
- return await ServerRepository.AddAsync(entity);
+ return await ServerRepository.AddAsync(model);
}
///
@@ -46,8 +44,7 @@ namespace DMS.Infrastructure.Services
/// 返回受影响的行数。
public virtual async Task UpdateAsync(TModel model)
{
- var entity = _mapper.Map(model);
- return await ServerRepository.UpdateAsync(entity);
+ return await ServerRepository.UpdateAsync(model);
}
///
@@ -57,8 +54,17 @@ namespace DMS.Infrastructure.Services
/// 返回受影响的行数。
public virtual async Task DeleteAsync(TModel model)
{
- var entity = _mapper.Map(model);
- return await ServerRepository.DeleteAsync(entity);
+ return await ServerRepository.DeleteAsync(model);
+ }
+
+ public virtual async Task> GetAllAsync()
+ {
+ return await ServerRepository.GetAllAsync();
+ }
+
+ public virtual async Task GetByIdAsync(int id)
+ {
+ return await ServerRepository.GetByIdAsync(id);
}
}
}
diff --git a/DMS.Infrastructure/Services/DatabaseInitializerService.cs b/DMS.Infrastructure/Services/DatabaseInitializerService.cs
deleted file mode 100644
index d4a7ba6..0000000
--- a/DMS.Infrastructure/Services/DatabaseInitializerService.cs
+++ /dev/null
@@ -1,57 +0,0 @@
-using DMS.Config;
-using DMS.Core.Enums;
-using DMS.Core.Models;
-using DMS.Infrastructure.Data;
-using DMS.Infrastructure.Entities;
-using SqlSugar;
-using System;
-using System.Linq;
-using System.Threading.Tasks;
-using DMS.Core.Interfaces;
-
-namespace DMS.Infrastructure.Services
-{
- public class DatabaseInitializerService : IDatabaseService
- {
- private readonly SqlSugarClient _db;
-
- public DatabaseInitializerService(SqlSugarDbContext dbContext)
- {
- _db = dbContext.GetInstance();
- }
-
- public void InitializeDataBase()
- {
- _db.DbMaintenance.CreateDatabase();
- _db.CodeFirst.InitTables();
- _db.CodeFirst.InitTables();
- _db.CodeFirst.InitTables();
- _db.CodeFirst.InitTables();
- _db.CodeFirst.InitTables();
- _db.CodeFirst.InitTables();
- _db.CodeFirst.InitTables();
- _db.CodeFirst.InitTables();
- _db.CodeFirst.InitTables();
- }
-
- public Task InitializeMenu()
- {
- var settings = AppSettings.Load();
- if (settings.Menus.Any())
- {
- return Task.CompletedTask;
- }
-
- settings.Menus.Add(new MenuBean() { Id=1, Header = "主页", Icon = "Home", ParentId = 0 });
- settings.Menus.Add(new MenuBean() { Id = 2, Header = "设备", Icon = "Devices3", ParentId = 0 });
- settings.Menus.Add(new MenuBean() { Id = 3, Header = "数据转换", Icon = "ChromeSwitch", ParentId = 0 });
- settings.Menus.Add(new MenuBean() { Id = 4, Header = "Mqtt服务器", Icon = "Cloud", ParentId = 0 });
- settings.Menus.Add(new MenuBean() { Id = 5, Header = "设置", Icon = "Settings", ParentId = 0 });
- settings.Menus.Add(new MenuBean() { Id = 6, Header = "关于", Icon = "Info", ParentId = 0 });
-
- settings.Save();
-
- return Task.CompletedTask;
- }
- }
-}
diff --git a/DMS.Infrastructure/Services/DatabaseService.cs b/DMS.Infrastructure/Services/DatabaseService.cs
new file mode 100644
index 0000000..a689266
--- /dev/null
+++ b/DMS.Infrastructure/Services/DatabaseService.cs
@@ -0,0 +1,76 @@
+using DMS.Config;
+using DMS.Core.Interfaces;
+using DMS.Core.Models;
+using DMS.Infrastructure.Data;
+using DMS.Infrastructure.Entities;
+using SqlSugar;
+
+namespace DMS.Infrastructure.Services;
+
+
+
+public class DatabaseService : IDatabaseService
+{
+ private readonly SqlSugarDbContext _dbContext;
+ private readonly SqlSugarClient _db;
+
+ public DatabaseService(SqlSugarDbContext dbContext)
+ {
+ _dbContext = dbContext;
+ _db = _dbContext.GetInstance();
+ }
+
+ public void InitializeTables()
+ {
+
+ _db.DbMaintenance.CreateDatabase();
+ _db.CodeFirst.InitTables();
+ _db.CodeFirst.InitTables();
+ _db.CodeFirst.InitTables();
+ _db.CodeFirst.InitTables();
+ _db.CodeFirst.InitTables();
+ _db.CodeFirst.InitTables();
+ _db.CodeFirst.InitTables();
+ _db.CodeFirst.InitTables();
+ _db.CodeFirst.InitTables();
+ }
+
+ public void InitializeTableIndex()
+ {
+ _db.DbMaintenance.CreateIndex(nameof(DbDevice),new []
+ {
+ nameof(DbDevice.Name),
+ nameof(DbDevice.OpcUaServerUrl),
+ },true);
+
+ _db.DbMaintenance.CreateIndex(nameof(DbVariable),new []
+ {
+ nameof(DbVariable.OpcUaNodeId)
+ },true);
+ _db.DbMaintenance.CreateIndex(nameof(DbMqttServer),new []
+ {
+ nameof(DbMqttServer.ServerName)
+ },true);
+ }
+
+
+ public void InitializeMenus()
+ {
+ var settings = AppSettings.Load();
+ if (settings.Menus.Any())
+ {
+ return ;
+ }
+
+ settings.Menus.Add(new MenuBean() { Id=1, Header = "主页", Icon = "Home", ParentId = 0 });
+ settings.Menus.Add(new MenuBean() { Id = 2, Header = "设备", Icon = "Devices3", ParentId = 0 });
+ settings.Menus.Add(new MenuBean() { Id = 3, Header = "数据转换", Icon = "ChromeSwitch", ParentId = 0 });
+ settings.Menus.Add(new MenuBean() { Id = 4, Header = "Mqtt服务器", Icon = "Cloud", ParentId = 0 });
+ settings.Menus.Add(new MenuBean() { Id = 5, Header = "设置", Icon = "Settings", ParentId = 0 });
+ settings.Menus.Add(new MenuBean() { Id = 6, Header = "关于", Icon = "Info", ParentId = 0 });
+
+ settings.Save();
+
+ return ;
+ }
+}
\ No newline at end of file
diff --git a/DMS.Infrastructure/Services/DeviceService.cs b/DMS.Infrastructure/Services/DeviceService.cs
index 3c37870..6dacd2f 100644
--- a/DMS.Infrastructure/Services/DeviceService.cs
+++ b/DMS.Infrastructure/Services/DeviceService.cs
@@ -16,69 +16,14 @@ using DMS.Core.Interfaces.Repositories;
namespace DMS.Infrastructure.Services
{
- public class DeviceService :BaseService
+ public class DeviceService : BaseService
{
private readonly IDeviceRepository _deviceRepository;
- private readonly IMenuRepository _menuRepository;
- private readonly IVariableTableRepository _variableTableRepository;
- private readonly IMapper _mapper;
-
- public DeviceService(IMapper mapper, DeviceRepository repository) : base(mapper, repository)
+ public DeviceService(DeviceRepository repository) : base(repository)
{
_deviceRepository = repository;
- _mapper = mapper;
}
- public async Task> GetAllAsync()
- {
- var dbDevices = await _deviceRepository.GetAllAsync();
-
- return _mapper.Map>(dbDevices);
- }
-
-
- public async Task AddAsync(Device device)
- {
- Device resDevice = null;
- 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;
- // }
- // 2. 将设备添加到数据库
- var addDevice = await _deviceRepository.AddAsync(_mapper.Map(device));
-
- //判断判断是否添加默认变量表
- //if (device.IsAddDefVarTable)
- //{
- // DbVariableTable dbVariableTable = new DbVariableTable();
- // dbVariableTable.Name = "默认变量表";
- // dbVariableTable.Description = "默认变量表";
- // dbVariableTable.DeviceId = addDevice.Id;
- // dbVariableTable.ProtocolType = addDevice.ProtocolType;
- // var dbAddVarTable= await _variableTableRepository.AddAsync(dbVariableTable);
- // if (addDevice.VariableTables==null)
- // {
- // addDevice.VariableTables= new List();
- // }
-
- // addDevice.VariableTables.Add(dbAddVarTable);
- //}
-
- // 4. 为新设备添加菜单
- //var addDeviceMenuId = await _menuRepository.AddAsync(addDevice);
- resDevice = _mapper.Map(addDevice);
-
-
- stopwatch.Stop();
- NlogHelper.Info($"添加设备 '{device.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
- return resDevice;
-
- }
}
-}
+}
\ No newline at end of file
diff --git a/DMS.Infrastructure/Services/MenuService.cs b/DMS.Infrastructure/Services/MenuService.cs
index 37cf9c9..f392302 100644
--- a/DMS.Infrastructure/Services/MenuService.cs
+++ b/DMS.Infrastructure/Services/MenuService.cs
@@ -5,9 +5,9 @@ using DMS.Infrastructure.Repositories;
namespace DMS.Infrastructure.Services
{
- public class MenuService : BaseService
+ public class MenuService : BaseService
{
- public MenuService(IMapper mapper, MenuRepository repository) : base(mapper, repository)
+ public MenuService(MenuRepository repository) : base(repository)
{
}
}
diff --git a/DMS.Infrastructure/Services/MqttService.cs b/DMS.Infrastructure/Services/MqttService.cs
index 7d84552..87ab217 100644
--- a/DMS.Infrastructure/Services/MqttService.cs
+++ b/DMS.Infrastructure/Services/MqttService.cs
@@ -13,9 +13,9 @@ using System.Threading.Tasks;
namespace DMS.Infrastructure.Services
{
- public class MqttService:BaseService
+ public class MqttService:BaseService
{
- public MqttService(IMapper mapper, MqttServerRepository serverRepository) : base(mapper, serverRepository)
+ public MqttService( MqttServerRepository serverRepository) : base( serverRepository)
{
}
}
diff --git a/DMS.Infrastructure/Services/VarTableService.cs b/DMS.Infrastructure/Services/VarTableService.cs
deleted file mode 100644
index d696f39..0000000
--- a/DMS.Infrastructure/Services/VarTableService.cs
+++ /dev/null
@@ -1,13 +0,0 @@
-using AutoMapper;
-using DMS.Core.Models;
-using DMS.Infrastructure.Entities;
-using DMS.Infrastructure.Repositories;
-
-namespace DMS.Infrastructure.Services;
-
-public class VarTableService : BaseService
-{
- public VarTableService(IMapper mapper, VariableTableRepository repository) : base(mapper, repository)
- {
- }
-}
\ No newline at end of file
diff --git a/DMS.Infrastructure/Services/VarDataService.cs b/DMS.Infrastructure/Services/VariableService.cs
similarity index 59%
rename from DMS.Infrastructure/Services/VarDataService.cs
rename to DMS.Infrastructure/Services/VariableService.cs
index 1cc73da..7951ad3 100644
--- a/DMS.Infrastructure/Services/VarDataService.cs
+++ b/DMS.Infrastructure/Services/VariableService.cs
@@ -13,13 +13,11 @@ using DMS.Infrastructure.Repositories;
namespace DMS.Infrastructure.Services
{
- public class VarDataService : BaseService
+ public class VariableService : BaseService
{
- private readonly IMapper _mapper;
- public VarDataService(IMapper mapper, VariableRepository repository) : base(mapper, repository)
+ public VariableService(VariableRepository repository) : base(repository)
{
- _mapper = mapper;
}
}
diff --git a/DMS.Infrastructure/Services/VariableTableService.cs b/DMS.Infrastructure/Services/VariableTableService.cs
new file mode 100644
index 0000000..61a26f4
--- /dev/null
+++ b/DMS.Infrastructure/Services/VariableTableService.cs
@@ -0,0 +1,13 @@
+using AutoMapper;
+using DMS.Core.Models;
+using DMS.Infrastructure.Entities;
+using DMS.Infrastructure.Repositories;
+
+namespace DMS.Infrastructure.Services;
+
+public class VariableTableService : BaseService
+{
+ public VariableTableService(VariableTableRepository repository) : base(repository)
+ {
+ }
+}
\ No newline at end of file