临时提交

This commit is contained in:
2025-07-19 09:25:01 +08:00
parent e1a89e7c70
commit 01fe2e14ef
52 changed files with 499 additions and 370 deletions

View File

@@ -21,7 +21,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DMS\DMS.csproj" />
<ProjectReference Include="..\DMS.Core\DMS.Core.csproj" />
</ItemGroup>
</Project>

View File

@@ -1,25 +1,46 @@
using DMS.Config;
using DMS.Config;
using DMS.Core.Interfaces;
using SqlSugar;
using System;
using System.Threading.Tasks;
namespace DMS.Infrastructure;
namespace DMS.Infrastructure.Data;
public class DbContext
public class SqlSugarDbContext : IUnitOfWork
{
public static SqlSugarClient GetInstance()
private readonly SqlSugarClient _db;
public SqlSugarDbContext(ConnectionSettings settings)
{
var settings = ConnectionSettings.Load();
var connectionString = settings.ToConnectionString();
var dbType = (SqlSugar.DbType)Enum.Parse(typeof(SqlSugar.DbType), settings.DbType);
var _db = new SqlSugarClient(new ConnectionConfig
_db = new SqlSugarClient(new ConnectionConfig
{
ConnectionString = connectionString,
DbType = dbType, // 根据实际数据库类型修改如DbType.MySql等
DbType = dbType,
IsAutoCloseConnection = true,
InitKeyType = InitKeyType.Attribute
});
}
public SqlSugarClient GetSqlSugarClient()
{
return _db;
}
public async Task BeginTranAsync()
{
await _db.BeginTranAsync();
}
public async Task CommitTranAsync()
{
await _db.CommitTranAsync();
}
public async Task RollbackTranAsync()
{
await _db.RollbackTranAsync();
}
}

View File

@@ -2,8 +2,7 @@ using SqlSugar;
using SqlSugar.DbConvert;
using S7.Net;
using DMS.Core.Enums;
namespace DMS.Infrastructure.Entities;
using DMS.Infrastructure.Entities;
/// <summary>
/// 表示数据库中的设备实体。
@@ -76,7 +75,7 @@ public class DbDevice
/// 设备的通信协议类型。
/// </summary>
[SugarColumn(ColumnDataType = "varchar(20)", SqlParameterDbType = typeof(EnumToStringConvert))]
public Enums_ProtocolType ProtocolType { get; set; }
public ProtocolType ProtocolType { get; set; }
/// <summary>
/// OPC UA Endpoint URL。

View File

@@ -1,9 +1,9 @@
using System;
using AutoMapper;
using DMS.Infrastructure.Entities;
using DMS.Models;
using DMS.Core.Models;
namespace DMS.Profiles;
namespace DMS.Infrastructure.Profiles;
/// <summary>
/// AutoMapper 的配置类,用于定义对象之间的映射规则。

View File

@@ -2,28 +2,30 @@ 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 DMS.Core.Helper;
using DMS.Core.Models;
using DMS.Infrastructure.Data;
using SqlSugar;
namespace DMS.Infrastructure.Repositories;
public class DeviceRepository
public class DeviceRepository : IDeviceRepository
{
private readonly IMapper _mapper;
private readonly MenuRepository _menuRepository;
private readonly VarTableRepository _varTableRepository;
private readonly IMenuRepository _menuRepository;
private readonly IVarTableRepository _varTableRepository;
private readonly IUnitOfWork _unitOfWork;
public DeviceRepository(IMapper mapper,MenuRepository menuRepository,VarTableRepository varTableRepository)
public DeviceRepository(IMapper mapper, IMenuRepository menuRepository, IVarTableRepository varTableRepository, IUnitOfWork unitOfWork)
{
_mapper = mapper;
_menuRepository = menuRepository;
_varTableRepository = varTableRepository;
_unitOfWork = unitOfWork;
}
/// <summary>
/// 编辑设备信息。
/// </summary>
@@ -33,25 +35,7 @@ public class DeviceRepository
{
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;
}
}
/// <summary>
/// 编辑设备信息。支持事务
/// </summary>
/// <param name="device">要编辑的设备对象。</param>
/// <returns>受影响的行数。</returns>
public async Task<int> UpdateAsync(Device device, SqlSugarClient db)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
var db = _unitOfWork.GetInstance();
var result = await db.Updateable<DbDevice>(_mapper.Map<DbDevice>(device))
.ExecuteCommandAsync();
stopwatch.Stop();
@@ -59,6 +43,8 @@ public class DeviceRepository
return result;
}
/// <summary>
/// 获取设备列表
@@ -73,13 +59,13 @@ public class DeviceRepository
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 )
.Includes(d => d.VariableTables, vt => vt.Variables, v => v.VariableMqtts)
.ToListAsync();
stopwatch.Stop();
NlogHelper.Info($"加载设备列表总耗时:{stopwatch.ElapsedMilliseconds}ms");
var devices= _mapper.Map<List<Device>>(dlist);
var devices = _mapper.Map<List<Device>>(dlist);
return devices;
}
}
@@ -111,59 +97,23 @@ public class DeviceRepository
/// <returns>受影响的行数。</returns>
public async Task<int> DeleteAsync(Device device, List<MenuBean> 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;
}
}
/// <summary>
/// 删除设备。
/// </summary>
/// <param name="device"></param>
/// <param name="menus"></param>
/// <param name="db"></param>
/// <param name="id">要删除的设备ID。</param>
/// <returns>受影响的行数。</returns>
public async Task<int> DeleteAsync(Device device, List<MenuBean> menus, SqlSugarClient db)
{
var db = _unitOfWork.GetInstance();
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
var result = await db.Deleteable<DbDevice>(new DbDevice { Id = device.Id })
.ExecuteCommandAsync();
// 删除变量表
await _varTableRepository.DeleteAsync(device.VariableTables, db);
await _varTableRepository.DeleteAsync(device.VariableTables);
// 删除菜单
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;
}
/// <summary>
/// 添加设备
/// </summary>
@@ -172,86 +122,25 @@ public class DeviceRepository
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
var db = DbContext.GetInstance();
try
{
// 开启事务
await db.BeginTranAsync();
//查询设备的名字是否存在
var db = _unitOfWork.GetInstance();
var exist = await db.Queryable<DbDevice>()
.Where(d => d.Name == device.Name)
.FirstAsync();
if (exist != null)
throw new InvalidOperationException("设备名称已经存在。");
//查询设备的名字是否存在
var exist = await db.Queryable<DbDevice>()
.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");
}
}
/// <summary>
/// 单独添加设备,不包括菜单和变量表
/// </summary>
/// <param name="device"></param>
/// <param name="db"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
private async Task<Device> AddAsync(Device device, SqlSugarClient db)
{
;
// 添加设备
// 2. 将设备添加到数据库
var addDevice = await db.Insertable<DbDevice>(_mapper.Map<DbDevice>(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);
}
var addDeviceMenuId = await _menuRepository.AddAsync(addDevice);
await _menuRepository.AddVarTableMenuAsync(addDevice, addDeviceMenuId, db);
return _mapper.Map<Device>(addDevice);
stopwatch.Stop();
NlogHelper.Info($"添加设备 '{device.Name}' 及相关菜单耗时:{stopwatch.ElapsedMilliseconds}ms");
}
}

View File

@@ -1,16 +1,15 @@
using System.Diagnostics;
using iNKORE.UI.WPF.Modern.Common.IconKeys;
using DMS.Extensions;
using SqlSugar;
using AutoMapper;
using DMS.Infrastructure.Entities;
using DMS.Core.Enums;
using DMS.Helper;
using DMS.Models;
using DMS.Core.Helper;
using DMS.Core.Models;
using DMS.Infrastructure.Data;
namespace DMS.Infrastructure.Repositories;
public class MenuRepository
public class MenuRepository : IMenuRepository
{
private readonly IMapper _mapper;
@@ -95,7 +94,7 @@ public class MenuRepository
var addVarTable = new MenuBean()
{
Name = "添加变量表",
Icon = SegoeFluentIcons.Add.Glyph,
// Icon = SegoeFluentIcons.Add.Glyph,
Type = MenuType.AddVariableTableMenu,
ParentId = parentMenuId,
DataId = dbDevice.Id
@@ -130,7 +129,7 @@ public class MenuRepository
Name = device.Name,
Type = MenuType.DeviceMenu,
DataId = device.Id,
Icon = SegoeFluentIcons.Devices4.Glyph,
// Icon = SegoeFluentIcons.Devices4.Glyph,
};
menu.ParentId = deviceMainMenu.Id;
var addDeviceMenuId = await db.Insertable<DbMenu>(_mapper.Map<DbMenu>(menu))

View File

@@ -2,16 +2,16 @@ 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.Core.Helper;
using DMS.Core.Models;
using DMS.Infrastructure.Data;
namespace DMS.Infrastructure.Repositories;
/// <summary>
/// Mqtt仓储类用于操作DbMqtt实体
/// </summary>
public class MqttRepository
public class MqttRepository : IMqttRepository
{
private readonly MenuRepository _menuRepository;
private readonly IMapper _mapper;
@@ -83,7 +83,7 @@ public class MqttRepository
var menu = new MenuBean()
{
Name = mqtt.Name,
Icon = SegoeFluentIcons.Wifi.Glyph,
// Icon = SegoeFluentIcons.Wifi.Glyph,
Type = MenuType.MqttMenu,
DataId = result,
ParentId = mqttMenu.Id,

View File

@@ -2,14 +2,15 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using DMS.Infrastructure.Entities;
using DMS.Helper;
using DMS.Core.Helper;
using DMS.Infrastructure.Data;
namespace DMS.Infrastructure.Repositories;
/// <summary>
/// 用户仓储类用于操作DbUser实体
/// </summary>
public class UserRepository
public class UserRepository : IUserRepository
{
/// <summary>

View File

@@ -1,20 +1,20 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using DMS.Extensions;
using SqlSugar;
using AutoMapper;
using Dm.util;
using DMS.Infrastructure.Entities;
using DMS.Helper;
using DMS.Models;
using DMS.Core.Helper;
using DMS.Core.Models;
using DMS.Infrastructure.Data;
namespace DMS.Infrastructure.Repositories;
/// <summary>
/// VariableData仓储类用于操作DbVariableData实体
/// </summary>
public class VarDataRepository
public class VarDataRepository : IVarDataRepository
{
private readonly IMapper _mapper;
@@ -359,12 +359,12 @@ public class VarDataRepository
if (existingAliasesDict.TryGetValue(key, out var existingAlias))
{
// 如果存在但别名不同,则准备更新
if (existingAlias.MqttAlias != variableMqtt.MqttAlias)
{
existingAlias.MqttAlias = variableMqtt.MqttAlias;
existingAlias.UpdateTime = DateTime.Now;
toUpdate.Add(existingAlias);
}
// if (existingAlias.MqttAlias != variableMqtt.MqttAlias)
// {
// existingAlias.MqttAlias = variableMqtt.MqttAlias;
// existingAlias.UpdateTime = DateTime.Now;
// toUpdate.Add(existingAlias);
// }
}
else
{
@@ -373,7 +373,7 @@ public class VarDataRepository
{
VariableId = variableMqtt.Variable.Id,
MqttId = variableMqtt.Mqtt.Id,
MqttAlias = variableMqtt.MqttAlias,
// MqttAlias = variableMqtt.MqttAlias,
CreateTime = DateTime.Now,
UpdateTime = DateTime.Now
});

View File

@@ -1,16 +1,14 @@
using iNKORE.UI.WPF.Modern.Common.IconKeys;
using DMS.Core.Enums;
using DMS.Extensions;
using SqlSugar;
using System.Diagnostics;
using AutoMapper;
using DMS.Infrastructure.Entities;
using DMS.Helper;
using DMS.Models;
using DMS.Core.Helper;
using DMS.Core.Models;
using DMS.Infrastructure.Data;
namespace DMS.Infrastructure.Repositories;
public class VarTableRepository
public class VarTableRepository : IVarTableRepository
{
private readonly IMapper _mapper;

View File

@@ -1,6 +1,7 @@
using SqlSugar;
using System.Collections.Generic;
using System.Threading.Tasks;
using DMS.Infrastructure.Data;
using DMS.Infrastructure.Entities;
namespace DMS.Infrastructure.Repositories;
@@ -8,7 +9,7 @@ namespace DMS.Infrastructure.Repositories;
/// <summary>
/// 变量与MQTT服务器别名关联的数据仓库。
/// </summary>
public class VariableMqttAliasRepository
public class VariableMqttAliasRepository : IVariableMqttAliasRepository
{
/// <summary>
/// 根据变量ID和MQTT服务器ID获取别名。

View File

@@ -0,0 +1,82 @@
using DMS.Config;
using DMS.Core.Enums;
using DMS.Infrastructure.Data;
using DMS.Infrastructure.Entities;
using SqlSugar;
using System;
using System.Threading.Tasks;
namespace DMS.Infrastructure.Services
{
public class DatabaseInitializerService : DMS.Core.Interfaces.IDatabaseService
{
private readonly SqlSugarClient _db;
public DatabaseInitializerService(SqlSugarDbContext dbContext)
{
_db = dbContext.GetSqlSugarClient();
}
public void InitializeDataBase()
{
_db.DbMaintenance.CreateDatabase();
_db.CodeFirst.InitTables<DbNlog>();
_db.CodeFirst.InitTables<DbDevice>();
_db.CodeFirst.InitTables<DbVariableTable>();
_db.CodeFirst.InitTables<DbVariable>();
_db.CodeFirst.InitTables<DbVariableHistory>();
_db.CodeFirst.InitTables<DbUser>();
_db.CodeFirst.InitTables<DbMqtt>();
_db.CodeFirst.InitTables<DbVariableMqtt>();
_db.CodeFirst.InitTables<DbMenu>();
}
public async Task InitializeMenu()
{
var homeMenu = new DbMenu()
{ Name = "主页", Type = MenuType.MainMenu, Icon = "Home", ParentId = 0 }; // Icon needs to be adjusted if it's not a string
var deviceMenu = new DbMenu()
{
Name = "设备", Type = MenuType.MainMenu, Icon = "Devices3",
ParentId = 0
};
var dataTransfromMenu = new DbMenu()
{
Name = "数据转换", Type = MenuType.MainMenu,
Icon = "ChromeSwitch", ParentId = 0
};
var mqttMenu = new DbMenu()
{
Name = "Mqtt服务器", Type = MenuType.MainMenu, Icon = "Cloud",
ParentId = 0
};
var settingMenu = new DbMenu()
{
Name = "设置", Type = MenuType.MainMenu, Icon = "Settings",
ParentId = 0
};
var aboutMenu = new DbMenu()
{ Name = "关于", Type = MenuType.MainMenu, Icon = "Info", ParentId = 0 };
await CheckMainMenuExist(homeMenu);
await CheckMainMenuExist(deviceMenu);
await CheckMainMenuExist(dataTransfromMenu);
await CheckMainMenuExist(mqttMenu);
await CheckMainMenuExist(settingMenu);
await CheckMainMenuExist(aboutMenu);
}
private async Task CheckMainMenuExist(DbMenu menu)
{
var homeMenuExist = await _db.Queryable<DbMenu>()
.FirstAsync(dm => dm.Name == menu.Name);
if (homeMenuExist == null)
{
await _db.Insertable<DbMenu>(menu)
.ExecuteCommandAsync();
}
}
}
}