Files
qhmes/yy-admin-master/YY.Admin/ViewModels/Print/PrintTemplateListViewModel.cs

202 lines
6.3 KiB
C#
Raw Normal View History

using System.Collections.ObjectModel;
using System.Windows;
using Prism.Commands;
using Prism.Events;
using YY.Admin.Core;
using YY.Admin.Core.Entity;
using YY.Admin.Core.Events;
using YY.Admin.Core.Services;
using YY.Admin.Services.Service.Print;
using YY.Admin.Views.Print;
namespace YY.Admin.ViewModels.Print;
public class PrintTemplateListViewModel : BaseViewModel
{
private readonly IPrintTemplateService _printTemplateService;
private readonly IEventAggregator _eventAggregator;
private SubscriptionToken? _changeToken;
private List<PrintTemplate> _allTemplates = new();
public ObservableCollection<PrintTemplate> Templates { get; } = new();
private string _statusMessage = string.Empty;
public string StatusMessage
{
get => _statusMessage;
set => SetProperty(ref _statusMessage, value);
}
private string? _filterCode;
public string? FilterCode
{
get => _filterCode;
set => SetProperty(ref _filterCode, value);
}
private string? _filterName;
public string? FilterName
{
get => _filterName;
set => SetProperty(ref _filterName, value);
}
private string? _filterCategory;
public string? FilterCategory
{
get => _filterCategory;
set => SetProperty(ref _filterCategory, value);
}
public DelegateCommand SearchCommand { get; }
public DelegateCommand ResetCommand { get; }
public DelegateCommand<PrintTemplate> PreviewCommand { get; }
public PrintTemplateListViewModel(
IPrintTemplateService printTemplateService,
IEventAggregator eventAggregator,
IContainerExtension container,
IRegionManager regionManager) : base(container, regionManager)
{
_printTemplateService = printTemplateService;
_eventAggregator = eventAggregator;
SearchCommand = new DelegateCommand(ApplyFilter);
PreviewCommand = new DelegateCommand<PrintTemplate>(ShowPreview);
ResetCommand = new DelegateCommand(() =>
{
FilterCode = null;
FilterName = null;
FilterCategory = null;
ApplyFilter();
});
_changeToken = _eventAggregator
.GetEvent<PrintTemplateChangedEvent>()
.Subscribe(_ => { RefreshSilentlyAsync().ConfigureAwait(false); }, ThreadOption.UIThread);
// 先用缓存立即填充,再后台静默刷新
ShowCached();
_ = RefreshSilentlyAsync();
}
private void ShowCached()
{
var cached = _printTemplateService.GetCached();
if (cached.Count == 0) return;
_allTemplates = cached.ToList();
ApplyFilter();
UpdateStatus();
}
private async Task RefreshSilentlyAsync()
{
try
{
var list = await _printTemplateService.RefreshCacheAsync().ConfigureAwait(false);
System.Windows.Application.Current.Dispatcher.Invoke(() =>
{
_allTemplates = list.ToList();
ApplyFilter();
UpdateStatus();
});
}
catch
{
// 静默失败:保留当前缓存内容不动,不显示错误
var cached = _printTemplateService.GetCached();
if (cached.Count > 0 && _allTemplates.Count == 0)
{
System.Windows.Application.Current.Dispatcher.Invoke(() =>
{
_allTemplates = cached.ToList();
ApplyFilter();
UpdateStatus();
});
}
}
}
private void ApplyFilter()
{
IEnumerable<PrintTemplate> result = _allTemplates;
if (!string.IsNullOrWhiteSpace(FilterCode))
result = result.Where(t => (t.TemplateCode ?? string.Empty)
.Contains(FilterCode, StringComparison.OrdinalIgnoreCase));
if (!string.IsNullOrWhiteSpace(FilterName))
result = result.Where(t => (t.TemplateName ?? string.Empty)
.Contains(FilterName, StringComparison.OrdinalIgnoreCase));
if (!string.IsNullOrWhiteSpace(FilterCategory))
result = result.Where(t => (t.Category ?? string.Empty)
.Contains(FilterCategory, StringComparison.OrdinalIgnoreCase));
var filtered = result.ToList();
// 原地差量更新,避免滚动位置重置和闪烁
for (int i = Templates.Count - 1; i >= 0; i--)
{
if (!filtered.Any(t => t.Id == Templates[i].Id))
Templates.RemoveAt(i);
}
for (int i = 0; i < filtered.Count; i++)
{
var item = filtered[i];
var existingIdx = -1;
for (int j = 0; j < Templates.Count; j++)
{
if (Templates[j].Id == item.Id) { existingIdx = j; break; }
}
if (existingIdx < 0)
Templates.Insert(i, item);
else
{
if (existingIdx != i) Templates.Move(existingIdx, i);
Templates[i] = item;
}
}
}
private void UpdateStatus()
{
var hasFilter = !string.IsNullOrWhiteSpace(FilterCode)
|| !string.IsNullOrWhiteSpace(FilterName)
|| !string.IsNullOrWhiteSpace(FilterCategory);
StatusMessage = hasFilter
? $"筛选结果 {Templates.Count} / {_allTemplates.Count} 个"
: _allTemplates.Count > 0
? $"共 {_allTemplates.Count} 个模板"
: "暂无模板";
}
private void ShowPreview(PrintTemplate template)
{
if (template == null) return;
ShowPreviewAsync(template);
}
private async Task ShowPreviewAsync(PrintTemplate template)
{
// 列表缓存可能不含 templateJson大字段按需通过 queryByCode 单独拉取
var json = template.TemplateJson;
if (string.IsNullOrWhiteSpace(json) || json == "{}")
{
try
{
var full = await _printTemplateService.GetByCodeAsync(template.TemplateCode ?? "");
json = full?.TemplateJson;
}
catch { /* 保持 json 为 null预览窗口显示"尚未设计" */ }
}
var win = new PrintPreviewWindow(template, json)
{
Owner = Application.Current.MainWindow
};
win.Show();
}
}