using System.Collections.ObjectModel; using System.Diagnostics; using HandyControl.Controls; using YY.Admin.Core; using YY.Admin.Core.Helper; using YY.Admin.Services.Service.EquipmentDb; namespace YY.Admin.ViewModels.McsActMill; public class McsActMillListViewModel : BaseViewModel { private readonly IMcsActMillService _service; private ObservableCollection _items = new(); public ObservableCollection 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? _filterActName; public string? FilterActName { get => _filterActName; set => SetProperty(ref _filterActName, value); } private string? _filterActAddrText; public string? FilterActAddrText { get => _filterActAddrText; set => SetProperty(ref _filterActAddrText, value); } public DelegateCommand SearchCommand { get; } public DelegateCommand ResetCommand { get; } public DelegateCommand PrevPageCommand { get; } public DelegateCommand NextPageCommand { get; } public McsActMillListViewModel( IMcsActMillService service, IContainerExtension container, IRegionManager regionManager) : base(container, regionManager) { _service = service; SearchCommand = new DelegateCommand(async () => { PageNo = 1; await LoadAsync(); }); ResetCommand = new DelegateCommand(async () => { FilterActName = null; FilterActAddrText = null; PageNo = 1; await LoadAsync(); }); PrevPageCommand = new DelegateCommand(async () => { if (PageNo > 1) { PageNo--; await LoadAsync(); } }); NextPageCommand = new DelegateCommand(async () => { if ((long)PageNo * PageSize < Total) { PageNo++; await LoadAsync(); } }); _ = InitializeAsync(); } private async Task InitializeAsync() { try { await UIHelper.WaitForRenderAsync(); await LoadAsync(); } catch (Exception ex) { Debug.WriteLine($"开炼机动作中间表初始化失败: {ex.Message}"); } } public async Task LoadAsync() { try { IsLoading = true; int? actAddr = null; if (!string.IsNullOrWhiteSpace(FilterActAddrText) && int.TryParse(FilterActAddrText.Trim(), out var parsed)) { actAddr = parsed; } var result = await _service.PageAsync(PageNo, PageSize, FilterActName, actAddr); Items = new ObservableCollection(result.Records); Total = result.Total; } catch (Exception ex) { Growl.Error($"加载开炼机动作中间表失败:{ex.Message}"); } finally { IsLoading = false; } } }