diff --git a/DMS.Core/Interfaces/IDatabaseService.cs b/DMS.Core/Interfaces/IDatabaseService.cs index 834ab85..561a08a 100644 --- a/DMS.Core/Interfaces/IDatabaseService.cs +++ b/DMS.Core/Interfaces/IDatabaseService.cs @@ -5,5 +5,7 @@ namespace DMS.Core.Interfaces void InitializeTables(); void InitializeTableIndex(); void InitializeMenus(); + + bool IsAnyTable(string tableName); } } \ No newline at end of file diff --git a/DMS.Infrastructure.UnitTests/DMS.Infrastructure.UnitTests.csproj b/DMS.Infrastructure.UnitTests/DMS.Infrastructure.UnitTests.csproj index 22fefae..e5ab291 100644 --- a/DMS.Infrastructure.UnitTests/DMS.Infrastructure.UnitTests.csproj +++ b/DMS.Infrastructure.UnitTests/DMS.Infrastructure.UnitTests.csproj @@ -12,6 +12,7 @@ + diff --git a/DMS.Infrastructure.UnitTests/Services/DatabaseServiceTest.cs b/DMS.Infrastructure.UnitTests/Services/DatabaseServiceTest.cs new file mode 100644 index 0000000..843aa81 --- /dev/null +++ b/DMS.Infrastructure.UnitTests/Services/DatabaseServiceTest.cs @@ -0,0 +1,31 @@ +using DMS.Core.Interfaces; +using DMS.Infrastructure.Configurations; +using DMS.Infrastructure.Data; +using DMS.Infrastructure.Services; + +namespace DMS.Infrastructure.UnitTests.Services; + +public class DatabaseServiceTest +{ + + private IDatabaseService _databaseService; + public DatabaseServiceTest() + { + AppSettings appSettings = new AppSettings(); + appSettings.Database.Database = "dms_test"; + SqlSugarDbContext dbContext=new SqlSugarDbContext(appSettings); + _databaseService = new DatabaseService(dbContext); + } + [Fact] + public void InitializeTables_Test() + { + _databaseService.InitializeTables(); + Assert.True(_databaseService.IsAnyTable("dbdevice")); + } + + [Fact] + public void InitializeTableIndex_Test() + { + _databaseService.InitializeTableIndex(); + } +} \ No newline at end of file diff --git a/DMS.Infrastructure.UnitTests/Services/DeviceServiceTest.cs b/DMS.Infrastructure.UnitTests/Services/DeviceServiceTest.cs new file mode 100644 index 0000000..b442540 --- /dev/null +++ b/DMS.Infrastructure.UnitTests/Services/DeviceServiceTest.cs @@ -0,0 +1,51 @@ +using AutoMapper; +using DMS.Core.Models; +using DMS.Infrastructure.Configurations; +using DMS.Infrastructure.Data; +using DMS.Infrastructure.Profiles; +using DMS.Infrastructure.Repositories; +using DMS.Infrastructure.Services; +using JetBrains.Annotations; + +namespace DMS.Infrastructure.UnitTests.Services; + +[TestSubject(typeof(DeviceService))] +public class DeviceServiceTest +{ + private readonly DeviceRepository _deviceRepository; + private readonly DeviceService _deviceService; + private readonly IMapper _mapper; + + public DeviceServiceTest() + { + // 1. 创建 MapperConfiguration + var mappingConfig = new MapperConfiguration(mc => + + { + // 添加你的所有 Profile + mc.AddProfile(new MappingProfile()); + // 如果有其他 Profile,也可以在这里添加 + // mc.AddProfile(new AnotherProfile()); + }); + + // 2. 验证映射配置是否有效 (可选,但在开发环境中推荐) + mappingConfig.AssertConfigurationIsValid(); + // 3. 创建 IMapper 实例 + _mapper = mappingConfig.CreateMapper(); + + + AppSettings appSettings = new AppSettings(); + appSettings.Database.Database = "dms_test"; + SqlSugarDbContext dbContext = new SqlSugarDbContext(appSettings); + _deviceRepository= new DeviceRepository(_mapper,dbContext); + _deviceService = new DeviceService(_deviceRepository); + } + + [Fact] + public async Task AddAsync_Test() + { + var dbDevice = FakerHelper.FakeDbDevice(); + var addDevice= await _deviceService.AddAsync(_mapper.Map(dbDevice)); + Assert.NotEqual(0, addDevice.Id); + } +} \ No newline at end of file diff --git a/DMS.Infrastructure/Configurations/AppSettings.cs b/DMS.Infrastructure/Configurations/AppSettings.cs index 69a5fd7..9e9c045 100644 --- a/DMS.Infrastructure/Configurations/AppSettings.cs +++ b/DMS.Infrastructure/Configurations/AppSettings.cs @@ -1,10 +1,7 @@ using DMS.Core.Models; using Newtonsoft.Json; -using System; -using System.Collections.Generic; -using System.IO; -namespace DMS.Config +namespace DMS.Infrastructure.Configurations { public class DatabaseSettings { diff --git a/DMS.Infrastructure/Data/SqlSugarDbContext.cs b/DMS.Infrastructure/Data/SqlSugarDbContext.cs index 351b204..28b640e 100644 --- a/DMS.Infrastructure/Data/SqlSugarDbContext.cs +++ b/DMS.Infrastructure/Data/SqlSugarDbContext.cs @@ -1,4 +1,4 @@ -using DMS.Config; +using DMS.Infrastructure.Configurations; using SqlSugar; namespace DMS.Infrastructure.Data; diff --git a/DMS.Infrastructure/Entities/DbDevice.cs b/DMS.Infrastructure/Entities/DbDevice.cs index b20131f..f81502e 100644 --- a/DMS.Infrastructure/Entities/DbDevice.cs +++ b/DMS.Infrastructure/Entities/DbDevice.cs @@ -56,6 +56,7 @@ public class DbDevice /// /// /// + [SugarColumn(IsNullable = true)] public string CpuType { get; set; } /// /// 设备槽号 (针对PLC等设备)。 @@ -73,4 +74,11 @@ public class DbDevice /// 设备是否激活/启用。 /// public bool IsActive { get; set; } + + + /// + /// 此设备包含的变量表集合。 + /// + [SugarColumn(IsIgnore = true)] + public List VariableTables { get; set; } = new(); } \ No newline at end of file diff --git a/DMS.Infrastructure/Profiles/MappingProfile.cs b/DMS.Infrastructure/Profiles/MappingProfile.cs index 7a68d1f..9c33a0a 100644 --- a/DMS.Infrastructure/Profiles/MappingProfile.cs +++ b/DMS.Infrastructure/Profiles/MappingProfile.cs @@ -17,15 +17,30 @@ public class MappingProfile : Profile // --- 设备映射 (包含List的父对象) --- // AutoMapper 会自动使用上面的规则来处理 VariableTables 属性 - CreateMap().ReverseMap(); + CreateMap() + .ReverseMap(); // --- 变量表映射 (List中的元素) --- - CreateMap().ReverseMap(); + CreateMap() + .ForMember(dest => dest.Variables, opt => opt.Ignore()) + .ForMember(dest => dest.Device, opt => opt.Ignore()) + .ReverseMap(); - CreateMap().ReverseMap(); + CreateMap() + .ForMember(dest => dest.Description, opt => opt.Ignore()) + .ForMember(dest => dest.VariableTable, opt => opt.Ignore()) + .ForMember(dest => dest.MqttAliases, opt => opt.Ignore()) + .ForMember(dest => dest.DataValue, opt => opt.Ignore()) + .ForMember(dest => dest.DisplayValue, opt => opt.Ignore()) + .ReverseMap(); // --- MQTT 和 变量数据 映射 --- - CreateMap().ReverseMap(); - CreateMap().ReverseMap(); + CreateMap() + .ForMember(dest => dest.VariableAliases, opt => opt.Ignore()) + .ReverseMap(); + CreateMap() + .ForMember(dest => dest.Variable, opt => opt.Ignore()) + .ForMember(dest => dest.MqttServer, opt => opt.Ignore()) + .ReverseMap(); CreateMap().ReverseMap(); diff --git a/DMS.Infrastructure/Services/DatabaseService.cs b/DMS.Infrastructure/Services/DatabaseService.cs index a689266..0769d1b 100644 --- a/DMS.Infrastructure/Services/DatabaseService.cs +++ b/DMS.Infrastructure/Services/DatabaseService.cs @@ -1,6 +1,6 @@ -using DMS.Config; using DMS.Core.Interfaces; using DMS.Core.Models; +using DMS.Infrastructure.Configurations; using DMS.Infrastructure.Data; using DMS.Infrastructure.Entities; using SqlSugar; @@ -22,7 +22,6 @@ public class DatabaseService : IDatabaseService public void InitializeTables() { - _db.DbMaintenance.CreateDatabase(); _db.CodeFirst.InitTables(); _db.CodeFirst.InitTables(); @@ -52,8 +51,17 @@ public class DatabaseService : IDatabaseService nameof(DbMqttServer.ServerName) },true); } - - + + public bool IsAnyTable(string tableName) + { + return _db.DbMaintenance.IsAnyTable(tableName, false); + } + + public bool IsAnyIndex(string indexName) + { + return _db.DbMaintenance.IsAnyIndex(indexName); + } + public void InitializeMenus() { var settings = AppSettings.Load();