using Admin.Core.Common; using Admin.Core.IService; using Aucma.Core.PrintTo.Common; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using CommunityToolkit.Mvvm.Messaging; using FastReport; using log4net; using Microsoft.Extensions.DependencyInjection; using NPOI.HSSF.Record.Chart; using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using System.Windows; namespace Aucma.Core.PrintTo.ViewModels { /** * * 特殊条码打印 * * */ public partial class SpecialPrintToViewModel : ObservableObject { private static readonly log4net.ILog log = LogManager.GetLogger(typeof(SpecialPrintToViewModel)); private Queue BarcodesUnprinted { get; } = new Queue(); private Queue BarcodesPrinted { get; } = new Queue(); protected readonly IOrderBomInfoServices _orderBomInfoServices; private PrintState printState { get; set; } = PrintState.Idle;//打印状态 protected bool isPrint { get; set; } = true;//打印打印标志 public SpecialPrintToViewModel(string barCode, int num) { _orderBomInfoServices = App.ServiceProvider.GetService(); State = "Green"; ErrorNum = 0; printState = PrintState.Printing; BarCode = barCode; Num = num; MaxProgress = num; Progress = 0; SuspendEnabled = "False"; OperateEnabled = "True"; // 非阻塞打印 Task.Run(async () => { await ExecPrintAsync(barCode, num); ; }); } #region 执行打印 /// /// 执行打印 /// /// 二维码 /// 打印数量 /// public Task ExecPrintAsync(string barCode,int num) { for (int i = 0; i < num; i++) { BarcodesUnprinted.Enqueue(barCode); } return Task.CompletedTask; } #endregion #region 打印 [RelayCommand] public void StartOperate() { printState= PrintState.Printing; SuspendEnabled = "True"; OperateEnabled = "False"; State = "Green"; if (isPrint)//首次打印 { isPrint= false; // 非阻塞打印 Task.Run(async () => { await Print(); }); } } public async Task Print() { try { string printer = Appsettings.app("Printer", "PrinterName"); int printNum = BarcodesUnprinted.Count;//打印数 int i = 1; while (BarcodesUnprinted.Count > 0) { if (printState == PrintState.Paused) { SetPaused(); Thread.Sleep(1 * 1000); continue; } if (printState == PrintState.Completetd) break; try { var item = BarcodesUnprinted.Peek(); #region 打印机 //对接打印机 Report barReport = new Report(); barReport.Load(System.Environment.CurrentDirectory + @"\Report\MaterialBar.frx"); // 设置打印机和打印选项 barReport.PrintSettings.ShowDialog = false; // 是否打开打印机选择框 barReport.PrintSettings.Printer = printer; // 设置打印机名称 barReport.PrintSettings.Copies = 1; // 设置打印份数 //barReport.PrintSettings.PageRange = "1-3"; // 设置打印页范围 barReport.SetParameterValue("BoxBarData.Id", item); barReport.SetParameterValue("BoxBarData.Order_No", item);//订单码 barReport.SetParameterValue("BoxBarData.Order_Material_Name", item);//物料名称 barReport.SetParameterValue("BoxBarData.Bar_Code", item);//二维码 //barReport.Prepare(); //string BarPath = System.Environment.CurrentDirectory + @"\pdf\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf"; //barReport.Export(new PDFExport(), BarPath); barReport.Print(); #endregion PrintProgress = $"[{printNum}/{i}] {item}"; BarcodesPrinted.Enqueue(BarcodesUnprinted.Dequeue()); Thread.Sleep(1000); Progress = i; i++; } catch (Exception ex) { log.Error($"打印出错:{ex.Message}"); ErrorNum++; } finally { isPrint = true; } } //打印完成 SetCompletetd(); SuspendEnabled = "False"; OperateEnabled = "False"; } catch (Exception ex) { log.Error($"打印出错:{ex.Message}"); } return await Task.FromResult(true); } #endregion #region 暂停 [RelayCommand] private void SuspendOperate() { printState = PrintState.Paused; State = "yellow"; isPrint = false; SuspendEnabled = "False"; OperateEnabled = "True"; } #endregion #region 暂停 /// /// 暂停 /// private void SetPaused() { printState = PrintState.Paused; State = "yellow"; isPrint = false; } #endregion #region 完成 /// /// 暂停 /// private void SetCompletetd() { printState = PrintState.Completetd; State = "Green"; isPrint = false; OperateEnabled = "True"; } #endregion #region 关闭当前界面 /// /// 关闭当前界面 /// /// [RelayCommand] public void CloseWindow(object parameter) { var window = parameter as Window; if (window == null) return; isPrint = true; if (printState == PrintState.Completetd) { WeakReferenceMessenger.Default.Send("Refresh");//刷新窗口 window.Close(); return; } if (MessageBox.Show("确定要退出打印吗?", "系统提醒", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes) { printState = PrintState.Stopped; WeakReferenceMessenger.Default.Send("Refresh");//刷新窗口 window.Close(); } } #endregion #region 属性 #region 二维码值 private string _barCode; public string BarCode { get => _barCode; set => SetProperty(ref _barCode, value); } #endregion #region 数量 private int _num; public int Num { get => _num; set => SetProperty(ref _num, value); } #endregion #region 进度条最大值 private int _maxProgress; public int MaxProgress { get => _maxProgress; set => SetProperty(ref _maxProgress, value); } #endregion #region 异常数量 private int _errorNum; public int ErrorNum { get => _errorNum; set => SetProperty(ref _errorNum, value); } #endregion #region 打印进度 private string _printProgress; public string PrintProgress { get => _printProgress; set => SetProperty(ref _printProgress, value); } #endregion #region 打印状态 private string _state; public string State { get => _state; set => SetProperty(ref _state, value); } #endregion #region 进度条进度 private int _progress; public int Progress { get => _progress; set => SetProperty(ref _progress, value); } #endregion #region 操作打印按钮状态 private string _operateEnabled; public string OperateEnabled { get => _operateEnabled; set => SetProperty(ref _operateEnabled, value); } #endregion #region 操作暂停按钮状态 private string _suspendEnabled; public string SuspendEnabled { get => _suspendEnabled; set => SetProperty(ref _suspendEnabled, value); } #endregion #endregion } }