You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1101 lines
47 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using NPOI.OpenXmlFormats.Spreadsheet;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Myshipping.Application.EDI
{
/// <summary>
/// 太平玛利亚
/// </summary>
public class MellEdiHelper
{
public MellEdiHelper()
{
}
#region 格式化每行
public static List<System.String> 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('\\');
var strtemp = "";
var strnewline = "";
var strtempnewline = "";
char[] spstring = { ' ', ';', ',', '.', ':', '/', '(', ')', '?', '+', '-' };
List<System.String> DestList = new List<System.String>();
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;
}
#endregion
public static string GetDateStr(string datestr, string dateformat)
{
var result = "";
if (datestr == null || datestr == "")
{
result = "";
}
else
{
result = Convert.ToDateTime(datestr).ToString(dateformat);
}
return result;
}
#region 字符转义
/// <summary>
/// 各种文本转义字符
/// </summary>
/// <param name="fileType">文件类型例如txt、xml</param>
/// <param name="str">文本字符串</param>
/// <returns></returns>
public static string formatEdiStr(string fileType, string str)
{
if (str == null)
str = string.Empty;
if (fileType == "txt")
{
return str.Replace("?", "??").Replace(":", "?:").Replace("+", "?+").Replace("'", "?'");
}
else if (fileType == "xml")
{
return str.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace("\"", "&quot;").Replace("'", "&apos;");
}
else if (fileType == "sitc")
{
return str.Replace("?", "??").Replace(":", "?:").Replace("'", "?'");
}
else
{
return str;
}
}
#endregion
#region 文本字段判断每行是否符合
/// <summary>
/// edi 文本格式处理判断例如1行35个字符不超过5行
/// </summary>
/// <param name="fileType">文件类型例如txt、xml</param>
/// <param name="str">要处理的数据</param>
/// <param name="length">每行长度</param>
/// <param name="sMBLNO">主提单号</param>
/// <param name="sType">数据类型(例如:发货人内容、货描等)</param>
/// <param name="rowNum">限制录入的行数“0”代表不限制</param>
/// <param name="sSymbol">限录后多出的放到货描中的数据的连接符(例如:“*”发货人内容、“**”收件人等)</param>
/// <returns></returns>
public static System.String formatlengthError(string fileType, string str, int length, string sMBLNO, string sType, int rowNum, string sSymbol, bool isHuoMiao)
{
string error = "";
string Shipping = str;
if (fileType == "txt")
{
//Shipping = formatEdiStr("txt", str);
}
else if (fileType == "xml")
{
Shipping = formatEdiStr("txt", str);
}
string[] argAGENT = Shipping.Split(new string[] { "\r\n" }, StringSplitOptions.None);
if (argAGENT.Length > 0)
{
if (argAGENT.Length == 1)
{
argAGENT = Shipping.Split(new string[] { "\n" }, StringSplitOptions.None);
if (argAGENT.Length > 0)
{
if (argAGENT.Length > rowNum && rowNum != 0)
{
if (isHuoMiao)
{
error = error + "<br />提单号:" + sMBLNO + " " + sType + " 不允许录入超过" + rowNum + "行数据!";
}
else
{
error = error + "<br />提单号:" + sMBLNO + " " + sType + " 不允许录入超过" + rowNum + "行数据,多余信息请手动以放到货物描述中!(例如:“货描内容" + sSymbol + "超出部分”)";
}
}
for (int j = 0; j < argAGENT.Length; j++)
{
//List<System.String> AgentList = formatlengthStr(argAGENT[j].ToString(), 35);
if (argAGENT[j].ToString().Length > length)
{
error = error + "<br />提单号:" + sMBLNO + " " + sType + " 第" + (j + 1) + "行超过" + length + "个字符";
}
}
}
}
else
{
if (argAGENT.Length > rowNum && rowNum != 0)
{
if (isHuoMiao)
{
error = error + "<br />提单号:" + sMBLNO + " " + sType + " 不允许录入超过" + rowNum + "行数据!";
}
else
{
error = error + "<br />提单号:" + sMBLNO + " " + sType + " 不允许录入超过" + rowNum + "行数据,多余信息请手动放到货物描述中!(例如:“货描内容" + sSymbol + "超出部分”)";
}
}
for (int j = 0; j < argAGENT.Length; j++)
{
//List<System.String> AgentList = formatlengthStr(argAGENT[j].ToString(), 35);
if (argAGENT[j].ToString().Length > length)
{
error = error + "<br />提单号:" + sMBLNO + " " + sType + " 第" + (j + 1) + "行超过" + length + "个字符";
}
}
}
}
return error;
}
#endregion
public static bool IsChinese(char c)
{
return (int)c > 0x80;
}
public static bool StringIsChinese(string str)
{
var result = false;
for (int i = 1; i < str.Length; i++)
{
if (IsChinese(str[i]))
{
result = true;
return result;
}
}
for (int i = 0; i < str.Length; i++)
{
string stemp = str.Substring(i, 1);
int ilen = System.Text.Encoding.Default.GetByteCount(stemp);
if (ilen == 2)
{
result = true;
return result;
}
}
return result;
}
public static string IsCreatePILMELL(EDIBaseModel InttrEdi)
{
var error = "";
foreach (var bill in InttrEdi.BSLIST)
{
if (InttrEdi.filetype == "B")
{
if (InttrEdi.UseForWarderCode)
{
if (InttrEdi.ForWarderCode?.Trim() == "")
{ error = error + "<br />货代代码不能为空"; }
if (InttrEdi.ForWarderName.Trim() == "")
{ error = error + "<br />货代称呼不能为空"; }
if (bill.ORDERNO.Trim() == "")
{ error = error + "<br />订舱编号不能为空"; }
}
}
//if (bill.OP == null || bill.OP == "")
//{ error = error + "<br />操作不能为空"; }
if (bill.CARRIERID != "YML")
{
if (string.IsNullOrWhiteSpace(bill.OpEName))
{ error = error + "<br />操作的英文名不能为空"; }
if (string.IsNullOrWhiteSpace(bill.OpTel))
{ error = error + "<br />操作的电话不能为空"; }
}
if (bill.SERVICE == "DOOR-DOOR" || bill.SERVICE == "DOOR-CY" || bill.SERVICE == "CY-DOOR")
{
if (bill.CARRIERID != "YML")
{
if (string.IsNullOrWhiteSpace(bill.EDIATTN))
{ error = error + "<br />EDI信息联系人不能为空"; }
if (string.IsNullOrWhiteSpace(bill.EDIATTNTEL))
{ error = error + "<br />EDI信息联系人电话不能为空"; }
if (string.IsNullOrWhiteSpace(bill.EDIATTNEMAIL))
{ error = error + "<br />EDI信息联系人邮箱不能为空"; }
}
}
if (bill.CARGOID == "D")
{
if (bill.CARRIERID != "YML")
{
if (string.IsNullOrEmpty(bill.EDIATTN))
{
}
else
{
if (bill.EDIATTN == null || bill.EDIATTN == "")
{ error = error + "<br />EDI信息联系人不能为空"; }
if (bill.EDIATTNTEL == null || bill.EDIATTNTEL == "")
{ error = error + "<br />EDI信息联系人电话不能为空"; }
if (bill.EDIATTNEMAIL == null || bill.EDIATTNEMAIL == "")
{ error = error + "<br />EDI信息联系人邮箱不能为空"; }
}
}
}
if (bill.MBLNO == null || bill.MBLNO == "")
{ }
else
{
if (StringIsChinese(bill.MBLNO))
{
error = error + "<br />提单号:" + bill.MBLNO + " 提单号含有中文或双字节字符";
}
}
if ((bill.BLFRT == null || bill.BLFRT == ""))
{ error = error + "<br />提单号:" + bill.MBLNO + " 付费方式不能为空"; }
//if (filetype == "E")
//{
//if (carrier != "MAEU")
//if ((bill.HSCODE == null || bill.HSCODE == ""))
//{ error = error + "<br />提单号:" + bill.MBLNO + " HSCODE不能为空"; }
if (bill.CARRIEREDICODE != "MCCQ" && bill.CARRIEREDICODE != "MAEU" && bill.CARRIEREDICODE != "SUDU")
{
if (bill.VESSEL == null || bill.VESSEL == "")
{ error = error + "<br />提单号:" + bill.MBLNO + " 船名不能为空"; }
if (bill.VOYNO == null || bill.VOYNO == "")
{ error = error + "<br />提单号:" + bill.MBLNO + " 航次不能为空"; }
}
//}
if (InttrEdi.filetype == "E")
{
if (bill.SIREMARK.IndexOf("PLEASE BOOK OCEAN CARRIER") > -1 || bill.SIREMARK.ToString().Trim() == "")
{
if (bill.CARRIEREDICODE == "COSU")
{
error = error + "<br />提单号:" + bill.MBLNO + " 请在SI备注中输入:COSCO 销售的营销代码(此代码船公司会直接提供)";
}
else
{
//if (carrier != "YML")
//error = error + "<br />提单号:" + bill.MBLNO + " EDI备注中不能为空";
}
}
else
{
if (StringIsChinese(bill.SIREMARK))
{
error = error + "<br />提单号:" + bill.MBLNO + " SI备注中含有中文或双字节字符";
}
//if (bill.EDIREMARK.ToString().Trim()!="" && BasicDataRefDAL.isHasChinese(bill.EDIREMARK))
//{
// error = error + "<br />提单号:" + bill.MBLNO + " 请在EDI备注中输入:英文(不允许录入中文)";
//}
}
}
else
{
if (bill.EDIREMARK.IndexOf("PLEASE BOOK OCEAN CARRIER") > -1 || bill.EDIREMARK.ToString().Trim() == "")
{
if (bill.CARRIEREDICODE == "COSU")
{
error = error + "<br />提单号:" + bill.MBLNO + " 请在EDI备注中输入:COSCO 销售的营销代码(此代码船公司会直接提供)";
}
else
{
//if (carrier != "YML")
//error = error + "<br />提单号:" + bill.MBLNO + " EDI备注中不能为空";
}
}
else
{
if (StringIsChinese(bill.EDIREMARK))
{
error = error + "<br />提单号:" + bill.MBLNO + " EDI备注中含有中文或双字节字符";
}
//if (bill.EDIREMARK.ToString().Trim()!="" && BasicDataRefDAL.isHasChinese(bill.EDIREMARK))
//{
// error = error + "<br />提单号:" + bill.MBLNO + " 请在EDI备注中输入:英文(不允许录入中文)";
//}
}
}
if (bill.CARRIER == null || bill.CARRIER == "")
{ error = error + "<br />提单号:" + bill.MBLNO + " 船公司不能为空"; }
else
{
if (string.IsNullOrWhiteSpace(bill.CARRIEREDICODE))
{ error = error + "<br />提单号:" + bill.MBLNO + " 船公司EDI代码不能为空"; }
}
if (string.IsNullOrWhiteSpace(bill.SHIPPER))
{
error = error + "<br />提单号:" + bill.MBLNO + " 发货人不能为空";
}
else
{
if (StringIsChinese(bill.SHIPPER))
{
error = error + "<br />提单号:" + bill.MBLNO + " 发货人含有中文或双字节字符";
}
}
if (bill.CONSIGNEE == "")
{
error = error + "<br />提单号:" + bill.MBLNO + " 收货人不能为空";
}
else
{
if (StringIsChinese(bill.CONSIGNEE))
{
error = error + "<br />提单号:" + bill.MBLNO + " 收货人含有中文或双字节字符";
}
}
/*
if (bill.SERVICE == "CY-DOOR")
{
if (bill.DOORADDR == "")
{
error = error + "<br />提单号:" + bill.MBLNO + " 运输条款为:CY-DOOREDI信息中的DOOR地址不能为空";
}
else
{
if (StringIsChinese(bill.DOORADDR))
{
error = error + "<br />提单号:" + bill.MBLNO + " DOOR地址含有中文或双字节字符";
}
}
}
if (bill.SERVICE == "DOOR-CY")
{
if (bill.ATTNADDR == "")
{
error = error + "<br />提单号:" + bill.MBLNO + " 运输条款为:DOOR-CYEDI信息中的联系人地址不能为空";
}
else
{
if (StringIsChinese(bill.ATTNADDR))
{
error = error + "<br />提单号:" + bill.MBLNO + " 联系人地址含有中文或双字节字符";
}
}
}
*/
if (bill.NOTIFYPARTY == "")
{
error = error + "<br />提单号:" + bill.MBLNO + " 通知人不能为空";
}
else
{
if (StringIsChinese(bill.NOTIFYPARTY))
{
error = error + "<br />提单号:" + bill.MBLNO + " 通知人含有中文或双字节字符";
}
else
{
//if (carrier == "HLCU")
//{
// error += formatlengthError("txt", bill.NOTIFYPARTY, 35, bill.MBLNO, "通知人", 6, "---", false);
//}
//else
//{
// error += formatlengthError("txt", bill.NOTIFYPARTY, 35, bill.MBLNO, "通知人", 6, "***", false);
//}
}
}
if (bill.MARKS == "")
{
error = error + "<br />提单号:" + bill.MBLNO + " 唛头不能为空";
}
else
{
if (StringIsChinese(bill.MARKS))
{
error = error + "<br />提单号:" + bill.MBLNO + " 唛头含有中文或双字节字符";
}
else
{
error += formatlengthError("txt", bill.MARKS, 35, bill.MBLNO, "唛头", 0, "", false);
}
}
if (bill.DESCRIPTION == "")
{
error = error + "<br />提单号:" + bill.MBLNO + " 货物描述不能为空";
}
else
{
if (StringIsChinese(bill.DESCRIPTION))
{
error = error + "<br />提单号:" + bill.MBLNO + " 货物描述含有中文或双字节字符";
}
else
{
//error += formatlengthError("txt", bill.DESCRIPTION, 35, bill.MBLNO, "货物描述", 0, "");
}
}
if (string.IsNullOrWhiteSpace(bill.PORTLOADID) || bill.PORTLOADID.Length != 5)
{ error = error + "<br />提单号:" + bill.MBLNO + " 装货港代码不能为空或录入不正确(必须是5位代码)"; }
if (bill.PORTLOAD == null || bill.PORTLOAD == "")
{ error = error + "<br />提单号:" + bill.MBLNO + " 装货港不能为空"; }
if (bill.ETD == null || bill.ETD == "")
{
error = error + "<br />提单号:" + bill.MBLNO + " 开船日期不能为空";
return error;
}
if (string.IsNullOrWhiteSpace(bill.PORTDISCHARGEID) || bill.PORTDISCHARGEID.Length != 5)
{ error = error + "<br />提单号:" + bill.MBLNO + " 卸货港代码不能为空或录入不正确(必须是5位代码)"; }
if (bill.PORTDISCHARGE == null || bill.PORTDISCHARGE == "")
{ error = error + "<br />提单号:" + bill.MBLNO + " 卸货港不能为空"; }
if (string.IsNullOrWhiteSpace(bill.DESTINATIONID) || bill.DESTINATIONID.Length != 5)
{ error = error + "<br />提单号:" + bill.MBLNO + " 目的地代码不能为空或录入不正确(必须是5位代码)"; }
if (string.IsNullOrWhiteSpace(bill.KINDPKGS_EDI_CODE))
{ error = error + "<br />提单号:" + bill.MBLNO + " 包装EDI代码不能为空"; }
if (Convert.ToDecimal(bill.PKGS) == 0)
{ error = error + "<br />提单号:" + bill.MBLNO + " 货物件数不能为空"; }
if (Convert.ToDecimal(bill.KGS) == 0)
{ error = error + "<br />提单号:" + bill.MBLNO + " 货物重量不能为空"; }
if (Convert.ToDecimal(bill.CBM) == 0)
{ error = error + "<br />提单号:" + bill.MBLNO + " 货物尺码不能为空"; }
if (bill.CARRIEREDICODE != "MCCQ" && bill.CARRIEREDICODE != "MAEU" && bill.CARRIEREDICODE != "SUDU" && bill.CARRIEREDICODE != "CMDU")
{
if (bill.HSCODE == null || bill.HSCODE == "")
{ error = error + "<br />提单号:" + bill.MBLNO + " HS编码不能为空"; }
}
if (bill.CARGOID == null || bill.CARGOID == "")
{ error = error + "<br />提单号:" + bill.MBLNO + " 货物标示不能为空"; }
if (bill.CARGOID == "D")
{
if (bill.DCLASS == null || bill.DCLASS == "")
{ error = error + "<br />提单号:" + bill.MBLNO + " 危险品分类不能为空"; }
if (bill.DUNNO == null || bill.DUNNO == "")
{ error = error + "<br />提单号:" + bill.MBLNO + " 危险品编号不能为空"; }
}
if (bill.CARGOID == "R")
{
if (bill.TEMPSET == null || bill.TEMPSET == "")
{ error = error + "<br />提单号:" + bill.MBLNO + " 设置温度不能为空"; }
if (bill.REEFERF == null || bill.REEFERF == "")
{ error = error + "<br />提单号:" + bill.MBLNO + " 通风度不能为空"; }
}
if (bill.BLFRT.IndexOf("PREPAID") >= 0)
{
if (bill.CARRIERID == "YML")
{
if (bill.PREPARDATID == "")
{ error = error + "<br />提单号:" + bill.MBLNO + " 预付地点或预付地点EDI代码不能为空"; }
}
else
{
if ((bill.PREPARDATID == ""))
{ error = error + "<br />提单号:" + bill.MBLNO + " 预付地点或预付地点EDI代码不能为空"; }
}
}
if (bill.BLFRT.IndexOf("COLLECT") >= 0)
{
if(string.IsNullOrWhiteSpace(bill.PAYABLEAT))
{ error = error + "<br />提单号:" + bill.MBLNO + " 到付地点或到付地点EDI代码不能为空"; }
}
if (InttrEdi.filetype == "E")
{
if(string.IsNullOrWhiteSpace(bill.ISSUEPLACEID))
{ error = error + "<br />提单号:" + bill.MBLNO + " 签单地点或到签单地点EDI代码不能为空"; }
}
if ((bill.SERVICE == null || bill.SERVICE == ""))
{ error = error + "<br />提单号:" + bill.MBLNO + " 运输条款不能为空"; }
if (bill.CTNLIST.Count == 0) { error = error + "<br />提单号:" + bill.MBLNO + " 集装箱信息不能为空"; };
if (InttrEdi.filetype == "E")
{
#region 集装箱判断检查
string sCtnCntrno = "";
int iCTN = 0;
int iCTNDetail = 0;
if (bill.CTNLIST.Count != 0)
{
Decimal dlPKGS = 0;
Decimal dlKGS = 0;
Decimal dlCBM = 0;
foreach (var ctn in bill.CTNLIST)
{
if(string.IsNullOrWhiteSpace(ctn.CTNALLCODE))
{ error = error + "<br />提单号:" + bill.MBLNO + " 集装箱箱型EDI代码不能为空"; }
if (ctn.CNTRNO == null || ctn.CNTRNO == "")
{ error = error + "<br />提单号:" + bill.MBLNO + " 箱号不能为空"; }
if (ctn.SEALNO == null || ctn.SEALNO == "")
{ error = error + "<br />提单号:" + bill.MBLNO + " 封号不能为空"; }
if (bill.BYCOUNTRY == "BRAZIL")
{
if (ctn.TAREWEIGHT == 0)
{ error = error + "<br />提单号:" + bill.MBLNO + " 箱皮重不能为空"; }
}
if (ctn.KINDPKGS != bill.KINDPKGS)
{ error = error + "<br />提单号:" + bill.MBLNO + " 中的包装类型与集装箱的包装类型不同"; }
sCtnCntrno += "、" + ctn.CNTRNO;
iCTN++;
//
dlPKGS += ctn.PKGS;
dlKGS += ctn.KGS;
dlCBM += ctn.CBM;
}
if (dlPKGS != Convert.ToInt32(bill.PKGS))
{ error = error + "<br />提单号:" + bill.MBLNO + " 集装箱件数合计数必须等于委托单总件数"; }
if (dlKGS != Convert.ToDecimal(bill.KGS))
{ error = error + "<br />提单号:" + bill.MBLNO + " 集装箱重量合计数必须等于委托单总重量数"; }
if (dlCBM != Convert.ToDecimal(bill.CBM))
{ error = error + "<br />提单号:" + bill.MBLNO + " 集装箱尺码合计数必须等于委托单总尺码数"; }
}
#endregion
if (bill.BYCOUNTRY == "USA")
{
if (bill.CONSIGNEEPOSTCODE == null || bill.CONSIGNEEPOSTCODE == "")
{ error = error + "<br />提单号:" + bill.MBLNO + " 收货人邮编不能为空"; }
if (bill.NOTIFYPARTYPOSTCODE == null || bill.NOTIFYPARTYPOSTCODE == "")
{ error = error + "<br />提单号:" + bill.MBLNO + " 通知人邮编不能为空"; }
}
else if (bill.BYCOUNTRY == "CANADA")
{
if (bill.CONSIGNEEPOSTCODE == null || bill.CONSIGNEEPOSTCODE == "")
{ error = error + "<br />提单号:" + bill.MBLNO + " 收货人邮编不能为空"; }
}
else if (bill.BYCOUNTRY == "BRAZIL")
{
if (bill.CONSIGNEECOUNTRY == null || bill.CONSIGNEECOUNTRY == "")
{ error = error + "<br />提单号:" + bill.MBLNO + " 收货人国家代码不能为空"; }
if (bill.CONSIGNEETAXNO == null || bill.CONSIGNEETAXNO == "")
{ error = error + "<br />提单号:" + bill.MBLNO + " 收货人税号不能为空"; }
if (bill.NOTIFYPARTYCOUNTRY == null || bill.NOTIFYPARTYCOUNTRY == "")
{ error = error + "<br />提单号:" + bill.MBLNO + " 通知人国家代码不能为空"; }
if (bill.NOTIFYPARTYTAXNO == null || bill.NOTIFYPARTYTAXNO == "")
{ error = error + "<br />提单号:" + bill.MBLNO + " 通知人税号不能为空"; }
if (bill.GOODSNCM == null || bill.GOODSNCM == "")
{ error = error + "<br />提单号:" + bill.MBLNO + " 货物NCM编码不能为空"; }
}
}
else
{
}
if (bill.CARRIERID == "YML")
{
//2023-03-07 SHIPPERCOUNTRY 没有这个字段
//if (string.IsNullOrEmpty(bill.SHIPPERCOUNTRY)) error = error + "<br />提单号:" + bill.MBLNO + " 发货人国家代码不能为空";
if (string.IsNullOrEmpty(bill.CONSIGNEECOUNTRY)) error = error + "<br />提单号:" + bill.MBLNO + " 收货人国家代码不能为空";
}
}
return error;
}
/// <summary>
///
/// </summary>
/// <param name="InttrEdi"></param>
/// <returns></returns>
public static CommonWebApiResult CreateEdiPILMELL(EDIBaseModel InttrEdi)
{
CommonWebApiResult result = new CommonWebApiResult { succ = false };
string dir = InttrEdi.filerpath + "\\" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + "\\";
//如果是部署linux需要修改路径
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
dir = dir.Replace("\\", "/");
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
string filename = dir + InttrEdi.BSLIST.FirstOrDefault().MBLNO + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".txt";
FileStream f = new FileStream(filename, FileMode.Create);
StreamWriter r = new StreamWriter(f, Encoding.Default);
var icount = 0;
var bsno = "";
/*
2023-03-13 跟韩工确认当前帮助类根据不同的用户需要选择不同的报文版本
UNOC:2-适用客户和川;
UNOB:1-适用其他客户,需要到具体客户时来配置。
考虑 InttrEdi.SENDCOMPANYCODE - 发送方部门代码,本帮助类未适用,所以改用这个字段配置
UNOC-UNOC:2UNOB-UNOB:1
*/
string sendCompanyCode = "UNOB:1";
if(!string.IsNullOrWhiteSpace(InttrEdi.SENDCOMPANYCODE)
&& InttrEdi.SENDCOMPANYCODE.Equals("UNOC",StringComparison.OrdinalIgnoreCase))
{
sendCompanyCode = "UNOC:2";
}
r.WriteLine("UNB+"+ sendCompanyCode + "+" + InttrEdi.SENDCODE + "SO:ZZZ+" + InttrEdi.RECEIVECODE + ":ZZZ+" + DateTime.Now.ToString("yyMMdd:HHmm") + "+" + InttrEdi.BSLIST.FirstOrDefault().ORDERNO + "'");
icount = icount + 1;
foreach (var bill in InttrEdi.BSLIST)
{
bsno = bill.ORDERNO;
r.WriteLine("UNH+" + bill.ORDERNO + "+IFTMBF:D:99B:UN'");
icount = icount + 1;
if (InttrEdi.filerole == "9")
r.WriteLine("BGM+335+" + bill.MBLNO + "+9'");
else
r.WriteLine("BGM+335+" + bill.MBLNO + "+5'");
r.WriteLine("DTM+137:" + DateTime.Now.ToString("yyyyMMddHHmm") + ":203'");
icount = icount + 2;
if (bill.SERVICE.ToUpper() == "DOOR-DOOR")
{
r.WriteLine("TSR+16+2'");
}
else if (bill.SERVICE.ToUpper() == "DOOR-CY")
{
r.WriteLine("TSR+15+2'");
}
else if (bill.SERVICE.ToUpper() == "CY-DOOR")
{
r.WriteLine("TSR+17+2'");
}
else if (bill.SERVICE.ToUpper() == "CFS-CFS")
{
r.WriteLine("TSR+14+2'");
}
else
{
r.WriteLine("TSR+11+2'");
}
icount = icount + 1;
var str_pay = "";
if (bill.BLFRT.IndexOf("PREPAID") >= 0)
str_pay = bill.PREPARDAT;
if (bill.BLFRT.IndexOf("COLLECT") >= 0)
str_pay = bill.PAYABLEAT;
r.WriteLine("LOC+88+" + bill.PORTLOADID + ":139:6:" + bill.PORTLOAD + "'");
icount = icount + 1;
if (!string.IsNullOrWhiteSpace(bill.DESTINATION))
{
r.WriteLine("LOC+7+" + bill.DESTINATIONID + ":139:6:" + bill.DESTINATION + "'");
icount = icount + 1;
}
r.WriteLine("DTM+95:" + GetDateStr(bill.ISSUEDATE, "yyyyMMdd") + ":102'");
icount = icount + 1;
r.WriteLine("RFF+BN:" + bill.MBLNO + "'");
r.WriteLine("RFF+BM:" + bill.MBLNO + "'");
icount = icount + 2;
if (!string.IsNullOrEmpty(bill.CONTRACTNO))
{
r.WriteLine("RFF+CT:" + formatEdiStr("txt", bill.CONTRACTNO) + "'");
icount = icount + 1;
}
var voyno = "";
if (bill.VOYNO.IndexOf(".") >= 0)
voyno = bill.VOYNO.Substring(bill.VOYNO.IndexOf(".") + 1, bill.VOYNO.Length - bill.VOYNO.IndexOf(".") - 1);
else
voyno = bill.VOYNO;
r.WriteLine("TDT+20+" + voyno + "+1++MELL:172+++:::" + bill.VESSEL + "'");
r.WriteLine("LOC+9+" + bill.PORTLOADID + ":139:6:" + bill.PORTLOAD + "'");
r.WriteLine("DTM+133:" + GetDateStr(bill.ETD, "yyyyMMdd") + ":102'");//yyyyMMddHHmm
r.WriteLine("LOC+11+" + bill.PORTDISCHARGEID + ":139:6:" + bill.PORTDISCHARGE + "'");
icount = icount + 4;
var Shipping = "";
var DescriptionShipper = "";
Shipping = formatEdiStr("txt", bill.SHIPPER);
List<System.String> ShippingList = formatlengthStr(Shipping, 35);
if (ShippingList.Count != 0 && Shipping.Length > 0)
{
for (var i = 0; i < ShippingList.Count; i++)
{
if (i == 0) Shipping = "NAD+CZ+" + InttrEdi.SENDSUBCOMPANYCODE + "+" + ShippingList[0] + ":";
if (i == 1) Shipping = Shipping + ShippingList[i];
if (i == 2 || i == 3) Shipping = Shipping + ":" + ShippingList[i];
if (i >= 4 && ShippingList.Count > 5)
{
if (i == 4)
{
if (ShippingList[i].Length > 34)
{
Shipping = Shipping + ":" + ShippingList[i].Substring(0, 34);
DescriptionShipper = ShippingList[i].Substring(34);
}
else
Shipping = Shipping + ":" + ShippingList[i];
Shipping = Shipping + "*";
DescriptionShipper = "*" + DescriptionShipper;
}
else if (i > 4)
{
DescriptionShipper = DescriptionShipper + " " + ShippingList[i];
}
}
else
if (i == 4) Shipping = Shipping + ":" + ShippingList[i];
}
}
r.WriteLine(Shipping + "'");
icount = icount + 1;
if (!string.IsNullOrWhiteSpace(bill.BYCOUNTRY))
Shipping = formatEdiStr("txt", bill.AMSCONSIGNEE);
else
Shipping = formatEdiStr("txt", bill.CONSIGNEE);
ShippingList = formatlengthStr(Shipping, 35);
var DescriptionConsignee = "";
if (ShippingList.Count != 0 && Shipping.Length > 0)
{
for (var i = 0; i < ShippingList.Count; i++)
{
if (i == 0) Shipping = "NAD+CN+" + InttrEdi.SENDSUBCOMPANYCODE + "+" + ShippingList[0] + ":";
if (i == 1) Shipping = Shipping + ShippingList[i];
if (i == 2 || i == 3) Shipping = Shipping + ":" + ShippingList[i];
if (i >= 4 && ShippingList.Count > 5)
{
if (i == 4)
{
if (ShippingList[i].Length > 33)
{
Shipping = Shipping + ":" + ShippingList[i].Substring(0, 33);
DescriptionConsignee = ShippingList[i].Substring(33);
}
else
Shipping = Shipping + ":" + ShippingList[i];
Shipping = Shipping + "**";
DescriptionConsignee = "**" + DescriptionConsignee;
}
else if (i > 4)
{
DescriptionConsignee = DescriptionConsignee + " " + ShippingList[i];
}
}
else
if (i == 4) Shipping = Shipping + ":" + ShippingList[i];
}
}
r.WriteLine(Shipping + "'");
icount = icount + 1;
if (!string.IsNullOrWhiteSpace(bill.BYCOUNTRY))
Shipping = formatEdiStr("txt", bill.AMSNOTIFYPARTY);
else
Shipping = formatEdiStr("txt", bill.NOTIFYPARTY);
ShippingList = formatlengthStr(Shipping, 35);
var DescriptionNotifyparty = "";
if (ShippingList.Count != 0 && Shipping.Length > 0)
{
for (var i = 0; i < ShippingList.Count; i++)
{
if (i == 0) Shipping = "NAD+NI+" + InttrEdi.SENDSUBCOMPANYCODE + "+" + ShippingList[0] + ":";
if (i == 1) Shipping = Shipping + ShippingList[i];
if (i == 2 || i == 3) Shipping = Shipping + ":" + ShippingList[i];
if (i >= 4 && ShippingList.Count > 5)
{
if (i == 4)
{
if (ShippingList[i].Length > 32)
{
Shipping = Shipping + ":" + ShippingList[i].Substring(0, 32);
DescriptionNotifyparty = ShippingList[i].Substring(32);
}
else
Shipping = Shipping + ":" + ShippingList[i];
Shipping = Shipping + "***";
DescriptionNotifyparty = "***" + DescriptionNotifyparty;
}
else if (i > 4)
{
DescriptionNotifyparty = DescriptionNotifyparty + " " + ShippingList[i];
}
}
else
if (i == 4) Shipping = Shipping + ":" + ShippingList[i];
}
}
if (bill.BYCOUNTRY == "USA")
{
Shipping = Shipping + "+++" + bill.NOTIFYPARTYPOSTCODE;
r.WriteLine(Shipping + "'");
}
else if (bill.BYCOUNTRY == "BRAZIL")
{
if (!string.IsNullOrWhiteSpace(bill.NOTIFYPARTYCOUNTRY))
Shipping = Shipping + "++++" + bill.NOTIFYPARTYCOUNTRY;
r.WriteLine(Shipping + "'");
if (!string.IsNullOrWhiteSpace(bill.NOTIFYPARTYTAXNO))
{
r.WriteLine("RFF+GN:" + bill.NOTIFYPARTYTAXNO + "'");
icount = icount + 1;
}
}
else r.WriteLine(Shipping + "'");
icount = icount + 1;
r.WriteLine("NAD+BP+" + InttrEdi.ALIASSENDCODE + "+" + InttrEdi.SENDNAME + ":160:86+'");
icount = icount + 1;
r.WriteLine("NAD+AP+" + InttrEdi.SENDSUBCOMPANYCODE + "+" + InttrEdi.SENDNAME + ":160:86+'");
icount = icount + 1;
if (bill.BLFRT.IndexOf("PREPAID") >= 0)
r.WriteLine("CPI+4++P'");
else if (bill.BLFRT.IndexOf("COLLECT") >= 0)
r.WriteLine("CPI+4++C'");
else
r.WriteLine("CPI+4++B'");
icount = icount + 1;
r.WriteLine("GID+1+" + bill.PKGS.ToString() + ":" + bill.KINDPKGS_EDI_CODE.Trim() + "::6:" + bill.KINDPKGS + "'");
icount = icount + 1;
if (!string.IsNullOrWhiteSpace(bill.HSCODE))
{
r.WriteLine("PIA+5+" + bill.HSCODE + ":HS'");
icount = icount + 1;
}
Shipping = formatEdiStr("txt", bill.DESCRIPTION);
Shipping = Shipping.Replace("\n", "\\");
Shipping = Shipping.Replace("\r", " ");
string[] DescriptionList = Shipping.Split('\\');
if (DescriptionList.Length != 0)
{
for (var i = 0; i < DescriptionList.Length; i++)
{
if (i <= 1)
{
r.WriteLine("FTX+AAA+++" + DescriptionList[i] + "'");
icount = icount + 1;
}
}
}
r.WriteLine("MEA+AAE+G+KGM:" + Math.Round(Convert.ToDecimal(bill.KGS), 3) + "'");
r.WriteLine("MEA+AAE+AAW+MTQ:" + Math.Round(Convert.ToDecimal(bill.CBM), 3) + "'");
icount = icount + 2;
if (bill.CARGOID == "D")
{
r.WriteLine("DGS+IMD+" + bill.DCLASS + "+" + bill.DUNNO + "'");
r.WriteLine("CTA+HG+:" + bill.EDIATTN + "'");
r.WriteLine("COM+" + bill.EDIATTNTEL + ":TE'");
icount = icount + 3;
}
foreach (var ctn in bill.CTNLIST)
{
if (bill.ISCONTAINERSOC)
r.WriteLine("EQD+CN++" + ctn.CTNALLCODE + "+1'");
else
r.WriteLine("EQD+CN++" + ctn.CTNALLCODE + "+2'");
r.WriteLine("EQN+" + ctn.CTNNUM.ToString() + "'");
icount = icount + 2;
if (bill.CARGOID == "R")
{
if (bill.REEFERF != "" && bill.REEFERF != "0")
{
r.WriteLine("MEA+AAE+AAS+CBM:" + bill.REEFERF + "'");
icount = icount + 1;
}
if (bill.HUMIDITY != "")
{
r.WriteLine("MEA+AAE+AAO+HMD:" + bill.HUMIDITY + "'");
icount = icount + 1;
}
r.WriteLine("TMP+2+" + bill.TEMPSET.Replace("+", "") + ":CEL'");
icount = icount + 1;
}
}
}
r.WriteLine("UNT+" + icount.ToString() + "+" + bsno + "'");
r.WriteLine("UNZ+" + InttrEdi.BSLIST.Count.ToString() + "+" + bsno + "'");
r.Close();
f.Close();
result.succ = true;
result.extra = filename;
result.extra2 = GetEmailTopic(InttrEdi);
return result;
}
/// <summary>
/// 获取邮件主题
/// </summary>
/// <param name="inttrEdi"></param>
/// <returns></returns>
public static string GetEmailTopic(EDIBaseModel inttrEdi)
{
if(inttrEdi.filetype.Equals("B",StringComparison.OrdinalIgnoreCase))
{
return "[CNTAO-QJM] IFTMBF" + inttrEdi.BSLIST.FirstOrDefault().MBLNO?.Trim();
}
else if (inttrEdi.filetype.Equals("E", StringComparison.OrdinalIgnoreCase))
{
return inttrEdi.BSLIST.FirstOrDefault().MBLNO?.Trim() + " " + "ESI";
}
return string.Empty;
}
}
}