Files
qhmes/yy-admin-master/YY.Admin/ViewModels/RubberQuickTestStd/RubberQuickTestStdListViewModel.cs

167 lines
5.8 KiB
C#
Raw Normal View History

using HandyControl.Controls;
using HandyControl.Tools.Extension;
using Prism.Events;
using System.Collections.ObjectModel;
using System.Diagnostics;
using YY.Admin.Core;
using YY.Admin.Core.Entity;
using YY.Admin.Core.Events;
using YY.Admin.Core.Helper;
using YY.Admin.Core.Services;
using YY.Admin.Services.Service;
using YY.Admin.Views.RubberQuickTestStd;
namespace YY.Admin.ViewModels.RubberQuickTestStd;
public class RubberQuickTestStdListViewModel : BaseViewModel
{
private readonly IRubberQuickTestStdService _stdService;
private readonly IJeecgDictSyncService _dictSyncService;
private SubscriptionToken? _changedToken;
private ObservableCollection<MesXslRubberQuickTestStd> _items = new();
public ObservableCollection<MesXslRubberQuickTestStd> Items
{
get => _items;
set => SetProperty(ref _items, value);
}
private long _total;
public long Total { get => _total; set => SetProperty(ref _total, value); }
private int _pageNo = 1;
public int PageNo { get => _pageNo; set => SetProperty(ref _pageNo, value); }
private int _pageSize = 20;
public int PageSize { get => _pageSize; set => SetProperty(ref _pageSize, value); }
private string? _filterStdName;
public string? FilterStdName { get => _filterStdName; set => SetProperty(ref _filterStdName, value); }
private string? _filterRubberMaterialName;
public string? FilterRubberMaterialName { get => _filterRubberMaterialName; set => SetProperty(ref _filterRubberMaterialName, value); }
private string? _filterEnableStatus;
public string? FilterEnableStatus { get => _filterEnableStatus; set => SetProperty(ref _filterEnableStatus, value); }
public ObservableCollection<KeyValuePair<string, string>> EnableStatusOptions { get; } = new();
public DelegateCommand SearchCommand { get; }
public DelegateCommand ResetCommand { get; }
public DelegateCommand<MesXslRubberQuickTestStd> ViewDetailCommand { get; }
public DelegateCommand PrevPageCommand { get; }
public DelegateCommand NextPageCommand { get; }
public RubberQuickTestStdListViewModel(
IRubberQuickTestStdService stdService,
IJeecgDictSyncService dictSyncService,
IContainerExtension container,
IRegionManager regionManager) : base(container, regionManager)
{
_stdService = stdService;
_dictSyncService = dictSyncService;
SearchCommand = new DelegateCommand(async () => { PageNo = 1; await LoadAsync(); });
ResetCommand = new DelegateCommand(async () =>
{
FilterStdName = null;
FilterRubberMaterialName = null;
FilterEnableStatus = null;
PageNo = 1;
await LoadAsync();
});
ViewDetailCommand = new DelegateCommand<MesXslRubberQuickTestStd>(async x => await ShowDetailAsync(x));
PrevPageCommand = new DelegateCommand(async () => { if (PageNo > 1) { PageNo--; await LoadAsync(); } });
NextPageCommand = new DelegateCommand(async () => { if ((long)PageNo * PageSize < Total) { PageNo++; await LoadAsync(); } });
_changedToken = _eventAggregator.GetEvent<RubberQuickTestStdChangedEvent>()
.Subscribe(async _ => await LoadAsync(), ThreadOption.UIThread);
_ = InitializeAsync();
}
private async Task InitializeAsync()
{
try
{
await LoadDictOptionsAsync();
await UIHelper.WaitForRenderAsync();
await LoadAsync();
}
catch (Exception ex)
{
Debug.WriteLine($"快检实验标准列表初始化失败: {ex.Message}");
}
}
private async Task LoadDictOptionsAsync()
{
try
{
var opts = await _dictSyncService.GetDictOptionsAsync("xslmes_rubber_quick_test_std_enable_status", includeAll: true);
EnableStatusOptions.Clear();
foreach (var item in opts) EnableStatusOptions.Add(item);
}
catch
{
EnableStatusOptions.Clear();
EnableStatusOptions.Add(new KeyValuePair<string, string>("全部", ""));
EnableStatusOptions.Add(new KeyValuePair<string, string>("使用中", "1"));
EnableStatusOptions.Add(new KeyValuePair<string, string>("已停用", "0"));
}
}
public async Task LoadAsync()
{
try
{
IsLoading = true;
var result = await _stdService.PageAsync(
PageNo, PageSize, FilterStdName, FilterRubberMaterialName, FilterEnableStatus);
Items = new ObservableCollection<MesXslRubberQuickTestStd>(result.Records);
Total = result.Total;
}
catch (Exception ex)
{
Growl.Error($"加载快检实验标准失败:{ex.Message}");
}
finally
{
IsLoading = false;
}
}
private async Task ShowDetailAsync(MesXslRubberQuickTestStd? row)
{
if (row?.Id == null) return;
try
{
var detail = await _stdService.GetByIdAsync(row.Id);
if (detail == null)
{
Growl.Warning("未找到该实验标准,可能已被删除");
await LoadAsync();
return;
}
await HandyControl.Controls.Dialog.Show<RubberQuickTestStdDetailDialogView>()
.Initialize<RubberQuickTestStdDetailDialogViewModel>(vm => vm.Initialize(detail))
.GetResultAsync<bool>();
}
catch (Exception ex)
{
Growl.Error($"打开详情失败:{ex.Message}");
}
}
protected override void CleanUp()
{
base.CleanUp();
if (_changedToken != null)
{
_eventAggregator.GetEvent<RubberQuickTestStdChangedEvent>().Unsubscribe(_changedToken);
_changedToken = null;
}
}
}