修改BC引入

optimize
jianghaiqing 2 years ago
parent 2588dbc655
commit c933c8d2f7

@ -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
{
/// <summary>
/// 文件附件帮助类
/// </summary>
public static class FileAttachHelper
{
#region 转移文件
/// <summary>
/// 转移文件
/// </summary>
/// <param name="fileDictKey">文件目录KEY</param>
/// <param name="sourceFilePath">源文件完整路径</param>
/// <param name="batchNo">批次号</param>
/// <param name="isLocalTempFile">是否生成本地文件</param>
/// <param name="attachFileType">附件类型 bcfiles-BC文件</param>
/// <returns>返沪新的文件路径</returns>
public static async Task<string> MoveFile(string fileDictKey,string sourceFilePath,string batchNo,
bool isLocalTempFile = false,string attachFileType = "bcfiles")
{
var logger = Log.CreateLogger(nameof(FileAttachHelper));
var fileCfg = App.GetOptions<BookingAttachOptions>();
string fileRoot = string.Empty;
if(fileCfg != null)
{
if(!isLocalTempFile)
{
if (!string.IsNullOrWhiteSpace(fileCfg.basePath))
{
fileRoot = fileCfg.basePath;
}
}
else
{
var opt = App.GetOptions<TempFileOptions>().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
}
}

@ -1297,7 +1297,7 @@ namespace Myshipping.Application
var opt = App.GetOptions<TempFileOptions>().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<UpdateBookingLetteryardInput>();
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

Loading…
Cancel
Save