216 lines
7.9 KiB
C#
216 lines
7.9 KiB
C#
using HandyControl.Controls;
|
|
using HandyControl.Tools.Extension;
|
|
using Prism.Events;
|
|
using System.Collections.ObjectModel;
|
|
using System.Diagnostics;
|
|
using System.Windows;
|
|
using YY.Admin.Core;
|
|
using YY.Admin.Core.Entity;
|
|
using YY.Admin.Core.Events;
|
|
using YY.Admin.Core.Services;
|
|
using YY.Admin.Services.Service;
|
|
using YY.Admin.Views.Supplier;
|
|
|
|
namespace YY.Admin.ViewModels.Supplier;
|
|
|
|
public class SupplierListViewModel : BaseViewModel
|
|
{
|
|
private readonly ISupplierService _supplierService;
|
|
private readonly IJeecgDictSyncService _dictSyncService;
|
|
private SubscriptionToken? _supplierChangedToken;
|
|
private SubscriptionToken? _syncConflictToken;
|
|
|
|
public ObservableCollection<MesXslSupplier> Suppliers { get; set; } = [];
|
|
public ObservableCollection<KeyValuePair<string, string>> StatusOptions { get; } = [];
|
|
public long Total { get; set; }
|
|
public int PageNo { get; set; } = 1;
|
|
public int PageSize { get; set; } = 20;
|
|
public string? FilterSupplierCode { get; set; }
|
|
public string? FilterSupplierName { get; set; }
|
|
public string? FilterSupplierShortName { get; set; }
|
|
public string? FilterErpCode { get; set; }
|
|
public string? FilterStatus { get; set; }
|
|
|
|
public DelegateCommand SearchCommand { get; }
|
|
public DelegateCommand ResetCommand { get; }
|
|
public DelegateCommand AddCommand { get; }
|
|
public DelegateCommand<MesXslSupplier> EditCommand { get; }
|
|
public DelegateCommand<MesXslSupplier> DeleteCommand { get; }
|
|
public DelegateCommand<MesXslSupplier> ToggleStatusCommand { get; }
|
|
public DelegateCommand PrevPageCommand { get; }
|
|
public DelegateCommand NextPageCommand { get; }
|
|
|
|
public SupplierListViewModel(
|
|
ISupplierService supplierService,
|
|
IJeecgDictSyncService dictSyncService,
|
|
IContainerExtension container,
|
|
IRegionManager regionManager)
|
|
: base(container, regionManager)
|
|
{
|
|
_supplierService = supplierService;
|
|
_dictSyncService = dictSyncService;
|
|
SearchCommand = new DelegateCommand(async () =>
|
|
{
|
|
PageNo = 1;
|
|
RaisePropertyChanged(nameof(PageNo));
|
|
await LoadAsync();
|
|
});
|
|
ResetCommand = new DelegateCommand(async () =>
|
|
{
|
|
FilterSupplierCode = FilterSupplierName = FilterSupplierShortName = FilterErpCode = FilterStatus = null;
|
|
PageNo = 1;
|
|
RaisePropertyChanged(nameof(PageNo));
|
|
await LoadAsync();
|
|
});
|
|
AddCommand = new DelegateCommand(async () => await ShowAddDialogAsync());
|
|
EditCommand = new DelegateCommand<MesXslSupplier>(async s => await ShowEditDialogAsync(s));
|
|
DeleteCommand = new DelegateCommand<MesXslSupplier>(async s => await DeleteAsync(s));
|
|
ToggleStatusCommand = new DelegateCommand<MesXslSupplier>(async s => await ToggleStatusAsync(s));
|
|
PrevPageCommand = new DelegateCommand(async () =>
|
|
{
|
|
if (PageNo > 1)
|
|
{
|
|
PageNo--;
|
|
RaisePropertyChanged(nameof(PageNo));
|
|
await LoadAsync();
|
|
}
|
|
});
|
|
NextPageCommand = new DelegateCommand(async () =>
|
|
{
|
|
if ((long)PageNo * PageSize < Total)
|
|
{
|
|
PageNo++;
|
|
RaisePropertyChanged(nameof(PageNo));
|
|
await LoadAsync();
|
|
}
|
|
});
|
|
|
|
_supplierChangedToken = _eventAggregator.GetEvent<SupplierChangedEvent>()
|
|
.Subscribe(async p => await OnSupplierChangedAsync(p), ThreadOption.UIThread);
|
|
_syncConflictToken = _eventAggregator.GetEvent<SyncConflictEvent>()
|
|
.Subscribe(OnSyncConflict, ThreadOption.UIThread);
|
|
|
|
_ = InitializeAsync();
|
|
}
|
|
|
|
private async Task InitializeAsync()
|
|
{
|
|
try
|
|
{
|
|
var statusOpts = await _dictSyncService.GetDictOptionsAsync("xslmes_supplier_status", includeAll: true);
|
|
StatusOptions.Clear();
|
|
foreach (var item in statusOpts) StatusOptions.Add(item);
|
|
if (StatusOptions.Count == 0)
|
|
{
|
|
StatusOptions.Add(new("全部", ""));
|
|
StatusOptions.Add(new("启用", "0"));
|
|
StatusOptions.Add(new("停用", "1"));
|
|
}
|
|
}
|
|
catch { }
|
|
await LoadAsync();
|
|
}
|
|
|
|
public async Task LoadAsync()
|
|
{
|
|
var result = await _supplierService.PageAsync(
|
|
PageNo, PageSize,
|
|
FilterSupplierCode, FilterSupplierName,
|
|
FilterSupplierShortName, FilterErpCode, FilterStatus);
|
|
Suppliers = new ObservableCollection<MesXslSupplier>(result.Records);
|
|
Total = result.Total;
|
|
RaisePropertyChanged(nameof(Suppliers));
|
|
RaisePropertyChanged(nameof(Total));
|
|
}
|
|
|
|
private async Task OnSupplierChangedAsync(SupplierChangedPayload payload)
|
|
{
|
|
if (payload.Action == "edit" && !string.IsNullOrWhiteSpace(payload.SupplierId))
|
|
{
|
|
await RefreshSingleAsync(payload.SupplierId!);
|
|
}
|
|
else
|
|
{
|
|
await LoadAsync();
|
|
}
|
|
}
|
|
|
|
private async Task RefreshSingleAsync(string supplierId)
|
|
{
|
|
try
|
|
{
|
|
var updated = await _supplierService.GetByIdAsync(supplierId);
|
|
if (updated == null) return;
|
|
var idx = Suppliers.ToList().FindIndex(s => string.Equals(s.Id, supplierId, StringComparison.OrdinalIgnoreCase));
|
|
if (idx >= 0)
|
|
{
|
|
Suppliers[idx] = updated;
|
|
RaisePropertyChanged(nameof(Suppliers));
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.WriteLine($"[供应商] 单条刷新失败: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
private void OnSyncConflict(SyncConflictPayload payload)
|
|
{
|
|
if (!string.Equals(payload.EntityName, "供应商", StringComparison.Ordinal)) return;
|
|
|
|
var parts = new List<string>();
|
|
if (payload.PushedCount > 0)
|
|
parts.Add($"已同步 {payload.PushedCount} 条本地改动到服务器");
|
|
if (payload.NewRecordsPushed > 0)
|
|
parts.Add($"已上传 {payload.NewRecordsPushed} 条本地新增记录");
|
|
if (payload.ConflictCount > 0)
|
|
parts.Add($"{payload.ConflictCount} 条记录与服务器版本冲突,已保留服务器版本");
|
|
|
|
if (parts.Count == 0) return;
|
|
|
|
var message = string.Join("\n", parts);
|
|
if (payload.ConflictCount > 0)
|
|
Growl.Warning(message);
|
|
else
|
|
Growl.Success(message);
|
|
}
|
|
|
|
private async Task ShowAddDialogAsync()
|
|
{
|
|
var ok = await HandyControl.Controls.Dialog.Show<SupplierEditDialogView>()
|
|
.Initialize<SupplierEditDialogViewModel>(vm => vm.InitializeForAdd())
|
|
.GetResultAsync<bool>();
|
|
if (ok) await LoadAsync();
|
|
}
|
|
|
|
private async Task ShowEditDialogAsync(MesXslSupplier supplier)
|
|
{
|
|
if (supplier == null) return;
|
|
var ok = await HandyControl.Controls.Dialog.Show<SupplierEditDialogView>()
|
|
.Initialize<SupplierEditDialogViewModel>(vm => vm.InitializeForEdit(supplier))
|
|
.GetResultAsync<bool>();
|
|
if (ok) await LoadAsync();
|
|
}
|
|
|
|
private async Task DeleteAsync(MesXslSupplier supplier)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(supplier?.Id)) return;
|
|
if (System.Windows.MessageBox.Show(
|
|
$"确定删除供应商【{supplier.SupplierName}】?",
|
|
"确认删除",
|
|
System.Windows.MessageBoxButton.OKCancel) != System.Windows.MessageBoxResult.OK) return;
|
|
if (await _supplierService.DeleteAsync(supplier.Id)) await LoadAsync();
|
|
}
|
|
|
|
private async Task ToggleStatusAsync(MesXslSupplier supplier)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(supplier?.Id)) return;
|
|
var newStatus = supplier.Status == "1" ? "0" : "1";
|
|
if (await _supplierService.UpdateStatusAsync(supplier.Id, newStatus))
|
|
{
|
|
supplier.Status = newStatus;
|
|
RaisePropertyChanged(nameof(Suppliers));
|
|
}
|
|
}
|
|
}
|