From 6c0087e871a76e508d0dc39fbbaa458938d3d82a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E9=AD=94=E4=BB=99?= <13068499+willowhh@user.noreply.gitee.com> Date: Thu, 10 Apr 2025 13:42:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=BAbar=E6=A8=A1?= =?UTF-8?q?=E6=9D=BF=E6=89=93=E5=8D=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MES/Product/ProWorkorderController.cs | 14 ++++- DOAN.Service/DOAN.Service.csproj | 11 ++++ .../Product/IService/IProWorkorderService.cs | 2 + .../MES/Product/ProWorkorderService.cs | 61 +++++++++++++++++-- 4 files changed, 79 insertions(+), 9 deletions(-) diff --git a/DOAN.Admin.WebApi/Controllers/MES/Product/ProWorkorderController.cs b/DOAN.Admin.WebApi/Controllers/MES/Product/ProWorkorderController.cs index 1c0496b..5e3f601 100644 --- a/DOAN.Admin.WebApi/Controllers/MES/Product/ProWorkorderController.cs +++ b/DOAN.Admin.WebApi/Controllers/MES/Product/ProWorkorderController.cs @@ -21,6 +21,7 @@ using Infrastructure.Converter; using NPOI.HPSF; using System.IO; using System.Web; +using System.Resources; //创建时间:2024-07-16 namespace DOAN.Admin.WebApi.Controllers @@ -386,14 +387,21 @@ namespace DOAN.Admin.WebApi.Controllers //TODO 打印机打印工单 [AllowAnonymous] [HttpGet("print")] - public IActionResult ExportWorkorderPDF(string[] workorderArray) + public async Task ExportWorkorderPDF(string[] workorderArray,string? path) { if(workorderArray==null||workorderArray.Length<1) { throw new CustomException("workorderArray"); } - Task<(string, Stream)> conntext = _ProWorkorderService.ExportPDFByQuestPDFDemo(workorderArray); - return File(conntext.Result.Item2, "application/pdf", HttpUtility.UrlEncode(conntext.Result.Item1)); + if (path==null) + { + path ="./Resources/gxassembly_production_label.btw"; + } + //Task<(string, Stream)> conntext = _ProWorkorderService.ExportPDFByQuestPDFDemo(workorderArray); + var exception = await _ProWorkorderService.PrintTicketsByTemplate(workorderArray,path); + + return (IActionResult)exception; + //return File(conntext.Result.Item2, "application/pdf", HttpUtility.UrlEncode(conntext.Result.Item1)); } diff --git a/DOAN.Service/DOAN.Service.csproj b/DOAN.Service/DOAN.Service.csproj index 0bea15a..e609a5f 100644 --- a/DOAN.Service/DOAN.Service.csproj +++ b/DOAN.Service/DOAN.Service.csproj @@ -8,6 +8,17 @@ 1591 + + + tlbimp + 1 + 10 + d58562c1-e51b-11cf-8941-00a024a9083f + 0 + false + true + + diff --git a/DOAN.Service/MES/Product/IService/IProWorkorderService.cs b/DOAN.Service/MES/Product/IService/IProWorkorderService.cs index f8bd932..c4065b7 100644 --- a/DOAN.Service/MES/Product/IService/IProWorkorderService.cs +++ b/DOAN.Service/MES/Product/IService/IProWorkorderService.cs @@ -10,6 +10,7 @@ using Microsoft.AspNetCore.Mvc; using DOAN.Model.MES.base_; using DOAN.Model.MES.base_.Dto; using SqlSugar; +using Infrastructure; @@ -64,5 +65,6 @@ namespace DOAN.Service.MES.product.IService int WorkOrderLog(string workorder, string log, string Operator); Task<(string, Stream)> ExportPDFByQuestPDFDemo(string[] workorderArray); + Task PrintTicketsByTemplate(string[] workorderArray,string path); } } diff --git a/DOAN.Service/MES/Product/ProWorkorderService.cs b/DOAN.Service/MES/Product/ProWorkorderService.cs index 3c90683..1ad530b 100644 --- a/DOAN.Service/MES/Product/ProWorkorderService.cs +++ b/DOAN.Service/MES/Product/ProWorkorderService.cs @@ -32,6 +32,9 @@ using QuestPDF; using QuestPDF.Fluent; using QuestPDF.Helpers; using QuestPDF.Infrastructure; +using BarTender; +using MathNet.Numerics.RootFinding; +using Infrastructure; namespace DOAN.Service.MES.product @@ -1303,12 +1306,12 @@ namespace DOAN.Service.MES.product /// /// https://www.questpdf.com/ - /// + /// 要打印的工单号 /// /// public async Task<(string, Stream)> ExportPDFByQuestPDFDemo(string[] workorderArray) { - + var dataList = Context.Queryable().Where(it => workorderArray.Contains(it.Workorder)) .ToList(); if (dataList.Count() == 0) @@ -1381,10 +1384,56 @@ namespace DOAN.Service.MES.product return new(fileName, ms); } + /// + /// 根据模板打印工单信息 + /// + /// + /// 模板路径 + /// + public async Task PrintTicketsByTemplate(string[] workorderArray,string path) + { + var dataList = await Context.Queryable().Where(p => workorderArray.Contains(p.Workorder)).ToListAsync(); + if (!dataList.Any()) + { + return new CustomException(204, "未找到匹配的工单数据"); + + } + Application bartenderApp = null; + Format bartenderFormat = null; + try + { + bartenderApp = new Application { Visible = false }; + bartenderFormat = bartenderApp.Formats.Open(path); + // 4. 遍历数据并打印 + foreach (var data in dataList) + { + bartenderFormat.SetNamedSubStringValue("Workorder", data.Workorder); + bartenderFormat.SetNamedSubStringValue("StoveCode", data.StoveCode); + bartenderFormat.SetNamedSubStringValue("PlanNum", data.PlanNum.ToString()); + await Task.Delay(500); + bartenderFormat.PrintOut(false, false); // 静默打印 + } + return new CustomException(200, "标签打印成功"); + } + catch (Exception ex) + { + // 5. 错误处理(记录日志) + Console.WriteLine($"打印标签时出错: {ex.Message}"); + return new CustomException(200, $"打印标签失败: {ex.Message}"); + } + finally + { + // 6. 确保资源释放(逆序关闭) + if (bartenderFormat != null) + { + bartenderFormat.Close(BtSaveOptions.btDoNotSaveChanges); + } + if (bartenderApp != null) + { + bartenderApp.Quit(BtSaveOptions.btDoNotSaveChanges); + } + } + } } - - - - } \ No newline at end of file