233 lines
9.1 KiB
C#
233 lines
9.1 KiB
C#
using Avalonia.Controls;
|
|
using Avalonia.Controls.Notifications;
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using CommunityToolkit.Mvvm.Input;
|
|
using Cowain.Base.Helpers;
|
|
using Cowain.Base.ViewModels;
|
|
using Ke.Bee.Localization.Localizer.Abstractions;
|
|
using Microsoft.Extensions.Logging;
|
|
using Plugin.Cowain.Wcs.IServices;
|
|
using Plugin.Cowain.Wcs.Views;
|
|
using System.Collections.ObjectModel;
|
|
using Ursa.Controls;
|
|
|
|
namespace Plugin.Cowain.Wcs.ViewModels
|
|
{
|
|
public partial class ProcessManagementViewModel : PageViewModelBase
|
|
{
|
|
[ObservableProperty]
|
|
private ObservableCollection<ProcessViewModel>? _processList;
|
|
[ObservableProperty]
|
|
private int _totals;
|
|
[ObservableProperty]
|
|
private int _pageSize;
|
|
[ObservableProperty]
|
|
private int _pageIndex;
|
|
|
|
private readonly ILocalizer _l;
|
|
private IProcessService _processService;
|
|
private IStationService _stationService;
|
|
private IProcessFlowService _processFlowService;
|
|
private readonly ILogger<ProcessManagementViewModel> _logger;
|
|
public ProcessManagementViewModel(ILocalizer localizer, IProcessService processService, IStationService stationService, IProcessFlowService processFlowService, ILogger<ProcessManagementViewModel> logger)
|
|
{
|
|
PageSize = 20;
|
|
_l = localizer;
|
|
_processService = processService;
|
|
_stationService = stationService;
|
|
_processFlowService = processFlowService;
|
|
_logger = logger;
|
|
ProcessList = new ObservableCollection<ProcessViewModel>();
|
|
// 异步调用刷新
|
|
RefreshCommand.ExecuteAsync(1);
|
|
}
|
|
|
|
[RelayCommand]
|
|
private async Task RefreshAsync(int pageIndex)
|
|
{
|
|
var (data, count) = await _processService.GetAllAsync(pageIndex, PageSize);
|
|
Totals = count;
|
|
if (count > 0)
|
|
{
|
|
ProcessList?.Clear();
|
|
foreach (var item in data)
|
|
{
|
|
ProcessList?.Add(item);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void Add()
|
|
{
|
|
|
|
ProcessList?.Add(new ProcessViewModel { Name = "请命名", Enable = true });
|
|
|
|
}
|
|
|
|
[RelayCommand]
|
|
private async Task EditAsync(ProcessViewModel? process)
|
|
{
|
|
if (process == null)
|
|
{
|
|
return;
|
|
}
|
|
var options = new DialogOptions()
|
|
{
|
|
Title = _l["ProcessManagement.Dialog.Title"],
|
|
ShowInTaskBar = true,
|
|
IsCloseButtonVisible = true,
|
|
StartupLocation = WindowStartupLocation.CenterScreen,
|
|
Button = DialogButton.OKCancel,
|
|
CanDragMove = true,
|
|
CanResize = true,
|
|
};
|
|
var stations = await _stationService.GetAllAsync();
|
|
ProcessEditDialogViewModel deviceModel = new ProcessEditDialogViewModel(stations, process, _processFlowService);
|
|
var deviceEditDialog = await Dialog.ShowCustomModal<ProcessEditDialog, ProcessEditDialogViewModel, bool>(deviceModel, options: options);
|
|
if (deviceEditDialog)
|
|
{
|
|
var add = await _processService.UpdateAsync(process);
|
|
if (add.IsSuccess)
|
|
{
|
|
// 获取当前流程
|
|
var flows = await _processFlowService.GetAllAsync();
|
|
var existingFlows = flows.Where(f => f.ProcessId == process.Id);
|
|
var newFlows = deviceModel.Connections.Select(x => x.ProcessFlow).ToList();
|
|
|
|
var flowsToUpdate = new List<ProcessFlowViewModel>();
|
|
var flowsToAdd = new List<ProcessFlowViewModel>();
|
|
|
|
foreach (var newFlow in newFlows)
|
|
{
|
|
var existFlow = existingFlows.FirstOrDefault(f =>
|
|
f.FromStationId1 == newFlow.FromStationId1 &&
|
|
f.ToStationId1 == newFlow.ToStationId1 &&
|
|
f.FromStationId2 == newFlow.FromStationId2 &&
|
|
f.ToStationId2 == newFlow.ToStationId2);
|
|
|
|
if (existFlow != null)
|
|
{
|
|
// 更新已有流程
|
|
newFlow.Id = existFlow.Id;
|
|
flowsToUpdate.Add(newFlow);
|
|
}
|
|
else
|
|
{
|
|
// 新增流程
|
|
flowsToAdd.Add(newFlow);
|
|
}
|
|
}
|
|
|
|
// 先更新已存在的流程
|
|
if (flowsToUpdate.Count > 0)
|
|
{
|
|
var updateResult = await _processFlowService.UpdateAsync(process.Id, flowsToUpdate);
|
|
if (!updateResult.IsSuccess)
|
|
{
|
|
NotificationHelper.ShowNormal(NotificationType.Error, _l["ProcessManagement.Edit.Error"] + ":" + updateResult.ErrorMessage);
|
|
return;
|
|
}
|
|
}
|
|
|
|
// 再新增不存在的流程
|
|
if (flowsToAdd.Count > 0)
|
|
{
|
|
var addResult = await _processFlowService.AddAsync(process.Id, flowsToAdd);
|
|
if (!addResult.IsSuccess)
|
|
{
|
|
NotificationHelper.ShowNormal(NotificationType.Error, _l["ProcessManagement.Edit.Error"] + ":" + addResult.ErrorMessage);
|
|
return;
|
|
}
|
|
}
|
|
NotificationHelper.ShowNormal(NotificationType.Success, _l["ProcessManagement.Edit.Success"]);
|
|
_logger.LogInformation($"修改工艺流程成功:工艺->{process.Name}");
|
|
}
|
|
else
|
|
{
|
|
_logger.LogError($"修改工艺流程失败:工艺->{process.Name},错误信息->{add.ErrorMessage}");
|
|
NotificationHelper.ShowNormal(NotificationType.Error, _l["ProcessManagement.Edit.Error"] + ":" + add.ErrorMessage);
|
|
}
|
|
}
|
|
}
|
|
|
|
[RelayCommand]
|
|
private async Task DeleteAsync(ProcessViewModel? process)
|
|
{
|
|
if (process == null)
|
|
{
|
|
return;
|
|
}
|
|
var result = await MessageBox.ShowOverlayAsync(_l["DeleteDialog"], _l["Message.Info.Title"], button: MessageBoxButton.YesNo);
|
|
if (result != MessageBoxResult.Yes)
|
|
{
|
|
return;
|
|
}
|
|
try
|
|
{
|
|
var deleteDevice = await _processService.DeleteAsync(process.Id);
|
|
if (deleteDevice.IsSuccess)
|
|
{
|
|
_logger.LogInformation($"删除工艺:{process.Name}");
|
|
NotificationHelper.ShowNormal(NotificationType.Success, _l["ProcessManagement.Delete.Success"]);
|
|
ProcessList?.Remove(process);
|
|
}
|
|
else
|
|
{
|
|
NotificationHelper.ShowNormal(NotificationType.Error, _l["ProcessManagement.Delete.Error"] + ":" + deleteDevice.ErrorMessage);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
NotificationHelper.ShowNormal(NotificationType.Error, _l["ProcessManagement.Delete.Error"] + ":" + ex.Message);
|
|
}
|
|
}
|
|
|
|
[RelayCommand]
|
|
private async Task SaveAsync(ProcessViewModel? process)
|
|
{
|
|
if (process == null)
|
|
{
|
|
return;
|
|
|
|
}
|
|
var result = await MessageBox.ShowOverlayAsync(_l["SaveDialog"], _l["Message.Info.Title"], button: MessageBoxButton.YesNo);
|
|
if (result != MessageBoxResult.Yes)
|
|
{
|
|
return;
|
|
}
|
|
if (process.Id == 0)
|
|
{
|
|
var add = await _processService.AddAsync(process);
|
|
if (add.IsSuccess)
|
|
{
|
|
_logger.LogInformation($"新增工艺:{process.Name}");
|
|
await RefreshAsync(1);
|
|
NotificationHelper.ShowNormal(NotificationType.Success, _l["ProcessManagement.Add.Success"]);
|
|
}
|
|
else
|
|
{
|
|
NotificationHelper.ShowNormal(NotificationType.Error, _l["ProcessManagement.Add.Error"]);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
|
|
var update = await _processService.UpdateAsync(process);
|
|
if (update.IsSuccess)
|
|
{
|
|
_logger.LogInformation($"更改工艺:{process.Name}");
|
|
NotificationHelper.ShowNormal(NotificationType.Success, _l["ProcessManagement.Update.Success"]);
|
|
}
|
|
else
|
|
{
|
|
NotificationHelper.ShowNormal(NotificationType.Error, _l["ProcessManagement.Update.Error"] + ":" + update.ErrorMessage);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|