|
|
|
@ -11,6 +11,8 @@ using FastReport;
|
|
|
|
|
using FastReport.DevComponents.DotNetBar;
|
|
|
|
|
using log4net;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using NPOI.SS.Formula.Functions;
|
|
|
|
|
using StackExchange.Profiling.Internal;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
@ -26,33 +28,44 @@ namespace Aucma.Core.PrintTo.ViewModels
|
|
|
|
|
private static readonly log4net.ILog log = LogManager.GetLogger(typeof(PrintToDevViewModel));
|
|
|
|
|
|
|
|
|
|
protected readonly IOrderBomInfoServices _orderBomInfoServices;
|
|
|
|
|
protected readonly IPrintBarCodeServices _printBarCodeServices;
|
|
|
|
|
|
|
|
|
|
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() { }
|
|
|
|
|
protected bool isPrint { get; set; } = true;//打印标志
|
|
|
|
|
|
|
|
|
|
public PrintToDevViewModel(string productCode, string materialCode, string materialName, int printAmount, string? barCode = null)
|
|
|
|
|
#region 构造函数
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 构造函数
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="productCode">产品编码</param>
|
|
|
|
|
/// <param name="materialCode">物料编码</param>
|
|
|
|
|
/// <param name="materialName">型号简码</param>
|
|
|
|
|
/// <param name="printAmount">打印数量</param>
|
|
|
|
|
public PrintToDevViewModel(string productCode, string materialCode, string materialName, int printAmount)
|
|
|
|
|
{
|
|
|
|
|
_orderBomInfoServices = App.ServiceProvider.GetService<IOrderBomInfoServices>();
|
|
|
|
|
_printBarCodeServices = App.ServiceProvider.GetService<IPrintBarCodeServices>();
|
|
|
|
|
ProductCode = productCode;
|
|
|
|
|
MaterialName = materialName;
|
|
|
|
|
MaterialCode= materialCode;
|
|
|
|
|
PrintAmount = printAmount;
|
|
|
|
|
Progress = 0;
|
|
|
|
|
MaxProgress = printAmount;//最大数
|
|
|
|
|
State = "Green";
|
|
|
|
|
ErrorNum = 0;
|
|
|
|
|
ErrorNum = 0;//异常
|
|
|
|
|
printState = PrintState.Printing;
|
|
|
|
|
isPrint=true;
|
|
|
|
|
SuspendEnabled = "False";
|
|
|
|
|
OperateEnabled = "True";
|
|
|
|
|
SuspendEnabled = "False";//暂停打印
|
|
|
|
|
OperateEnabled = "True";//打印
|
|
|
|
|
// 非阻塞打印
|
|
|
|
|
Task.Run(async () =>
|
|
|
|
|
{
|
|
|
|
|
await ExecPrintAsync(productCode, materialCode, materialName, 0);
|
|
|
|
|
await ExecPrintAsync(productCode, materialCode, printAmount);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region 选中订单参数
|
|
|
|
|
|
|
|
|
@ -74,16 +87,16 @@ namespace Aucma.Core.PrintTo.ViewModels
|
|
|
|
|
public string ProductCode { get => _productCode; set => SetProperty(ref _productCode, value); }
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region 物料型号
|
|
|
|
|
#region 型号简码
|
|
|
|
|
private string _materialName;
|
|
|
|
|
|
|
|
|
|
public string MaterialName { get => _materialName; set => SetProperty(ref _materialName, value); }
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region 打印条码
|
|
|
|
|
private string _barCode;
|
|
|
|
|
#region 物料编码
|
|
|
|
|
private string _materialCode;
|
|
|
|
|
|
|
|
|
|
public string BarCode { get => _barCode; set => SetProperty(ref _barCode, value); }
|
|
|
|
|
public string MaterialCode { get => _materialCode; set => SetProperty(ref _materialCode, value); }
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region 打印数量
|
|
|
|
@ -214,47 +227,36 @@ namespace Aucma.Core.PrintTo.ViewModels
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region 执行打印
|
|
|
|
|
#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)
|
|
|
|
|
/// <param name="orderNo">订单号</param>
|
|
|
|
|
/// <param name="materialCode">物料码</param>
|
|
|
|
|
/// <param name="planAmount">打印数量</param>
|
|
|
|
|
public async Task ExecPrintAsync(string orderNo, string materialCode, int planAmount)
|
|
|
|
|
{
|
|
|
|
|
string printer = Appsettings.app("Printer", "PrinterName");
|
|
|
|
|
|
|
|
|
|
var boxList = await _orderBomInfoServices.QueryAsync(d => d.ParentId == orderNo&&d.MaterialName.Contains("箱体"));
|
|
|
|
|
if (boxList.Count == 0)
|
|
|
|
|
var obj = await _orderBomInfoServices.FirstAsync(d => d.MaterialCode == materialCode);
|
|
|
|
|
if (obj == null)
|
|
|
|
|
{
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < planAmount; i++) { BarcodesUnprinted.Enqueue(obj); }
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region 打印
|
|
|
|
|
|
|
|
|
|
public Task Print()
|
|
|
|
|
public async Task<bool> Print()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string printer = Appsettings.app("Printer", "PrinterName");
|
|
|
|
|
int printNum = BarcodesUnprinted.Count;//打印数
|
|
|
|
|
int i = 0;
|
|
|
|
|
MaxProgress = printNum;
|
|
|
|
|
ErrorNum = 0;
|
|
|
|
|
int i = 1;
|
|
|
|
|
while (BarcodesUnprinted.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
if (printState == PrintState.Paused)
|
|
|
|
@ -264,62 +266,89 @@ namespace Aucma.Core.PrintTo.ViewModels
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (printState == PrintState.Completetd) break;
|
|
|
|
|
PrintBarCode printBarCode = new PrintBarCode();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var item = BarcodesUnprinted.Peek();
|
|
|
|
|
string barCode = string.Empty;
|
|
|
|
|
if (item.MaterialName.Contains("U壳"))
|
|
|
|
|
{
|
|
|
|
|
string rowNum = await GetMaxNumAsync(item.MaterialCode,"B");//最大值+1
|
|
|
|
|
barCode = $"B{DateTime.Now.ToString("yyMMdd")}{item.MaterialCode}{rowNum}";
|
|
|
|
|
PrintProgress = $"[{PrintAmount}/{i}] {barCode}";
|
|
|
|
|
printBarCode.MaterialBarcode = barCode;
|
|
|
|
|
printBarCode.PrintBarType = 1;
|
|
|
|
|
Console.WriteLine($"B{DateTime.Now.ToString("yyMMdd")}{item.MaterialCode}{rowNum}");
|
|
|
|
|
}
|
|
|
|
|
if (item.MaterialName.Contains("内胆"))
|
|
|
|
|
{
|
|
|
|
|
string rowNum = await GetMaxNumAsync(item.MaterialCode, "L");//最大值+1
|
|
|
|
|
barCode = $"L{DateTime.Now.ToString("yyMMdd")}{item.MaterialCode}{rowNum}";
|
|
|
|
|
PrintProgress = $"[{PrintAmount}/{i}] {barCode}";
|
|
|
|
|
printBarCode.MaterialBarcode = barCode;
|
|
|
|
|
printBarCode.PrintBarType = 2;//内胆码
|
|
|
|
|
Console.WriteLine($"L{DateTime.Now.ToString("yyMMdd")}{item.MaterialCode}{rowNum}");
|
|
|
|
|
}
|
|
|
|
|
#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"; // 设置打印页范围
|
|
|
|
|
|
|
|
|
|
////对接打印机
|
|
|
|
|
//Report barReport = new Report();
|
|
|
|
|
//barReport.Load(System.Environment.CurrentDirectory + @"\Report\MaterialBar.frx");//打印报表位置
|
|
|
|
|
barReport.SetParameterValue("BoxBarData.Id", barCode);
|
|
|
|
|
barReport.SetParameterValue("BoxBarData.Order_No", item.);//订单码
|
|
|
|
|
barReport.SetParameterValue("BoxBarData.Order_Material_Name", item.MaterialName);//物料简码
|
|
|
|
|
barReport.SetParameterValue("BoxBarData.Bar_Code", barCode);//二维码
|
|
|
|
|
|
|
|
|
|
//// 设置打印机和打印选项
|
|
|
|
|
//barReport.PrintSettings.ShowDialog = false; // 是否打开打印机选择框
|
|
|
|
|
//barReport.PrintSettings.Printer = printer; // 设置打印机名称
|
|
|
|
|
//barReport.PrintSettings.Copies = 1; // 设置打印份数
|
|
|
|
|
// //barReport.PrintSettings.PageRange = "1-3"; // 设置打印页范围
|
|
|
|
|
//barReport.SetParameterValue("BoxBarData.Id", item.barCode);
|
|
|
|
|
//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.Prepare();
|
|
|
|
|
////string BarPath = System.Environment.CurrentDirectory + @"\pdf\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf";
|
|
|
|
|
////barReport.Export(new PDFExport(), BarPath);
|
|
|
|
|
barReport.Print();
|
|
|
|
|
|
|
|
|
|
//barReport.Print();
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
if (item.MaterialName.Contains("箱体"))
|
|
|
|
|
printBarCode.ProductCode = ProductCode;
|
|
|
|
|
printBarCode.MaterialCode = item.MaterialCode;
|
|
|
|
|
printBarCode.MaterialName = item.MaterialName;
|
|
|
|
|
printBarCode.PrintTime = DateTime.Now;
|
|
|
|
|
printBarCode.SupplementMaterial =0;
|
|
|
|
|
if (string.IsNullOrEmpty(printBarCode.MaterialBarcode))
|
|
|
|
|
{
|
|
|
|
|
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}");
|
|
|
|
|
throw new Exception("条码生成异常");//抛出异常,回滚事务,事务退出
|
|
|
|
|
}
|
|
|
|
|
if (item.MaterialName.Contains("内胆"))
|
|
|
|
|
var num= await _printBarCodeServices.AddAsync(printBarCode);
|
|
|
|
|
if (num<=0)
|
|
|
|
|
{
|
|
|
|
|
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}");
|
|
|
|
|
ErrorNum++;
|
|
|
|
|
log.Error($"打印保存出错:{printBarCode.ToJson()}");
|
|
|
|
|
}
|
|
|
|
|
BarcodesPrinted.Enqueue(BarcodesUnprinted.Dequeue());
|
|
|
|
|
i++;
|
|
|
|
|
Progress = i;
|
|
|
|
|
|
|
|
|
|
Thread.Sleep(1000);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
log.Error($"打印出错:{ex.Message}");
|
|
|
|
|
log.Error($"打印出错:{ex.Message}\n{printBarCode.ToJson()}");
|
|
|
|
|
ErrorNum++;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
Progress = i;
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
//打印完成
|
|
|
|
|
SetCompletetd();
|
|
|
|
|
return Task.FromResult(true);
|
|
|
|
|
return await Task.FromResult(true);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
log.Error($"打印出错:{ex.Message}");
|
|
|
|
|
return Task.FromResult(false);
|
|
|
|
|
return await Task.FromResult(false);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
@ -327,6 +356,38 @@ namespace Aucma.Core.PrintTo.ViewModels
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region 获取最大值
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取最大值
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="MaterialCode"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task<string> GetMaxNumAsync(string MaterialCode,string falg)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
List<int> tempList = new List<int>();
|
|
|
|
|
var list = await _printBarCodeServices.QueryAsync(d => d.MaterialBarcode.Contains(MaterialCode) && d.MaterialBarcode.Substring(0, 1).Equals(falg));
|
|
|
|
|
if (list.Count() == 0)
|
|
|
|
|
{
|
|
|
|
|
return "0001";
|
|
|
|
|
}
|
|
|
|
|
list.ForEach(item =>
|
|
|
|
|
{
|
|
|
|
|
int num = Convert.ToInt32(item.MaterialBarcode.Substring(item.MaterialBarcode.Length - 1, 4));
|
|
|
|
|
tempList.Add(num);
|
|
|
|
|
});
|
|
|
|
|
string maxStr = (tempList.Max() + 1).ToString();
|
|
|
|
|
string rowNum = maxStr.PadLeft(4, '0');
|
|
|
|
|
return rowNum;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return "0001";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region 暂停
|
|
|
|
|