@ -62,6 +62,12 @@ using Myshipping.Application.EDI.ESL;
using NPOI.HSSF.UserModel ;
using NPOI.HSSF.UserModel ;
using NPOI.SS.UserModel ;
using NPOI.SS.UserModel ;
using NPOI.SS.Formula.Functions ;
using NPOI.SS.Formula.Functions ;
using NPOI.HPSF ;
using MimeKit ;
using NPOI.SS.Formula ;
using NPOI.Util ;
using System.Collections.Specialized ;
using System.Net.Http.Headers ;
namespace Myshipping.Application
namespace Myshipping.Application
{
{
@ -3263,7 +3269,7 @@ namespace Myshipping.Application
var primaryModel = order . Adapt < MsOpSeaeEDIBaseModel > ( ) ;
var primaryModel = order . Adapt < MsOpSeaeEDIBaseModel > ( ) ;
//起运港是CNTAO并且船公司是太平需要判断场站EDI
//起运港是CNTAO并且船公司是太平需要判断场站EDI
if ( order . PORTLOADID = = "CNTAO" & & ediRouteEnum = = EDIRouteEnum . PIL )
if ( ( order . PORTLOADID = = "CNTAO" & & ediRouteEnum = = EDIRouteEnum . PIL ) | | ediRouteEnum = = EDIRouteEnum . WY )
{
{
//场站
//场站
var ediYardList = _cache . GetAllMappingYard ( ) . GetAwaiter ( ) . GetResult ( )
var ediYardList = _cache . GetAllMappingYard ( ) . GetAwaiter ( ) . GetResult ( )
@ -3581,20 +3587,44 @@ namespace Myshipping.Application
CancellationTokenSource cts = new CancellationTokenSource ( ) ;
CancellationTokenSource cts = new CancellationTokenSource ( ) ;
//后续发送
string host = string . Empty ;
var ftpPostObj = new
string port = string . Empty ;
if ( ediCfg . SERVERIP . IndexOf ( ":" ) > = 0 )
{
{
host = ediCfg . SERVERIP ,
host = ediCfg . SERVERIP . Split ( new char [ ] { ':' } ) . FirstOrDefault ( ) . Trim ( ) ;
username = ediCfg . USERNAME ,
port = ediCfg . SERVERIP . Split ( new char [ ] { ':' } ) . Last ( ) ? . Trim ( ) ;
pwd = ediCfg . PASSWORD ,
}
path = ediCfg . FOLDERNAME
} ;
NameValueCollection par = new NameValueCollection ( ) ;
par . Add ( "host" , host ) ;
par . Add ( "port" , port ) ;
par . Add ( "username" , ediCfg . USERNAME ) ;
par . Add ( "pwd" , ediCfg . PASSWORD ) ;
par . Add ( "path" , ediCfg . FOLDERNAME ) ;
var ftpSpiderUrl = _cache . GetAllDictData ( ) . GetAwaiter ( ) . GetResult ( ) . FirstOrDefault ( x = > x . TypeCode = = "url_set" & & x . Code = = "booking_edi_ftp_server" ) . Value ;
var ftpSpiderUrl = _cache . GetAllDictData ( ) . GetAwaiter ( ) . GetResult ( ) . FirstOrDefault ( x = > x . TypeCode = = "url_set" & & x . Code = = "booking_edi_ftp_server" ) . Value ;
var fileInfo = new FileInfo ( filePath ) ;
//var fileInfo = new FileInfo(filePath);
_logger . LogInformation ( $"准备请求发送ftp: {ftpSpiderUrl} ,参数:{JSON.Serialize(par)},文件:{filePath}" ) ;
_logger . LogInformation ( $"准备请求发送ftp: {ftpSpiderUrl} ,参数:{ftpPostObj.ToJsonString()},文件:{filePath}" ) ;
System . IO . FileStream file = new System . IO . FileStream ( filePath , FileMode . Open , FileAccess . Read ) ;
int SplitSize = 5242880 ; //5M分片长度
int index = 1 ; //序号 第几片
long StartPosition = 5242880 * ( index - 1 ) ;
long lastLens = file . Length - StartPosition ; //真不知道怎么起命了,就这样吧
if ( lastLens < 5242880 )
{
SplitSize = ( int ) lastLens ;
}
byte [ ] heByte = new byte [ SplitSize ] ;
file . Seek ( StartPosition , SeekOrigin . Begin ) ;
//第一个参数是 起始位置
file . Read ( heByte , 0 , SplitSize ) ;
//第三个参数是 读取长度(剩余长度)
file . Close ( ) ;
string strJoin = System . IO . File . ReadAllText ( filePath ) ;
string strJoin = System . IO . File . ReadAllText ( filePath ) ;
@ -3602,17 +3632,18 @@ namespace Myshipping.Application
_logger . LogInformation ( "FTP 开始上传" ) ;
_logger . LogInformation ( "FTP 开始上传" ) ;
var res = await ftpSpiderUrl
var res = TransmitFtpFile ( ftpSpiderUrl , par , new
. SetContentType ( "multipart/form-data" )
{
. SetBody ( ftpPostObj )
file = "file" ,
. SetBodyBytes ( ( "file" , Encoding . UTF8 . GetBytes ( strJoin ) , fileInfo . Name ) )
fileName = Path . GetFileName ( filePath ) ,
. PostAsStringAsync ( ) ;
fileBytes = heByte
} ) ;
DateTime eDate = DateTime . Now ;
DateTime eDate = DateTime . Now ;
TimeSpan ts = eDate . Subtract ( bDate ) ;
TimeSpan ts = eDate . Subtract ( bDate ) ;
var timeDiff = ts . TotalMilliseconds ;
var timeDiff = ts . TotalMilliseconds ;
_logger . LogInformation ( $"FTP 上传完成 上传文件大小:{ fileInfo .Length} 用时:{timeDiff}ms.,{strJoin}") ;
_logger . LogInformation ( $"FTP 上传完成 上传文件大小:{ heByte .Length} 用时:{timeDiff}ms.,{strJoin}") ;
_logger . LogInformation ( $"发送ftp返回: {res}" ) ;
_logger . LogInformation ( $"发送ftp返回: {res}" ) ;
@ -3628,6 +3659,76 @@ namespace Myshipping.Application
}
}
# endregion
# endregion
# region
/// <summary>
/// 转发EDI内部方法
/// </summary>
/// <param name="requestUrl">请求接口地址</param>
/// <param name="nameValueCollection">键值对参数</param>
/// <param name="fileInfo">文件信息</param>
/// <param name="contentType">默认 application/json</param>
/// <returns>返回结果</returns>
private static string TransmitFtpFile ( string requestUrl , NameValueCollection nameValueCollection , dynamic fileInfo ,
string contentType = "application/json" )
{
var result = string . Empty ;
using ( var httpClient = new HttpClient ( ) )
{
try
{
using ( var reduceAttach = new MultipartFormDataContent ( ) )
{
string [ ] allKeys = nameValueCollection . AllKeys ;
foreach ( string key in allKeys )
{
var dataContent = new ByteArrayContent ( Encoding . UTF8 . GetBytes ( nameValueCollection [ key ] ) ) ;
dataContent . Headers . ContentDisposition = new ContentDispositionHeaderValue ( $"form-data" )
{
Name = key
} ;
reduceAttach . Add ( dataContent ) ;
}
#region 文件参数
if ( fileInfo ! = null )
{
var Content = new ByteArrayContent ( fileInfo . fileBytes ) ;
//Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
//{
// Name = fileInfo.file.ToString(),
// FileName = fileInfo.fileName.ToString(),
//};
Content . Headers . Add ( "Content-Type" , contentType ) ;
reduceAttach . Add ( Content , fileInfo . file . ToString ( ) , HttpUtility . UrlEncode ( fileInfo . fileName . ToString ( ) ) ) ;
}
# endregion
//请求
var response = httpClient . PostAsync ( requestUrl , reduceAttach ) . Result ;
result = response . Content . ReadAsStringAsync ( ) . Result ;
}
}
catch ( Exception ex )
{
result = JSON . Serialize ( new
{
message = $"{nameof(TransmitFtpFile)} 转发EDI内部方法异常, 原因: {ex.Message}" ,
status = 0
} ) ;
}
}
return result ;
}
# endregion
#region 触发订舱
#region 触发订舱
/// <summary>
/// <summary>
/// 触发订舱
/// 触发订舱