using DSWeb.Common.DB;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml;
namespace DSWeb.Helper
{
///
/// 大简云舱单导入报文解析
///
public static class AnalyzeNew
{
private static Dictionary> dicEdiChkLen = new Dictionary>();
static AnalyzeNew()
{
//dicEdiChkLen.Add("MFNO", new Tuple(50, ""));
//dicEdiChkLen.Add("BSNO", new Tuple(100, ""));
//dicEdiChkLen.Add("MASTERNO", new Tuple(100, ""));
//dicEdiChkLen.Add("EDITYPE", new Tuple(20, ""));
//dicEdiChkLen.Add("EDISTATUS", new Tuple(12, ""));
//dicEdiChkLen.Add("INPUTBY", new Tuple(12, ""));
//dicEdiChkLen.Add("CUSTNO", new Tuple(20, ""));
//dicEdiChkLen.Add("MBLNO", new Tuple(20, ""));
//dicEdiChkLen.Add("HBLNO", new Tuple(20, ""));
dicEdiChkLen.Add("SHIPPERNAME", new Tuple(200, "发货人名称"));
dicEdiChkLen.Add("SHIPPERADDR1", new Tuple(200, "发货人地址"));
//dicEdiChkLen.Add("SHIPPERADDR2", new Tuple(200, ""));
//dicEdiChkLen.Add("SHIPPERADDR3", new Tuple(200, ""));
//dicEdiChkLen.Add("SHIPPERCITY", new Tuple(30, ""));
//dicEdiChkLen.Add("SHIPPERPROVINCE", new Tuple(3, ""));
//dicEdiChkLen.Add("SHIPPERPOSTCODE", new Tuple(10, ""));
//dicEdiChkLen.Add("SHIPPERCOUNTRY", new Tuple(2, ""));
//dicEdiChkLen.Add("SHIPPERATTN", new Tuple(25, ""));
//dicEdiChkLen.Add("SHIPPERTEL", new Tuple(50, ""));
dicEdiChkLen.Add("CONSIGNEENAME", new Tuple(200, "收货人名称"));
dicEdiChkLen.Add("CONSIGNEEADDR1", new Tuple(200, "收货人地址"));
//dicEdiChkLen.Add("CONSIGNEEADDR2", new Tuple(200, ""));
//dicEdiChkLen.Add("CONSIGNEEADDR3", new Tuple(200, ""));
//dicEdiChkLen.Add("CONSIGNEECITY", new Tuple(30, ""));
//dicEdiChkLen.Add("CONSIGNEEPROVINCE", new Tuple(3, ""));
//dicEdiChkLen.Add("CONSIGNEEPOSTCODE", new Tuple(10, ""));
//dicEdiChkLen.Add("CONSIGNEECOUNTRY", new Tuple(2, ""));
//dicEdiChkLen.Add("CONSIGNEEATTN", new Tuple(25, ""));
//dicEdiChkLen.Add("CONSIGNEETEL", new Tuple(50, ""));
dicEdiChkLen.Add("NOTIFYPARTYNAME", new Tuple(200, "通知人名称"));
dicEdiChkLen.Add("NOTIFYPARTYADDR1", new Tuple(200, "通知人地址"));
//dicEdiChkLen.Add("NOTIFYPARTYADDR2", new Tuple(200, ""));
//dicEdiChkLen.Add("NOTIFYPARTYADDR3", new Tuple(200, ""));
//dicEdiChkLen.Add("NOTIFYPARTYCITY", new Tuple(30, ""));
//dicEdiChkLen.Add("NOTIFYPARTYPROVINCE", new Tuple(3, ""));
//dicEdiChkLen.Add("NOTIFYPARTYPOSTCODE", new Tuple(10, ""));
//dicEdiChkLen.Add("NOTIFYPARTYCOUNTRY", new Tuple(2, ""));
//dicEdiChkLen.Add("NOTIFYPARTYATTN", new Tuple(25, ""));
//dicEdiChkLen.Add("NOTIFYPARTYTEL", new Tuple(50, ""));
//dicEdiChkLen.Add("NOTIFYPARTY2NAME", new Tuple(40, ""));
//dicEdiChkLen.Add("NOTIFYPARTY2ADDR1", new Tuple(35, ""));
//dicEdiChkLen.Add("NOTIFYPARTY2ADDR2", new Tuple(35, ""));
//dicEdiChkLen.Add("NOTIFYPARTY2ADDR3", new Tuple(35, ""));
//dicEdiChkLen.Add("NOTIFYPARTY2CITY", new Tuple(30, ""));
//dicEdiChkLen.Add("NOTIFYPARTY2PROVINCE", new Tuple(3, ""));
//dicEdiChkLen.Add("NOTIFYPARTY2POSTCODE", new Tuple(10, ""));
//dicEdiChkLen.Add("NOTIFYPARTY2COUNTRY", new Tuple(2, ""));
//dicEdiChkLen.Add("NOTIFYPARTY2ATTN", new Tuple(25, ""));
//dicEdiChkLen.Add("NOTIFYPARTY2TEL", new Tuple(23, ""));
dicEdiChkLen.Add("SHIPPER", new Tuple(600, "发货人"));
dicEdiChkLen.Add("CONSIGNEE", new Tuple(600, "收货人"));
dicEdiChkLen.Add("NOTIFYPARTY", new Tuple(600, "通知人"));
dicEdiChkLen.Add("VESSEL", new Tuple(60, "船名"));
dicEdiChkLen.Add("VOYNO", new Tuple(12, "航次"));
//dicEdiChkLen.Add("PORTLOADID", new Tuple(60, ""));
//dicEdiChkLen.Add("PORTLOAD", new Tuple(60, ""));
//dicEdiChkLen.Add("PORTDISCHARGEID", new Tuple(60, ""));
//dicEdiChkLen.Add("PORTDISCHARGE", new Tuple(60, ""));
//dicEdiChkLen.Add("PLACEDELIVERYID", new Tuple(60, ""));
//dicEdiChkLen.Add("PLACEDELIVERY", new Tuple(60, ""));
//dicEdiChkLen.Add("DESTINATIONID", new Tuple(60, ""));
//dicEdiChkLen.Add("DESTINATION", new Tuple(60, ""));
//dicEdiChkLen.Add("SERVICE", new Tuple(50, ""));
dicEdiChkLen.Add("MARKS", new Tuple(2000, "唛头"));
dicEdiChkLen.Add("DESCRIPTION", new Tuple(2000, "货物描述"));
//dicEdiChkLen.Add("KINDPKGS", new Tuple(20, ""));
//dicEdiChkLen.Add("CARGOID", new Tuple(1, ""));
//dicEdiChkLen.Add("DCLASS", new Tuple(5, ""));
//dicEdiChkLen.Add("DUNNO", new Tuple(16, ""));
//dicEdiChkLen.Add("REEFERF", new Tuple(20, ""));
//dicEdiChkLen.Add("TEMPID", new Tuple(1, ""));
//dicEdiChkLen.Add("TEMPSET", new Tuple(5, ""));
//dicEdiChkLen.Add("TEMPMAX", new Tuple(5, ""));
//dicEdiChkLen.Add("TEMPMIN", new Tuple(5, ""));
//dicEdiChkLen.Add("FREIGHTAGMNO", new Tuple(20, ""));
//dicEdiChkLen.Add("HUMIDITY", new Tuple(5, ""));
//dicEdiChkLen.Add("SHIPIMO", new Tuple(20, ""));
//dicEdiChkLen.Add("CARRIERID", new Tuple(10, ""));
//dicEdiChkLen.Add("ORDERNO", new Tuple(35, ""));
//dicEdiChkLen.Add("FILEROLE", new Tuple(1, ""));
//dicEdiChkLen.Add("PACKINGTYPE", new Tuple(10, ""));
//dicEdiChkLen.Add("SHIPSERVICENO", new Tuple(20, ""));
//dicEdiChkLen.Add("TRANSTYPE", new Tuple(20, ""));
//dicEdiChkLen.Add("FILETYPE", new Tuple(10, ""));
//dicEdiChkLen.Add("BLFRT", new Tuple(20, ""));
//dicEdiChkLen.Add("ISSUEPLACE", new Tuple(60, ""));
//dicEdiChkLen.Add("ETDTIME", new Tuple(10, ""));
//dicEdiChkLen.Add("AGENTID", new Tuple(10, ""));
//dicEdiChkLen.Add("CNTRSEALNO", new Tuple(500, ""));
//dicEdiChkLen.Add("STATUS", new Tuple(20, ""));
//dicEdiChkLen.Add("YARD", new Tuple(20, ""));
//dicEdiChkLen.Add("CHANGERESON", new Tuple(20, ""));
//dicEdiChkLen.Add("FORWARD", new Tuple(20, ""));
//dicEdiChkLen.Add("CARRIER", new Tuple(20, ""));
//dicEdiChkLen.Add("REMARKS", new Tuple(100, ""));
//dicEdiChkLen.Add("CNTRNO", new Tuple(200, ""));
//dicEdiChkLen.Add("CORPID", new Tuple(50, ""));
//dicEdiChkLen.Add("DPAGE", new Tuple(7, ""));
//dicEdiChkLen.Add("DLABEL", new Tuple(32, ""));
//dicEdiChkLen.Add("LINKMAN", new Tuple(35, ""));
//dicEdiChkLen.Add("FORWARDER", new Tuple(20, ""));
//dicEdiChkLen.Add("DCARRIER", new Tuple(20, ""));
//dicEdiChkLen.Add("ARVESSEL", new Tuple(60, ""));
//dicEdiChkLen.Add("ARVOYNO", new Tuple(12, ""));
//dicEdiChkLen.Add("ARSHIPIMO", new Tuple(20, ""));
//dicEdiChkLen.Add("ARSHIPSERVICENO", new Tuple(20, ""));
//dicEdiChkLen.Add("FIRSTPORTOFCALLID", new Tuple(20, ""));
//dicEdiChkLen.Add("FIRSTPORTOFCALLDATE", new Tuple(20, ""));
//dicEdiChkLen.Add("LASTPORTOFCALLID", new Tuple(20, ""));
//dicEdiChkLen.Add("LASTPORTOFCALLDATE", new Tuple(20, ""));
//dicEdiChkLen.Add("MANIFESTSTATUS", new Tuple(100, ""));
//dicEdiChkLen.Add("PLACERECEIPTID", new Tuple(60, ""));
//dicEdiChkLen.Add("PLACERECEIPT", new Tuple(60, ""));
//dicEdiChkLen.Add("SHIPPERCODE", new Tuple(50, ""));
//dicEdiChkLen.Add("SHIPPEREMAIL", new Tuple(50, ""));
//dicEdiChkLen.Add("CONSIGNEECODE", new Tuple(50, ""));
//dicEdiChkLen.Add("CONSIGNEEEMAIL", new Tuple(50, ""));
//dicEdiChkLen.Add("NOTIFYPARTYCODE", new Tuple(50, ""));
//dicEdiChkLen.Add("NOTIFYPARTYEMAIL", new Tuple(50, ""));
//dicEdiChkLen.Add("LeiXingFa", new Tuple(100, ""));
//dicEdiChkLen.Add("LeiXingShou", new Tuple(100, ""));
//dicEdiChkLen.Add("LeiXingTong", new Tuple(100, ""));
//dicEdiChkLen.Add("DaiMaFa", new Tuple(100, ""));
//dicEdiChkLen.Add("DaiMaShou", new Tuple(100, ""));
//dicEdiChkLen.Add("DaiMaTong", new Tuple(100, ""));
//dicEdiChkLen.Add("CreateUser", new Tuple(100, ""));
//dicEdiChkLen.Add("JiZhuangXiang", new Tuple(100, ""));
//dicEdiChkLen.Add("WeiTuoFaSongFang", new Tuple(100, ""));
//dicEdiChkLen.Add("INPUTBYID", new Tuple(100, ""));
//dicEdiChkLen.Add("ZhiFaText", new Tuple(5000, ""));
//dicEdiChkLen.Add("comname", new Tuple(50, ""));
//dicEdiChkLen.Add("DTEL", new Tuple(50, ""));
//dicEdiChkLen.Add("DATTN", new Tuple(50, ""));
//dicEdiChkLen.Add("DATEL", new Tuple(50, ""));
//dicEdiChkLen.Add("DeleteUser", new Tuple(50, ""));
}
#region txt报文解析
///
/// 解析导入EDI的TXT报文
///
public static bool AnalyzeContent(string content, out OpSeaeEdi edi, out List ctnList, out string msg)
{
var lines = content.Replace("?'", "し").Split('\'').ToList(); //特殊符号处理
for (var idx = 0; idx < lines.Count; idx++)
{
lines[idx] = lines[idx].Replace("\r\n", "");
}
CommonDataContext commonData = new CommonDataContext();
EdiDataContext ediData = new EdiDataContext();
edi = new OpSeaeEdi();
edi.MFNO = Guid.NewGuid().ToString();
//edi.BSNO todo:分单需查询主单的MFNO
edi.MASTERNO = string.Empty;
edi.EDITYPE = string.Empty;
edi.EDISTATUS = "已录入";
//edi.INPUTBY todo:当前用户
edi.INPUTDATE = DateTime.Now;
edi.CUSTNO = string.Empty;
edi.PORTLOAD = "QINGDAO,CHINA";
edi.PORTLOADID = "CNTAO";
edi.PLACEDELIVERY = "QINGDAO,CHINA";
edi.PLACEDELIVERYID = "CNTAO";
edi.LeiXingFa = string.Empty;
edi.LeiXingShou = string.Empty;
edi.LeiXingTong = string.Empty;
edi.DaiMaFa = string.Empty;
edi.DaiMaShou = string.Empty;
edi.DaiMaTong = string.Empty;
edi.CreateUser = string.Empty;
edi.WeiTuoFaSongFang = string.Empty;
edi.ZhiFaText = string.Empty;
edi.comname = string.Empty;
ctnList = new List();
var succ = DealPrefix12(lines, edi, out msg);
OpSeaeEdi parentEdi;
if (!string.IsNullOrEmpty(edi.MBLNO) && !string.IsNullOrEmpty(edi.HBLNO))
{
var mblno = edi.MBLNO;
var hblno = edi.HBLNO;
var existEdi = ediData.Edis.AsNoTracking().FirstOrDefault(x => x.MBLNO == mblno && x.HBLNO == hblno);
if (existEdi != null)
{
msg = "提单号重复 ";
return false;
}
parentEdi = ediData.Edis.AsNoTracking().FirstOrDefault(x => x.MBLNO == mblno && (x.HBLNO == null || x.HBLNO == ""));
if (parentEdi != null)
{
edi.BSNO = parentEdi.MFNO;
edi.ETD = parentEdi.ETD;
edi.FORWARDER = parentEdi.FORWARDER;
edi.CARRIER = parentEdi.CARRIER;
edi.CARRIERID = parentEdi.CARRIERID;
edi.KINDPKGS = parentEdi.KINDPKGS;
edi.BLFRT = parentEdi.BLFRT;
edi.SERVICE = parentEdi.SERVICE;
edi.CARGOID = parentEdi.CARGOID;
edi.TEMPID = parentEdi.TEMPID;
edi.TEMPSET = parentEdi.TEMPSET;
edi.DCLASS = parentEdi.DCLASS;
edi.DUNNO = parentEdi.DUNNO;
}
else
{
msg = "未找到主单信息";
return false;
}
}
else
{
msg = "提单号解析失败";
return false;
}
succ = DealPrefix10(lines, edi, out msg);
if (!succ)
{
return false;
}
//succ = DealPrefix11(lines, edi, out msg);
//if (!succ)
//{
// return false;
//}
succ = DealPrefix13(lines, edi, parentEdi, out msg);
if (!succ)
{
return false;
}
succ = DealPrefix16(lines, edi, parentEdi, out msg);
if (!succ)
{
return false;
}
succ = DealPrefix17(lines, edi, parentEdi, out msg);
if (!succ)
{
return false;
}
succ = DealPrefix18(lines, edi, parentEdi, out msg);
if (!succ)
{
return false;
}
succ = DealPrefix41(lines, edi, out msg);
if (!succ)
{
return false;
}
//succ = DealPrefix43(lines, edi, out msg);
//if (!succ)
//{
// return false;
//}
succ = DealPrefix44(lines, edi, out msg);
if (!succ)
{
return false;
}
succ = DealPrefix47(lines, edi, out msg);
if (!succ)
{
return false;
}
succ = DealPrefix51(lines, edi, ctnList, out msg);
//将整票件重尺放到第一个箱子里
if (ctnList.Count > 0)
{
ctnList[0].KGS = edi.KGS;
ctnList[0].PKGS = edi.PKGS;
ctnList[0].CBM = edi.CBM;
}
return succ;
}
///
/// 提单的第一个记录
///
private static bool DealPrefix12(List lines, OpSeaeEdi model, out string msg)
{
var items = GetFirstItemByFixNo(lines, "12");
if (items == null)
{
msg = "缺少12标记,无法解析提单号";
return false;
}
model.MBLNO = GetPosString(items, 1); //提单号
model.HBLNO = GetPosString(items, 16);
model.CBM = GetPosDecimal(items, 20);
msg = null;
return true;
}
///
/// 船舶有关的基本信息
///
private static bool DealPrefix10(List lines, OpSeaeEdi model, out string msg)
{
var items = GetFirstItemByFixNo(lines, "10");
if (items == null)
{
msg = "缺少10标记,无法解析船名航次";
return false;
}
model.VESSEL = GetPosString(items, 2);
model.VOYNO = GetPosString(items, 4);
model.ETA = DateTime.ParseExact(GetPosString(items, 8).Substring(0, 8), "yyyyMMdd", CultureInfo.CurrentCulture); //eta?
msg = null;
return true;
}
///
/// 船舶有关的补充信息
///
private static bool DealPrefix11(List lines, OpSeaeEdi model, out string msg)
{
var items = GetFirstItemByFixNo(lines, "11");
model.CARRIERID = GetPosString(items, 1); //船公司代号
model.CARRIER = GetPosString(items, 2); //船公司
msg = null;
return true;
}
///
/// 提单的地点信息
///
private static bool DealPrefix13(List lines, OpSeaeEdi model, OpSeaeEdi parent, out string msg)
{
var items = GetFirstItemByFixNo(lines, "13");
CommonDataContext commonData = new CommonDataContext();
model.PORTDISCHARGEID = GetPosString(items, 1); //卸货地代码
model.PORTDISCHARGE = GetPosString(items, 2); //卸货地
var objPort = commonData.Disports.AsNoTracking().FirstOrDefault(d => d.EDICODE == model.PORTDISCHARGEID);
if (objPort != null)
{
model.PORTDISCHARGE = objPort.PORT;
}
else //2021-8-10 报文导入时,分单的港口默认取分单报文里的港口,如果该港口代码大简云平台不存在,则取主单的港口。
{
model.PORTDISCHARGEID = parent.PORTDISCHARGEID;
model.PORTDISCHARGE = parent.PORTDISCHARGE;
}
model.DESTINATIONID = GetPosString(items, 3); //交货地代码
model.DESTINATION = GetPosString(items, 4); //交货地
if (string.IsNullOrEmpty(model.DESTINATIONID))
{
model.DESTINATIONID = model.PORTDISCHARGEID;
model.DESTINATION = model.PORTDISCHARGE;
}
else
{
objPort = commonData.Disports.AsNoTracking().FirstOrDefault(d => d.EDICODE == model.DESTINATIONID);
if (objPort != null)
{
model.DESTINATION = objPort.PORT;
}
else //2021-8-10 报文导入时,分单的港口默认取分单报文里的港口,如果该港口代码大简云平台不存在,则取主单的港口。
{
model.DESTINATIONID = parent.DESTINATIONID;
model.DESTINATION = parent.DESTINATION;
}
}
msg = null;
return true;
}
///
/// 发货人信息
///
private static bool DealPrefix16(List lines, OpSeaeEdi model, OpSeaeEdi parent, out string msg)
{
var items = GetFirstItemByFixNo(lines, "16");
/* 2021-8-11
1.导入报文时,如果报文中的发货人名称或收货人名称或通知人名称是PERSONAL EFFECTS,则该分票信息的收发通取主单的。
2.如果报文中的收发通电话或国家代码为空,则取主单的。
3.取消导入报文时,根据前两个单词调用舱单收发通的设置。
*/
var shipper = GetPosString(items, 2);
if (shipper == "PERSONAL EFFECTS")
{
model.SHIPPER = parent.SHIPPER;
model.SHIPPERNAME = parent.SHIPPERNAME;
model.SHIPPERADDR1 = parent.SHIPPERADDR1;
model.SHIPPERCOUNTRY = parent.SHIPPERCOUNTRY;
model.SHIPPERTEL = parent.SHIPPERTEL;
}
else
{
model.SHIPPER = shipper; //发货人
model.SHIPPERNAME = model.SHIPPER;
model.SHIPPERADDR1 = GetPosString(items, 3); //发货人地址
model.SHIPPERCOUNTRY = GetPosString(items, 11); //发货人国家代码
model.SHIPPERTEL = Regex.Replace(GetPosString(items, 4), @"[^\d]*", ""); //发货人电话
if (string.IsNullOrEmpty(model.SHIPPERCOUNTRY))
{
model.SHIPPERCOUNTRY = parent.SHIPPERCOUNTRY;
}
if (string.IsNullOrEmpty(model.SHIPPERTEL))
{
model.SHIPPERTEL = parent.SHIPPERTEL;
}
}
msg = null;
return true;
}
///
/// 收货人信息
///
private static bool DealPrefix17(List lines, OpSeaeEdi model, OpSeaeEdi parent, out string msg)
{
var items = GetFirstItemByFixNo(lines, "17");
/* 2021-8-11
1.导入报文时,如果报文中的发货人名称或收货人名称或通知人名称是PERSONAL EFFECTS,则该分票信息的收发通取主单的。
2.如果报文中的收发通电话或国家代码为空,则取主单的。
3.取消导入报文时,根据前两个单词调用舱单收发通的设置。
*/
var CONSIGNEE = GetPosString(items, 2);
if (CONSIGNEE == "PERSONAL EFFECTS")
{
model.CONSIGNEE = parent.CONSIGNEE;
model.CONSIGNEENAME = parent.CONSIGNEENAME;
model.CONSIGNEEADDR1 = parent.CONSIGNEEADDR1;
model.CONSIGNEECOUNTRY = parent.CONSIGNEECOUNTRY;
model.CONSIGNEETEL = parent.CONSIGNEETEL;
}
else
{
model.CONSIGNEE = CONSIGNEE; //收货人
model.CONSIGNEENAME = model.CONSIGNEE;
model.CONSIGNEEADDR1 = GetPosString(items, 3); //收货人地址
model.CONSIGNEECOUNTRY = GetPosString(items, 11); //收货人国家代码
model.CONSIGNEETEL = Regex.Replace(GetPosString(items, 4), @"[^\d]*", ""); //收货人电话
if (string.IsNullOrEmpty(model.CONSIGNEECOUNTRY))
{
model.CONSIGNEECOUNTRY = parent.CONSIGNEECOUNTRY;
}
if (string.IsNullOrEmpty(model.CONSIGNEETEL))
{
model.CONSIGNEETEL = parent.CONSIGNEETEL;
}
}
msg = null;
return true;
}
///
/// 通知人信息
///
private static bool DealPrefix18(List lines, OpSeaeEdi model, OpSeaeEdi parent, out string msg)
{
var items = GetFirstItemByFixNo(lines, "18");
/* 2021-8-11
1.导入报文时,如果报文中的发货人名称或收货人名称或通知人名称是PERSONAL EFFECTS,则该分票信息的收发通取主单的。
2.如果报文中的收发通电话或国家代码为空,则取主单的。
3.取消导入报文时,根据前两个单词调用舱单收发通的设置。
*/
var NOTIFYPARTY = GetPosString(items, 2);
if (NOTIFYPARTY == "PERSONAL EFFECTS")
{
model.NOTIFYPARTY = parent.NOTIFYPARTY;
model.NOTIFYPARTYNAME = parent.NOTIFYPARTYNAME;
model.NOTIFYPARTYADDR1 = parent.NOTIFYPARTYADDR1;
model.NOTIFYPARTYCOUNTRY = parent.NOTIFYPARTYCOUNTRY;
model.NOTIFYPARTYTEL = parent.NOTIFYPARTYTEL;
}
else
{
model.NOTIFYPARTY = NOTIFYPARTY; //通知人
model.NOTIFYPARTYNAME = model.NOTIFYPARTY;
model.NOTIFYPARTYADDR1 = GetPosString(items, 3); //通知人地址
model.NOTIFYPARTYCOUNTRY = GetPosString(items, 11); //通知人国家代码
model.NOTIFYPARTYTEL = Regex.Replace(GetPosString(items, 4), @"[^\d]*", ""); //通知人电话
if (string.IsNullOrEmpty(model.NOTIFYPARTYCOUNTRY))
{
model.NOTIFYPARTYCOUNTRY = parent.NOTIFYPARTYCOUNTRY;
}
if (string.IsNullOrEmpty(model.NOTIFYPARTYTEL))
{
model.NOTIFYPARTYTEL = parent.NOTIFYPARTYTEL;
}
}
msg = null;
return true;
}
///
/// 货物信息
///
private static bool DealPrefix41(List lines, OpSeaeEdi model, out string msg)
{
var items = GetFirstItemByFixNo(lines, "41");
model.PKGS = GetPosInt(items, 3); //件数
model.KGS = GetPosDecimal(items, 6); //重量
//model.KINDPKGS = GetPosString(items, 5); //包装 2021-7-12,修改为取主单包装
//model.CBM = GetPosDecimal(items, 8); //尺码
msg = null;
return true;
}
///
/// 危险品和冷藏信息
///
private static bool DealPrefix43(List lines, OpSeaeEdi model, out string msg)
{
var items = GetFirstItemByFixNo(lines, "43");
model.CARGOID = "S";
if (items != null && items.Count > 1 && !string.IsNullOrEmpty(GetPosString(items, 10)))
{
model.CARGOID = "R";
model.TEMPID = GetPosString(items, 9); //?需要验证
model.TEMPSET = GetPosString(items, 10); //?需要验证
}
else if (items != null && items.Count > 4 && !string.IsNullOrEmpty(GetPosString(items, 2)))
{
model.CARGOID = "D";
model.DCLASS = GetPosString(items, 1); //?需要验证
model.DUNNO = GetPosString(items, 3); //?需要验证
}
msg = null;
return true;
}
///
/// 唛头信息
///
///
private static bool DealPrefix44(List lines, OpSeaeEdi model, out string msg)
{
var items = GetFirstItemByFixNo(lines, "44");
if (items.Count >= 2)
{
StringBuilder bld = new StringBuilder();
for (int i = 1; i < items.Count; i++)
bld.AppendLine(GetPosString(items, i));
model.MARKS = bld.ToString();
}
else
{
model.MARKS = "";
}
if (model.MARKS.EndsWith("\r\n"))
{
model.MARKS = model.MARKS.Substring(0, model.MARKS.Length - 2);
}
msg = null;
return true;
}
///
/// 货物描述
///
private static bool DealPrefix47(List lines, OpSeaeEdi model, out string msg)
{
var items = GetFirstItemByFixNo(lines, "47");
if (items.Count >= 2)
{
StringBuilder bld = new StringBuilder();
for (int i = 1; i < items.Count; i++)
{
string ms = GetPosString(items, i);
if (!string.IsNullOrEmpty(ms))
{
bld.AppendLine(ms);
}
}
model.DESCRIPTION = bld.ToString().TrimEnd('\n').TrimEnd('\r');
}
else
{
model.DESCRIPTION = "";
}
msg = null;
return true;
}
///
/// 集装箱信息
///
private static bool DealPrefix51(List lines, OpSeaeEdi model, List ctnList, out string msg)
{
var lstItem = GetItemsByFixNo2(lines, "51");
CommonDataContext commonData = new CommonDataContext();
int idx = 1;
foreach (var ctn_line in lstItem)
{
var ctn = new OpSeaeEdiCtn();
ctn.ctn_id = Guid.NewGuid().ToString();
ctn.MFNO = model.MFNO;
ctn.CNTRNO = GetPosString(ctn_line, 1);
ctn.SEALNO = GetPosString(ctn_line, 2);
ctn.CTNCODE = idx++.ToString().PadLeft(2, '0');
var ediCode = GetPosString(ctn_line, 3);
var objCtn = commonData.Ctns.AsNoTracking().FirstOrDefault(c => c.EDICODE == ediCode);
ctn.CTNALL = objCtn?.CTN;
ctn.KINDPKGS = model.KINDPKGS;
ctn.ZhongKongBiaoShi = GetPosInt(ctn_line, 4);
ctn.ShiFengRen = string.Empty;
ctn.LeiXingFa = string.Empty;
ctn.LeiXingShou = string.Empty;
ctn.LeiXingTong = string.Empty;
ctnList.Add(ctn);
}
msg = null;
return true;
}
///
/// 根据行的header序号得到行内容
///
///
///
private static List GetFirstItemByFixNo(List lines, string fixno)
{
fixno += ":";
var line = lines.FirstOrDefault(l => l.StartsWith(fixno));
if (string.IsNullOrEmpty(line))
{
return null;
}
//处理?:的转义功能
List items = line.TrimEnd('\'').Replace("し", "'").Replace("?:", "△").Split(':').ToList();
for (int i = 0; i < items.Count; i++)
{
items[i] = items[i].Replace("△", ":").TrimEnd();
}
return items;
}
///
/// 根据行的header序号得到行内容(箱信息)
///
///
///
private static List> GetItemsByFixNo2(List lines, string fixno)
{
List> lstList = new List>();
fixno += ":";
lines
.Where(r => r.Length > 3 && r.Substring(0, 3) == fixno)
.ToList().ForEach(r =>
{
List items = r.TrimEnd('\'').Replace("し", "'").Replace("?:", "△").Split(':').ToList();
for (int i = 0; i < items.Count; i++)
{
items[i] = items[i].Replace("△", ":").TrimEnd();
}
lstList.Add(items);
});
return lstList;
}
private static string GetPosString(List list, int idx)
{
if (list == null)
{
return "";
}
return list.Count > idx ? list[idx] : "";
}
private static int GetPosInt(List list, int idx)
{
if (list == null)
{
return 0;
}
if (list.Count <= idx)
{
return 0;
}
var succ = int.TryParse(list[idx], out int rtn);
if (succ)
{
return rtn;
}
return 0;
}
private static decimal GetPosDecimal(List list, int idx)
{
if (list == null)
{
return 0;
}
if (list.Count <= idx)
{
return 0;
}
var succ = decimal.TryParse(list[idx], out decimal rtn);
if (succ)
{
return rtn;
}
return 0;
}
#endregion
#region xls解析
public static bool AnalyzeXls(string filePath, out List ediList, out List ctnList, out string msg)
{
using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
HSSFWorkbook hssfworkbook = new HSSFWorkbook(file);
HSSFSheet sheet = hssfworkbook.GetSheetAt(0) as HSSFSheet;
var mblno = sheet.GetRow(5).GetCell(0).StringCellValue;
ediList = new List();
ctnList = new List();
if (mblno.StartsWith("MB/L NO. :"))
{
mblno = mblno.Replace("MB/L NO. :", "").Trim();
EdiDataContext ediData = new EdiDataContext();
var parentEdi = ediData.Edis.AsNoTracking().FirstOrDefault(x => x.MBLNO == mblno && (x.HBLNO == null || x.HBLNO == ""));
if (parentEdi == null)
{
msg = "未找到主单信息";
return false;
}
for (int r = 8; r < sheet.LastRowNum; r++)
{
var firstValue = sheet.GetRow(r).GetCell(0).StringCellValue;
if (firstValue == "TOTAL :")
{
break;
}
OpSeaeEdi edi = new OpSeaeEdi();
edi.MFNO = Guid.NewGuid().ToString();
edi.MASTERNO = string.Empty;
edi.EDITYPE = string.Empty;
edi.EDISTATUS = "已录入";
edi.INPUTDATE = DateTime.Now;
edi.CUSTNO = string.Empty;
edi.PORTLOAD = "QINGDAO,CHINA";
edi.PORTLOADID = "CNTAO";
edi.PLACEDELIVERY = "QINGDAO,CHINA";
edi.PLACEDELIVERYID = "CNTAO";
edi.MBLNO = mblno;
edi.HBLNO = firstValue;
//部分不能为null的字段
edi.LeiXingFa = string.Empty;
edi.LeiXingShou = string.Empty;
edi.LeiXingTong = string.Empty;
edi.DaiMaFa = string.Empty;
edi.DaiMaShou = string.Empty;
edi.DaiMaTong = string.Empty;
edi.CreateUser = string.Empty;
edi.WeiTuoFaSongFang = string.Empty;
edi.ZhiFaText = string.Empty;
edi.comname = string.Empty;
//部分信息跟主单一致
edi.BSNO = parentEdi.MFNO;
edi.ETD = parentEdi.ETD;
edi.FORWARDER = parentEdi.FORWARDER;
edi.CARRIER = parentEdi.CARRIER;
edi.CARRIERID = parentEdi.CARRIERID;
edi.KINDPKGS = parentEdi.KINDPKGS;
edi.BLFRT = parentEdi.BLFRT;
edi.SERVICE = parentEdi.SERVICE;
edi.CARGOID = parentEdi.CARGOID;
edi.TEMPID = parentEdi.TEMPID;
edi.TEMPSET = parentEdi.TEMPSET;
edi.DCLASS = parentEdi.DCLASS;
edi.DUNNO = parentEdi.DUNNO;
edi.VESSEL = parentEdi.VESSEL;
edi.VOYNO = parentEdi.VOYNO;
edi.PORTDISCHARGE = parentEdi.PORTDISCHARGE;
edi.PORTDISCHARGEID = parentEdi.PORTDISCHARGEID;
edi.DESTINATION = parentEdi.DESTINATION;
edi.DESTINATIONID = parentEdi.DESTINATIONID;
ediList.Add(edi);
var strPKGS = sheet.GetRow(r).GetCell(5).StringCellValue;
var arrPKGS = strPKGS.Split('\n');
if (arrPKGS.Length == 2)
{
if (int.TryParse(arrPKGS[0], out int pkgs))
{
edi.PKGS = pkgs;
}
else
{
msg = "格式有误";
return false;
}
edi.KINDPKGS = arrPKGS[1];
}
else
{
msg = "格式有误";
return false;
}
//货描
var strDESP = sheet.GetRow(r).GetCell(9).StringCellValue;
edi.DESCRIPTION = strDESP;
//kgs
var numKGS = sheet.GetRow(r).GetCell(12).NumericCellValue;
edi.KGS = Convert.ToDecimal(numKGS);
//cbm
var numCBM = sheet.GetRow(r).GetCell(13).NumericCellValue;
edi.CBM = Convert.ToDecimal(numCBM);
//箱信息
var strCTN = sheet.GetRow(r).GetCell(10).StringCellValue;
var arrCTN = strCTN.Split('/');
if (arrCTN.Length == 3)
{
var ctn = new OpSeaeEdiCtn();
ctn.ctn_id = Guid.NewGuid().ToString();
ctn.MFNO = edi.MFNO;
ctn.CNTRNO = arrCTN[0];
ctn.SEALNO = arrCTN[1];
ctn.CTNCODE = "01";
ctn.CTNALL = arrCTN[2];
ctn.KINDPKGS = edi.KINDPKGS;
ctn.ZhongKongBiaoShi = 5;
ctn.ShiFengRen = string.Empty;
ctn.LeiXingFa = string.Empty;
ctn.LeiXingShou = string.Empty;
ctn.LeiXingTong = string.Empty;
ctnList.Add(ctn);
//将整票件重尺放到第一个箱子里
ctn.KGS = edi.KGS;
ctn.PKGS = edi.PKGS;
ctn.CBM = edi.CBM;
}
}
msg = "解析成功";
return true;
}
else
{
msg = "格式有误";
return false;
}
}
}
#endregion
#region 荣圣达报文
public static bool AnalyzeZhongYuanXLS(string filePath, out List ediList, out List ctnList, out string msg)
{
EdiDataContext ediData = new EdiDataContext();
using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
IWorkbook hssfworkbook = null;
ISheet sheet = null;
if (Path.GetExtension(filePath).Equals(".xls"))
{
hssfworkbook = new HSSFWorkbook(file);
sheet = hssfworkbook.GetSheetAt(0) as HSSFSheet;
}
else
{
hssfworkbook = new XSSFWorkbook(file);
sheet = hssfworkbook.GetSheetAt(0) as XSSFSheet;
}
ediList = new List();
ctnList = new List();
CommonDataContext commonData = new CommonDataContext();
var codeCtnList = commonData.Ctns.AsNoTracking().ToList();
List parseList = new List();
var vessel = string.Empty;
var voyno = string.Empty;
var portdischarge = string.Empty;
var portdischargeId = string.Empty;
var destination = string.Empty;
var destinationId = string.Empty;
var forwarder = string.Empty;
var colCount = sheet.GetRow(2).Cells.Count;
if (colCount != 42)
{
msg = "excel文件格式有误";
return false;
}
for (int r = 3; r <= sheet.LastRowNum; r++)
{
var sheetRow = sheet.GetRow(r);
if (sheetRow == null)
{
break;
}
var bookNO = sheetRow.GetCell(0).GetStringValue(string.Empty);
if (string.IsNullOrEmpty(bookNO))
{
break;
}
//船名、航次、卸货港、目的港、船代,只在第一行有
if (r == 3)
{
vessel = sheetRow.GetCell(37).GetStringValue(string.Empty);
var cellVoyno = sheetRow.GetCell(38);
if (cellVoyno.CellType == NPOI.SS.UserModel.CellType.NUMERIC)
{
voyno = cellVoyno.NumericCellValue.ToString();
}
else
{
voyno = cellVoyno.GetStringValue(string.Empty);
}
portdischarge = sheetRow.GetCell(39).GetStringValue(string.Empty);
destination = sheetRow.GetCell(40).GetStringValue(string.Empty);
forwarder = sheetRow.GetCell(41).GetStringValue(string.Empty);
var dis = commonData.Disports.AsNoTracking().FirstOrDefault(x => x.PORT.Contains(destination));
if (dis != null)
{
portdischarge = dis.PORT;
portdischargeId = dis.EDICODE;
}
dis = commonData.Disports.AsNoTracking().FirstOrDefault(x => x.PORT.Contains(destination));
if (dis != null)
{
destination = dis.PORT;
destinationId = dis.EDICODE;
}
}
var cargoid = sheetRow.GetCell(35).GetStringValue(string.Empty).Trim();
if (cargoid == "GC-General")
{
cargoid = "S";
}
else if (cargoid == "RF-Reefer")
{
cargoid = "R";
}
else if (cargoid == "DG-Dangerous")
{
cargoid = "D";
}
var cellPKGS = sheetRow.GetCell(31);
var cellKGS = sheetRow.GetCell(32);
var cellCBM = sheetRow.GetCell(33);
int pkgs = cellPKGS.GetIntValue();
decimal kgs = cellKGS.GetDecimalValue();
decimal cbm = cellCBM.GetDecimalValue();
//if (cellPKGS.CellType == NPOI.SS.UserModel.CellType.NUMERIC)
//{
// pkgs = Convert.ToInt32(cellPKGS.NumericCellValue);
//}
//else
//{
// var strVal = cellPKGS.GetStringValue(string.Empty);
// int.TryParse(strVal, out pkgs);
//}
//if (cellKGS.CellType == NPOI.SS.UserModel.CellType.NUMERIC)
//{
// kgs = Convert.ToDecimal(cellKGS.NumericCellValue);
//}
//else
//{
// var strVal = cellKGS.GetStringValue(string.Empty);
// decimal.TryParse(strVal, out kgs);
//}
//if (cellCBM.CellType == NPOI.SS.UserModel.CellType.NUMERIC)
//{
// cbm = Convert.ToDecimal(cellCBM.NumericCellValue);
//}
//else
//{
// var strVal = cellCBM.GetStringValue(string.Empty);
// decimal.TryParse(strVal, out cbm);
//}
var cellMBLNO = sheetRow.GetCell(26);
var mblno = cellMBLNO.GetStringValue(string.Empty);
//if (cellMBLNO.CellType == NPOI.SS.UserModel.CellType.NUMERIC)
//{
// mblno = Convert.ToString(cellMBLNO.NumericCellValue);
//}
//else
//{
// mblno = cellMBLNO.GetStringValue(string.Empty)?.Trim();
//}
var cellHBLNO = sheetRow.GetCell(27);
var hblno = cellHBLNO.GetStringValue(string.Empty);
//if (cellHBLNO != null)
//{
// if (cellHBLNO.CellType == NPOI.SS.UserModel.CellType.NUMERIC)
// {
// hblno = Convert.ToString(cellHBLNO.NumericCellValue);
// }
// else
// {
// hblno = cellHBLNO.GetStringValue(string.Empty)?.Trim();
// }
//}
parseList.Add(new
{
SHIPPERNAME = sheetRow.GetCell(1).GetStringValue(string.Empty),
SHIPPERADDR1 = sheetRow.GetCell(2).GetStringValue(string.Empty),
SHIPPERCOUNTRY = sheetRow.GetCell(3).GetStringValue(string.Empty),
SHIPPERTEL = sheetRow.GetCell(7).GetStringValue(string.Empty),
CONSIGNEENAME = sheetRow.GetCell(10).GetStringValue(string.Empty),
CONSIGNEEADDR1 = sheetRow.GetCell(11).GetStringValue(string.Empty),
CONSIGNEECOUNTRY = sheetRow.GetCell(12).GetStringValue(string.Empty),
CONSIGNEETEL = sheetRow.GetCell(16).GetStringValue(string.Empty),
NOTIFYPARTYNAME = sheetRow.GetCell(18).GetStringValue(string.Empty),
NOTIFYPARTYADDR1 = sheetRow.GetCell(19).GetStringValue(string.Empty),
NOTIFYPARTYCOUNTRY = sheetRow.GetCell(20).GetStringValue(string.Empty),
NOTIFYPARTYTEL = sheetRow.GetCell(24).GetStringValue(string.Empty),
MBLNO = mblno,
HBLNO = hblno,
CNTRNO = sheetRow.GetCell(28).GetStringValue(string.Empty)?.Trim(),
SEALNO = sheetRow.GetCell(29).GetStringValue(string.Empty)?.Trim(),
CTNALL = sheetRow.GetCell(30).GetStringValue(string.Empty),
PKGS = pkgs,
KGS = kgs,
CBM = cbm,
DESCRIPTION = sheetRow.GetCell(34).GetStringValue(string.Empty),
CARGOID = cargoid,
KINDPKGS = sheetRow.GetCell(36).GetStringValue(string.Empty),
});
}
var blList = parseList.Select(x => new { x.MBLNO, x.HBLNO }).Distinct().ToList();
//处理主单
foreach (var bl in blList)
{
var dataList = parseList.Where(x => x.MBLNO == bl.MBLNO && x.HBLNO == bl.HBLNO).ToList();
OpSeaeEdi edi = new OpSeaeEdi();
edi.MFNO = Guid.NewGuid().ToString();
if (string.IsNullOrEmpty(bl.HBLNO))
{
edi.BSNO = string.Empty;
}
else
{
var zhudan = ediList.FirstOrDefault(x => x.MBLNO == bl.MBLNO && x.HBLNO == "");
if (zhudan == null)
{
string blno = bl.MBLNO;
zhudan = ediData.Edis.AsNoTracking().FirstOrDefault(x => x.MBLNO == blno && x.HBLNO == "");
}
if (zhudan == null)
{
msg = $"找不到主单信息 {bl.MBLNO},分单号:{bl.HBLNO}";
return false;
}
edi.BSNO = zhudan.MFNO;
}
edi.MASTERNO = string.Empty;
edi.EDITYPE = string.Empty;
edi.EDISTATUS = "已录入";
edi.INPUTDATE = DateTime.Now;
edi.CUSTNO = string.Empty;
edi.PORTLOAD = "QINGDAO,CHINA";
edi.PORTLOADID = "CNTAO";
edi.PLACEDELIVERY = "QINGDAO,CHINA";
edi.PLACEDELIVERYID = "CNTAO";
edi.MBLNO = bl.MBLNO;
edi.HBLNO = bl.HBLNO;
edi.MARKS = "N/M";
//部分不能为null的字段
edi.LeiXingFa = string.Empty;
edi.LeiXingShou = string.Empty;
edi.LeiXingTong = string.Empty;
edi.DaiMaFa = string.Empty;
edi.DaiMaShou = string.Empty;
edi.DaiMaTong = string.Empty;
edi.CreateUser = string.Empty;
edi.WeiTuoFaSongFang = string.Empty;
edi.ZhiFaText = string.Empty;
edi.comname = string.Empty;
edi.PORTDISCHARGE = portdischarge;
edi.PORTDISCHARGEID = portdischargeId;
edi.DESTINATION = destination;
edi.DESTINATIONID = destinationId;
edi.VESSEL = vessel;
edi.VOYNO = voyno;
edi.FORWARDER = forwarder;
edi.ETD = DateTime.Today.AddDays(5); //ETD默认当前日期加5天
edi.SHIPPER = dataList[0].SHIPPERNAME;
edi.SHIPPERNAME = dataList[0].SHIPPERNAME;
edi.SHIPPERADDR1 = dataList[0].SHIPPERADDR1;
edi.SHIPPERCOUNTRY = dataList[0].SHIPPERCOUNTRY;
edi.SHIPPERTEL = dataList[0].SHIPPERTEL;
edi.CONSIGNEE = dataList[0].CONSIGNEENAME;
edi.CONSIGNEENAME = dataList[0].CONSIGNEENAME;
edi.CONSIGNEEADDR1 = dataList[0].CONSIGNEEADDR1;
edi.CONSIGNEECOUNTRY = dataList[0].CONSIGNEECOUNTRY;
edi.CONSIGNEETEL = dataList[0].CONSIGNEETEL;
edi.NOTIFYPARTY = dataList[0].NOTIFYPARTYNAME;
edi.NOTIFYPARTYNAME = dataList[0].NOTIFYPARTYNAME;
edi.NOTIFYPARTYADDR1 = dataList[0].NOTIFYPARTYADDR1;
edi.NOTIFYPARTYCOUNTRY = dataList[0].NOTIFYPARTYCOUNTRY;
edi.NOTIFYPARTYTEL = dataList[0].NOTIFYPARTYTEL;
edi.CARGOID = dataList[0].CARGOID;
edi.DESCRIPTION = string.Join("\r\n", dataList.Select(x => x.DESCRIPTION).Distinct());
edi.SERVICE = "CY-CY";
edi.BLFRT = "FREIGHT PREPAID";
edi.KGS = 0;
edi.PKGS = 0;
edi.CBM = 0;
ediList.Add(edi);
//箱信息
for (int i = 0; i < dataList.Count; i++)
{
var item = dataList[i];
var ctn = new OpSeaeEdiCtn();
ctn.ctn_id = Guid.NewGuid().ToString();
ctn.MFNO = edi.MFNO;
ctn.CNTRNO = item.CNTRNO;
ctn.SEALNO = item.SEALNO;
ctn.CTNCODE = i.ToString().PadLeft(2, '0');
ctn.CTNALL = item.CTNALL;
ctn.ZhongKongBiaoShi = 5;
string kindPkgsSrc = item.KINDPKGS;
var codePkg = commonData.Packages.AsNoTracking().FirstOrDefault(p => p.cosco == kindPkgsSrc);
if (codePkg != null)
{
ctn.KINDPKGS = codePkg.PKGS;
//整票
if (i == 0)
{
edi.KINDPKGS = codePkg.PKGS;
}
}
else
{
msg = $"包装种类未找到:{kindPkgsSrc}";
return false;
}
var codeCtn = codeCtnList.FirstOrDefault(cc => cc.EDICODE == item.CTNALL);
if (codeCtn == null)
{
ctn.CTN = string.Empty;
ctn.SIZE = string.Empty;
}
else
{
ctn.CTN = codeCtn.CTN;
ctn.SIZE = codeCtn.CTNSIZE;
}
if (ctn.CTNALL == "45G0")
{
ctn.CTNALL = "40HC";
ctn.CTN = "HC";
ctn.SIZE = "40";
}
else if (ctn.CTNALL == "22G0")
{
ctn.CTNALL = "20GP";
ctn.CTN = "GP";
ctn.SIZE = "20";
}
ctn.ShiFengRen = string.Empty;
ctn.LeiXingFa = string.Empty;
ctn.LeiXingShou = string.Empty;
ctn.LeiXingTong = string.Empty;
ctn.KGS = item.KGS;
ctn.PKGS = item.PKGS;
ctn.CBM = item.CBM;
//合计主单件重尺
edi.KGS += ctn.KGS;
edi.PKGS += ctn.PKGS;
edi.CBM += ctn.CBM;
ctnList.Add(ctn);
}
}
msg = "解析成功";
return true;
}
}
///
/// OLEDB方式读取Excel
///
/// Excel路径
///
public static bool AnalyzeZhongYuanXlsOledb(string pathName, out List ediList, out List ctnList, out string msg)
{
EdiDataContext ediData = new EdiDataContext();
System.Data.DataTable dt = new System.Data.DataTable();
string ConnectionString = string.Empty;
FileInfo file = new FileInfo(pathName);
if (!file.Exists)
{
msg = "文件不存在";
ediList = null;
ctnList = null;
return false;
}
ediList = new List();
ctnList = new List();
string extension = file.Extension;
switch (extension) // 连接字符串
{
case ".xls":
ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathName + ";Extended Properties='Excel 8.0;HDR=no;IMEX=1;'";
break;
case ".xlsx":
ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + pathName + ";Extended Properties='Excel 12.0;HDR=no;IMEX=1;'";
break;
default:
ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathName + ";Extended Properties='Excel 8.0;HDR=no;IMEX=1;'";
break;
}
System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection(ConnectionString);
try
{
con.Open();
{
//默认读取第一个有数据的工作表
var tables = con.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { });
if (tables.Rows.Count == 0)
{
msg = "没有sheet页";
return false;
}
string strSheetTableName = tables.Rows[0]["TABLE_NAME"].ToString();
//过滤无效SheetName
if (strSheetTableName.Contains("$") && strSheetTableName.Replace("'", "").EndsWith("$"))
{
System.Data.DataTable tableColumns = con.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Columns, new object[] { null, null, strSheetTableName, null });
if (tableColumns.Rows.Count < 42) //工作表列数
{
msg = "格式有误";
return false;
}
System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand("select * from [" + strSheetTableName + "]", con);
System.Data.OleDb.OleDbDataAdapter apt = new System.Data.OleDb.OleDbDataAdapter(cmd);
apt.Fill(dt);
dt.TableName = strSheetTableName.Replace("$", "").Replace("'", "");
}
}
if (dt.Rows.Count <= 3 || dt.Columns.Count < 42)
{
msg = "格式有误";
return false;
}
else
{
CommonDataContext commonData = new CommonDataContext();
List parseList = new List();
var vessel = string.Empty;
var voyno = string.Empty;
var portdischarge = string.Empty;
var portdischargeId = string.Empty;
var destination = string.Empty;
var destinationId = string.Empty;
var forwarder = string.Empty;
for (int r = 3; r < dt.Rows.Count; r++)
{
var sheetRow = dt.Rows[r];
var bookNO = sheetRow[0].ToString();
if (string.IsNullOrEmpty(bookNO))
{
break;
}
//船名、航次、卸货港、目的港、船代,只在第一行有
if (r == 3)
{
vessel = sheetRow[37].ToString();
voyno = sheetRow[38].ToString();
portdischarge = sheetRow[39].ToString();
destination = sheetRow[40].ToString();
forwarder = sheetRow[41].ToString();
var dis = commonData.Disports.AsNoTracking().FirstOrDefault(x => x.PORT.Contains(destination));
if (dis != null)
{
portdischarge = dis.PORT;
portdischargeId = dis.EDICODE;
}
dis = commonData.Disports.AsNoTracking().FirstOrDefault(x => x.PORT.Contains(destination));
if (dis != null)
{
destination = dis.PORT;
destinationId = dis.EDICODE;
}
}
var cargoid = sheetRow[35].ToString().Trim();
if (cargoid == "GC-General")
{
cargoid = "S";
}
else if (cargoid == "RF-Reefer")
{
cargoid = "R";
}
else if (cargoid == "DG-Dangerous")
{
cargoid = "D";
}
var cellPKGS = sheetRow[31].ToString();
var cellKGS = sheetRow[32].ToString();
var cellCBM = sheetRow[33].ToString();
int pkgs = 0;
decimal kgs = 0;
decimal cbm = 0;
int.TryParse(cellPKGS, out pkgs);
decimal.TryParse(cellKGS, out kgs);
decimal.TryParse(cellCBM, out cbm);
parseList.Add(new
{
SHIPPERNAME = sheetRow[1].ToString(),
SHIPPERADDR1 = sheetRow[2].ToString(),
SHIPPERCOUNTRY = sheetRow[3].ToString(),
SHIPPERTEL = sheetRow[7].ToString(),
CONSIGNEENAME = sheetRow[10].ToString(),
CONSIGNEEADDR1 = sheetRow[11].ToString(),
CONSIGNEECOUNTRY = sheetRow[12].ToString(),
CONSIGNEETEL = sheetRow[16].ToString(),
NOTIFYPARTYNAME = sheetRow[18].ToString(),
NOTIFYPARTYADDR1 = sheetRow[19].ToString(),
NOTIFYPARTYCOUNTRY = sheetRow[20].ToString(),
NOTIFYPARTYTEL = sheetRow[24].ToString(),
MBLNO = sheetRow[26].ToString(),
HBLNO = sheetRow[27].ToString(),
CNTRNO = sheetRow[28].ToString(),
SEALNO = sheetRow[29].ToString(),
CTNALL = sheetRow[30].ToString(),
PKGS = pkgs,
KGS = kgs,
CBM = cbm,
DESCRIPTION = sheetRow[34].ToString(),
CARGOID = cargoid,
KINDPKGS = sheetRow[36].ToString(),
});
}
var blList = parseList.Select(x => new { x.MBLNO, x.HBLNO }).Distinct().ToList();
//处理主单
foreach (var bl in blList)
{
var dataList = parseList.Where(x => x.MBLNO == bl.MBLNO && x.HBLNO == bl.HBLNO).ToList();
OpSeaeEdi edi = new OpSeaeEdi();
edi.MFNO = Guid.NewGuid().ToString();
if (string.IsNullOrEmpty(bl.HBLNO))
{
edi.BSNO = string.Empty;
}
else
{
//edi.BSNO = ediList.First(x => x.MBLNO == bl.MBLNO && x.HBLNO == "").MFNO;
//2022-8-1,excel里找不到主单,从数据库中找
var zhudan = ediList.FirstOrDefault(x => x.MBLNO == bl.MBLNO && x.HBLNO == "");
if (zhudan == null)
{
string blno = bl.MBLNO;
zhudan = ediData.Edis.AsNoTracking().FirstOrDefault(x => x.MBLNO == blno && x.HBLNO == "");
}
if (zhudan == null)
{
msg = $"找不到主单信息 {bl.MBLNO},分单号:{bl.HBLNO}";
return false;
}
edi.BSNO = zhudan.MFNO;
}
edi.MASTERNO = string.Empty;
edi.EDITYPE = string.Empty;
edi.EDISTATUS = "已录入";
edi.INPUTDATE = DateTime.Now;
edi.CUSTNO = string.Empty;
edi.PORTLOAD = "QINGDAO,CHINA";
edi.PORTLOADID = "CNTAO";
edi.PLACEDELIVERY = "QINGDAO,CHINA";
edi.PLACEDELIVERYID = "CNTAO";
edi.MBLNO = bl.MBLNO;
edi.HBLNO = bl.HBLNO;
edi.MARKS = "N/M";
//部分不能为null的字段
edi.LeiXingFa = string.Empty;
edi.LeiXingShou = string.Empty;
edi.LeiXingTong = string.Empty;
edi.DaiMaFa = string.Empty;
edi.DaiMaShou = string.Empty;
edi.DaiMaTong = string.Empty;
edi.CreateUser = string.Empty;
edi.WeiTuoFaSongFang = string.Empty;
edi.ZhiFaText = string.Empty;
edi.comname = string.Empty;
edi.PORTDISCHARGE = portdischarge;
edi.PORTDISCHARGEID = portdischargeId;
edi.DESTINATION = destination;
edi.DESTINATIONID = destinationId;
edi.VESSEL = vessel;
edi.VOYNO = voyno;
edi.FORWARDER = forwarder;
edi.ETD = DateTime.Today.AddDays(5); //ETD默认当前日期加5天
edi.SHIPPER = dataList[0].SHIPPERNAME;
edi.SHIPPERNAME = dataList[0].SHIPPERNAME;
edi.SHIPPERADDR1 = dataList[0].SHIPPERADDR1;
edi.SHIPPERCOUNTRY = dataList[0].SHIPPERCOUNTRY;
edi.SHIPPERTEL = dataList[0].SHIPPERTEL;
edi.CONSIGNEE = dataList[0].CONSIGNEENAME;
edi.CONSIGNEENAME = dataList[0].CONSIGNEENAME;
edi.CONSIGNEEADDR1 = dataList[0].CONSIGNEEADDR1;
edi.CONSIGNEECOUNTRY = dataList[0].CONSIGNEECOUNTRY;
edi.CONSIGNEETEL = dataList[0].CONSIGNEETEL;
edi.NOTIFYPARTY = dataList[0].NOTIFYPARTYNAME;
edi.NOTIFYPARTYNAME = dataList[0].NOTIFYPARTYNAME;
edi.NOTIFYPARTYADDR1 = dataList[0].NOTIFYPARTYADDR1;
edi.NOTIFYPARTYCOUNTRY = dataList[0].NOTIFYPARTYCOUNTRY;
edi.NOTIFYPARTYTEL = dataList[0].NOTIFYPARTYTEL;
edi.CARGOID = dataList[0].CARGOID;
edi.DESCRIPTION = string.Join("\r\n", dataList.Select(x => x.DESCRIPTION).Distinct());
edi.SERVICE = "CY-CY";
edi.BLFRT = "FREIGHT PREPAID";
edi.KGS = 0;
edi.PKGS = 0;
edi.CBM = 0;
ediList.Add(edi);
//箱信息
for (int i = 0; i < dataList.Count; i++)
{
var item = dataList[i];
var ctn = new OpSeaeEdiCtn();
ctn.ctn_id = Guid.NewGuid().ToString();
ctn.MFNO = edi.MFNO;
ctn.CNTRNO = item.CNTRNO;
ctn.SEALNO = item.SEALNO;
ctn.CTNCODE = i.ToString().PadLeft(2, '0');
ctn.CTNALL = item.CTNALL;
ctn.ZhongKongBiaoShi = 5;
string kindPkgsSrc = item.KINDPKGS;
var codePkg = commonData.Packages.AsNoTracking().FirstOrDefault(p => p.cosco == kindPkgsSrc);
if (codePkg != null)
{
ctn.KINDPKGS = codePkg.PKGS;
//整票
if (i == 0)
{
edi.KINDPKGS = codePkg.PKGS;
}
}
else
{
msg = $"包装种类未找到:{kindPkgsSrc}";
return false;
}
ctn.ShiFengRen = string.Empty;
ctn.LeiXingFa = string.Empty;
ctn.LeiXingShou = string.Empty;
ctn.LeiXingTong = string.Empty;
ctn.KGS = item.KGS;
ctn.PKGS = item.PKGS;
ctn.CBM = item.CBM;
//合计主单件重尺
edi.KGS += ctn.KGS;
edi.PKGS += ctn.PKGS;
edi.CBM += ctn.CBM;
ctnList.Add(ctn);
}
}
}
msg = "";
return true;
}
catch (Exception ee)
{
msg = ee.Message;
ediList = null;
ctnList = null;
return false;
}
finally
{
con.Close();
}
}
#endregion
#region 导入海关XML
public static bool AnalyzeCustomsXML(string xmlFile, out List ediList, out List ctnList, out string msg)
{
List listEdi = new List();
List listEdiCtn = new List();
CommonDataContext commonData = new CommonDataContext();
EdiDataContext ediData = new EdiDataContext();
var codeCtnList = commonData.Ctns.AsNoTracking().ToList();
var codePkgList = commonData.Packages.AsNoTracking().ToList();
var disPortList = commonData.Disports.AsNoTracking().ToList();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlFile);
var nodeFirst = xmlDoc.GetElementsByTagName("Manifest")[0];
var propXmlns = nodeFirst.Attributes.GetNamedItem("xmlns");
var xmlns = propXmlns == null ? string.Empty : propXmlns.Value;
XmlNamespaceManager xnm = new XmlNamespaceManager(xmlDoc.NameTable);
xnm.AddNamespace("x", xmlns);
var nodeDeclaration = nodeFirst.SelectSingleNode("x:Declaration", xnm);
var nodeCarrierId = nodeDeclaration.SelectSingleNode("x:Carrier/x:ID", xnm); //船公司
var nodeBorderTransportMeansJourneyID = nodeDeclaration.SelectSingleNode("x:BorderTransportMeans/x:JourneyID", xnm);//航次 BorderTransportMeans JourneyID
var nodeBorderTransportMeansName = nodeDeclaration.SelectSingleNode("x:BorderTransportMeans/x:Name", xnm);//船名 BorderTransportMeans Name
var listConsignment = nodeDeclaration.SelectNodes("x:Consignment", xnm);
foreach (XmlNode nd in listConsignment)
{
var nodeMBLNO = nd.SelectSingleNode("x:TransportContractDocument/x:ID", xnm); //主提单号
var nodeHBLNO = nd.SelectSingleNode("x:AssociatedTransportDocument/x:ID", xnm); //分提单号
var nodeLoadingLocation = nd.SelectSingleNode("x:LoadingLocation/x:ID", xnm);//装货地
var nodeUnloadingLocation = nd.SelectSingleNode("x:UnloadingLocation/x:ID", xnm);//卸货地
var nodeGoodsReceiptPlace = nd.SelectSingleNode("x:GoodsReceiptPlace/x:ID", xnm);//收货地
var nodeFreightPayment = nd.SelectSingleNode("x:FreightPayment/x:MethodCode", xnm);//支付方式 FreightPayment MethodCode
var nodeConsignmentPackagingQuantityQuantity = nd.SelectSingleNode("x:ConsignmentPackaging/x:QuantityQuantity", xnm);//总件数 ConsignmentPackaging QuantityQuantity
var nodeConsignmentPackagingTypeCode = nd.SelectSingleNode("x:ConsignmentPackaging/x:TypeCode", xnm);//包装种类 ConsignmentPackaging TypeCode
var nodeTotalGrossMassMeasure = nd.SelectSingleNode("x:TotalGrossMassMeasure", xnm);//总毛重 TotalGrossMassMeasure
var nodeConsignee = nd.SelectSingleNode("x:Consignee", xnm);//收货人 Consignee
var nodeConsigneeName = nodeConsignee.SelectSingleNode("x:Name", xnm);//名称 Name
var nodeConsigneeAddress = nodeConsignee.SelectSingleNode("x:Address/x:Line", xnm);//地址 Address Line
var nodeConsigneeAddressCountryCode = nodeConsignee.SelectSingleNode("x:Address/x:CountryCode", xnm);//国家代码 Address CountryCode
var nodeConsigneeCommunicationID = nodeConsignee.SelectSingleNode("x:Communication/x:ID", xnm);//联系人通讯方式号码 Communication ID
var nodeConsigneeCommunicationTypeID = nodeConsignee.SelectSingleNode("x:Communication/x:TypeID", xnm);//联系人通讯方式类别代码 Communication TypeID
var nodeConsignor = nd.SelectSingleNode("x:Consignor", xnm);//发货人 Consignor
var nodeConsignorName = nodeConsignor.SelectSingleNode("x:Name", xnm);//名称 Name
var nodeConsignorAddress = nodeConsignor.SelectSingleNode("x:Address/x:Line", xnm);//地址 Address Line
var nodeConsignorAddressCountryCode = nodeConsignor.SelectSingleNode("x:Address/x:CountryCode", xnm);//国家代码 Address CountryCode
var nodeConsignorCommunicationID = nodeConsignor.SelectSingleNode("x:Communication/x:ID", xnm);//联系人通讯方式号码 Communication ID
var nodeConsignorCommunicationTypeID = nodeConsignor.SelectSingleNode("x:Communication/x:TypeID", xnm);//联系人通讯方式类别代码 Communication TypeID
var nodeNotifyParty = nd.SelectSingleNode("x:NotifyParty", xnm);//通知人 NotifyParty
var nodeNotifyPartyName = nodeNotifyParty.SelectSingleNode("x:Name", xnm);//名称 Name
var nodeNotifyPartyAddress = nodeNotifyParty.SelectSingleNode("x:Address/x:Line", xnm);//地址 Address Line
var nodeNotifyPartyAddressCountryCode = nodeNotifyParty.SelectSingleNode("x:Address/x:CountryCode", xnm);//国家代码 Address CountryCode
var nodeNotifyPartyCommunicationID = nodeNotifyParty.SelectSingleNode("x:Communication/x:ID", xnm);//联系人通讯方式号码 Communication ID
var nodeNotifyPartyCommunicationTypeID = nodeNotifyParty.SelectSingleNode("x:Communication/x:TypeID", xnm);//联系人通讯方式类别代码 Communication TypeID
var nodeUNDGContact = nd.SelectSingleNode("x:UNDGContact", xnm);//危险品联系人信息 UNDGContact
var nodeUNDGContactName = nodeUNDGContact?.SelectSingleNode("x:Name", xnm);//危险品联系人姓名 Name
var nodeUNDGContactCommunicationID = nodeUNDGContact?.SelectSingleNode("x:Communication/x:ID", xnm);//危险品联系人通讯方式号码 Communication ID
var nodeUNDGContactCommunicationTypeID = nodeUNDGContact?.SelectSingleNode("x:Communication/x:TypeID", xnm);//危险品联系人通讯方式类别代码 Communication TypeID
#region 数据对象构建
OpSeaeEdi edi = new OpSeaeEdi();
edi.MFNO = Guid.NewGuid().ToString();
edi.MASTERNO = string.Empty;
edi.EDITYPE = string.Empty;
edi.EDISTATUS = "已录入";
edi.INPUTDATE = DateTime.Now;
edi.CUSTNO = string.Empty;
edi.MBLNO = nodeMBLNO.InnerText;
edi.HBLNO = nodeHBLNO.InnerText;
edi.MARKS = "N/M";
//查找主单
var zhudan = ediData.Edis.AsNoTracking().FirstOrDefault(z => z.MBLNO == nodeMBLNO.InnerText && (z.HBLNO == null || z.HBLNO == ""));
if (zhudan == null)
{
msg = $"未找到主单信息({nodeMBLNO.InnerText}):分单号{nodeHBLNO.InnerText}";
ediList = null;
ctnList = null;
return false;
}
edi.BSNO = zhudan.MFNO;
//部分不能为null的字段
edi.LeiXingFa = string.Empty;
edi.LeiXingShou = string.Empty;
edi.LeiXingTong = string.Empty;
edi.DaiMaFa = string.Empty;
edi.DaiMaShou = string.Empty;
edi.DaiMaTong = string.Empty;
edi.CreateUser = string.Empty;
edi.WeiTuoFaSongFang = string.Empty;
edi.ZhiFaText = string.Empty;
edi.comname = string.Empty;
//var codeDisport = disPortList.FirstOrDefault(x => x.EDICODE == nodeUnloadingLocation.InnerText);
//if (codeDisport == null)
//{
// msg = $"卸货港未找到:{nodeUnloadingLocation.InnerText}";
// ediList = null;
// ctnList = null;
// return false;
//}
// 导入后,船代,船公司,装货港,装货港代码,卸货港,卸货港代码,收货地,收货地代码,目的地,目的地代码,默认取主单的信息。
edi.CARRIER = zhudan.CARRIER;
edi.CARRIERID = zhudan.CARRIERID;
edi.FORWARDER = zhudan.FORWARDER;
edi.PORTLOAD = zhudan.PORTLOAD;
edi.PORTLOADID = zhudan.PORTLOADID;
edi.PLACEDELIVERY = zhudan.PLACEDELIVERY;
edi.PLACEDELIVERYID = zhudan.PLACEDELIVERYID;
edi.PORTDISCHARGE = zhudan.PORTDISCHARGE;
edi.PORTDISCHARGEID = zhudan.PORTDISCHARGEID;
edi.DESTINATION = zhudan.DESTINATION;
edi.DESTINATIONID = zhudan.DESTINATIONID;
edi.VESSEL = nodeBorderTransportMeansName == null || string.IsNullOrEmpty(nodeBorderTransportMeansName.InnerText) ? zhudan.VESSEL : nodeBorderTransportMeansName.InnerText;
edi.VOYNO = nodeBorderTransportMeansJourneyID == null || string.IsNullOrEmpty(nodeBorderTransportMeansJourneyID.InnerText) ? zhudan.VOYNO : nodeBorderTransportMeansJourneyID.InnerText;
edi.ETD = DateTime.Today.AddDays(5); //ETD默认当前日期加5天
edi.SHIPPER = nodeConsignorName.InnerText;
edi.SHIPPERNAME = nodeConsignorName.InnerText;
edi.SHIPPERADDR1 = nodeConsignorAddress.InnerText;
edi.SHIPPERCOUNTRY = nodeConsignorAddressCountryCode==null? zhudan.SHIPPERCOUNTRY : nodeConsignorAddressCountryCode.InnerText;
edi.SHIPPERTEL = nodeConsignorCommunicationID==null ? zhudan.SHIPPERTEL : nodeConsignorCommunicationID?.InnerText;
edi.CONSIGNEE = nodeConsigneeName.InnerText;
edi.CONSIGNEENAME = nodeConsigneeName.InnerText;
edi.CONSIGNEEADDR1 = nodeConsigneeAddress.InnerText;
edi.CONSIGNEECOUNTRY = nodeConsigneeAddressCountryCode==null? zhudan.CONSIGNEECOUNTRY : nodeConsigneeAddressCountryCode.InnerText;
edi.CONSIGNEETEL = nodeConsigneeCommunicationID==null? zhudan.CONSIGNEETEL : nodeConsigneeCommunicationID?.InnerText;
edi.NOTIFYPARTY = nodeNotifyPartyName.InnerText;
edi.NOTIFYPARTYNAME = nodeNotifyPartyName.InnerText;
edi.NOTIFYPARTYADDR1 = nodeNotifyPartyAddress.InnerText;
edi.NOTIFYPARTYCOUNTRY = nodeNotifyPartyAddressCountryCode==null? zhudan.NOTIFYPARTYCOUNTRY : nodeNotifyPartyAddressCountryCode.InnerText;
edi.NOTIFYPARTYTEL = nodeNotifyPartyCommunicationID==null ? zhudan.NOTIFYPARTYTEL: nodeNotifyPartyCommunicationID?.InnerText;
if (nodeUNDGContactName == null || string.IsNullOrEmpty(nodeUNDGContactName.InnerText))
{
edi.CARGOID = "S";
}
else
{
edi.CARGOID = "D";
edi.LINKMAN = nodeUNDGContactName.InnerText;
edi.DTEL = nodeUNDGContactCommunicationID?.InnerText;
}
//edi.DESCRIPTION = string.Join("\r\n", dataList.Select(x => x.DESCRIPTION).Distinct());
edi.SERVICE = "CY-CY";
edi.BLFRT = "FREIGHT PREPAID";
edi.KGS = 0;
edi.PKGS = 0;
edi.CBM = 0;
listEdi.Add(edi);
#endregion
var listTransportEquipment = nd.SelectNodes("x:TransportEquipment", xnm);//集装箱(器)信息 TransportEquipment
var i = 1;
foreach (XmlNode item in listTransportEquipment)
{
var nodeEquipmentIdentificationID = item.SelectSingleNode("x:EquipmentIdentification/x:ID", xnm);//集装箱(器)编 EquipmentIdentification ID
var nodeEquipmentCharacteristicCode = item.SelectSingleNode("x:CharacteristicCode", xnm);//集装箱(器)箱型 CharacteristicCode
var nodeEquipmentSealID = item.SelectSingleNode("x:SealID", xnm);//封号 SealID
var nodeEquipmentFullnessCode = item.SelectSingleNode("x:FullnessCode", xnm);//重箱或者空箱标识代码
var CNTRNO = nodeEquipmentIdentificationID.InnerText;
#region 构建数据对象 箱信息
var ctn = new OpSeaeEdiCtn();
ctn.ctn_id = Guid.NewGuid().ToString();
ctn.MFNO = edi.MFNO;
ctn.CNTRNO = nodeEquipmentIdentificationID.InnerText;
ctn.SEALNO = nodeEquipmentSealID.InnerText.StartsWith("M/") ? nodeEquipmentSealID.InnerText.Substring(2) : nodeEquipmentSealID.InnerText;
ctn.CTNCODE = i++.ToString().PadLeft(2, '0');
//EDICODE找不到就用原始报文里的code
var codeCtn = codeCtnList.FirstOrDefault(cc => cc.EDICODE == nodeEquipmentCharacteristicCode.InnerText);
if (codeCtn == null)
{
ctn.CTNALL = nodeEquipmentCharacteristicCode.InnerText;
ctn.CTN = string.Empty;
ctn.SIZE = string.Empty;
}
else
{
ctn.CTNALL = codeCtn.CTN;
//ctn.CTN = codeCtn.CTN;
ctn.SIZE = codeCtn.CTNSIZE;
}
//45G0,转40HC,22G0,转20GP
if (ctn.CTNALL == "45G0")
{
ctn.CTNALL = "40HC";
ctn.CTN = "HC";
ctn.SIZE = "40";
}
else if (ctn.CTNALL == "22G0")
{
ctn.CTNALL = "20GP";
ctn.CTN = "GP";
ctn.SIZE = "20";
}
if (string.IsNullOrEmpty(nodeEquipmentFullnessCode.InnerText))
{
ctn.ZhongKongBiaoShi = 5; //默认重箱
}
else
{
ctn.ZhongKongBiaoShi = Convert.ToInt32(nodeEquipmentFullnessCode.InnerText);
}
ctn.ShiFengRen = string.Empty;
ctn.LeiXingFa = string.Empty;
ctn.LeiXingShou = string.Empty;
ctn.LeiXingTong = string.Empty;
ctn.ChengZhongFangShi = "累加";
ctn.ChengZhongShiJian = DateTime.Today.AddDays(5).ToString("yyyy-MM-dd");
listEdiCtn.Add(ctn);
#endregion
}
i = 0;
var listConsignmentItem = nd.SelectNodes("x:ConsignmentItem", xnm);//商品项信息 ConsignmentItem
foreach (XmlNode item in listConsignmentItem)
{
var nodeSequenceNumeric = item.SelectSingleNode("x:SequenceNumeric", xnm);//商品项序号 SequenceNumeric
var nodeConsignmentItemPackaging = item.SelectSingleNode("x:ConsignmentItemPackaging", xnm);//商品项包装信息 ConsignmentItemPackaging
var nodeConsignmentItemPackagingQuantityQuantity = nodeConsignmentItemPackaging.SelectSingleNode("x:QuantityQuantity", xnm);//商品项货物件数 QuantityQuantity
var nodeConsignmentItemPackagingQuantityTypeCode = nodeConsignmentItemPackaging.SelectSingleNode("x:TypeCode", xnm);//包装种类代码 TypeCode
var nodeConsignmentItemPackagingQuantityMarksNumbers = nodeConsignmentItemPackaging.SelectSingleNode("x:MarksNumbers", xnm);//唛头 MarksNumbers
var nodeConsignmentItemCommodity = item.SelectSingleNode("x:Commodity", xnm);//商品项简要描述 Commodity
var nodeConsignmentItemCommodityCargoDescription = nodeConsignmentItemCommodity.SelectSingleNode("x:CargoDescription", xnm);//商品项简要描述 CargoDescription
var nodeConsignmentItemCommodityUNDGCode = nodeConsignmentItemCommodity.SelectSingleNode("x:UNDGCode", xnm);//危险品编号 UNDGCode
var nodeConsignmentItemAdditionalInformationContent = item.SelectSingleNode("x:AdditionalInformation/x:Content", xnm);//商品项补充描述 备注 AdditionalInformation Content
var nodeConsignmentItemGoodsMeasureGrossMassMeasure = item.SelectSingleNode("x:GoodsMeasure/x:GrossMassMeasure", xnm);//商品项货物毛重 GoodsMeasure GrossMassMeasure
var nodeConsignmentItemEquipmentIdentificationID = item.SelectSingleNode("x:EquipmentIdentification/x:ID", xnm);//集装箱(器)编号信息 集装箱(器)编号 EquipmentIdentification ID
edi.DESCRIPTION += nodeConsignmentItemCommodity.InnerText;
#region 构建数据对象 箱信息
if (nodeConsignmentItemEquipmentIdentificationID != null && !string.IsNullOrEmpty(nodeConsignmentItemEquipmentIdentificationID.InnerText))
{
var ctn = listEdiCtn.FirstOrDefault(c => c.CNTRNO == nodeConsignmentItemEquipmentIdentificationID.InnerText && c.MFNO == edi.MFNO);
if (ctn != null)
{
string kindPkgsSrc = nodeConsignmentItemPackagingQuantityTypeCode.InnerText;
var codePkgBG = codePkgList.First(p => p.EDICODE == "BG");
if (!string.IsNullOrEmpty(kindPkgsSrc))
{
var codePkg = codePkgList.FirstOrDefault(p => p.EDICODE == kindPkgsSrc);
if (codePkg != null)
{
ctn.KINDPKGS = codePkg.PKGS;
//整票
if (i == 0)
{
edi.KINDPKGS = codePkg.PKGS;
}
}
else
{
//2022-7-25:衣国豪,找不到包装的默认写BAGS(BG)
ctn.KINDPKGS = codePkgBG.PKGS;
//整票
if (i == 0)
{
edi.KINDPKGS = codePkgBG.PKGS;
}
}
}
else
{
//2022-7-25:衣国豪,找不到包装的默认写BAGS(BG)
ctn.KINDPKGS = codePkgBG.PKGS;
//整票
if (i == 0)
{
edi.KINDPKGS = codePkgBG.PKGS;
}
}
ctn.ShiFengRen = "SH";
ctn.LeiXingFa = string.Empty;
ctn.LeiXingShou = string.Empty;
ctn.LeiXingTong = string.Empty;
ctn.KGS = Convert.ToDecimal(nodeConsignmentItemGoodsMeasureGrossMassMeasure.InnerText);
ctn.PKGS = Convert.ToInt32(nodeConsignmentItemPackagingQuantityQuantity.InnerText);
ctn.ChengZhongZhongLiang = ctn.KGS + ctn.PIZHONG;
ctn.CBM = 0;
ctn.GOODVALUE = 0;
//合计主单件重尺
edi.KGS += ctn.KGS;
edi.PKGS += ctn.PKGS;
//edi.CBM += ctn.CBM;
}
}
#endregion
i++;
}
if (!CheckEdiDataLength(edi, out string mm))
{
msg = mm;
ediList = null;
ctnList = null;
return false;
}
}
////处理主分单关系
//var fendanList = listEdi.Where(x => !string.IsNullOrEmpty(x.HBLNO)).ToList();
//foreach (var fendan in fendanList)
//{
// var zhudan = listEdi.FirstOrDefault(y => y.MBLNO == fendan.MBLNO && string.IsNullOrEmpty(y.HBLNO));
// if (zhudan == null)
// {
// zhudan = ediData.Edis.AsNoTracking().FirstOrDefault(z => z.MBLNO == fendan.MBLNO && (z.HBLNO == null || z.HBLNO == ""));
// if (zhudan == null)
// {
// msg = $"未找到主单信息:{fendan.MBLNO}";
// ediList = null;
// ctnList = null;
// return false;
// }
// }
// fendan.BSNO = zhudan.MFNO;
//}
ediList = listEdi;
ctnList = listEdiCtn;
msg = "处理完成";
return true;
}
#endregion
#region 导入大简云excel
public static bool AnalyzeDJYXLS(string filePath, out List ediList, out List ctnList, out string msg, out bool flag , bool merge)
{
EdiDataContext ediData = new EdiDataContext();
using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
IWorkbook hssfworkbook = null;
ISheet sheet = null;
if (Path.GetExtension(filePath).Equals(".xls"))
{
hssfworkbook = new HSSFWorkbook(file);
sheet = hssfworkbook.GetSheetAt(0) as HSSFSheet;
}
else
{
hssfworkbook = new XSSFWorkbook(file);
sheet = hssfworkbook.GetSheetAt(0) as XSSFSheet;
}
ediList = new List();
ctnList = new List();
CommonDataContext commonData = new CommonDataContext();
var codeCtnList = commonData.Ctns.AsNoTracking().ToList();
List parseList = new List();
var vessel = string.Empty;//船名
var voyno = string.Empty;//航次
var portdischarge = string.Empty;////卸货港
var portdischargeId = string.Empty;////卸货港
var destination = string.Empty;//目的地
var destinationId = string.Empty;//目的地
var forwarder = string.Empty;//船代
var carrier = string.Empty;//船公司
var colCount = sheet.GetRow(0).Cells.Count;
if (colCount != 35)
{
msg = "excel文件格式有误";
flag = false;
return false;
}
for (int r = 1; r <= sheet.LastRowNum; r++)
{
var sheetRow = sheet.GetRow(r);
if (sheetRow == null)
{
break;
}
if (string.IsNullOrWhiteSpace(sheetRow.GetCell(24).GetStringValue(string.Empty)))
{
break;
}
vessel = sheetRow.GetCell(24).GetStringValue(string.Empty);
var cellVoyno = sheetRow.GetCell(25);
if (cellVoyno.CellType == NPOI.SS.UserModel.CellType.NUMERIC)
{
voyno = cellVoyno.NumericCellValue.ToString();
}
else
{
voyno = cellVoyno.GetStringValue(string.Empty);
}
portdischarge = sheetRow.GetCell(26).GetStringValue(string.Empty);
destination = sheetRow.GetCell(27).GetStringValue(string.Empty);
forwarder = sheetRow.GetCell(28).GetStringValue(string.Empty);
var dis = commonData.Disports.AsNoTracking().FirstOrDefault(x => x.PORT.Contains(destination));
if (dis != null)
{
portdischarge = dis.PORT;
portdischargeId = dis.EDICODE;
}
dis = commonData.Disports.AsNoTracking().FirstOrDefault(x => x.PORT.Contains(destination));
if (dis != null)
{
destination = dis.PORT;
destinationId = dis.EDICODE;
}
var cargoid = sheetRow.GetCell(22).GetStringValue(string.Empty).Trim();
if (cargoid == "GC-General" || cargoid == "普通货")
{
cargoid = "S";
}
else if (cargoid == "RF-Reefer" || cargoid == "冻柜")
{
cargoid = "R";
}
else if (cargoid == "DG-Dangerous" || cargoid == "危险品")
{
cargoid = "D";
}
else if (cargoid == "超限箱")
{
cargoid = "O";
}
var cellPKGS = sheetRow.GetCell(18);
var cellKGS = sheetRow.GetCell(19);
var cellCBM = sheetRow.GetCell(20);
int pkgs = 0;
if (cellPKGS.CellType == NPOI.SS.UserModel.CellType.NUMERIC)
{
pkgs = cellPKGS.GetIntValue();
}
else if (cellPKGS.CellType == NPOI.SS.UserModel.CellType.STRING)
{
var p = cellPKGS.GetStringValue(string.Empty);
pkgs = Convert.ToInt16(p.Trim());
}
else {
pkgs = 0;
}
decimal kgs = 0;
if (cellKGS.CellType == NPOI.SS.UserModel.CellType.NUMERIC)
{
kgs = cellKGS.GetDecimalValue();
}
else if (cellKGS.CellType == NPOI.SS.UserModel.CellType.STRING)
{
kgs = Convert.ToDecimal(cellKGS.GetStringValue(string.Empty).Trim());
}
else {
kgs = 0;
}
decimal cbm = 0;
if (cellCBM.CellType == NPOI.SS.UserModel.CellType.NUMERIC)
{
cbm = cellCBM.GetDecimalValue();
}
else if (cellCBM.CellType == NPOI.SS.UserModel.CellType.STRING)
{
cbm = Convert.ToDecimal(cellCBM.GetStringValue(string.Empty).Trim());
}
else {
cbm = 0;
}
var cellMBLNO = sheetRow.GetCell(12);
var mblno = cellMBLNO.GetStringValue(string.Empty);
var cellHBLNO = sheetRow.GetCell(13);
var hblno = "";
if (cellHBLNO != null)
{
if (cellHBLNO.CellType == NPOI.SS.UserModel.CellType.NUMERIC)
{
hblno = Convert.ToString(cellHBLNO.NumericCellValue);
}
else
{
hblno = cellHBLNO.GetStringValue(string.Empty)?.Trim();
}
}
parseList.Add(new
{
MFNO = Guid.NewGuid().ToString(),
SHIPPERNAME = sheetRow.GetCell(0).GetStringValue(string.Empty),
SHIPPERADDR1 = sheetRow.GetCell(1).GetStringValue(string.Empty),
SHIPPERCOUNTRY = sheetRow.GetCell(2).GetStringValue(string.Empty),
SHIPPERTEL = sheetRow.GetCell(3).GetStringValue(string.Empty),
CONSIGNEENAME = sheetRow.GetCell(4).GetStringValue(string.Empty),
CONSIGNEEADDR1 = sheetRow.GetCell(5).GetStringValue(string.Empty),
CONSIGNEECOUNTRY = sheetRow.GetCell(6).GetStringValue(string.Empty),
CONSIGNEETEL = sheetRow.GetCell(7).GetStringValue(string.Empty),
NOTIFYPARTYNAME = sheetRow.GetCell(8).GetStringValue(string.Empty),
NOTIFYPARTYADDR1 = sheetRow.GetCell(9).GetStringValue(string.Empty),
NOTIFYPARTYCOUNTRY = sheetRow.GetCell(10).GetStringValue(string.Empty),
NOTIFYPARTYTEL = sheetRow.GetCell(11).GetStringValue(string.Empty),
MBLNO = mblno,
HBLNO = hblno,
CARRIER = sheetRow.GetCell(14).GetStringValue(string.Empty)?.Trim(),
CNTRNO = sheetRow.GetCell(15).GetStringValue(string.Empty)?.Trim(),
SEALNO = sheetRow.GetCell(16).GetStringValue(string.Empty)?.Trim(),
CTNALL = sheetRow.GetCell(17).GetStringValue(string.Empty),
PKGS = pkgs,
KGS = kgs,
CBM = cbm,
DESCRIPTION = sheetRow.GetCell(21).GetStringValue(string.Empty),
CARGOID = cargoid,
KINDPKGS = sheetRow.GetCell(23).GetStringValue(string.Empty),
DCLASS= sheetRow.GetCell(31).GetStringValue(string.Empty),
DUNNO = sheetRow.GetCell(32).GetStringValue(string.Empty),
DATTN = sheetRow.GetCell(33).GetStringValue(string.Empty),
DTEL = sheetRow.GetCell(34).GetStringValue(string.Empty),
});
}
var blList = parseList.Select(x => new { x.MBLNO, x.HBLNO }).Distinct().ToList();
//处理主单
foreach (var bl in blList)
{
var dataList = parseList.Where(x => x.MBLNO == bl.MBLNO && x.HBLNO == bl.HBLNO).ToList();
OpSeaeEdi edi = new OpSeaeEdi();
edi.MFNO = dataList[0].MFNO;
if (string.IsNullOrEmpty(bl.HBLNO))
{
edi.BSNO = string.Empty;
}
else
{
var zhudan = ediList.FirstOrDefault(x => x.MBLNO == bl.MBLNO && x.HBLNO == "");
if (zhudan == null)
{
string blno = bl.MBLNO;
zhudan = ediData.Edis.AsNoTracking().FirstOrDefault(x => x.MBLNO == blno && x.HBLNO == "");
if (zhudan==null) {
if (parseList.Where(x => x.MBLNO == bl.MBLNO && x.HBLNO == "").Count()>0) {
zhudan = new OpSeaeEdi();
zhudan.MFNO = parseList.Where(x => x.MBLNO == bl.MBLNO && x.HBLNO == "").Select(x=>x.MFNO).FirstOrDefault();
}
}
}
if (zhudan == null)
{
if (!merge)
{
msg = $"找不到主单信息 {bl.MBLNO},分单号:{bl.HBLNO}";
flag = true;
return false;
}
}
if (merge)
{
edi.BSNO = "";
}
else
{
edi.BSNO = zhudan.MFNO;
}
}
edi.MASTERNO = string.Empty;
edi.EDITYPE = string.Empty;
edi.EDISTATUS = "已录入";
edi.INPUTDATE = DateTime.Now;
edi.CUSTNO = string.Empty;
edi.PORTLOAD = "QINGDAO,CHINA";
edi.PORTLOADID = "CNTAO";
edi.PLACEDELIVERY = "QINGDAO,CHINA";
edi.PLACEDELIVERYID = "CNTAO";
edi.MBLNO = bl.MBLNO;
edi.HBLNO = bl.HBLNO;
edi.MARKS = "N/M";
//部分不能为null的字段
edi.LeiXingShou = string.Empty;
edi.LeiXingTong = string.Empty;
edi.DaiMaFa = string.Empty;
edi.DaiMaShou = string.Empty;
edi.DaiMaTong = string.Empty;
edi.CreateUser = string.Empty;
edi.WeiTuoFaSongFang = string.Empty;
edi.ZhiFaText = string.Empty;
edi.comname = string.Empty;
edi.PORTDISCHARGE = portdischarge;
edi.PORTDISCHARGEID = portdischargeId;
edi.DESTINATION = destination;
edi.DESTINATIONID = destinationId;
edi.VESSEL = vessel;
edi.VOYNO = voyno;
edi.CARRIER = dataList[0].CARRIER;
edi.FORWARDER = forwarder;
edi.ETD = DateTime.Today.AddDays(5); //ETD默认当前日期加5天
edi.SHIPPER = dataList[0].SHIPPERNAME;
edi.SHIPPERNAME = dataList[0].SHIPPERNAME;
edi.SHIPPERADDR1 = dataList[0].SHIPPERADDR1;
edi.SHIPPERCOUNTRY = dataList[0].SHIPPERCOUNTRY;
edi.SHIPPERTEL = dataList[0].SHIPPERTEL;
edi.CONSIGNEE = dataList[0].CONSIGNEENAME;
edi.CONSIGNEENAME = dataList[0].CONSIGNEENAME;
edi.CONSIGNEEADDR1 = dataList[0].CONSIGNEEADDR1;
edi.CONSIGNEECOUNTRY = dataList[0].CONSIGNEECOUNTRY;
edi.CONSIGNEETEL = dataList[0].CONSIGNEETEL;
edi.NOTIFYPARTY = dataList[0].NOTIFYPARTYNAME;
edi.NOTIFYPARTYNAME = dataList[0].NOTIFYPARTYNAME;
edi.NOTIFYPARTYADDR1 = dataList[0].NOTIFYPARTYADDR1;
edi.NOTIFYPARTYCOUNTRY = dataList[0].NOTIFYPARTYCOUNTRY;
edi.NOTIFYPARTYTEL = dataList[0].NOTIFYPARTYTEL;
edi.CARGOID = dataList[0].CARGOID;
edi.DESCRIPTION = string.Join("\r\n", dataList.Select(x => x.DESCRIPTION).Distinct());
edi.SERVICE = "CY-CY";
edi.BLFRT = "FREIGHT PREPAID";
edi.DCLASS = dataList[0].DCLASS;
edi.DUNNO = dataList[0].DUNNO;
edi.DTEL = dataList[0].DTEL;
edi.DATTN = dataList[0].DATTN;
edi.KGS = 0;
edi.PKGS = 0;
edi.CBM = 0;
//edi.SHIPPERADDR2 = "";
//edi.SHIPPERADDR3 = "";
//edi.SHIPPERCITY = "";
//edi.SHIPPERPROVINCE = "";
//edi.SHIPPERPOSTCODE = "";
//edi.SHIPPERATTN = "";
//edi.CONSIGNEEADDR2 = "";
//edi.CONSIGNEEADDR3 = "";
//edi.CONSIGNEECITY = "";
//edi.CONSIGNEEPROVINCE= "";
//edi.CONSIGNEECODE = "";
//edi.CONSIGNEEATTN = "";
//edi.NOTIFYPARTYADDR2 = "";
//edi.NOTIFYPARTYADDR3 = "";
//edi.NOTIFYPARTYCITY = "";
//edi.NOTIFYPARTYPROVINCE = "";
//edi.NOTIFYPARTYCODE = "";
//edi.NOTIFYPARTYATTN = "";
//edi.NOTIFYPARTY2NAME = "";
//edi.NOTIFYPARTY2ADDR1 = "";
//edi.NOTIFYPARTY2ADDR2 = "";
//edi.NOTIFYPARTY2ADDR3 = "";
//edi.NOTIFYPARTY2CITY = "";
//edi.NOTIFYPARTY2POSTCODE = "";
//edi.NOTIFYPARTY2COUNTRY = "";
//edi.NOTIFYPARTY2ATTN = "";
//edi.NOTIFYPARTY2TEL = "";
//edi.REEFERF = "0";
//edi.TEMPID = "C";
//edi.TEMPSET = "0";
//edi.TEMPMAX = "0";
//edi.TEMPMIN = "0";
//edi.FREIGHTAGMNO = "0";
//edi.HUMIDITY = "0";
//edi.SHIPIMO = "";
//edi.CARRIERID = "";
//edi.ORDERNO = "";
//edi.FILEROLE = "";
//edi.PACKINGTYPE = "";
//edi.SHIPSERVICENO = "";
//edi.TRANSTYPE = "";
//edi.FILETYPE = "";
//edi.ISSUEPLACE = "";
//edi.ETDTIME = "";
//edi.AGENTID = "";
//edi.CNTRSEALNO = "";
//edi.YARD = "";
//edi.STATUS = "";
//edi.CHANGERESON = "";
//edi.FORWARD = "";
//edi.REMARKS = "";
//edi.CNTRNO = "";
//edi.DPAGE = "";
//edi.DLABEL = "";
//edi.LINKMAN = "";
ediList.Add(edi);
//箱信息
for (int i = 0; i < dataList.Count; i++)
{
var item = dataList[i];
var ctn = new OpSeaeEdiCtn();
ctn.ctn_id = Guid.NewGuid().ToString();
ctn.MFNO = edi.MFNO;
ctn.CNTRNO = item.CNTRNO;
ctn.SEALNO = item.SEALNO;
ctn.CTNCODE = i.ToString().PadLeft(2, '0');
ctn.CTNALL = item.CTNALL;
ctn.ZhongKongBiaoShi = 5;
string kindPkgsSrc = item.KINDPKGS;
var codePkg = commonData.Packages.AsNoTracking().FirstOrDefault(p => p.PKGS == kindPkgsSrc);
if (codePkg != null)
{
ctn.KINDPKGS = codePkg.PKGS;
//整票
if (i == 0)
{
edi.KINDPKGS = codePkg.PKGS;
}
}
else
{
msg = $"包装种类未找到:{kindPkgsSrc}";
flag = false;
return false;
}
var codeCtn = codeCtnList.FirstOrDefault(cc => cc.EDICODE == item.CTNALL);
if (codeCtn == null)
{
ctn.CTN = string.Empty;
ctn.SIZE = string.Empty;
}
else
{
ctn.CTN = codeCtn.CTN;
ctn.SIZE = codeCtn.CTNSIZE;
}
if (ctn.CTNALL == "45G0")
{
ctn.CTNALL = "40HC";
ctn.CTN = "HC";
ctn.SIZE = "40";
}
else if (ctn.CTNALL == "22G0")
{
ctn.CTNALL = "20GP";
ctn.CTN = "GP";
ctn.SIZE = "20";
}
ctn.ShiFengRen = string.Empty;
ctn.LeiXingFa = string.Empty;
ctn.LeiXingShou = string.Empty;
ctn.LeiXingTong = string.Empty;
ctn.KGS = item.KGS;
ctn.PKGS = item.PKGS;
ctn.CBM = item.CBM;
//合计主单件重尺
edi.KGS += ctn.KGS;
edi.PKGS += ctn.PKGS;
edi.CBM += ctn.CBM;
ctnList.Add(ctn);
}
}
msg = "解析成功";
flag = false;
return true;
}
}
#endregion
#region 导入CUSTEXCEL
public static bool AnalyzeCUSTEXCEL(string filePath, out List ediList, out List ctnList, out string msg, out bool flag, bool merge)
{
EdiDataContext ediData = new EdiDataContext();
using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
IWorkbook hssfworkbook = null;
ISheet sheet = null;
if (Path.GetExtension(filePath).Equals(".xls"))
{
hssfworkbook = new HSSFWorkbook(file);
sheet = hssfworkbook.GetSheetAt(0) as HSSFSheet;
}
else
{
hssfworkbook = new XSSFWorkbook(file);
sheet = hssfworkbook.GetSheetAt(0) as XSSFSheet;
}
ediList = new List();
ctnList = new List();
CommonDataContext commonData = new CommonDataContext();
var codeCtnList = commonData.Ctns.AsNoTracking().ToList();
List parseList = new List();
var vessel = string.Empty;//船名
var voyno = string.Empty;//航次
var portdischarge = string.Empty;////卸货港
var portdischargeId = string.Empty;////卸货港
var destination = string.Empty;//目的地
var destinationId = string.Empty;//目的地
var forwarder = string.Empty;//船代
var carrier = string.Empty;//船公司
var MARKS = string.Empty;//唛头
var COUNTRY = string.Empty;//国家代码
var colCount = sheet.GetRow(0).Cells.Count;
if (colCount < 34)
{
msg = "excel文件格式有误";
flag = false;
return false;
}
for (int r = 1; r <= sheet.LastRowNum; r++)
{
var sheetRow = sheet.GetRow(r);
if (sheetRow == null)
{
break;
}
if (string.IsNullOrWhiteSpace(sheetRow.GetCell(1).GetStringValue(string.Empty)))
{
break;
}
vessel = sheetRow.GetCell(1).GetStringValue(string.Empty);
var cellVoyno = sheetRow.GetCell(2);
if (cellVoyno.CellType == NPOI.SS.UserModel.CellType.NUMERIC)
{
voyno = cellVoyno.NumericCellValue.ToString();
}
else
{
voyno = cellVoyno.GetStringValue(string.Empty);
}
portdischarge = sheetRow.GetCell(10).GetStringValue(string.Empty);
portdischargeId=sheetRow.GetCell(11).GetStringValue(string.Empty);
destination = sheetRow.GetCell(12).GetStringValue(string.Empty);
destinationId = sheetRow.GetCell(13).GetStringValue(string.Empty);
forwarder = sheetRow.GetCell(0).GetStringValue(string.Empty);
COUNTRY = sheetRow.GetCell(20).GetStringValue(string.Empty);
var cargoid ="S";
var cellPKGS = sheetRow.GetCell(30);
var cellKGS = sheetRow.GetCell(31);
var cellCBM = sheetRow.GetCell(32);
int pkgs = 0;
if (cellPKGS.CellType == NPOI.SS.UserModel.CellType.NUMERIC)
{
pkgs = cellPKGS.GetIntValue();
}
else if (cellPKGS.CellType == NPOI.SS.UserModel.CellType.STRING)
{
pkgs = Convert.ToInt16(cellPKGS.GetStringValue(string.Empty).Trim());
}
else {
pkgs = 0;
}
decimal kgs = 0;
if (cellKGS.CellType == NPOI.SS.UserModel.CellType.NUMERIC)
{
kgs = cellKGS.GetDecimalValue();
}
else if (cellKGS.CellType == NPOI.SS.UserModel.CellType.STRING)
{
kgs = Convert.ToDecimal(cellKGS.GetStringValue(string.Empty).Trim());
}
else {
kgs = 0;
}
decimal cbm = 0;
if (cellCBM.CellType == NPOI.SS.UserModel.CellType.NUMERIC)
{
cbm = cellCBM.GetDecimalValue();
}
else if (cellCBM.CellType == NPOI.SS.UserModel.CellType.STRING)
{
cbm = Convert.ToDecimal(cellCBM.GetStringValue(string.Empty).Trim());
}
else {
cbm = 0;
}
var mblno = sheetRow.GetCell(3).GetStringValue(string.Empty);
var cellHBLNO = sheetRow.GetCell(4);
var hblno = "";
if (cellHBLNO != null)
{
if (cellHBLNO.CellType == NPOI.SS.UserModel.CellType.NUMERIC)
{
hblno = Convert.ToString(cellHBLNO.NumericCellValue);
}
else
{
hblno = cellHBLNO.GetStringValue(string.Empty)?.Trim();
}
}
parseList.Add(new
{
MFNO = Guid.NewGuid().ToString(),
SHIPPERNAME = sheetRow.GetCell(14).GetStringValue(string.Empty),
SHIPPERADDR1 = sheetRow.GetCell(15).GetStringValue(string.Empty),
SHIPPERCOUNTRY = "CN",
SHIPPERTEL = sheetRow.GetCell(17).GetStringValue(string.Empty),
CONSIGNEENAME = sheetRow.GetCell(18).GetStringValue(string.Empty),
CONSIGNEEADDR1 = sheetRow.GetCell(19).GetStringValue(string.Empty),
CONSIGNEECOUNTRY = COUNTRY,
CONSIGNEETEL = sheetRow.GetCell(21).GetStringValue(string.Empty),
NOTIFYPARTYNAME = sheetRow.GetCell(22).GetStringValue(string.Empty),
NOTIFYPARTYADDR1 = sheetRow.GetCell(23).GetStringValue(string.Empty),
NOTIFYPARTYCOUNTRY = COUNTRY,
NOTIFYPARTYTEL = sheetRow.GetCell(25).GetStringValue(string.Empty),
MBLNO = mblno,
HBLNO = hblno,
CARRIER = sheetRow.GetCell(5).GetStringValue(string.Empty)?.Trim(),
CNTRNO = sheetRow.GetCell(27).GetStringValue(string.Empty)?.Trim(),
SEALNO = sheetRow.GetCell(28).GetStringValue(string.Empty)?.Trim(),
CTNALL = sheetRow.GetCell(26).GetStringValue(string.Empty),
PKGS = pkgs,
KGS = kgs,
CBM = cbm,
DESCRIPTION = sheetRow.GetCell(33).GetStringValue(string.Empty),
CARGOID = cargoid,
KINDPKGS = sheetRow.GetCell(29).GetStringValue(string.Empty),
MARKS = sheetRow.GetCell(34).GetStringValue(string.Empty),
});
}
var blList = parseList.Select(x => new { x.MBLNO, x.HBLNO }).Distinct().ToList();
//处理主单
foreach (var bl in blList)
{
var dataList = parseList.Where(x => x.MBLNO == bl.MBLNO && x.HBLNO == bl.HBLNO).ToList();
OpSeaeEdi edi = new OpSeaeEdi();
edi.MFNO = dataList[0].MFNO;
if (string.IsNullOrEmpty(bl.HBLNO))
{
edi.BSNO = string.Empty;
}
else
{
var zhudan = ediList.FirstOrDefault(x => x.MBLNO == bl.MBLNO && x.HBLNO == "");
if (zhudan == null)
{
string blno = bl.MBLNO;
zhudan = ediData.Edis.AsNoTracking().FirstOrDefault(x => x.MBLNO == blno && x.HBLNO == "");
if (zhudan == null)
{
if (parseList.Where(x => x.MBLNO == bl.MBLNO && x.HBLNO == "").Count()>0)
{
zhudan = new OpSeaeEdi();
zhudan.MFNO = parseList.Where(x => x.MBLNO == bl.MBLNO && x.HBLNO == "").Select(x => x.MFNO).FirstOrDefault();
}
}
}
if (zhudan == null)
{
if (!merge)
{
msg = $"找不到主单信息 {bl.MBLNO},分单号:{bl.HBLNO}";
flag = true;
return false;
}
}
if (merge)
{
edi.BSNO = "";
}
else
{
edi.BSNO = zhudan.MFNO;
}
}
edi.MASTERNO = string.Empty;
edi.EDITYPE = string.Empty;
edi.EDISTATUS = "已录入";
edi.INPUTDATE = DateTime.Now;
edi.CUSTNO = string.Empty;
edi.PORTLOAD = "QINGDAO,CHINA";
edi.PORTLOADID = "CNTAO";
edi.PLACEDELIVERY = "QINGDAO,CHINA";
edi.PLACEDELIVERYID = "CNTAO";
edi.MBLNO = bl.MBLNO;
edi.HBLNO = bl.HBLNO;
edi.MARKS = "N/M";
//部分不能为null的字段
edi.LeiXingShou = string.Empty;
edi.LeiXingTong = string.Empty;
edi.DaiMaFa = string.Empty;
edi.DaiMaShou = string.Empty;
edi.DaiMaTong = string.Empty;
edi.CreateUser = string.Empty;
edi.WeiTuoFaSongFang = string.Empty;
edi.ZhiFaText = string.Empty;
edi.comname = string.Empty;
edi.PORTDISCHARGE = portdischarge;
edi.PORTDISCHARGEID = portdischargeId;
edi.DESTINATION = destination;
edi.DESTINATIONID = destinationId;
edi.VESSEL = vessel;
edi.VOYNO = voyno;
edi.CARRIER = dataList[0].CARRIER;
edi.FORWARDER = forwarder;
edi.ETD = DateTime.Today.AddDays(5); //ETD默认当前日期加5天
edi.SHIPPER = dataList[0].SHIPPERNAME;
edi.SHIPPERNAME = dataList[0].SHIPPERNAME;
edi.SHIPPERADDR1 = dataList[0].SHIPPERADDR1;
edi.SHIPPERCOUNTRY = dataList[0].SHIPPERCOUNTRY;
edi.SHIPPERTEL = dataList[0].SHIPPERTEL;
edi.CONSIGNEE = dataList[0].CONSIGNEENAME;
edi.CONSIGNEENAME = dataList[0].CONSIGNEENAME;
edi.CONSIGNEEADDR1 = dataList[0].CONSIGNEEADDR1;
edi.CONSIGNEECOUNTRY = dataList[0].CONSIGNEECOUNTRY;
edi.CONSIGNEETEL = dataList[0].CONSIGNEETEL;
edi.NOTIFYPARTY = dataList[0].NOTIFYPARTYNAME;
edi.NOTIFYPARTYNAME = dataList[0].NOTIFYPARTYNAME;
edi.NOTIFYPARTYADDR1 = dataList[0].NOTIFYPARTYADDR1;
edi.NOTIFYPARTYCOUNTRY = dataList[0].NOTIFYPARTYCOUNTRY;
edi.NOTIFYPARTYTEL = dataList[0].NOTIFYPARTYTEL;
edi.CARGOID = dataList[0].CARGOID;
edi.DESCRIPTION = string.Join("\r\n", dataList.Select(x => x.DESCRIPTION).Distinct());
edi.SERVICE = "CY-CY";
edi.BLFRT = "FREIGHT PREPAID";
edi.MARKS= dataList[0].MARKS;
edi.KGS = 0;
edi.PKGS = 0;
edi.CBM = 0;
ediList.Add(edi);
//箱信息
for (int i = 0; i < dataList.Count; i++)
{
var item = dataList[i];
var ctn = new OpSeaeEdiCtn();
ctn.ctn_id = Guid.NewGuid().ToString();
ctn.MFNO = edi.MFNO;
ctn.CNTRNO = item.CNTRNO;
ctn.SEALNO = item.SEALNO;
ctn.CTNCODE = i.ToString().PadLeft(2, '0');
ctn.CTNALL = item.CTNALL;
ctn.ZhongKongBiaoShi = 5;
string kindPkgsSrc = item.KINDPKGS;
var codePkg = commonData.Packages.AsNoTracking().FirstOrDefault(p => p.PKGS == kindPkgsSrc);
if (codePkg != null)
{
ctn.KINDPKGS = codePkg.PKGS;
//整票
if (i == 0)
{
edi.KINDPKGS = codePkg.PKGS;
}
}
else
{
msg = $"包装种类未找到:{kindPkgsSrc}";
flag = false;
return false;
}
var codeCtn = codeCtnList.FirstOrDefault(cc => cc.EDICODE == item.CTNALL);
if (codeCtn == null)
{
ctn.CTN = string.Empty;
ctn.SIZE = string.Empty;
}
else
{
ctn.CTN = codeCtn.CTN;
ctn.SIZE = codeCtn.CTNSIZE;
}
if (ctn.CTNALL == "45G0")
{
ctn.CTNALL = "40HC";
ctn.CTN = "HC";
ctn.SIZE = "40";
}
else if (ctn.CTNALL == "22G0")
{
ctn.CTNALL = "20GP";
ctn.CTN = "GP";
ctn.SIZE = "20";
}
ctn.ShiFengRen = string.Empty;
ctn.LeiXingFa = string.Empty;
ctn.LeiXingShou = string.Empty;
ctn.LeiXingTong = string.Empty;
ctn.KGS = item.KGS;
ctn.PKGS = item.PKGS;
ctn.CBM = item.CBM;
//合计主单件重尺
edi.KGS += ctn.KGS;
edi.PKGS += ctn.PKGS;
edi.CBM += ctn.CBM;
ctnList.Add(ctn);
}
}
msg = "解析成功";
flag = false;
return true;
}
}
#endregion
private static bool CheckEdiDataLength(OpSeaeEdi edi, out string msg)
{
var errList = new List();
var props = edi.GetType().GetProperties();
foreach (var key in dicEdiChkLen.Keys)
{
var p = props.FirstOrDefault(pp => pp.Name == key);
if (p != null)
{
var valObj = p.GetValue(edi);
if (valObj != null)
{
var len = valObj.ToString().Length;
if (len > dicEdiChkLen[key].Item1)
{
errList.Add($"{dicEdiChkLen[key].Item2}长度超过限制({dicEdiChkLen[key].Item1})");
}
}
}
}
if (errList.Count > 0)
{
msg = string.Join("
", errList);
return false;
}
else
{
msg = string.Empty;
return true;
}
}
}
public static class ICellExtent
{
public static int? GetIntValueNull(this ICell cell)
{
if (cell == null)
{
return null;
}
if (cell.CellType == CellType.NUMERIC)
{
return Convert.ToInt32(cell.NumericCellValue);
}
if (cell.CellType == CellType.STRING)
{
return Convert.ToInt32(cell.StringCellValue);
}
HSSFFormulaEvaluator evalor = new HSSFFormulaEvaluator(cell.Sheet.Workbook);
if (cell.CellType == CellType.FORMULA)
{
var cellVal = evalor.Evaluate(cell);
if (cellVal.CellType == CellType.NUMERIC)
{
return Convert.ToInt32(cellVal.NumberValue);
}
else if (cellVal.CellType == CellType.STRING)
{
return Convert.ToInt32(cell.StringCellValue);
}
}
return null;
}
public static int GetIntValue(this ICell cell)
{
var val = GetIntValueNull(cell);
return val.HasValue ? val.Value : 0;
}
public static decimal? GetDecimalValueNull(this ICell cell)
{
if (cell == null)
{
return null;
}
if (cell.CellType == CellType.NUMERIC)
{
return Convert.ToDecimal(cell.NumericCellValue);
}
if (cell.CellType == CellType.STRING)
{
return Convert.ToDecimal(cell.StringCellValue);
}
HSSFFormulaEvaluator evalor = new HSSFFormulaEvaluator(cell.Sheet.Workbook);
if (cell.CellType == CellType.FORMULA)
{
var cellVal = evalor.Evaluate(cell);
if (cellVal.CellType == CellType.NUMERIC)
{
return Convert.ToDecimal(cellVal.NumberValue);
}
else if (cellVal.CellType == CellType.STRING)
{
return Convert.ToDecimal(cell.StringCellValue);
}
}
return null;
}
public static decimal GetDecimalValue(this ICell cell)
{
var val = GetDecimalValueNull(cell);
return val.HasValue ? val.Value : 0;
}
public static string GetStringValue(this ICell cell, string defVal = null)
{
if (cell == null)
{
return defVal;
}
if (cell.CellType == CellType.NUMERIC)
{
return cell.NumericCellValue.ToString();
}
if (cell.CellType == CellType.STRING)
{
return cell.StringCellValue;
}
HSSFFormulaEvaluator evalor = new HSSFFormulaEvaluator(cell.Sheet.Workbook);
if (cell.CellType == CellType.FORMULA)
{
var cellVal = evalor.Evaluate(cell);
if (cellVal.CellType == CellType.NUMERIC)
{
return cellVal.NumberValue.ToString();
}
else if (cellVal.CellType == CellType.STRING)
{
return cell.StringCellValue;
}
}
return defVal;
}
}
}