using System.Diagnostics; using AutoMapper; using DMS.Infrastructure.Entities; using DMS.Core.Enums; using DMS.Helper; using DMS.Models; using iNKORE.UI.WPF.Modern.Common.IconKeys; using DMS.Extensions; using SqlSugar; namespace DMS.Infrastructure.Repositories; public class DeviceRepository { private readonly IMapper _mapper; private readonly MenuRepository _menuRepository; private readonly VarTableRepository _varTableRepository; public DeviceRepository(IMapper mapper,MenuRepository menuRepository,VarTableRepository varTableRepository) { _mapper = mapper; _menuRepository = menuRepository; _varTableRepository = varTableRepository; } /// /// 编辑设备信息。 /// /// 要编辑的设备对象。 /// 受影响的行数。 public async Task UpdateAsync(Device device) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); using (var db = DbContext.GetInstance()) { var result = await UpdateAsync(device, db); stopwatch.Stop(); NlogHelper.Info($"编辑设备 '{device.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } } /// /// 编辑设备信息。支持事务 /// /// 要编辑的设备对象。 /// 受影响的行数。 public async Task UpdateAsync(Device device, SqlSugarClient db) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); var result = await db.Updateable(_mapper.Map(device)) .ExecuteCommandAsync(); stopwatch.Stop(); NlogHelper.Info($"编辑设备 '{device.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } /// /// 获取设备列表 /// /// public async Task> GetAllAsync() { using (var db = DbContext.GetInstance()) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); var dlist = await db.Queryable() .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(); stopwatch.Stop(); NlogHelper.Info($"加载设备列表总耗时:{stopwatch.ElapsedMilliseconds}ms"); var devices= _mapper.Map>(dlist); return devices; } } /// /// 根据ID获取设备信息。 /// /// 设备ID。 /// 对应的DbDevice对象。 public async Task GetByIdAsync(int id) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); using (var db = DbContext.GetInstance()) { var result = await db.Queryable() .FirstAsync(p => p.Id == id); stopwatch.Stop(); NlogHelper.Info($"根据ID '{id}' 获取设备耗时:{stopwatch.ElapsedMilliseconds}ms"); return _mapper.Map(result); } } /// /// 删除设备。 /// /// 要删除的设备ID。 /// 受影响的行数。 public async Task DeleteAsync(Device device, List menus) { using (var db = DbContext.GetInstance()) { await db.BeginTranAsync(); try { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); var res = await DeleteAsync(device, menus, db); stopwatch.Stop(); NlogHelper.Info($"删除设备:{device.Name},耗时:{stopwatch.ElapsedMilliseconds}ms"); await db.CommitTranAsync(); return res; } catch (Exception e) { await db.RollbackTranAsync(); throw; } return 0; } } /// /// 删除设备。 /// /// /// /// /// 要删除的设备ID。 /// 受影响的行数。 public async Task DeleteAsync(Device device, List menus, SqlSugarClient db) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); var result = await db.Deleteable(new DbDevice { Id = device.Id }) .ExecuteCommandAsync(); // 删除变量表 await _varTableRepository.DeleteAsync(device.VariableTables, db); // 删除菜单 var menu = DataServicesHelper.FindMenusForDevice(device, menus); if (menu == null) throw new NullReferenceException($"没有找到设备:{device.Name},的菜单对象。"); await _menuRepository.DeleteAsync(menu, db); stopwatch.Stop(); NlogHelper.Info($"删除设备:{device.Name},耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } /// /// 添加设备 /// /// public async Task AddAsync(Device device) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); var db = DbContext.GetInstance(); try { // 开启事务 await db.BeginTranAsync(); //查询设备的名字是否存在 var exist = await db.Queryable() .Where(d => d.Name == device.Name) .FirstAsync(); if (exist != null) throw new InvalidOperationException("设备名称已经存在。"); // 2. 将设备添加到数据库 var addDevice = await AddAsync(device, db); await db.CommitTranAsync(); // 菜单也添加成功,通知 UI 更新 MessageHelper.SendLoadMessage(LoadTypes.Menu); MessageHelper.SendLoadMessage(LoadTypes.Devices); } catch (Exception e) { // 中间出错了 回滚 await db.RollbackTranAsync(); // 捕获并记录所有未预期的异常 NotificationHelper.ShowError("添加设备的过程中发生了不可预期的错误:" + e.Message, e); } finally { stopwatch.Stop(); NlogHelper.Info($"添加设备 '{device.Name}' 及相关菜单耗时:{stopwatch.ElapsedMilliseconds}ms"); } } /// /// 单独添加设备,不包括菜单和变量表 /// /// /// /// /// private async Task AddAsync(Device device, SqlSugarClient db) { ; // 添加设备 var addDevice = await db.Insertable(_mapper.Map(device)) .ExecuteReturnEntityAsync(); // 4. 为新设备添加菜单 var addDeviceMenuId = await _menuRepository.AddAsync(addDevice, db); if (device.IsAddDefVarTable) { // 添加默认变量表 var varTable = new VariableTable(); device.VariableTables = new (); varTable.IsActive = true; varTable.DeviceId = addDevice.Id; varTable.Name = "默认变量表"; varTable.Description = "默认变量表"; varTable.ProtocolType = device.ProtocolType; device.VariableTables.Add(varTable); var addVarTable = await _varTableRepository.AddAsync(varTable, db); // 添加添加变量表的菜单 var varTableMenu = new MenuBean() { Name = "默认变量表", Icon = SegoeFluentIcons.Tablet.Glyph, Type = MenuType.VariableTableMenu, ParentId = addDeviceMenuId, DataId = addVarTable.Id }; await _menuRepository.AddAsync(varTableMenu, db); } await _menuRepository.AddVarTableMenuAsync(addDevice, addDeviceMenuId, db); return _mapper.Map(addDevice); } }