Files
WCS/Plugins/Driver/Cowain.Driver/ViewModels/AlarmHistoryViewModel.cs
2026-03-02 09:08:20 +08:00

140 lines
4.7 KiB
C#

using Avalonia.Collections;
using Avalonia.Controls.Notifications;
using Avalonia.Media;
using Avalonia.Platform.Storage;
using Avalonia.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Cowain.Base.Helpers;
using Cowain.Base.ViewModels;
using Ke.Bee.Localization.Localizer.Abstractions;
using Plugin.Cowain.Driver.Abstractions;
using Plugin.Cowain.Driver.IServices;
using Plugin.Cowain.Driver.Services;
using SixLabors.Fonts.Tables.AdvancedTypographic;
using System.Collections.ObjectModel;
namespace Plugin.Cowain.Driver.ViewModels;
public partial class AlarmHistoryViewModel : PageViewModelBase
{
private readonly ILocalizer _l;
private readonly IAlarmService _alarmService;
private readonly IAlarmGroupService _alarmGroupService;
private readonly IAlarmLevelService _levelService;
public AlarmHistoryViewModel(ILocalizer localizer, IAlarmService alarmService, IAlarmGroupService alarmGroupService, IAlarmLevelService alarmLevelService)
{
PageSize = 40;
_l = localizer;
_alarmService = alarmService;
_alarmGroupService = alarmGroupService;
_levelService = alarmLevelService;
}
[ObservableProperty]
private int _totals;
[ObservableProperty]
private int _pageSize;
[ObservableProperty]
private int _pageIndex;
[ObservableProperty]
private ObservableCollection<AlarmViewModel>? _alarms = new();
[ObservableProperty] private DateTime? _startDate;
[ObservableProperty] private DateTime? _endDate;
[ObservableProperty] private IList<AlarmGroupViewModel>? _selectedGroups = new AvaloniaList<AlarmGroupViewModel>();
[ObservableProperty] private IList<AlarmLevelViewModel>? _selectedLevels = new AvaloniaList<AlarmLevelViewModel>();
[ObservableProperty]
private ObservableCollection<AlarmLevelViewModel>? _alarmLevels;
[ObservableProperty]
private ObservableCollection<AlarmGroupViewModel>? _alarmGroups;
[RelayCommand]
private async Task LoadedAsync()
{
//获取所有设备列表
var alarmLevels = await _levelService.GetAllAsync();
AlarmLevels = new ObservableCollection<AlarmLevelViewModel>(alarmLevels);
var alarmGroups = await _alarmGroupService.GetAllAsync();
AlarmGroups = new ObservableCollection<AlarmGroupViewModel>(alarmGroups);
}
[RelayCommand]
private async Task RefreshAsync()
{
if (StartDate == null)
{
NotificationHelper.ShowNormal(NotificationType.Warning, _l["AlarmHistory.Warning.StartDateIsNull"]);
return;
}
if (EndDate == null)
{
NotificationHelper.ShowNormal(NotificationType.Warning, _l["AlarmHistory.Warning.EndDateIsNull"]);
return;
}
List<int>? groups = SelectedGroups?.Select(g => g.Id).ToList();
List<int>? levels = SelectedLevels?.Select(g => g.Id).ToList();
var (data, count) = await _alarmService.GetAlarmAsync(PageIndex, PageSize, StartDate, EndDate, groups, levels);
Alarms?.Clear();
Totals = count;
if (count > 0)
{
foreach (var item in data)
{
// 设置报警组名称和等级名称
item.GroupName = AlarmGroups?.FirstOrDefault(g => g.Id == item.Group)?.Name ?? string.Empty;
item.LevelName = AlarmLevels?.FirstOrDefault(g => g.Id == item.Level)?.Name ?? string.Empty;
//设置颜色
if (item.Status)
{
item.Color = AlarmLevels?.FirstOrDefault(g => g.Id == item.Level)?.Color ?? string.Empty;
}
else
{
item.Color = Colors.Gray.ToString();
}
Alarms?.Add(item);
}
}
}
[RelayCommand]
private async Task ExportAsync()
{
var saveDialog = await FileDialogHelper.SaveFileDialogAsync(GetFileTypes());
if (!saveDialog.IsSuccess)
{
return;
}
if (Alarms == null)
{
return;
}
var result = await ExcelHelper<AlarmViewModel>.ExportExcelAsync(Alarms.ToList(), saveDialog.Data!.Path.LocalPath);
if (result.IsSuccess)
{
NotificationHelper.ShowNormal(NotificationType.Success, _l["AlarmHistory.Export.Success"]);
}
else
{
NotificationHelper.ShowNormal(NotificationType.Error, _l["AlarmHistory.Export.Error"] + ":" + result.ErrorMessage);
}
}
List<FilePickerFileType>? GetFileTypes()
{
return
[
new FilePickerFileType("Excel"){ Patterns=["*.xlsx"]}
];
}
}