using HandyControl.Tools.Extension; using System.Collections.ObjectModel; using YY.Admin.Core; using YY.Admin.Core.Entity; using YY.Admin.Core.Services; using YY.Admin.Services.Service.MixerMaterialTareStrategy; namespace YY.Admin.ViewModels.RawMaterialEntry; /// /// 拆码明细手动选择「密炼物料皮重策略」弹窗。 /// public class TareStrategyPickerDialogViewModel : BaseViewModel, IDialogResultable { private readonly IMixerMaterialTareStrategyService _tareStrategyService; private string? _materialId; private string? _supplierId; private DateTime? _entryDate; private double? _portionWeight; public ObservableCollection Records { get; } = new(); private MesXslMixerMaterialTareStrategy? _selectedStrategy; public MesXslMixerMaterialTareStrategy? SelectedStrategy { get => _selectedStrategy; set { SetProperty(ref _selectedStrategy, value); ConfirmCommand.RaiseCanExecuteChanged(); RaisePropertyChanged(nameof(SelectedStrategyDisplay)); } } public string SelectedStrategyDisplay => _selectedStrategy == null ? "请选中一条策略后确认,或点「使用自动匹配」" : $"规格:{_selectedStrategy.MaterialSpec ?? "-"} 包装物:{_selectedStrategy.TareWeight:0.###} 托盘:{_selectedStrategy.PalletWeight:0.###}"; private bool _result; public bool Result { get => _result; set => SetProperty(ref _result, value); } public Action? CloseAction { get; set; } public DelegateCommand ConfirmCommand { get; } public DelegateCommand UseAutoMatchCommand { get; } public DelegateCommand CancelCommand { get; } public TareStrategyPickerDialogViewModel( IMixerMaterialTareStrategyService tareStrategyService, IContainerExtension container, IRegionManager regionManager) : base(container, regionManager) { _tareStrategyService = tareStrategyService; ConfirmCommand = new DelegateCommand(ConfirmSelection, () => SelectedStrategy != null); UseAutoMatchCommand = new DelegateCommand(UseAutoMatch); CancelCommand = new DelegateCommand(() => CloseAction?.Invoke()); } public void Initialize( string? materialId, string? supplierId, DateTime? entryDate, double? portionWeight, string? currentStrategyId) { _materialId = materialId; _supplierId = supplierId; _entryDate = entryDate; _portionWeight = portionWeight; _ = LoadAsync(currentStrategyId); } private async Task LoadAsync(string? currentStrategyId) { try { var all = await _tareStrategyService.GetAllForMatchAsync(); var filtered = MixerMaterialTareStrategyMatcher.FilterCandidates( all, _materialId, _supplierId, _entryDate, _portionWeight); Records.Clear(); foreach (var item in filtered) Records.Add(item); if (!string.IsNullOrWhiteSpace(currentStrategyId)) { SelectedStrategy = Records.FirstOrDefault(r => string.Equals(r.Id, currentStrategyId, StringComparison.OrdinalIgnoreCase)) ?? all.FirstOrDefault(r => string.Equals(r.Id, currentStrategyId, StringComparison.OrdinalIgnoreCase)); } } catch { Records.Clear(); } } private void ConfirmSelection() { Result = true; CloseAction?.Invoke(); } private void UseAutoMatch() { SelectedStrategy = null; Result = true; CloseAction?.Invoke(); } }