From 3b329120484571f50393f6f3be31b624c2e1c3ef Mon Sep 17 00:00:00 2001 From: jianghaiqing Date: Thu, 18 May 2023 14:43:19 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9DRAFT=20=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BookingOrder/BookingValueAddedService.cs | 167 +++++++++++++++--- 1 file changed, 139 insertions(+), 28 deletions(-) diff --git a/Myshipping.Application/Service/BookingOrder/BookingValueAddedService.cs b/Myshipping.Application/Service/BookingOrder/BookingValueAddedService.cs index 9bd97fda..c995b026 100644 --- a/Myshipping.Application/Service/BookingOrder/BookingValueAddedService.cs +++ b/Myshipping.Application/Service/BookingOrder/BookingValueAddedService.cs @@ -452,20 +452,38 @@ namespace Myshipping.Application 4、请求相应的链接。 5、返回成功写入附件。 */ - if(string.IsNullOrWhiteSpace(bookingOrder.MBLNO)) + if(bookingOrder.CARRIERID.Equals("ESL", StringComparison.OrdinalIgnoreCase)) { - if (!string.IsNullOrWhiteSpace(bookingOrder.CUSTNO)) + if(string.IsNullOrWhiteSpace(bookingOrder.TMBLNO)) { - result.bno = $"订 {bookingOrder.CUSTNO}"; + if (!string.IsNullOrWhiteSpace(bookingOrder.CUSTNO)) + { + result.bno = $"订 {bookingOrder.CUSTNO}"; + } + else if (string.IsNullOrWhiteSpace(bookingOrder.MBLNO)) + { + result.bno = $"NO.{sortNo}"; + } + + throw Oops.Bah($"EP号不能为空"); } - else + } + else + { + if (string.IsNullOrWhiteSpace(bookingOrder.MBLNO)) { - result.bno = $"NO.{sortNo}"; + if (!string.IsNullOrWhiteSpace(bookingOrder.CUSTNO)) + { + result.bno = $"订 {bookingOrder.CUSTNO}"; + } + else + { + result.bno = $"NO.{sortNo}"; + } + + throw Oops.Bah($"主提单号不能为空"); } - - throw Oops.Bah($"主提单号不能为空"); } - var bcOrDraftRouteCfg = _cache.GetAllMappingCarrier().GetAwaiter().GetResult() .FirstOrDefault(t => t.Module.Equals(CONST_MAPPING_DRAFT_MODULE_ROUTE, StringComparison.OrdinalIgnoreCase) @@ -509,34 +527,80 @@ namespace Myshipping.Application if (userWebAccountConfig == null) throw Oops.Oh($" 未配置个人或公司网站账户,网站{webKey}"); - BCOrDraftRequestDto requestDto = new BCOrDraftRequestDto + string downloadFilePathRlt = string.Empty; + string erroMsg = string.Empty; + + if(bcOrDraftRouteCfg.MapCode.Trim().Equals("TSL", StringComparison.OrdinalIgnoreCase)) { - user_key = App.Configuration["BCOrDraftUserKey"], - user_secret = App.Configuration["BCOrDraftUserSecret"], - web_user = userWebAccountConfig.Account?.Trim(), - web_psw = userWebAccountConfig.Password?.Trim(), - bno = bookingOrder.MBLNO, - is_parse = false - }; + ESLDraftRequestDto requestDto = new ESLDraftRequestDto + { + u = userWebAccountConfig.Account?.Trim(), + p = userWebAccountConfig.Password?.Trim(), + ep_code = bookingOrder.TMBLNO?.Trim().ToUpper(), + }; - _logger.LogInformation("批次={no} json={json} 请求Draft远端下载开始", batchNo, JSON.Serialize(requestDto)); + _logger.LogInformation("批次={no} json={json} 请求Draft远端下载开始", batchNo, JSON.Serialize(requestDto)); - DateTime bDate = DateTime.Now; - //开始请求BC - var rlt = await ExcuteDraftDownload(bcUrl, requestDto, batchNo); + DateTime bDate = DateTime.Now; + //开始请求BC + var rlt = await ExcuteESLDraftDownload(bcUrl, requestDto, batchNo); - DateTime eDate = DateTime.Now; - TimeSpan ts = eDate.Subtract(bDate); - var timeDiff = ts.TotalMilliseconds; + DateTime eDate = DateTime.Now; + TimeSpan ts = eDate.Subtract(bDate); + var timeDiff = ts.TotalMilliseconds; - _logger.LogInformation("批次={no} result={result} 请求Draft远端下载结束 耗时:{timeDiff}ms. ", batchNo, - JSON.Serialize(rlt), timeDiff); + _logger.LogInformation("批次={no} result={result} 请求Draft远端下载结束 耗时:{timeDiff}ms. ", batchNo, + JSON.Serialize(rlt), timeDiff); - if (rlt.code == 200) + if (rlt.status == 1) + { + downloadFilePathRlt = rlt.data.api_path; + } + else + { + erroMsg = rlt.message; + } + } + else + { + BCOrDraftRequestDto requestDto = new BCOrDraftRequestDto + { + user_key = App.Configuration["BCOrDraftUserKey"], + user_secret = App.Configuration["BCOrDraftUserSecret"], + web_user = userWebAccountConfig.Account?.Trim(), + web_psw = userWebAccountConfig.Password?.Trim(), + bno = bookingOrder.MBLNO, + is_parse = false + }; + + _logger.LogInformation("批次={no} json={json} 请求Draft远端下载开始", batchNo, JSON.Serialize(requestDto)); + + DateTime bDate = DateTime.Now; + //开始请求BC + var rlt = await ExcuteDraftDownload(bcUrl, requestDto, batchNo); + + DateTime eDate = DateTime.Now; + TimeSpan ts = eDate.Subtract(bDate); + var timeDiff = ts.TotalMilliseconds; + + _logger.LogInformation("批次={no} result={result} 请求Draft远端下载结束 耗时:{timeDiff}ms. ", batchNo, + JSON.Serialize(rlt), timeDiff); + + if (rlt.code == 200) + { + downloadFilePathRlt = rlt.data.FirstOrDefault().path; + } + else + { + erroMsg = rlt.msg; + } + } + + if (!string.IsNullOrWhiteSpace(downloadFilePathRlt)) { _logger.LogInformation("批次={no} 下载文件成功,转存本地", batchNo); - string currFilePath = rlt.data.FirstOrDefault().path; + string currFilePath = downloadFilePathRlt; string fileTypeCode = "draft"; string fileTypeName = "Draft"; @@ -594,7 +658,7 @@ namespace Myshipping.Application else { result.succ = false; - result.msg = $"Draft下载失败,原因={rlt.msg}"; + result.msg = $"Draft下载失败,原因={erroMsg}"; } } catch (Exception ex) @@ -705,6 +769,53 @@ namespace Myshipping.Application } #endregion + #region ESL Draft请求远端下载 + /// + /// ESL Draft请求远端下载 + /// 由于现有的ESL和TSL接口不一致,需要提供单独的POST方法 + /// + /// 请求URL + /// 请求详情 + /// 批次号 + /// 返回结果 + [NonAction] + private async Task ExcuteESLDraftDownload(string url, ESLDraftRequestDto info, string batchNo) + { + ESLDraftAPIBaseResult model = null; + /* + 1、填充请求的类,并生成JSON报文 + 2、POST请求接口,并记录回执。 + 3、返回信息。 + */ + try + { + _logger.LogInformation("批次={no} 对应请求报文 request={res}", batchNo, JSON.Serialize(info)); + + var res = await url.SetHttpMethod(HttpMethod.Post) + .SetBody(JSON.Serialize(info), "application/json") + .SetContentEncoding(Encoding.UTF8) + .PostAsync(); + + _logger.LogInformation("批次={no} 对应请求报文完成 res={res}", batchNo, JSON.Serialize(res)); + + if (res.StatusCode == System.Net.HttpStatusCode.OK) + { + var userResult = await res.Content.ReadAsStringAsync(); + + model = JSON.Deserialize(userResult); + } + } + catch (Exception ex) + { + //写日志 + if (ex is HttpRequestException) + throw Oops.Oh(10000002); + } + + return model; + } + #endregion + /// /// 到港时间更新 From 4f15dfe8f533b495985a9f53653d63813167a616 Mon Sep 17 00:00:00 2001 From: jianghaiqing Date: Thu, 18 May 2023 14:54:19 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9ESL=20Draft=20=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Service/BookingOrder/BookingValueAddedService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Myshipping.Application/Service/BookingOrder/BookingValueAddedService.cs b/Myshipping.Application/Service/BookingOrder/BookingValueAddedService.cs index c995b026..dbe11f3a 100644 --- a/Myshipping.Application/Service/BookingOrder/BookingValueAddedService.cs +++ b/Myshipping.Application/Service/BookingOrder/BookingValueAddedService.cs @@ -530,7 +530,7 @@ namespace Myshipping.Application string downloadFilePathRlt = string.Empty; string erroMsg = string.Empty; - if(bcOrDraftRouteCfg.MapCode.Trim().Equals("TSL", StringComparison.OrdinalIgnoreCase)) + if(bcOrDraftRouteCfg.MapCode.Trim().Equals("ESL", StringComparison.OrdinalIgnoreCase)) { ESLDraftRequestDto requestDto = new ESLDraftRequestDto { From 8b65c8579320f1bf9157778ec2d2c344ae09c0e3 Mon Sep 17 00:00:00 2001 From: jianghaiqing Date: Thu, 18 May 2023 14:59:57 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9ESL=20DRAFT=20=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BookingOrder/BookingValueAddedService.cs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/Myshipping.Application/Service/BookingOrder/BookingValueAddedService.cs b/Myshipping.Application/Service/BookingOrder/BookingValueAddedService.cs index dbe11f3a..f60956d8 100644 --- a/Myshipping.Application/Service/BookingOrder/BookingValueAddedService.cs +++ b/Myshipping.Application/Service/BookingOrder/BookingValueAddedService.cs @@ -692,7 +692,10 @@ namespace Myshipping.Application { _logger.LogInformation("批次={no} 对应请求报文 request={res}", batchNo, JSON.Serialize(info)); - var res = await url.SetHttpMethod(HttpMethod.Post) + var res = await url.OnClientCreating(client => { + // client 为 HttpClient 对象 + client.Timeout = TimeSpan.FromMinutes(15); // 设置超时时间 15分钟 + }).SetHttpMethod(HttpMethod.Post) .SetBody(JSON.Serialize(info), "application/json") .SetContentEncoding(Encoding.UTF8) .PostAsync(); @@ -741,7 +744,10 @@ namespace Myshipping.Application { _logger.LogInformation("批次={no} 对应请求报文 request={res}", batchNo, JSON.Serialize(info)); - var res = await url.SetHttpMethod(HttpMethod.Post) + var res = await url.OnClientCreating(client => { + // client 为 HttpClient 对象 + client.Timeout = TimeSpan.FromMinutes(15); // 设置超时时间 15分钟 + }).SetHttpMethod(HttpMethod.Post) .SetBody(JSON.Serialize(info), "application/json") .SetContentEncoding(Encoding.UTF8) .PostAsync(); @@ -791,7 +797,10 @@ namespace Myshipping.Application { _logger.LogInformation("批次={no} 对应请求报文 request={res}", batchNo, JSON.Serialize(info)); - var res = await url.SetHttpMethod(HttpMethod.Post) + var res = await url.OnClientCreating(client => { + // client 为 HttpClient 对象 + client.Timeout = TimeSpan.FromMinutes(15); // 设置超时时间 15分钟 + }).SetHttpMethod(HttpMethod.Post) .SetBody(JSON.Serialize(info), "application/json") .SetContentEncoding(Encoding.UTF8) .PostAsync();