using iNKORE.UI.WPF.Modern.Common.IconKeys; using PMSWPF.Data.Entities; using PMSWPF.Enums; using PMSWPF.Extensions; using PMSWPF.Helper; using PMSWPF.Models; using SqlSugar; using System.Diagnostics; using AutoMapper; namespace PMSWPF.Data.Repositories; public class VarTableRepository { private readonly IMapper _mapper; public VarTableRepository(IMapper mapper) { _mapper = mapper; } /// /// 添加变量表 /// /// /// 变量表的ID public async Task Add(VariableTable varTable) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); using (var db = DbContext.GetInstance()) { var addVarTable = await Add(varTable, db); stopwatch.Stop(); NlogHelper.Info($"添加变量表 '{varTable.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return addVarTable; } } /// /// 添加变量表支持事务 /// /// /// /// public async Task Add(VariableTable variableTable, SqlSugarClient db) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); var addVarTabel = await db.Insertable(_mapper.Map(variableTable)) .ExecuteReturnEntityAsync(); stopwatch.Stop(); NlogHelper.Info($"添加设备 '{addVarTabel.Name}' 的默认变量表耗时:{stopwatch.ElapsedMilliseconds}ms"); return _mapper.Map(addVarTabel); } /// /// 编辑变量表 /// /// /// public async Task Edit(VariableTable variableTable) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); using (var db = DbContext.GetInstance()) { var result = await Edit(variableTable, db); stopwatch.Stop(); NlogHelper.Info($"编辑变量表 '{variableTable.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } } /// /// 编辑变量表,支持事务 /// /// /// public async Task Edit(VariableTable variableTable, SqlSugarClient db) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); var result = await db.Updateable(_mapper.Map(variableTable)) .ExecuteCommandAsync(); stopwatch.Stop(); NlogHelper.Info($"编辑变量表 '{variableTable.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } /// /// 删除变量表 /// /// /// public async Task Delete(VariableTable variableTable) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); using (var db = DbContext.GetInstance()) { var result = await Delete(variableTable, db); stopwatch.Stop(); NlogHelper.Info($"删除变量表 '{variableTable.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } } /// /// 删除变量表支持事务 /// /// /// public async Task Delete(VariableTable varTable, SqlSugarClient db) { if (varTable == null ) return 0; Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); // 转换对象 var res= await db.Deleteable(_mapper.Map(varTable)) .ExecuteCommandAsync(); stopwatch.Stop(); NlogHelper.Info($"删除变量表 '{varTable.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); return res; } /// /// 删除变量表支持事务 /// /// /// public async Task Delete(List deviceVariableTables, SqlSugarClient db) { if (deviceVariableTables == null || deviceVariableTables.Count == 0) return; // 转换对象 var dbList = deviceVariableTables.Select(v => _mapper.Map(v)) .ToList(); await db.Deleteable(dbList) .ExecuteCommandAsync(); } }