重构服务类和仓库类
This commit is contained in:
64
DMS.Infrastructure/Services/BaseService.cs
Normal file
64
DMS.Infrastructure/Services/BaseService.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using AutoMapper;
|
||||
using DMS.Infrastructure.Repositories;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DMS.Infrastructure.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// 通用服务基类,封装了常见的增、删、改操作。
|
||||
/// </summary>
|
||||
/// <typeparam name="TModel">业务逻辑模型类型。</typeparam>
|
||||
/// <typeparam name="TEntity">数据库实体类型。</typeparam>
|
||||
/// <typeparam name="TRepository">与实体对应的仓储类型。</typeparam>
|
||||
public abstract class BaseService<TModel, TEntity, TRepository>
|
||||
where TEntity : class, new()
|
||||
where TRepository : BaseRepository<TEntity>
|
||||
{
|
||||
protected readonly IMapper _mapper;
|
||||
protected readonly TRepository _repository;
|
||||
|
||||
/// <summary>
|
||||
/// 初始化 BaseService 的新实例。
|
||||
/// </summary>
|
||||
/// <param name="mapper">AutoMapper 实例,用于对象映射。</param>
|
||||
/// <param name="repository">仓储实例,用于数据访问。</param>
|
||||
protected BaseService(IMapper mapper, TRepository repository)
|
||||
{
|
||||
_mapper = mapper;
|
||||
_repository = repository;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步添加一个新的业务模型对象。
|
||||
/// </summary>
|
||||
/// <param name="model">要添加的业务模型对象。</param>
|
||||
/// <returns>返回添加后的数据库实体。</returns>
|
||||
public virtual async Task<TEntity> AddAsync(TModel model)
|
||||
{
|
||||
var entity = _mapper.Map<TEntity>(model);
|
||||
return await _repository.AddAsync(entity);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步更新一个现有的业务模型对象。
|
||||
/// </summary>
|
||||
/// <param name="model">要更新的业务模型对象。</param>
|
||||
/// <returns>返回受影响的行数。</returns>
|
||||
public virtual async Task<int> UpdateAsync(TModel model)
|
||||
{
|
||||
var entity = _mapper.Map<TEntity>(model);
|
||||
return await _repository.UpdateAsync(entity);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步删除一个业务模型对象。
|
||||
/// </summary>
|
||||
/// <param name="model">要删除的业务模型对象。</param>
|
||||
/// <returns>返回受影响的行数。</returns>
|
||||
public virtual async Task<int> DeleteAsync(TModel model)
|
||||
{
|
||||
var entity = _mapper.Map<TEntity>(model);
|
||||
return await _repository.DeleteAsync(entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
using DMS.Config;
|
||||
using DMS.Core.Enums;
|
||||
using DMS.Core.Models;
|
||||
using DMS.Infrastructure.Data;
|
||||
using DMS.Infrastructure.Entities;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DMS.Infrastructure.Services
|
||||
@@ -31,52 +33,24 @@ namespace DMS.Infrastructure.Services
|
||||
_db.CodeFirst.InitTables<DbMenu>();
|
||||
}
|
||||
|
||||
public async Task InitializeMenu()
|
||||
public 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()
|
||||
var settings = AppSettings.Load();
|
||||
if (settings.Menus.Any())
|
||||
{
|
||||
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();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
settings.Menus.Add(new MenuBean() { Id=1, Name = "主页", Type = MenuType.MainMenu, Icon = "Home", ParentId = 0 });
|
||||
settings.Menus.Add(new MenuBean() { Id = 2, Name = "设备", Type = MenuType.MainMenu, Icon = "Devices3", ParentId = 0 });
|
||||
settings.Menus.Add(new MenuBean() { Id = 3, Name = "数据转换", Type = MenuType.MainMenu, Icon = "ChromeSwitch", ParentId = 0 });
|
||||
settings.Menus.Add(new MenuBean() { Id = 4, Name = "Mqtt服务器", Type = MenuType.MainMenu, Icon = "Cloud", ParentId = 0 });
|
||||
settings.Menus.Add(new MenuBean() { Id = 5, Name = "设置", Type = MenuType.MainMenu, Icon = "Settings", ParentId = 0 });
|
||||
settings.Menus.Add(new MenuBean() { Id = 6, Name = "关于", Type = MenuType.MainMenu, Icon = "Info", ParentId = 0 });
|
||||
|
||||
settings.Save();
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,86 +15,68 @@ using System.Collections.Concurrent;
|
||||
|
||||
namespace DMS.Infrastructure.Services
|
||||
{
|
||||
public class DeviceService : IDeviceService
|
||||
public class DeviceService :BaseService<Device, DbDevice, DeviceRepository>
|
||||
{
|
||||
private readonly DeviceRepository _deviceRepository;
|
||||
private readonly IDeviceRepository _deviceRepository;
|
||||
private readonly IMenuRepository _menuRepository;
|
||||
private readonly IVarTableRepository _varTableRepository;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
private ConcurrentDictionary<int, Device> _devicesDic;
|
||||
|
||||
public DeviceService(DeviceRepository deviceRepository, IMenuRepository menuRepository, IVarTableRepository varTableRepository, IMapper mapper, SqlSugarDbContext dbContext)
|
||||
public DeviceService(IMapper mapper, DeviceRepository repository) : base(mapper, repository)
|
||||
{
|
||||
_deviceRepository = deviceRepository;
|
||||
_menuRepository = menuRepository;
|
||||
_varTableRepository = varTableRepository;
|
||||
_deviceRepository = repository;
|
||||
_mapper = mapper;
|
||||
_devicesDic = new ConcurrentDictionary<int, Device>();
|
||||
}
|
||||
|
||||
public async Task<List<Device>> GetAllAsync()
|
||||
{
|
||||
var dbDevices = await _deviceRepository.GetAllAsync();
|
||||
|
||||
var deviceDic = _mapper.Map<List<Device>>(dbDevices).ToDictionary(d => d.Id);
|
||||
_devicesDic = new ConcurrentDictionary<int, Device>(deviceDic);
|
||||
|
||||
return deviceDic.Values.ToList();
|
||||
return _mapper.Map<List<Device>>(dbDevices);
|
||||
}
|
||||
|
||||
|
||||
public async Task<Device> AddAsync(Device device)
|
||||
{
|
||||
Device resDevice = null;
|
||||
try
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
var dbList = await GetAllAsync();
|
||||
//查询设备的名字是否存在
|
||||
if (dbList.Any(d => d.Name == device.Name || (d.Ip == device.Ip && d.Prot == device.Prot) || d.OpcUaEndpointUrl == device.OpcUaEndpointUrl))
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
|
||||
//查询设备的名字是否存在
|
||||
if (_devicesDic.Values.Any(d => d.Name == device.Name || (d.Ip == device.Ip && d.Prot == device.Prot) || d.OpcUaEndpointUrl == device.OpcUaEndpointUrl))
|
||||
{
|
||||
NlogHelper.Warn("设备的名称,Ip:端口,OpcUrl,不可以重复。");
|
||||
return resDevice;
|
||||
}
|
||||
await _deviceRepository.GetTransaction().BeginTranAsync();
|
||||
// 2. 将设备添加到数据库
|
||||
var addDevice = await _deviceRepository.AddAsync(_mapper.Map<DbDevice>(device));
|
||||
|
||||
//判断判断是否添加默认变量表
|
||||
if (device.IsAddDefVarTable)
|
||||
{
|
||||
DbVariableTable dbVariableTable = new DbVariableTable();
|
||||
dbVariableTable.Name = "默认变量表";
|
||||
dbVariableTable.Description = "默认变量表";
|
||||
dbVariableTable.DeviceId = addDevice.Id;
|
||||
dbVariableTable.ProtocolType = addDevice.ProtocolType;
|
||||
var dbAddVarTable= await _varTableRepository.AddAsync(dbVariableTable);
|
||||
if (addDevice.VariableTables==null)
|
||||
{
|
||||
addDevice.VariableTables= new List<DbVariableTable>();
|
||||
}
|
||||
|
||||
addDevice.VariableTables.Add(dbAddVarTable);
|
||||
}
|
||||
|
||||
// 4. 为新设备添加菜单
|
||||
//var addDeviceMenuId = await _menuRepository.AddAsync(addDevice);
|
||||
resDevice = _mapper.Map<Device>(addDevice);
|
||||
|
||||
await _deviceRepository.GetTransaction().CommitTranAsync();
|
||||
|
||||
stopwatch.Stop();
|
||||
NlogHelper.Info($"添加设备 '{device.Name}' 及相关菜单耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
NlogHelper.Warn("设备的名称,Ip:端口,OpcUrl,不可以重复。");
|
||||
return resDevice;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
await _deviceRepository.GetTransaction().RollbackTranAsync();
|
||||
throw;
|
||||
|
||||
}
|
||||
// 2. 将设备添加到数据库
|
||||
var addDevice = await _deviceRepository.AddAsync(_mapper.Map<DbDevice>(device));
|
||||
|
||||
//判断判断是否添加默认变量表
|
||||
//if (device.IsAddDefVarTable)
|
||||
//{
|
||||
// DbVariableTable dbVariableTable = new DbVariableTable();
|
||||
// dbVariableTable.Name = "默认变量表";
|
||||
// dbVariableTable.Description = "默认变量表";
|
||||
// dbVariableTable.DeviceId = addDevice.Id;
|
||||
// dbVariableTable.ProtocolType = addDevice.ProtocolType;
|
||||
// var dbAddVarTable= await _varTableRepository.AddAsync(dbVariableTable);
|
||||
// if (addDevice.VariableTables==null)
|
||||
// {
|
||||
// addDevice.VariableTables= new List<DbVariableTable>();
|
||||
// }
|
||||
|
||||
// addDevice.VariableTables.Add(dbAddVarTable);
|
||||
//}
|
||||
|
||||
// 4. 为新设备添加菜单
|
||||
//var addDeviceMenuId = await _menuRepository.AddAsync(addDevice);
|
||||
resDevice = _mapper.Map<Device>(addDevice);
|
||||
|
||||
|
||||
stopwatch.Stop();
|
||||
NlogHelper.Info($"添加设备 '{device.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return resDevice;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,102 +1,14 @@
|
||||
using AutoMapper;
|
||||
using DMS.Core.Enums;
|
||||
using DMS.Core.Helper;
|
||||
using DMS.Core.Models;
|
||||
using DMS.Infrastructure.Data;
|
||||
using DMS.Infrastructure.Entities;
|
||||
using SqlSugar;
|
||||
using System.Diagnostics;
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using DMS.Infrastructure.Repositories;
|
||||
|
||||
namespace DMS.Infrastructure.Services
|
||||
{
|
||||
public class MenuService
|
||||
public class MenuService : BaseService<MenuBean, DbMenu, MenuRepository>
|
||||
{
|
||||
private readonly IMapper _mapper;
|
||||
private readonly SqlSugarDbContext _dbContext;
|
||||
private SqlSugarClient Db => _dbContext.GetInstance();
|
||||
|
||||
public MenuService(IMapper mapper, SqlSugarDbContext dbContext)
|
||||
public MenuService(IMapper mapper, MenuRepository repository) : base(mapper, repository)
|
||||
{
|
||||
_mapper = mapper;
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
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<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 = "添加变量表",
|
||||
Type = MenuType.AddVariableTableMenu,
|
||||
ParentId = parentMenuId,
|
||||
DataId = dbDevice.Id
|
||||
};
|
||||
var addTableRes = await db.Insertable<DbMenu>(addVarTable)
|
||||
.ExecuteCommandAsync();
|
||||
stopwatch.Stop();
|
||||
return addTableRes;
|
||||
}
|
||||
|
||||
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,
|
||||
};
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ using DMS.Core.Helper;
|
||||
using DMS.Core.Models;
|
||||
using DMS.Infrastructure.Data;
|
||||
using DMS.Infrastructure.Entities;
|
||||
using DMS.Infrastructure.Interfaces;
|
||||
using DMS.Infrastructure.Repositories;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
@@ -13,112 +14,10 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace DMS.Infrastructure.Services
|
||||
{
|
||||
public class MqttService
|
||||
public class MqttService:BaseService<Mqtt, DbMqtt, MqttRepository>
|
||||
{
|
||||
private readonly MqttRepository _mqttRepository;
|
||||
private readonly MenuRepository _menuRepository;
|
||||
private readonly IMapper _mapper;
|
||||
private readonly SqlSugarDbContext _dbContext;
|
||||
private SqlSugarClient Db => _dbContext.GetInstance();
|
||||
|
||||
public MqttService(MqttRepository mqttRepository, MenuRepository menuRepository, IMapper mapper, SqlSugarDbContext dbContext)
|
||||
public MqttService(IMapper mapper, MqttRepository repository) : base(mapper, repository)
|
||||
{
|
||||
_mqttRepository = mqttRepository;
|
||||
_menuRepository = menuRepository;
|
||||
_mapper = mapper;
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
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,
|
||||
Type = MenuType.MqttMenu,
|
||||
DataId = result,
|
||||
ParentId = mqttMenu.Id,
|
||||
};
|
||||
await _menuRepository.AddAsync(_mapper.Map<DbMenu>(menu));
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
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(_mapper.Map<DbMenu>(menu));
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
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(_mapper.Map<DbMenu>(menu));
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user