2025-07-21 22:02:42 +08:00
|
|
|
|
using System.Diagnostics;
|
2025-07-15 22:18:37 +08:00
|
|
|
|
using AutoMapper;
|
2025-07-21 18:49:49 +08:00
|
|
|
|
using DMS.Core.Interfaces.Repositories;
|
2025-07-19 09:25:01 +08:00
|
|
|
|
using DMS.Core.Models;
|
|
|
|
|
|
using DMS.Infrastructure.Data;
|
2025-07-21 18:49:49 +08:00
|
|
|
|
using DMS.Infrastructure.Entities;
|
2025-09-04 17:29:24 +08:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2025-06-10 22:13:06 +08:00
|
|
|
|
|
2025-07-18 22:21:16 +08:00
|
|
|
|
namespace DMS.Infrastructure.Repositories;
|
2025-06-10 22:13:06 +08:00
|
|
|
|
|
2025-07-24 19:58:34 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 设备仓储实现类,负责设备数据的持久化操作。
|
|
|
|
|
|
/// 继承自 <see cref="BaseRepository{DbDevice}"/> 并实现 <see cref="IDeviceRepository"/> 接口。
|
|
|
|
|
|
/// </summary>
|
2025-07-21 18:49:49 +08:00
|
|
|
|
public class DeviceRepository : BaseRepository<DbDevice>, IDeviceRepository
|
2025-06-10 22:13:06 +08:00
|
|
|
|
{
|
2025-07-21 18:49:49 +08:00
|
|
|
|
private readonly IMapper _mapper;
|
2025-07-02 22:07:16 +08:00
|
|
|
|
|
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 DeviceRepository(IMapper mapper, SqlSugarDbContext dbContext, ILogger<DeviceRepository> logger)
|
|
|
|
|
|
: base(dbContext, logger)
|
2025-06-24 20:48:38 +08:00
|
|
|
|
{
|
2025-07-21 18:49:49 +08:00
|
|
|
|
_mapper = mapper;
|
2025-06-10 22:13:06 +08:00
|
|
|
|
}
|
2025-06-23 17:01:06 +08:00
|
|
|
|
|
2025-07-24 19:58:34 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步获取所有设备。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>包含所有设备实体的列表。</returns>
|
2025-07-24 15:07:03 +08:00
|
|
|
|
public async Task<List<Core.Models.Device>> GetAllAsync()
|
2025-07-21 18:49:49 +08:00
|
|
|
|
{
|
|
|
|
|
|
var dbList = await base.GetAllAsync();
|
2025-07-24 15:07:03 +08:00
|
|
|
|
return _mapper.Map<List<Core.Models.Device>>(dbList);
|
2025-07-21 18:49:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-24 19:58:34 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步根据ID获取单个设备。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="id">设备的唯一标识符。</param>
|
|
|
|
|
|
/// <returns>对应的设备实体,如果不存在则为null。</returns>
|
2025-07-24 15:07:03 +08:00
|
|
|
|
public async Task<Core.Models.Device> GetByIdAsync(int id)
|
2025-07-21 18:49:49 +08:00
|
|
|
|
{
|
|
|
|
|
|
var dbDevice = await base.GetByIdAsync(id);
|
2025-07-24 15:07:03 +08:00
|
|
|
|
return _mapper.Map<Core.Models.Device>(dbDevice);
|
2025-07-21 18:49:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-24 19:58:34 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步添加新设备。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="model">要添加的设备实体。</param>
|
|
|
|
|
|
/// <returns>添加成功后的设备实体(包含数据库生成的ID等信息)。</returns>
|
2025-07-24 15:07:03 +08:00
|
|
|
|
public async Task<Core.Models.Device> AddAsync(Core.Models.Device model)
|
2025-07-21 18:49:49 +08:00
|
|
|
|
{
|
|
|
|
|
|
var dbDevice = await base.AddAsync(_mapper.Map<DbDevice>(model));
|
|
|
|
|
|
return _mapper.Map(dbDevice, model);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-24 19:58:34 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步更新现有设备。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="model">要更新的设备实体。</param>
|
|
|
|
|
|
/// <returns>受影响的行数。</returns>
|
2025-10-21 13:04:11 +08:00
|
|
|
|
public async Task<int> UpdateAsync(Device model) => await base.UpdateAsync(_mapper.Map<DbDevice>(model));
|
2025-07-21 18:49:49 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-07-24 19:58:34 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步删除设备。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="model">要删除的设备实体。</param>
|
|
|
|
|
|
/// <returns>受影响的行数。</returns>
|
2025-10-21 13:01:27 +08:00
|
|
|
|
public async Task<int> DeleteAsync(Device model) => await base.DeleteAsync(new List<DbDevice> { _mapper.Map<DbDevice>(model) });
|
2025-07-21 22:02:42 +08:00
|
|
|
|
|
2025-08-22 20:24:09 +08:00
|
|
|
|
|
2025-10-21 13:04:11 +08:00
|
|
|
|
public async Task<List<Device>> AddAsync(List<Device> entities)
|
2025-08-22 20:24:09 +08:00
|
|
|
|
{
|
|
|
|
|
|
var dbEntities = _mapper.Map<List<DbDevice>>(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<Device>>(addedEntities);
|
2025-08-22 20:24:09 +08:00
|
|
|
|
}
|
2025-10-21 12:27:45 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步根据实体列表批量删除设备。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="entities">要删除的设备实体列表。</param>
|
|
|
|
|
|
/// <returns>受影响的行数。</returns>
|
|
|
|
|
|
public async Task<int> DeleteAsync(List<Device> entities)
|
|
|
|
|
|
{
|
|
|
|
|
|
var dbEntities = _mapper.Map<List<DbDevice>>(entities);
|
|
|
|
|
|
return await base.DeleteAsync(dbEntities);
|
|
|
|
|
|
}
|
2025-06-10 22:13:06 +08:00
|
|
|
|
}
|