From 1945aff9b987f04c3328bef7eb9337371e8ea287 Mon Sep 17 00:00:00 2001 From: jianghaiqing Date: Thu, 1 Jun 2023 16:55:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9ONE=20=E4=B8=8B=E8=BD=BDEDI?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E7=94=9F=E6=88=90=E8=AE=A2=E8=88=B1=E9=99=84?= =?UTF-8?q?=E4=BB=B6=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Helper/FileAttachHelper.cs | 90 +++++++++++++++++++ .../BookingOrder/BookingOrderService.cs | 75 +++++++++++++++- .../BookingTruck/BookingTruckService.cs | 8 +- .../TaskManagePlat/TaskManageTruckService.cs | 8 +- 4 files changed, 171 insertions(+), 10 deletions(-) diff --git a/Myshipping.Application/Helper/FileAttachHelper.cs b/Myshipping.Application/Helper/FileAttachHelper.cs index ec171875..efaa875b 100644 --- a/Myshipping.Application/Helper/FileAttachHelper.cs +++ b/Myshipping.Application/Helper/FileAttachHelper.cs @@ -1,4 +1,5 @@ using Furion; +using Furion.FriendlyException; using Furion.Logging; using Furion.RemoteRequest.Extensions; using Microsoft.AspNetCore.Http; @@ -192,5 +193,94 @@ namespace Myshipping.Application return fileFullName; } #endregion + + #region 保存文件并返回文件完整路径 + /// + /// 保存文件并返回文件完整路径 + /// + /// 文件目录KEY + /// 文件二进制流 + /// 批次号 + /// 文件名称不带后缀名 + /// 文件类型 + /// 附件类型 bcfiles-BC文件 sofile-订舱附件 + /// 是否生成日期文件名 + /// 返回文件完整路径 + public static async Task SaveFile(string fileDictKey, byte[] fileBytes, string batchNo, string fileNameNoSuffix, + PrintFileTypeEnum printFileType, string attachFileType = "sofiles", bool isUseTimeDocName = false) + { + var logger = Log.CreateLogger(nameof(FileAttachHelper)); + + var fileCfg = App.GetOptions(); + + string fileRoot = string.Empty; + + if (fileCfg != null) + { + if (!string.IsNullOrWhiteSpace(fileCfg.basePath)) + { + fileRoot = fileCfg.basePath; + } + } + + if (string.IsNullOrWhiteSpace(fileRoot)) + fileRoot = App.WebHostEnvironment.WebRootPath; + + string relativePath = fileCfg.relativePath; + + if (!string.IsNullOrWhiteSpace(attachFileType)) + relativePath += $"\\{attachFileType}"; + + if (!string.IsNullOrWhiteSpace(fileDictKey)) + relativePath += $"\\{fileDictKey}"; + + relativePath += $"\\{DateTime.Now.ToString("yyyyMMddHHmmssfff")}"; + + string filePath = $"{fileRoot}\\{relativePath}"; + + var fileType = string.Empty; + if (printFileType == PrintFileTypeEnum.PDF) + { + fileType = ".pdf"; + } + else if (printFileType == PrintFileTypeEnum.XLSX) + { + fileType = ".xlsx"; + } + else if (printFileType == PrintFileTypeEnum.DOCX) + { + fileType = ".docx"; + } + + string curFileName = fileNameNoSuffix; + + if (isUseTimeDocName) + { + curFileName = DateTime.Now.ToString("yyyyMMddHHmmssfff"); + } + + string fileFullName = $"{filePath}\\{curFileName}{fileType}"; + + if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + relativePath = relativePath.Replace("\\", "/"); + filePath = filePath.Replace("\\", "/"); + fileFullName = fileFullName.Replace("\\", "/"); + } + + logger.LogInformation("批次={no} 生成文件保存路径完成 路由={filePath} 服务器系统={system}", batchNo, filePath, + RuntimeInformation.OSDescription); + + //预先创建目录 + if (!Directory.Exists(filePath)) + { + Directory.CreateDirectory(filePath); + } + + await File.WriteAllBytesAsync(fileFullName, fileBytes); + + return fileFullName; + } + #endregion } } diff --git a/Myshipping.Application/Service/BookingOrder/BookingOrderService.cs b/Myshipping.Application/Service/BookingOrder/BookingOrderService.cs index bc94776b..fd8f3d84 100644 --- a/Myshipping.Application/Service/BookingOrder/BookingOrderService.cs +++ b/Myshipping.Application/Service/BookingOrder/BookingOrderService.cs @@ -129,6 +129,8 @@ namespace Myshipping.Application const string CONST_TSL_EDI_URL = "tsl_edi_declare_url"; const string CONST_TSL_TYPE_CODE = "TslWeb"; + const string CONST_ONE_SOFILE_CATE_CODE = "one_so_file_template"; + const string PRINT_DATASOURCE_KEY = "booking_order"; public BookingOrderService(SqlSugarRepository rep, SqlSugarRepository repCtn, SqlSugarRepository ctndetailrep, SqlSugarRepository bookinglog, SqlSugarRepository bookinglogdetail, SqlSugarRepository bookingremark, @@ -6250,11 +6252,80 @@ namespace Myshipping.Application /// /// 打印船公司ONE的订舱附件(生成后自动写入订舱附件) /// - /// 订舱ID + /// 订舱详情 /// - private async Task PrintCarrierOneSOFileAsync(long bookingId) + private async Task PrintCarrierOneSOFileAsync(BookingOrder model) { + string batchNo = IDGen.NextID().ToString(); + /* + 1、提取默认的ONE订舱附件打印模板。 + 2、生成打印文件。 + 3、将文件写入订舱附件。 + */ + try + { + var printTemplate = await _repPrintTemplate.AsQueryable() + .Filter(null, true) + .InnerJoin((d, t) => d.Id == t.PrintTemplateId + && t.SysUserId == UserManager.UserId) + .Where(d => d.TenantId == UserManager.TENANT_ID + && d.CateCode.Equals(CONST_ONE_SOFILE_CATE_CODE) + && d.Type == BookingPrintTemplateType.FastReport.ToString()) + .FirstAsync(); + + //取到打印模板文件 + if(printTemplate == null) + { + _logger.LogInformation("打印船公司ONE的订舱附件 batchNo={batchno} 获取模板文件失败", batchNo); + + throw Oops.Bah("获取模板文件失败"); + } + + _logger.LogInformation("打印船公司ONE的订舱附件 batchNo={batchno} 获取模板文件成功", batchNo); + //打印报表服务地址 + var reportUrl = _cache.GetAllDictData().Result.FirstOrDefault(x => x.TypeCode == "url_set" && + x.Code == "url_report_generate").Value; + + if(string.IsNullOrWhiteSpace(reportUrl)) + { + _logger.LogInformation("打印船公司ONE的订舱附件 batchNo={batchno} 获取打印报表服务地址失败", batchNo); + + throw Oops.Bah("获取打印报表服务地址失败"); + } + + if (!reportUrl.EndsWith("/")) + { + reportUrl += "/"; + } + + //调取生成打印二进制流 + var bs = await PrintHelper.GeneratePrintFile(JSON.Serialize(model), reportUrl, + PRINT_DATASOURCE_KEY, PrintFileTypeEnum.PDF, printTemplate); + + //保存文件 + var bookFilePath = await FileAttachHelper.SaveFile(model.Id.ToString(), bs, batchNo, "", + PrintFileTypeEnum.PDF, "sofile", true); + + + _logger.LogInformation("打印船公司ONE的订舱附件 批次={no} id={id} 完成文件保存 {filepath}", batchNo + , model.Id, bookFilePath); + + //写入订舱随附单据 + string fileTypeCode = "sofile"; + string fileTypeName = "订舱附件"; + + //将BC引入的文件写入订舱的附件 + await SaveEDIFile(model.Id, bookFilePath, new System.IO.FileInfo(bookFilePath).Name, + fileTypeCode, fileTypeName); + + _logger.LogInformation("打印船公司ONE的订舱附件 批次={no} id={id} 完成写入附件表 {filepath}", batchNo + , model.Id, bookFilePath); + } + catch (Exception ex) + { + _logger.LogInformation("打印船公司ONE的订舱附件失败 batchNo={batchno} 原因={reason}", batchNo,ex.GetMessage()); + } } #endregion } diff --git a/Myshipping.Application/Service/BookingTruck/BookingTruckService.cs b/Myshipping.Application/Service/BookingTruck/BookingTruckService.cs index 29f48f40..1d163240 100644 --- a/Myshipping.Application/Service/BookingTruck/BookingTruckService.cs +++ b/Myshipping.Application/Service/BookingTruck/BookingTruckService.cs @@ -52,7 +52,7 @@ namespace Myshipping.Application private readonly SqlSugarRepository _bookingTruckContaRepository; private readonly SqlSugarRepository _bookingOrderRepository; private readonly SqlSugarRepository _bookingCtnRepository; - private readonly SqlSugarRepository _bookingPrintTemplateTemplate; + private readonly SqlSugarRepository _bookingPrintTemplateRepository; private readonly SqlSugarRepository _djyUserConfigConfig; private readonly SqlSugarRepository _bookingExcelTemplateRepository; private readonly SqlSugarRepository _sysUserRepository; @@ -87,7 +87,7 @@ namespace Myshipping.Application _bookingCtnRepository = bookingCtnRepository; _taskManageExternalService = taskManageExternalService; - _bookingPrintTemplateTemplate = bookingPrintTemplateTemplate; + _bookingPrintTemplateRepository = bookingPrintTemplateTemplate; _djyUserConfigConfig = djyUserConfigConfig; _bookingExcelTemplateRepository = bookingExcelTemplateRepository; _sysUserRepository = sysUserRepository; @@ -1241,7 +1241,7 @@ namespace Myshipping.Application { var typeCode = printType.ToString(); //当前公司所有已配置的模板 - var allList = await _bookingPrintTemplateTemplate.AsQueryable() + var allList = await _bookingPrintTemplateRepository.AsQueryable() .Filter(null, true).InnerJoin((d, t) => d.Id == t.PrintTemplateId && t.SysUserId == UserManager.UserId) .Where(d => d.TenantId == UserManager.TENANT_ID && d.CateCode.Contains(cateCode) && d.Type == typeCode) .Select(d => new @@ -1297,7 +1297,7 @@ namespace Myshipping.Application public async Task Print([FromQuery] long id, [FromQuery] long templateId, [FromQuery] string cateCode, [FromQuery] PrintFileTypeEnum printFileType = PrintFileTypeEnum.PDF, [FromQuery] BookingPrintTemplateType printType = BookingPrintTemplateType.FastReport) { - var printTemplate = await _bookingPrintTemplateTemplate.AsQueryable().Filter(null, true) + var printTemplate = await _bookingPrintTemplateRepository.AsQueryable().Filter(null, true) .FirstAsync(x => x.Id == templateId); if (printTemplate == null) diff --git a/Myshipping.Application/Service/TaskManagePlat/TaskManageTruckService.cs b/Myshipping.Application/Service/TaskManagePlat/TaskManageTruckService.cs index aa8ccf8e..531cf01b 100644 --- a/Myshipping.Application/Service/TaskManagePlat/TaskManageTruckService.cs +++ b/Myshipping.Application/Service/TaskManagePlat/TaskManageTruckService.cs @@ -38,7 +38,7 @@ namespace Myshipping.Application private readonly SqlSugarRepository _taskTruckRepository; private readonly SqlSugarRepository _taskTruckContaRepository; private readonly SqlSugarRepository _taskBaseRepository; - private readonly SqlSugarRepository _bookingPrintTemplateTemplate; + private readonly SqlSugarRepository _bookingPrintTemplateRepository; private readonly SqlSugarRepository _djyUserConfigConfig; private readonly SqlSugarRepository _bookingExcelTemplateRepository; private readonly SqlSugarRepository _bookingOrderRepository; @@ -65,7 +65,7 @@ namespace Myshipping.Application _taskTruckContaRepository = taskTruckContaRepository; _taskBaseRepository = taskBaseRepository; - _bookingPrintTemplateTemplate = bookingPrintTemplateTemplate; + _bookingPrintTemplateRepository = bookingPrintTemplateTemplate; _djyUserConfigConfig = djyUserConfigConfig; _bookingExcelTemplateRepository = bookingExcelTemplateRepository; _bookingOrderRepository = bookingOrderRepository; @@ -324,7 +324,7 @@ namespace Myshipping.Application public async Task Print([FromQuery] string taskPKId, [FromQuery] long templateId, [FromQuery] string cateCode, [FromQuery] PrintFileTypeEnum printFileType = PrintFileTypeEnum.PDF, [FromQuery] BookingPrintTemplateType printType = BookingPrintTemplateType.FastReport) { - var printTemplate = await _bookingPrintTemplateTemplate.AsQueryable().Filter(null, true) + var printTemplate = await _bookingPrintTemplateRepository.AsQueryable().Filter(null, true) .FirstAsync(x => x.Id == templateId); if (printTemplate == null) @@ -1269,7 +1269,7 @@ namespace Myshipping.Application { var typeCode = printType.ToString(); //当前公司所有已配置的模板 - var allList = await _bookingPrintTemplateTemplate.AsQueryable() + var allList = await _bookingPrintTemplateRepository.AsQueryable() .Filter(null, true).InnerJoin((d, t) => d.Id == t.PrintTemplateId && t.SysUserId == UserManager.UserId) .Where(d => d.TenantId == UserManager.TENANT_ID && d.CateCode.Contains(cateCode) && d.Type == typeCode) .Select(d => new