using System.Diagnostics;
using AutoMapper;
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;
///
/// Mqtt仓储类,用于操作DbMqtt实体
///
public class MqttServerRepository : BaseRepository, IMqttServerRepository
{
private readonly IMapper _mapper;
public MqttServerRepository(IMapper mapper, SqlSugarDbContext dbContext)
: base(dbContext)
{
_mapper = mapper;
}
public async Task GetByIdAsync(int id)
{
var dbMqttServer = await base.GetByIdAsync(id);
return _mapper.Map(dbMqttServer);
}
public async Task> GetAllAsync()
{
var dbList = await base.GetAllAsync();
return _mapper.Map>(dbList);
}
public async Task AddAsync(MqttServer entity)
{
var dbMqttServer = await base.AddAsync(_mapper.Map(entity));
return _mapper.Map(dbMqttServer, entity);
}
public async Task UpdateAsync(MqttServer entity) => await base.UpdateAsync(_mapper.Map(entity));
public async Task DeleteAsync(MqttServer entity) => await base.DeleteAsync(_mapper.Map(entity));
public async Task DeleteAsync(int id)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
var result = await Db.Deleteable(new MqttServer() { Id = id })
.ExecuteCommandAsync();
stopwatch.Stop();
NlogHelper.Info($"Delete {typeof(MqttServer)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
}