Files
DMS/DMS.Infrastructure/Repositories/DeviceRepository.cs

146 lines
4.8 KiB
C#
Raw Normal View History

2025-07-03 13:53:29 +08:00
using System.Diagnostics;
using AutoMapper;
using DMS.Infrastructure.Entities;
using DMS.Core.Enums;
2025-07-19 09:25:01 +08:00
using DMS.Core.Helper;
using DMS.Core.Models;
using DMS.Infrastructure.Data;
using SqlSugar;
2025-06-10 22:13:06 +08:00
namespace DMS.Infrastructure.Repositories;
2025-06-10 22:13:06 +08:00
2025-07-19 09:25:01 +08:00
public class DeviceRepository : IDeviceRepository
2025-06-10 22:13:06 +08:00
{
private readonly IMapper _mapper;
2025-07-19 09:25:01 +08:00
private readonly IMenuRepository _menuRepository;
private readonly IVarTableRepository _varTableRepository;
private readonly IUnitOfWork _unitOfWork;
2025-07-02 22:07:16 +08:00
2025-07-19 09:25:01 +08:00
public DeviceRepository(IMapper mapper, IMenuRepository menuRepository, IVarTableRepository varTableRepository, IUnitOfWork unitOfWork)
{
_mapper = mapper;
_menuRepository = menuRepository;
_varTableRepository = varTableRepository;
2025-07-19 09:25:01 +08:00
_unitOfWork = unitOfWork;
2025-06-10 22:13:06 +08:00
}
2025-07-02 22:07:16 +08:00
2025-07-19 09:25:01 +08:00
2025-07-03 13:53:29 +08:00
/// <summary>
/// 编辑设备信息。
/// </summary>
/// <param name="device">要编辑的设备对象。</param>
/// <returns>受影响的行数。</returns>
public async Task<int> UpdateAsync(Device device)
2025-06-10 22:13:06 +08:00
{
2025-07-03 13:53:29 +08:00
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
2025-07-19 09:25:01 +08:00
var db = _unitOfWork.GetInstance();
var result = await db.Updateable<DbDevice>(_mapper.Map<DbDevice>(device))
2025-07-04 18:33:48 +08:00
.ExecuteCommandAsync();
stopwatch.Stop();
2025-07-06 19:51:53 +08:00
NlogHelper.Info($"编辑设备 '{device.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
2025-07-04 18:33:48 +08:00
return result;
}
2025-07-19 09:25:01 +08:00
2025-07-02 22:07:16 +08:00
/// <summary>
/// 获取设备列表
/// </summary>
/// <returns></returns>
public async Task<List<Device>> GetAllAsync()
2025-07-01 21:34:20 +08:00
{
using (var db = DbContext.GetInstance())
{
2025-07-03 13:53:29 +08:00
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
2025-07-03 12:55:00 +08:00
var dlist = await db.Queryable<DbDevice>()
.Includes(d => d.VariableTables, dv => dv.Device)
.Includes(d => d.VariableTables, dvd => dvd.Variables, data => data.VariableTable)
2025-07-19 09:25:01 +08:00
.Includes(d => d.VariableTables, vt => vt.Variables, v => v.VariableMqtts)
2025-07-03 12:55:00 +08:00
.ToListAsync();
2025-07-19 09:25:01 +08:00
2025-07-04 18:33:48 +08:00
2025-07-03 13:53:29 +08:00
stopwatch.Stop();
2025-07-06 19:51:53 +08:00
NlogHelper.Info($"加载设备列表总耗时:{stopwatch.ElapsedMilliseconds}ms");
2025-07-19 09:25:01 +08:00
var devices = _mapper.Map<List<Device>>(dlist);
2025-07-01 21:34:20 +08:00
return devices;
}
2025-06-10 22:13:06 +08:00
}
2025-07-03 13:53:29 +08:00
/// <summary>
/// 根据ID获取设备信息。
/// </summary>
/// <param name="id">设备ID。</param>
/// <returns>对应的DbDevice对象。</returns>
public async Task<Device> GetByIdAsync(int id)
2025-06-10 22:13:06 +08:00
{
2025-07-03 13:53:29 +08:00
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
2025-07-02 22:07:16 +08:00
using (var db = DbContext.GetInstance())
2025-07-01 21:34:20 +08:00
{
2025-07-03 13:53:29 +08:00
var result = await db.Queryable<DbDevice>()
2025-07-04 18:33:48 +08:00
.FirstAsync(p => p.Id == id);
2025-07-03 13:53:29 +08:00
stopwatch.Stop();
2025-07-06 19:51:53 +08:00
NlogHelper.Info($"根据ID '{id}' 获取设备耗时:{stopwatch.ElapsedMilliseconds}ms");
return _mapper.Map<Device>(result);
2025-07-01 21:34:20 +08:00
}
2025-06-10 22:13:06 +08:00
}
2025-07-04 18:33:48 +08:00
2025-07-03 13:53:29 +08:00
/// <summary>
2025-07-04 18:33:48 +08:00
/// 删除设备。
2025-07-03 13:53:29 +08:00
/// </summary>
/// <param name="id">要删除的设备ID。</param>
/// <returns>受影响的行数。</returns>
public async Task<int> DeleteAsync(Device device, List<MenuBean> menus)
2025-06-10 22:13:06 +08:00
{
2025-07-19 09:25:01 +08:00
var db = _unitOfWork.GetInstance();
2025-07-04 18:33:48 +08:00
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
var result = await db.Deleteable<DbDevice>(new DbDevice { Id = device.Id })
.ExecuteCommandAsync();
// 删除变量表
2025-07-19 09:25:01 +08:00
await _varTableRepository.DeleteAsync(device.VariableTables);
2025-07-04 18:33:48 +08:00
stopwatch.Stop();
2025-07-06 19:51:53 +08:00
NlogHelper.Info($"删除设备:{device.Name},耗时:{stopwatch.ElapsedMilliseconds}ms");
2025-07-04 18:33:48 +08:00
return result;
}
2025-07-19 09:25:01 +08:00
2025-07-04 18:33:48 +08:00
/// <summary>
/// 添加设备
/// </summary>
/// <param name="device"></param>
public async Task AddAsync(Device device)
2025-07-02 22:07:16 +08:00
{
2025-07-03 13:53:29 +08:00
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
2025-07-19 09:25:01 +08:00
var db = _unitOfWork.GetInstance();
2025-07-04 18:33:48 +08:00
2025-07-19 09:25:01 +08:00
//查询设备的名字是否存在
var exist = await db.Queryable<DbDevice>()
.Where(d => d.Name == device.Name)
.FirstAsync();
if (exist != null)
throw new InvalidOperationException("设备名称已经存在。");
2025-07-02 22:07:16 +08:00
2025-07-19 09:25:01 +08:00
// 2. 将设备添加到数据库
var addDevice = await db.Insertable<DbDevice>(_mapper.Map<DbDevice>(device))
2025-07-04 18:33:48 +08:00
.ExecuteReturnEntityAsync();
// 4. 为新设备添加菜单
2025-07-19 09:25:01 +08:00
var addDeviceMenuId = await _menuRepository.AddAsync(addDevice);
2025-07-04 18:33:48 +08:00
2025-07-19 09:25:01 +08:00
stopwatch.Stop();
NlogHelper.Info($"添加设备 '{device.Name}' 及相关菜单耗时:{stopwatch.ElapsedMilliseconds}ms");
2025-06-10 22:13:06 +08:00
}
2025-07-19 09:25:01 +08:00
2025-06-10 22:13:06 +08:00
}