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? _alarms = new(); [ObservableProperty] private DateTime? _startDate; [ObservableProperty] private DateTime? _endDate; [ObservableProperty] private IList? _selectedGroups = new AvaloniaList(); [ObservableProperty] private IList? _selectedLevels = new AvaloniaList(); [ObservableProperty] private ObservableCollection? _alarmLevels; [ObservableProperty] private ObservableCollection? _alarmGroups; [RelayCommand] private async Task LoadedAsync() { //获取所有设备列表 var alarmLevels = await _levelService.GetAllAsync(); AlarmLevels = new ObservableCollection(alarmLevels); var alarmGroups = await _alarmGroupService.GetAllAsync(); AlarmGroups = new ObservableCollection(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? groups = SelectedGroups?.Select(g => g.Id).ToList(); List? 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.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? GetFileTypes() { return [ new FilePickerFileType("Excel"){ Patterns=["*.xlsx"]} ]; } }