Files
qhmes/yy-admin-master/YY.Admin/ViewModels/WeightRecord/CustomerPickerDialogViewModel.cs

85 lines
2.6 KiB
C#

using HandyControl.Tools.Extension;
using System.Collections.ObjectModel;
using YY.Admin.Core;
using YY.Admin.Core.Entity;
using YY.Admin.Core.Services;
namespace YY.Admin.ViewModels.WeightRecord;
public class CustomerPickerDialogViewModel : BaseViewModel, IDialogResultable<bool>
{
private readonly ICustomerService _customerService;
private string? _searchText;
public string? SearchText { get => _searchText; set => SetProperty(ref _searchText, value); }
public ObservableCollection<MesXslCustomer> Customers { get; } = new();
private MesXslCustomer? _selectedCustomer;
public MesXslCustomer? SelectedCustomer
{
get => _selectedCustomer;
set
{
SetProperty(ref _selectedCustomer, value);
ConfirmCommand.RaiseCanExecuteChanged();
RaisePropertyChanged(nameof(SelectedCustomerDisplay));
RaisePropertyChanged(nameof(HasSelectedCustomer));
}
}
public string SelectedCustomerDisplay => _selectedCustomer != null
? $"[{_selectedCustomer.CustomerCode}] {_selectedCustomer.CustomerName}"
: "选中客户后点击「确认选择」";
public bool HasSelectedCustomer => _selectedCustomer != null;
private bool _result;
public bool Result { get => _result; set => SetProperty(ref _result, value); }
public Action? CloseAction { get; set; }
public DelegateCommand SearchCommand { get; }
public DelegateCommand ConfirmCommand { get; }
public DelegateCommand CancelCommand { get; }
public CustomerPickerDialogViewModel(
ICustomerService customerService,
IContainerExtension container,
IRegionManager regionManager) : base(container, regionManager)
{
_customerService = customerService;
SearchCommand = new DelegateCommand(async () => await LoadAsync());
ConfirmCommand = new DelegateCommand(Confirm, () => SelectedCustomer != null);
CancelCommand = new DelegateCommand(() => CloseAction?.Invoke());
_ = LoadAsync();
}
private async Task LoadAsync()
{
try
{
IsLoading = true;
var keyword = SearchText?.Trim();
var result = await _customerService.PageAsync(1, 200, customerName: keyword);
Customers.Clear();
foreach (var c in result.Records)
Customers.Add(c);
}
catch
{
Customers.Clear();
}
finally
{
IsLoading = false;
}
}
private void Confirm()
{
if (SelectedCustomer == null) return;
Result = true;
CloseAction?.Invoke();
}
}