using HandyControl.Controls; 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; namespace YY.Admin.ViewModels.MixingProductionPlan; public class MixingProductionPlanListViewModel : BaseViewModel { private readonly IMixingProductionPlanService _planService; private SubscriptionToken? _changedToken; 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 DateTime? _filterPlanDateFrom; public DateTime? FilterPlanDateFrom { get => _filterPlanDateFrom; set => SetProperty(ref _filterPlanDateFrom, value); } private DateTime? _filterPlanDateTo; public DateTime? FilterPlanDateTo { get => _filterPlanDateTo; set => SetProperty(ref _filterPlanDateTo, value); } private string? _filterMachineName; public string? FilterMachineName { get => _filterMachineName; set => SetProperty(ref _filterMachineName, value); } private int? _filterShiftFlag; public int? FilterShiftFlag { get => _filterShiftFlag; set => SetProperty(ref _filterShiftFlag, value); } private string? _filterPlanNo; public string? FilterPlanNo { get => _filterPlanNo; set => SetProperty(ref _filterPlanNo, value); } private string? _filterMaterialName; public string? FilterMaterialName { get => _filterMaterialName; set => SetProperty(ref _filterMaterialName, value); } public ObservableCollection> ShiftOptions { get; } = new() { new KeyValuePair("全部", null), new KeyValuePair("早班", 1), new KeyValuePair("中班", 2), new KeyValuePair("晚班", 3) }; public DelegateCommand SearchCommand { get; } public DelegateCommand ResetCommand { get; } public DelegateCommand PrevPageCommand { get; } public DelegateCommand NextPageCommand { get; } public MixingProductionPlanListViewModel( IMixingProductionPlanService planService, IContainerExtension container, IRegionManager regionManager) : base(container, regionManager) { _planService = planService; SearchCommand = new DelegateCommand(async () => { PageNo = 1; await LoadAsync(); }); ResetCommand = new DelegateCommand(async () => { FilterPlanDateFrom = null; FilterPlanDateTo = null; FilterMachineName = null; FilterShiftFlag = null; FilterPlanNo = null; FilterMaterialName = 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(); } }); _changedToken = _eventAggregator.GetEvent() .Subscribe(async _ => await LoadAsync(), ThreadOption.UIThread); _ = 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; var result = await _planService.PageAsync( PageNo, PageSize, FilterPlanDateFrom, FilterPlanDateTo, FilterMachineName, FilterShiftFlag, FilterPlanNo, FilterMaterialName); Items = new ObservableCollection(result.Records); Total = result.Total; } catch (Exception ex) { Growl.Error($"加载密炼计划失败:{ex.Message}"); } finally { IsLoading = false; } } protected override void CleanUp() { base.CleanUp(); if (_changedToken != null) { _eventAggregator.GetEvent().Unsubscribe(_changedToken); _changedToken = null; } } }