using System.Diagnostics; using DMS.Core.Interfaces.Repositories; using DMS.Infrastructure.Data; using DMS.Infrastructure.Entities; using AutoMapper; using DMS.Core.Helper; using DMS.Core.Models; namespace DMS.Infrastructure.Repositories; /// /// 变量与MQTT别名关联仓储实现类,负责变量与MQTT别名关联数据的持久化操作。 /// 继承自 并实现 接口。 /// public class VariableMqttAliasRepository : BaseRepository, IVariableMqttAliasRepository { private readonly IMapper _mapper; /// /// 构造函数,注入 AutoMapper 和 SqlSugarDbContext。 /// /// AutoMapper 实例,用于实体模型和数据库模型之间的映射。 /// SqlSugar 数据库上下文,用于数据库操作。 public VariableMqttAliasRepository(IMapper mapper, SqlSugarDbContext dbContext) : base(dbContext) { _mapper = mapper; } /// /// 异步根据ID获取单个变量与MQTT别名关联。 /// /// 变量与MQTT别名关联的唯一标识符。 /// 对应的变量与MQTT别名关联实体,如果不存在则为null。 public async Task GetByIdAsync(int id) { var dbVariableMqttAlias = await base.GetByIdAsync(id); return _mapper.Map(dbVariableMqttAlias); } /// /// 异步获取所有变量与MQTT别名关联。 /// /// 包含所有变量与MQTT别名关联实体的列表。 public async Task> GetAllAsync() { var dbList = await base.GetAllAsync(); return _mapper.Map>(dbList); } /// /// 异步添加新变量与MQTT别名关联。 /// /// 要添加的变量与MQTT别名关联实体。 /// 添加成功后的变量与MQTT别名关联实体(包含数据库生成的ID等信息)。 public async Task AddAsync(VariableMqttAlias entity) { var dbVariableMqttAlias = await base.AddAsync(_mapper.Map(entity)); return _mapper.Map(dbVariableMqttAlias, entity); } /// /// 异步更新现有变量与MQTT别名关联。 /// /// 要更新的变量与MQTT别名关联实体。 /// 受影响的行数。 public async Task UpdateAsync(VariableMqttAlias entity) => await base.UpdateAsync(_mapper.Map(entity)); /// /// 异步删除变量与MQTT别名关联。 /// /// 要删除的变量与MQTT别名关联实体。 /// 受影响的行数。 public async Task DeleteAsync(VariableMqttAlias entity) => await base.DeleteAsync(_mapper.Map(entity)); /// /// 异步根据ID删除变量与MQTT别名关联。 /// /// 要删除变量与MQTT别名关联的唯一标识符。 /// 受影响的行数。 public async Task DeleteByIdAsync(int id) { var stopwatch = new Stopwatch(); stopwatch.Start(); var result = await Db.Deleteable(new DbVariableMqttAlias() { Id = id }) .ExecuteCommandAsync(); stopwatch.Stop(); NlogHelper.Info($"Delete {typeof(DbVariableMqttAlias)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } /// /// 异步获取指定数量的变量与MQTT别名关联。 /// /// 要获取的变量与MQTT别名关联数量。 /// 包含指定数量变量与MQTT别名关联实体的列表。 public new async Task> TakeAsync(int number) { var dbList = await base.TakeAsync(number); return _mapper.Map>(dbList); } public Task AddBatchAsync(List entities) { var dbEntities = _mapper.Map>(entities); return base.AddBatchAsync(dbEntities); } }