完成新建设备的单元 测试包括,添加变量表,和添加菜单

This commit is contained in:
2025-07-24 15:07:03 +08:00
parent b0d5db3626
commit ac38128e4d
33 changed files with 497 additions and 416 deletions

View File

@@ -1,24 +1,61 @@
using DMS.Core.Enums;
using SqlSugar;
using SqlSugar.DbConvert;
namespace DMS.Infrastructure.Entities;
/// <summary>
/// 数据库中的菜单项实体
/// </summary>
public class DbMenu
{
/// <summary>
/// 菜单的唯一标识符
/// </summary>
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public int Id { get; set; }
/// <summary>
/// 父菜单的标识符。如果是根菜单,则为 null。
/// </summary>
[SugarColumn(IsNullable = true)]
public int? ParentId { get; set; }
/// <summary>
/// 菜单项显示的文本
/// </summary>
public string Header { get; set; }
/// <summary>
/// 与菜单项关联的图标
/// </summary>
public string Icon { get; set; }
public string TargetViewKey { get; set; }
/// <summary>
/// 菜单的类型
/// </summary>
[SugarColumn(ColumnDataType="varchar(20)",SqlParameterDbType=typeof(EnumToStringConvert))]
public MenuType MenuType { get; set; }
/// <summary>
/// 菜单关联的数据ID例如设备Id变量表Id
/// </summary>
public int TargetId { get; set; }
/// <summary>
/// 导航的可选参数
/// </summary>
[SugarColumn(IsNullable = true)]
public string NavigationParameter { get; set; }
/// <summary>
/// 子菜单项
/// </summary>
[SugarColumn(IsIgnore = true)]
public List<DbMenu> Childrens { get; set; }
/// <summary>
/// 菜单项的显示顺序
/// </summary>
public int DisplayOrder { get; set; }
}

View File

@@ -2,21 +2,79 @@ using SqlSugar;
namespace DMS.Infrastructure.Entities;
/// <summary>
/// MQTT服务器配置实体
/// </summary>
public class DbMqttServer
{
/// <summary>
/// 唯一标识符
/// </summary>
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public int Id { get; set; }
/// <summary>
/// 服务器名称
/// </summary>
public string ServerName { get; set; }
/// <summary>
/// MQTT代理地址
/// </summary>
public string BrokerAddress { get; set; }
/// <summary>
/// 端口号
/// </summary>
public int Port { get; set; }
/// <summary>
/// 用户名
/// </summary>
public string Username { get; set; }
/// <summary>
/// 密码
/// </summary>
public string Password { get; set; }
/// <summary>
/// 是否激活
/// </summary>
public bool IsActive { get; set; }
/// <summary>
/// 订阅的主题
/// </summary>
public string SubscribeTopic { get; set; }
/// <summary>
/// 发布的主题
/// </summary>
public string PublishTopic { get; set; }
/// <summary>
/// 客户端ID
/// </summary>
public string ClientId { get; set; }
/// <summary>
/// 创建时间
/// </summary>
public DateTime CreatedAt { get; set; }
/// <summary>
/// 连接时间
/// </summary>
public DateTime? ConnectedAt { get; set; }
/// <summary>
/// 连接持续时间(秒)
/// </summary>
public long ConnectionDuration { get; set; }
/// <summary>
/// 消息格式
/// </summary>
public string MessageFormat { get; set; }
}

View File

@@ -35,7 +35,7 @@ public class DbVariableTable
/// 关联的设备
/// </summary>
[SugarColumn(IsIgnore = true)]
public DbDevice Device { get; set; }
public DbDevice DbDevice { get; set; }
/// <summary>
/// 协议类型

View File

@@ -17,7 +17,7 @@ public class MappingProfile : Profile
// --- 设备映射 (包含List的父对象) ---
// AutoMapper 会自动使用上面的规则来处理 VariableTables 属性
CreateMap<DbDevice, Device>()
CreateMap<DbDevice, Core.Models.Device>()
.ReverseMap();
// --- 变量表映射 (List中的元素) ---

View File

@@ -18,28 +18,28 @@ public class DeviceRepository : BaseRepository<DbDevice>, IDeviceRepository
_mapper = mapper;
}
public async Task<List<Device>> GetAllAsync()
public async Task<List<Core.Models.Device>> GetAllAsync()
{
var dbList = await base.GetAllAsync();
return _mapper.Map<List<Device>>(dbList);
return _mapper.Map<List<Core.Models.Device>>(dbList);
}
public async Task<Device> GetByIdAsync(int id)
public async Task<Core.Models.Device> GetByIdAsync(int id)
{
var dbDevice = await base.GetByIdAsync(id);
return _mapper.Map<Device>(dbDevice);
return _mapper.Map<Core.Models.Device>(dbDevice);
}
public async Task<Device> AddAsync(Device model)
public async Task<Core.Models.Device> AddAsync(Core.Models.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> UpdateAsync(Core.Models.Device model) => await base.UpdateAsync(_mapper.Map<DbDevice>(model));
public async Task<int> DeleteAsync(Device model) => await base.DeleteAsync(_mapper.Map<DbDevice>(model));
public async Task<int> DeleteAsync(Core.Models.Device model) => await base.DeleteAsync(_mapper.Map<DbDevice>(model));
public async Task<int> DeleteAsync(int id)
{
@@ -52,10 +52,10 @@ public class DeviceRepository : BaseRepository<DbDevice>, IDeviceRepository
return result;
}
public new async Task<List<Device>> TakeAsync(int number)
public new async Task<List<Core.Models.Device>> TakeAsync(int number)
{
var dbList = await base.TakeAsync(number);
return _mapper.Map<List<Device>>(dbList);
return _mapper.Map<List<Core.Models.Device>>(dbList);
}
}

View File

@@ -1,20 +1,18 @@
using DMS.Core.Interfaces;
using DMS.Core.Interfaces.Repositories;
using DMS.Core.Models;
using DMS.Infrastructure.Configurations;
using DMS.Infrastructure.Data;
using DMS.Infrastructure.Entities;
using SqlSugar;
namespace DMS.Infrastructure.Services;
namespace DMS.Infrastructure.Repositories;
public class DatabaseService : IDatabaseService
public class InitializeRepository : IInitializeRepository
{
private readonly SqlSugarDbContext _dbContext;
private readonly SqlSugarClient _db;
public DatabaseService(SqlSugarDbContext dbContext)
public InitializeRepository(SqlSugarDbContext dbContext)
{
_dbContext = dbContext;
_db = _dbContext.GetInstance();

View File

@@ -18,6 +18,7 @@ public class RepositoryManager : IRepositoryManager
_dbContext = dbContext;
_db = dbContext.GetInstance();
InitializeRepository=new InitializeRepository(dbContext);
Devices = new DeviceRepository(mapper, dbContext);
VariableTables = new VariableTableRepository(mapper, dbContext);
Variables = new VariableRepository(mapper, dbContext);
@@ -41,6 +42,7 @@ public class RepositoryManager : IRepositoryManager
public IMenuRepository Menus { get; set; }
public IVariableHistoryRepository VariableHistories { get; set; }
public IUserRepository Users { get; set; }
public IInitializeRepository InitializeRepository { get; set; }
public async Task BeginTranAsync() => await _db.BeginTranAsync();
public async Task CommitAsync() => await _db.CommitTranAsync();

View File

@@ -1,74 +0,0 @@
using AutoMapper;
using DMS.Infrastructure.Repositories;
using System.Threading.Tasks;
using DMS.Core.Interfaces.Repositories;
namespace DMS.Infrastructure.Services
{
/// <summary>
/// 通用服务基类,封装了常见的增、删、改操作。
/// </summary>
/// <typeparam name="TModel">业务逻辑模型类型。</typeparam>
/// <typeparam name="TEntity">数据库实体类型。</typeparam>
/// <typeparam name="TRepository">与实体对应的仓储类型。</typeparam>
public abstract class BaseService<TModel, TRepository>
where TModel : class, new()
where TRepository : IBaseRepository<TModel>
{
protected readonly TRepository ServerRepository;
/// <summary>
/// 初始化 BaseService 的新实例。
/// </summary>
/// <param name="mapper">AutoMapper 实例,用于对象映射。</param>
/// <param name="serverRepository">仓储实例,用于数据访问。</param>
protected BaseService( TRepository serverRepository)
{
ServerRepository = serverRepository;
}
/// <summary>
/// 异步添加一个新的业务模型对象。
/// </summary>
/// <param name="model">要添加的业务模型对象。</param>
/// <returns>返回添加后的数据库实体。</returns>
public virtual async Task<TModel> AddAsync(TModel model)
{
return await ServerRepository.AddAsync(model);
}
/// <summary>
/// 异步更新一个现有的业务模型对象。
/// </summary>
/// <param name="model">要更新的业务模型对象。</param>
/// <returns>返回受影响的行数。</returns>
public virtual async Task<int> UpdateAsync(TModel model)
{
return await ServerRepository.UpdateAsync(model);
}
/// <summary>
/// 异步删除一个业务模型对象。
/// </summary>
/// <param name="model">要删除的业务模型对象。</param>
/// <returns>返回受影响的行数。</returns>
public virtual async Task<int> DeleteAsync(TModel model)
{
return await ServerRepository.DeleteAsync(model);
}
public virtual async Task<List<TModel>> GetAllAsync()
{
return await ServerRepository.GetAllAsync();
}
public virtual async Task<TModel> GetByIdAsync(int id)
{
return await ServerRepository.GetByIdAsync(id);
}
public virtual async Task<List<TModel>> TakeAsync(int number)
{
return await ServerRepository.TakeAsync(number);
}
}
}

View File

@@ -1,29 +0,0 @@
using AutoMapper;
using DMS.Core.Helper;
using DMS.Core.Models;
using DMS.Infrastructure.Data;
using DMS.Infrastructure.Entities;
using DMS.Infrastructure.Repositories;
using SqlSugar;
using System;
using System.Collections.Generic;
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
{
public class DeviceService : BaseService<Device, DeviceRepository>
{
private readonly IDeviceRepository _deviceRepository;
public DeviceService(DeviceRepository repository) : base(repository)
{
_deviceRepository = repository;
}
}
}

View File

@@ -1,14 +0,0 @@
using AutoMapper;
using DMS.Core.Models;
using DMS.Infrastructure.Entities;
using DMS.Infrastructure.Repositories;
namespace DMS.Infrastructure.Services
{
public class MenuService : BaseService<MenuBean, MenuRepository>
{
public MenuService(MenuRepository repository) : base(repository)
{
}
}
}

View File

@@ -1,22 +0,0 @@
using AutoMapper;
using DMS.Core.Enums;
using DMS.Core.Helper;
using DMS.Core.Models;
using DMS.Infrastructure.Data;
using DMS.Infrastructure.Entities;
using DMS.Infrastructure.Repositories;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
namespace DMS.Infrastructure.Services
{
public class MqttService:BaseService<MqttServer, MqttServerRepository>
{
public MqttService( MqttServerRepository serverRepository) : base( serverRepository)
{
}
}
}

View File

@@ -1,24 +0,0 @@
using AutoMapper;
using DMS.Core.Helper;
using DMS.Core.Models;
using DMS.Infrastructure.Data;
using DMS.Infrastructure.Entities;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using DMS.Infrastructure.Repositories;
namespace DMS.Infrastructure.Services
{
public class VariableService : BaseService<Variable, VariableRepository>
{
public VariableService(VariableRepository repository) : base(repository)
{
}
}
}

View File

@@ -1,13 +0,0 @@
using AutoMapper;
using DMS.Core.Models;
using DMS.Infrastructure.Entities;
using DMS.Infrastructure.Repositories;
namespace DMS.Infrastructure.Services;
public class VariableTableService : BaseService<VariableTable, VariableTableRepository>
{
public VariableTableService(VariableTableRepository repository) : base(repository)
{
}
}