using Admin.Core.IService; using Admin.Core.Model.Model_New; using Admin.Core.Model.ViewModels; using Admin.Core.Service; using Aucma.Core.PrintTo.Common; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using CommunityToolkit.Mvvm.Messaging; using FastReport; using FastReport.DevComponents.DotNetBar; using log4net; using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Windows; namespace Aucma.Core.PrintTo.ViewModels { public partial class PrintToDevViewModel : ObservableObject { private static readonly log4net.ILog log = LogManager.GetLogger(typeof(PrintToDevViewModel)); protected readonly IOrderBomInfoServices _orderBomInfoServices; private List items { get; } private Queue BarcodesUnprinted { get; } = new Queue(); private Queue BarcodesPrinted { get; } = new Queue(); private PrintState printState { get; set; } = PrintState.Idle;//打印状态 protected bool isPrint { get; set; } = true;//打印打印标志 public PrintToDevViewModel() { } //public PrintToDevViewModel(string code,int num) //{ // Task.Run(async () => // { // await Print(code,num); // }); //} public PrintToDevViewModel(string productCode, string materialCode, string materialName, int printAmount, string? barCode = null) { ProductCode = productCode; MaterialName = materialName; PrintAmount = printAmount; _orderBomInfoServices = App.ServiceProvider.GetService(); MaxProgress = 100; Progress = 0; //PrintProgress = $"[100/4] B23060512345678910001"; State = "Green"; ErrorNum = 0; printState = PrintState.Printing; isPrint=true; StopOrStart = "暂停"; OperateEnabled = "True"; // 非阻塞打印 Task.Run(async () => { await ExecPrintAsync(productCode, materialCode, materialName, 0); ; }); } #region 选中订单参数 #region 订单编码 private string _orderNo; public string OrderNo { get => _orderNo; set => SetProperty(ref _orderNo, value); } #endregion #region 产品编码 private string _productCode; public string ProductCode { get => _productCode; set => SetProperty(ref _productCode, value); } #endregion #region 物料型号 private string _materialName; public string MaterialName { get => _materialName; set => SetProperty(ref _materialName, value); } #endregion #region 打印条码 private string _barCode; public string BarCode { get => _barCode; set => SetProperty(ref _barCode, value); } #endregion #region 打印数量 private int _printAmount; public int PrintAmount { get => _printAmount; set => SetProperty(ref _printAmount, 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 int _maxProgress; public int MaxProgress { get => _maxProgress; set => SetProperty(ref _maxProgress, value); } #endregion #region 进度条进度 private int _progress; public int Progress { get => _progress; set => SetProperty(ref _progress, value); } #endregion #region 打印状态 private string _state; public string State { get => _state; set => SetProperty(ref _state, value); } #endregion #region 开启、暂停状态按钮 名称 private string _stopOrStart; public string StopOrStart { get => _stopOrStart; set => SetProperty(ref _stopOrStart, value); } #endregion #region 操作打印按钮状态 private string _operateEnabled; public string OperateEnabled { get { return _operateEnabled; } set { _operateEnabled = value; OnPropertyChanged();//属性通知 } } #endregion #endregion #region 暂停打印、关闭界面 [RelayCommand] public void Operate() { System.Windows.Application.Current.Dispatcher.Invoke((Action)(() => { if (printState == PrintState.Completetd) { isPrint = false; StopOrStart = "已完成"; MessageBox.Show("打印已完成", "系统提醒"); return; } if (isPrint&& printState == PrintState.Printing) { if (MessageBox.Show("确定要暂停打印吗?", "系统提醒", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes) { isPrint = false; printState = PrintState.Paused; StopOrStart = "继续"; } } else { if (MessageBox.Show("确定要继续打印吗?", "系统提醒", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes) { isPrint = true; printState = PrintState.Printing; State = "Green"; StopOrStart = "暂停"; } } })); } [RelayCommand] public void CloseWindow(object parameter) { var window = parameter as Window; if (window == null) return; // $"还有【{BarcodesUnprinted.Count}】个条码未打印。确定要退出打印?" if (MessageBox.Show("确定要退出打印吗?", "系统提醒", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes) { if (printState == PrintState.Printing) MessageBox.Show("请暂停打印后关闭打印界面吗?", "系统提醒"); printState = PrintState.Stopped; WeakReferenceMessenger.Default.Send("Refresh");//刷新窗口 window.Close(); } } #endregion #region 执行打印 /// /// 执行打印 /// /// /// /// /// public async Task ExecPrintAsync(string orderNo, string materialCode, string materialName, int planAmount) { string printer = "ZDesigner ZT411-300dpi ZPL"; var boxList = await _orderBomInfoServices.QueryAsync(d => d.ParentId == orderNo&&d.MaterialName.Contains("箱体")); if (boxList.Count == 0) { MessageBox.Show("无法查询打印条码数据", "系统提醒"); return; } var list = await _orderBomInfoServices.QueryAsync(d => d.ParentId == orderNo && d.MaterialName.Contains("内胆")); if (list.Count == 0) { MessageBox.Show("无法查询打印条码数据", "系统提醒"); return; } list.ForEach(d => BarcodesUnprinted.Enqueue(d)); await Print(printer); } #endregion #region 打印 public Task Print(string printer) { try { int printNum = BarcodesUnprinted.Count;//打印数 int i = 0; MaxProgress = printNum; ErrorNum = 0; while (BarcodesUnprinted.Count > 0) { if (printState == PrintState.Paused) { SetPaused(); continue; } if (printState == PrintState.Completetd) break; try { var item = BarcodesUnprinted.Peek(); ////对接打印机 //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.Order_No", orderNo);//订单码 //barReport.SetParameterValue("BoxBarData.Order_Material_Name", materialName);//物料名称 //barReport.SetParameterValue("BoxBarData.Bar_Code", barCode);//二维码 ////barReport.Prepare(); ////string BarPath = System.Environment.CurrentDirectory + @"\pdf\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf"; ////barReport.Export(new PDFExport(), BarPath); //barReport.Print(); if (item.MaterialName.Contains("箱体")) { Console.WriteLine(item.MaterialName); PrintProgress = $"[{printNum}/{i}] B{DateTime.Now.ToString("yyMMdd")}1234567890000{i + 1}"; Console.WriteLine($"B{DateTime.Now.ToString("yyMMdd")}{item.MaterialCode}{i + 1}"); } if (item.MaterialName.Contains("内胆")) { Console.WriteLine(item.MaterialName); PrintProgress = $"[{printNum}/{i}] B{DateTime.Now.ToString("yyMMdd")}{item.MaterialCode}0000{i + 1}"; Console.WriteLine($"L{DateTime.Now.ToString("yyMMdd")}{item.MaterialCode}0000{i + 1}"); } BarcodesPrinted.Enqueue(BarcodesUnprinted.Dequeue()); i++; Progress = i; Thread.Sleep(1000); } catch (Exception ex) { log.Error($"打印出错:{ex.Message}"); ErrorNum++; } } //打印完成 SetCompletetd(); return Task.FromResult(true); } catch (Exception ex) { log.Error($"打印出错:{ex.Message}"); return Task.FromResult(false); } } #endregion #region 暂停 /// /// 暂停 /// private void SetPaused() { printState = PrintState.Paused; State = "yellow"; StopOrStart = "继续"; isPrint = false; } #endregion #region 完成 /// /// 暂停 /// private void SetCompletetd() { printState = PrintState.Completetd; State = "Green"; StopOrStart = "已完成"; isPrint = false; OperateEnabled = "True"; } #endregion } }