using HandyControl.Controls; 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; namespace YY.Admin.ViewModels.Supplier; public class SupplierEditDialogViewModel : BaseViewModel, HandyControl.Tools.Extension.IDialogResultable { private readonly ISupplierService _supplierService; private readonly IJeecgDictSyncService _dictSyncService; private bool _result; public bool Result { get => _result; set => SetProperty(ref _result, value); } public Action? CloseAction { get; set; } private MesXslSupplier? _supplier; public MesXslSupplier? Supplier { get => _supplier; set => SetProperty(ref _supplier, value); } public bool IsAddMode => string.IsNullOrWhiteSpace(Supplier?.Id) || (Supplier?.Id?.StartsWith("local-") ?? false); public string DialogTitle => IsAddMode ? "新增供应商" : "编辑供应商"; public ObservableCollection> StatusOptions { get; } = new(); public DelegateCommand SaveCommand { get; } public DelegateCommand CancelCommand { get; } public SupplierEditDialogViewModel(ISupplierService supplierService, IJeecgDictSyncService dictSyncService, IContainerExtension container, IRegionManager regionManager) : base(container, regionManager) { _supplierService = supplierService; _dictSyncService = dictSyncService; SaveCommand = new DelegateCommand(async () => await SaveAsync()); CancelCommand = new DelegateCommand(() => CloseAction?.Invoke()); _ = LoadDictOptionsAsync(); } private async Task LoadDictOptionsAsync() { try { var statusOpts = await _dictSyncService.GetDictOptionsAsync("xslmes_supplier_status"); StatusOptions.Clear(); foreach (var item in statusOpts) StatusOptions.Add(item); if (StatusOptions.Count == 0) { StatusOptions.Add(new KeyValuePair("启用", "0")); StatusOptions.Add(new KeyValuePair("停用", "1")); } } catch { } } public void InitializeForAdd() { Supplier = new MesXslSupplier { Status = "0" }; RaisePropertyChanged(nameof(IsAddMode)); RaisePropertyChanged(nameof(DialogTitle)); } public void InitializeForEdit(MesXslSupplier supplier) { Supplier = new MesXslSupplier { Id = supplier.Id, SupplierCode = supplier.SupplierCode, SupplierName = supplier.SupplierName, SupplierShortName = supplier.SupplierShortName, ErpCode = supplier.ErpCode, Status = supplier.Status, Remark = supplier.Remark, TenantId = supplier.TenantId, }; RaisePropertyChanged(nameof(IsAddMode)); RaisePropertyChanged(nameof(DialogTitle)); } private async Task SaveAsync() { if (Supplier == null) return; if (string.IsNullOrWhiteSpace(Supplier.SupplierCode)) { HandyControl.Controls.MessageBox.Warning("供应商编码不能为空!"); return; } if (string.IsNullOrWhiteSpace(Supplier.SupplierName)) { HandyControl.Controls.MessageBox.Warning("供应商名称不能为空!"); return; } bool ok = IsAddMode ? await _supplierService.AddAsync(Supplier) : await _supplierService.EditAsync(Supplier); if (ok) { if (IsAddMode) { HandyControl.Controls.MessageBox.Success("新增供应商成功!"); } Result = true; CloseAction?.Invoke(); return; } else { HandyControl.Controls.MessageBox.Error("保存供应商失败!"); } } }