using Furion;
using Furion.Logging;
using Furion.RemoteRequest.Extensions;
using Microsoft.Extensions.Logging;
using Myshipping.Application.ConfigOption;
using Myshipping.Application.Entity;
using Myshipping.Application.Helper;
using Myshipping.Core;
using Myshipping.Core.Entity;
using Myshipping.Core.Helper;
using Myshipping.Core.Service;
using Newtonsoft.Json.Linq;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Myshipping.Application.EDI
{
///
/// 下货纸辅助类
///
public static class XiahuozhiHelpler
{
private static readonly string EdiFilePath = "XHZ";
public static async Task> Send(
long bookingId, string filerole)
{
var repUser = App.GetService>();
var repOrder = App.GetService>();
var repCtn = App.GetService>();
var repBookingFile = App.GetService>();
var repUserMail = App.GetService>();
var cacheService = App.GetService();
var order = repOrder.FirstOrDefault(o => o.Id == bookingId);
if (order == null)
{
return new KeyValuePair(false, "订舱信息未找到");
}
var yardArr = new string[] { "GLJ", "GJF", "JIEFENG", "GANGLIANXIN", "ZHONGCHUANG" };
if (!yardArr.Contains(order.YARDID.ToUpper()))
{
return new KeyValuePair(false, "不支持的场站");
}
if (string.IsNullOrEmpty(order.MBLNO))
{
return new KeyValuePair(false, "主提单号不能为空");
}
if (string.IsNullOrEmpty(order.VESSEL))
{
return new KeyValuePair(false, "船名不能为空");
}
if (string.IsNullOrEmpty(order.VOYNO))
{
return new KeyValuePair(false, "海关航次不能为空");
}
var fileOpt = App.GetOptions();
if (string.IsNullOrEmpty(fileOpt.basePath))
{
fileOpt.basePath = App.WebHostEnvironment.WebRootPath;
}
#region 港联捷场站
if (order.YARDID == "GLJ")
{
//租户参数-港联捷下货纸代号
var paraXHZ = (await cacheService.GetAllTenantParam()).FirstOrDefault(p => p.TenantId == order.TenantId && p.ParaCode == "CODE_GLJ_XHZ");
if (paraXHZ == null || string.IsNullOrEmpty(paraXHZ.ItemCode))
{
return new KeyValuePair(false, $"{order.TenantName} 港联捷场站 下货纸代号未找到或配置有误");
}
var ediFileName = $"{paraXHZ.ItemCode}_{order.MBLNO}_{DateTime.Now.Ticks}.txt";
var ediFileRela = Path.Combine(fileOpt.relativePath, EdiFilePath, ediFileName);
var ediPathAbs = Path.Combine(fileOpt.basePath, fileOpt.relativePath, EdiFilePath);
var ediFileAbs = Path.Combine(ediPathAbs, ediFileName);
if (!Directory.Exists(ediPathAbs))
{
Directory.CreateDirectory(ediPathAbs);
}
var ediSettints = await cacheService.GetAllEdiSetting();
var dicData = await cacheService.GetAllDictData();
//港联捷所有用户使用一个ftp用户名和密码发送报文,使用文件名中的下货纸代号进行区分
var paraServer = dicData.FirstOrDefault(x => x.TypeCode == "booking_ftp_setting_glj" && x.Code == "ftp_server");
var paraUser = dicData.FirstOrDefault(x => x.TypeCode == "booking_ftp_setting_glj" && x.Code == "username");
var paraPwd = dicData.FirstOrDefault(x => x.TypeCode == "booking_ftp_setting_glj" && x.Code == "pwd");
if (paraServer == null || paraUser == null || paraPwd == null)
{
return new KeyValuePair(false, $"港联捷下货纸FTP服务地址参数未配置");
}
StringBuilder sbData = new StringBuilder();
sbData.AppendLine($"01:{order.VESSEL}:{order.VOYNO}"); //船名 航次
sbData.AppendLine($"12:{order.MBLNO}:{order.TRANSPORTID}:{order.PORTDISCHARGEID}:{paraXHZ.ItemCode}:{order.CARRIERID}::{ExchangeStr(order.YARDREMARK)}"); //主提单 中转港代码 目的港代码 货代代码 船公司 HPL参考号 备注
//2021-8-17,报文中不能体现英文字符①【:】②【,】,以空格代替
/*
* 表象:QDAC064651 客户毛重28600 EDI进来28
港联捷回复:是前面货名太长,后面内容截断了,每行不超过255字符就行
解决方案:每行不超过255,品名只取前100就行
*/
var despStr = ExchangeStr(order.DESCRIPTION.Replace(":", " ").Replace(",", " ")).Replace("\r\n", " ").Replace("\n", " ");
if (despStr.Length > 100)
{
despStr = despStr.Substring(0, 100);
}
sbData.AppendLine($"22:{despStr}:{order.PKGS}:{order.KINDPKGS}:{order.KGS}:{order.CBM}:{(order.CARGOID == "D" ? "Y" : "N")}:{order.DCLASS}:{order.DUNNO}::{order.TEMPSET}:{order.REEFERF}:{order.HUMIDITY}");//货名 件数 包装代码 总重量 总体积 危品标志 危品等级 危规号 副危品等级 副危规号 温度 通风度 湿度
var ctnCodes = (await GetAllCodeCtn()).Select(x => new { x.Code, x.Size }).ToList();
var ctnEdiList = (await GetAllMappingCtn()).AsQueryable().Where(x => x.Module == "XHZ_GLJ").ToList();
var ctns = repCtn.Where(c => c.BILLID == bookingId).ToList();
foreach (var ctn in ctns)
{
var ctnFind = ctnCodes.FirstOrDefault(c => c.Code == ctn.CTNCODE);
if (ctnFind == null)
{
return new KeyValuePair(false, $"未找到箱型 {ctn.CTNALL},请联系管理员");
}
var ctnEdi = ctnEdiList.FirstOrDefault(cc => cc.Code == ctn.CTNCODE);
if (ctnEdi == null)
{
return new KeyValuePair(false, $"箱型({ctn.CTNALL})不匹配(EDI类型:XHZ_GLJ),无法发送下货纸,请联系管理员");
}
sbData.AppendLine($"13:{(order.ISCONTAINERSOC.HasValue && order.ISCONTAINERSOC.Value ? "SOC" : order.CARRIERID)}:{ctnFind.Size}:{ctnEdi.MapCode}:{ctn.CTNNUM}:{ExchangeStr(ctn.REMARK)}:N");//箱经营人代码 尺寸 箱型 箱数 备注 加重箱
}
Log.Information($"生成下货纸报文:{sbData.ToString()}");
File.WriteAllText(ediFileAbs, sbData.ToString());
//挂载附件
var attFile = new BookingFile();
attFile.BookingId = order.Id;
attFile.FileName = ediFileName;
attFile.FilePath = ediFileRela;
attFile.TypeCode = "xiahuozhi";
attFile.TypeName = "下货纸";
attFile.TenantName = order.TenantName;
repBookingFile.Insert(attFile);
//发送ftp
SplitFtpServerAndPort(paraServer.Value, out string server, out int port);
var ftpSpiderUrl = dicData.FirstOrDefault(x => x.TypeCode == "url_set" && x.Code == "booking_edi_ftp_server").Value;
NameValueCollection par = new NameValueCollection();
par.Add("host", server);
par.Add("port", port.ToString());
par.Add("username", paraUser.Value);
par.Add("pwd", paraPwd.Value);
par.Add("path", "/");
Log.Information($"准备请求发送ftp:{ftpSpiderUrl} ,参数:{par.ToJsonString()},文件:{ediFileName}");
var res = FTPHelper.TransmitFtpFile(ftpSpiderUrl, par, new
{
file = "file",
fileName = Path.GetFileName(ediFileName),
fileBytes = Encoding.UTF8.GetBytes(sbData.ToString())
});
Log.Information($"发送ftp返回:{res}");
var jobjRetn = JObject.Parse(res);
if (jobjRetn.GetStringValue("status") != "1")
{
return new KeyValuePair(false, $"ftp发送失败:{jobjRetn.GetStringValue("message")}");
}
return new KeyValuePair(true, $"已发送");
}
#endregion
#region 港捷丰场站 捷丰场站 NOTE:港捷丰场站和捷丰场站下货纸功能,根据东胜7同功能模块代码翻译而来
else if (order.YARDID == "GJF" || order.YARDID == "JIEFENG")
{
if (!order.PKGS.HasValue || order.PKGS.Value == 0)
{
return new KeyValuePair(false, $"件数不能为空");
}
if (string.IsNullOrEmpty(order.KINDPKGS))
{
return new KeyValuePair(false, $"件数包装不能为空");
}
if (!order.KGS.HasValue || order.KGS.Value == 0)
{
return new KeyValuePair(false, $"重量不能为空");
}
if (!order.CBM.HasValue || order.CBM.Value == 0)
{
return new KeyValuePair(false, $"尺码不能为空");
}
var ediFileName = $"{order.VESSEL}_{order.VOYNO}_{order.MBLNO}_{DateTime.Now.Ticks}.txt";
var ediFileRela = Path.Combine(fileOpt.relativePath, EdiFilePath, ediFileName);
var ediPathAbs = Path.Combine(fileOpt.basePath, fileOpt.relativePath, EdiFilePath);
var ediFileAbs = Path.Combine(ediPathAbs, ediFileName);
if (!Directory.Exists(ediPathAbs))
{
Directory.CreateDirectory(ediPathAbs);
}
DjyEdiSetting ftpset = null;
if (order.YARDID == "GJF")
{
ftpset = (await cacheService.GetAllEdiSetting()).FirstOrDefault(f => f.EDICODE == "XHZ_GJF" && f.TenantId == order.TenantId);
}
else if (order.YARDID == "JIEFENG")
{
ftpset = (await cacheService.GetAllEdiSetting()).FirstOrDefault(f => f.EDICODE == "XHZ_JIEFENG" && f.TenantId == order.TenantId);
}
if (ftpset == null)
{
return new KeyValuePair(false, $"ftp设置未找到");
}
var ediParaName = "XHZ_GJF";
if (order.YARDID == "JIEFENG")
{
ediParaName = "XHZ_JIEFENG";
}
List listJoinStr = new List();
listJoinStr.Add("00:IFCSUM:BK:" + filerole + ":" + ftpset.SENDCODE + ":" + ftpset.RECEIVECODE + ":" + DateTime.Now.ToString("yyyyMMddHHmm") + ":2.1'");
//提单号
listJoinStr.Add("02:" + order.Id.ToString() + ":" + order.MBLNO.Trim() + ":" + order.SERVICE + ":" + ftpset.SENDNAME + ":" + ftpset.RECEIVECODE + "::::::::::" + order.CONTRACTNO + "::'");
//提单地址
var ISSUETYPE = "";
if (order.ISSUETYPE == "正本")
{
ISSUETYPE = "ORI";
}
else
{
ISSUETYPE = "EXP";
}
listJoinStr.Add("03:" + ISSUETYPE + ":" + order.ISSUEPLACEID + ":" + order.ISSUEPLACE + ":" + GetDateStr(order.ISSUEDATE, "yyyyMMdd") + ":" + GetBillNum2(order.NOBILL) + ":" + order.PORTLOADID + ":" + order.PREPARDAT + ":" + order.PAYABLEAT + "::'");//美国AMS标志、加拿大AMS标志
//船
listJoinStr.Add("11:" + GetVesselEDICode(order.VESSEL, ediParaName) + ":" + order.VESSEL + ":" + order.VOYNO + ":::" + GetCarrierEDICode(order.CARRIERID, ediParaName) + "::" + GetDateStr(order.ETD, "yyyyMMdd") + "::::::" + order.YARDCONTRACT + ":" + formatEdiStr("txt", order.YARDREMARK) + ":'");
//港口 (咨询韩工,代码不转换,直接传原始代码)
//listJoinStr.Add("12:" + GetPortEDICode(commonData, order.PLACERECEIPTID, type) + ":" + order.PLACERECEIPT + ":" + GetPortEDICode(order.PORTLOADID, type) + ":" + order.PORTLOAD + ":" + GetPortEDICode(order.PORTDISCHARGEID, type) + ":" + order.PORTDISCHARGE + ":::" + GetPortEDICode(order.PLACEDELIVERYID, type) + ":" + order.PLACEDELIVERY + ":" + GetPortEDICode(order.DESTINATIONID, type) + ":" + order.DESTINATION + "'");
listJoinStr.Add("12:" + order.PLACERECEIPTID + ":" + order.PLACERECEIPT + ":" + order.PORTLOADID + ":" + order.PORTLOAD + ":" + order.PORTDISCHARGEID + ":" + order.PORTDISCHARGE + ":::" + order.PLACEDELIVERYID + ":" + order.PLACEDELIVERY + ":" + order.DESTINATIONID + ":" + order.DESTINATION + "'");
//付费方式
var frtstr = GetFrtEDICode(order.BLFRT);
if (frtstr != "")
{
listJoinStr.Add("14:" + frtstr + ":" + order.BLFRT + "'");
listJoinStr.Add("15:::" + frtstr + ":::::::'");
}
else
{
listJoinStr.Add("14:P:" + order.BLFRT + "'");
listJoinStr.Add("15:::P:::::::'");
}
var Shipping = "";
List ShippingList = null;
//2021年9月1日修改,17行取消,放到11行原FAX位置(于斐)
////EDI备注
//if (!string.IsNullOrWhiteSpace(order.YardRemark))
//{
// Shipping = formatEdiStr("txt", order.YardRemark);
// ShippingList = formatlengthStr(Shipping, 70);
// if (Shipping != "")
// {
// listJoinStr.Add("17:" + formatListStr(ShippingList, 5) + "'");
// }
//}
//发货人
Shipping = formatEdiStr("txt", order.SHIPPER);
ShippingList = formatlengthStr(Shipping, 35);
if (ShippingList.Count != 0 && Shipping.Length > 0)
{
listJoinStr.Add("18::" + formatListStr(ShippingList, 6) + "'");
}
//收货人
Shipping = formatEdiStr("txt", order.CONSIGNEE);
ShippingList = formatlengthStr(Shipping, 35);
if (ShippingList.Count != 0 && Shipping.Length > 0)
{
listJoinStr.Add("19::" + formatListStr(ShippingList, 6) + "'");
}
//通知人
Shipping = formatEdiStr("txt", order.NOTIFYPARTY);
ShippingList = formatlengthStr(Shipping, 35);
if (ShippingList.Count != 0 && Shipping.Length > 0)
{
listJoinStr.Add("20::" + formatListStr(ShippingList, 6) + "'");
}
//货物标识
var cargoid = order.CARGOID;
if (string.IsNullOrWhiteSpace(cargoid))
{
cargoid = "S";
}
listJoinStr.Add("41:1::" + cargoid + ":" + order.PKGS.ToString() + ":" + order.KINDPKGS + ":" + order.KINDPKGS + ":" + order.KGS.ToString() + ":" + order.CBM.ToString() + "::::::" + order.KGS.ToString() + ":::'");//bill.HSCODE
if (cargoid == "R")
{
listJoinStr.Add("43:::::::::::" + order.REEFERF + ":C:" + order.TEMPSET + ":" + order.TEMPMIN + ":" + order.TEMPMAX + "::::::" + order.HUMIDITY + "::::'");
}
else if (cargoid == "D")
{
listJoinStr.Add("43:" + order.DCLASS + ":" + order.DPAGE + ":" + order.DUNNO + ":" + order.DLABEL + "::::::::::::::::::::'");
}
//唛头
Shipping = formatEdiStr("txt", order.MARKS);
ShippingList = formatlengthStr(Shipping, 35);
for (var i = 0; i < Math.Ceiling(Convert.ToDecimal(Convert.ToDecimal(ShippingList.Count) / Convert.ToDecimal(9))); i++)
{
var tempstr = "44:";
for (var z = 0; z < 9; z++)
{
if ((i * 9 + z) < ShippingList.Count)
tempstr = tempstr + ShippingList[i * 9 + z];
if (z < 8) tempstr = tempstr + ":";
}
if (tempstr != "44:")
{
listJoinStr.Add(tempstr + "'");
}
}
//货物描述
Shipping = formatEdiStr("txt", order.DESCRIPTION);
ShippingList = formatlengthStr(Shipping, 70);
var m = 1;
var strtemp = "";
if (ShippingList.Count != 0 && Shipping.Length > 0)
{
for (var i = 0; i < ShippingList.Count; i++)
{
if (m <= 5)
{
if (m == 1) strtemp = "47:" + ShippingList[i] + ":";
else
{
if (m == 5)
strtemp = strtemp + ShippingList[i];
else
strtemp = strtemp + ShippingList[i] + ":";
}
}
else
{
m = 1;
listJoinStr.Add(strtemp + "'");
strtemp = "47:" + ShippingList[i] + ":";
}
m = m + 1;
}
}
if (strtemp != "")
{
listJoinStr.Add(strtemp + "'");
}
//集装箱
var ctns = repCtn.Where(c => c.BILLID == bookingId).ToList();
var groupList = ctns.Where(c => c.CTNNUM.HasValue).GroupBy(c => c.CTNCODE).Select(c => new { c.Key, CTNNUM = c.Sum(cc => cc.CTNNUM) }).ToList();
foreach (var g in groupList)
{
listJoinStr.Add("48:" + GetCtnEDICode(g.Key, ediParaName) + ":" + g.CTNNUM + $":F:::::{(order.ISCONTAINERSOC.HasValue && order.ISCONTAINERSOC.Value ? "Y" : "N")}'");
}
foreach (var ctn in ctns)
{
if (ctn.CNTRNO != "")
{
listJoinStr.Add("51:" + ctn.CNTRNO + ":" + GetCtnEDICode(ctn.CTNCODE, ediParaName) + ":" + ctn.SEALNO + ":M:CA:F:" + ctn.PKGS.ToString() + ":" + ctn.KGS.ToString() + "::" + ctn.CBM + "'");
}
}
listJoinStr.Add("99:" + listJoinStr.Count + 1 + "'");
var strJoin = string.Join(Environment.NewLine, listJoinStr);
Log.Information($"生成下货纸报文:{strJoin}");
File.WriteAllText(ediFileAbs, strJoin, Encoding.Default);
//挂载附件
var attFile = new BookingFile();
attFile.BookingId = order.Id;
attFile.FileName = ediFileName;
attFile.FilePath = ediFileRela;
attFile.TypeCode = "xiahuozhi";
attFile.TypeName = "下货纸";
attFile.TenantName = order.TenantName;
await repBookingFile.InsertAsync(attFile);
//发送ftp
SplitFtpServerAndPort(ftpset.SERVERIP, out string server, out int port);
var ftpSpiderUrl = (await cacheService.GetAllDictData()).FirstOrDefault(x => x.TypeCode == "url_set" && x.Code == "booking_edi_ftp_server").Value;
NameValueCollection par = new NameValueCollection();
par.Add("host", server);
par.Add("port", port.ToString());
par.Add("username", ftpset.USERNAME);
par.Add("pwd", ftpset.PASSWORD);
par.Add("path", ftpset.FOLDERNAME);
Log.Information($"准备请求发送ftp:{ftpSpiderUrl} ,参数:{par.ToJsonString()},文件:{ediFileName}");
var res = FTPHelper.TransmitFtpFile(ftpSpiderUrl, par, new
{
file = "file",
fileName = Path.GetFileName(ediFileName),
fileBytes = Encoding.UTF8.GetBytes(strJoin)
});
Log.Information($"发送ftp返回:{res}");
var jobjRetn = JObject.Parse(res);
if (jobjRetn.GetStringValue("status") != "1")
{
return new KeyValuePair(false, $"ftp发送失败:{jobjRetn.GetStringValue("message")}");
}
return new KeyValuePair(true, $"完成");
}
#endregion
#region 港联欣场站
else if (order.YARDID.ToUpper() == "GANGLIANXIN" || order.YARDID.ToUpper() == "ZHONGCHUANG")
{
//2022年6月15日增加:港联欣场站若配置了通过港联捷EDI发送,则使用港联捷报文格式
var tenantParam = await cacheService.GetAllTenantParam();
var pGlxEdi = tenantParam.FirstOrDefault(p => p.TenantId == order.TenantId && p.ParaCode == "XIAHUOZHI_GLX_SEND_TYPE");
if (order.YARDID.ToUpper() == "GANGLIANXIN" && pGlxEdi != null && pGlxEdi.ItemCode == "EDI_GLX")
{
var ftpset = (await cacheService.GetAllEdiSetting()).FirstOrDefault(f => f.EDICODE == "XHZ_GLX" && f.TenantId == order.TenantId);
if (ftpset == null)
{
return new KeyValuePair(false, $"港联欣下货纸ftp设置未找到");
}
var paraXHZ = tenantParam.FirstOrDefault(p => p.TenantId == order.TenantId && p.ParaCode == "CODE_GLX_XHZ");
if (paraXHZ == null || string.IsNullOrEmpty(paraXHZ.ItemCode))
{
return new KeyValuePair(false, $"下货纸代号未找到或配置有误");
}
var ediFileName = $"{paraXHZ.ItemCode}_{order.MBLNO}_{DateTime.Now.Ticks}.txt";
var ediFileRela = Path.Combine(fileOpt.relativePath, EdiFilePath, ediFileName);
var ediPathAbs = Path.Combine(fileOpt.basePath, fileOpt.relativePath, EdiFilePath);
var ediFileAbs = Path.Combine(ediPathAbs, ediFileName);
if (!Directory.Exists(ediPathAbs))
{
Directory.CreateDirectory(ediPathAbs);
}
StringBuilder sbData = new StringBuilder();
sbData.AppendLine($"01:{order.VESSEL}:{order.VOYNO}"); //船名 航次
sbData.AppendLine($"12:{order.MBLNO}:{order.TRANSPORTID}:{order.PORTDISCHARGEID}:{paraXHZ.ItemCode}:{order.CARRIERID}::{order.PORTDISCHARGE}"); //主提单 中转港代码 目的港代码 货代代码 船公司 HPL参考号 备注
var despStr = ExchangeStr(order.DESCRIPTION.Replace(":", " ").Replace(",", " ")).Replace("\r\n", " ").Replace("\n", " ");
if (despStr.Length > 100)
{
despStr = despStr.Substring(0, 100);
}
sbData.AppendLine($"22:{despStr}:{order.PKGS}:{order.KINDPKGS}:{order.KGS}:{order.CBM}:{(order.CARGOID == "D" ? "Y" : "N")}:{order.DCLASS}:{order.DUNNO}::::{order.TEMPSET}:{order.REEFERF}:{order.HUMIDITY}:");//货名 件数 包装代码 总重量 总体积 危品标志 危品等级 危规号 副危品等级 副危规号 温度 通风度 湿度
var ctnCodes = (await GetAllCodeCtn()).Select(x => new { x.Code, x.Size }).ToList();
var ctnEdiList = (await GetAllMappingCtn()).AsQueryable().Where(x => x.Module == "XHZ_GLX").ToList();
var ctns = repCtn.Where(c => c.BILLID == bookingId).ToList();
foreach (var ctn in ctns)
{
var ctnFind = ctnCodes.FirstOrDefault(c => c.Code == ctn.CTNCODE);
if (ctnFind == null)
{
return new KeyValuePair(false, $"未找到箱型 {ctn.CTNALL},请联系管理员");
}
var ctnEdi = ctnEdiList.FirstOrDefault(cc => cc.Code == ctn.CTNCODE);
if (ctnEdi == null)
{
return new KeyValuePair(false, $"箱型({ctn.CTNALL})不匹配(EDI类型:XHZ_GLX),无法发送下货纸,请联系管理员");
}
sbData.AppendLine($"13:{(order.ISCONTAINERSOC.HasValue && order.ISCONTAINERSOC.Value ? "SOC" : order.CARRIERID)}:{ctnFind.Size}:{ctnEdi.MapCode}:{ctn.CTNNUM}:{ExchangeStr(ctn.REMARK)}:N");//箱经营人代码 尺寸 箱型 箱数 备注 加重箱
}
Log.Information($"生成下货纸报文:{sbData.ToString()}");
File.WriteAllText(ediFileAbs, sbData.ToString());
//挂载附件
var attFile = new BookingFile();
attFile.BookingId = order.Id;
attFile.FileName = ediFileName;
attFile.FilePath = ediFileRela;
attFile.TypeCode = "xiahuozhi";
attFile.TypeName = "下货纸";
attFile.TenantName = order.TenantName;
repBookingFile.Insert(attFile);
//发送ftp
SplitFtpServerAndPort(ftpset.SERVERIP, out string server, out int port);
var ftpSpiderUrl = (await cacheService.GetAllDictData()).FirstOrDefault(x => x.TypeCode == "url_set" && x.Code == "booking_edi_ftp_server").Value;
NameValueCollection par = new NameValueCollection();
par.Add("host", server);
par.Add("port", port.ToString());
par.Add("username", ftpset.USERNAME);
par.Add("pwd", ftpset.PASSWORD);
par.Add("path", ftpset.FOLDERNAME);
Log.Information($"准备请求发送ftp:{ftpSpiderUrl} ,参数:{par.ToJsonString()},文件:{ediFileName}");
var res = FTPHelper.TransmitFtpFile(ftpSpiderUrl, par, new
{
file = "file",
fileName = Path.GetFileName(ediFileName),
fileBytes = Encoding.UTF8.GetBytes(sbData.ToString())
});
Log.Information($"发送ftp返回:{res}");
var jobjRetn = JObject.Parse(res);
if (jobjRetn.GetStringValue("status") != "1")
{
return new KeyValuePair(false, $"ftp发送失败:{jobjRetn.GetStringValue("message")}");
}
}
else //邮件方式
{
if (string.IsNullOrEmpty(order.YARDCONTRACTEMAIL))
{
return new KeyValuePair(false, $"场站联系人邮箱不能为空");
}
var userMail = repUserMail.FirstOrDefault(x => x.CreatedUserId == order.CreatedUserId);
if (userMail == null
|| string.IsNullOrEmpty(userMail.SmtpServer)
|| userMail.SmtpPort == 0)
{
return new KeyValuePair(false, $"发件邮箱未配置");
}
var userbase = repUser.FirstOrDefault(u => u.Id == order.CreatedUserId);
var title = $"下货纸:{order.MBLNO}/{order.CARRIER}/{order.VESSEL}/{order.VOYNO}/{order.TenantName}";
var bodyExt = string.Empty;
if (order.CARGOID == "R")
{
bodyExt = $@"温度:{order.TEMPSET}{order.TEMPID}
通风:{order.REEFERF}
湿度:{order.HUMIDITY}
";
}
else if (order.CARGOID == "D")
{
bodyExt = $@"危险品等级:{order.DCLASS}
危险品编号:{order.DUNNO}
危险品联系方式:{order.LINKMAN}
";
}
var body = $@"提单号:{order.MBLNO}
船名航次:{order.VESSEL}/{order.VOYNO}
起运港:{order.PORTLOAD},{order.PORTLOADID}
目的港:{order.PORTDISCHARGE},{order.PORTDISCHARGEID}
目的地:{order.DESTINATION},{order.DESTINATIONID}
预计船期:{(order.ETD.HasValue ? order.ETD.Value.ToString("yyyy-MM-dd") : "")}
箱型箱量:{order.CNTRTOTAL}
件重尺:{order.PKGS} {order.KINDPKGS}/{order.KGS}KGS/{order.CBM}CBM
货物描述:{order.DESCRIPTION}
{bodyExt}
备注:{order.YARDREMARK}
订舱代理:{order.TenantName}
联系人:{order.CreatedUserName}
电话:{userbase.Tel} 手机:{userbase.Phone}
邮箱:{userbase.Email}
";
var sendResult = await MailSendHelper.SendMail(userMail, title, body, order.YARDCONTRACTEMAIL);
if (!sendResult.Key)
{
return new KeyValuePair(false, sendResult.Value);
}
}
return new KeyValuePair(true, "已发送");
}
#endregion
else
{
return new KeyValuePair(false, "不支持的场站");
}
}
//转义处理字符串中的特殊字符
private static string ExchangeStr(string str)
{
if (string.IsNullOrEmpty(str))
{
return string.Empty;
}
return str.Replace("?", "??").Replace("'", "?'").Replace("+", "?+").Replace(":", "?:");
}
//根据船名,获取船名edi代码(根据东胜7同功能模块代码翻译而成)
public static string GetVesselEDICode(string vessel, string EDINAME)
{
string EDICODE = "";
vessel = vessel.Replace("'", "''");
var codeVesselEdi = GetAllMappingVessel().Result.FirstOrDefault(v => v.Name == vessel && v.Module == EDINAME);
if (codeVesselEdi != null)
{
EDICODE = codeVesselEdi.MapName.Trim();
}
if (string.IsNullOrEmpty(EDICODE))
{
var codeVessel = GetAllCodeVessel().Result.FirstOrDefault(v => v.Name == vessel);
if (codeVessel != null && !string.IsNullOrEmpty(codeVessel.EdiCode))
{
EDICODE = codeVessel.EdiCode.Trim();
}
else
{
EDICODE = vessel;
}
}
return EDICODE;
}
//获取时间字符串
public static string GetDateStr(DateTime? dt, string dateformat)
{
var result = "";
if (!dt.HasValue)
{
result = "";
}
else
{
result = dt.Value.ToString(dateformat);
}
return result;
}
//获取客户edi代码(根据东胜7同功能模块代码翻译而成)
public static string GetCarrierEDICode(string carrierCode, string EDINAME)
{
string EDICODE = "";
var custEdi = GetAllMappingCarrier().Result.FirstOrDefault(c => c.Code == carrierCode && c.Module == EDINAME);
if (custEdi != null)
{
EDICODE = custEdi.MapCode.Trim();
}
if (string.IsNullOrEmpty(EDICODE))
{
var infoclient = GetAllCodeCarrier().Result.FirstOrDefault(c => c.Code == carrierCode);
if (infoclient != null)
{
EDICODE = infoclient.EdiCode.Trim();
}
}
return EDICODE;
}
//获取付费方式edi代码(根据东胜7同功能模块代码翻译而成)
public static string GetFrtEDICode(string FRT)
{
string EDICODE = "P";
var ediFRT = GetAllMappingFrt().Result.FirstOrDefault(c => c.Code == FRT);
if (ediFRT != null)
{
EDICODE = ediFRT.MapCode.Trim();
}
return EDICODE;
}
//获取edi代码(根据东胜7同功能模块移植)
public static string GetBillNum2(string str)
{
if (str == "ZERO") return "00";
if (str == "ONE") return "01";
if (str == "TWO") return "02";
if (str == "THREE") return "03";
if (str == "FOUR") return "04";
if (str == "FIVE") return "05";
if (str == "SIX") return "06";
if (str == "SERVEN") return "07";
if (str == "EIGHT") return "08";
if (str == "NINE") return "09";
if (str == "TEN") return "10";
else return "";
}
///
/// 各种文本转义字符(东胜7移植)
///
/// 文件类型(例如:txt、xml)
/// 文本字符串
///
public static string formatEdiStr(string fileType, string str)
{
if (str == null)
str = string.Empty;
if (str == null)
{
return string.Empty;
}
if (fileType == "txt")
{
return str.Replace("?", "??").Replace(":", "?:").Replace("+", "?+").Replace("'", "?'");
}
else if (fileType == "xml")
{
return str.Replace("&", "&").Replace("<", "<").Replace(">", ">").Replace("\"", """).Replace("'", "'");
}
else if (fileType == "sitc")
{
return str.Replace("?", "??").Replace(":", "?:").Replace("'", "?'");
}
else
{
return str;
}
}
//东胜7移植
public static List formatlengthStr(string str, int length, bool formatstr = false, bool nodelsp = false)
{
str = str.Replace("\r\n", "\\");
str = str.Replace("\n", "\\");
str = str.Replace("\r", " ");
string[] StrList = str.Split('\\');
//List StrListtmp = new List();
//for (var i = StrList.Length - 1; i >=0; i--)
//{
//}
var strtemp = "";
var strnewline = "";
var strtempnewline = "";
char[] spstring = { ' ', ';', ',', '.', ':', '/', '(', ')', '?', '+', '-' };
List DestList = new List();
for (var i = 0; i <= StrList.Length - 1; i++)
{
if (StrList[i].Length <= length)
{
if (formatstr)
DestList.Add(formatEdiStr("txt", StrList[i]));
else
DestList.Add(StrList[i]);
}
else
{
strtemp = StrList[i] + " ";
strtempnewline = "";
strnewline = "";
for (var j = 0; j < strtemp.Length; j++)
{
strtempnewline = strtempnewline + strtemp[j];
if (strtemp[j] == ' ' || strtemp[j] == ':' || strtemp[j] == ',' || strtemp[j] == '.' || strtemp[j] == ':' || strtemp[j] == '/'
|| strtemp[j] == '?' || strtemp[j] == ')' || strtemp[j] == '}' || strtemp[j] == '+' || strtemp[j] == '-')
{
if ((strnewline.Length + strtempnewline.Length) <= length)
strnewline = strnewline + strtempnewline;
else
{
if (formatstr)
DestList.Add(formatEdiStr("txt", strnewline));
else
DestList.Add(strnewline);
strnewline = strtempnewline;
}
strtempnewline = "";
}
}
if (nodelsp)
{
if (formatstr)
DestList.Add(formatEdiStr("txt", strnewline));
else DestList.Add(strnewline);
}
else if (strnewline.Trim() != "")
{
if (formatstr)
DestList.Add(formatEdiStr("txt", strnewline));
else DestList.Add(strnewline);
}
}
}
return DestList;
}
//东胜7移植
public static string formatListStr(List DestList, int lineCount)
{
var result = "";
for (var i = 0; i < lineCount; i++)
{
if (DestList.Count > i)
{
result = result + DestList[i];
}
if (i != (lineCount - 1))
result = result + ":";
}
return result;
}
///
/// 获取箱型的EDI代码
///
///
///
///
public static string GetCtnEDICode(string ctnCode, string EDINAME)
{
string EDICODE = "";
var ctnEdi = GetAllMappingCtn().Result.FirstOrDefault(c => c.Code == ctnCode && c.Module == EDINAME);
if (ctnEdi != null)
{
EDICODE = ctnEdi.MapCode.Trim();
}
if (string.IsNullOrEmpty(EDICODE))
{
var infoclient = GetAllCodeCtn().Result.FirstOrDefault(c => c.Code == ctnCode);
if (infoclient != null)
{
EDICODE = infoclient.EdiCode.Trim();
}
}
return EDICODE;
}
///
/// 获取所有箱型(使用缓存)
///
///
private static async Task> GetAllCodeCtn()
{
var cacheService = App.GetService();
var ctnList = await cacheService.GetAllCodeCtn();
if (ctnList == null || ctnList.Count == 0)
{
var rep = App.GetService>();
ctnList = await rep.ToListAsync();
await cacheService.SetAllCodeCtn(ctnList);
}
return ctnList;
}
///
/// 获取所有船司(使用缓存)
///
///
private static async Task> GetAllCodeCarrier()
{
var cacheService = App.GetService();
var list = await cacheService.GetAllCodeCarrier();
if (list == null || list.Count == 0)
{
var rep = App.GetService>();
list = await rep.ToListAsync();
await cacheService.SetAllCodeCarrier(list);
}
return list;
}
///
/// 获取所有箱型映射(使用缓存)
///
///
private static async Task> GetAllMappingCtn()
{
var cacheService = App.GetService();
var ctnList = await cacheService.GetAllMappingCtn();
if (ctnList == null || ctnList.Count == 0)
{
var rep = App.GetService>();
ctnList = await rep.ToListAsync();
await cacheService.SetAllMappingCtn(ctnList);
}
return ctnList;
}
///
/// 获取所有船司映射(使用缓存)
///
///
private static async Task> GetAllMappingCarrier()
{
var cacheService = App.GetService();
var list = await cacheService.GetAllMappingCarrier();
if (list == null || list.Count == 0)
{
var rep = App.GetService>();
list = await rep.ToListAsync();
await cacheService.SetAllMappingCarrier(list);
}
return list;
}
///
/// 获取所有付费方式(使用缓存)
///
///
private static async Task> GetAllCodeFrt()
{
var cacheService = App.GetService();
var list = await cacheService.GetAllCodeFrt();
if (list == null || list.Count == 0)
{
var rep = App.GetService>();
list = await rep.ToListAsync();
await cacheService.SetAllCodeFrt(list);
}
return list;
}
///
/// 获取所有付费方式映射(使用缓存)
///
///
private static async Task> GetAllMappingFrt()
{
var cacheService = App.GetService();
var list = await cacheService.GetAllMappingFrt();
if (list == null || list.Count == 0)
{
var rep = App.GetService>();
list = await rep.ToListAsync();
await cacheService.SetAllMappingFrt(list);
}
return list;
}
/////
///// 获取所有公司参数(使用缓存)
/////
/////
//private static async Task> GetAllTenantParam()
//{
// var cacheService = App.GetService();
// var list = await cacheService.GetAllTenantParam();
// if (list == null || list.Count == 0)
// {
// var rep = App.GetService>();
// list = await sugerClient.Queryable().ToListAsync();
// await cacheService.SetAllTenantParam(list);
// }
// return list;
//}
///
/// 获取所有船名(使用缓存)
///
///
private static async Task> GetAllCodeVessel()
{
var cacheService = App.GetService();
var list = await cacheService.GetAllCodeVessel();
if (list == null || list.Count == 0)
{
var rep = App.GetService>();
list = await rep.ToListAsync();
await cacheService.SetAllCodeVessel(list);
}
return list;
}
///
/// 获取所有船名映射(使用缓存)
///
///
private static async Task> GetAllMappingVessel()
{
var cacheService = App.GetService();
var list = await cacheService.GetAllMappingVessel();
if (list == null || list.Count == 0)
{
var rep = App.GetService>();
list = await rep.ToListAsync();
await cacheService.SetAllMappingVessel(list);
}
return list;
}
///
/// 分割ftp服务器地址和端口
///
///
private static void SplitFtpServerAndPort(string str, out string server, out int port)
{
if (str.IndexOf(':') > -1)
{
var arr = str.Split(':');
server = arr[0];
port = Convert.ToInt32(arr[1]);
}
else
{
server = str;
port = 21;
}
}
}
}