2025-09-09 13:35:16 +08:00
|
|
|
|
using AutoMapper;
|
|
|
|
|
|
using DMS.Application.DTOs;
|
|
|
|
|
|
using DMS.Application.Interfaces;
|
2025-10-23 16:25:22 +08:00
|
|
|
|
using DMS.Application.Interfaces.Management;
|
|
|
|
|
|
using DMS.Application.Services;
|
2025-09-09 13:35:16 +08:00
|
|
|
|
using DMS.Core.Enums;
|
2025-10-07 17:51:24 +08:00
|
|
|
|
using DMS.Core.Models;
|
2025-09-09 13:35:16 +08:00
|
|
|
|
using DMS.WPF.Interfaces;
|
2025-10-06 18:17:56 +08:00
|
|
|
|
using DMS.WPF.ItemViewModel;
|
2025-10-23 16:25:22 +08:00
|
|
|
|
using DMS.WPF.ViewModels;
|
|
|
|
|
|
using iNKORE.UI.WPF.Modern.Common.IconKeys;
|
|
|
|
|
|
using System.Collections.ObjectModel;
|
2025-09-09 13:35:16 +08:00
|
|
|
|
|
2025-10-22 14:06:16 +08:00
|
|
|
|
namespace DMS.WPF.Services.ViewService;
|
2025-09-09 13:35:16 +08:00
|
|
|
|
|
2025-10-22 14:06:16 +08:00
|
|
|
|
public class VariableTableViewService : IVariableTableViewService
|
2025-09-09 13:35:16 +08:00
|
|
|
|
{
|
|
|
|
|
|
private readonly IMapper _mapper;
|
2025-10-22 14:06:16 +08:00
|
|
|
|
private readonly IViewDataService _viewDataService;
|
2025-10-23 16:25:22 +08:00
|
|
|
|
private readonly IAppDataService _appDataService;
|
|
|
|
|
|
private readonly IVariableTableManagementService _variableTableManagementService;
|
2025-10-20 22:47:22 +08:00
|
|
|
|
private readonly IMenuViewService _menuDataService;
|
2025-09-09 13:35:16 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-10-23 16:25:22 +08:00
|
|
|
|
public VariableTableViewService(IMapper mapper, IViewDataService viewDataService, IAppDataService appDataService, IVariableTableManagementService variableTableManagementService,
|
2025-10-20 22:47:22 +08:00
|
|
|
|
IMenuViewService menuDataService)
|
2025-09-09 13:35:16 +08:00
|
|
|
|
{
|
|
|
|
|
|
_mapper = mapper;
|
2025-10-23 16:25:22 +08:00
|
|
|
|
_viewDataService = viewDataService;
|
|
|
|
|
|
_appDataService = appDataService;
|
|
|
|
|
|
_variableTableManagementService = variableTableManagementService;
|
2025-09-09 13:35:16 +08:00
|
|
|
|
_menuDataService = menuDataService;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-23 16:25:22 +08:00
|
|
|
|
|
2025-09-09 13:35:16 +08:00
|
|
|
|
public void LoadAllVariableTables()
|
|
|
|
|
|
{
|
2025-10-22 14:06:16 +08:00
|
|
|
|
foreach (var device in _viewDataService.Devices)
|
2025-09-09 13:35:16 +08:00
|
|
|
|
{
|
2025-09-16 13:05:37 +08:00
|
|
|
|
foreach (var variableTable in device.Value.VariableTables)
|
2025-09-09 15:57:04 +08:00
|
|
|
|
{
|
2025-10-23 16:25:22 +08:00
|
|
|
|
_viewDataService.VariableTables.Add(variableTable.Id, variableTable);
|
2025-09-09 15:57:04 +08:00
|
|
|
|
}
|
2025-09-09 13:35:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-23 16:25:22 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步添加变量表
|
|
|
|
|
|
/// 此方法创建一个新的变量表项,包括创建对应的菜单项,并将其添加到数据库和视图数据服务中
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="variableTableItem">要添加的变量表项</param>
|
|
|
|
|
|
/// <returns>如果添加成功返回true,否则返回false</returns>
|
|
|
|
|
|
public async Task<bool> AddAsync(VariableTableItem variableTableItem)
|
2025-09-09 13:35:16 +08:00
|
|
|
|
{
|
2025-10-23 16:25:22 +08:00
|
|
|
|
// 检查传入的参数是否为空,如果为空则直接返回false
|
|
|
|
|
|
if (variableTableItem is null) return false;
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-09-09 13:35:16 +08:00
|
|
|
|
|
2025-10-23 16:25:22 +08:00
|
|
|
|
// 创建包含变量表和菜单的DTO对象
|
|
|
|
|
|
CreateVariableTableWithMenuDto createDto = new CreateVariableTableWithMenuDto();
|
|
|
|
|
|
createDto.VariableTable = _mapper.Map<VariableTable>(variableTableItem); // 将VariableTableItem映射为VariableTable对象
|
|
|
|
|
|
// 创建与变量表关联的菜单项
|
|
|
|
|
|
var deviceMenu = _viewDataService.Menus.FirstOrDefault(m => m.MenuType == MenuType.DeviceMenu && m.TargetId == variableTableItem.DeviceId);
|
|
|
|
|
|
if (deviceMenu is not null)
|
2025-09-09 13:35:16 +08:00
|
|
|
|
{
|
2025-10-23 16:25:22 +08:00
|
|
|
|
|
|
|
|
|
|
createDto.Menu = new MenuBean()
|
|
|
|
|
|
{
|
|
|
|
|
|
Header = variableTableItem.Name, // 菜单标题为变量表名称
|
|
|
|
|
|
ParentId = deviceMenu.Id,
|
|
|
|
|
|
MenuType = MenuType.VariableTableMenu,
|
|
|
|
|
|
Icon = SegoeFluentIcons.DataSense.Glyph, // 设置菜单图标
|
|
|
|
|
|
TargetViewKey = nameof(VariableTableViewModel) // 设置目标视图
|
|
|
|
|
|
};
|
2025-09-09 13:35:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-23 16:25:22 +08:00
|
|
|
|
// 调用应用中心服务异步添加变量表
|
|
|
|
|
|
var resDto = await _variableTableManagementService.AddAsync(createDto);
|
|
|
|
|
|
// 检查返回结果是否为空,如果为空则表示添加失败,返回false
|
|
|
|
|
|
if (resDto is null) return false;
|
|
|
|
|
|
|
|
|
|
|
|
// 设置变量表项的ID为服务器返回的ID
|
|
|
|
|
|
variableTableItem.Id = resDto.VariableTable.Id;
|
|
|
|
|
|
// 如果变量表项有关联的设备,则将该变量表添加到设备的变量表集合中
|
|
|
|
|
|
variableTableItem.Device?.VariableTables.Add(variableTableItem);
|
2025-09-09 13:35:16 +08:00
|
|
|
|
|
2025-10-23 16:25:22 +08:00
|
|
|
|
_viewDataService.VariableTables.TryAdd(variableTableItem.Id, variableTableItem);
|
2025-09-09 13:35:16 +08:00
|
|
|
|
|
2025-10-23 16:25:22 +08:00
|
|
|
|
// 将新创建的菜单项添加到菜单视图服务中
|
|
|
|
|
|
_menuDataService.AddMenuToView(_mapper.Map<MenuItem>(resDto.Menu));
|
|
|
|
|
|
// 成功添加,返回true
|
|
|
|
|
|
return true;
|
2025-09-09 13:35:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-10-23 16:25:22 +08:00
|
|
|
|
public async Task<bool> UpdateAsync(VariableTableItem variableTable)
|
2025-09-09 13:35:16 +08:00
|
|
|
|
{
|
2025-10-23 16:25:22 +08:00
|
|
|
|
// 检查变量表是否存在于应用数据存储中
|
|
|
|
|
|
if (!_appDataService.VariableTables.TryGetValue(variableTable.Id, out var variableTableInApp))
|
2025-09-09 13:35:16 +08:00
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2025-10-23 16:25:22 +08:00
|
|
|
|
_mapper.Map(variableTable, variableTableInApp);
|
|
|
|
|
|
if (await _variableTableManagementService.UpdateAsync(variableTableInApp) > 0)
|
2025-09-09 13:35:16 +08:00
|
|
|
|
{
|
2025-10-01 19:16:47 +08:00
|
|
|
|
// 更新数据库后会自动更新内存,无需额外操作
|
2025-09-09 13:35:16 +08:00
|
|
|
|
|
2025-10-22 14:06:16 +08:00
|
|
|
|
var menu = _viewDataService.Menus.FirstOrDefault(m =>
|
2025-09-09 13:35:16 +08:00
|
|
|
|
m.MenuType == MenuType.VariableTableMenu &&
|
|
|
|
|
|
m.TargetId == variableTable.Id);
|
2025-10-23 16:25:22 +08:00
|
|
|
|
if (menu is not null && menu.Header != variableTable.Name)
|
2025-09-09 13:35:16 +08:00
|
|
|
|
{
|
|
|
|
|
|
menu.Header = variableTable.Name;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-23 16:25:22 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步删除变量表
|
|
|
|
|
|
/// 此方法用于删除指定的变量表项,包括从数据库中删除、移除关联的变量和菜单项
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="variableTable">要删除的变量表项</param>
|
|
|
|
|
|
/// <returns>如果删除成功返回true,否则返回false</returns>
|
|
|
|
|
|
public async Task<bool> DeleteAsync(VariableTableItem variableTable)
|
2025-09-09 13:35:16 +08:00
|
|
|
|
{
|
2025-10-23 16:25:22 +08:00
|
|
|
|
// 检查变量表是否存在于应用数据存储中
|
|
|
|
|
|
if (!_appDataService.VariableTables.TryGetValue(variableTable.Id, out var variableTableInApp))
|
2025-09-09 13:35:16 +08:00
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-23 16:25:22 +08:00
|
|
|
|
// 从数据库和内存中删除变量表相关数据
|
|
|
|
|
|
if (!await _variableTableManagementService.DeleteAsync(variableTableInApp))
|
2025-09-09 13:35:16 +08:00
|
|
|
|
{
|
2025-10-23 16:25:22 +08:00
|
|
|
|
return false;
|
2025-09-09 13:35:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-23 16:25:22 +08:00
|
|
|
|
// 从界面删除变量表相关数据集
|
|
|
|
|
|
|
2025-09-09 13:35:16 +08:00
|
|
|
|
foreach (var variable in variableTable.Variables)
|
|
|
|
|
|
{
|
2025-10-22 14:06:16 +08:00
|
|
|
|
_viewDataService.Variables.Remove(variable.Id);
|
2025-09-09 13:35:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-23 16:25:22 +08:00
|
|
|
|
// 删除变量表相关的菜单
|
2025-09-09 13:35:16 +08:00
|
|
|
|
var variableTableMenu
|
2025-10-23 16:25:22 +08:00
|
|
|
|
= _viewDataService.Menus.FirstOrDefault(m => m.MenuType == MenuType.VariableTableMenu &&
|
|
|
|
|
|
m.TargetId == variableTable.Id);
|
|
|
|
|
|
if (variableTableMenu != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_menuDataService.DeleteMenuToView(variableTableMenu);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 从视图数据服务中删除变量表
|
2025-10-22 14:06:16 +08:00
|
|
|
|
_viewDataService.VariableTables.Remove(variableTable.Id);
|
2025-10-23 16:25:22 +08:00
|
|
|
|
// 从设备的变量表集合中移除该变量表
|
|
|
|
|
|
variableTable.Device?.VariableTables.Remove(variableTable);
|
|
|
|
|
|
|
2025-09-09 13:35:16 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|