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

81 lines
2.9 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-07-19 11:11:01 +08:00
using DMS.Infrastructure.Interfaces;
2025-06-10 22:13:06 +08:00
namespace DMS.Infrastructure.Repositories;
2025-06-10 22:13:06 +08:00
2025-07-19 11:11:01 +08:00
public class DeviceRepository : BaseRepository<DbDevice, Device>, IDeviceRepository
2025-06-10 22:13:06 +08:00
{
2025-07-19 09:25:01 +08:00
private readonly IMenuRepository _menuRepository;
private readonly IVarTableRepository _varTableRepository;
2025-07-02 22:07:16 +08:00
2025-07-19 11:11:01 +08:00
public DeviceRepository(IMapper mapper, IMenuRepository menuRepository, IVarTableRepository varTableRepository, ITransaction transaction)
: base(mapper, transaction)
{
_menuRepository = menuRepository;
_varTableRepository = varTableRepository;
2025-06-10 22:13:06 +08:00
}
2025-07-19 11:11:01 +08:00
2025-07-02 22:07:16 +08:00
2025-07-19 11:11:01 +08:00
public override async Task<List<Device>> GetAllAsync()
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 11:11:01 +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)
.Includes(d => d.VariableTables, vt => vt.Variables, v => v.VariableMqtts)
.ToListAsync();
2025-07-04 18:33:48 +08:00
stopwatch.Stop();
2025-07-19 11:11:01 +08:00
NlogHelper.Info($"加载设备列表总耗时:{stopwatch.ElapsedMilliseconds}ms");
var devices = _mapper.Map<List<Device>>(dlist);
return devices;
2025-07-04 18:33:48 +08:00
}
2025-07-19 09:25:01 +08:00
public async Task<int> DeleteAsync(Device device, List<MenuBean> menus)
2025-06-10 22:13:06 +08:00
{
2025-07-04 18:33:48 +08:00
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
2025-07-19 11:11:01 +08:00
var result = await Db.Deleteable<DbDevice>(new DbDevice { Id = device.Id })
2025-07-04 18:33:48 +08:00
.ExecuteCommandAsync();
// 删除变量表
2025-07-19 11:11: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;
}
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-04 18:33:48 +08:00
2025-07-19 09:25:01 +08:00
//查询设备的名字是否存在
2025-07-19 11:11:01 +08:00
var exist = await Db.Queryable<DbDevice>()
2025-07-19 09:25:01 +08:00
.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. 将设备添加到数据库
2025-07-19 11:11:01 +08:00
var addDevice = await Db.Insertable<DbDevice>(_mapper.Map<DbDevice>(device))
2025-07-04 18:33:48 +08:00
.ExecuteReturnEntityAsync();
// 4. 为新设备添加菜单
2025-07-19 11:11: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
}
}