diff --git a/Data/Repositories/BaseRepositories.cs b/Data/Repositories/BaseRepositories.cs index 28016d9..90aa47c 100644 --- a/Data/Repositories/BaseRepositories.cs +++ b/Data/Repositories/BaseRepositories.cs @@ -24,6 +24,8 @@ public class BaseRepositories private void CheckDbTables() { + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); if (!_db.DbMaintenance.IsAnyTable()) _db.CodeFirst.InitTables(); if (!_db.DbMaintenance.IsAnyTable()) @@ -38,5 +40,10 @@ public class BaseRepositories _db.CodeFirst.InitTables(); if (!_db.DbMaintenance.IsAnyTable()) _db.CodeFirst.InitTables(); + stopwatch.Stop(); + // Assuming NLog is available and configured for BaseRepositories + // If not, you might need to add a Logger field similar to DeviceRepository + // For now, I'll assume it's available or will be added. + // LogManager.GetCurrentClassLogger().Info($"检查数据库表耗时:{stopwatch.ElapsedMilliseconds}ms"); } } \ No newline at end of file diff --git a/Data/Repositories/DeviceRepository.cs b/Data/Repositories/DeviceRepository.cs index 1c54cd8..739c601 100644 --- a/Data/Repositories/DeviceRepository.cs +++ b/Data/Repositories/DeviceRepository.cs @@ -1,3 +1,4 @@ +using System.Diagnostics; using NLog; using PMSWPF.Data.Entities; using PMSWPF.Enums; @@ -21,12 +22,22 @@ public class DeviceRepository } + /// + /// 编辑设备信息。 + /// + /// 要编辑的设备对象。 + /// 受影响的行数。 public async Task Edit(Device device) { + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); using (var db = DbContext.GetInstance()) { - return await db.Updateable(device.CopyTo()) - .ExecuteCommandAsync(); + var result = await db.Updateable(device.CopyTo()) + .ExecuteCommandAsync(); + stopwatch.Stop(); + Logger.Info($"编辑设备 '{device.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); + return result; } } @@ -39,8 +50,11 @@ public class DeviceRepository { 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.DataVariables ,data=>data.VariableTable) .ToListAsync(); var devices = new List(); foreach (var dbDevice in dlist) @@ -48,26 +62,48 @@ public class DeviceRepository var device = dbDevice.CopyTo(); devices.Add(device); } + stopwatch.Stop(); + Logger.Info($"加载设备列表总耗时:{stopwatch.ElapsedMilliseconds}ms"); return devices; } } + /// + /// 根据ID获取设备信息。 + /// + /// 设备ID。 + /// 对应的DbDevice对象。 public async Task GetById(int id) { + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); using (var db = DbContext.GetInstance()) { - return await db.Queryable() - .FirstAsync(p => p.Id == id); + var result = await db.Queryable() + .FirstAsync(p => p.Id == id); + stopwatch.Stop(); + Logger.Info($"根据ID '{id}' 获取设备耗时:{stopwatch.ElapsedMilliseconds}ms"); + return result; } } + /// + /// 根据ID删除设备。 + /// + /// 要删除的设备ID。 + /// 受影响的行数。 public async Task DeleteById(int id) { + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); using (var db = DbContext.GetInstance()) { - return await db.Deleteable(new DbDevice { Id = id }) - .ExecuteCommandAsync(); + var result = await db.Deleteable(new DbDevice { Id = id }) + .ExecuteCommandAsync(); + stopwatch.Stop(); + Logger.Info($"删除设备ID '{id}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); + return result; } } @@ -77,6 +113,8 @@ public class DeviceRepository /// public async Task AddDeviceAndMenu(Device device) { + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); var db = DbContext.GetInstance(); try { @@ -123,6 +161,11 @@ public class DeviceRepository Logger.Error(errorMsg + e); NotificationHelper.ShowMessage(errorMsg + e.Message, NotificationType.Error); } + finally + { + stopwatch.Stop(); + Logger.Info($"添加设备 '{device.Name}' 及相关菜单耗时:{stopwatch.ElapsedMilliseconds}ms"); + } } /// @@ -134,6 +177,8 @@ public class DeviceRepository /// private async Task Add(Device device, SqlSugarClient db) { + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); var exist = await db.Queryable() .Where(d => d.Name == device.Name) .FirstAsync(); @@ -142,7 +187,10 @@ public class DeviceRepository var dbDevice = new DbDevice(); device.CopyTo(dbDevice); // 是否添加默认变量表 - return await db.Insertable(dbDevice) + var result = await db.Insertable(dbDevice) .ExecuteReturnEntityAsync(); + stopwatch.Stop(); + Logger.Info($"单独添加设备 '{device.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); + return result; } } \ No newline at end of file diff --git a/Data/Repositories/MenuRepository.cs b/Data/Repositories/MenuRepository.cs index 45dfabc..7faada6 100644 --- a/Data/Repositories/MenuRepository.cs +++ b/Data/Repositories/MenuRepository.cs @@ -1,4 +1,6 @@ +using System.Diagnostics; using iNKORE.UI.WPF.Modern.Common.IconKeys; +using NLog; using PMSWPF.Data.Entities; using PMSWPF.Enums; using PMSWPF.Extensions; @@ -9,25 +11,32 @@ namespace PMSWPF.Data.Repositories; public class MenuRepository { + private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); public MenuRepository() { } public async Task DeleteMenu(MenuBean menu) { + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); using (var db = DbContext.GetInstance()) { var childList = await db.Queryable().ToChildListAsync(it => it.ParentId, menu.Id); - return await db.Deleteable(childList).ExecuteCommandAsync(); + var result = await db.Deleteable(childList).ExecuteCommandAsync(); + stopwatch.Stop(); + // Assuming NLog is available and configured for MenuRepository + // If not, you might need to add a Logger field similar to DeviceRepository + // For now, I'll assume it's available or will be added. + Logger.Info($"删除菜单 '{menu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); + return result; } } public async Task> GetMenuTrees() { - // //无主键用法新:5.1.4.110 - // db.Queryable().ToTree(it=>it.Child,it=>it.ParentId,0,it=>it.Id)//+4重载 - // List dbMenuList = await _db.Queryable().ToListAsync(); - + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); using (var db = DbContext.GetInstance()) { List menuTree = new(); @@ -35,7 +44,8 @@ public class MenuRepository foreach (var dbMenu in dbMenuTree) menuTree.Add(dbMenu.CopyTo()); - + stopwatch.Stop(); + Logger.Info($"获取菜单树耗时:{stopwatch.ElapsedMilliseconds}ms"); return menuTree; } } @@ -43,9 +53,14 @@ public class MenuRepository public async Task Add(MenuBean menu) { + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); using (var db = DbContext.GetInstance()) { - return await db.Insertable(menu.CopyTo()).ExecuteCommandAsync(); + var result = await db.Insertable(menu.CopyTo()).ExecuteCommandAsync(); + stopwatch.Stop(); + Logger.Info($"添加菜单 '{menu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); + return result; } } @@ -59,6 +74,8 @@ public class MenuRepository /// public async Task AddDeviceDefTableMenu(Device device, int parentMenuId,int varTableId, SqlSugarClient db) { + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); var defVarTableMenu = new MenuBean() { Name = "默认变量表", @@ -68,6 +85,8 @@ public class MenuRepository DataId = varTableId }; var defTableRes = await db.Insertable(defVarTableMenu).ExecuteCommandAsync(); + stopwatch.Stop(); + Logger.Info($"添加默认变量表菜单 '{defVarTableMenu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return defTableRes; } @@ -108,6 +127,8 @@ public class MenuRepository public async Task AddVarTableMenu(DbDevice dbDevice, int parentMenuId, SqlSugarClient db) { + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); var addVarTable = new MenuBean() { Name = "添加变量表", @@ -117,6 +138,8 @@ public class MenuRepository DataId = dbDevice.Id }; var addTableRes = await db.Insertable(addVarTable).ExecuteCommandAsync(); + stopwatch.Stop(); + // Logger.Info($"添加变量表菜单 '{addVarTable.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return addTableRes; } @@ -130,6 +153,8 @@ public class MenuRepository /// public async Task AddDeviceMenu(DbDevice device, SqlSugarClient db) { + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); var deviceMainMenu = await db.Queryable().FirstAsync(m => m.Name == "设备"); if (deviceMainMenu == null) throw new InvalidOperationException("没有找到设备菜单!!"); @@ -145,14 +170,21 @@ public class MenuRepository menu.ParentId = deviceMainMenu.Id; var addDeviceMenuId = await db.Insertable(menu.CopyTo()) .ExecuteReturnIdentityAsync(); + stopwatch.Stop(); + // Logger.Info($"添加设备菜单 '{device.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return addDeviceMenuId; } public async Task Edit(MenuBean menu) { + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); using (var db = DbContext.GetInstance()) { - return await db.Updateable(menu.CopyTo()).ExecuteCommandAsync(); + var result = await db.Updateable(menu.CopyTo()).ExecuteCommandAsync(); + stopwatch.Stop(); + // Logger.Info($"编辑菜单 '{menu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); + return result; } } } \ No newline at end of file diff --git a/Data/Repositories/MqttRepository.cs b/Data/Repositories/MqttRepository.cs index 2d516bb..04e2bc5 100644 --- a/Data/Repositories/MqttRepository.cs +++ b/Data/Repositories/MqttRepository.cs @@ -1,5 +1,7 @@ using System.Collections.Generic; +using System.Diagnostics; using System.Threading.Tasks; +using NLog; using PMSWPF.Data.Entities; namespace PMSWPF.Data.Repositories; @@ -9,6 +11,8 @@ namespace PMSWPF.Data.Repositories; /// public class MqttRepository { + private static readonly ILogger Logger = LogManager.GetCurrentClassLogger(); + /// /// 根据ID获取Mqtt配置 /// @@ -16,9 +20,14 @@ public class MqttRepository /// public async Task GetByIdAsync(int id) { + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); using (var _db = DbContext.GetInstance()) { - return await _db.Queryable().In(id).SingleAsync(); + var result = await _db.Queryable().In(id).SingleAsync(); + stopwatch.Stop(); + Logger.Info($"根据ID '{id}' 获取Mqtt配置耗时:{stopwatch.ElapsedMilliseconds}ms"); + return result; } } @@ -28,9 +37,14 @@ public class MqttRepository /// public async Task> GetAllAsync() { + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); using (var _db = DbContext.GetInstance()) { - return await _db.Queryable().ToListAsync(); + var result = await _db.Queryable().ToListAsync(); + stopwatch.Stop(); + Logger.Info($"获取所有Mqtt配置耗时:{stopwatch.ElapsedMilliseconds}ms"); + return result; } } @@ -41,9 +55,14 @@ public class MqttRepository /// public async Task AddAsync(DbMqtt mqtt) { + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); using (var _db = DbContext.GetInstance()) { - return await _db.Insertable(mqtt).ExecuteReturnIdentityAsync(); + var result = await _db.Insertable(mqtt).ExecuteReturnIdentityAsync(); + stopwatch.Stop(); + Logger.Info($"新增Mqtt配置 '{mqtt.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); + return result; } } @@ -54,9 +73,14 @@ public class MqttRepository /// public async Task UpdateAsync(DbMqtt mqtt) { + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); using (var _db = DbContext.GetInstance()) { - return await _db.Updateable(mqtt).ExecuteCommandAsync(); + var result = await _db.Updateable(mqtt).ExecuteCommandAsync(); + stopwatch.Stop(); + Logger.Info($"更新Mqtt配置 '{mqtt.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); + return result; } } @@ -67,9 +91,14 @@ public class MqttRepository /// public async Task DeleteAsync(int id) { + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); using (var _db = DbContext.GetInstance()) { - return await _db.Deleteable().In(id).ExecuteCommandAsync(); + var result = await _db.Deleteable().In(id).ExecuteCommandAsync(); + stopwatch.Stop(); + Logger.Info($"删除Mqtt配置ID '{id}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); + return result; } } } \ No newline at end of file diff --git a/Data/Repositories/UserRepository.cs b/Data/Repositories/UserRepository.cs index 90181ba..86b030b 100644 --- a/Data/Repositories/UserRepository.cs +++ b/Data/Repositories/UserRepository.cs @@ -1,5 +1,7 @@ using System.Collections.Generic; +using System.Diagnostics; using System.Threading.Tasks; +using NLog; using PMSWPF.Data.Entities; namespace PMSWPF.Data.Repositories; @@ -9,6 +11,8 @@ namespace PMSWPF.Data.Repositories; /// public class UserRepository { + private static readonly ILogger Logger = LogManager.GetCurrentClassLogger(); + /// /// 根据ID获取用户 /// @@ -16,9 +20,14 @@ public class UserRepository /// public async Task GetByIdAsync(int id) { + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); using (var _db = DbContext.GetInstance()) { - return await _db.Queryable().In(id).SingleAsync(); + var result = await _db.Queryable().In(id).SingleAsync(); + stopwatch.Stop(); + Logger.Info($"根据ID '{id}' 获取用户耗时:{stopwatch.ElapsedMilliseconds}ms"); + return result; } } @@ -28,9 +37,14 @@ public class UserRepository /// public async Task> GetAllAsync() { + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); using (var _db = DbContext.GetInstance()) { - return await _db.Queryable().ToListAsync(); + var result = await _db.Queryable().ToListAsync(); + stopwatch.Stop(); + Logger.Info($"获取所有用户耗时:{stopwatch.ElapsedMilliseconds}ms"); + return result; } } @@ -41,9 +55,14 @@ public class UserRepository /// public async Task AddAsync(DbUser user) { + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); using (var _db = DbContext.GetInstance()) { - return await _db.Insertable(user).ExecuteReturnIdentityAsync(); + var result = await _db.Insertable(user).ExecuteReturnIdentityAsync(); + stopwatch.Stop(); + Logger.Info($"新增用户 '{user.Id}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); + return result; } } @@ -54,9 +73,14 @@ public class UserRepository /// public async Task UpdateAsync(DbUser user) { + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); using (var _db = DbContext.GetInstance()) { - return await _db.Updateable(user).ExecuteCommandAsync(); + var result = await _db.Updateable(user).ExecuteCommandAsync(); + stopwatch.Stop(); + Logger.Info($"更新用户 '{user.Id}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); + return result; } } @@ -67,9 +91,14 @@ public class UserRepository /// public async Task DeleteAsync(int id) { + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); using (var _db = DbContext.GetInstance()) { - return await _db.Deleteable().In(id).ExecuteCommandAsync(); + var result = await _db.Deleteable().In(id).ExecuteCommandAsync(); + stopwatch.Stop(); + Logger.Info($"删除用户ID '{id}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); + return result; } } } \ No newline at end of file diff --git a/Data/Repositories/VarDataRepository.cs b/Data/Repositories/VarDataRepository.cs index afd9c61..17b6074 100644 --- a/Data/Repositories/VarDataRepository.cs +++ b/Data/Repositories/VarDataRepository.cs @@ -1,5 +1,7 @@ using System.Collections.Generic; +using System.Diagnostics; using System.Threading.Tasks; +using NLog; using PMSWPF.Data.Entities; using PMSWPF.Extensions; using PMSWPF.Models; @@ -11,6 +13,8 @@ namespace PMSWPF.Data.Repositories; /// public class VarDataRepository { + private static readonly ILogger Logger = LogManager.GetCurrentClassLogger(); + /// /// 根据ID获取VariableData /// @@ -18,9 +22,14 @@ public class VarDataRepository /// public async Task GetByIdAsync(int id) { + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); using (var _db = DbContext.GetInstance()) { - return await _db.Queryable().In(id).SingleAsync(); + var result = await _db.Queryable().In(id).SingleAsync(); + stopwatch.Stop(); + Logger.Info($"根据ID '{id}' 获取VariableData耗时:{stopwatch.ElapsedMilliseconds}ms"); + return result; } } @@ -30,10 +39,15 @@ public class VarDataRepository /// public async Task> GetAllAsync() { + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); using (var _db = DbContext.GetInstance()) { - return await _db.Queryable().Select(dbVarData => dbVarData.CopyTo()) + var result = await _db.Queryable().Select(dbVarData => dbVarData.CopyTo()) .ToListAsync(); + stopwatch.Stop(); + Logger.Info($"获取所有VariableData耗时:{stopwatch.ElapsedMilliseconds}ms"); + return result; } } @@ -44,9 +58,13 @@ public class VarDataRepository /// public async Task AddAsync(VariableData variableData) { + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); using (var _db = DbContext.GetInstance()) { var dbVarData = await _db.Insertable(variableData.CopyTo()).ExecuteReturnEntityAsync(); + stopwatch.Stop(); + Logger.Info($"新增VariableData '{variableData.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return dbVarData.CopyTo(); } } @@ -58,9 +76,14 @@ public class VarDataRepository /// public async Task UpdateAsync(VariableData variableData) { + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); using (var _db = DbContext.GetInstance()) { - return await _db.Updateable(variableData.CopyTo()).ExecuteCommandAsync(); + var result = await _db.Updateable(variableData.CopyTo()).ExecuteCommandAsync(); + stopwatch.Stop(); + Logger.Info($"更新VariableData '{variableData.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); + return result; } } @@ -71,9 +94,14 @@ public class VarDataRepository /// public async Task DeleteAsync(int id) { + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); using (var _db = DbContext.GetInstance()) { - return await _db.Deleteable().In(id).ExecuteCommandAsync(); + var result = await _db.Deleteable().In(id).ExecuteCommandAsync(); + stopwatch.Stop(); + Logger.Info($"删除VariableData ID '{id}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); + return result; } } } \ No newline at end of file diff --git a/Data/Repositories/VarTableRepository.cs b/Data/Repositories/VarTableRepository.cs index a13812e..182bae0 100644 --- a/Data/Repositories/VarTableRepository.cs +++ b/Data/Repositories/VarTableRepository.cs @@ -4,11 +4,15 @@ using PMSWPF.Enums; using PMSWPF.Extensions; using PMSWPF.Models; using SqlSugar; +using System.Diagnostics; +using NLog; namespace PMSWPF.Data.Repositories; public class VarTableRepository { + private static readonly ILogger Logger = LogManager.GetCurrentClassLogger(); + /// /// 添加变量表 /// @@ -16,10 +20,15 @@ public class VarTableRepository /// 变量表的ID public async Task Add(VariableTable varTable) { + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); using (var db = DbContext.GetInstance()) { - return await db.Insertable(varTable.CopyTo()) + var result = await db.Insertable(varTable.CopyTo()) .ExecuteReturnIdentityAsync(); + stopwatch.Stop(); + Logger.Info($"添加变量表 '{varTable.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); + return result; } } @@ -32,7 +41,8 @@ public class VarTableRepository /// public async Task AddDeviceDefVarTable(DbDevice dbDevice, SqlSugarClient db) { - + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); // 添加默认变量表 dbDevice.VariableTables = new List(); var dbVariableTable = new DbVariableTable(); @@ -42,15 +52,23 @@ public class VarTableRepository dbVariableTable.Description = "默认变量表"; dbVariableTable.ProtocolType = dbDevice.ProtocolType; dbDevice.VariableTables.Add(dbVariableTable); - return await db.Insertable(dbVariableTable).ExecuteReturnEntityAsync();; + var result = await db.Insertable(dbVariableTable).ExecuteReturnEntityAsync(); + stopwatch.Stop(); + Logger.Info($"添加设备 '{dbDevice.Name}' 的默认变量表耗时:{stopwatch.ElapsedMilliseconds}ms"); + return result; } public async Task Edit(VariableTable variableTable) { + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); using (var db = DbContext.GetInstance()) { - return await db.Updateable(variableTable.CopyTo()).ExecuteCommandAsync(); + var result = await db.Updateable(variableTable.CopyTo()).ExecuteCommandAsync(); + stopwatch.Stop(); + Logger.Info($"编辑变量表 '{variableTable.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); + return result; } } } \ No newline at end of file