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_Application.Infrastructure.Model; using System.Linq; using System.Reflection; 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().Publish($"打印模块初始化开始"); // 订阅打印事件,使用UI线程处理,保持强引用 _printEventToken = _eventAggregator.GetEvent() .Subscribe(OnPrintRequested, ThreadOption.UIThread, true); _eventAggregator.GetEvent().Publish($"打印模块初始化完成"); } catch (Exception ex) { _eventAggregator.GetEvent().Publish($"打印模块初始化失败: {ex.Message}"); throw; } } private void OnPrintRequested(PrintDto printDto) { // 判断是否是打印主站 bool isPrintMain = SiteConfigs.Current.IsPrintMain.Value; if (!isPrintMain) { _eventAggregator.GetEvent().Publish($"打印请求被忽略:此站点非打印主站"); return; } string siteNo = SiteConfigs.Current.SiteName; if(printDto.SiteNo != siteNo) { _eventAggregator.GetEvent().Publish($"打印请求被忽略:站点号不匹配{printDto.SiteNo}-{siteNo}"); return; } if (_isDisposed) { _eventAggregator.GetEvent().Publish($"打印请求被忽略:打印资源已释放"); return; } try { _eventAggregator.GetEvent().Publish($"收到打印请求: {printDto.Name}"); var printParameters = new Dictionary(); Type type = printDto.GetType(); PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo property in properties) { string value = property.GetValue(printDto)?.ToString(); string dictionaryKey = property.Name; printParameters[dictionaryKey] = value; } // 1.准备全部打印参数表 //var printParameters = new Dictionary //{ // { "10", printDto.PartNumber }, // { "11", "02S" }, // {"12",printDto.Sepcification }, // {"13",null }, // {"14",null }, // {"15",null }, // {"16",null }, // { "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() }, // {"10013",null }, //}; var subString = _printHelper.GetNamedSubStrings(templatePath:printDto.Path).Result; var Intersect = printParameters.Where(x => subString.Contains(x.Key)).ToDictionary( x=>x.Key,x=>x.Value ); // 执行打印 bool printSuccess = _printHelper.PrintLabel( templatePath: printDto.Path, subStringValues: Intersect, copies: 1); //bool printSuccess = barTenderPrinter.PrintLabel(templatePath: printDto.Path, // subStringValues: Intersect, // copies: 1); if (printSuccess) { _eventAggregator.GetEvent().Publish($"打印成功: {printDto.Name}"); } else { _eventAggregator.GetEvent().Publish($"打印失败: {printDto.Name}"); } } catch (Exception ex) { _eventAggregator.GetEvent().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); } } }