From e674b6929ab96b8034532244cb70fbabee5a8fec Mon Sep 17 00:00:00 2001 From: anerx <512464164@qq.com> Date: Thu, 27 Mar 2025 11:48:28 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9E=A5=E5=8A=A0=E7=AE=80=E5=8D=95=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E7=9A=84=E5=AF=BC=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/IReportExportService.cs | 2 + .../Services/ReportExportService.cs | 63 +++++++++++++++++++ Seyounth.Hyosung.Runtime/HyosungRuntime.cs | 2 +- 3 files changed, 66 insertions(+), 1 deletion(-) diff --git a/Seyounth.Hyosung.Data/Services/IReportExportService.cs b/Seyounth.Hyosung.Data/Services/IReportExportService.cs index 7f62c64..e6c1658 100644 --- a/Seyounth.Hyosung.Data/Services/IReportExportService.cs +++ b/Seyounth.Hyosung.Data/Services/IReportExportService.cs @@ -3,6 +3,8 @@ namespace Seyounth.Hyosung.Data.Services; public interface IReportExportService { Task ExportAsync(string trayCode); + + Task ExportSampleAsync(string trayCode); Task ExportNoExportAsync(); diff --git a/Seyounth.Hyosung.Data/Services/ReportExportService.cs b/Seyounth.Hyosung.Data/Services/ReportExportService.cs index 079be16..a4b3e0a 100644 --- a/Seyounth.Hyosung.Data/Services/ReportExportService.cs +++ b/Seyounth.Hyosung.Data/Services/ReportExportService.cs @@ -31,6 +31,69 @@ public class ReportExportService( } } + public async Task ExportSampleAsync(string trayCode) + { + try + { + var tray = await trayService.GetByCode(trayCode); + var variety = await varietyService.GetById(tray.VarietyId); + var yarns = await yarnService.GetYarnsByTrayIdAsync(tray.Id); + await CreateSample(tray, variety, yarns); + await trayService.ExportedAsync(tray.TrayCode); + } + catch (Exception e) + { + logger.LogError(e, $"export report [{trayCode}] error"); + } + } + + private async Task CreateSample(Tray tray, Variety variety, List yarns) + { + try + { + DataTable dt = new DataTable(); + dt.Columns.Add("LOT"); + dt.Columns.Add("DTEX_FILA"); + dt.Columns.Add("总个数/垛"); + dt.Columns.Add("生产时间"); + dt.Columns.Add("机台"); + dt.Columns.Add("班次"); + for (int i = 1; i <= yarns.Count; i++) + { + if (i == 1) + { + dt.Rows.Add(yarns[i - 1].Lot, tray.DtexFila, yarns.Count, yarns[i - 1].ProduceTime, + yarns[i - 1].Machine, yarns[i - 1].WorkShift); + } + else + { + dt.Rows.Add(yarns[i - 1].Lot, tray.DtexFila, "", yarns[i - 1].ProduceTime, + yarns[i - 1].Machine, yarns[i - 1].WorkShift); + } + } + + var path = await GetDirectoryAsync(); + var fileName = $"{variety.Lot}_{tray.Barcode.Split(" ")[1]}_{DateTime.Now:yyyyMMdd}.xlsx"; + var filePath = Path.Combine(path, fileName); + using var workbook = new XLWorkbook(); + var worksheet = workbook.Worksheets.Add(dt); + // 设置数据行字体大小 + var dataRows = worksheet.RowsUsed(); + foreach (var row in dataRows) + { + row.Style.Font.FontSize = 14; + } + + // 自动调整列宽 + worksheet.Columns().AdjustToContents(); + workbook.SaveAs(filePath); + } + catch (Exception e) + { + logger.LogError(e, "export today report error"); + } + } + private async Task Create(Tray tray, Variety variety, List yarns) { using var workbook = new XLWorkbook(TempPath); diff --git a/Seyounth.Hyosung.Runtime/HyosungRuntime.cs b/Seyounth.Hyosung.Runtime/HyosungRuntime.cs index 06c3856..ed9a4a7 100644 --- a/Seyounth.Hyosung.Runtime/HyosungRuntime.cs +++ b/Seyounth.Hyosung.Runtime/HyosungRuntime.cs @@ -207,7 +207,7 @@ public class HyosungRuntime( await hyosungAgvService.StorageAsync(info.TrayCode); //标志下线已完成 await hyosungPlcService.LeaveCompletedAsync(); - await reportExportService.ExportAsync(info.TrayCode); + await reportExportService.ExportSampleAsync(info.TrayCode); logger.LogInformation($"plc leaving production line success"); } catch (Exception e)