重构了代码
This commit is contained in:
@@ -1,59 +1,46 @@
|
||||
using NLog;
|
||||
using PMSWPF.Data.Entities;
|
||||
using PMSWPF.Enums;
|
||||
using PMSWPF.Extensions;
|
||||
using PMSWPF.Helper;
|
||||
using PMSWPF.Models;
|
||||
using SqlSugar;
|
||||
|
||||
namespace PMSWPF.Data.Repositories;
|
||||
|
||||
public class DeviceRepository
|
||||
public class DeviceRepository
|
||||
{
|
||||
private readonly MenuRepository _menuRepository;
|
||||
private readonly VarTableRepository _varTableRepository;
|
||||
private static readonly ILogger Logger = LogManager.GetCurrentClassLogger();
|
||||
|
||||
public DeviceRepository()
|
||||
{
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// 添加设备
|
||||
/// </summary>
|
||||
/// <param name="device"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="InvalidOperationException"></exception>
|
||||
public async Task<Device> Add(Device device)
|
||||
{
|
||||
using (var db=DbContext.GetInstance())
|
||||
{
|
||||
var exist = await db.Queryable<DbDevice>().Where(d => d.Name == device.Name).FirstAsync();
|
||||
if (exist != null)
|
||||
throw new InvalidOperationException("设备名称已经存在。");
|
||||
var dbDevice = new DbDevice();
|
||||
device.CopyTo(dbDevice);
|
||||
dbDevice.VariableTables = new List<DbVariableTable>();
|
||||
// 添加默认变量表
|
||||
var dbVariableTable = new DbVariableTable();
|
||||
dbVariableTable.Name = "默认变量表";
|
||||
dbVariableTable.Description = "默认变量表";
|
||||
dbVariableTable.ProtocolType = dbDevice.ProtocolType;
|
||||
dbDevice.VariableTables.Add(dbVariableTable);
|
||||
var addDbDevice= await db.InsertNav(dbDevice).Include(d => d.VariableTables).ExecuteReturnEntityAsync();
|
||||
return addDbDevice.CopyTo<Device>();
|
||||
}
|
||||
|
||||
_menuRepository=new MenuRepository();
|
||||
_varTableRepository=new VarTableRepository();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public async Task<int> Edit(Device device)
|
||||
{
|
||||
using (var db=DbContext.GetInstance())
|
||||
using (var db = DbContext.GetInstance())
|
||||
{
|
||||
return await db.Updateable<DbDevice>(device.CopyTo<DbDevice>()).ExecuteCommandAsync();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取设备列表
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<List<Device>> GetAll()
|
||||
{
|
||||
using (var db = DbContext.GetInstance())
|
||||
{
|
||||
var dlist = await db.Queryable<DbDevice>().Includes(d => d.VariableTables,dv=>dv.Device).ToListAsync();
|
||||
var dlist = await db.Queryable<DbDevice>().Includes(d => d.VariableTables, dv => dv.Device).ToListAsync();
|
||||
var devices = new List<Device>();
|
||||
foreach (var dbDevice in dlist)
|
||||
{
|
||||
@@ -63,16 +50,14 @@ public class DeviceRepository
|
||||
|
||||
return devices;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public async Task<DbDevice> GetById(int id)
|
||||
{
|
||||
using (var db=DbContext.GetInstance())
|
||||
using (var db = DbContext.GetInstance())
|
||||
{
|
||||
return await db.Queryable<DbDevice>().FirstAsync(p => p.Id == id);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public async Task<int> DeleteById(int id)
|
||||
@@ -81,6 +66,78 @@ public class DeviceRepository
|
||||
{
|
||||
return await db.Deleteable<DbDevice>(new DbDevice { Id = id }).ExecuteCommandAsync();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加设备,包括菜单
|
||||
/// </summary>
|
||||
/// <param name="device"></param>
|
||||
public async Task AddDeviceAndMenu(Device device)
|
||||
{
|
||||
var db = DbContext.GetInstance();
|
||||
try
|
||||
{
|
||||
// 开启事务
|
||||
await db.BeginTranAsync();
|
||||
// 2. 将设备添加到数据库
|
||||
var addDevice = await Add(device, db);
|
||||
// 如果数据库添加失败
|
||||
if (addDevice == null)
|
||||
{
|
||||
string addDeviceErrorMsg = $"添加设备失败:{device.Name}";
|
||||
Logger.Error(addDeviceErrorMsg);
|
||||
NotificationHelper.ShowMessage(addDeviceErrorMsg, NotificationType.Error);
|
||||
return; // 提前返回
|
||||
}
|
||||
|
||||
// 3. 设备成功添加到数据库,进行菜单添加
|
||||
// 这里立即发出成功的通知和日志
|
||||
string addDeviceSuccessMsg = $"添加设备成功:{device.Name}";
|
||||
Logger.Info(addDeviceSuccessMsg);
|
||||
NotificationHelper.ShowMessage(addDeviceSuccessMsg, NotificationType.Success);
|
||||
|
||||
// 4. 为新设备添加菜单
|
||||
var addDeviceMenuId = await _menuRepository.AddDeviceMenu(addDevice, db);
|
||||
if (device.IsAddDefVarTable)
|
||||
{
|
||||
var defVarTable = await _varTableRepository.AddDeviceDefVarTable(addDevice, db);
|
||||
await _menuRepository.AddDeviceDefTableMenu(device, addDeviceMenuId, defVarTable.Id,db);
|
||||
}
|
||||
|
||||
// 添加添加变量表的菜单
|
||||
await _menuRepository.AddVarTableMenu(addDevice,addDeviceMenuId, db);
|
||||
await db.CommitTranAsync();
|
||||
// 菜单也添加成功,通知 UI 更新
|
||||
MessageHelper.SendLoadMessage(LoadTypes.Menu);
|
||||
MessageHelper.SendLoadMessage(LoadTypes.Devices);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
// 中间出错了 回滚
|
||||
await db.RollbackTranAsync();
|
||||
// 捕获并记录所有未预期的异常
|
||||
string errorMsg = $"在添加设备过程中发生未预期错误:";
|
||||
Logger.Error(errorMsg+e);
|
||||
NotificationHelper.ShowMessage(errorMsg+e.Message, NotificationType.Error);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 单独添加设备,不包括菜单和变量表
|
||||
/// </summary>
|
||||
/// <param name="device"></param>
|
||||
/// <param name="db"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="InvalidOperationException"></exception>
|
||||
private async Task<DbDevice> Add(Device device, SqlSugarClient db)
|
||||
{
|
||||
var exist = await db.Queryable<DbDevice>().Where(d => d.Name == device.Name).FirstAsync();
|
||||
if (exist != null)
|
||||
throw new InvalidOperationException("设备名称已经存在。");
|
||||
var dbDevice = new DbDevice();
|
||||
device.CopyTo(dbDevice);
|
||||
// 是否添加默认变量表
|
||||
return await db.Insertable<DbDevice>(dbDevice).ExecuteReturnEntityAsync();
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ using PMSWPF.Data.Entities;
|
||||
using PMSWPF.Enums;
|
||||
using PMSWPF.Extensions;
|
||||
using PMSWPF.Models;
|
||||
using SqlSugar;
|
||||
|
||||
namespace PMSWPF.Data.Repositories;
|
||||
|
||||
@@ -47,63 +48,104 @@ public class MenuRepository
|
||||
return await db.Insertable<DbMenu>(menu.CopyTo<DbMenu>()).ExecuteCommandAsync();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public async Task<bool> AddDeviceMenu(Device device)
|
||||
|
||||
/// <summary>
|
||||
/// 添加默认变量表的菜单
|
||||
/// </summary>
|
||||
/// <param name="device"></param>
|
||||
/// <param name="addDeviceMenuId"></param>
|
||||
/// <param name="db"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="InvalidOperationException"></exception>
|
||||
public async Task<int> AddDeviceDefTableMenu(Device device, int parentMenuId,int varTableId, SqlSugarClient db)
|
||||
{
|
||||
using (var db = DbContext.GetInstance())
|
||||
var defVarTableMenu = new MenuBean()
|
||||
{
|
||||
bool result = false;
|
||||
var deviceMainMenu = await db.Queryable<DbMenu>().FirstAsync(m => m.Name == "设备");
|
||||
if (deviceMainMenu == null)
|
||||
throw new InvalidOperationException("没有找到设备菜单!!");
|
||||
Name = "默认变量表",
|
||||
Icon = SegoeFluentIcons.Tablet.Glyph,
|
||||
Type = MenuType.VariableTableMenu,
|
||||
ParentId = parentMenuId,
|
||||
DataId = varTableId
|
||||
};
|
||||
var defTableRes = await db.Insertable<DbMenu>(defVarTableMenu).ExecuteCommandAsync();
|
||||
return defTableRes;
|
||||
}
|
||||
|
||||
// 添加菜单项
|
||||
MenuBean menu = new MenuBean()
|
||||
{
|
||||
Name = device.Name,
|
||||
Type = MenuType.DeviceMenu,
|
||||
DataId = device.Id,
|
||||
Icon = SegoeFluentIcons.Devices4.Glyph,
|
||||
};
|
||||
/// <summary>
|
||||
/// 给设备添加默认变量表菜单
|
||||
/// </summary>
|
||||
/// <param name="device"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="InvalidOperationException"></exception>
|
||||
// public async Task<bool> AddDeviceDefVarTableMenu(Device device)
|
||||
// {
|
||||
// var db = DbContext.GetInstance();
|
||||
// try
|
||||
// {
|
||||
// await db.BeginTranAsync();
|
||||
// bool result = false;
|
||||
// var parentMenuId = await AddDeviceMenu(device, db);
|
||||
// var defTableRes = await AddDeviceDefTableMenu(device, parentMenuId, db);
|
||||
// var addTableRes = await AddVarTableMenu(parentMenuId, db);
|
||||
// // if ((addTableRes + defTableRes) != 2)
|
||||
// // {
|
||||
// // // 如果出错删除原来添加的设备菜单
|
||||
// // await db.Deleteable<DbMenu>().Where(m => m.Id == parentMenuId).ExecuteCommandAsync();
|
||||
// // throw new InvalidOperationException("添加默认变量表时发生了错误!!");
|
||||
// // }
|
||||
//
|
||||
// await db.CommitTranAsync();
|
||||
// return true;
|
||||
// }
|
||||
// catch (Exception e)
|
||||
// {
|
||||
// await db.RollbackTranAsync();
|
||||
// }
|
||||
// finally
|
||||
// {
|
||||
// }
|
||||
// }
|
||||
|
||||
menu.ParentId = deviceMainMenu.Id;
|
||||
var addDeviceMenuId = await db.Insertable<DbMenu>(menu.CopyTo<DbMenu>())
|
||||
.ExecuteReturnIdentityAsync();
|
||||
if (addDeviceMenuId == 0)
|
||||
throw new InvalidOperationException($"{menu.Name},设备菜单添加失败!!");
|
||||
public async Task<int> AddVarTableMenu(DbDevice dbDevice, int parentMenuId, SqlSugarClient db)
|
||||
{
|
||||
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();
|
||||
return addTableRes;
|
||||
}
|
||||
|
||||
var defVarTable = await db.Queryable<DbVariableTable>()
|
||||
.FirstAsync(v => v.DeviceId == device.Id && v.Name == "默认变量表");
|
||||
if (defVarTable == null)
|
||||
throw new InvalidOperationException($"没有找到{device.Name}的默认变量表。");
|
||||
var defVarTableMenu = new MenuBean()
|
||||
{
|
||||
Name = "默认变量表",
|
||||
Icon = SegoeFluentIcons.Tablet.Glyph,
|
||||
Type = MenuType.VariableTableMenu,
|
||||
ParentId = addDeviceMenuId,
|
||||
DataId = defVarTable.Id
|
||||
};
|
||||
var addVarTable = new MenuBean()
|
||||
{
|
||||
Name = "添加变量表",
|
||||
Icon = SegoeFluentIcons.Add.Glyph,
|
||||
Type = MenuType.AddVariableTableMenu,
|
||||
ParentId = addDeviceMenuId,
|
||||
};
|
||||
var defTableRes = await db.Insertable<DbMenu>(defVarTableMenu).ExecuteCommandAsync();
|
||||
var addTableRes = await db.Insertable<DbMenu>(addVarTable).ExecuteCommandAsync();
|
||||
if ((addTableRes + defTableRes) != 2)
|
||||
{
|
||||
// 如果出错删除原来添加的设备菜单
|
||||
await db.Deleteable<DbMenu>().Where(m => m.Id == addDeviceMenuId).ExecuteCommandAsync();
|
||||
throw new InvalidOperationException("添加默认变量表时发生了错误!!");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加设备菜单
|
||||
/// </summary>
|
||||
/// <param name="device"></param>
|
||||
/// <param name="db"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="InvalidOperationException"></exception>
|
||||
public async Task<int> AddDeviceMenu(DbDevice device, SqlSugarClient db)
|
||||
{
|
||||
var deviceMainMenu = await db.Queryable<DbMenu>().FirstAsync(m => m.Name == "设备");
|
||||
if (deviceMainMenu == null)
|
||||
throw new InvalidOperationException("没有找到设备菜单!!");
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
// 添加菜单项
|
||||
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>(menu.CopyTo<DbMenu>())
|
||||
.ExecuteReturnIdentityAsync();
|
||||
return addDeviceMenuId;
|
||||
}
|
||||
|
||||
public async Task<int> Edit(MenuBean menu)
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
using iNKORE.UI.WPF.Modern.Common.IconKeys;
|
||||
using PMSWPF.Data.Entities;
|
||||
using PMSWPF.Enums;
|
||||
using PMSWPF.Extensions;
|
||||
using PMSWPF.Models;
|
||||
using SqlSugar;
|
||||
|
||||
namespace PMSWPF.Data.Repositories;
|
||||
|
||||
@@ -15,10 +18,34 @@ public class VarTableRepository
|
||||
{
|
||||
using (var db = DbContext.GetInstance())
|
||||
{
|
||||
return await db.Insertable<DbVariableTable>(varTable.CopyTo<DbVariableTable>()).ExecuteReturnIdentityAsync();
|
||||
return await db.Insertable<DbVariableTable>(varTable.CopyTo<DbVariableTable>())
|
||||
.ExecuteReturnIdentityAsync();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 添加默认变量表
|
||||
/// </summary>
|
||||
/// <param name="db"></param>
|
||||
/// <param name="dbDevice"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<DbVariableTable> AddDeviceDefVarTable(DbDevice dbDevice, SqlSugarClient db)
|
||||
{
|
||||
|
||||
// 添加默认变量表
|
||||
dbDevice.VariableTables = new List<DbVariableTable>();
|
||||
var dbVariableTable = new DbVariableTable();
|
||||
dbVariableTable.IsActive = true;
|
||||
dbVariableTable.DeviceId=dbDevice.Id;
|
||||
dbVariableTable.Name = "默认变量表";
|
||||
dbVariableTable.Description = "默认变量表";
|
||||
dbVariableTable.ProtocolType = dbDevice.ProtocolType;
|
||||
dbDevice.VariableTables.Add(dbVariableTable);
|
||||
return await db.Insertable<DbVariableTable>(dbVariableTable).ExecuteReturnEntityAsync();;
|
||||
}
|
||||
|
||||
|
||||
public async Task<int> Edit(VariableTable variableTable)
|
||||
{
|
||||
using (var db = DbContext.GetInstance())
|
||||
@@ -26,4 +53,4 @@ public class VarTableRepository
|
||||
return await db.Updateable<DbVariableTable>(variableTable.CopyTo<DbVariableTable>()).ExecuteCommandAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user