diff --git a/DMS.Core/Interfaces/Repositories/IBaseRepository.cs b/DMS.Core/Interfaces/Repositories/IBaseRepository.cs index 718e814..520e47a 100644 --- a/DMS.Core/Interfaces/Repositories/IBaseRepository.cs +++ b/DMS.Core/Interfaces/Repositories/IBaseRepository.cs @@ -42,4 +42,10 @@ public interface IBaseRepository where T : class /// /// 要删除的实体的主键ID。 Task DeleteAsync(int id); + + /// + /// 从数据库获取数据。 + /// + /// 从数据库获取数据的条数 + Task> TakeAsync(int number); } \ No newline at end of file diff --git a/DMS.Infrastructure.UnitTests/Services/DeviceServiceTest.cs b/DMS.Infrastructure.UnitTests/Services/DeviceServiceTest.cs index b442540..f42b719 100644 --- a/DMS.Infrastructure.UnitTests/Services/DeviceServiceTest.cs +++ b/DMS.Infrastructure.UnitTests/Services/DeviceServiceTest.cs @@ -48,4 +48,11 @@ public class DeviceServiceTest var addDevice= await _deviceService.AddAsync(_mapper.Map(dbDevice)); Assert.NotEqual(0, addDevice.Id); } + + [Fact] + public async Task TakeAsync_Test() + { + var device= await _deviceService.TakeAsync(2); + Assert.Equal(2,device.Count); + } } \ No newline at end of file diff --git a/DMS.Infrastructure.UnitTests/Services_Test/DeviceServer_Test.cs b/DMS.Infrastructure.UnitTests/Services_Test/DeviceServer_Test.cs deleted file mode 100644 index 696ecaa..0000000 --- a/DMS.Infrastructure.UnitTests/Services_Test/DeviceServer_Test.cs +++ /dev/null @@ -1,14 +0,0 @@ -namespace DMS.Infrastructure.UnitTests.Services_Test; - -public class DeviceServer_Test -{ - public DeviceServer_Test() - { - - } - - public async Task AddDeviceAsync_Test() - { - - } -} \ No newline at end of file diff --git a/DMS.Infrastructure/Repositories/BaseRepository.cs b/DMS.Infrastructure/Repositories/BaseRepository.cs index a2a80eb..b939e3f 100644 --- a/DMS.Infrastructure/Repositories/BaseRepository.cs +++ b/DMS.Infrastructure/Repositories/BaseRepository.cs @@ -1,6 +1,7 @@ using System.Diagnostics; using System.Linq.Expressions; using DMS.Core.Helper; +using DMS.Core.Models; using DMS.Infrastructure.Data; using SqlSugar; @@ -162,4 +163,15 @@ public abstract class BaseRepository { await Db.RollbackTranAsync(); } + + protected async Task> TakeAsync(int number) + { + var stopwatch = new Stopwatch(); + stopwatch.Start(); + var entity = await Db.Queryable().Take(number).ToListAsync(); + + stopwatch.Stop(); + NlogHelper.Info($"TakeAsync {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms"); + return entity; + } } \ No newline at end of file diff --git a/DMS.Infrastructure/Repositories/DeviceRepository.cs b/DMS.Infrastructure/Repositories/DeviceRepository.cs index 91912bd..a5550ba 100644 --- a/DMS.Infrastructure/Repositories/DeviceRepository.cs +++ b/DMS.Infrastructure/Repositories/DeviceRepository.cs @@ -51,4 +51,11 @@ public class DeviceRepository : BaseRepository, IDeviceRepository NlogHelper.Info($"Delete {typeof(DbDevice)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } + + public new async Task> TakeAsync(int number) + { + var dbList = await base.TakeAsync(number); + return _mapper.Map>(dbList); + + } } \ No newline at end of file diff --git a/DMS.Infrastructure/Repositories/MenuRepository.cs b/DMS.Infrastructure/Repositories/MenuRepository.cs index 7b65493..ee4084f 100644 --- a/DMS.Infrastructure/Repositories/MenuRepository.cs +++ b/DMS.Infrastructure/Repositories/MenuRepository.cs @@ -64,4 +64,11 @@ public class MenuRepository : BaseRepository, IMenuRepository NlogHelper.Info($"Delete {typeof(DbMenu)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } + + public new async Task> TakeAsync(int number) + { + var dbList = await base.TakeAsync(number); + return _mapper.Map>(dbList); + + } } \ No newline at end of file diff --git a/DMS.Infrastructure/Repositories/MqttServerRepository.cs b/DMS.Infrastructure/Repositories/MqttServerRepository.cs index 2e1b7da..58283cc 100644 --- a/DMS.Infrastructure/Repositories/MqttServerRepository.cs +++ b/DMS.Infrastructure/Repositories/MqttServerRepository.cs @@ -53,4 +53,11 @@ public class MqttServerRepository : BaseRepository, IMqttServerRep NlogHelper.Info($"Delete {typeof(MqttServer)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } + + public new async Task> TakeAsync(int number) + { + var dbList = await base.TakeAsync(number); + return _mapper.Map>(dbList); + + } } \ No newline at end of file diff --git a/DMS.Infrastructure/Repositories/UserRepository.cs b/DMS.Infrastructure/Repositories/UserRepository.cs index 955e4ed..ab6e56d 100644 --- a/DMS.Infrastructure/Repositories/UserRepository.cs +++ b/DMS.Infrastructure/Repositories/UserRepository.cs @@ -64,4 +64,11 @@ public class UserRepository : BaseRepository, IUserRepository NlogHelper.Info($"Delete {typeof(DbMenu)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } + + public new async Task> TakeAsync(int number) + { + var dbList = await base.TakeAsync(number); + return _mapper.Map>(dbList); + + } } \ No newline at end of file diff --git a/DMS.Infrastructure/Repositories/VariableHistoryRepository.cs b/DMS.Infrastructure/Repositories/VariableHistoryRepository.cs index 1b21a79..d4d2870 100644 --- a/DMS.Infrastructure/Repositories/VariableHistoryRepository.cs +++ b/DMS.Infrastructure/Repositories/VariableHistoryRepository.cs @@ -56,4 +56,12 @@ public class VariableHistoryRepository : BaseRepository, IVar NlogHelper.Info($"Delete {typeof(DbMenu)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } + + + public new async Task> TakeAsync(int number) + { + var dbList = await base.TakeAsync(number); + return _mapper.Map>(dbList); + + } } \ No newline at end of file diff --git a/DMS.Infrastructure/Repositories/VariableMqttAliasRepository.cs b/DMS.Infrastructure/Repositories/VariableMqttAliasRepository.cs index bfddf4b..3d26a76 100644 --- a/DMS.Infrastructure/Repositories/VariableMqttAliasRepository.cs +++ b/DMS.Infrastructure/Repositories/VariableMqttAliasRepository.cs @@ -61,4 +61,11 @@ public class VariableMqttAliasRepository : BaseRepository, NlogHelper.Info($"Delete {typeof(DbMenu)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } + + public new async Task> TakeAsync(int number) + { + var dbList = await base.TakeAsync(number); + return _mapper.Map>(dbList); + + } } \ No newline at end of file diff --git a/DMS.Infrastructure/Repositories/VariableRepository.cs b/DMS.Infrastructure/Repositories/VariableRepository.cs index 4d65782..6eacf73 100644 --- a/DMS.Infrastructure/Repositories/VariableRepository.cs +++ b/DMS.Infrastructure/Repositories/VariableRepository.cs @@ -137,4 +137,11 @@ public class VariableRepository : BaseRepository, IVariableRepositor NlogHelper.Info($"Delete {typeof(DbMenu)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } + + public new async Task> TakeAsync(int number) + { + var dbList = await base.TakeAsync(number); + return _mapper.Map>(dbList); + + } } \ No newline at end of file diff --git a/DMS.Infrastructure/Repositories/VariableTableRepository.cs b/DMS.Infrastructure/Repositories/VariableTableRepository.cs index fe7c98b..c30a8bd 100644 --- a/DMS.Infrastructure/Repositories/VariableTableRepository.cs +++ b/DMS.Infrastructure/Repositories/VariableTableRepository.cs @@ -56,4 +56,11 @@ public class VariableTableRepository : BaseRepository, IVariabl NlogHelper.Info($"Delete {typeof(DbMenu)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } + + public new async Task> TakeAsync(int number) + { + var dbList = await base.TakeAsync(number); + return _mapper.Map>(dbList); + + } } \ No newline at end of file diff --git a/DMS.Infrastructure/Services/BaseService.cs b/DMS.Infrastructure/Services/BaseService.cs index efcd595..dac6c17 100644 --- a/DMS.Infrastructure/Services/BaseService.cs +++ b/DMS.Infrastructure/Services/BaseService.cs @@ -66,5 +66,9 @@ namespace DMS.Infrastructure.Services { return await ServerRepository.GetByIdAsync(id); } + public virtual async Task> TakeAsync(int number) + { + return await ServerRepository.TakeAsync(number); + } } }