优化了添加设备时,并添加和更新菜单栏
This commit is contained in:
@@ -17,7 +17,8 @@ public class DeviceRepository
|
||||
public async Task<bool> Add(Device device)
|
||||
{
|
||||
var exist = await _db.Queryable<DbDevice>().Where(d => d.Name == device.Name).FirstAsync();
|
||||
if (exist != null) throw new DbExistException("设备名称已经存在。");
|
||||
if (exist != null)
|
||||
throw new InvalidOperationException("设备名称已经存在。");
|
||||
var dbDevice = new DbDevice();
|
||||
device.CopyTo(dbDevice);
|
||||
dbDevice.VariableTables = new List<DbVariableTable>();
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
using System.Windows.Controls;
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
using iNKORE.UI.WPF.Modern.Common.IconKeys;
|
||||
using PMSWPF.Data.Entities;
|
||||
using PMSWPF.Enums;
|
||||
using PMSWPF.Extensions;
|
||||
using PMSWPF.Helper;
|
||||
using PMSWPF.Message;
|
||||
using PMSWPF.Models;
|
||||
using SqlSugar;
|
||||
|
||||
@@ -34,11 +39,48 @@ public class MenuRepository
|
||||
return await _db.Insertable<DbMenu>(menu.CopyTo<DbMenu>()).ExecuteCommandAsync();
|
||||
}
|
||||
|
||||
public async Task<int> AddDeviceMenu(MenuBean menu)
|
||||
|
||||
public async Task<bool> AddDeviceMenu(MenuBean menu)
|
||||
{
|
||||
var deviceMenu = await _db.Queryable<DbMenu>().FirstAsync(m => m.Name == "设备");
|
||||
if (deviceMenu == null) return 0;
|
||||
menu.ParentId = deviceMenu.Id;
|
||||
return await _db.Insertable<DbMenu>(menu.CopyTo<DbMenu>()).ExecuteCommandAsync();
|
||||
bool result = false;
|
||||
var deviceMainMenu = await _db.Queryable<DbMenu>().FirstAsync(m => m.Name == "设备");
|
||||
if (deviceMainMenu == null)
|
||||
throw new InvalidOperationException("没有找到设备菜单!!");
|
||||
|
||||
menu.ParentId=deviceMainMenu.Id;
|
||||
var addDeviceMenuRes = await _db.Insertable<DbMenu>(menu.CopyTo<DbMenu>())
|
||||
.ExecuteCommandAsync();
|
||||
if (addDeviceMenuRes == 0)
|
||||
throw new InvalidOperationException($"{menu.Name},设备菜单添加失败!!");
|
||||
|
||||
var addDM = await _db.Queryable<DbMenu>().OrderBy(m => m.Id, OrderByType.Desc)
|
||||
.FirstAsync(m => m.Name == menu.Name);
|
||||
if (addDM == null)
|
||||
throw new InvalidOperationException($"添加默认变量表菜单时,没有找到名字为:{menu.Name}的菜单项!");
|
||||
|
||||
|
||||
var defVarTable=new MenuBean()
|
||||
{
|
||||
Name = "默认变量表",
|
||||
Icon = SegoeFluentIcons.Tablet.Glyph,
|
||||
ParentId = addDM.Id,
|
||||
};
|
||||
var addVarTable=new MenuBean()
|
||||
{
|
||||
Name = "添加变量表",
|
||||
Icon = SegoeFluentIcons.Add.Glyph,
|
||||
ParentId = addDM.Id,
|
||||
};
|
||||
var defTableRes = await _db.Insertable<DbMenu>(defVarTable).ExecuteCommandAsync();
|
||||
var addTableRes = await _db.Insertable<DbMenu>(addVarTable).ExecuteCommandAsync();
|
||||
if ((addTableRes+defTableRes) != 2)
|
||||
{
|
||||
// 如果出错删除原来添加的设备菜单
|
||||
await _db.Deleteable<DbMenu>().Where(m=>m.Id==addDM.Id).ExecuteCommandAsync();
|
||||
throw new InvalidOperationException("添加默认变量表时发生了错误!!");
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
using System.Reflection;
|
||||
|
||||
namespace PMSWPF.Helper;
|
||||
|
||||
public class CovertHelper
|
||||
{
|
||||
public static List<TTarget> ConvertList<TSource, TTarget>(List<TSource> sourceList)
|
||||
{
|
||||
var targetList = new List<TTarget>();
|
||||
var sourceType = typeof(TSource);
|
||||
var targetType = typeof(TTarget);
|
||||
|
||||
// 获取源类型和目标类型的公共属性
|
||||
var sourceProperties = sourceType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
|
||||
var targetProperties = targetType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
|
||||
|
||||
foreach (var sourceObject in sourceList)
|
||||
{
|
||||
var targetObject = Activator.CreateInstance<TTarget>();
|
||||
foreach (var targetProperty in targetProperties)
|
||||
{
|
||||
var sourceProperty = sourceProperties.FirstOrDefault(p =>
|
||||
p.Name == targetProperty.Name && p.PropertyType == targetProperty.PropertyType);
|
||||
if (sourceProperty != null)
|
||||
{
|
||||
var value = sourceProperty.GetValue(sourceObject);
|
||||
targetProperty.SetValue(targetObject, value);
|
||||
}
|
||||
}
|
||||
|
||||
targetList.Add(targetObject);
|
||||
}
|
||||
|
||||
return targetList;
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
using PMSWPF.Data;
|
||||
|
||||
namespace PMSWPF.Helper;
|
||||
|
||||
public class SqlSugarHelper
|
||||
{
|
||||
private DbContext _db;
|
||||
|
||||
public SqlSugarHelper()
|
||||
{
|
||||
_db = new DbContext();
|
||||
}
|
||||
|
||||
public void InitTables()
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
namespace PMSWPF.Message;
|
||||
|
||||
public class DialogMessage
|
||||
{
|
||||
public bool IsCancel { get; set; }
|
||||
public bool IsConfirm { get; set; }
|
||||
public Object? Request { get; set; }
|
||||
public Object? Response { get; set; }
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
using CommunityToolkit.Mvvm.Messaging.Messages;
|
||||
|
||||
namespace PMSWPF.Message;
|
||||
|
||||
public class OpenDialogMessage:RequestMessage<DialogMessage>
|
||||
{
|
||||
// public bool IsCancel { get; set; }
|
||||
// public bool IsConfirm { get; set; }
|
||||
// public Object? Request { get; set; }
|
||||
// public Object? Response { get; set; }
|
||||
public DialogMessage Message { get; set; }
|
||||
|
||||
public OpenDialogMessage()
|
||||
{
|
||||
}
|
||||
|
||||
public OpenDialogMessage(DialogMessage message)
|
||||
{
|
||||
Message = message;
|
||||
}
|
||||
}
|
||||
@@ -33,33 +33,4 @@ public class DialogService :IDialogService
|
||||
MessageBox.Show(message);
|
||||
}
|
||||
|
||||
|
||||
public void Receive(OpenDialogMessage message)
|
||||
{
|
||||
// DialogMessage response = new DialogMessage();
|
||||
// Device device = new Device();
|
||||
// if (message.Message! != null && message.Message.Request != null && message.Message.Request is Device)
|
||||
// {
|
||||
// device = message.Message.Request as Device;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// var ddvm = new DeviceDialogViewModel(device)
|
||||
// {
|
||||
// Title = "添加设备"
|
||||
// };
|
||||
// var dialog = new DeviceDialog(ddvm);
|
||||
// var res = dialog.ShowAsync().GetAwaiter().GetResult();
|
||||
// if (res == ContentDialogResult.Primary)
|
||||
// {
|
||||
// response.IsConfirm = true;
|
||||
// response.Response = device;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// response.IsCancel = true;
|
||||
// }
|
||||
// }
|
||||
// message.Reply(response);
|
||||
}
|
||||
}
|
||||
@@ -58,54 +58,31 @@ public partial class DevicesViewModel : ViewModelBase
|
||||
Name = device.Name,
|
||||
Icon = SegoeFluentIcons.Devices4.Glyph,
|
||||
};
|
||||
var rows = await _menuRepository.AddDeviceMenu(deviceMenu);
|
||||
if (rows > 0)
|
||||
bool addMenuRes = await _menuRepository.AddDeviceMenu(deviceMenu);
|
||||
if (addMenuRes)
|
||||
{
|
||||
WeakReferenceMessenger.Default.Send<UpdateMenuMessage>(new UpdateMenuMessage(2));
|
||||
// 通知更新菜单
|
||||
WeakReferenceMessenger.Default.Send<UpdateMenuMessage>(new UpdateMenuMessage(0));
|
||||
NotificationHelper.ShowMessage(msg, NotificationType.Success);
|
||||
}
|
||||
else
|
||||
{
|
||||
var msgerr = $"给设备添加菜单失败:{device.Name}";
|
||||
_logger.LogInformation(msgerr);
|
||||
NotificationHelper.ShowMessage(msgerr, NotificationType.Error);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var msg = $"添加设备失败:{device.Name}";
|
||||
_logger.LogInformation(msg);
|
||||
NotificationHelper.ShowMessage(msg, NotificationType.Error);
|
||||
}
|
||||
}
|
||||
|
||||
// OpenDialogMessage dialog = new OpenDialogMessage();
|
||||
//
|
||||
// var res=WeakReferenceMessenger.Default.Send<OpenDialogMessage>(dialog);
|
||||
|
||||
Console.WriteLine("");
|
||||
|
||||
// device = await _deviceDialogService.ShowAddDeviceDialog();
|
||||
// if (device != null)
|
||||
// {
|
||||
// var isOk = await _deviceRepository.Add(device);
|
||||
// if (isOk)
|
||||
// {
|
||||
// // 添加菜单项
|
||||
// MenuBean deviceMenu = new MenuBean()
|
||||
// { Name = device.Name, Icon = SegoeFluentIcons.Devices4.Glyph, ParentId = 2 };
|
||||
// MenuRepository mre = new MenuRepository();
|
||||
// mre.AddMenu(deviceMenu);
|
||||
//
|
||||
// // MessageBox.Show("Device added successfully");
|
||||
// await OnLoadedAsync();
|
||||
// var msg = $"设备添加成功:{device.Name}";
|
||||
// _logger.LogInformation(msg);
|
||||
// NotificationHelper.ShowMessage(msg, NotificationType.Success);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
catch (DbExistException e)
|
||||
{
|
||||
var msg = $"设备添加失败:名称为{device?.Name}的设备已经存在。请更换是被名称";
|
||||
_logger.LogError(msg);
|
||||
NotificationHelper.ShowMessage(msg, NotificationType.Error);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
var msg = $"添加设备的过程中发生错误:{e.Message}";
|
||||
var msg = $"添加设备失败:{e.Message}";
|
||||
_logger.LogError(msg);
|
||||
NotificationHelper.ShowMessage(msg, NotificationType.Success);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user