临时提交2

This commit is contained in:
2025-07-21 18:49:49 +08:00
parent 29a2d44319
commit 525c681b6c
57 changed files with 628 additions and 558 deletions

View File

@@ -1,5 +1,6 @@
using DMS.Application.DTOs;
using DMS.Core.Enums;
using DMS.Core.Models;
namespace DMS.Application.Interfaces;
@@ -33,7 +34,7 @@ public interface IDeviceAppService
/// <summary>
/// 异步删除一个设备。
/// </summary>
Task DeleteDeviceAsync(int id);
Task DeleteDeviceAsync(Device device);
/// <summary>
/// 异步切换设备的激活状态。

View File

@@ -1,9 +1,9 @@
using AutoMapper;
using DMS.Core.Interfaces;
using DMS.Core.Models;
using DMS.Application.DTOs;
using DMS.Application.Interfaces;
using DMS.Core.Enums;
using DMS.Core.Interfaces;
namespace DMS.Application.Services;
@@ -49,7 +49,7 @@ public class DeviceAppService : IDeviceAppService
{
try
{
_repoManager.BeginTransaction();
_repoManager.BeginTranAsync();
var device = _mapper.Map<Device>(dto.Device);
device.IsActive = true; // 默认激活
@@ -101,9 +101,9 @@ public class DeviceAppService : IDeviceAppService
/// <summary>
/// 异步删除一个设备。
/// </summary>
public async Task DeleteDeviceAsync(int id)
public async Task DeleteDeviceAsync(Device device)
{
await _repoManager.Devices.DeleteAsync(id);
await _repoManager.Devices.DeleteAsync(device);
await _repoManager.CommitAsync();
}
@@ -127,7 +127,7 @@ public class DeviceAppService : IDeviceAppService
/// </summary>
public async Task<List<DeviceDto>> GetDevicesByProtocolAsync(ProtocolType protocol)
{
var devices = await _repoManager.Devices.GetActiveDevicesWithDetailsAsync(protocol);
var devices = await _repoManager.Devices.GetAllAsync();
return _mapper.Map<List<DeviceDto>>(devices);
}
}

View File

@@ -34,27 +34,54 @@ public class MenuService : IMenuService
public async Task<int> CreateMenuAsync(MenuBeanDto menuDto)
{
var menu = _mapper.Map<MenuBean>(menuDto);
await _repoManager.Menus.AddAsync(menu);
await _repoManager.CommitAsync();
return menu.Id;
try
{
_repoManager.BeginTranAsync();
var menu = _mapper.Map<MenuBean>(menuDto);
await _repoManager.Menus.AddAsync(menu);
await _repoManager.CommitAsync();
return menu.Id;
}
catch (Exception ex)
{
await _repoManager.RollbackAsync();
throw new ApplicationException("创建菜单时发生错误,操作已回滚。", ex);
}
}
public async Task UpdateMenuAsync(MenuBeanDto menuDto)
{
var menu = await _repoManager.Menus.GetByIdAsync(menuDto.Id);
if (menu == null)
try
{
throw new ApplicationException($"Menu with ID {menuDto.Id} not found.");
_repoManager.BeginTranAsync();
var menu = await _repoManager.Menus.GetByIdAsync(menuDto.Id);
if (menu == null)
{
throw new ApplicationException($"Menu with ID {menuDto.Id} not found.");
}
_mapper.Map(menuDto, menu);
await _repoManager.Menus.UpdateAsync(menu);
await _repoManager.CommitAsync();
}
catch (Exception ex)
{
await _repoManager.RollbackAsync();
throw new ApplicationException("更新菜单时发生错误,操作已回滚。", ex);
}
_mapper.Map(menuDto, menu);
await _repoManager.Menus.UpdateAsync(menu);
await _repoManager.CommitAsync();
}
public async Task DeleteMenuAsync(int id)
{
await _repoManager.Menus.DeleteAsync(id);
await _repoManager.CommitAsync();
try
{
_repoManager.BeginTranAsync();
await _repoManager.Menus.DeleteAsync(id);
await _repoManager.CommitAsync();
}
catch (Exception ex)
{
await _repoManager.RollbackAsync();
throw new ApplicationException("删除菜单时发生错误,操作已回滚。", ex);
}
}
}

View File

@@ -34,27 +34,54 @@ public class MqttAliasAppService : IMqttAliasAppService
public async Task<int> CreateMqttAliasAsync(VariableMqttAliasDto mqttAliasDto)
{
var mqttAlias = _mapper.Map<VariableMqttAlias>(mqttAliasDto);
await _repoManager.VariableMqttAliases.AddAsync(mqttAlias);
await _repoManager.CommitAsync();
return mqttAlias.Id;
try
{
_repoManager.BeginTranAsync();
var mqttAlias = _mapper.Map<VariableMqttAlias>(mqttAliasDto);
await _repoManager.VariableMqttAliases.AddAsync(mqttAlias);
await _repoManager.CommitAsync();
return mqttAlias.Id;
}
catch (Exception ex)
{
await _repoManager.RollbackAsync();
throw new ApplicationException("创建MQTT别名时发生错误操作已回滚。", ex);
}
}
public async Task UpdateMqttAliasAsync(VariableMqttAliasDto mqttAliasDto)
{
var mqttAlias = await _repoManager.VariableMqttAliases.GetByIdAsync(mqttAliasDto.Id);
if (mqttAlias == null)
try
{
throw new ApplicationException($"MQTT Alias with ID {mqttAliasDto.Id} not found.");
_repoManager.BeginTranAsync();
var mqttAlias = await _repoManager.VariableMqttAliases.GetByIdAsync(mqttAliasDto.Id);
if (mqttAlias == null)
{
throw new ApplicationException($"MQTT Alias with ID {mqttAliasDto.Id} not found.");
}
_mapper.Map(mqttAliasDto, mqttAlias);
await _repoManager.VariableMqttAliases.UpdateAsync(mqttAlias);
await _repoManager.CommitAsync();
}
catch (Exception ex)
{
await _repoManager.RollbackAsync();
throw new ApplicationException("更新MQTT别名时发生错误操作已回滚。", ex);
}
_mapper.Map(mqttAliasDto, mqttAlias);
await _repoManager.VariableMqttAliases.UpdateAsync(mqttAlias);
await _repoManager.CommitAsync();
}
public async Task DeleteMqttAliasAsync(int id)
{
await _repoManager.VariableMqttAliases.DeleteAsync(id);
await _repoManager.CommitAsync();
try
{
_repoManager.BeginTranAsync();
await _repoManager.VariableMqttAliases.DeleteAsync(id);
await _repoManager.CommitAsync();
}
catch (Exception ex)
{
await _repoManager.RollbackAsync();
throw new ApplicationException("删除MQTT别名时发生错误操作已回滚。", ex);
}
}
}

View File

@@ -34,27 +34,54 @@ public class MqttAppService : IMqttAppService
public async Task<int> CreateMqttServerAsync(MqttServerDto mqttServerDto)
{
var mqttServer = _mapper.Map<MqttServer>(mqttServerDto);
await _repoManager.MqttServers.AddAsync(mqttServer);
await _repoManager.CommitAsync();
return mqttServer.Id;
try
{
_repoManager.BeginTranAsync();
var mqttServer = _mapper.Map<MqttServer>(mqttServerDto);
await _repoManager.MqttServers.AddAsync(mqttServer);
await _repoManager.CommitAsync();
return mqttServer.Id;
}
catch (Exception ex)
{
await _repoManager.RollbackAsync();
throw new ApplicationException("创建MQTT服务器时发生错误操作已回滚。", ex);
}
}
public async Task UpdateMqttServerAsync(MqttServerDto mqttServerDto)
{
var mqttServer = await _repoManager.MqttServers.GetByIdAsync(mqttServerDto.Id);
if (mqttServer == null)
try
{
throw new ApplicationException($"MQTT Server with ID {mqttServerDto.Id} not found.");
_repoManager.BeginTranAsync();
var mqttServer = await _repoManager.MqttServers.GetByIdAsync(mqttServerDto.Id);
if (mqttServer == null)
{
throw new ApplicationException($"MQTT Server with ID {mqttServerDto.Id} not found.");
}
_mapper.Map(mqttServerDto, mqttServer);
await _repoManager.MqttServers.UpdateAsync(mqttServer);
await _repoManager.CommitAsync();
}
catch (Exception ex)
{
await _repoManager.RollbackAsync();
throw new ApplicationException("更新MQTT服务器时发生错误操作已回滚。", ex);
}
_mapper.Map(mqttServerDto, mqttServer);
await _repoManager.MqttServers.UpdateAsync(mqttServer);
await _repoManager.CommitAsync();
}
public async Task DeleteMqttServerAsync(int id)
{
await _repoManager.MqttServers.DeleteAsync(id);
await _repoManager.CommitAsync();
try
{
_repoManager.BeginTranAsync();
await _repoManager.MqttServers.DeleteAsync(id);
await _repoManager.CommitAsync();
}
catch (Exception ex)
{
await _repoManager.RollbackAsync();
throw new ApplicationException("删除MQTT服务器时发生错误操作已回滚。", ex);
}
}
}

View File

@@ -34,27 +34,54 @@ public class VariableAppService : IVariableAppService
public async Task<int> CreateVariableAsync(VariableDto variableDto)
{
var variable = _mapper.Map<Variable>(variableDto);
await _repoManager.Variables.AddAsync(variable);
await _repoManager.CommitAsync();
return variable.Id;
try
{
_repoManager.BeginTranAsync();
var variable = _mapper.Map<Variable>(variableDto);
await _repoManager.Variables.AddAsync(variable);
await _repoManager.CommitAsync();
return variable.Id;
}
catch (Exception ex)
{
await _repoManager.RollbackAsync();
throw new ApplicationException("创建变量时发生错误,操作已回滚。", ex);
}
}
public async Task UpdateVariableAsync(VariableDto variableDto)
{
var variable = await _repoManager.Variables.GetByIdAsync(variableDto.Id);
if (variable == null)
try
{
throw new ApplicationException($"Variable with ID {variableDto.Id} not found.");
_repoManager.BeginTranAsync();
var variable = await _repoManager.Variables.GetByIdAsync(variableDto.Id);
if (variable == null)
{
throw new ApplicationException($"Variable with ID {variableDto.Id} not found.");
}
_mapper.Map(variableDto, variable);
await _repoManager.Variables.UpdateAsync(variable);
await _repoManager.CommitAsync();
}
catch (Exception ex)
{
await _repoManager.RollbackAsync();
throw new ApplicationException("更新变量时发生错误,操作已回滚。", ex);
}
_mapper.Map(variableDto, variable);
await _repoManager.Variables.UpdateAsync(variable);
await _repoManager.CommitAsync();
}
public async Task DeleteVariableAsync(int id)
{
await _repoManager.Variables.DeleteAsync(id);
await _repoManager.CommitAsync();
try
{
_repoManager.BeginTranAsync();
await _repoManager.Variables.DeleteAsync(id);
await _repoManager.CommitAsync();
}
catch (Exception ex)
{
await _repoManager.RollbackAsync();
throw new ApplicationException("删除变量时发生错误,操作已回滚。", ex);
}
}
}

View File

@@ -5,5 +5,7 @@ namespace DMS.Core.Enums;
public enum DeviceType
{
[Description("西门子PLC")] SiemensPLC,
[Description("OpcUa设备")] OpcUa,
[Description("Modbus TCP设备")] ModbusTCP,
[Description("三菱PLC")] MelsecPLC
}

View File

@@ -1,6 +1,4 @@
using System.Threading.Tasks;
namespace DMS.Infrastructure.Interfaces
namespace DMS.Core.Interfaces
{
public interface IDatabaseService
{

View File

@@ -1,23 +0,0 @@
using DMS.Core.Models;
namespace DMS.Core.Interfaces;
/// <summary>
/// 继承自IBaseRepository提供设备相关的特定数据查询功能。
/// </summary>
public interface IDeviceRepository : IBaseRepository<Device>
{
/// <summary>
/// 异步获取所有激活的设备,并级联加载其下的变量表和变量。
/// 这是后台轮询服务需要的主要数据。
/// </summary>
/// <returns>包含完整层级结构的激活设备列表。</returns>
Task<List<Device>> GetActiveDevicesWithDetailsAsync(ProtocolType protocol);
/// <summary>
/// 异步根据设备ID获取设备及其所有详细信息变量表、变量、MQTT别名等
/// </summary>
/// <param name="deviceId">设备ID。</param>
/// <returns>包含详细信息的设备对象。</returns>
Task<Device> GetDeviceWithDetailsAsync(int deviceId);
}

View File

@@ -1,8 +0,0 @@
using DMS.Core.Models;
namespace DMS.Core.Interfaces;
public interface IMenuRepository : IBaseRepository<MenuBean>
{
// 可以添加特定于菜单的查询方法,例如获取所有菜单项
}

View File

@@ -1,13 +0,0 @@
using DMS.Core.Models;
namespace DMS.Core.Interfaces;
public interface IMqttServerRepository : IBaseRepository<MqttServer>
{
/// <summary>
/// 异步获取一个MQTT服务器及其关联的所有变量别名。
/// </summary>
/// <param name="serverId">MQTT服务器ID。</param>
/// <returns>包含变量别名信息的MQTT服务器对象。</returns>
Task<MqttServer> GetMqttServerWithVariableAliasesAsync(int serverId);
}

View File

@@ -1,3 +1,5 @@
using DMS.Core.Interfaces.Repositories;
namespace DMS.Core.Interfaces;
/// <summary>
@@ -10,47 +12,47 @@ public interface IRepositoryManager : IDisposable
/// 获取设备仓储的实例。
/// 所有通过此管理器获取的仓储都共享同一个数据库上下文和事务。
/// </summary>
IDeviceRepository Devices { get; }
IDeviceRepository Devices { get; set; }
/// <summary>
/// 获取变量表仓储的实例。
/// </summary>
IVariableTableRepository VariableTables { get; }
IVariableTableRepository VariableTables { get; set; }
/// <summary>
/// 获取变量仓储的实例。
/// </summary>
IVariableRepository Variables { get; }
IVariableRepository Variables { get; set; }
/// <summary>
/// 获取MQTT服务器仓储的实例。
/// </summary>
IMqttServerRepository MqttServers { get; }
IMqttServerRepository MqttServers { get; set; }
/// <summary>
/// 获取变量MQTT别名仓储的实例。
/// </summary>
IVariableMqttAliasRepository VariableMqttAliases { get; }
IVariableMqttAliasRepository VariableMqttAliases { get; set; }
/// <summary>
/// 获取菜单仓储的实例。
/// </summary>
IMenuRepository Menus { get; }
IMenuRepository Menus { get; set; }
/// <summary>
/// 获取变量历史仓储的实例。
/// </summary>
IVariableHistoryRepository VariableHistories { get; }
IVariableHistoryRepository VariableHistories { get; set; }
/// <summary>
/// 获取用户仓储的实例。
/// </summary>
IUserRepository Users { get; }
IUserRepository Users { get; set; }
/// <summary>
/// 开始一个新的数据库事务。
/// </summary>
void BeginTransaction();
Task BeginTranAsync();
/// <summary>
/// 异步提交当前事务中的所有变更。

View File

@@ -1,13 +0,0 @@
using DMS.Core.Models;
namespace DMS.Core.Interfaces;
public interface IUserRepository : IBaseRepository<User>
{
/// <summary>
/// 异步根据用户名获取用户。
/// </summary>
/// <param name="username">用户名。</param>
/// <returns>用户对象如果不存在则为null。</returns>
Task<User> GetByUsernameAsync(string username);
}

View File

@@ -1,8 +0,0 @@
using DMS.Core.Models;
namespace DMS.Core.Interfaces;
public interface IVariableHistoryRepository : IBaseRepository<VariableHistory>
{
// 可以添加特定于VariableHistory的查询方法
}

View File

@@ -1,21 +0,0 @@
using DMS.Core.Models;
namespace DMS.Core.Interfaces;
public interface IVariableMqttAliasRepository : IBaseRepository<VariableMqttAlias>
{
/// <summary>
/// 异步获取指定变量的所有MQTT别名关联并加载关联的MQTT服务器信息。
/// </summary>
/// <param name="variableId">变量ID。</param>
/// <returns>指定变量的所有MQTT别名关联列表。</returns>
Task<List<VariableMqttAlias>> GetAliasesForVariableAsync(int variableId);
/// <summary>
/// 异步根据变量ID和MQTT服务器ID获取特定的MQTT别名关联。
/// </summary>
/// <param name="variableId">变量ID。</param>
/// <param name="mqttServerId">MQTT服务器ID。</param>
/// <returns>匹配的VariableMqttAlias对象如果不存在则为null。</returns>
Task<VariableMqttAlias> GetByVariableAndServerAsync(int variableId, int mqttServerId);
}

View File

@@ -1,13 +0,0 @@
using DMS.Core.Models;
namespace DMS.Core.Interfaces;
public interface IVariableRepository : IBaseRepository<Variable>
{
/// <summary>
/// 异步获取一个变量及其关联的所有MQTT别名和对应的MQTT服务器信息。
/// </summary>
/// <param name="variableId">变量ID。</param>
/// <returns>包含别名和服务器信息的变量对象。</returns>
Task<Variable> GetVariableWithMqttAliasesAsync(int variableId);
}

View File

@@ -1,8 +0,0 @@
using DMS.Core.Models;
namespace DMS.Core.Interfaces;
public interface IVariableTableRepository : IBaseRepository<VariableTable>
{
// 可以添加特定于VariableTable的查询方法
}

View File

@@ -1,4 +1,4 @@
namespace DMS.Core.Interfaces;
namespace DMS.Core.Interfaces.Repositories;
/// <summary>
/// 提供泛型数据访问操作的基础仓储接口。
@@ -23,17 +23,17 @@ public interface IBaseRepository<T> where T : class
/// 异步添加一个新实体。
/// </summary>
/// <param name="entity">要添加的实体。</param>
Task AddAsync(T entity);
Task<T> AddAsync(T entity);
/// <summary>
/// 异步更新一个已存在的实体。
/// </summary>
/// <param name="entity">要更新的实体。</param>
Task UpdateAsync(T entity);
Task<int> UpdateAsync(T entity);
/// <summary>
/// 异步根据ID删除一个实体。
/// </summary>
/// <param name="id">要删除的实体的主键ID。</param>
Task DeleteAsync(int id);
Task<int> DeleteAsync(T entity);
}

View File

@@ -0,0 +1,10 @@
using DMS.Core.Models;
namespace DMS.Core.Interfaces.Repositories
{
public interface IDeviceRepository:IBaseRepository<Device>
{
}
}

View File

@@ -0,0 +1,10 @@
using DMS.Core.Models;
namespace DMS.Core.Interfaces.Repositories
{
public interface IMenuRepository:IBaseRepository<MenuBean>
{
}
}

View File

@@ -0,0 +1,11 @@
using DMS.Core.Models;
namespace DMS.Core.Interfaces.Repositories
{
public interface IMqttServerRepository:IBaseRepository<MqttServer>
{
}
}

View File

@@ -0,0 +1,9 @@
using DMS.Core.Models;
namespace DMS.Core.Interfaces.Repositories
{
public interface IUserRepository:IBaseRepository<User>
{
}
}

View File

@@ -0,0 +1,8 @@
using DMS.Core.Models;
namespace DMS.Core.Interfaces.Repositories;
public interface IVariableHistoryRepository:IBaseRepository<VariableHistory>
{
}

View File

@@ -0,0 +1,9 @@
namespace DMS.Core.Interfaces.Repositories
{
public interface IVariableMqttAliasRepository:IBaseRepository<VariableMqttAlias>
{
}
}

View File

@@ -0,0 +1,10 @@
using DMS.Core.Models;
namespace DMS.Core.Interfaces.Repositories
{
public interface IVariableRepository:IBaseRepository<Variable>
{
}
}

View File

@@ -0,0 +1,11 @@
using DMS.Core.Models;
namespace DMS.Core.Interfaces.Repositories
{
public interface IVariableTableRepository:IBaseRepository<VariableTable>
{
}
}

View File

@@ -10,23 +10,23 @@ namespace DMS.Infrastructure.UnitTests
{
public static class FakerHelper
{
// public static DbDevice FakeDbDevice()
// {
// // var dbDevice = new Faker<DbDevice>()
// // .RuleFor(d => d.Name, f => f.Commerce.ProductName())
// // .RuleFor(d => d.Description, f => f.Commerce.ProductDescription())
// // .RuleFor(d => d.Ip, f => f.Internet.Ip())
// // .Generate();
// // dbDevice.Prot = 102;
// // dbDevice.ProtocolType = Core.Enums.ProtocolType.S7;
// // dbDevice.Slot = 1;
// // dbDevice.Rack = 0;
// // dbDevice.CpuType = S7.Net.CpuType.S71200;
// // dbDevice.DeviceType = Core.Enums.DeviceType.SiemensPLC;
//
//
// return dbDevice;
// }
public static DbDevice FakeDbDevice()
{
var dbDevice = new Faker<DbDevice>()
.RuleFor(d => d.Name, f => f.Commerce.ProductName())
.RuleFor(d => d.Description, f => f.Commerce.ProductDescription())
.RuleFor(d => d.IpAddress, f => f.Internet.Ip())
.RuleFor(d => d.OpcUaServerUrl, f => f.Internet.Url())
.Generate();
dbDevice.Port = 102;
dbDevice.Protocol = ProtocolType.S7;
dbDevice.Slot = 1;
dbDevice.Rack = 0;
dbDevice.CpuType = "S7-1200";
dbDevice.DeviceType = Core.Enums.DeviceType.SiemensPLC;
return dbDevice;
}
// public static DbVariableTable FakeDbVariableTable()
// {

View File

@@ -0,0 +1,48 @@
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<DbDevice>();
// 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);
}
}

View File

@@ -24,4 +24,8 @@
<ProjectReference Include="..\DMS.Core\DMS.Core.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Interfaces\" />
</ItemGroup>
</Project>

View File

@@ -1,8 +1,5 @@
using DMS.Config;
using DMS.Infrastructure.Interfaces;
using SqlSugar;
using System;
using System.Threading.Tasks;
namespace DMS.Infrastructure.Data;

View File

@@ -1,4 +1,6 @@
using DMS.Core.Enums;
using SqlSugar;
using SqlSugar.DbConvert;
namespace DMS.Infrastructure.Entities;
@@ -18,10 +20,16 @@ public class DbDevice
/// 设备名称。
/// </summary>
public string Name { get; set; }
/// <summary>
/// 设备描述。
/// </summary>
[SugarColumn(IsNullable = true)]
public string Description { get; set; }
/// <summary>
/// 设备通信协议类型,对应 ProtocolType 枚举。
/// </summary>
[SugarColumn(ColumnDataType="varchar(20)",SqlParameterDbType=typeof(EnumToStringConvert))]
public ProtocolType Protocol { get; set; }
/// <summary>
@@ -37,16 +45,29 @@ public class DbDevice
/// <summary>
/// 设备机架号 (针对PLC等设备)。
/// </summary>
[SugarColumn(IsNullable = true)]
public int Rack { get; set; }
/// <summary>
/// 设备槽号 (针对PLC等设备)。
/// </summary>
[SugarColumn(IsNullable = true)]
public int Slot { get; set; }
/// <summary>
///
/// </summary>
public string CpuType { get; set; }
/// <summary>
/// 设备槽号 (针对PLC等设备)。
/// </summary>
[SugarColumn(ColumnDataType="varchar(20)",SqlParameterDbType=typeof(EnumToStringConvert))]
public DeviceType DeviceType { get; set; }
/// <summary>
/// OPC UA服务器的URL地址。
/// </summary>
[SugarColumn(IsNullable = true)]
public string OpcUaServerUrl { get; set; }
/// <summary>

View File

@@ -1,20 +0,0 @@
using DMS.Core.Models;
using SqlSugar;
using System.Collections.Generic;
using System.Threading.Tasks;
using DMS.Infrastructure.Entities;
namespace DMS.Infrastructure.Interfaces
{
public interface IDeviceRepository
{
Task<DbDevice> AddAsync(DbDevice model);
Task<int> UpdateAsync(DbDevice model);
Task<int> DeleteAsync(DbDevice model);
Task<List<DbDevice>> GetAllAsync();
Task<DbDevice> GetByIdAsync(int id);
Task BeginTranAsync();
Task CommitTranAsync();
Task RollbackTranAsync();
}
}

View File

@@ -1,12 +0,0 @@
using DMS.Core.Models;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace DMS.Infrastructure.Interfaces
{
public interface IDeviceService
{
Task<Device> AddAsync(Device device);
Task<List<Device>> GetAllAsync();
}
}

View File

@@ -1,19 +0,0 @@
using DMS.Core.Models;
using DMS.Core.Enums;
using System.Collections.Generic;
using System.Threading.Tasks;
using DMS.Infrastructure.Entities;
namespace DMS.Infrastructure.Interfaces
{
public interface IMenuRepository
{
Task<int> DeleteAsync(DbMenu menu);
Task<List<DbMenu>> GetMenuTreesAsync();
Task<DbMenu> AddAsync(DbMenu menu);
Task<int> UpdateAsync(DbMenu menu);
Task BeginTranAsync();
Task CommitTranAsync();
Task RollbackTranAsync();
}
}

View File

@@ -1,20 +0,0 @@
using DMS.Core.Models;
using DMS.Infrastructure.Entities;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace DMS.Infrastructure.Interfaces
{
public interface IMqttRepository
{
Task<DbMqttServer> GetByIdAsync(int id);
Task<List<DbMqttServer>> GetAllAsync();
Task<int> AddAsync(DbMqttServer mqtt);
Task<int> UpdateAsync(DbMqttServer mqtt);
Task<int> DeleteAsync(DbMqttServer mqtt);
Task BeginTranAsync();
Task CommitTranAsync();
Task RollbackTranAsync();
}
}

View File

@@ -1,18 +0,0 @@
using DMS.Core.Models;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace DMS.Infrastructure.Interfaces
{
public interface IUserRepository
{
Task<User> GetByIdAsync(int id);
Task<List<User>> GetAllAsync();
Task<int> AddAsync(User user);
Task<int> UpdateAsync(User user);
Task<int> DeleteAsync(int id);
Task BeginTranAsync();
Task CommitTranAsync();
Task RollbackTranAsync();
}
}

View File

@@ -1,18 +0,0 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using DMS.Core.Models;
namespace DMS.Infrastructure.Interfaces
{
public interface IVarDataRepository
{
Task<Variable> GetByIdAsync(int id);
Task<List<Variable>> GetAllAsync();
Task<Variable> AddAsync(Variable variable);
Task<int> UpdateAsync(Variable variable);
Task<int> DeleteAsync(Variable variable);
Task BeginTranAsync();
Task CommitTranAsync();
Task RollbackTranAsync();
}
}

View File

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

View File

@@ -1,18 +0,0 @@
using DMS.Core.Models;
using System.Collections.Generic;
using System.Threading.Tasks;
using DMS.Infrastructure.Entities;
namespace DMS.Infrastructure.Interfaces
{
public interface IVariableMqttAliasRepository
{
Task<DbVariableMqttAlias?> GetByIdAsync(int variableDataId, int mqttId);
Task<int> UpdateAliasAsync(int variableDataId, int mqttId, string newAlias);
Task<int> DeleteAsync(int variableDataId, int mqttId);
Task BeginTranAsync();
Task CommitTranAsync();
Task RollbackTranAsync();
}
}

View File

@@ -1,15 +1,13 @@
using AutoMapper;
using DMS.Core.Helper;
using DMS.Infrastructure.Interfaces;
using DMS.Infrastructure.Data;
using SqlSugar;
using System.Diagnostics;
using System.Linq.Expressions;
using DMS.Core.Helper;
using DMS.Infrastructure.Data;
using SqlSugar;
namespace DMS.Infrastructure.Repositories;
/// <summary>
/// 通用仓储基类,封装了对实体对象的常用 CRUD 操作。
/// 通用仓储基类,封装了对实体对象的常用 CRUD 操作。
/// </summary>
/// <typeparam name="TEntity">实体类型,必须是引用类型且具有无参构造函数。</typeparam>
public abstract class BaseRepository<TEntity>
@@ -17,121 +15,132 @@ public abstract class BaseRepository<TEntity>
{
private readonly SqlSugarDbContext _dbContext;
/// <summary>
/// 获取当前事务的 SqlSugarClient 实例,用于数据库操作。
/// </summary>
protected SqlSugarClient Db => _dbContext.GetInstance();
/// <summary>
/// 初始化 BaseRepository 的新实例。
/// 初始化 BaseRepository 的新实例。
/// </summary>
/// <param name="dbContext">事务管理对象,通过依赖注入提供。</param>
protected BaseRepository(SqlSugarDbContext dbContext)
{
this._dbContext = dbContext;
_dbContext = dbContext;
}
/// <summary>
/// 异步添加一个新实体
/// 获取当前事务的 SqlSugarClient 实例,用于数据库操作
/// </summary>
protected SqlSugarClient Db
{
get { return _dbContext.GetInstance(); }
}
/// <summary>
/// 异步添加一个新实体。
/// </summary>
/// <param name="entity">要添加的实体对象。</param>
/// <returns>返回已添加的实体对象(可能包含数据库生成的主键等信息)。</returns>
public virtual async Task<TEntity> AddAsync(TEntity entity)
{
Stopwatch stopwatch = new Stopwatch();
var stopwatch = new Stopwatch();
stopwatch.Start();
var result = await Db.Insertable(entity).ExecuteReturnEntityAsync();
var result = await Db.Insertable(entity)
.ExecuteReturnEntityAsync();
stopwatch.Stop();
NlogHelper.Info($"Add {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
/// <summary>
/// 异步更新一个现有实体。
/// 异步更新一个现有实体。
/// </summary>
/// <param name="entity">要更新的实体对象。</param>
/// <returns>返回受影响的行数。</returns>
public virtual async Task<int> UpdateAsync(TEntity entity)
{
Stopwatch stopwatch = new Stopwatch();
var stopwatch = new Stopwatch();
stopwatch.Start();
var result = await Db.Updateable(entity).ExecuteCommandAsync();
var result = await Db.Updateable(entity)
.ExecuteCommandAsync();
stopwatch.Stop();
NlogHelper.Info($"Update {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
/// <summary>
/// 异步删除一个实体。
/// 异步删除一个实体。
/// </summary>
/// <param name="entity">要删除的实体对象。</param>
/// <returns>返回受影响的行数。</returns>
public virtual async Task<int> DeleteAsync(TEntity entity)
{
Stopwatch stopwatch = new Stopwatch();
var stopwatch = new Stopwatch();
stopwatch.Start();
var result = await Db.Deleteable(entity).ExecuteCommandAsync();
var result = await Db.Deleteable(entity)
.ExecuteCommandAsync();
stopwatch.Stop();
NlogHelper.Info($"Delete {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
/// <summary>
/// 异步获取所有实体。
/// 异步获取所有实体。
/// </summary>
/// <returns>返回包含所有实体的列表。</returns>
public virtual async Task<List<TEntity>> GetAllAsync()
{
Stopwatch stopwatch = new Stopwatch();
var stopwatch = new Stopwatch();
stopwatch.Start();
var entities = await Db.Queryable<TEntity>().ToListAsync();
var entities = await Db.Queryable<TEntity>()
.ToListAsync();
stopwatch.Stop();
NlogHelper.Info($"GetAll {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
return entities;
}
/// <summary>
/// 异步根据主键 ID 获取单个实体。
/// 异步根据主键 ID 获取单个实体。
/// </summary>
/// <param name="id">实体的主键 ID。</param>
/// <returns>返回找到的实体,如果未找到则返回 null。</returns>
public virtual async Task<TEntity> GetByIdAsync(int id)
{
Stopwatch stopwatch = new Stopwatch();
var stopwatch = new Stopwatch();
stopwatch.Start();
var entity = await Db.Queryable<TEntity>().In(id).FirstAsync();
var entity = await Db.Queryable<TEntity>()
.In(id)
.FirstAsync();
stopwatch.Stop();
NlogHelper.Info($"GetById {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
return entity;
}
/// <summary>
/// 异步根据指定条件获取单个实体。
/// 异步根据指定条件获取单个实体。
/// </summary>
/// <param name="expression">查询条件的 Lambda 表达式。</param>
/// <returns>返回满足条件的第一个实体,如果未找到则返回 null。</returns>
public virtual async Task<TEntity> GetByConditionAsync(Expression<Func<TEntity, bool>> expression)
{
Stopwatch stopwatch = new Stopwatch();
var stopwatch = new Stopwatch();
stopwatch.Start();
var entity = await Db.Queryable<TEntity>().FirstAsync(expression);
var entity = await Db.Queryable<TEntity>()
.FirstAsync(expression);
stopwatch.Stop();
NlogHelper.Info($"GetByCondition {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
return entity;
}
/// <summary>
/// 异步判断是否存在满足条件的实体。
/// 异步判断是否存在满足条件的实体。
/// </summary>
/// <param name="expression">查询条件的 Lambda 表达式。</param>
/// <returns>如果存在则返回 true否则返回 false。</returns>
public virtual async Task<bool> ExistsAsync(Expression<Func<TEntity, bool>> expression)
{
Stopwatch stopwatch = new Stopwatch();
var stopwatch = new Stopwatch();
stopwatch.Start();
var result = await Db.Queryable<TEntity>().AnyAsync(expression);
var result = await Db.Queryable<TEntity>()
.AnyAsync(expression);
stopwatch.Stop();
NlogHelper.Info($"Exists {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
@@ -148,9 +157,8 @@ public abstract class BaseRepository<TEntity>
}
public async Task RollbackTranAsync()
{
await Db.RollbackTranAsync();
}
}
}

View File

@@ -1,22 +1,41 @@
using System.Diagnostics;
using AutoMapper;
using DMS.Infrastructure.Entities;
using DMS.Core.Enums;
using DMS.Core.Helper;
using DMS.Core.Interfaces.Repositories;
using DMS.Core.Models;
using DMS.Infrastructure.Data;
using SqlSugar;
using DMS.Infrastructure.Interfaces;
using DMS.Infrastructure.Entities;
namespace DMS.Infrastructure.Repositories;
public class DeviceRepository : BaseRepository<DbDevice>,IDeviceRepository
public class DeviceRepository : BaseRepository<DbDevice>, IDeviceRepository
{
private readonly IMapper _mapper;
public DeviceRepository(SqlSugarDbContext dbContext)
public DeviceRepository(IMapper mapper, SqlSugarDbContext dbContext)
: base(dbContext)
{
_mapper = mapper;
}
public async Task<List<Device>> GetAllAsync()
{
var dbList = await base.GetAllAsync();
return _mapper.Map<List<Device>>(dbList);
}
public async Task<Device> GetByIdAsync(int id)
{
var dbDevice = await base.GetByIdAsync(id);
return _mapper.Map<Device>(dbDevice);
}
public async Task<Device> AddAsync(Device model)
{
var dbDevice = await base.AddAsync(_mapper.Map<DbDevice>(model));
return _mapper.Map(dbDevice, model);
}
public async Task<int> UpdateAsync(Device model) => await base.UpdateAsync(_mapper.Map<DbDevice>(model));
public async Task<int> DeleteAsync(Device model) => await base.DeleteAsync(_mapper.Map<DbDevice>(model));
}

View File

@@ -1,49 +1,43 @@
using System.Diagnostics;
using SqlSugar;
using AutoMapper;
using DMS.Infrastructure.Entities;
using DMS.Core.Enums;
using DMS.Core.Helper;
using DMS.Core.Interfaces.Repositories;
using DMS.Core.Models;
using DMS.Infrastructure.Data;
using DMS.Infrastructure.Interfaces;
using DMS.Infrastructure.Entities;
namespace DMS.Infrastructure.Repositories;
public class MenuRepository : BaseRepository<DbMenu>,IMenuRepository
public class MenuRepository : BaseRepository<DbMenu>, IMenuRepository
{
public MenuRepository(SqlSugarDbContext dbContext)
private readonly IMapper _mapper;
public MenuRepository(IMapper mapper, SqlSugarDbContext dbContext)
: base(dbContext)
{
_mapper = mapper;
}
public async Task<List<DbMenu>> GetMenuTreesAsync()
public async Task<List<MenuBean>> GetMenuTreesAsync()
{
Stopwatch stopwatch = new Stopwatch();
var stopwatch = new Stopwatch();
stopwatch.Start();
var dbMenuTree = await Db.Queryable<DbMenu>()
.ToTreeAsync(dm => dm.Childrens, dm => dm.ParentId, 0);
stopwatch.Stop();
NlogHelper.Info($"获取菜单树耗时:{stopwatch.ElapsedMilliseconds}ms");
return dbMenuTree;
return _mapper.Map<List<MenuBean>>(dbMenuTree);
}
// /// <summary>
// /// 编辑菜单,支持事务
// /// </summary>
// /// <param name="menu"></param>
// /// <returns></returns>
//
// public async Task<DbMenu?> GetMenuByDataIdAsync(int dataId, MenuType menuType)
// {
// Stopwatch stopwatch = new Stopwatch();
// stopwatch.Start();
// var result = await Db.Queryable<DbMenu>()
// .FirstAsync(m => m.DataId == dataId && m.Type == menuType);
// stopwatch.Stop();
// NlogHelper.Info($"根据DataId '{dataId}' 和 MenuType '{menuType}' 获取菜单耗时:{stopwatch.ElapsedMilliseconds}ms");
// return result;
// }
public async Task<MenuBean> GetByIdAsync(int id) => throw new NotImplementedException();
public async Task<List<MenuBean>> GetAllAsync() => throw new NotImplementedException();
public async Task<MenuBean> AddAsync(MenuBean entity) => throw new NotImplementedException();
public async Task<int> UpdateAsync(MenuBean entity) => throw new NotImplementedException();
public async Task<int> DeleteAsync(MenuBean entity) => throw new NotImplementedException();
}

View File

@@ -1,56 +0,0 @@
using System.Diagnostics;
using AutoMapper;
using DMS.Infrastructure.Entities;
using DMS.Core.Enums;
using DMS.Core.Helper;
using DMS.Core.Models;
using DMS.Infrastructure.Data;
using DMS.Infrastructure.Interfaces;
namespace DMS.Infrastructure.Repositories;
/// <summary>
/// Mqtt仓储类用于操作DbMqtt实体
/// </summary>
public class MqttRepository : BaseRepository<DbMqttServer>
{
public MqttRepository(SqlSugarDbContext dbContext)
: base(dbContext)
{
}
/// <summary>
/// 根据ID获取Mqtt配置
/// </summary>
/// <param name="id">主键ID</param>
/// <returns></returns>
public override async Task<DbMqttServer> GetByIdAsync(int id)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
var result = await Db.Queryable<DbMqttServer>()
.In(id)
.SingleAsync();
stopwatch.Stop();
NlogHelper.Info($"根据ID '{id}' 获取Mqtt配置耗时{stopwatch.ElapsedMilliseconds}ms");
return result;
}
/// <summary>
/// 获取所有Mqtt配置
/// </summary>
/// <returns></returns>
public override async Task<List<DbMqttServer>> GetAllAsync()
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
var result = await Db.Queryable<DbMqttServer>()
.ToListAsync();
stopwatch.Stop();
NlogHelper.Info($"获取所有Mqtt配置耗时{stopwatch.ElapsedMilliseconds}ms");
return result;
}
}

View File

@@ -0,0 +1,29 @@
using System.Diagnostics;
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;
/// <summary>
/// Mqtt仓储类用于操作DbMqtt实体
/// </summary>
public class MqttServerRepository : BaseRepository<DbMqttServer>, IMqttServerRepository
{
public MqttServerRepository(SqlSugarDbContext dbContext)
: base(dbContext)
{
}
public async Task<MqttServer> GetByIdAsync(int id) => throw new NotImplementedException();
public async Task<List<MqttServer>> GetAllAsync() => throw new NotImplementedException();
public async Task<MqttServer> AddAsync(MqttServer entity) => throw new NotImplementedException();
public async Task<int> UpdateAsync(MqttServer entity) => throw new NotImplementedException();
public async Task<int> DeleteAsync(MqttServer entity) => throw new NotImplementedException();
}

View File

@@ -0,0 +1,46 @@
using DMS.Core.Interfaces;
using DMS.Core.Interfaces.Repositories;
using DMS.Infrastructure.Data;
using SqlSugar;
namespace DMS.Infrastructure.Repositories;
public class RepositoryManager : IRepositoryManager
{
private readonly SqlSugarClient _db;
private readonly SqlSugarDbContext _dbContext;
public RepositoryManager(SqlSugarDbContext dbContext)
{
_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);
}
public void Dispose()
{
_db.Close();
}
public IDeviceRepository Devices { get; set; }
public IVariableTableRepository VariableTables { get; set; }
public IVariableRepository Variables { get; set; }
public IMqttServerRepository MqttServers { get; set; }
public IVariableMqttAliasRepository VariableMqttAliases { get; set; }
public IMenuRepository Menus { get; set; }
public IVariableHistoryRepository VariableHistories { get; set; }
public IUserRepository Users { get; set; }
public async Task BeginTranAsync() => await _db.BeginTranAsync();
public async Task CommitAsync() => await _db.CommitTranAsync();
public async Task RollbackAsync() => await _db.RollbackTranAsync();
}

View File

@@ -1,18 +1,28 @@
using AutoMapper;
using DMS.Core.Interfaces;
using DMS.Core.Interfaces.Repositories;
using DMS.Core.Models;
using DMS.Infrastructure.Interfaces;
using DMS.Infrastructure.Entities;
using DMS.Infrastructure.Data;
using DMS.Infrastructure.Entities;
namespace DMS.Infrastructure.Repositories;
/// <summary>
/// 用户仓储类用于操作DbUser实体
/// 用户仓储类用于操作DbUser实体
/// </summary>
public class UserRepository : BaseRepository<DbUser>
public class UserRepository : BaseRepository<DbUser>, IUserRepository
{
public UserRepository(SqlSugarDbContext dbContext)
: base(dbContext)
{
}
public async Task<User> GetByIdAsync(int id) => throw new NotImplementedException();
public async Task<List<User>> GetAllAsync() => throw new NotImplementedException();
public async Task<User> AddAsync(User entity) => throw new NotImplementedException();
public async Task<int> UpdateAsync(User entity) => throw new NotImplementedException();
public async Task<int> DeleteAsync(User entity) => throw new NotImplementedException();
}

View File

@@ -1,18 +0,0 @@
using AutoMapper;
using DMS.Core.Models;
using DMS.Infrastructure.Interfaces;
using DMS.Infrastructure.Entities;
using System.Diagnostics;
using DMS.Infrastructure.Data;
namespace DMS.Infrastructure.Repositories;
public class VarTableRepository : BaseRepository<DbVariableTable>, IVarTableRepository
{
public VarTableRepository(SqlSugarDbContext dbContext)
: base(dbContext)
{
}
}

View File

@@ -0,0 +1,24 @@
using DMS.Core.Interfaces.Repositories;
using DMS.Core.Models;
using DMS.Infrastructure.Data;
using DMS.Infrastructure.Entities;
namespace DMS.Infrastructure.Repositories;
public class VariableHistoryRepository : BaseRepository<DbVariableHistory>, IVariableHistoryRepository
{
public VariableHistoryRepository(SqlSugarDbContext dbContext)
: base(dbContext)
{
}
public async Task<VariableHistory> GetByIdAsync(int id) => throw new NotImplementedException();
public async Task<List<VariableHistory>> GetAllAsync() => throw new NotImplementedException();
public async Task<VariableHistory> AddAsync(VariableHistory entity) => throw new NotImplementedException();
public async Task<int> UpdateAsync(VariableHistory entity) => throw new NotImplementedException();
public async Task<int> DeleteAsync(VariableHistory entity) => throw new NotImplementedException();
}

View File

@@ -1,18 +1,26 @@
using AutoMapper;
using DMS.Core.Models;
using DMS.Infrastructure.Interfaces;
using DMS.Infrastructure.Entities;
using DMS.Core.Interfaces.Repositories;
using DMS.Infrastructure.Data;
using DMS.Infrastructure.Entities;
namespace DMS.Infrastructure.Repositories;
/// <summary>
/// 变量与MQTT服务器别名关联的数据仓库。
/// 变量与MQTT服务器别名关联的数据仓库。
/// </summary>
public class VariableMqttAliasRepository : BaseRepository<DbVariableMqttAlias>
public class VariableMqttAliasRepository : BaseRepository<DbVariableMqttAlias>, IVariableMqttAliasRepository
{
public VariableMqttAliasRepository(SqlSugarDbContext dbContext)
: base(dbContext)
{
}
}
public async Task<VariableMqttAlias> GetByIdAsync(int id) => throw new NotImplementedException();
public async Task<List<VariableMqttAlias>> GetAllAsync() => throw new NotImplementedException();
public async Task<VariableMqttAlias> AddAsync(VariableMqttAlias entity) => throw new NotImplementedException();
public async Task<int> UpdateAsync(VariableMqttAlias entity) => throw new NotImplementedException();
public async Task<int> DeleteAsync(VariableMqttAlias entity) => throw new NotImplementedException();
}

View File

@@ -1,56 +1,21 @@
using AutoMapper;
using DMS.Core.Models;
using DMS.Infrastructure.Interfaces;
using System.Diagnostics;
using DMS.Infrastructure.Entities;
using DMS.Core.Interfaces.Repositories;
using DMS.Core.Models;
using DMS.Infrastructure.Data;
using DMS.Infrastructure.Entities;
namespace DMS.Infrastructure.Repositories;
/// <summary>
/// VariableData仓储类用于操作DbVariableData实体
/// VariableData仓储类用于操作DbVariableData实体
/// </summary>
public class VarDataRepository : BaseRepository<DbVariable>
public class VariableRepository : BaseRepository<DbVariable>, IVariableRepository
{
public VarDataRepository(SqlSugarDbContext dbContext)
public VariableRepository(SqlSugarDbContext dbContext)
: base(dbContext)
{
}
public override async Task<List<DbVariable>> GetAllAsync()
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
var result = await Db.Queryable<DbVariable>()
.ToListAsync();
stopwatch.Stop();
//NlogHelper.Info($"获取所有VariableData耗时{stopwatch.ElapsedMilliseconds}ms");
return result;
}
public async Task<List<DbVariable>> GetByVariableTableIdAsync(int varTableId)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
var result = await Db.Queryable<DbVariable>()
.Where(d => d.VariableTableId == varTableId)
.ToListAsync();
stopwatch.Stop();
//NlogHelper.Info($"获取变量表的所有变量{result.Count()}个耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
// public VarDataRepository(IMapper _mapper)
// {
// _mapper = _mapper;
// }
/*
/// <summary>
@@ -134,4 +99,13 @@ public class VarDataRepository : BaseRepository<DbVariable>
}
}
*/
public async Task<Variable> GetByIdAsync(int id) => throw new NotImplementedException();
public async Task<List<Variable>> GetAllAsync() => throw new NotImplementedException();
public async Task<Variable> AddAsync(Variable entity) => throw new NotImplementedException();
public async Task<int> UpdateAsync(Variable entity) => throw new NotImplementedException();
public async Task<int> DeleteAsync(Variable entity) => throw new NotImplementedException();
}

View File

@@ -0,0 +1,24 @@
using DMS.Core.Interfaces.Repositories;
using DMS.Core.Models;
using DMS.Infrastructure.Data;
using DMS.Infrastructure.Entities;
namespace DMS.Infrastructure.Repositories;
public class VariableTableRepository : BaseRepository<DbVariableTable>, IVariableTableRepository
{
public VariableTableRepository(SqlSugarDbContext dbContext)
: base(dbContext)
{
}
public async Task<VariableTable> GetByIdAsync(int id) => throw new NotImplementedException();
public async Task<List<VariableTable>> GetAllAsync() => throw new NotImplementedException();
public async Task<VariableTable> AddAsync(VariableTable entity) => throw new NotImplementedException();
public async Task<int> UpdateAsync(VariableTable entity) => throw new NotImplementedException();
public async Task<int> DeleteAsync(VariableTable entity) => throw new NotImplementedException();
}

View File

@@ -15,17 +15,17 @@ namespace DMS.Infrastructure.Services
where TRepository : BaseRepository<TEntity>
{
protected readonly IMapper _mapper;
protected readonly TRepository _repository;
protected readonly TRepository ServerRepository;
/// <summary>
/// 初始化 BaseService 的新实例。
/// </summary>
/// <param name="mapper">AutoMapper 实例,用于对象映射。</param>
/// <param name="repository">仓储实例,用于数据访问。</param>
protected BaseService(IMapper mapper, TRepository repository)
/// <param name="serverRepository">仓储实例,用于数据访问。</param>
protected BaseService(IMapper mapper, TRepository serverRepository)
{
_mapper = mapper;
_repository = repository;
ServerRepository = serverRepository;
}
/// <summary>
@@ -36,7 +36,7 @@ namespace DMS.Infrastructure.Services
public virtual async Task<TEntity> AddAsync(TModel model)
{
var entity = _mapper.Map<TEntity>(model);
return await _repository.AddAsync(entity);
return await ServerRepository.AddAsync(entity);
}
/// <summary>
@@ -47,7 +47,7 @@ namespace DMS.Infrastructure.Services
public virtual async Task<int> UpdateAsync(TModel model)
{
var entity = _mapper.Map<TEntity>(model);
return await _repository.UpdateAsync(entity);
return await ServerRepository.UpdateAsync(entity);
}
/// <summary>
@@ -58,7 +58,7 @@ namespace DMS.Infrastructure.Services
public virtual async Task<int> DeleteAsync(TModel model)
{
var entity = _mapper.Map<TEntity>(model);
return await _repository.DeleteAsync(entity);
return await ServerRepository.DeleteAsync(entity);
}
}
}

View File

@@ -7,10 +7,11 @@ using SqlSugar;
using System;
using System.Linq;
using System.Threading.Tasks;
using DMS.Core.Interfaces;
namespace DMS.Infrastructure.Services
{
public class DatabaseInitializerService : DMS.Infrastructure.Interfaces.IDatabaseService
public class DatabaseInitializerService : IDatabaseService
{
private readonly SqlSugarClient _db;

View File

@@ -4,7 +4,6 @@ using DMS.Core.Models;
using DMS.Infrastructure.Data;
using DMS.Infrastructure.Entities;
using DMS.Infrastructure.Repositories;
using DMS.Infrastructure.Interfaces;
using SqlSugar;
using System;
using System.Collections.Generic;
@@ -12,6 +11,8 @@ using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Concurrent;
using DMS.Core.Interfaces;
using DMS.Core.Interfaces.Repositories;
namespace DMS.Infrastructure.Services
{
@@ -19,7 +20,7 @@ namespace DMS.Infrastructure.Services
{
private readonly IDeviceRepository _deviceRepository;
private readonly IMenuRepository _menuRepository;
private readonly IVarTableRepository _varTableRepository;
private readonly IVariableTableRepository _variableTableRepository;
private readonly IMapper _mapper;
@@ -60,7 +61,7 @@ namespace DMS.Infrastructure.Services
// dbVariableTable.Description = "默认变量表";
// dbVariableTable.DeviceId = addDevice.Id;
// dbVariableTable.ProtocolType = addDevice.ProtocolType;
// var dbAddVarTable= await _varTableRepository.AddAsync(dbVariableTable);
// var dbAddVarTable= await _variableTableRepository.AddAsync(dbVariableTable);
// if (addDevice.VariableTables==null)
// {
// addDevice.VariableTables= new List<DbVariableTable>();

View File

@@ -4,7 +4,6 @@ using DMS.Core.Helper;
using DMS.Core.Models;
using DMS.Infrastructure.Data;
using DMS.Infrastructure.Entities;
using DMS.Infrastructure.Interfaces;
using DMS.Infrastructure.Repositories;
using SqlSugar;
using System;
@@ -14,9 +13,9 @@ using System.Threading.Tasks;
namespace DMS.Infrastructure.Services
{
public class MqttService:BaseService<MqttServer, DbMqttServer, MqttRepository>
public class MqttService:BaseService<MqttServer, DbMqttServer, MqttServerRepository>
{
public MqttService(IMapper mapper, MqttRepository repository) : base(mapper, repository)
public MqttService(IMapper mapper, MqttServerRepository serverRepository) : base(mapper, serverRepository)
{
}
}

View File

@@ -13,11 +13,11 @@ using DMS.Infrastructure.Repositories;
namespace DMS.Infrastructure.Services
{
public class VarDataService : BaseService<Variable, DbVariable, VarDataRepository>
public class VarDataService : BaseService<Variable, DbVariable, VariableRepository>
{
private readonly IMapper _mapper;
public VarDataService(IMapper mapper, VarDataRepository repository) : base(mapper, repository)
public VarDataService(IMapper mapper, VariableRepository repository) : base(mapper, repository)
{
_mapper = mapper;
}

View File

@@ -5,9 +5,9 @@ using DMS.Infrastructure.Repositories;
namespace DMS.Infrastructure.Services;
public class VarTableService : BaseService<VariableTable, DbVariableTable, VarTableRepository>
public class VarTableService : BaseService<VariableTable, DbVariableTable, VariableTableRepository>
{
public VarTableService(IMapper mapper, VarTableRepository repository) : base(mapper, repository)
public VarTableService(IMapper mapper, VariableTableRepository repository) : base(mapper, repository)
{
}
}