2025-06-28 19:32:51 +08:00
|
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
|
|
|
|
using CommunityToolkit.Mvvm.Messaging;
|
2025-06-30 14:19:22 +08:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2025-06-28 19:32:51 +08:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
using PMSWPF.Data.Repositories;
|
|
|
|
|
|
using PMSWPF.Enums;
|
|
|
|
|
|
using PMSWPF.Helper;
|
|
|
|
|
|
using PMSWPF.Message;
|
|
|
|
|
|
using PMSWPF.Models;
|
2025-06-30 14:19:22 +08:00
|
|
|
|
using PMSWPF.ViewModels;
|
2025-07-01 21:34:20 +08:00
|
|
|
|
using SqlSugar;
|
2025-06-28 19:32:51 +08:00
|
|
|
|
|
|
|
|
|
|
namespace PMSWPF.Services;
|
|
|
|
|
|
|
2025-06-30 13:06:51 +08:00
|
|
|
|
public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
|
2025-06-28 19:32:51 +08:00
|
|
|
|
{
|
|
|
|
|
|
private readonly ILogger<DataServices> _logger;
|
2025-06-30 22:03:49 +08:00
|
|
|
|
[ObservableProperty] private List<Device> _devices;
|
|
|
|
|
|
[ObservableProperty] private List<VariableTable> _variableTables;
|
2025-07-01 21:34:20 +08:00
|
|
|
|
[ObservableProperty] private List<MenuBean> menuTrees;
|
2025-06-28 19:32:51 +08:00
|
|
|
|
private readonly DeviceRepository _deviceRepository;
|
|
|
|
|
|
private readonly MenuRepository _menuRepository;
|
|
|
|
|
|
|
|
|
|
|
|
public event Action<List<Device>> OnDeviceListChanged;
|
2025-07-01 21:34:20 +08:00
|
|
|
|
public event Action<List<MenuBean>> OnMenuTreeListChanged;
|
2025-06-28 19:32:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-06-30 13:06:51 +08:00
|
|
|
|
partial void OnDevicesChanged(List<Device> devices)
|
2025-06-28 19:32:51 +08:00
|
|
|
|
{
|
|
|
|
|
|
OnDeviceListChanged?.Invoke(devices);
|
2025-07-01 21:34:20 +08:00
|
|
|
|
if (menuTrees != null && Devices != null)
|
2025-06-30 22:03:49 +08:00
|
|
|
|
{
|
2025-07-01 21:34:20 +08:00
|
|
|
|
FillMenuData(MenuTrees, Devices);
|
2025-06-30 22:03:49 +08:00
|
|
|
|
}
|
2025-06-28 19:32:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-01 21:34:20 +08:00
|
|
|
|
partial void OnMenuTreesChanged(List<MenuBean> MenuTrees)
|
2025-06-28 19:32:51 +08:00
|
|
|
|
{
|
2025-07-01 21:34:20 +08:00
|
|
|
|
OnMenuTreeListChanged?.Invoke(MenuTrees);
|
|
|
|
|
|
if (MenuTrees != null && Devices != null)
|
2025-06-30 22:03:49 +08:00
|
|
|
|
{
|
2025-07-01 21:34:20 +08:00
|
|
|
|
FillMenuData(MenuTrees, Devices);
|
2025-06-30 22:03:49 +08:00
|
|
|
|
}
|
2025-06-28 19:32:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-30 13:06:51 +08:00
|
|
|
|
|
|
|
|
|
|
public DataServices(ILogger<DataServices> logger)
|
2025-06-28 19:32:51 +08:00
|
|
|
|
{
|
|
|
|
|
|
_logger = logger;
|
|
|
|
|
|
IsActive = true;
|
2025-06-30 13:06:51 +08:00
|
|
|
|
_deviceRepository = new DeviceRepository();
|
|
|
|
|
|
_menuRepository = new MenuRepository();
|
2025-06-28 19:32:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-06-30 13:06:51 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 给Menu菜单的Data填充数据
|
|
|
|
|
|
/// </summary>
|
2025-07-01 21:34:20 +08:00
|
|
|
|
/// <param name="MenuTrees"></param>
|
|
|
|
|
|
private void FillMenuData(List<MenuBean> MenuTrees, List<Device> devices)
|
2025-06-28 19:32:51 +08:00
|
|
|
|
{
|
2025-07-01 21:34:20 +08:00
|
|
|
|
if (MenuTrees == null || MenuTrees.Count == 0)
|
2025-06-30 13:06:51 +08:00
|
|
|
|
return;
|
2025-06-30 22:03:49 +08:00
|
|
|
|
|
2025-07-01 21:34:20 +08:00
|
|
|
|
foreach (MenuBean menuBean in MenuTrees)
|
2025-06-30 13:06:51 +08:00
|
|
|
|
{
|
|
|
|
|
|
switch (menuBean.Type)
|
2025-06-30 22:03:49 +08:00
|
|
|
|
{
|
2025-06-30 13:06:51 +08:00
|
|
|
|
case MenuType.MainMenu:
|
2025-07-01 21:34:20 +08:00
|
|
|
|
menuBean.ViewModel =DataServicesHelper.GetMainViewModel(menuBean.Name);
|
2025-06-30 13:06:51 +08:00
|
|
|
|
break;
|
|
|
|
|
|
case MenuType.DeviceMenu:
|
2025-06-30 14:19:22 +08:00
|
|
|
|
menuBean.ViewModel = App.Current.Services.GetRequiredService<DeviceDetailViewModel>();
|
2025-06-30 22:03:49 +08:00
|
|
|
|
menuBean.Data = devices.FirstOrDefault(d => d.Id == menuBean.DataId);
|
2025-06-30 13:06:51 +08:00
|
|
|
|
break;
|
|
|
|
|
|
case MenuType.VariableTableMenu:
|
2025-06-30 14:19:22 +08:00
|
|
|
|
var varTableVM = App.Current.Services.GetRequiredService<VariableTableViewModel>();
|
|
|
|
|
|
varTableVM.VariableTable = FindVarTableForDevice(menuBean.DataId);
|
|
|
|
|
|
menuBean.ViewModel = varTableVM;
|
2025-06-30 22:03:49 +08:00
|
|
|
|
menuBean.Data = varTableVM.VariableTable;
|
2025-06-30 13:06:51 +08:00
|
|
|
|
break;
|
|
|
|
|
|
case MenuType.AddVariableTableMenu:
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
2025-06-30 22:03:49 +08:00
|
|
|
|
|
|
|
|
|
|
if (menuBean.Items != null && menuBean.Items.Count > 0)
|
2025-06-30 13:06:51 +08:00
|
|
|
|
{
|
2025-07-01 11:11:02 +08:00
|
|
|
|
FillMenuData(menuBean.Items, devices);
|
2025-06-30 13:06:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-01 21:34:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 查找设备所对应的菜单对象
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="device"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task<int> UpdateMenuForDevice(Device device)
|
2025-06-30 14:19:22 +08:00
|
|
|
|
{
|
2025-07-01 21:34:20 +08:00
|
|
|
|
var menu= DataServicesHelper.FindMenusForDevice(device, MenuTrees);
|
|
|
|
|
|
if (menu != null)
|
|
|
|
|
|
return await _menuRepository.Edit(menu);
|
2025-06-30 14:19:22 +08:00
|
|
|
|
|
2025-07-01 21:34:20 +08:00
|
|
|
|
return 0;
|
2025-06-30 14:19:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-01 11:11:02 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 从设备列表中找到变量表VarTable对象
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="vtableId">VarTable的ID</param>
|
|
|
|
|
|
/// <returns>如果找到择返回对象,否则返回null</returns>
|
2025-06-30 13:06:51 +08:00
|
|
|
|
private VariableTable FindVarTableForDevice(int vtableId)
|
|
|
|
|
|
{
|
|
|
|
|
|
VariableTable varTable = null;
|
|
|
|
|
|
foreach (var device in _devices)
|
|
|
|
|
|
{
|
2025-06-30 22:03:49 +08:00
|
|
|
|
varTable = device.VariableTables.FirstOrDefault(v => v.Id == vtableId);
|
2025-07-01 11:11:02 +08:00
|
|
|
|
if (varTable != null)
|
2025-06-30 22:03:49 +08:00
|
|
|
|
return varTable;
|
2025-06-30 13:06:51 +08:00
|
|
|
|
}
|
2025-06-30 22:03:49 +08:00
|
|
|
|
|
2025-06-30 13:06:51 +08:00
|
|
|
|
return varTable;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-01 11:11:02 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 接受加载消息,收到消息后从数据库加载对应的数据
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="message">消息的类型,如加载菜单LoadMessage.Menu</param>
|
|
|
|
|
|
/// <exception cref="ArgumentException"></exception>
|
2025-06-30 13:06:51 +08:00
|
|
|
|
public async void Receive(LoadMessage message)
|
|
|
|
|
|
{
|
2025-06-28 19:32:51 +08:00
|
|
|
|
if (!(message.Value is LoadTypes))
|
|
|
|
|
|
throw new ArgumentException($"接受到的加载类型错误:{message.Value}");
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-06-30 13:06:51 +08:00
|
|
|
|
switch ((LoadTypes)message.Value)
|
2025-06-28 19:32:51 +08:00
|
|
|
|
{
|
2025-06-30 13:06:51 +08:00
|
|
|
|
case LoadTypes.All:
|
|
|
|
|
|
Devices = await _deviceRepository.GetAll();
|
2025-06-30 22:03:49 +08:00
|
|
|
|
await LoadMenus();
|
|
|
|
|
|
break;
|
2025-06-28 19:32:51 +08:00
|
|
|
|
case LoadTypes.Devices:
|
2025-06-30 13:06:51 +08:00
|
|
|
|
Devices = await _deviceRepository.GetAll();
|
2025-06-28 19:32:51 +08:00
|
|
|
|
break;
|
|
|
|
|
|
case LoadTypes.Menu:
|
2025-06-30 22:03:49 +08:00
|
|
|
|
await LoadMenus();
|
2025-06-28 19:32:51 +08:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
|
|
|
|
|
NotificationHelper.ShowMessage($"加载数据出现了错误:{e.Message}");
|
2025-06-30 22:03:49 +08:00
|
|
|
|
_logger.LogError($"加载数据出现了错误:{e}");
|
2025-06-28 19:32:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-06-30 13:06:51 +08:00
|
|
|
|
|
2025-06-30 22:03:49 +08:00
|
|
|
|
private async Task LoadMenus()
|
|
|
|
|
|
{
|
2025-07-01 21:34:20 +08:00
|
|
|
|
MenuTrees = await _menuRepository.GetMenuTrees();
|
|
|
|
|
|
foreach (MenuBean menu in MenuTrees)
|
2025-06-30 22:03:49 +08:00
|
|
|
|
{
|
|
|
|
|
|
MenuHelper.MenuAddParent(menu);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-07-01 21:34:20 +08:00
|
|
|
|
|
|
|
|
|
|
public async Task<int> DeleteMenuForDevice(Device device)
|
|
|
|
|
|
{
|
|
|
|
|
|
var menu= DataServicesHelper.FindMenusForDevice(device, MenuTrees);
|
|
|
|
|
|
if (menu != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return await _menuRepository.DeleteMenu(menu);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2025-06-28 19:32:51 +08:00
|
|
|
|
}
|