You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

353 lines
12 KiB
C#

using Admin.Core.Common;
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<OrderBomInfo> items { get; }
private Queue<OrderBomInfo> BarcodesUnprinted { get; } = new Queue<OrderBomInfo>();
private Queue<OrderBomInfo> BarcodesPrinted { get; } = new Queue<OrderBomInfo>();
private PrintState printState { get; set; } = PrintState.Idle;//打印状态
protected bool isPrint { get; set; } = true;//打印打印标志
public PrintToDevViewModel() { }
public PrintToDevViewModel(string productCode, string materialCode, string materialName, int printAmount, string? barCode = null)
{
_orderBomInfoServices = App.ServiceProvider.GetService<IOrderBomInfoServices>();
ProductCode = productCode;
MaterialName = materialName;
PrintAmount = printAmount;
Progress = 0;
State = "Green";
ErrorNum = 0;
printState = PrintState.Printing;
isPrint=true;
SuspendEnabled = "False";
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 => _operateEnabled;
set => SetProperty(ref _operateEnabled, value);
}
#endregion
#region 操作暂停按钮状态
private string _suspendEnabled;
public string SuspendEnabled
{
get => _suspendEnabled;
set => SetProperty(ref _suspendEnabled, value);
}
#endregion
#endregion
#region 打印操作
[RelayCommand]
public void StartOperate()
{
printState = PrintState.Printing;
SuspendEnabled = "True";
OperateEnabled = "False";
State = "Green";
if (isPrint)//首次打印
{
isPrint = false;
// 非阻塞打印
Task.Run(async () =>
{
await Print();
});
}
}
#region 暂停
[RelayCommand]
public void SuspendOperate()
{
if (MessageBox.Show("确定要暂停打印吗?", "系统提醒", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes)
{
isPrint = false;
printState = PrintState.Paused;
}
}
#endregion
#region 关闭当前界面
/// <summary>
/// 关闭当前界面
/// </summary>
/// <param name="parameter"></param>
[RelayCommand]
public void CloseWindow(object parameter)
{
var window = parameter as Window;
if (window == null) return;
isPrint = true;
if (printState == PrintState.Completetd)
{
WeakReferenceMessenger.Default.Send<string>("Refresh");//刷新窗口
window.Close();
return;
}
if (MessageBox.Show("确定要退出打印吗?", "系统提醒", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes)
{
printState = PrintState.Stopped;
WeakReferenceMessenger.Default.Send<string>("Refresh");//刷新窗口
window.Close();
}
}
#endregion
#endregion
#region 执行打印
/// <summary>
/// 执行打印
/// </summary>
/// <param name="planCode"></param>
/// <param name="orderNo"></param>
/// <param name="materialName"></param>
/// <param name="planAmount"></param>
public async Task ExecPrintAsync(string orderNo, string materialCode, string materialName, int planAmount)
{
string printer = Appsettings.app("Printer", "PrinterName");
var boxList = await _orderBomInfoServices.QueryAsync(d => d.ParentId == orderNo&&d.MaterialName.Contains("箱体"));
if (boxList.Count == 0)
{
MaxProgress = boxList.Count;
MessageBox.Show("无法查询打印条码数据", "系统提醒");
return;
}
var list = await _orderBomInfoServices.QueryAsync(d => d.ParentId == orderNo && d.MaterialName.Contains("内胆"));
if (list.Count == 0)
{
MaxProgress = list.Count;
MessageBox.Show("无法查询打印条码数据", "系统提醒");
return;
}
list.ForEach(d => BarcodesUnprinted.Enqueue(d));
}
#endregion
#region 打印
public Task Print()
{
try
{
string printer = Appsettings.app("Printer", "PrinterName");
int printNum = BarcodesUnprinted.Count;//打印数
int i = 0;
MaxProgress = printNum;
ErrorNum = 0;
while (BarcodesUnprinted.Count > 0)
{
if (printState == PrintState.Paused)
{
SetPaused();
Thread.Sleep(1 * 1000);
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.Id", item);
//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")}item.MaterialCode{i + 1}";
Console.WriteLine($"B{DateTime.Now.ToString("yyMMdd")}{item.MaterialCode}{i + 1}");
}
if (item.MaterialName.Contains("内胆"))
{
Console.WriteLine(item.MaterialName);
PrintProgress = $"[{printNum}/{i}] L{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);
}
finally
{
isPrint = true;
}
}
#endregion
#region 暂停
/// <summary>
/// 暂停
/// </summary>
private void SetPaused()
{
printState = PrintState.Paused;
State = "yellow";
isPrint = false;
SuspendEnabled = "False";
OperateEnabled = "True";
}
#endregion
#region 完成
/// <summary>
/// 暂停
/// </summary>
private void SetCompletetd()
{
printState = PrintState.Completetd;
State = "Green";
StopOrStart = "已完成";
isPrint = false;
OperateEnabled = "True";
}
#endregion
}
}