Files
qhmes/yy-admin-master/YY.Admin/ViewModels/RawMaterialEntry/RawMaterialEntryListViewModel.cs

209 lines
8.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using HandyControl.Controls;
using Prism.Events;
using Prism.Navigation;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Windows;
using YY.Admin.Core;
using YY.Admin.Core.Events;
using YY.Admin.Core.Helper;
using YY.Admin.Core.Entity;
using YY.Admin.Core.Services;
using YY.Admin.Event;
using YY.Admin.Services.Service;
using YY.Admin.Module;
namespace YY.Admin.ViewModels.RawMaterialEntry;
public class RawMaterialEntryListViewModel : BaseViewModel
{
private readonly IRawMaterialEntryService _entryService;
private readonly IJeecgDictSyncService _dictSyncService;
private SubscriptionToken? _entryChangedToken;
private SubscriptionToken? _syncConflictToken;
private ObservableCollection<MesXslRawMaterialEntry> _entries = new();
public ObservableCollection<MesXslRawMaterialEntry> Entries
{
get => _entries;
set => SetProperty(ref _entries, 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 string? _filterBarcode;
public string? FilterBarcode { get => _filterBarcode; set => SetProperty(ref _filterBarcode, value); }
private string? _filterBatchNo;
public string? FilterBatchNo { get => _filterBatchNo; set => SetProperty(ref _filterBatchNo, value); }
private string? _filterBillNo;
public string? FilterBillNo { get => _filterBillNo; set => SetProperty(ref _filterBillNo, value); }
private string? _filterMaterialName;
public string? FilterMaterialName { get => _filterMaterialName; set => SetProperty(ref _filterMaterialName, value); }
private string? _filterSupplierName;
public string? FilterSupplierName { get => _filterSupplierName; set => SetProperty(ref _filterSupplierName, value); }
public DelegateCommand SearchCommand { get; }
public DelegateCommand ResetCommand { get; }
public DelegateCommand AddCommand { get; }
public DelegateCommand<MesXslRawMaterialEntry> EditCommand { get; }
public DelegateCommand<MesXslRawMaterialEntry> DeleteCommand { get; }
public DelegateCommand PrevPageCommand { get; }
public DelegateCommand NextPageCommand { get; }
public RawMaterialEntryListViewModel(
IRawMaterialEntryService entryService,
IJeecgDictSyncService dictSyncService,
IContainerExtension container,
IRegionManager regionManager) : base(container, regionManager)
{
_entryService = entryService;
_dictSyncService = dictSyncService;
SearchCommand = new DelegateCommand(async () => { PageNo = 1; await LoadAsync(); });
ResetCommand = new DelegateCommand(async () =>
{
FilterBarcode = null; FilterBatchNo = null; FilterBillNo = null;
FilterMaterialName = null; FilterSupplierName = null;
PageNo = 1;
await LoadAsync();
});
AddCommand = new DelegateCommand(OpenAddPage);
//update-begin---author:jiangxh ---date:20260731 for【原料入场】编辑改为打开与新增相同的操作页-----------
EditCommand = new DelegateCommand<MesXslRawMaterialEntry>(OpenEditPage);
//update-end---author:jiangxh ---date:20260731 for【原料入场】编辑改为打开与新增相同的操作页-----------
DeleteCommand = new DelegateCommand<MesXslRawMaterialEntry>(async e => await DeleteAsync(e));
PrevPageCommand = new DelegateCommand(async () => { if (PageNo > 1) { PageNo--; await LoadAsync(); } });
NextPageCommand = new DelegateCommand(async () => { if ((long)PageNo * PageSize < Total) { PageNo++; await LoadAsync(); } });
_entryChangedToken = _eventAggregator
.GetEvent<RawMaterialEntryChangedEvent>()
.Subscribe(async p => await OnEntryChangedAsync(p), ThreadOption.UIThread);
_syncConflictToken = _eventAggregator.GetEvent<SyncConflictEvent>()
.Subscribe(OnSyncConflict, ThreadOption.UIThread);
_ = InitializeAsync();
}
private async Task OnEntryChangedAsync(RawMaterialEntryChangedPayload payload)
{
if (payload.Action == "edit" && !string.IsNullOrWhiteSpace(payload.EntryId))
await RefreshSingleAsync(payload.EntryId!);
else
await LoadAsync();
}
private async Task RefreshSingleAsync(string entryId)
{
try
{
var updated = await _entryService.GetByIdAsync(entryId);
if (updated == null) return;
var idx = Entries.ToList().FindIndex(e => string.Equals(e.Id, entryId, StringComparison.OrdinalIgnoreCase));
if (idx >= 0) Entries[idx] = updated;
}
catch (Exception ex) { Debug.WriteLine($"[原料入场] 单条刷新失败: {ex.Message}"); }
}
private void OnSyncConflict(SyncConflictPayload payload)
{
if (!string.Equals(payload.EntityName, "原料入场", StringComparison.OrdinalIgnoreCase)) 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 InitializeAsync()
{
try
{
await UIHelper.WaitForRenderAsync();
await LoadAsync();
}
catch (Exception ex) { Debug.WriteLine($"原料入场列表初始化失败: {ex.Message}"); }
}
public async Task LoadAsync()
{
try
{
IsLoading = true;
var result = await _entryService.PageAsync(PageNo, PageSize,
FilterBarcode, FilterBatchNo, FilterBillNo, FilterMaterialName, FilterSupplierName);
Entries = new ObservableCollection<MesXslRawMaterialEntry>(result.Records);
Total = result.Total;
}
catch (Exception ex) { Growl.Error($"加载原料入场记录失败:{ex.Message}"); }
finally { IsLoading = false; }
}
private void OpenAddPage()
{
_eventAggregator.GetEvent<TabSourceSelectedEvent>().Publish(new TabSource
{
Name = "新增原料入场记录",
Icon = "\ue7ce",
ViewName = "RawMaterialEntryOperationView"
});
}
//update-begin---author:jiangxh ---date:20260731 for【原料入场】编辑改为打开与新增相同的操作页-----------
private void OpenEditPage(MesXslRawMaterialEntry? entry)
{
if (entry == null || string.IsNullOrWhiteSpace(entry.Id)) return;
var label = string.IsNullOrWhiteSpace(entry.Barcode) ? entry.Id : entry.Barcode;
_eventAggregator.GetEvent<TabSourceSelectedEvent>().Publish(new TabSource
{
Name = $"编辑原料入场记录 {label}",
Icon = "\ue7ce",
ViewName = "RawMaterialEntryOperationView",
NavigationParameter = new NavigationParameters { { "editEntryId", entry.Id } }
});
}
//update-end---author:jiangxh ---date:20260731 for【原料入场】编辑改为打开与新增相同的操作页-----------
private async Task DeleteAsync(MesXslRawMaterialEntry entry)
{
if (entry?.Id == null) return;
var confirm = System.Windows.MessageBox.Show(
$"确定删除该原料入场记录(条码:{entry.Barcode})?此操作不可恢复!",
"确认删除", MessageBoxButton.OKCancel, MessageBoxImage.Question);
if (confirm != System.Windows.MessageBoxResult.OK) return;
var ok = await _entryService.DeleteAsync(entry.Id);
if (ok) { Growl.Success("删除成功!"); await LoadAsync(); }
else Growl.Error("删除失败!");
}
protected override void CleanUp()
{
base.CleanUp();
if (_entryChangedToken != null)
{
_eventAggregator.GetEvent<RawMaterialEntryChangedEvent>().Unsubscribe(_entryChangedToken);
_entryChangedToken = null;
}
if (_syncConflictToken != null)
{
_eventAggregator.GetEvent<SyncConflictEvent>().Unsubscribe(_syncConflictToken);
_syncConflictToken = null;
}
}
}