Compare commits

...

3 Commits

Author SHA1 Message Date
416b283341 merge 2025-06-04 15:49:41 +08:00
82192fde0d Revert "fix test"
This reverts commit 457ee8f5d3.
2025-05-30 11:30:46 +08:00
457ee8f5d3 fix test 2025-05-30 11:15:11 +08:00
3 changed files with 55 additions and 66 deletions

View File

@ -18,23 +18,23 @@ namespace PSTW_CentralSystem.Areas.MMS.Controllers
{ {
// From tbl_marine_tarball // From tbl_marine_tarball
public int Id { get; set; } public int Id { get; set; }
public string StationID { get; set; } public required string StationID { get; set; }
public string Longitude { get; set; } public required string Longitude { get; set; }
public string Latitude { get; set; } public required string Latitude { get; set; }
public DateTime DateSample { get; set; } public required DateTime DateSample { get; set; }
public TimeSpan TimeSample { get; set; } public required TimeSpan TimeSample { get; set; }
public string ClassifyID { get; set; } public required string ClassifyID { get; set; }
public string OptionalName1 { get; set; } public string? OptionalName1 { get; set; }
public string OptionalName2 { get; set; } public string? OptionalName2 { get; set; }
public string OptionalName3 { get; set; } public string? OptionalName3 { get; set; }
public string OptionalName4 { get; set; } public string? OptionalName4 { get; set; }
public string FirstSampler { get; set; } public required string FirstSampler { get; set; }
// From joined tables // From joined tables
public string LocationName { get; set; } // From tbl_marine_station public required string LocationName { get; set; } // From tbl_marine_station
public string StateName { get; set; } // From tbl_state public required string StateName { get; set; } // From tbl_state
public string FullName { get; set; } // From tbl_user public required string FullName { get; set; } // From tbl_user
public string LevelName { get; set; } // From tbl_level public required string LevelName { get; set; } // From tbl_level
} }
[Area("MMS")] [Area("MMS")]
@ -176,7 +176,8 @@ namespace PSTW_CentralSystem.Areas.MMS.Controllers
// Determine content type based on extension // Determine content type based on extension
string contentType = "image/jpeg"; // default string contentType = "image/jpeg"; // default
string extension = Path.GetExtension(sanitizedFileName)?.ToLower(); string extension = Path.GetExtension(sanitizedFileName).ToLower();
//string extension = Path.GetExtension(sanitizedFileName)?.ToLower();
if (extension == ".png") if (extension == ".png")
{ {
@ -256,36 +257,33 @@ namespace PSTW_CentralSystem.Areas.MMS.Controllers
{ {
Console.WriteLine($"Requested ID in {(forceDownload ? "GenerateReport" : "ViewPDF")}: {id}"); Console.WriteLine($"Requested ID in {(forceDownload ? "GenerateReport" : "ViewPDF")}: {id}");
// Temporary network connection test // Test network connection first
try try
{ {
Console.WriteLine("Testing network connection...");
_networkAccessService.ConnectToNetworkPath(); _networkAccessService.ConnectToNetworkPath();
Console.WriteLine("Network connected successfully!");
_networkAccessService.DisconnectFromNetworkShare(); _networkAccessService.DisconnectFromNetworkShare();
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine($"NETWORK ERROR: {ex.Message}");
return StatusCode(500, $"Cannot access network: {ex.Message}"); return StatusCode(500, $"Cannot access network: {ex.Message}");
} }
try try
{ {
// Connect to the network path
_networkAccessService.ConnectToNetworkPath(); _networkAccessService.ConnectToNetworkPath();
// ===== 1. NEW SQL QUERY APPROACH ===== // ===== 1. Get Data from Database =====
var query = @" var query = @"
SELECT SELECT
marine.*, marine.*,
station.LocationName, station.LocationName,
state.StateName, state.StateName,
user.FullName,level.LevelName user.FullName,
level.LevelName
FROM tbl_marine_tarball marine FROM tbl_marine_tarball marine
JOIN tbl_marine_station station ON marine.StationID = station.StationID JOIN tbl_marine_station station ON marine.StationID = station.StationID
JOIN tbl_state state ON station.StateID = state.StateID JOIN tbl_state state ON station.StateID = state.StateID
JOIN tbl_user user ON marine.FirstSampler = user.FullName -- Corrected column name JOIN tbl_user user ON marine.FirstSampler = user.FullName
JOIN tbl_level level ON user.LevelID = level.LevelID JOIN tbl_level level ON user.LevelID = level.LevelID
WHERE marine.Id = @id"; WHERE marine.Id = @id";
@ -296,69 +294,60 @@ namespace PSTW_CentralSystem.Areas.MMS.Controllers
if (tarball == null) if (tarball == null)
return NotFound("Record not found"); return NotFound("Record not found");
//Prepare boolean values for PDF // Prepare boolean values for PDF
bool tarBallYes = tarball.ClassifyID != "NO"; bool tarBallYes = tarball.ClassifyID != "NO";
bool tarBallNo = tarball.ClassifyID == "NO"; bool tarBallNo = tarball.ClassifyID == "NO";
bool isSand = tarball.ClassifyID == "SD"; bool isSand = tarball.ClassifyID == "SD";
bool isNonSandy = tarball.ClassifyID == "NS"; bool isNonSandy = tarball.ClassifyID == "NS";
bool isCoquina = tarball.ClassifyID == "CO"; bool isCoquina = tarball.ClassifyID == "CO";
// ===== 2. Get Images from path ===== // ===== 2. Get Images =====
// For date (stored as DATE in DB → "2025-01-30" becomes "20250130")
var sampleDateString = tarball.DateSample.ToString("yyyyMMdd"); var sampleDateString = tarball.DateSample.ToString("yyyyMMdd");
//var sampleTimeString = tarball.TimeSample.ToString("hhmmss");
var sampleTimePrefix = ((int)tarball.TimeSample.TotalHours).ToString("D2") + // For time (stored as TIME in DB → "16:49:02" becomes "164902")
tarball.TimeSample.Minutes.ToString("D2"); var sampleTimeString = tarball.TimeSample.ToString("hhmmss");
var stationFolder = Path.Combine(PhotoBasePath, tarball.StationID); var stationFolder = Path.Combine(PhotoBasePath, tarball.StationID);
//Image collection
var stationImages = new Dictionary<string, string>(); var stationImages = new Dictionary<string, string>();
var foundAnyImages = false;
if (Directory.Exists(stationFolder)) if (Directory.Exists(stationFolder))
{ {
var imageTypes = new Dictionary<string, string> var basePattern = $"{tarball.StationID}_{sampleDateString}_{sampleTimeString}_";
{ var allImages = Directory.GetFiles(stationFolder, $"{basePattern}*");
{ "LEFTSIDECOASTALVIEW", null },
{ "RIGHTSIDECOASTALVIEW", null },
{ "DRAWINGVERTICALLINES", null },
{ "DRAWINGHORIZONTALLINES", null },
{ "OPTIONAL01", null }, // Will remain null if not found
{ "OPTIONAL02", null },
{ "OPTIONAL03", null },
{ "OPTIONAL04", null }
};
foreach (var imagePath in Directory.GetFiles(stationFolder)) foreach (var imagePath in allImages)
{ {
var fileName = Path.GetFileNameWithoutExtension(imagePath); var fileName = Path.GetFileNameWithoutExtension(imagePath);
foreach (var type in imageTypes.Keys.ToList()) var type = fileName.Split('_').Last();
stationImages[type] = imagePath;
foundAnyImages = true;
}
}
if (!foundAnyImages)
{ {
if (fileName.EndsWith(type, StringComparison.OrdinalIgnoreCase)) return StatusCode(404, "No images found for this record");
{
imageTypes[type] = imagePath;
break;
} }
}
} // Verify mandatory images exist
stationImages = imageTypes; var mandatoryImages = new[] {
}
//Mandatory images
var mandatoryImages = new List<string>
{
"LEFTSIDECOASTALVIEW", "LEFTSIDECOASTALVIEW",
"RIGHTSIDECOASTALVIEW", "RIGHTSIDECOASTALVIEW",
"DRAWINGVERTICALLINES", "DRAWINGVERTICALLINES",
"DRAWINGHORIZONTALLINES" "DRAWINGHORIZONTALLINES"
}; };
foreach(var mandatoryType in mandatoryImages) foreach (var type in mandatoryImages)
{ {
if(!stationImages.ContainsKey(mandatoryType)) if (!stationImages.ContainsKey(type))
{ {
return StatusCode(400, $"Missing mandatory image: {mandatoryType}"); return StatusCode(400, $"Missing mandatory image: {type}");
} }
} }
// ===== 3. GENERATE PDF (ADAPTED FOR NEW DTO) ===== // ===== 3. Generate PDF =====
var pdf = new TarBallPDF( var pdf = new TarBallPDF(
tarball.StateName, tarball.StateName,
tarball.StationID, tarball.StationID,
@ -390,16 +379,15 @@ namespace PSTW_CentralSystem.Areas.MMS.Controllers
tarball.LevelName tarball.LevelName
).GeneratePdf(); ).GeneratePdf();
// ===== 4. Return PDF =====
var downloadName = $"{tarball.StationID}_{sampleDateString}_{sampleTimeString}.pdf";
return forceDownload return forceDownload
? File(pdf, "application/pdf", $"{tarball.StationID}_{tarball.DateSample:yyyyMMdd}_{tarball.TimeSample:hhmmss}.pdf") ? File(pdf, "application/pdf", downloadName)
: File(pdf, "application/pdf"); : File(pdf, "application/pdf");
} }
catch (Exception ex) catch (Exception ex)
{ {
var errorMessage = ex.InnerException != null return StatusCode(500, $"PDF generation failed: {ex.Message}");
? $"{ex.Message} (Inner: {ex.InnerException.Message})"
: ex.Message;
return Content($"PDF generation failed: {errorMessage}<br/>{ex.StackTrace}", "text/html");
} }
finally finally
{ {

View File

@ -11,8 +11,8 @@ namespace PSTW_CentralSystem.Areas.MMS.Models.PDFGenerator
string longitude, string latitude, DateTime dateSample, TimeSpan timeSample, string longitude, string latitude, DateTime dateSample, TimeSpan timeSample,
string classifyID, bool tarBallYes, bool tarBallNo, bool isSand, bool isNonSandy, string classifyID, bool tarBallYes, bool tarBallNo, bool isSand, bool isNonSandy,
bool isCoquina, string photoPath1, string photoPath2, string photoPath3, string photoPath4, bool isCoquina, string photoPath1, string photoPath2, string photoPath3, string photoPath4,
string photoPath5, string photoPath6, string photoPath7, string photoPath8, string? photoPath5, string? photoPath6, string? photoPath7, string? photoPath8,
string optionalName1, string optionalName2, string optionalName3, string optionalName4, string? optionalName1, string? optionalName2, string? optionalName3, string? optionalName4,
string firstSampler, string fullName, string levelName string firstSampler, string fullName, string levelName
) )
: IDocument : IDocument
@ -360,7 +360,7 @@ namespace PSTW_CentralSystem.Areas.MMS.Models.PDFGenerator
table.Cell().Element(CellStyle).Text("Signature").FontSize(12); table.Cell().Element(CellStyle).Text("Signature").FontSize(12);
table.Cell().Element(CellStyle).Text(""); table.Cell().Element(CellStyle).Text("");
table.Cell().Element(CellStyle).Text("Date").FontSize(12); table.Cell().Element(CellStyle).Text("Date").FontSize(12);
table.Cell().Element(CellStyle).Text(_dateSample.ToString("yyyy/MM/dd")).FontSize(12); table.Cell().Element(CellStyle).Text($"{_dateSample:yyyyMMdd}").FontSize(12);
table.Cell().Element(CellStyle).Text("Designation").FontSize(12); table.Cell().Element(CellStyle).Text("Designation").FontSize(12);
table.Cell().ColumnSpan(3).Element(CellStyle).Text(_levelName).FontSize(12); table.Cell().ColumnSpan(3).Element(CellStyle).Text(_levelName).FontSize(12);

View File

@ -156,6 +156,7 @@
}, },
{ "data": "date", "render": (data) => new Date(data).toLocaleDateString('en-GB') }, { "data": "date", "render": (data) => new Date(data).toLocaleDateString('en-GB') },
{ "data": "station" }, { "data": "station" },
{ {
"data": null, "data": null,
"render": () => ` "render": () => `