From 3fedb785cd2f38587bb6410622e71a25651b5f32 Mon Sep 17 00:00:00 2001 From: jianghaiqing Date: Fri, 18 Nov 2022 17:27:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9EFTP=20Client=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0EDI=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Myshipping.Application/EDI/ESLEdiHelper.cs | 8 +- Myshipping.Application/EDI/PILEdiHelper.cs | 9 +- Myshipping.Application/EDI/TSLEdiHelper.cs | 17 ++- Myshipping.Application/EDI/WYEdiHelper.cs | 8 +- Myshipping.Application/EDI/YMLEdiHelper.cs | 17 ++- Myshipping.Application/EDI/YTEdiHelper.cs | 9 +- .../Myshipping.Application.csproj | 4 + .../Myshipping.Application.xml | 8 ++ .../BookingOrder/BookingOrderService.cs | 113 ++++++++++++++---- 9 files changed, 156 insertions(+), 37 deletions(-) diff --git a/Myshipping.Application/EDI/ESLEdiHelper.cs b/Myshipping.Application/EDI/ESLEdiHelper.cs index ce2c9f4a..4f4269c2 100644 --- a/Myshipping.Application/EDI/ESLEdiHelper.cs +++ b/Myshipping.Application/EDI/ESLEdiHelper.cs @@ -610,8 +610,10 @@ namespace Myshipping.Application.EDI.ESL #endregion #region 生成报文(订舱(InttrEdi.filetype=="B"),截单(InttrEdi.filetype=="E")) - public static string CreateEdiESL(MsESLEdiModel InttrEdi) + public static CommonWebApiResult CreateEdiESL(MsESLEdiModel InttrEdi) { + CommonWebApiResult result = new CommonWebApiResult { succ = false }; + var filetype = "IFTMIN"; if (InttrEdi.filetype != "E") filetype = "IFTMBF"; @@ -1025,7 +1027,9 @@ namespace Myshipping.Application.EDI.ESL r.Close(); f.Close(); - return filename; + result.succ = true; + result.extra = filename; + return result; } #endregion diff --git a/Myshipping.Application/EDI/PILEdiHelper.cs b/Myshipping.Application/EDI/PILEdiHelper.cs index 609f3f5c..8d7db7b8 100644 --- a/Myshipping.Application/EDI/PILEdiHelper.cs +++ b/Myshipping.Application/EDI/PILEdiHelper.cs @@ -1,4 +1,5 @@ using Myshipping.Application.EDI.ESL; +using MySqlX.XDevAPI.Common; using System; using System.Collections.Generic; using System.IO; @@ -653,8 +654,10 @@ namespace Myshipping.Application.EDI.PIL #endregion #region 生成报文(订舱(filetype=="B"),截单(filetype=="E")) - public static string CreateEdiPIL(EDIBaseModel InttrEdi) + public static CommonWebApiResult CreateEdiPIL(EDIBaseModel InttrEdi) { + CommonWebApiResult result = new CommonWebApiResult { succ = false }; + var estr = ""; if (InttrEdi.filetype == "E") { @@ -1779,7 +1782,9 @@ namespace Myshipping.Application.EDI.PIL r.Close(); f.Close(); - return filename; + result.succ = true; + result.extra = filename; + return result; } #endregion diff --git a/Myshipping.Application/EDI/TSLEdiHelper.cs b/Myshipping.Application/EDI/TSLEdiHelper.cs index 30df77cb..7a18b402 100644 --- a/Myshipping.Application/EDI/TSLEdiHelper.cs +++ b/Myshipping.Application/EDI/TSLEdiHelper.cs @@ -1,4 +1,5 @@ using Furion.LinqBuilder; +using MySqlX.XDevAPI.Common; using System; using System.Collections.Generic; using System.IO; @@ -716,8 +717,10 @@ namespace Myshipping.Application.EDI.TSL #endregion #region 订舱 - public static string CreateEdiTSL(EDIBaseModel InttrEdi) + public static CommonWebApiResult CreateEdiTSL(EDIBaseModel InttrEdi) { + CommonWebApiResult result = new CommonWebApiResult { succ = false }; + string filename = InttrEdi.filerpath + "\\" + InttrEdi.BSLIST[0].MBLNO + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".txt"; //如果是部署linux需要修改路径 @@ -1314,13 +1317,17 @@ namespace Myshipping.Application.EDI.TSL r.Close(); f.Close(); - return filename; + result.succ = true; + result.extra = filename; + return result; } #endregion #region 确认 - public static string CreateEdiTSLSI(EDIBaseModel InttrEdi) + public static CommonWebApiResult CreateEdiTSLSI(EDIBaseModel InttrEdi) { + CommonWebApiResult result = new CommonWebApiResult { succ = false }; + string filename = InttrEdi.filerpath + "\\" + InttrEdi.BSLIST[0].MBLNO + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".txt"; if (System.IO.File.Exists(filename)) @@ -2060,7 +2067,9 @@ namespace Myshipping.Application.EDI.TSL r.Close(); f.Close(); - return filename; + result.succ = true; + result.extra = filename; + return result; } #endregion diff --git a/Myshipping.Application/EDI/WYEdiHelper.cs b/Myshipping.Application/EDI/WYEdiHelper.cs index 1815e37b..7e7a095b 100644 --- a/Myshipping.Application/EDI/WYEdiHelper.cs +++ b/Myshipping.Application/EDI/WYEdiHelper.cs @@ -618,8 +618,10 @@ namespace Myshipping.Application.EDI.WY #endregion #region 生成报文(订舱(filetype=="B"),截单(filetype=="E")) - public static string CreateEdiWY(EDIBaseModel InttrEdi) + public static CommonWebApiResult CreateEdiWY(EDIBaseModel InttrEdi) { + CommonWebApiResult result = new CommonWebApiResult { succ = false }; + var filetype = "IFTMBF"; string filename = InttrEdi.filerpath + "\\" + filetype + "_" + InttrEdi.BSLIST[0].MBLNO + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".txt"; @@ -906,7 +908,9 @@ namespace Myshipping.Application.EDI.WY r.Close(); f.Close(); - return filename; + result.succ = true; + result.extra = filename; + return result; } #endregion } diff --git a/Myshipping.Application/EDI/YMLEdiHelper.cs b/Myshipping.Application/EDI/YMLEdiHelper.cs index 37d7685a..fb8da775 100644 --- a/Myshipping.Application/EDI/YMLEdiHelper.cs +++ b/Myshipping.Application/EDI/YMLEdiHelper.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Runtime.InteropServices; +using MySqlX.XDevAPI.Common; namespace Myshipping.Application.EDI.YML { @@ -644,8 +645,10 @@ namespace Myshipping.Application.EDI.YML #endregion #region 生成报文(订舱) - public static string CreateEdiYML(EDIBaseModel InttrEdi) + public static CommonWebApiResult CreateEdiYML(EDIBaseModel InttrEdi) { + CommonWebApiResult result = new CommonWebApiResult { succ = false }; + var filetype = "IFTMBF"; string filename = InttrEdi.filerpath + "\\" + filetype + "_" + InttrEdi.BSLIST[0].MBLNO + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".txt"; @@ -968,13 +971,17 @@ namespace Myshipping.Application.EDI.YML r.Close(); f.Close(); - return filename; + result.succ = true; + result.extra = filename; + return result; } #endregion #region 生成报文(确认) - public static string CreateEdiYMLSI(EDIBaseModel InttrEdi) + public static CommonWebApiResult CreateEdiYMLSI(EDIBaseModel InttrEdi) { + CommonWebApiResult result = new CommonWebApiResult { succ = false }; + string filename = InttrEdi.filerpath + "\\" + InttrEdi.BSLIST[0].MBLNO + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".txt"; if (System.IO.File.Exists(filename)) @@ -1797,7 +1804,9 @@ namespace Myshipping.Application.EDI.YML r.Close(); f.Close(); - return filename; + result.succ = true; + result.extra = filename; + return result; } #endregion diff --git a/Myshipping.Application/EDI/YTEdiHelper.cs b/Myshipping.Application/EDI/YTEdiHelper.cs index 48d8b2da..7ff7817b 100644 --- a/Myshipping.Application/EDI/YTEdiHelper.cs +++ b/Myshipping.Application/EDI/YTEdiHelper.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Runtime.InteropServices; +using MySqlX.XDevAPI.Common; namespace Myshipping.Application.EDI.YT { @@ -631,8 +632,10 @@ namespace Myshipping.Application.EDI.YT #endregion #region 生成报文(订舱(filetype=="B"),截单(filetype=="E")) - public static string CreateEdiYT(EDIBaseModel InttrEdi) + public static CommonWebApiResult CreateEdiYT(EDIBaseModel InttrEdi) { + CommonWebApiResult result = new CommonWebApiResult { succ = false }; + var filetype = "IFTMIN"; if (InttrEdi.filetype != "E") filetype = "IFTMBF"; @@ -1119,7 +1122,9 @@ namespace Myshipping.Application.EDI.YT r.Close(); f.Close(); - return filename; + result.succ = true; + result.extra = filename; + return result; } #endregion diff --git a/Myshipping.Application/Myshipping.Application.csproj b/Myshipping.Application/Myshipping.Application.csproj index a22ccd2b..6c460996 100644 --- a/Myshipping.Application/Myshipping.Application.csproj +++ b/Myshipping.Application/Myshipping.Application.csproj @@ -30,6 +30,10 @@ + + + + diff --git a/Myshipping.Application/Myshipping.Application.xml b/Myshipping.Application/Myshipping.Application.xml index ce966acc..4e2cb428 100644 --- a/Myshipping.Application/Myshipping.Application.xml +++ b/Myshipping.Application/Myshipping.Application.xml @@ -4757,6 +4757,14 @@ 订舱、截单EDI请求 返回回执 + + + 上传FTP + + EDI文件路径 + FTP配置 + true-成功 false-失败 + 触发订舱 diff --git a/Myshipping.Application/Service/BookingOrder/BookingOrderService.cs b/Myshipping.Application/Service/BookingOrder/BookingOrderService.cs index a9d66ce9..a5698fe1 100644 --- a/Myshipping.Application/Service/BookingOrder/BookingOrderService.cs +++ b/Myshipping.Application/Service/BookingOrder/BookingOrderService.cs @@ -47,6 +47,9 @@ using Myshipping.Application.EDI.WY; using Myshipping.Application.EDI.YML; using Myshipping.Application.EDI.YT; using System.Runtime.InteropServices; +using FluentFTP; +using System.Threading; +using Furion.JsonSerialization; namespace Myshipping.Application { @@ -1798,14 +1801,76 @@ namespace Myshipping.Application { throw Oops.Bah(result.msg); } + + //上传FTP + var sendStatus = await InnerSendBookingOrClosingEDIToFTP(result.extra.ToString(), ftpSet); + return result.extra.ToString(); + } + #endregion + /// + /// 上传FTP + /// + /// EDI文件路径 + /// FTP配置 + /// true-成功 false-失败 + private async Task InnerSendBookingOrClosingEDIToFTP(string filePath, DjyEdiSetting ediCfg) + { + CancellationTokenSource cts = new CancellationTokenSource(); + + var ftpClient = new AsyncFtpClient($"ftp://{ediCfg.SERVERIP}", ediCfg.USERNAME, ediCfg.PASSWORD); + _logger.LogInformation("创建FTP Client"); + if (!ftpClient.IsConnected) + { + await ftpClient.Connect(); + _logger.LogInformation("FTP Client 连接"); - return result.extra.ToString(); + var setOpts = await ftpClient.Execute("OPTS UTF8 ON", cts.Token); + + _logger.LogInformation("FTP Client 设置 OPTS UTF8 ON"); + + if (!setOpts.Code.Equals("200") && !setOpts.Code.Equals("202")) + ftpClient.Encoding = Encoding.GetEncoding("ISO-8859-1"); + } + + if (!File.Exists(filePath)) + throw Oops.Bah($"校验文件完成 file={filePath}"); + + _logger.LogInformation("读取文件信息"); + + var fileInfo = new FileInfo(filePath); + + string targetPath = $"{ediCfg.FOLDERNAME}/{fileInfo.Name}"; + + DateTime bDate = DateTime.Now; + + _logger.LogInformation("FTP Client 开始上传"); + + var uploadRlt = await ftpClient.UploadFile(filePath, targetPath, createRemoteDir: true); + + DateTime eDate = DateTime.Now; + TimeSpan ts = eDate.Subtract(bDate); + var timeDiff = ts.TotalMilliseconds; + + _logger.LogInformation($"FTP Client 上传完成 上传文件大小:{fileInfo.Length} 用时:{timeDiff}ms.,{JSON.Serialize(uploadRlt)}"); + + if (ftpClient.IsConnected) + { + await ftpClient.Disconnect(); + + _logger.LogInformation("FTP Client 断开链接"); + } + + ftpClient.Dispose(); + + _logger.LogInformation("FTP Client 释放完成"); + + return uploadRlt == FtpStatus.Success; } - #endregion + /// /// 触发订舱 @@ -1831,10 +1896,10 @@ namespace Myshipping.Application if (!string.IsNullOrWhiteSpace(strCheck)) throw Oops.Bah($"发送{EDIRouteEnum.PIL.ToString()}校验失败,{strCheck}"); - string ediFile = PILEdiHelper.CreateEdiPIL(ediModel); + var currRlt = PILEdiHelper.CreateEdiPIL(ediModel); #endregion - result.succ = true; - result.extra = ediFile; + result.succ = currRlt.succ; + result.extra = currRlt.succ ? currRlt.extra.ToString() : ""; } else if (ediRouteEnum == EDIRouteEnum.TSL) { @@ -1846,20 +1911,26 @@ namespace Myshipping.Application if (!string.IsNullOrWhiteSpace(strCheck)) throw Oops.Bah($"发送{EDIRouteEnum.PIL.ToString()}校验失败,{strCheck}"); - string ediFile = string.Empty; + CommonWebApiResult currRlt = new CommonWebApiResult(); if (model.sendType == "B") { - ediFile = TSLEdiHelper.CreateEdiTSL(ediModel); + currRlt = TSLEdiHelper.CreateEdiTSL(ediModel); } else if (model.sendType == "E") { - ediFile = TSLEdiHelper.CreateEdiTSL(ediModel); + currRlt = TSLEdiHelper.CreateEdiTSLSI(ediModel); } #endregion - result.succ = true; - result.extra = ediFile; + result.succ = currRlt.succ; + result.extra = currRlt.succ ? currRlt.extra.ToString() : ""; + + //鐩存帴鍙戦€佹姤鏂囧埌FTP鏈嶅姟鍣? + if (model.send) + { + + } } else if (ediRouteEnum == EDIRouteEnum.WY) { @@ -1871,11 +1942,11 @@ namespace Myshipping.Application if (!string.IsNullOrWhiteSpace(strCheck)) throw Oops.Bah($"发送{EDIRouteEnum.PIL.ToString()}校验失败,{strCheck}"); - string ediFile = WYEdiHelper.CreateEdiWY(ediModel); + var currRlt = WYEdiHelper.CreateEdiWY(ediModel); #endregion - result.succ = true; - result.extra = ediFile; + result.succ = currRlt.succ; + result.extra = currRlt.succ ? currRlt.extra.ToString() : ""; } else if (ediRouteEnum == EDIRouteEnum.YML) { @@ -1887,20 +1958,20 @@ namespace Myshipping.Application if (!string.IsNullOrWhiteSpace(strCheck)) throw Oops.Bah($"发送{EDIRouteEnum.PIL.ToString()}校验失败,{strCheck}"); - string ediFile = string.Empty; + CommonWebApiResult currRlt = new CommonWebApiResult(); if (model.sendType == "B") { - ediFile = YMLEdiHelper.CreateEdiYML(ediModel); + currRlt = YMLEdiHelper.CreateEdiYML(ediModel); } else if (model.sendType == "E") { - ediFile = YMLEdiHelper.CreateEdiYMLSI(ediModel); + currRlt = YMLEdiHelper.CreateEdiYMLSI(ediModel); } #endregion - result.succ = true; - result.extra = ediFile; + result.succ = currRlt.succ; + result.extra = currRlt.succ ? currRlt.extra.ToString() : ""; } else if (ediRouteEnum == EDIRouteEnum.YT) { @@ -1912,11 +1983,11 @@ namespace Myshipping.Application if (!string.IsNullOrWhiteSpace(strCheck)) throw Oops.Bah($"发送{EDIRouteEnum.PIL.ToString()}校验失败,{strCheck}"); - string ediFile = YTEdiHelper.CreateEdiYT(ediModel); + CommonWebApiResult currRlt = YTEdiHelper.CreateEdiYT(ediModel); #endregion - result.succ = true; - result.extra = ediFile; + result.succ = currRlt.succ; + result.extra = currRlt.succ ? currRlt.extra.ToString() : ""; } } catch (Exception ex)