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.
BookingHeChuan/Myshipping.Application/EDI/SeaLeadEdiHelper.cs

498 lines
22 KiB
C#

using Furion.FriendlyException;
using Furion.Logging;
using Myshipping.Application.EDI.VOLTA;
using NPOI.HPSF;
using NPOI.HSSF.UserModel;
using NPOI.OpenXmlFormats.Wordprocessing;
using NPOI.SS.UserModel;
using StackExchange.Profiling.Internal;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Myshipping.Application.EDI.SeaLead
{
/// <summary>
/// 海领截单帮助
/// </summary>
public static class SeaLeadEdiHelper
{
#region 生成VOLTA申报报文截单
/// <summary>
/// 生成VOLTA申报报文截单
/// </summary>
/// <param name="model">VOLTA申报详情</param>
/// <returns>返回回执</returns>
public static CommonWebApiResult CreateSIEdi(VOLTAEDIBaseModel model,string fileAbsPath)
{
CommonWebApiResult result = new CommonWebApiResult { succ = false };
//日志
var logger = Log.CreateLogger(nameof(SeaLeadEdiHelper));
try
{
ValidateInput(model);
string bno = model.BookingId;
DateTime nowDate = DateTime.Now;
//读取文件
var file = new FileStream(fileAbsPath, FileMode.Open);
var excelwork = new HSSFWorkbook(file);
//截单主信息
var sheetBase = excelwork.GetSheetAt(0);
if (sheetBase == null)
throw Oops.Bah("读取模板Sheet Header data失败");
var lstLabelRow = sheetBase.GetRow(sheetBase.LastRowNum);
Dictionary<int, string> baseColsDict = lstLabelRow.Cells.Select((p, idx) =>
{
if (p != null)
return new { Name = p.StringCellValue?.Trim(), Idx = idx };
return new { Name = "", Idx = idx };
}).Where(p => !string.IsNullOrWhiteSpace(p.Name)).ToDictionary(a => a.Idx, b => b.Name);
var newRow = sheetBase.CreateRow(sheetBase.LastRowNum + 1);
for (int i = 0; i < lstLabelRow.Cells.Count; i++)
{
if (i > baseColsDict.Max(a => a.Key))
break;
#region 主信息填值
if (baseColsDict[i].Equals("Booking ID", StringComparison.OrdinalIgnoreCase))
{
GenerateCellInfo(model.BookingId, i, newRow);
}
else if (baseColsDict[i].Equals("Shipper", StringComparison.OrdinalIgnoreCase))
{
GenerateCellInfo(model.ShpperName, i, newRow);
}
else if (baseColsDict[i].Equals("Consignee", StringComparison.OrdinalIgnoreCase))
{
GenerateCellInfo(model.ConsigneeName, i, newRow);
}
else if (baseColsDict[i].Equals("Notify Party", StringComparison.OrdinalIgnoreCase))
{
GenerateCellInfo(model.NotifyName, i, newRow);
}
else if (baseColsDict[i].Equals("Additional Notify Party", StringComparison.OrdinalIgnoreCase))
{
GenerateCellInfo(model.NotifySecondName, i, newRow);
}
else if (baseColsDict[i].Equals("Shipper Address", StringComparison.OrdinalIgnoreCase))
{
GenerateCellInfo(model.ShpperAddr, i, newRow);
}
else if (baseColsDict[i].Equals("Consignee Address", StringComparison.OrdinalIgnoreCase))
{
GenerateCellInfo(model.ConsigneeAddr, i, newRow);
}
else if (baseColsDict[i].Equals("Notify Party Address", StringComparison.OrdinalIgnoreCase))
{
GenerateCellInfo(model.NotifyAddr, i, newRow);
}
else if (baseColsDict[i].Equals("Additional Notify Party Address", StringComparison.OrdinalIgnoreCase))
{
GenerateCellInfo(model.NotifySecondAddr, i, newRow);
}
else if (baseColsDict[i].Equals("Shipper Export Reference", StringComparison.OrdinalIgnoreCase))
{
GenerateCellInfo(model.ShipperExportReference, i, newRow);
}
else if (baseColsDict[i].Equals("Shipper Export Reference Type", StringComparison.OrdinalIgnoreCase))
{
GenerateCellInfo(model.ShipperExportReferenceType, i, newRow);
}
else if (baseColsDict[i].Equals("Payment Term", StringComparison.OrdinalIgnoreCase))
{
GenerateCellInfo(model.BLFRT, i, newRow);
}
else if (baseColsDict[i].Equals("Marks and Numbers", StringComparison.OrdinalIgnoreCase))
{
GenerateCellInfo(model.Marks, i, newRow);
}
else if (baseColsDict[i].Equals("Description of Pkgs", StringComparison.OrdinalIgnoreCase))
{
GenerateCellInfo(model.CargoDescription, i, newRow);
}
else if (baseColsDict[i].Equals("Document type", StringComparison.OrdinalIgnoreCase))
{
GenerateCellInfo(model.DocumentTypeCode, i, newRow);
}
else if (baseColsDict[i].Equals("Port Of Loading", StringComparison.OrdinalIgnoreCase))
{
GenerateCellInfo(model.LoadPort, i, newRow);
}
else if (baseColsDict[i].Equals("Port Of Discharge", StringComparison.OrdinalIgnoreCase))
{
GenerateCellInfo(model.DischargePort, i, newRow);
}
else if (baseColsDict[i].Equals("Place of Receipt by Precarrier", StringComparison.OrdinalIgnoreCase))
{
GenerateCellInfo(model.PlaceOfOrigin, i, newRow);
}
else if (baseColsDict[i].Equals("Place of Final Delivery by On Carrier", StringComparison.OrdinalIgnoreCase))
{
GenerateCellInfo(model.FinalDestination, i, newRow);
}
else if (baseColsDict[i].Equals("HS Code", StringComparison.OrdinalIgnoreCase))
{
GenerateCellInfo(model.HSCode, i, newRow);
}
else if (baseColsDict[i].Equals("Precarriage", StringComparison.OrdinalIgnoreCase))
{
GenerateCellInfo(model.Precarriage, i, newRow);
}
#endregion
}
//写箱信息
var sheetCtn = excelwork.GetSheetAt(1);
if (sheetCtn == null)
throw Oops.Bah("读取模板Sheet Container data失败");
int ctnStartNo = sheetCtn.LastRowNum + 1;
var lstCtnLabelRow = sheetCtn.GetRow(sheetCtn.LastRowNum);
var noLabelRow = sheetCtn.GetRow(1);
Dictionary<int, string> ctnColsDict = lstCtnLabelRow.Cells.Select((p, idx) =>
{
if (p != null)
return new { Name = p.StringCellValue?.Trim(), Idx = idx };
return new { Name = "", Idx = idx };
}).Where(p => !string.IsNullOrWhiteSpace(p.Name)).ToDictionary(a => a.Idx, b => b.Name);
for (int j = 0; j < model.ContaList.Count; j++)
{
var newCtnRow = sheetCtn.CreateRow(ctnStartNo);
GenerateCellInfo(model.ContaList[j].SNo.ToString(), 0, newCtnRow);
for (int k = 0; k < lstCtnLabelRow.Cells.Count; k++)
{
if (k > ctnColsDict.Max(a => a.Key))
break;
#region 箱信息填写
if (ctnColsDict[k].Equals("Item Number", StringComparison.OrdinalIgnoreCase))
{
GenerateCellInfo(model.ContaList[j].SNo.ToString(), k + 1, newCtnRow);
}
else if (ctnColsDict[k].Equals("Container Serial Number", StringComparison.OrdinalIgnoreCase))
{
GenerateCellInfo(model.ContaList[j].ContaNo, k + 1, newCtnRow);
}
else if (ctnColsDict[k].Equals("Container type", StringComparison.OrdinalIgnoreCase))
{
GenerateCellInfo(model.ContaList[j].CtnType, k + 1, newCtnRow);
}
else if (ctnColsDict[k].Equals("Outer package ID", StringComparison.OrdinalIgnoreCase))
{
GenerateCellInfo(model.ContaList[j].OuterPackageID, k + 1, newCtnRow);
}
else if (ctnColsDict[k].Equals("Type of Package", StringComparison.OrdinalIgnoreCase))
{
GenerateCellInfo(model.ContaList[j].EdiPkgs, k + 1, newCtnRow);
}
else if (ctnColsDict[k].Equals("Number of outer packages", StringComparison.OrdinalIgnoreCase))
{
GenerateCellInfo(model.ContaList[j].Qty.HasValue ? model.ContaList[j].Qty.Value.ToString("0.##") : "", k + 1, newCtnRow);
}
else if (ctnColsDict[k].Equals("Weight value", StringComparison.OrdinalIgnoreCase))
{
GenerateCellInfo(model.ContaList[j].GWt.HasValue ? model.ContaList[j].GWt.Value.ToString("0.##") : "", k + 1, newCtnRow);
}
else if (ctnColsDict[k].Equals("Gross Weight UoM", StringComparison.OrdinalIgnoreCase))
{
GenerateCellInfo(model.ContaList[j].WTUnit, k + 1, newCtnRow);
}
else if (ctnColsDict[k].Equals("Volume value", StringComparison.OrdinalIgnoreCase))
{
GenerateCellInfo(model.ContaList[j].CBM.HasValue ? model.ContaList[j].CBM.Value.ToString("0.##") : "", k + 1, newCtnRow);
}
else if (ctnColsDict[k].Equals("Gross Volume UoM", StringComparison.OrdinalIgnoreCase))
{
GenerateCellInfo(model.ContaList[j].CBMUnit, k + 1, newCtnRow);
}
else if (ctnColsDict[k].Equals("Length", StringComparison.OrdinalIgnoreCase))
{
GenerateCellInfo(model.ContaList[j].Length.HasValue? model.ContaList[j].Length.Value.ToString("0.##"):"", k + 1, newCtnRow);
}
else if (ctnColsDict[k].Equals("Width", StringComparison.OrdinalIgnoreCase))
{
GenerateCellInfo(model.ContaList[j].Width.HasValue ? model.ContaList[j].Width.Value.ToString("0.##") : "", k + 1, newCtnRow);
}
else if (ctnColsDict[k].Equals("Height", StringComparison.OrdinalIgnoreCase))
{
GenerateCellInfo(model.ContaList[j].Height.HasValue ? model.ContaList[j].Height.Value.ToString("0.##") : "", k + 1, newCtnRow);
}
else if (ctnColsDict[k].Equals("Unit of Measure", StringComparison.OrdinalIgnoreCase))
{
GenerateCellInfo(model.ContaList[j].UnitofMeasure, k + 1, newCtnRow);
}
else if (ctnColsDict[k].Equals("Vehicle Identification Number", StringComparison.OrdinalIgnoreCase))
{
GenerateCellInfo(model.ContaList[j].VehicleIdNumber, k + 1, newCtnRow);
}
else if (ctnColsDict[k].Equals("Vehicle Registration PI", StringComparison.OrdinalIgnoreCase))
{
GenerateCellInfo(model.ContaList[j].VehicleRegistPI, k + 1, newCtnRow);
}
else if (ctnColsDict[k].Equals("Seal Number 1", StringComparison.OrdinalIgnoreCase))
{
GenerateCellInfo(model.ContaList[j].SealNo, k + 1, newCtnRow);
}
else if (ctnColsDict[k].Equals("Seal Number 2", StringComparison.OrdinalIgnoreCase))
{
GenerateCellInfo(model.ContaList[j].SealNumber2, k + 1, newCtnRow);
}
else if (ctnColsDict[k].Equals("Seal Number 3", StringComparison.OrdinalIgnoreCase))
{
GenerateCellInfo(model.ContaList[j].SealNumber3, k + 1, newCtnRow);
}
else if (ctnColsDict[k].Equals("Seal Number 4", StringComparison.OrdinalIgnoreCase))
{
GenerateCellInfo(model.ContaList[j].SealNumber4, k + 1, newCtnRow);
}
else if (ctnColsDict[k].Equals("Seal Number 5", StringComparison.OrdinalIgnoreCase))
{
GenerateCellInfo(model.ContaList[j].SealNumber5, k + 1, newCtnRow);
}
#endregion
}
ctnStartNo++;
}
var fileName = $"{model.BookingId}_SLS_{DateTime.Now.Ticks.ToString()}.xls";//名称
var excelFileName = Path.Combine(model.FilePath, fileName);
var filestream = new FileStream(excelFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
excelwork.Write(filestream);
filestream.Close();
filestream.Dispose();
result.succ = true;
result.extra = excelFileName;
result.extra2 = $"截单样本:{model.Vessel} {model.VoyNo} {model.BookingId} SLS";
}
catch (Exception ex)
{
result.succ = false;
result.msg = ex.Message;
}
return result;
}
#endregion
private static void GenerateCellInfo(string val, int cellIndex, IRow row)
{
if (!string.IsNullOrWhiteSpace(val))
{
row.CreateCell(cellIndex).SetCellValue(val);
}
}
#region 校验VOLTA请求参数s
/// <summary>
/// 校验VOLTA请求参数
/// </summary>
/// <param name="model">VOLTA请求参数</param>
public static void ValidateInput(VOLTAEDIBaseModel model)
{
StringBuilder msgBuilder = new StringBuilder();
if (string.IsNullOrEmpty(model.BookingId))
msgBuilder.AppendLine("提单号不能为空");
if (string.IsNullOrEmpty(model.ShpperName))
msgBuilder.AppendLine("发货人名称不能为空");
if (string.IsNullOrEmpty(model.ShpperAddr))
msgBuilder.AppendLine("发货人地址不能为空");
if (!string.IsNullOrEmpty(model.ShpperAddr))
{
if (model.ShpperAddr.Length > 450)
msgBuilder.AppendLine("发货人地址不能大于450个字符");
}
if (string.IsNullOrEmpty(model.ConsigneeName))
msgBuilder.AppendLine("收货人名称不能为空");
if (string.IsNullOrEmpty(model.ConsigneeAddr))
msgBuilder.AppendLine("收货人地址不能为空");
if (!string.IsNullOrEmpty(model.ConsigneeAddr))
{
if (model.ConsigneeAddr.Length > 450)
msgBuilder.AppendLine("收货人地址不能大于450个字符");
}
if (string.IsNullOrEmpty(model.NotifyName))
msgBuilder.AppendLine("通知人名称不能为空");
if (string.IsNullOrEmpty(model.NotifyAddr))
msgBuilder.AppendLine("通知人地址不能为空");
if (!string.IsNullOrEmpty(model.NotifyAddr))
{
if (model.NotifyAddr.Length > 450)
msgBuilder.AppendLine("通知人地址不能大于450个字符");
}
if (!string.IsNullOrEmpty(model.NotifySecondName) && string.IsNullOrEmpty(model.NotifySecondAddr))
msgBuilder.AppendLine("通知人1地址不能为空");
if (string.IsNullOrEmpty(model.NotifySecondName) && !string.IsNullOrEmpty(model.NotifySecondAddr))
msgBuilder.AppendLine("通知人1名称不能为空");
if (!string.IsNullOrEmpty(model.NotifySecondAddr))
{
if (model.NotifySecondAddr.Length > 450)
msgBuilder.AppendLine("通知人1地址不能大于450个字符");
}
if (string.IsNullOrEmpty(model.PlaceOfOrigin))
msgBuilder.AppendLine("发货地不能为空");
if (string.IsNullOrEmpty(model.LoadPort))
msgBuilder.AppendLine("起运港不能为空");
if (string.IsNullOrEmpty(model.DischargePort))
msgBuilder.AppendLine("卸货港不能为空");
if (string.IsNullOrEmpty(model.FinalDestination))
msgBuilder.AppendLine("最终目的港不能为空");
if (string.IsNullOrEmpty(model.PlaceOfDelivery))
msgBuilder.AppendLine("最终交货地不能为空");
if (string.IsNullOrEmpty(model.CargoDescription))
msgBuilder.AppendLine("品名不能为空");
if (string.IsNullOrEmpty(model.DocumentTypeCode))
msgBuilder.AppendLine("文档类型不能为空");
if (string.IsNullOrEmpty(model.BLFRT))
msgBuilder.AppendLine("付款方式不能为空");
if (string.IsNullOrEmpty(model.HSCode))
{
msgBuilder.AppendLine("HSCode不能为空");
}
else
{
if (!Regex.IsMatch(model.HSCode, "[0-9]{4}\\.[0-9]{2}\\.[0-9]{2}\\.[0-9]{2}") || model.HSCode.Length != 13)
{
msgBuilder.AppendLine("HSCode格式错误请参考格式 0000.00.00.00");
}
}
if (!string.IsNullOrEmpty(model.CargoDescription))
{
if (model.CargoDescription.Length > 3000)
msgBuilder.AppendLine("品名不能大于3000个字符");
}
if (string.IsNullOrEmpty(model.Marks))
msgBuilder.AppendLine("唛头不能为空");
if (!string.IsNullOrEmpty(model.Marks))
{
if (model.Marks.Length > 1000)
msgBuilder.AppendLine("唛头不能大于1000个字符");
}
if (model.ContaList == null || model.ContaList.Count == 0)
msgBuilder.AppendLine("箱明细不能为空");
model.ContaList.ForEach(ctn =>
{
StringBuilder ctnMsgBuilder = new StringBuilder();
if (string.IsNullOrWhiteSpace(ctn.ContaNo))
{
ctnMsgBuilder.Append("箱号不能为空;");
}
else
{
if (ctn.ContaNo.Length > 11)
{
ctnMsgBuilder.Append("箱号长度不能大于11");
}
}
if (string.IsNullOrWhiteSpace(ctn.CtnType))
{
ctnMsgBuilder.Append("箱型不能为空;");
}
if (string.IsNullOrWhiteSpace(ctn.SealNo))
{
ctnMsgBuilder.Append("铅封号不能为空;");
}
if (string.IsNullOrWhiteSpace(ctn.WTUnit))
{
ctnMsgBuilder.Append("重量单位不能为空;");
}
if (string.IsNullOrWhiteSpace(ctn.CBMUnit))
{
ctnMsgBuilder.Append("尺寸单位不能为空;");
}
if (!ctn.GWt.HasValue)
{
ctnMsgBuilder.Append("毛重不能为空;");
}
if (!ctn.CBM.HasValue)
{
ctnMsgBuilder.Append("尺寸不能为空;");
}
if (!ctn.Qty.HasValue)
{
ctnMsgBuilder.Append("件数不能为空;");
}
if (string.IsNullOrWhiteSpace(ctn.EdiPkgs))
{
ctnMsgBuilder.Append("包装不能为空;");
}
if (ctnMsgBuilder.Length > 0)
{
msgBuilder.AppendLine($"箱【{ctn.SNo}】{ctnMsgBuilder.ToString()}");
}
});
if (msgBuilder.Length > 0)
throw Oops.Bah(msgBuilder.ToString());
}
#endregion
}
}