diff --git a/Myshipping.Application/Service/BookingOrder/BookingValueAddedService.cs b/Myshipping.Application/Service/BookingOrder/BookingValueAddedService.cs index 91959c09..9c2cd24a 100644 --- a/Myshipping.Application/Service/BookingOrder/BookingValueAddedService.cs +++ b/Myshipping.Application/Service/BookingOrder/BookingValueAddedService.cs @@ -2612,9 +2612,25 @@ namespace Myshipping.Application /// /// 单票正本下载 /// - public async Task DownloadOriginal(long id) + [HttpGet("/BookingValueAdded/DownloadOriginal")] + public async Task DownloadOriginal(long id, bool? isCheck) { + const string FileTypeCode = "zhengben"; + const string FileTypeName = "正本"; + TaskManageOrderResultDto result = new TaskManageOrderResultDto(); + + if (isCheck == true) + { + if (await _bookingFileRepository.IsExistsAsync(x => x.BookingId == id && x.TypeCode == FileTypeCode)) + { + result.status = -1; + result.succ = false; + result.msg = "文件已存在"; + return result; + } + } + try { if (id < 0) @@ -2625,24 +2641,26 @@ namespace Myshipping.Application if (order == null) throw Oops.Oh($"订舱信息获取失败(订舱信息不存在或已作废)"); + + result.bno = order.MBLNO; var allSysConfig = _cache.GetAllSysConfig().Result; - var url = allSysConfig.FirstOrDefault(x => x.Code == "OriginalServiceUrl")?.Value; - var key = allSysConfig.FirstOrDefault(x => x.Code == "OriginalServiceKey")?.Value; - var secret = allSysConfig.FirstOrDefault(x => x.Code == "OriginalServiceSecret")?.Value; + var url = allSysConfig.FirstOrDefault(x => x.Code == "DraftDownloadServiceUrl")?.Value; + var key = allSysConfig.FirstOrDefault(x => x.Code == "DraftDownloadServiceKey")?.Value; + var token = allSysConfig.FirstOrDefault(x => x.Code == "DraftDownloadServiceToken")?.Value; if (string.IsNullOrWhiteSpace(url)) { - throw Oops.Bah("正本下载服务接口地址未配置,请联系管理员"); + throw Oops.Bah("单据下载服务接口地址未配置,请联系管理员"); } if (string.IsNullOrWhiteSpace(key)) { - throw Oops.Bah("正本下载服务接口Key未配置,请联系管理员"); + throw Oops.Bah("单据下载服务接口Key未配置,请联系管理员"); } - if (string.IsNullOrWhiteSpace(secret)) + if (string.IsNullOrWhiteSpace(token)) { - throw Oops.Bah("正本下载服务接口Secret未配置,请联系管理员"); + throw Oops.Bah("单据下载服务接口Token未配置,请联系管理员"); } var webAccount = _webAccountConfigService.GetAccountConfig("MSKWeb", UserManager.UserId).Result; @@ -2651,6 +2669,10 @@ namespace Myshipping.Application throw Oops.Bah("未配置网站账户,类型:MSKWeb"); } + if (string.IsNullOrWhiteSpace(order.ISSUETYPE)) + { + throw Oops.Bah("签单方式为空"); + } if (string.IsNullOrWhiteSpace(order.CARRIERID)) { throw Oops.Bah("船公司为空"); @@ -2664,7 +2686,7 @@ namespace Myshipping.Application carrierId = (carrierId ?? order.CARRIERID).ToUpper(); if (carrierId != "MSK") { - throw Oops.Bah($"[{order.CARRIER}]船公司此功能待开发"); + throw Oops.Bah($"船公司=[{order.CARRIER}]暂不支持正本下载"); } // 判断签单方式是否符合条件 @@ -2672,27 +2694,30 @@ namespace Myshipping.Application issueType = issueType ?? order.ISSUETYPE; if (!issueType.Equals("original", StringComparison.OrdinalIgnoreCase)) { - throw Oops.Bah("签单方式非[正本下载],无法下载正本"); + throw Oops.Bah("此单签单方式非[正本下载],无法下载正本"); } - url += ""; + // 实时下载接口 + url += "/documents_server/api/draft/v1/download_realtime"; var body = new { - //user_key = key, - //user_secret = secret, + web_code = carrierId, web_user = webAccount.Account, - web_psw = webAccount.Password, - //web_code = order.CarrierId, - //bno = order.MBLNO + web_pwd = webAccount.Password, + bill_no = order.MBLNO, + consumer_key = key, + iscontent = false, + }; + var header = new Dictionary() + { + { "Token",token }, { "Content-Type", "application/json" } }; - _logger.LogInformation("请求正本下载接口开始,mblno={mblno}, url={url}, body={body}", order.MBLNO, url, body); DateTime bDate = DateTime.Now; var rtn = await url.OnClientCreating(client => { - // client 为 HttpClient 对象 client.Timeout = TimeSpan.FromMinutes(3); // 设置超时时间 3分钟 - }).SetBody(body).PostAsStringAsync(); + }).SetBody(body).SetHeaders(header).PostAsStringAsync(); DateTime eDate = DateTime.Now; TimeSpan ts = eDate.Subtract(bDate); @@ -2701,76 +2726,77 @@ namespace Myshipping.Application _logger.LogInformation("请求正本下载接口结束,mblno={mblno}, rtn={rtn}, 耗时:{timeDiff}ms", order.MBLNO, rtn, timeDiff); var jsonRtn = JObject.Parse(rtn); - if (jsonRtn.GetIntValue("code") == 200) + if (jsonRtn.GetIntValue("status") == 1) { _logger.LogInformation("请求正本下载接口成功,转存本地,mblno={mblno}", order.MBLNO); - result.succ = true; - result.msg = "正本下载成功"; + var data = jsonRtn.GetJObjectValue("data"); - //string currFilePath = rlt.data.FirstOrDefault().path; + // 远程文件路径 + var remoteFilePath = data.GetStringValue("proxy_path"); + // 真实文件名 + var reallyFileName = data.GetStringValue("file_name"); - //string fileTypeCode = "bc"; - //string fileTypeName = "Booking Confirmation"; + // 读取文件保存配置 + var fileCfg = App.GetOptions(); - ////读取文件配置 - //var fileCfg = App.GetOptions(); + string relativePath = $"{fileCfg.relativePath}\\originalfiles\\{order.Id}"; + string relativeDic = $"{(!string.IsNullOrWhiteSpace(fileCfg.basePath) ? fileCfg.basePath : App.WebHostEnvironment.WebRootPath)}\\{relativePath}"; - //string relativePath = $"{fileCfg.relativePath}\\bcfiles\\{bookingOrder.Id}"; - //string filePath = $"{(!string.IsNullOrWhiteSpace(fileCfg.basePath) ? fileCfg.basePath : App.WebHostEnvironment.WebRootPath)}\\{relativePath}"; + string fileSavePath = $"{relativeDic}\\{new System.IO.FileInfo(remoteFilePath).Name}"; - //string fileFullName = $"{filePath}\\{new System.IO.FileInfo(currFilePath).Name}"; + if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + relativePath = relativePath.Replace("\\", "/"); + relativeDic = relativeDic.Replace("\\", "/"); - //if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) - //{ - // relativePath = relativePath.Replace("\\", "/"); - // filePath = filePath.Replace("\\", "/"); + fileSavePath = fileSavePath.Replace("\\", "/"); + } - // fileFullName = fileFullName.Replace("\\", "/"); - //} + _logger.LogInformation("mblno={mblno}, 生成文件保存路径完成 路由={relativeDic} 服务器系统={system}", order.MBLNO, relativeDic, RuntimeInformation.OSDescription); + //预先创建目录 + if (!Directory.Exists(relativeDic)) + { + Directory.CreateDirectory(relativeDic); + } - //_logger.LogInformation("批次={no} 生成文件保存路径完成 路由={filePath} 服务器系统={system}", batchNo, filePath, RuntimeInformation.OSDescription); - ////预先创建目录 - //if (!Directory.Exists(filePath)) - //{ - // Directory.CreateDirectory(filePath); - //} + var bcStream = await remoteFilePath.GetAsStreamAsync(); - //var bcStream = await currFilePath.GetAsStreamAsync(); + using (var fileStream = File.Create(fileSavePath)) + { + await bcStream.CopyToAsync(fileStream); + } - //using (var fileStream = File.Create(fileFullName)) - //{ - // await bcStream.CopyToAsync(fileStream); - //} + _logger.LogInformation("mblno={mblno} 完成文件保存 filepath={path}", order.MBLNO, fileSavePath); - //_logger.LogInformation("批次={no} 完成文件保存 filepath={path}", batchNo, fileFullName); + string bookFilePath = string.Empty; - //string bookFilePath = string.Empty; + if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + bookFilePath = System.Text.RegularExpressions.Regex.Match(fileSavePath, relativePath.Replace("/", "\\/") + ".*").Value; + } + else + { + bookFilePath = System.Text.RegularExpressions.Regex.Match(fileSavePath, relativePath.Replace("\\", "\\\\") + ".*").Value; + } - //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; - //} + result.succ = true; + result.msg = "正本下载成功"; - ////这里先写入附件表 - //await SaveEDIFile(bookingOrder.Id, bookFilePath, new System.IO.FileInfo(currFilePath).Name, - // fileTypeCode, fileTypeName); + await _bookingFileRepository.DeleteAsync(x => x.TypeCode == FileTypeCode && x.BookingId == order.Id); + await SaveEDIFile(order.Id, bookFilePath, reallyFileName, FileTypeCode, FileTypeName); } else { - var msg = jsonRtn.GetStringValue("msg")?.Replace("爬虫", ""); + var msg = jsonRtn.GetStringValue("message")?.Replace("爬虫", ""); _logger.LogInformation("请求正本下载接口失败,mblno={mblno}, 服务返回:{msg}", order.MBLNO, msg); throw Oops.Bah(msg); } - } catch (Exception ex) { result.succ = false; + result.status = -2; result.msg = $"正本下载失败,{ex.Message}"; } return result;