using System.Diagnostics; using DMS.Core.Interfaces.Repositories; using DMS.Infrastructure.Data; using DMS.Infrastructure.Entities; using AutoMapper; using Microsoft.Extensions.Logging; using DMS.Core.Models; using System.Collections.Generic; using System.Threading.Tasks; using System.Linq; namespace DMS.Infrastructure.Repositories; /// /// 变量与MQTT别名关联仓储实现类,负责变量与MQTT别名关联数据的持久化操作。 /// 继承自 并实现 接口。 /// public class MqttAliasRepository : BaseRepository, IMqttAliasRepository { private readonly IMapper _mapper; /// /// 构造函数,注入 AutoMapper 和 SqlSugarDbContext。 /// /// AutoMapper 实例,用于实体模型和数据库模型之间的映射。 /// SqlSugar 数据库上下文,用于数据库操作。 /// 日志记录器实例。 public MqttAliasRepository(IMapper mapper, SqlSugarDbContext dbContext, ILogger logger) : base(dbContext, logger) { _mapper = mapper; } /// /// 异步根据ID获取单个变量与MQTT别名关联。 /// /// 变量与MQTT别名关联的唯一标识符。 /// 对应的变量与MQTT别名关联实体,如果不存在则为null。 public async Task GetByIdAsync(int id) { var dbMqttAlias = await base.GetByIdAsync(id); return _mapper.Map(dbMqttAlias); } /// /// 异步获取所有变量与MQTT别名关联。 /// /// 包含所有变量与MQTT别名关联实体的列表。 public async Task> GetAllAsync() { var dbList = await base.GetAllAsync(); return _mapper.Map>(dbList); } /// /// 异步添加新变量与MQTT别名关联。 /// /// 要添加的变量与MQTT别名关联实体。 /// 添加成功后的变量与MQTT别名关联实体(包含数据库生成的ID等信息)。 public async Task AddAsync(MqttAlias entity) { var dbMqttAlias = await base.AddAsync(_mapper.Map(entity)); return _mapper.Map(dbMqttAlias, entity); } /// /// 异步更新现有变量与MQTT别名关联。 /// /// 要更新的变量与MQTT别名关联实体。 /// 受影响的行数。 public async Task UpdateAsync(MqttAlias entity) => await base.UpdateAsync(_mapper.Map(entity)); /// /// 异步删除变量与MQTT别名关联。 /// /// 要删除的变量与MQTT别名关联实体。 /// 受影响的行数。 public async Task DeleteAsync(MqttAlias entity) => await base.DeleteAsync(new List { _mapper.Map(entity) }); /// /// 异步根据ID删除变量与MQTT别名关联。 /// /// 要删除变量与MQTT别名关联的唯一标识符。 /// 受影响的行数。 public async Task DeleteByIdAsync(int id) { var stopwatch = new Stopwatch(); stopwatch.Start(); var result = await _dbContext.GetInstance().Deleteable(new DbMqttAlias() { Id = id }) .ExecuteCommandAsync(); stopwatch.Stop(); _logger.LogInformation($"Delete {typeof(DbMqttAlias)},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 async Task> AddBatchAsync(List entities) { var dbEntities = _mapper.Map>(entities); var addedEntities = await base.AddBatchAsync(dbEntities); return _mapper.Map>(addedEntities); } /// /// 异步根据实体列表批量删除变量与MQTT别名关联。 /// /// 要删除的变量与MQTT别名关联实体列表。 /// 受影响的行数。 public async Task DeleteAsync(List entities) { var dbEntities = _mapper.Map>(entities); return await base.DeleteAsync(dbEntities); } }