From 7b6b435dec7d118cc8cfcfcb883e86d9d300907b Mon Sep 17 00:00:00 2001 From: jianghaiqing Date: Thu, 11 Jul 2024 12:25:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9BC=E7=94=9F=E6=88=90=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E9=BB=98=E8=AE=A4=E5=80=BC=20=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E6=B5=B7=E9=A2=86=E6=88=AA=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../EDI/SeaLeadEdiHelper.cs | 345 +++++++++++++++++- .../Myshipping.Application.csproj | 1 + .../BookingOrder/BookingMSKAPIService.cs | 2 +- .../BookingOrder/BookingMSKSPOTAPIService.cs | 2 +- Myshipping.Core/Const/TenantParamCode.cs | 5 + Myshipping.Core/Myshipping.Core.xml | 5 + Myshipping.Core/Service/Auth/AuthService.cs | 3 +- 7 files changed, 349 insertions(+), 14 deletions(-) diff --git a/Myshipping.Application/EDI/SeaLeadEdiHelper.cs b/Myshipping.Application/EDI/SeaLeadEdiHelper.cs index dff2b4e9..8d9c2783 100644 --- a/Myshipping.Application/EDI/SeaLeadEdiHelper.cs +++ b/Myshipping.Application/EDI/SeaLeadEdiHelper.cs @@ -1,5 +1,9 @@ -using Furion.FriendlyException; +using DocumentFormat.OpenXml; +using DocumentFormat.OpenXml.Packaging; +using DocumentFormat.OpenXml.Spreadsheet; +using Furion.FriendlyException; using Furion.Logging; +using Microsoft.VisualBasic; using Myshipping.Application.EDI.VOLTA; using NPOI.HPSF; using NPOI.HSSF.UserModel; @@ -16,6 +20,7 @@ using System.Runtime.InteropServices; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; +using Yitter.IdGenerator; namespace Myshipping.Application.EDI.SeaLead { @@ -45,9 +50,287 @@ namespace Myshipping.Application.EDI.SeaLead DateTime nowDate = DateTime.Now; + var excelFileName = string.Empty; + //读取文件 - var file = new FileStream(fileAbsPath, FileMode.Open); + //var file = new FileStream(fileAbsPath, FileMode.Open); + + var fileName = $"{model.BookingId}_SLS_{DateTime.Now.Ticks.ToString()}.xlsx";//名称 + + excelFileName = Path.Combine(model.FilePath, fileName); + + System.IO.File.Copy(fileAbsPath, excelFileName, true); + + using (SpreadsheetDocument spreadSheetDocument = SpreadsheetDocument.Open(excelFileName, true)) + { + WorkbookPart wbPart = spreadSheetDocument.WorkbookPart; + IEnumerable sheets = spreadSheetDocument.WorkbookPart.Workbook.GetFirstChild().Elements(); + + var sheetNameArgs = sheets.Select(sh => + { + return sh.Id.Value; + }).ToArray(); + + int startNo = 1; + foreach (var sheet in sheetNameArgs) + { + var sheetInfo = new ParserSheetInfo(); + + WorksheetPart worksheetPart = (WorksheetPart)spreadSheetDocument.WorkbookPart.GetPartById(sheet); + Worksheet workSheet = worksheetPart.Worksheet; + SheetData sheetData = workSheet.GetFirstChild(); + + var rowList = sheetData.Descendants().ToList(); + + + if (startNo == 1) + { + var baseColsDict = rowList.ElementAt(rowList.Count - 1).Select((col, idx) => + { + var cell = (Cell)col; + + var colInfo = new ParserColsInfo + { + colsIndx = idx, + colsName = (string)GetCellValue(spreadSheetDocument, cell), + }; + + return colInfo; + }).ToDictionary(a => a.colsIndx, b => b.colsName); + + var newRow = new Row + { + RowIndex = (UInt32)(rowList.Count + 1) + }; + + for (int i = 0; i < baseColsDict.Count; i++) + { + #region 主信息填值 + if (baseColsDict[i].Equals("Booking ID", StringComparison.OrdinalIgnoreCase)) + { + GenerateCellInfoOpenXml(model.BookingId, i, newRow); + } + else if (baseColsDict[i].Equals("Shipper", StringComparison.OrdinalIgnoreCase)) + { + GenerateCellInfoOpenXml(model.ShpperName, i, newRow); + } + else if (baseColsDict[i].Equals("Consignee", StringComparison.OrdinalIgnoreCase)) + { + GenerateCellInfoOpenXml(model.ConsigneeName, i, newRow); + } + else if (baseColsDict[i].Equals("Notify Party", StringComparison.OrdinalIgnoreCase)) + { + GenerateCellInfoOpenXml(model.NotifyName, i, newRow); + } + else if (baseColsDict[i].Equals("Additional Notify Party", StringComparison.OrdinalIgnoreCase)) + { + GenerateCellInfoOpenXml(model.NotifySecondName, i, newRow); + } + else if (baseColsDict[i].Equals("Shipper Address", StringComparison.OrdinalIgnoreCase)) + { + GenerateCellInfoOpenXml(model.ShpperAddr, i, newRow); + } + else if (baseColsDict[i].Equals("Consignee Address", StringComparison.OrdinalIgnoreCase)) + { + GenerateCellInfoOpenXml(model.ConsigneeAddr, i, newRow); + } + else if (baseColsDict[i].Equals("Notify Party Address", StringComparison.OrdinalIgnoreCase)) + { + GenerateCellInfoOpenXml(model.NotifyAddr, i, newRow); + } + else if (baseColsDict[i].Equals("Additional Notify Party Address", StringComparison.OrdinalIgnoreCase)) + { + GenerateCellInfoOpenXml(model.NotifySecondAddr, i, newRow); + } + else if (baseColsDict[i].Equals("Shipper Export Reference", StringComparison.OrdinalIgnoreCase)) + { + GenerateCellInfoOpenXml(model.ShipperExportReference, i, newRow); + } + else if (baseColsDict[i].Equals("Shipper Export Reference Type", StringComparison.OrdinalIgnoreCase)) + { + GenerateCellInfoOpenXml(model.ShipperExportReferenceType, i, newRow); + } + else if (baseColsDict[i].Equals("Payment Term", StringComparison.OrdinalIgnoreCase)) + { + GenerateCellInfoOpenXml(model.BLFRT, i, newRow); + } + else if (baseColsDict[i].Equals("Marks and Numbers", StringComparison.OrdinalIgnoreCase)) + { + GenerateCellInfoOpenXml(model.Marks, i, newRow); + } + else if (baseColsDict[i].Equals("Description of Pkgs", StringComparison.OrdinalIgnoreCase)) + { + GenerateCellInfoOpenXml(model.CargoDescription, i, newRow); + } + else if (baseColsDict[i].Equals("Document type", StringComparison.OrdinalIgnoreCase)) + { + GenerateCellInfoOpenXml(model.DocumentTypeCode, i, newRow); + } + else if (baseColsDict[i].Equals("Port Of Loading", StringComparison.OrdinalIgnoreCase)) + { + GenerateCellInfoOpenXml(model.LoadPort, i, newRow); + } + else if (baseColsDict[i].Equals("Port Of Discharge", StringComparison.OrdinalIgnoreCase)) + { + GenerateCellInfoOpenXml(model.DischargePort, i, newRow); + } + else if (baseColsDict[i].Equals("Place of Receipt by Precarrier", StringComparison.OrdinalIgnoreCase)) + { + GenerateCellInfoOpenXml(model.PlaceOfOrigin, i, newRow); + } + else if (baseColsDict[i].Equals("Place of Final Delivery by On Carrier", StringComparison.OrdinalIgnoreCase)) + { + GenerateCellInfoOpenXml(model.FinalDestination, i, newRow); + } + else if (baseColsDict[i].Equals("HS Code", StringComparison.OrdinalIgnoreCase)) + { + GenerateCellInfoOpenXml(model.HSCode, i, newRow); + } + else if (baseColsDict[i].Equals("Precarriage", StringComparison.OrdinalIgnoreCase)) + { + GenerateCellInfoOpenXml(model.Precarriage, i, newRow); + } + #endregion + } + + sheetData.Append(newRow); + } + else if(startNo == 2) + { + var ctnColsDict = rowList.ElementAt(rowList.Count - 1).Select((col, idx) => + { + var cell = (Cell)col; + + var colInfo = new ParserColsInfo + { + colsIndx = idx, + colsName = (string)GetCellValue(spreadSheetDocument, cell), + }; + + return colInfo; + }).ToDictionary(a => a.colsIndx, b => b.colsName); + + for (int j = 0; j < model.ContaList.Count; j++) + { + var newCtnRow = new Row + { + RowIndex = (UInt32)(rowList.Count + 1) + }; + + GenerateCellInfoOpenXml(model.ContaList[j].SNo.ToString(), 0, newCtnRow); + + for (int k = 0; k < ctnColsDict.Count; k++) + { + #region 箱信息填写 + if (ctnColsDict[k].Equals("Container Serial Number", StringComparison.OrdinalIgnoreCase)) + { + GenerateCellInfoOpenXml(model.ContaList[j].ContaNo, k + 1, newCtnRow); + } + else if (ctnColsDict[k].Equals("Container type", StringComparison.OrdinalIgnoreCase)) + { + GenerateCellInfoOpenXml(model.ContaList[j].CtnType, k + 1, newCtnRow); + } + else if (ctnColsDict[k].Equals("Outer package ID", StringComparison.OrdinalIgnoreCase)) + { + GenerateCellInfoOpenXml(model.ContaList[j].OuterPackageID, k + 1, newCtnRow); + } + else if (ctnColsDict[k].Equals("Type of Package", StringComparison.OrdinalIgnoreCase)) + { + GenerateCellInfoOpenXml(model.ContaList[j].EdiPkgs, k + 1, newCtnRow); + } + else if (ctnColsDict[k].Equals("Number of outer packages", StringComparison.OrdinalIgnoreCase)) + { + GenerateCellInfoOpenXml(model.ContaList[j].Qty.HasValue ? model.ContaList[j].Qty.Value.ToString("0.##") : "", k + 1, newCtnRow); + } + else if (ctnColsDict[k].Equals("Weight value", StringComparison.OrdinalIgnoreCase)) + { + GenerateCellInfoOpenXml(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)) + { + GenerateCellInfoOpenXml(model.ContaList[j].WTUnit, k + 1, newCtnRow); + } + else if (ctnColsDict[k].Equals("Volume value", StringComparison.OrdinalIgnoreCase)) + { + GenerateCellInfoOpenXml(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)) + { + GenerateCellInfoOpenXml(model.ContaList[j].CBMUnit, k + 1, newCtnRow); + } + else if (ctnColsDict[k].Equals("Length", StringComparison.OrdinalIgnoreCase)) + { + GenerateCellInfoOpenXml(model.ContaList[j].Length.HasValue ? model.ContaList[j].Length.Value.ToString("0.##") : "", k + 1, newCtnRow); + } + else if (ctnColsDict[k].Equals("Width", StringComparison.OrdinalIgnoreCase)) + { + GenerateCellInfoOpenXml(model.ContaList[j].Width.HasValue ? model.ContaList[j].Width.Value.ToString("0.##") : "", k + 1, newCtnRow); + } + else if (ctnColsDict[k].Equals("Height", StringComparison.OrdinalIgnoreCase)) + { + GenerateCellInfoOpenXml(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)) + { + GenerateCellInfoOpenXml(model.ContaList[j].UnitofMeasure, k + 1, newCtnRow); + } + else if (ctnColsDict[k].Equals("Vehicle Identification Number", StringComparison.OrdinalIgnoreCase)) + { + GenerateCellInfoOpenXml(model.ContaList[j].VehicleIdNumber, k + 1, newCtnRow); + } + else if (ctnColsDict[k].Equals("Vehicle Registration PI", StringComparison.OrdinalIgnoreCase)) + { + GenerateCellInfoOpenXml(model.ContaList[j].VehicleRegistPI, k + 1, newCtnRow); + } + else if (ctnColsDict[k].Equals("Seal Number 1", StringComparison.OrdinalIgnoreCase)) + { + GenerateCellInfoOpenXml(model.ContaList[j].SealNo, k + 1, newCtnRow); + } + else if (ctnColsDict[k].Equals("Seal Number 2", StringComparison.OrdinalIgnoreCase)) + { + GenerateCellInfoOpenXml(model.ContaList[j].SealNumber2, k + 1, newCtnRow); + } + else if (ctnColsDict[k].Equals("Seal Number 3", StringComparison.OrdinalIgnoreCase)) + { + GenerateCellInfoOpenXml(model.ContaList[j].SealNumber3, k + 1, newCtnRow); + } + else if (ctnColsDict[k].Equals("Seal Number 4", StringComparison.OrdinalIgnoreCase)) + { + GenerateCellInfoOpenXml(model.ContaList[j].SealNumber4, k + 1, newCtnRow); + } + else if (ctnColsDict[k].Equals("Seal Number 5", StringComparison.OrdinalIgnoreCase)) + { + GenerateCellInfoOpenXml(model.ContaList[j].SealNumber5, k + 1, newCtnRow); + } + #endregion + } + + + sheetData.Append(newCtnRow); + } + } + + startNo++; + } + + wbPart.Workbook.Save(); + + + //byte[] pReadByte = new byte[file.Length]; + ////var bytes = file.(); + //var filestream = new FileStream(excelFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); + + //file.Write(pReadByte, 0, pReadByte.Length); + ////ar fileStream = new FileStream(filepath, FileMode.Create, FileAccess.Write, FileShare.Read); + ////fileStream.Write(bytes, 0, bytes.Length); + //file.Flush(); + + //file.Close(); + //file.Dispose(); + //document.Close(); + } + /* var excelwork = new XSSFWorkbook(file); //截单主信息 @@ -291,9 +574,7 @@ namespace Myshipping.Application.EDI.SeaLead excelwork.SetActiveSheet(0); - var fileName = $"{model.BookingId}_SLS_{DateTime.Now.Ticks.ToString()}.xlsx";//名称 - - var excelFileName = Path.Combine(model.FilePath, fileName); + var filestream = new FileStream(excelFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); @@ -302,9 +583,9 @@ namespace Myshipping.Application.EDI.SeaLead //filestream.Flush(); //filestream.Position = 0; - - filestream.Close(); - filestream.Dispose(); + */ + // + //filestream.Dispose(); result.succ = true; result.extra = excelFileName; @@ -320,13 +601,32 @@ namespace Myshipping.Application.EDI.SeaLead } #endregion - private static void GenerateCellInfo(string val, int cellIndex, XSSFRow row, CellType cellType = CellType.String) + public static string GetCellValue(SpreadsheetDocument document, Cell cell) + { + SharedStringTablePart stringTablePart = document.WorkbookPart.SharedStringTablePart; + string value = String.Empty; + + if (cell.CellValue != null) + value = cell.CellValue.InnerXml; + + if (cell.DataType != null && (cell.DataType.Value == CellValues.SharedString || cell.DataType.Value == CellValues.String || cell.DataType.Value == CellValues.Number)) + { + return stringTablePart.SharedStringTable.ChildElements[Int32.Parse(value)].InnerText; + } + else //浮点数和日期对应的cell.DataType都为NULL + { + // DateTime.FromOADate((double.Parse(value)); 如果确定是日期就可以直接用过该方法转换为日期对象,可是无法确定DataType==NULL的时候这个CELL 数据到底是浮点型还是日期.(日期被自动转换为浮点 + return value; + } + } + + private static void GenerateCellInfo(string val, int cellIndex, XSSFRow row, NPOI.SS.UserModel.CellType cellType = NPOI.SS.UserModel.CellType.String) { if (!string.IsNullOrWhiteSpace(val)) { var currCell = (XSSFCell)row.CreateCell(cellIndex, cellType); - if (cellType == CellType.Numeric) + if (cellType == NPOI.SS.UserModel.CellType.Numeric) { currCell.SetCellValue(double.Parse(val)); } @@ -344,10 +644,33 @@ namespace Myshipping.Application.EDI.SeaLead private static void GenerateCellInfoWithInt(int val, int cellIndex, XSSFRow row) { - var currCell = (XSSFCell)row.CreateCell(cellIndex,CellType.String); + var currCell = (XSSFCell)row.CreateCell(cellIndex, NPOI.SS.UserModel.CellType.String); currCell.SetCellValue(val.ToString()); } + private static void GenerateCellInfoOpenXml(string val, int cellIndex, Row row) + { + if (!string.IsNullOrWhiteSpace(val)) + { + var cell = new Cell + { + CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(val), + DataType = new EnumValue(CellValues.String), + }; + row.AppendChild(cell); + + } + else + { + var cell = new Cell + { + CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(""), + DataType = new EnumValue(CellValues.String) + }; + row.AppendChild(cell); + } + } + #region 校验VOLTA请求参数s /// /// 校验VOLTA请求参数 diff --git a/Myshipping.Application/Myshipping.Application.csproj b/Myshipping.Application/Myshipping.Application.csproj index 304132d2..b636ec8f 100644 --- a/Myshipping.Application/Myshipping.Application.csproj +++ b/Myshipping.Application/Myshipping.Application.csproj @@ -42,6 +42,7 @@ + diff --git a/Myshipping.Application/Service/BookingOrder/BookingMSKAPIService.cs b/Myshipping.Application/Service/BookingOrder/BookingMSKAPIService.cs index 1b9a1973..166e1c64 100644 --- a/Myshipping.Application/Service/BookingOrder/BookingMSKAPIService.cs +++ b/Myshipping.Application/Service/BookingOrder/BookingMSKAPIService.cs @@ -964,7 +964,7 @@ namespace Myshipping.Application.Service.BookingOrder if (string.IsNullOrWhiteSpace(model.bookedByCompanyContactEmail)) throw Oops.Bah($"订舱方公司邮箱必填"); - if (string.IsNullOrWhiteSpace(model.priceOwnerContactName)) + if (string.IsNullOrWhiteSpace(model.priceOwnerCompanyName)) throw Oops.Bah($"持约方公司名称必填"); if (string.IsNullOrWhiteSpace(model.priceOwnerMaerskPartyCode)) diff --git a/Myshipping.Application/Service/BookingOrder/BookingMSKSPOTAPIService.cs b/Myshipping.Application/Service/BookingOrder/BookingMSKSPOTAPIService.cs index 070d270d..a39b645f 100644 --- a/Myshipping.Application/Service/BookingOrder/BookingMSKSPOTAPIService.cs +++ b/Myshipping.Application/Service/BookingOrder/BookingMSKSPOTAPIService.cs @@ -1096,7 +1096,7 @@ namespace Myshipping.Application.Service.BookingOrder if (string.IsNullOrWhiteSpace(model.bookedByCompanyContactEmail)) throw Oops.Bah($"订舱方公司邮箱必填"); - if (string.IsNullOrWhiteSpace(model.priceOwnerContactName)) + if (string.IsNullOrWhiteSpace(model.priceOwnerCompanyName)) throw Oops.Bah($"持约方公司名称必填"); if (string.IsNullOrWhiteSpace(model.priceOwnerMaerskPartyCode)) diff --git a/Myshipping.Core/Const/TenantParamCode.cs b/Myshipping.Core/Const/TenantParamCode.cs index 4ea2bb63..600522bf 100644 --- a/Myshipping.Core/Const/TenantParamCode.cs +++ b/Myshipping.Core/Const/TenantParamCode.cs @@ -40,5 +40,10 @@ /// 订舱途径选择 /// public const string BOOKING_CHANNEL_SELECT_SHOW = "BOOKING_CHANNEL_SELECT_SHOW"; + + /// + /// BC任务生成默认选项 + /// + public const string BC_TASK_CREATE_ORDER = "BC_TASK_CREATE_ORDER"; } } diff --git a/Myshipping.Core/Myshipping.Core.xml b/Myshipping.Core/Myshipping.Core.xml index 494dd58f..cb0a2169 100644 --- a/Myshipping.Core/Myshipping.Core.xml +++ b/Myshipping.Core/Myshipping.Core.xml @@ -913,6 +913,11 @@ 订舱途径选择 + + + BC任务生成默认选项 + + 代码 diff --git a/Myshipping.Core/Service/Auth/AuthService.cs b/Myshipping.Core/Service/Auth/AuthService.cs index 4a273214..1cb584b8 100644 --- a/Myshipping.Core/Service/Auth/AuthService.cs +++ b/Myshipping.Core/Service/Auth/AuthService.cs @@ -261,7 +261,8 @@ public class AuthService : IAuthService, IDynamicApiController, ITransient } // 返回前端需要使用的租户参数 - var paraCodeArr = new string[] { TenantParamCode.ENABLE_SLOT_ABILITY, TenantParamCode.ENABLE_FEE_ABILITY, TenantParamCode.VESSEL_FROM_CONFIG_ONLY, TenantParamCode.BOOKING_CHANNEL_SELECT_SHOW }; + var paraCodeArr = new string[] { TenantParamCode.ENABLE_SLOT_ABILITY, TenantParamCode.ENABLE_FEE_ABILITY, TenantParamCode.VESSEL_FROM_CONFIG_ONLY, + TenantParamCode.BOOKING_CHANNEL_SELECT_SHOW, TenantParamCode.BC_TASK_CREATE_ORDER }; loginOutput.TenantParams = await _djyTenantParamService.GetParaCodeWithValue(paraCodeArr); //多账号关联