基本修改完毕开始进行单元测试
This commit is contained in:
@@ -8,81 +8,75 @@ using System.Linq.Expressions;
|
||||
|
||||
namespace DMS.Infrastructure.Repositories;
|
||||
|
||||
public abstract class BaseRepository<TEntity, TModel>
|
||||
public abstract class BaseRepository<TEntity>
|
||||
where TEntity : class, new()
|
||||
where TModel : class, new()
|
||||
{
|
||||
protected readonly IMapper _mapper;
|
||||
private readonly SqlSugarDbContext dbContext;
|
||||
private readonly ITransaction _transaction;
|
||||
|
||||
protected SqlSugarClient Db => dbContext.GetInstance();
|
||||
protected SqlSugarClient Db => _transaction.GetInstance();
|
||||
|
||||
protected BaseRepository(IMapper mapper, ITransaction transaction)
|
||||
protected BaseRepository(ITransaction transaction)
|
||||
{
|
||||
_mapper = mapper;
|
||||
this.dbContext = dbContext;
|
||||
this._transaction = transaction;
|
||||
}
|
||||
|
||||
public virtual async Task<int> AddAsync(TModel model)
|
||||
public virtual async Task<TEntity> AddAsync(TEntity entity)
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
var entity = _mapper.Map<TEntity>(model);
|
||||
var result = await Db.Insertable(entity).ExecuteCommandAsync();
|
||||
var result = await Db.Insertable(entity).ExecuteReturnEntityAsync();
|
||||
stopwatch.Stop();
|
||||
NlogHelper.Info($"Add {typeof(TModel).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
NlogHelper.Info($"Add {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return result;
|
||||
}
|
||||
|
||||
public virtual async Task<int> UpdateAsync(TModel model)
|
||||
public virtual async Task<int> UpdateAsync(TEntity entity)
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
var entity = _mapper.Map<TEntity>(model);
|
||||
var result = await Db.Updateable(entity).ExecuteCommandAsync();
|
||||
stopwatch.Stop();
|
||||
NlogHelper.Info($"Update {typeof(TModel).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
NlogHelper.Info($"Update {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return result;
|
||||
}
|
||||
|
||||
public virtual async Task<int> DeleteAsync(TModel model)
|
||||
public virtual async Task<int> DeleteAsync(TEntity entity)
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
var entity = _mapper.Map<TEntity>(model);
|
||||
var result = await Db.Deleteable(entity).ExecuteCommandAsync();
|
||||
stopwatch.Stop();
|
||||
NlogHelper.Info($"Delete {typeof(TModel).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
NlogHelper.Info($"Delete {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return result;
|
||||
}
|
||||
|
||||
public virtual async Task<List<TModel>> GetAllAsync()
|
||||
public virtual async Task<List<TEntity>> GetAllAsync()
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
var entities = await Db.Queryable<TEntity>().ToListAsync();
|
||||
stopwatch.Stop();
|
||||
NlogHelper.Info($"GetAll {typeof(TModel).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return _mapper.Map<List<TModel>>(entities);
|
||||
NlogHelper.Info($"GetAll {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return entities;
|
||||
}
|
||||
|
||||
public virtual async Task<TModel> GetByIdAsync(int id)
|
||||
public virtual async Task<TEntity> GetByIdAsync(int id)
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
var entity = await Db.Queryable<TEntity>().In(id).FirstAsync();
|
||||
stopwatch.Stop();
|
||||
NlogHelper.Info($"GetById {typeof(TModel).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return _mapper.Map<TModel>(entity);
|
||||
NlogHelper.Info($"GetById {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return entity;
|
||||
}
|
||||
|
||||
public virtual async Task<TModel> GetByConditionAsync(Expression<Func<TEntity, bool>> expression)
|
||||
public virtual async Task<TEntity> GetByConditionAsync(Expression<Func<TEntity, bool>> expression)
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
var entity = await Db.Queryable<TEntity>().FirstAsync(expression);
|
||||
stopwatch.Stop();
|
||||
NlogHelper.Info($"GetByCondition {typeof(TModel).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return _mapper.Map<TModel>(entity);
|
||||
NlogHelper.Info($"GetByCondition {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,21 +10,17 @@ using DMS.Infrastructure.Interfaces;
|
||||
|
||||
namespace DMS.Infrastructure.Repositories;
|
||||
|
||||
public class DeviceRepository : BaseRepository<DbDevice, Device>, IDeviceRepository
|
||||
public class DeviceRepository : BaseRepository<DbDevice>
|
||||
{
|
||||
private readonly IMenuRepository _menuRepository;
|
||||
private readonly IVarTableRepository _varTableRepository;
|
||||
|
||||
public DeviceRepository(IMapper mapper, IMenuRepository menuRepository, IVarTableRepository varTableRepository, ITransaction transaction)
|
||||
: base(mapper, transaction)
|
||||
public DeviceRepository(ITransaction transaction)
|
||||
: base(transaction)
|
||||
{
|
||||
_menuRepository = menuRepository;
|
||||
_varTableRepository = varTableRepository;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public override async Task<List<Device>> GetAllAsync()
|
||||
public override async Task<List<DbDevice>> GetAllAsync()
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
@@ -36,46 +32,10 @@ public class DeviceRepository : BaseRepository<DbDevice, Device>, IDeviceReposit
|
||||
|
||||
stopwatch.Stop();
|
||||
NlogHelper.Info($"加载设备列表总耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
var devices = _mapper.Map<List<Device>>(dlist);
|
||||
return devices;
|
||||
return dlist;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public async Task<int> DeleteAsync(Device device, List<MenuBean> menus)
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
var result = await Db.Deleteable<DbDevice>(new DbDevice { Id = device.Id })
|
||||
.ExecuteCommandAsync();
|
||||
// 删除变量表
|
||||
//await _varTableRepository.DeleteAsync(device.VariableTables);
|
||||
|
||||
stopwatch.Stop();
|
||||
NlogHelper.Info($"删除设备:{device.Name},耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task AddAsync(Device device)
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
|
||||
//查询设备的名字是否存在
|
||||
var exist = await Db.Queryable<DbDevice>()
|
||||
.Where(d => d.Name == device.Name)
|
||||
.FirstAsync();
|
||||
if (exist != null)
|
||||
throw new InvalidOperationException("设备名称已经存在。");
|
||||
|
||||
// 2. 将设备添加到数据库
|
||||
var addDevice = await Db.Insertable<DbDevice>(_mapper.Map<DbDevice>(device))
|
||||
.ExecuteReturnEntityAsync();
|
||||
|
||||
// 4. 为新设备添加菜单
|
||||
//var addDeviceMenuId = await _menuRepository.AddAsync(addDevice);
|
||||
|
||||
stopwatch.Stop();
|
||||
NlogHelper.Info($"添加设备 '{device.Name}' 及相关菜单耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -10,56 +10,36 @@ using DMS.Infrastructure.Interfaces;
|
||||
|
||||
namespace DMS.Infrastructure.Repositories;
|
||||
|
||||
public class MenuRepository : BaseRepository<DbMenu, MenuBean>
|
||||
public class MenuRepository : BaseRepository<DbMenu>
|
||||
{
|
||||
public MenuRepository(IMapper mapper, ITransaction transaction)
|
||||
: base(mapper, transaction)
|
||||
public MenuRepository(SqlSugarDbContext dbContext)
|
||||
: base(dbContext)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<int> DeleteAsync(MenuBean menu)
|
||||
public override async Task<int> DeleteAsync(DbMenu menu)
|
||||
{
|
||||
return await DeleteAsync(menu, Db);
|
||||
return await base.DeleteAsync(menu);
|
||||
}
|
||||
|
||||
public async Task<int> DeleteAsync(MenuBean menu, SqlSugarClient db)
|
||||
{
|
||||
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
var childList = await db.Queryable<DbMenu>()
|
||||
.ToChildListAsync(it => it.ParentId, menu.Id);
|
||||
var result = await db.Deleteable<DbMenu>(childList)
|
||||
.ExecuteCommandAsync();
|
||||
stopwatch.Stop();
|
||||
NlogHelper.Info($"删除菜单 '{menu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
public async Task<List<MenuBean>> GetMenuTreesAsync()
|
||||
public async Task<List<DbMenu>> GetMenuTreesAsync()
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
List<MenuBean> menuTree = new();
|
||||
var dbMenuTree = await Db.Queryable<DbMenu>()
|
||||
.ToTreeAsync(dm => dm.Items, dm => dm.ParentId, 0);
|
||||
|
||||
foreach (var dbMenu in dbMenuTree)
|
||||
menuTree.Add(_mapper.Map<MenuBean>(dbMenu));
|
||||
stopwatch.Stop();
|
||||
NlogHelper.Info($"获取菜单树耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return menuTree;
|
||||
return dbMenuTree;
|
||||
}
|
||||
|
||||
|
||||
public async Task<int> AddAsync(MenuBean menu)
|
||||
public override async Task<DbMenu> AddAsync(DbMenu menu)
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
var result = await AddAsync(menu, Db);
|
||||
stopwatch.Stop();
|
||||
NlogHelper.Info($"添加菜单 '{menu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return result;
|
||||
return await base.AddAsync(menu);
|
||||
}
|
||||
|
||||
|
||||
@@ -69,36 +49,10 @@ public class MenuRepository : BaseRepository<DbMenu, MenuBean>
|
||||
/// <param name="menu"></param>
|
||||
/// <param name="db"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<int> AddAsync(MenuBean menu, SqlSugarClient db)
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
var result = await db.Insertable<DbMenu>(_mapper.Map<DbMenu>(menu))
|
||||
.ExecuteCommandAsync();
|
||||
stopwatch.Stop();
|
||||
NlogHelper.Info($"添加菜单 '{menu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public async Task<int> AddVarTableMenuAsync(DbDevice dbDevice, int parentMenuId, SqlSugarClient db)
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
var addVarTable = new MenuBean()
|
||||
{
|
||||
Name = "添加变量表",
|
||||
// Icon = SegoeFluentIcons.Add.Glyph,
|
||||
Type = MenuType.AddVariableTableMenu,
|
||||
ParentId = parentMenuId,
|
||||
DataId = dbDevice.Id
|
||||
};
|
||||
var addTableRes = await db.Insertable<DbMenu>(addVarTable)
|
||||
.ExecuteCommandAsync();
|
||||
stopwatch.Stop();
|
||||
// NlogHelper.Info($"添加变量表菜单 '{addVarTable.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return addTableRes;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
@@ -108,44 +62,16 @@ public class MenuRepository : BaseRepository<DbMenu, MenuBean>
|
||||
/// <param name="db"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="InvalidOperationException"></exception>
|
||||
public async Task<int> AddAsync(DbDevice device, SqlSugarClient db)
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
var deviceMainMenu = await db.Queryable<DbMenu>()
|
||||
.FirstAsync(m => m.Name == "设备");
|
||||
if (deviceMainMenu == null)
|
||||
throw new InvalidOperationException("没有找到设备菜单!!");
|
||||
|
||||
// 添加菜单项
|
||||
MenuBean menu = new MenuBean()
|
||||
{
|
||||
Name = device.Name,
|
||||
Type = MenuType.DeviceMenu,
|
||||
DataId = device.Id,
|
||||
// Icon = SegoeFluentIcons.Devices4.Glyph,
|
||||
};
|
||||
menu.ParentId = deviceMainMenu.Id;
|
||||
var addDeviceMenuId = await db.Insertable<DbMenu>(_mapper.Map<DbMenu>(menu))
|
||||
.ExecuteReturnIdentityAsync();
|
||||
stopwatch.Stop();
|
||||
NlogHelper.Info($"添加设备菜单 '{device.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return addDeviceMenuId;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 编辑菜单
|
||||
/// </summary>
|
||||
/// <param name="menu"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<int> UpdateAsync(MenuBean menu)
|
||||
public override async Task<int> UpdateAsync(DbMenu menu)
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
var result = await UpdateAsync(menu, Db);
|
||||
stopwatch.Stop();
|
||||
NlogHelper.Info($"编辑菜单 '{menu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return result;
|
||||
return await base.UpdateAsync(menu);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -153,18 +79,9 @@ public class MenuRepository : BaseRepository<DbMenu, MenuBean>
|
||||
/// </summary>
|
||||
/// <param name="menu"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<int> UpdateAsync(MenuBean menu, SqlSugarClient db)
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
var result = await db.Updateable<DbMenu>(_mapper.Map<DbMenu>(menu))
|
||||
.ExecuteCommandAsync();
|
||||
stopwatch.Stop();
|
||||
NlogHelper.Info($"编辑菜单 '{menu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
public async Task<MenuBean?> GetMenuByDataIdAsync(int dataId, MenuType menuType)
|
||||
public async Task<DbMenu?> GetMenuByDataIdAsync(int dataId, MenuType menuType)
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
@@ -172,12 +89,12 @@ public class MenuRepository : BaseRepository<DbMenu, MenuBean>
|
||||
.FirstAsync(m => m.DataId == dataId && m.Type == menuType);
|
||||
stopwatch.Stop();
|
||||
NlogHelper.Info($"根据DataId '{dataId}' 和 MenuType '{menuType}' 获取菜单耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return _mapper.Map<MenuBean>(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<MenuBean> GetMainMenuByNameAsync(string name)
|
||||
public async Task<DbMenu> GetMainMenuByNameAsync(string name)
|
||||
{
|
||||
var dbMenu= await Db.Queryable<DbMenu>().FirstAsync(m => m.Name == name && m.Type == MenuType.MainMenu);
|
||||
return _mapper.Map<MenuBean>(dbMenu);
|
||||
return dbMenu;
|
||||
}
|
||||
}
|
||||
@@ -12,14 +12,12 @@ namespace DMS.Infrastructure.Repositories;
|
||||
/// <summary>
|
||||
/// Mqtt仓储类,用于操作DbMqtt实体
|
||||
/// </summary>
|
||||
public class MqttRepository : BaseRepository<DbMqtt, Mqtt>
|
||||
public class MqttRepository : BaseRepository<DbMqtt>
|
||||
{
|
||||
private readonly MenuRepository _menuRepository;
|
||||
|
||||
public MqttRepository(MenuRepository menuRepository, IMapper mapper, ITransaction transaction)
|
||||
: base(mapper, transaction)
|
||||
public MqttRepository(SqlSugarDbContext dbContext)
|
||||
: base(dbContext)
|
||||
{
|
||||
_menuRepository = menuRepository;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -27,7 +25,7 @@ public class MqttRepository : BaseRepository<DbMqtt, Mqtt>
|
||||
/// </summary>
|
||||
/// <param name="id">主键ID</param>
|
||||
/// <returns></returns>
|
||||
public async Task<Mqtt> GetByIdAsync(int id)
|
||||
public override async Task<DbMqtt> GetByIdAsync(int id)
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
@@ -36,14 +34,14 @@ public class MqttRepository : BaseRepository<DbMqtt, Mqtt>
|
||||
.SingleAsync();
|
||||
stopwatch.Stop();
|
||||
NlogHelper.Info($"根据ID '{id}' 获取Mqtt配置耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return _mapper.Map<Mqtt>(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有Mqtt配置
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<List<Mqtt>> GetAllAsync()
|
||||
public override async Task<List<DbMqtt>> GetAllAsync()
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
@@ -53,115 +51,8 @@ public class MqttRepository : BaseRepository<DbMqtt, Mqtt>
|
||||
.ToListAsync();
|
||||
stopwatch.Stop();
|
||||
NlogHelper.Info($"获取所有Mqtt配置耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return result.Select(m => _mapper.Map<Mqtt>(m))
|
||||
.ToList();
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 新增Mqtt配置
|
||||
/// </summary>
|
||||
/// <param name="mqtt">Mqtt实体</param>
|
||||
/// <returns></returns>
|
||||
public async Task<int> AddAsync(Mqtt mqtt)
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
await Db.BeginTranAsync();
|
||||
try
|
||||
{
|
||||
var result = await Db.Insertable(_mapper.Map<DbMqtt>(mqtt))
|
||||
.ExecuteReturnIdentityAsync();
|
||||
var mqttMenu = await _menuRepository.GetMainMenuByNameAsync("Mqtt服务器");
|
||||
// AddAsync menu entry
|
||||
var menu = new MenuBean()
|
||||
{
|
||||
Name = mqtt.Name,
|
||||
// Icon = SegoeFluentIcons.Wifi.Glyph,
|
||||
Type = MenuType.MqttMenu,
|
||||
DataId = result,
|
||||
ParentId = mqttMenu.Id,
|
||||
};
|
||||
await _menuRepository.AddAsync(menu, Db);
|
||||
await Db.CommitTranAsync();
|
||||
stopwatch.Stop();
|
||||
NlogHelper.Info($"新增Mqtt配置 '{mqtt.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await Db.RollbackTranAsync();
|
||||
NlogHelper.Error($"添加MQTT配置 {{mqtt.Name}} 失败", ex);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新Mqtt配置
|
||||
/// </summary>
|
||||
/// <param name="mqtt">Mqtt实体</param>
|
||||
/// <returns></returns>
|
||||
public async Task<int> UpdateAsync(Mqtt mqtt)
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
await Db.BeginTranAsync();
|
||||
try
|
||||
{
|
||||
var result = await Db.Updateable(_mapper.Map<DbMqtt>(mqtt))
|
||||
.ExecuteCommandAsync();
|
||||
// Update menu entry
|
||||
var menu = await _menuRepository.GetMenuByDataIdAsync(mqtt.Id, MenuType.MqttMenu);
|
||||
if (menu != null)
|
||||
{
|
||||
menu.Name = mqtt.Name;
|
||||
await _menuRepository.UpdateAsync(menu, Db);
|
||||
}
|
||||
|
||||
await Db.CommitTranAsync();
|
||||
stopwatch.Stop();
|
||||
NlogHelper.Info($"更新Mqtt配置 '{mqtt.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await Db.RollbackTranAsync();
|
||||
NlogHelper.Error($"更新MQTT配置 {{mqtt.Name}} 失败", ex);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据ID删除Mqtt配置
|
||||
/// </summary>
|
||||
/// <param name="mqtt">Mqtt实体</param>
|
||||
/// <returns></returns>
|
||||
public async Task<int> DeleteAsync(Mqtt mqtt)
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
await Db.BeginTranAsync();
|
||||
try
|
||||
{
|
||||
var result = await Db.Deleteable<DbMqtt>()
|
||||
.In(mqtt.Id)
|
||||
.ExecuteCommandAsync();
|
||||
// DeleteAsync menu entry
|
||||
var menu = await _menuRepository.GetMenuByDataIdAsync(mqtt.Id, MenuType.MqttMenu);
|
||||
if (menu != null)
|
||||
{
|
||||
await _menuRepository.DeleteAsync(menu, Db);
|
||||
}
|
||||
|
||||
await Db.CommitTranAsync();
|
||||
stopwatch.Stop();
|
||||
NlogHelper.Info($"删除Mqtt配置ID '{mqtt.Id}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await Db.RollbackTranAsync();
|
||||
NlogHelper.Error($"删除MQTT配置 {{mqtt.Name}} 失败", ex);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,10 +9,10 @@ namespace DMS.Infrastructure.Repositories;
|
||||
/// <summary>
|
||||
/// 用户仓储类,用于操作DbUser实体
|
||||
/// </summary>
|
||||
public class UserRepository : BaseRepository<DbUser, User>
|
||||
public class UserRepository : BaseRepository<DbUser>
|
||||
{
|
||||
public UserRepository(IMapper mapper, ITransaction transaction)
|
||||
: base(mapper, transaction)
|
||||
public UserRepository(SqlSugarDbContext dbContext)
|
||||
: base(dbContext)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -10,16 +10,16 @@ namespace DMS.Infrastructure.Repositories;
|
||||
/// <summary>
|
||||
/// VariableData仓储类,用于操作DbVariableData实体
|
||||
/// </summary>
|
||||
public class VarDataRepository : BaseRepository<DbVariable, Variable>
|
||||
public class VarDataRepository : BaseRepository<DbVariable>
|
||||
{
|
||||
public VarDataRepository(IMapper mapper, ITransaction transaction)
|
||||
: base(mapper, transaction)
|
||||
public VarDataRepository(SqlSugarDbContext dbContext)
|
||||
: base(dbContext)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
|
||||
public override async Task<List<Variable>> GetAllAsync()
|
||||
public override async Task<List<DbVariable>> GetAllAsync()
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
@@ -29,11 +29,10 @@ public class VarDataRepository : BaseRepository<DbVariable, Variable>
|
||||
.ToListAsync();
|
||||
stopwatch.Stop();
|
||||
//NlogHelper.Info($"获取所有VariableData耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return result.Select(d => _mapper.Map<Variable>(d))
|
||||
.ToList();
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<List<Variable>> GetByVariableTableIdAsync(int varTableId)
|
||||
public async Task<List<DbVariable>> GetByVariableTableIdAsync(int varTableId)
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
@@ -42,21 +41,21 @@ public class VarDataRepository : BaseRepository<DbVariable, Variable>
|
||||
.ToListAsync();
|
||||
stopwatch.Stop();
|
||||
//NlogHelper.Info($"获取变量表的所有变量{result.Count()}个耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return result.Select(d => _mapper.Map<Variable>(d))
|
||||
.ToList();
|
||||
return result;
|
||||
}
|
||||
|
||||
public override async Task<int> AddAsync(Variable variable)
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
var dbVarData = await Db.Insertable(_mapper.Map<DbVariable>(variable))
|
||||
.ExecuteReturnEntityAsync();
|
||||
stopwatch.Stop();
|
||||
//NlogHelper.Info($"新增VariableData '{variable.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return dbVarData.Id;
|
||||
}
|
||||
//public override async Task<DbVariable> AddAsync(DbVariable entity)
|
||||
//{
|
||||
// Stopwatch stopwatch = new Stopwatch();
|
||||
// stopwatch.Start();
|
||||
// var dbVarData = await Db.Insertable(entity)
|
||||
// .ExecuteReturnEntityAsync();
|
||||
// stopwatch.Stop();
|
||||
// //NlogHelper.Info($"新增VariableData '{entity.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
// return dbVarData.Id;
|
||||
//}
|
||||
|
||||
/*
|
||||
public async Task<int> AddAsync(IEnumerable<Variable> variableDatas)
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
@@ -75,19 +74,21 @@ public class VarDataRepository : BaseRepository<DbVariable, Variable>
|
||||
//NlogHelper.Info($"新增VariableData '{variableDatas.Count()}'个, 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return res;
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
public override async Task<int> UpdateAsync(Variable variable)
|
||||
public override async Task<int> UpdateAsync(DbVariable entity)
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
var result = await Db.Updateable<DbVariable>(_mapper.Map<DbVariable>(variable))
|
||||
var result = await Db.Updateable(entity)
|
||||
.ExecuteCommandAsync();
|
||||
stopwatch.Stop();
|
||||
//NlogHelper.Info($"更新VariableData '{variable.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
//NlogHelper.Info($"更新VariableData '{entity.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
public async Task<int> UpdateAsync(List<Variable> variableDatas)
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
@@ -101,19 +102,21 @@ public class VarDataRepository : BaseRepository<DbVariable, Variable>
|
||||
//NlogHelper.Info($"更新VariableData {variableDatas.Count()}个 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return result;
|
||||
}
|
||||
*/
|
||||
|
||||
public override async Task<int> DeleteAsync(Variable variable)
|
||||
public override async Task<int> DeleteAsync(DbVariable entity)
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
var result = await Db.Deleteable<DbVariable>()
|
||||
.Where(d => d.Id == variable.Id)
|
||||
.Where(d => d.Id == entity.Id)
|
||||
.ExecuteCommandAsync();
|
||||
stopwatch.Stop();
|
||||
//NlogHelper.Info($"删除VariableData: '{variable.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
//NlogHelper.Info($"删除VariableData: '{entity.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
public async Task<int> DeleteAsync(IEnumerable<Variable> variableDatas)
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
@@ -128,12 +131,14 @@ public class VarDataRepository : BaseRepository<DbVariable, Variable>
|
||||
//NlogHelper.Info($"删除VariableData: '{variableDatas.Count()}'个 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return result;
|
||||
}
|
||||
*/
|
||||
|
||||
// public VarDataRepository(IMapper mapper)
|
||||
// {
|
||||
// _mapper = mapper;
|
||||
// }
|
||||
|
||||
/*
|
||||
/// <summary>
|
||||
/// 为变量添加MQTT服务器关联,并指定别名。
|
||||
/// </summary>
|
||||
@@ -156,7 +161,7 @@ public class VarDataRepository : BaseRepository<DbVariable, Variable>
|
||||
.ToListAsync();
|
||||
|
||||
var existingAliasesDict = existingAliases
|
||||
.ToDictionary(a => (a.VariableId, a.MqttId), a => a);
|
||||
.ToDictionary(a => (a.VariableId, a.Mqtt.Id), a => a);
|
||||
|
||||
var toInsert = new List<DbVariableMqtt>();
|
||||
var toUpdate = new List<DbVariableMqtt>();
|
||||
@@ -214,4 +219,5 @@ public class VarDataRepository : BaseRepository<DbVariable, Variable>
|
||||
throw;
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
@@ -7,10 +7,10 @@ using DMS.Infrastructure.Data;
|
||||
|
||||
namespace DMS.Infrastructure.Repositories;
|
||||
|
||||
public class VarTableRepository : BaseRepository<DbVariableTable, VariableTable>
|
||||
public class VarTableRepository : BaseRepository<DbVariableTable>
|
||||
{
|
||||
public VarTableRepository(IMapper mapper, ITransaction transaction)
|
||||
: base(mapper, transaction)
|
||||
public VarTableRepository(SqlSugarDbContext dbContext)
|
||||
: base(dbContext)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -19,16 +19,16 @@ public class VarTableRepository : BaseRepository<DbVariableTable, VariableTable>
|
||||
/// </summary>
|
||||
/// <param name="varTable"></param>
|
||||
/// <returns>变量表的ID</returns>
|
||||
public async Task<VariableTable> AddAsync(VariableTable varTable)
|
||||
public override async Task<DbVariableTable> AddAsync(DbVariableTable entity)
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
var addVarTabel = await Db.Insertable<DbVariableTable>(_mapper.Map<DbVariableTable>(varTable))
|
||||
var addVarTabel = await Db.Insertable(entity)
|
||||
.ExecuteReturnEntityAsync();
|
||||
|
||||
stopwatch.Stop();
|
||||
//NlogHelper.Info($"添加变量表 '{varTable.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return _mapper.Map<VariableTable>(addVarTabel);
|
||||
//NlogHelper.Info($"添加变量表 '{entity.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return addVarTabel;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -36,14 +36,14 @@ public class VarTableRepository : BaseRepository<DbVariableTable, VariableTable>
|
||||
/// </summary>
|
||||
/// <param name="variableTable"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<int> UpdateAsync(VariableTable variableTable)
|
||||
public override async Task<int> UpdateAsync(DbVariableTable entity)
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
var result = await Db.Updateable<DbVariableTable>(_mapper.Map<DbVariableTable>(variableTable))
|
||||
var result = await Db.Updateable(entity)
|
||||
.ExecuteCommandAsync();
|
||||
stopwatch.Stop();
|
||||
//NlogHelper.Info($"编辑变量表 '{variableTable.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
//NlogHelper.Info($"编辑变量表 '{entity.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -52,17 +52,17 @@ public class VarTableRepository : BaseRepository<DbVariableTable, VariableTable>
|
||||
/// </summary>
|
||||
/// <param name="variableTable"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<int> DeleteAsync(VariableTable varTable)
|
||||
public override async Task<int> DeleteAsync(DbVariableTable entity)
|
||||
{
|
||||
if (varTable == null )
|
||||
if (entity == null )
|
||||
return 0;
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
// 转换对象
|
||||
var res= await Db.Deleteable<DbVariableTable>(_mapper.Map<DbVariableTable>(varTable))
|
||||
var res= await Db.Deleteable(entity)
|
||||
.ExecuteCommandAsync();
|
||||
stopwatch.Stop();
|
||||
//NlogHelper.Info($"删除变量表 '{varTable.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
//NlogHelper.Info($"删除变量表 '{entity.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -71,14 +71,12 @@ public class VarTableRepository : BaseRepository<DbVariableTable, VariableTable>
|
||||
/// </summary>
|
||||
/// <param name="deviceVariableTables"></param>
|
||||
/// <param name="db"></param>
|
||||
public async Task DeleteAsync(IEnumerable<VariableTable> deviceVariableTables)
|
||||
public async Task DeleteAsync(IEnumerable<DbVariableTable> deviceVariableTables)
|
||||
{
|
||||
if (deviceVariableTables == null || deviceVariableTables.Count() == 0)
|
||||
return;
|
||||
// 转换对象
|
||||
var dbList = deviceVariableTables.Select(v => _mapper.Map<DbVariableTable>(v))
|
||||
.ToList();
|
||||
await Db.Deleteable<DbVariableTable>(dbList)
|
||||
await Db.Deleteable<DbVariableTable>(deviceVariableTables)
|
||||
.ExecuteCommandAsync();
|
||||
}
|
||||
|
||||
|
||||
@@ -9,10 +9,10 @@ namespace DMS.Infrastructure.Repositories;
|
||||
/// <summary>
|
||||
/// 变量与MQTT服务器别名关联的数据仓库。
|
||||
/// </summary>
|
||||
public class VariableMqttAliasRepository : BaseRepository<DbVariableMqtt, VariableMqtt>
|
||||
public class VariableMqttAliasRepository : BaseRepository<DbVariableMqtt>
|
||||
{
|
||||
public VariableMqttAliasRepository(IMapper mapper, ITransaction transaction)
|
||||
: base(mapper, transaction)
|
||||
public VariableMqttAliasRepository(SqlSugarDbContext dbContext)
|
||||
: base(dbContext)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user