refactor:完成重构设备的添加,删除,更新。
This commit is contained in:
123
DMS.WPF/Services/ViewService/VariableTableViewService.cs
Normal file
123
DMS.WPF/Services/ViewService/VariableTableViewService.cs
Normal file
@@ -0,0 +1,123 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using AutoMapper;
|
||||
using DMS.Application.DTOs;
|
||||
using DMS.Application.Interfaces;
|
||||
using DMS.Core.Enums;
|
||||
using DMS.Core.Models;
|
||||
using DMS.WPF.Interfaces;
|
||||
using DMS.WPF.ItemViewModel;
|
||||
|
||||
namespace DMS.WPF.Services.ViewService;
|
||||
|
||||
public class VariableTableViewService : IVariableTableViewService
|
||||
{
|
||||
private readonly IMapper _mapper;
|
||||
private readonly IViewDataService _viewDataService;
|
||||
private readonly IAppCenterService _appCenterService;
|
||||
private readonly IMenuViewService _menuDataService;
|
||||
|
||||
|
||||
|
||||
public VariableTableViewService(IMapper mapper, IViewDataService dataStorageService, IAppCenterService appCenterService,
|
||||
IMenuViewService menuDataService)
|
||||
{
|
||||
_mapper = mapper;
|
||||
_viewDataService = dataStorageService;
|
||||
_appCenterService = appCenterService;
|
||||
_menuDataService = menuDataService;
|
||||
}
|
||||
|
||||
|
||||
public void LoadAllVariableTables()
|
||||
{
|
||||
foreach (var device in _viewDataService.Devices)
|
||||
{
|
||||
foreach (var variableTable in device.Value.VariableTables)
|
||||
{
|
||||
_viewDataService.VariableTables.Add(variableTable.Id,variableTable);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<int> AddVariableTable(VariableTable variableTable,
|
||||
MenuBean menuDto = null, bool isAddDb = false)
|
||||
{
|
||||
if (variableTable == null)
|
||||
return 0;
|
||||
|
||||
if (isAddDb && menuDto != null)
|
||||
{
|
||||
CreateVariableTableWithMenuDto createDto = new CreateVariableTableWithMenuDto();
|
||||
createDto.VariableTable = variableTable;
|
||||
createDto.DeviceId = variableTable.DeviceId;
|
||||
createDto.Menu = menuDto;
|
||||
var resDto = await _appCenterService.VariableTableManagementService.CreateVariableTableAsync(createDto);
|
||||
|
||||
_menuDataService.AddMenuToView(_mapper.Map<MenuItem>(resDto.Menu));
|
||||
return resDto.VariableTable.Id;
|
||||
}
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
public async Task<bool> UpdateVariableTable(VariableTableItem variableTable)
|
||||
{
|
||||
if (variableTable == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var variableTable_mapped = _mapper.Map<VariableTable>(variableTable);
|
||||
if (await _appCenterService.VariableTableManagementService.UpdateVariableTableAsync(variableTable_mapped) > 0)
|
||||
{
|
||||
// 更新数据库后会自动更新内存,无需额外操作
|
||||
|
||||
var menu = _viewDataService.Menus.FirstOrDefault(m =>
|
||||
m.MenuType == MenuType.VariableTableMenu &&
|
||||
m.TargetId == variableTable.Id);
|
||||
if (menu != null)
|
||||
{
|
||||
menu.Header = variableTable.Name;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteVariableTable(VariableTableItem variableTable, bool isDeleteDb = false)
|
||||
{
|
||||
if (variableTable == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isDeleteDb)
|
||||
{
|
||||
if (!await _appCenterService.VariableTableManagementService.DeleteVariableTableAsync(variableTable.Id))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// 删除与当前变量表关联的所有变量
|
||||
foreach (var variable in variableTable.Variables)
|
||||
{
|
||||
_viewDataService.Variables.Remove(variable.Id);
|
||||
}
|
||||
|
||||
// 删除变量表界面相关的菜单
|
||||
|
||||
var variableTableMenu
|
||||
=_viewDataService.Menus.FirstOrDefault(m => m.MenuType == MenuType.VariableTableMenu && m.TargetId == variableTable.Id);
|
||||
_menuDataService.DeleteMenuToView(variableTableMenu);
|
||||
// 删除变量表
|
||||
_viewDataService.VariableTables.Remove(variableTable.Id);
|
||||
variableTable.Device.VariableTables.Remove(variableTable);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user