From c933c8d2f7c643243a29f367bb069d95e4d23cb1 Mon Sep 17 00:00:00 2001 From: jianghaiqing Date: Mon, 29 May 2023 17:23:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9BC=E5=BC=95=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Helper/FileAttachHelper.cs | 144 ++++++++++++++++++ .../BookingOrder/BookingValueAddedService.cs | 31 +++- 2 files changed, 170 insertions(+), 5 deletions(-) create mode 100644 Myshipping.Application/Helper/FileAttachHelper.cs diff --git a/Myshipping.Application/Helper/FileAttachHelper.cs b/Myshipping.Application/Helper/FileAttachHelper.cs new file mode 100644 index 00000000..76a46fc5 --- /dev/null +++ b/Myshipping.Application/Helper/FileAttachHelper.cs @@ -0,0 +1,144 @@ +using Furion; +using Furion.Logging; +using Furion.RemoteRequest.Extensions; +using Microsoft.Extensions.Logging; +using Myshipping.Application.ConfigOption; +using Myshipping.Application.Entity; +using Myshipping.Core; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; + +namespace Myshipping.Application +{ + /// + /// 文件附件帮助类 + /// + public static class FileAttachHelper + { + #region 转移文件 + /// + /// 转移文件 + /// + /// 文件目录KEY + /// 源文件完整路径 + /// 批次号 + /// 是否生成本地文件 + /// 附件类型 bcfiles-BC文件 + /// 返沪新的文件路径 + public static async Task MoveFile(string fileDictKey,string sourceFilePath,string batchNo, + bool isLocalTempFile = false,string attachFileType = "bcfiles") + { + var logger = Log.CreateLogger(nameof(FileAttachHelper)); + + var fileCfg = App.GetOptions(); + + string fileRoot = string.Empty; + + if(fileCfg != null) + { + if(!isLocalTempFile) + { + if (!string.IsNullOrWhiteSpace(fileCfg.basePath)) + { + fileRoot = fileCfg.basePath; + } + } + else + { + var opt = App.GetOptions().Path; + + if (!string.IsNullOrWhiteSpace(fileCfg.relativePath)) + { + fileRoot = $"{Path.Combine(App.WebHostEnvironment.WebRootPath, opt)}"; + } + } + } + + 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("yyyyMMddHHmmss")}"; + + string filePath = $"{fileRoot}\\{relativePath}"; + + string fileFullName = $"{filePath}\\{new System.IO.FileInfo(sourceFilePath).Name}"; + + 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); + } + + if (sourceFilePath.StartsWith("http", StringComparison.OrdinalIgnoreCase) || + sourceFilePath.StartsWith("https", StringComparison.OrdinalIgnoreCase)) + { + var bcStream = await sourceFilePath.GetAsStreamAsync(); + + using (var fileStream = File.Create(fileFullName)) + { + await bcStream.CopyToAsync(fileStream); + } + } + else + { + System.IO.FileStream file = new System.IO.FileStream(sourceFilePath, FileMode.Open, FileAccess.Read); + + using (var fileStream = File.Create(fileFullName)) + { + await file.CopyToAsync(fileStream); + } + + file.Close(); + + try + { + //删除原文件 + System.IO.File.Delete(sourceFilePath); + } + catch(Exception delEx) + { + logger.LogInformation("批次={no} 删除文件异常 filepath={path} ex={ex}", batchNo, fileFullName, delEx.Message); + } + } + + logger.LogInformation("批次={no} 完成文件保存 filepath={path}", batchNo, fileFullName); + + string bookFilePath = string.Empty; + + if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + bookFilePath = System.Text.RegularExpressions.Regex.Match(fileFullName, relativePath.Replace("/", "\\/") + ".*").Value; + } + else + { + bookFilePath = System.Text.RegularExpressions.Regex.Match(fileFullName, relativePath.Replace("\\", "\\\\") + ".*").Value; + } + + return bookFilePath; + } + #endregion + } +} diff --git a/Myshipping.Application/Service/BookingOrder/BookingValueAddedService.cs b/Myshipping.Application/Service/BookingOrder/BookingValueAddedService.cs index 10d4ab46..e6753309 100644 --- a/Myshipping.Application/Service/BookingOrder/BookingValueAddedService.cs +++ b/Myshipping.Application/Service/BookingOrder/BookingValueAddedService.cs @@ -1297,7 +1297,7 @@ namespace Myshipping.Application var opt = App.GetOptions().Path; - string filePath = $"{Path.Combine(App.WebHostEnvironment.WebRootPath, opt)}\\bcfilestemp\\{bookingOrder.Id}\\"; + string filePath = $"{Path.Combine(App.WebHostEnvironment.WebRootPath, opt)}\\bcfilestemp\\{bookingOrder.Id}"; string fileFullName = $"{filePath}\\{file.FileName}"; @@ -1357,7 +1357,7 @@ namespace Myshipping.Application try { var response = await requestUrl.SetContentType("multipart/form-data") - .SetBodyBytes((fileInfo.file.ToString(), fileInfo.fileBytes, fileInfo.fileName.ToString())) + .SetBodyBytes((fileInfo.file.ToString(), fileInfo.fileBytes, HttpUtility.UrlEncode(fileInfo.fileName.ToString()))) .PostAsync(); if (response.StatusCode == System.Net.HttpStatusCode.OK) @@ -1499,17 +1499,33 @@ namespace Myshipping.Application _logger.LogInformation("批次={no} id={id} 单票BC更新订舱后,用户选择转为入货通知,开始执行入货通知", batchNo , bookingOrder.Id); + if (string.IsNullOrWhiteSpace(model.FileTempPath)) + { + _logger.LogInformation("批次={no} id={id} 未提交文件路径,请求失败", batchNo + , bookingOrder.Id); + + throw Oops.Bah($"未提交文件路径,执行放舱失败"); + } + var letterYardDto = new UpdateBookingLetteryardInput(); letterYardDto = model.LetteryardDto.Adapt(); - - if(model.LetterYardId.HasValue) + + if (model.LetterYardId.HasValue && model.LetterYardId.Value > 0) { letterYardDto.Id = model.LetterYardId.Value; _logger.LogInformation("批次={no} id={id} 单票BC更新订舱后,存在放舱记录 LetterYardId={LetterYardId}", batchNo , bookingOrder.Id, letterYardDto.Id); } + else + { + var letterYardModel = _bookingLetteryardRepository.AsQueryable() + .First(x => x.BookingId == model.BookingOrderId); + + if(letterYardModel != null) + letterYardDto.Id = letterYardModel.Id; + } //放舱保存 var letterYardId = await _bookingOrderService.LetteryardSave(letterYardDto); @@ -1529,8 +1545,13 @@ namespace Myshipping.Application string fileTypeCode = "bc"; string fileTypeName = "Booking Confirmation"; + //重新将暂存文件写入正式路径 + + //读取文件配置 + var bookFilePath = await FileAttachHelper.MoveFile(bookingOrder.Id.ToString(), model.FileTempPath,batchNo); + //将BC引入的文件写入订舱的附件 - await SaveEDIFile(bookingOrder.Id, model.FileTempPath, new System.IO.FileInfo(model.FileTempPath).Name, + await SaveEDIFile(bookingOrder.Id, bookFilePath, new System.IO.FileInfo(bookFilePath).Name, fileTypeCode, fileTypeName); _logger.LogInformation("批次={no} id={id} 完成写入附件表 {filepath}", batchNo