Files
shgxtzcjhoudaosaomadayinwpf/RIZO_Application/Modules/RIZO_Application.Modules.ModuleName/ViewModels/PrintControlViewModel.cs

144 lines
4.7 KiB
C#
Raw Normal View History

using linesider_screen_tool;
using System.Collections.Generic;
using System;
using Prism.Events;
using Prism.Regions;
using RIZO_Application.Core;
using RIZO_Application.Core.Mvvm;
using RIZO_Helper.Tools;
using RIZO_Application.Infrastructure.Model;
namespace RIZO_Application.Modules.ModuleName.ViewModels
{
public class PrintControlViewModel : RegionViewModelBase, IDisposable
{
private readonly IEventAggregator _eventAggregator;
private readonly BartenderPrintHelper _printHelper;
private SubscriptionToken _printEventToken;
private bool _isDisposed;
public PrintControlViewModel(
IRegionManager regionManager,
IEventAggregator eventAggregator,
BartenderPrintHelper printHelper)
: base(regionManager)
{
_eventAggregator = eventAggregator;
_printHelper = printHelper;
Initialize();
}
private void Initialize()
{
try
{
_eventAggregator.GetEvent<SystemLogEvent>().Publish($"打印模块初始化开始");
// 订阅打印事件使用UI线程处理保持强引用
_printEventToken = _eventAggregator.GetEvent<PrintEvent>()
.Subscribe(OnPrintRequested, ThreadOption.UIThread, true);
_eventAggregator.GetEvent<SystemLogEvent>().Publish($"打印模块初始化完成");
}
catch (Exception ex)
{
_eventAggregator.GetEvent<SystemLogEvent>().Publish($"打印模块初始化失败: {ex.Message}");
throw;
}
}
private void OnPrintRequested(PrintDto printDto)
{
// 判断是否是打印主站
bool isPrintMain = SiteConfigs.Current.IsPrintMain.Value;
if (!isPrintMain)
{
_eventAggregator.GetEvent<SystemLogEvent>().Publish($"打印请求被忽略:此站点非打印主站");
return;
}
string siteNo = SiteConfigs.Current.SiteName;
if(printDto.SiteNo != siteNo)
{
_eventAggregator.GetEvent<SystemLogEvent>().Publish($"打印请求被忽略:站点号不匹配{printDto.SiteNo}-{siteNo}");
return;
}
if (_isDisposed)
{
_eventAggregator.GetEvent<SystemLogEvent>().Publish($"打印请求被忽略:打印资源已释放");
return;
}
try
{
_eventAggregator.GetEvent<SystemLogEvent>().Publish($"收到打印请求: {printDto.Name}");
// 准备打印参数
var printParameters = new Dictionary<string, string>
{
{ "10", printDto.PartNumber },
{ "11", "02S" },
{ "10000", printDto.WorkOrder },
{ "10002", printDto.Team },
{ "10003", printDto.Sort.ToString() },
{ "10004", printDto.BatchCode },
{ "10005", printDto.Sort.ToString() },
{ "10006", printDto.BatchCode },
{ "10007", printDto.PackageNum.ToString() },
{ "10011", printDto.LabelType.ToString() }
};
// 执行打印
bool printSuccess = _printHelper.PrintLabel(
templatePath: printDto.Path,
subStringValues: printParameters,
copies: 1);
if (printSuccess)
{
_eventAggregator.GetEvent<SystemLogEvent>().Publish($"打印成功: {printDto.Name}");
}
else
{
_eventAggregator.GetEvent<SystemLogEvent>().Publish($"打印失败: {printDto.Name}");
}
}
catch (Exception ex)
{
_eventAggregator.GetEvent<SystemLogEvent>().Publish($"打印过程中发生错误: {ex.Message}");
}
}
public void Destroy()
{
Dispose();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (_isDisposed)
return;
_isDisposed = true;
if (disposing)
{
// 释放托管资源
_printEventToken?.Dispose();
_printHelper?.Dispose();
}
}
~PrintControlViewModel()
{
Dispose(false);
}
}
}