2025-07-21 18:49:49 +08:00
|
|
|
|
using System.Diagnostics;
|
2025-07-21 22:02:42 +08:00
|
|
|
|
using AutoMapper;
|
2025-07-21 18:49:49 +08:00
|
|
|
|
using DMS.Core.Interfaces.Repositories;
|
|
|
|
|
|
using DMS.Core.Models;
|
|
|
|
|
|
using DMS.Infrastructure.Data;
|
|
|
|
|
|
using DMS.Infrastructure.Entities;
|
2025-09-04 17:29:24 +08:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2025-07-21 18:49:49 +08:00
|
|
|
|
|
|
|
|
|
|
namespace DMS.Infrastructure.Repositories;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-07-24 19:58:34 +08:00
|
|
|
|
/// MQTT服务器仓储实现类,负责MQTT服务器数据的持久化操作。
|
|
|
|
|
|
/// 继承自 <see cref="BaseRepository{DbMqttServer}"/> 并实现 <see cref="IMqttServerRepository"/> 接口。
|
2025-07-21 18:49:49 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class MqttServerRepository : BaseRepository<DbMqttServer>, IMqttServerRepository
|
|
|
|
|
|
{
|
2025-07-21 22:02:42 +08:00
|
|
|
|
private readonly IMapper _mapper;
|
|
|
|
|
|
|
2025-07-24 19:58:34 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 构造函数,注入 AutoMapper 和 SqlSugarDbContext。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="mapper">AutoMapper 实例,用于实体模型和数据库模型之间的映射。</param>
|
|
|
|
|
|
/// <param name="dbContext">SqlSugar 数据库上下文,用于数据库操作。</param>
|
2025-09-04 17:29:24 +08:00
|
|
|
|
/// <param name="logger">日志记录器实例。</param>
|
|
|
|
|
|
public MqttServerRepository(IMapper mapper, SqlSugarDbContext dbContext, ILogger<MqttServerRepository> logger)
|
|
|
|
|
|
: base(dbContext, logger)
|
2025-07-21 18:49:49 +08:00
|
|
|
|
{
|
2025-07-21 22:02:42 +08:00
|
|
|
|
_mapper = mapper;
|
2025-07-21 18:49:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-24 19:58:34 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步根据ID获取单个MQTT服务器。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="id">MQTT服务器的唯一标识符。</param>
|
|
|
|
|
|
/// <returns>对应的MQTT服务器实体,如果不存在则为null。</returns>
|
2025-07-21 22:02:42 +08:00
|
|
|
|
public async Task<MqttServer> GetByIdAsync(int id)
|
|
|
|
|
|
{
|
|
|
|
|
|
var dbMqttServer = await base.GetByIdAsync(id);
|
|
|
|
|
|
return _mapper.Map<MqttServer>(dbMqttServer);
|
|
|
|
|
|
}
|
2025-07-21 18:49:49 +08:00
|
|
|
|
|
2025-07-24 19:58:34 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步获取所有MQTT服务器。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>包含所有MQTT服务器实体的列表。</returns>
|
2025-07-21 22:02:42 +08:00
|
|
|
|
public async Task<List<MqttServer>> GetAllAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
var dbList = await base.GetAllAsync();
|
|
|
|
|
|
return _mapper.Map<List<MqttServer>>(dbList);
|
|
|
|
|
|
}
|
2025-07-21 18:49:49 +08:00
|
|
|
|
|
2025-07-24 19:58:34 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步添加新MQTT服务器。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="entity">要添加的MQTT服务器实体。</param>
|
|
|
|
|
|
/// <returns>添加成功后的MQTT服务器实体(包含数据库生成的ID等信息)。</returns>
|
2025-07-21 22:02:42 +08:00
|
|
|
|
public async Task<MqttServer> AddAsync(MqttServer entity)
|
|
|
|
|
|
{
|
|
|
|
|
|
var dbMqttServer = await base.AddAsync(_mapper.Map<DbMqttServer>(entity));
|
|
|
|
|
|
return _mapper.Map(dbMqttServer, entity);
|
|
|
|
|
|
}
|
2025-07-21 18:49:49 +08:00
|
|
|
|
|
2025-07-24 19:58:34 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步更新现有MQTT服务器。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="entity">要更新的MQTT服务器实体。</param>
|
|
|
|
|
|
/// <returns>受影响的行数。</returns>
|
2025-07-21 22:02:42 +08:00
|
|
|
|
public async Task<int> UpdateAsync(MqttServer entity) => await base.UpdateAsync(_mapper.Map<DbMqttServer>(entity));
|
2025-07-21 18:49:49 +08:00
|
|
|
|
|
2025-07-24 19:58:34 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步删除MQTT服务器。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="entity">要删除的MQTT服务器实体。</param>
|
|
|
|
|
|
/// <returns>受影响的行数。</returns>
|
2025-10-21 12:27:45 +08:00
|
|
|
|
public async Task<int> DeleteAsync(MqttServer entity) => await base.DeleteAsync(new List<DbMqttServer> { _mapper.Map<DbMqttServer>(entity) });
|
2025-07-21 22:02:42 +08:00
|
|
|
|
|
2025-08-22 20:24:09 +08:00
|
|
|
|
|
2025-10-21 13:01:27 +08:00
|
|
|
|
|
2025-10-21 13:04:11 +08:00
|
|
|
|
public async Task<List<MqttServer>> AddAsync(List<MqttServer> entities)
|
2025-08-22 20:24:09 +08:00
|
|
|
|
{
|
|
|
|
|
|
var dbEntities = _mapper.Map<List<DbMqttServer>>(entities);
|
2025-10-21 13:04:11 +08:00
|
|
|
|
var addedEntities = await base.AddAsync(dbEntities);
|
2025-09-15 13:12:14 +08:00
|
|
|
|
return _mapper.Map<List<MqttServer>>(addedEntities);
|
2025-08-22 20:24:09 +08:00
|
|
|
|
}
|
2025-10-21 12:27:45 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步根据实体列表批量删除MQTT服务器。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="entities">要删除的MQTT服务器实体列表。</param>
|
|
|
|
|
|
/// <returns>受影响的行数。</returns>
|
|
|
|
|
|
public async Task<int> DeleteAsync(List<MqttServer> entities)
|
|
|
|
|
|
{
|
|
|
|
|
|
var dbEntities = _mapper.Map<List<DbMqttServer>>(entities);
|
|
|
|
|
|
return await base.DeleteAsync(dbEntities);
|
|
|
|
|
|
}
|
2025-07-21 18:49:49 +08:00
|
|
|
|
}
|