git_bsmd/ENI2/Excel/ExcelLocalImportHelper.cs
2026-01-21 09:13:11 +01:00

247 lines
10 KiB
C#

using bsmd.database;
using ClosedXML.Excel;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
namespace ENI2.Excel
{
internal static class ExcelLocalImportHelper
{
#region Last 10 Port Facilities Called Import from Excel
public static int ImportLast10PortFacilities(SEC sec)
{
int cnt = 0;
OpenFileDialog ofd = new OpenFileDialog
{
Filter = "Excel Files|*.xls;*.xlsx"
};
if (ofd.ShowDialog() ?? false)
{
try
{
using (var workbook = new XLWorkbook(ofd.FileName))
{
var worksheet = workbook.Worksheet(1);
var rows = worksheet.RangeUsed().RowsUsed();
int columnCount = worksheet.RangeUsed().ColumnCount();
// Check minimum columns upfront
if (columnCount < 8)
{
throw new InvalidDataException("Sheet must have at least 8 Columns of data");
}
List<LastTenPortFacilitiesCalled> importL10C = new List<LastTenPortFacilitiesCalled>();
foreach (var row in rows)
{
if (cnt >= 10) break; // Maximum 10 rows
// Skip empty rows
if (row.Cell(1).IsEmpty() && row.Cell(2).IsEmpty()) continue;
LastTenPortFacilitiesCalled l10c = new LastTenPortFacilitiesCalled();
try
{
// Column 1: Port Name
if (!row.Cell(1).IsEmpty())
l10c.PortFacilityPortName = row.Cell(1).GetString();
// Column 3: Country
if (!row.Cell(3).IsEmpty())
l10c.PortFacilityPortCountry = row.Cell(3).GetString();
// Column 4: Locode
if (!row.Cell(4).IsEmpty())
l10c.PortFacilityPortLoCode = row.Cell(4).GetString();
// Column 5: Arrival Date
if (!row.Cell(5).IsEmpty())
{
l10c.PortFacilityDateOfArrival = row.Cell(5).GetDateTime();
}
// Column 6: Departure Date
if (!row.Cell(6).IsEmpty())
{
l10c.PortFacilityDateOfDeparture = row.Cell(6).GetDateTime();
}
// Column 7: Security Level (byte)
if (!row.Cell(7).IsEmpty())
{
l10c.PortFacilityShipSecurityLevel = row.Cell(7).GetValue<byte>();
}
// Column 8: GISIS Code
if (!row.Cell(8).IsEmpty())
{
int gisis = row.Cell(8).GetValue<int>();
l10c.PortFacilityGISISCode = gisis.ToString().PadLeft(4, '0');
}
// Column 9: Security Matters (optional, can be beyond minimum columns)
if (columnCount >= 9 && !row.Cell(9).IsEmpty())
{
l10c.PortFacilitySecurityMattersToReport = row.Cell(9).GetString();
if (l10c.PortFacilitySecurityMattersToReport?.Equals("nil", StringComparison.OrdinalIgnoreCase) == true)
l10c.PortFacilitySecurityMattersToReport = null;
}
// Column 10: GISIS Code Locode (optional, can be beyond minimum columns)
if (columnCount >= 10 && !row.Cell(10).IsEmpty())
{
l10c.PortFacilityGISISCodeLocode = row.Cell(10).GetString();
}
l10c.SEC = sec;
l10c.IsDirty = true;
l10c.Identifier = LastTenPortFacilitiesCalled.GetNewIdentifier(sec.LastTenPortFacilitesCalled);
sec.LastTenPortFacilitesCalled.Add(l10c);
importL10C.Add(l10c);
cnt++;
}
catch (Exception ex)
{
throw new InvalidDataException($"Error processing row {row.RowNumber()}: {ex.Message}", ex);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error reading Excel: " + ex.Message, Properties.Resources.textCaptionError,
MessageBoxButton.OK, MessageBoxImage.Error);
}
}
return cnt;
}
#endregion
#region Last 30 days Import from Excel
public static int ImportLast30Days(MDH mdh)
{
int cnt = 0;
OpenFileDialog ofd = new OpenFileDialog
{
Filter = "Excel Files|*.xls;*.xlsx"
};
if (ofd.ShowDialog() ?? false)
{
try
{
using (var workbook = new XLWorkbook(ofd.FileName))
{
var worksheet = workbook.Worksheet(1);
var rows = worksheet.RangeUsed().RowsUsed();
int columnCount = worksheet.RangeUsed().ColumnCount();
List<PortOfCallLast30Days> importPoC30 = new List<PortOfCallLast30Days>();
foreach (var row in rows)
{
if (row.RowNumber() > worksheet.RangeUsed().RowCount()) break;
if (columnCount < 3)
{
throw new InvalidDataException("Sheet must have at least 3 Columns of data");
}
PortOfCallLast30Days poc30 = new PortOfCallLast30Days();
if (row.Cell(1).IsEmpty() && row.Cell(2).IsEmpty()) continue;
// Safer locode extraction
if (!row.Cell(1).IsEmpty())
{
try
{
poc30.PortOfCallLast30DaysLocode = row.Cell(1).GetString();
}
catch (Exception ex)
{
throw new InvalidDataException($"Invalid locode in row {row.RowNumber()}: {ex.Message}");
}
}
// Safer date extraction
if (!row.Cell(2).IsEmpty())
{
try
{
poc30.PortOfCallLast30DaysDateOfDeparture = row.Cell(2).GetDateTime();
}
catch (Exception ex)
{
throw new InvalidDataException($"Invalid date in row {row.RowNumber()}: {ex.Message}");
}
}
// Safer boolean extraction
string boolString = "";
if (!row.Cell(3).IsEmpty())
{
boolString = row.Cell(3).GetString()?.Trim() ?? "";
}
poc30.PortOfCallLast30DaysCrewMembersJoined = (
boolString.Equals("y", StringComparison.OrdinalIgnoreCase) ||
boolString.Equals("yes", StringComparison.OrdinalIgnoreCase) ||
boolString.Equals("j", StringComparison.OrdinalIgnoreCase));
// FIX: Check column count before accessing column 4
if (columnCount >= 4)
{
string allNewCrew = row.Cell(4).IsEmpty() ? null : row.Cell(4).GetString()?.Trim();
if (!allNewCrew.IsNullOrEmpty())
{
string[] crewNames = allNewCrew.Split(',', ';');
foreach (string crewName in crewNames)
{
string trimmedName = crewName.Trim();
if (trimmedName.IsNullOrEmpty()) continue;
PortOfCallLast30DaysCrewJoinedShip poc30Crew = new PortOfCallLast30DaysCrewJoinedShip
{
PortOfCallLast30DaysCrewJoinedShipName = trimmedName,
PortOfCallLast30Days = poc30
};
poc30.CrewJoinedShip.Add(poc30Crew);
}
}
}
poc30.MDH = mdh;
mdh.PortOfCallLast30Days.Add(poc30);
importPoC30.Add(poc30);
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error reading Excel: " + ex.Message, Properties.Resources.textCaptionError,
MessageBoxButton.OK, MessageBoxImage.Error);
}
}
return cnt;
}
#endregion
}
}