修复错误
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using AutoMapper;
|
||||
using DMS.Infrastructure.Repositories;
|
||||
using System.Threading.Tasks;
|
||||
using DMS.Core.Interfaces.Repositories;
|
||||
|
||||
namespace DMS.Infrastructure.Services
|
||||
{
|
||||
@@ -10,11 +11,10 @@ namespace DMS.Infrastructure.Services
|
||||
/// <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>
|
||||
public abstract class BaseService<TModel, TRepository>
|
||||
where TModel : class, new()
|
||||
where TRepository : IBaseRepository<TModel>
|
||||
{
|
||||
protected readonly IMapper _mapper;
|
||||
protected readonly TRepository ServerRepository;
|
||||
|
||||
/// <summary>
|
||||
@@ -22,9 +22,8 @@ namespace DMS.Infrastructure.Services
|
||||
/// </summary>
|
||||
/// <param name="mapper">AutoMapper 实例,用于对象映射。</param>
|
||||
/// <param name="serverRepository">仓储实例,用于数据访问。</param>
|
||||
protected BaseService(IMapper mapper, TRepository serverRepository)
|
||||
protected BaseService( TRepository serverRepository)
|
||||
{
|
||||
_mapper = mapper;
|
||||
ServerRepository = serverRepository;
|
||||
}
|
||||
|
||||
@@ -33,10 +32,9 @@ namespace DMS.Infrastructure.Services
|
||||
/// </summary>
|
||||
/// <param name="model">要添加的业务模型对象。</param>
|
||||
/// <returns>返回添加后的数据库实体。</returns>
|
||||
public virtual async Task<TEntity> AddAsync(TModel model)
|
||||
public virtual async Task<TModel> AddAsync(TModel model)
|
||||
{
|
||||
var entity = _mapper.Map<TEntity>(model);
|
||||
return await ServerRepository.AddAsync(entity);
|
||||
return await ServerRepository.AddAsync(model);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -46,8 +44,7 @@ namespace DMS.Infrastructure.Services
|
||||
/// <returns>返回受影响的行数。</returns>
|
||||
public virtual async Task<int> UpdateAsync(TModel model)
|
||||
{
|
||||
var entity = _mapper.Map<TEntity>(model);
|
||||
return await ServerRepository.UpdateAsync(entity);
|
||||
return await ServerRepository.UpdateAsync(model);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -57,8 +54,17 @@ namespace DMS.Infrastructure.Services
|
||||
/// <returns>返回受影响的行数。</returns>
|
||||
public virtual async Task<int> DeleteAsync(TModel model)
|
||||
{
|
||||
var entity = _mapper.Map<TEntity>(model);
|
||||
return await ServerRepository.DeleteAsync(entity);
|
||||
return await ServerRepository.DeleteAsync(model);
|
||||
}
|
||||
|
||||
public virtual async Task<List<TModel>> GetAllAsync()
|
||||
{
|
||||
return await ServerRepository.GetAllAsync();
|
||||
}
|
||||
|
||||
public virtual async Task<TModel> GetByIdAsync(int id)
|
||||
{
|
||||
return await ServerRepository.GetByIdAsync(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,57 +0,0 @@
|
||||
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;
|
||||
using DMS.Core.Interfaces;
|
||||
|
||||
namespace DMS.Infrastructure.Services
|
||||
{
|
||||
public class DatabaseInitializerService : IDatabaseService
|
||||
{
|
||||
private readonly SqlSugarClient _db;
|
||||
|
||||
public DatabaseInitializerService(SqlSugarDbContext dbContext)
|
||||
{
|
||||
_db = dbContext.GetInstance();
|
||||
}
|
||||
|
||||
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<DbMqttServer>();
|
||||
_db.CodeFirst.InitTables<DbVariableMqttAlias>();
|
||||
_db.CodeFirst.InitTables<DbMenu>();
|
||||
}
|
||||
|
||||
public Task InitializeMenu()
|
||||
{
|
||||
var settings = AppSettings.Load();
|
||||
if (settings.Menus.Any())
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
settings.Menus.Add(new MenuBean() { Id=1, Header = "主页", Icon = "Home", ParentId = 0 });
|
||||
settings.Menus.Add(new MenuBean() { Id = 2, Header = "设备", Icon = "Devices3", ParentId = 0 });
|
||||
settings.Menus.Add(new MenuBean() { Id = 3, Header = "数据转换", Icon = "ChromeSwitch", ParentId = 0 });
|
||||
settings.Menus.Add(new MenuBean() { Id = 4, Header = "Mqtt服务器", Icon = "Cloud", ParentId = 0 });
|
||||
settings.Menus.Add(new MenuBean() { Id = 5, Header = "设置", Icon = "Settings", ParentId = 0 });
|
||||
settings.Menus.Add(new MenuBean() { Id = 6, Header = "关于", Icon = "Info", ParentId = 0 });
|
||||
|
||||
settings.Save();
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
76
DMS.Infrastructure/Services/DatabaseService.cs
Normal file
76
DMS.Infrastructure/Services/DatabaseService.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
using DMS.Config;
|
||||
using DMS.Core.Interfaces;
|
||||
using DMS.Core.Models;
|
||||
using DMS.Infrastructure.Data;
|
||||
using DMS.Infrastructure.Entities;
|
||||
using SqlSugar;
|
||||
|
||||
namespace DMS.Infrastructure.Services;
|
||||
|
||||
|
||||
|
||||
public class DatabaseService : IDatabaseService
|
||||
{
|
||||
private readonly SqlSugarDbContext _dbContext;
|
||||
private readonly SqlSugarClient _db;
|
||||
|
||||
public DatabaseService(SqlSugarDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_db = _dbContext.GetInstance();
|
||||
}
|
||||
|
||||
public void InitializeTables()
|
||||
{
|
||||
|
||||
_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<DbMqttServer>();
|
||||
_db.CodeFirst.InitTables<DbVariableMqttAlias>();
|
||||
_db.CodeFirst.InitTables<DbMenu>();
|
||||
}
|
||||
|
||||
public void InitializeTableIndex()
|
||||
{
|
||||
_db.DbMaintenance.CreateIndex(nameof(DbDevice),new []
|
||||
{
|
||||
nameof(DbDevice.Name),
|
||||
nameof(DbDevice.OpcUaServerUrl),
|
||||
},true);
|
||||
|
||||
_db.DbMaintenance.CreateIndex(nameof(DbVariable),new []
|
||||
{
|
||||
nameof(DbVariable.OpcUaNodeId)
|
||||
},true);
|
||||
_db.DbMaintenance.CreateIndex(nameof(DbMqttServer),new []
|
||||
{
|
||||
nameof(DbMqttServer.ServerName)
|
||||
},true);
|
||||
}
|
||||
|
||||
|
||||
public void InitializeMenus()
|
||||
{
|
||||
var settings = AppSettings.Load();
|
||||
if (settings.Menus.Any())
|
||||
{
|
||||
return ;
|
||||
}
|
||||
|
||||
settings.Menus.Add(new MenuBean() { Id=1, Header = "主页", Icon = "Home", ParentId = 0 });
|
||||
settings.Menus.Add(new MenuBean() { Id = 2, Header = "设备", Icon = "Devices3", ParentId = 0 });
|
||||
settings.Menus.Add(new MenuBean() { Id = 3, Header = "数据转换", Icon = "ChromeSwitch", ParentId = 0 });
|
||||
settings.Menus.Add(new MenuBean() { Id = 4, Header = "Mqtt服务器", Icon = "Cloud", ParentId = 0 });
|
||||
settings.Menus.Add(new MenuBean() { Id = 5, Header = "设置", Icon = "Settings", ParentId = 0 });
|
||||
settings.Menus.Add(new MenuBean() { Id = 6, Header = "关于", Icon = "Info", ParentId = 0 });
|
||||
|
||||
settings.Save();
|
||||
|
||||
return ;
|
||||
}
|
||||
}
|
||||
@@ -16,69 +16,14 @@ using DMS.Core.Interfaces.Repositories;
|
||||
|
||||
namespace DMS.Infrastructure.Services
|
||||
{
|
||||
public class DeviceService :BaseService<Device, DbDevice, DeviceRepository>
|
||||
public class DeviceService : BaseService<Device, DeviceRepository>
|
||||
{
|
||||
private readonly IDeviceRepository _deviceRepository;
|
||||
private readonly IMenuRepository _menuRepository;
|
||||
private readonly IVariableTableRepository _variableTableRepository;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
|
||||
public DeviceService(IMapper mapper, DeviceRepository repository) : base(mapper, repository)
|
||||
public DeviceService(DeviceRepository repository) : base(repository)
|
||||
{
|
||||
_deviceRepository = repository;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public async Task<List<Device>> GetAllAsync()
|
||||
{
|
||||
var dbDevices = await _deviceRepository.GetAllAsync();
|
||||
|
||||
return _mapper.Map<List<Device>>(dbDevices);
|
||||
}
|
||||
|
||||
|
||||
public async Task<Device> AddAsync(Device device)
|
||||
{
|
||||
Device resDevice = null;
|
||||
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))
|
||||
// {
|
||||
// NlogHelper.Warn("设备的名称,Ip:端口,OpcUrl,不可以重复。");
|
||||
// return resDevice;
|
||||
// }
|
||||
// 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 _variableTableRepository.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;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,9 +5,9 @@ using DMS.Infrastructure.Repositories;
|
||||
|
||||
namespace DMS.Infrastructure.Services
|
||||
{
|
||||
public class MenuService : BaseService<MenuBean, DbMenu, MenuRepository>
|
||||
public class MenuService : BaseService<MenuBean, MenuRepository>
|
||||
{
|
||||
public MenuService(IMapper mapper, MenuRepository repository) : base(mapper, repository)
|
||||
public MenuService(MenuRepository repository) : base(repository)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,9 +13,9 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace DMS.Infrastructure.Services
|
||||
{
|
||||
public class MqttService:BaseService<MqttServer, DbMqttServer, MqttServerRepository>
|
||||
public class MqttService:BaseService<MqttServer, MqttServerRepository>
|
||||
{
|
||||
public MqttService(IMapper mapper, MqttServerRepository serverRepository) : base(mapper, serverRepository)
|
||||
public MqttService( MqttServerRepository serverRepository) : base( serverRepository)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
using AutoMapper;
|
||||
using DMS.Core.Models;
|
||||
using DMS.Infrastructure.Entities;
|
||||
using DMS.Infrastructure.Repositories;
|
||||
|
||||
namespace DMS.Infrastructure.Services;
|
||||
|
||||
public class VarTableService : BaseService<VariableTable, DbVariableTable, VariableTableRepository>
|
||||
{
|
||||
public VarTableService(IMapper mapper, VariableTableRepository repository) : base(mapper, repository)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -13,13 +13,11 @@ using DMS.Infrastructure.Repositories;
|
||||
|
||||
namespace DMS.Infrastructure.Services
|
||||
{
|
||||
public class VarDataService : BaseService<Variable, DbVariable, VariableRepository>
|
||||
public class VariableService : BaseService<Variable, VariableRepository>
|
||||
{
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public VarDataService(IMapper mapper, VariableRepository repository) : base(mapper, repository)
|
||||
public VariableService(VariableRepository repository) : base(repository)
|
||||
{
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
}
|
||||
13
DMS.Infrastructure/Services/VariableTableService.cs
Normal file
13
DMS.Infrastructure/Services/VariableTableService.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using AutoMapper;
|
||||
using DMS.Core.Models;
|
||||
using DMS.Infrastructure.Entities;
|
||||
using DMS.Infrastructure.Repositories;
|
||||
|
||||
namespace DMS.Infrastructure.Services;
|
||||
|
||||
public class VariableTableService : BaseService<VariableTable, VariableTableRepository>
|
||||
{
|
||||
public VariableTableService(VariableTableRepository repository) : base(repository)
|
||||
{
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user