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

View File

@ -11,8 +11,8 @@ namespace PSTW_CentralSystem.Areas.MMS.Models.PDFGenerator
string longitude, string latitude, DateTime dateSample, TimeSpan timeSample,
string classifyID, bool tarBallYes, bool tarBallNo, bool isSand, bool isNonSandy,
bool isCoquina, string photoPath1, string photoPath2, string photoPath3, string photoPath4,
string photoPath5, string photoPath6, string photoPath7, string photoPath8,
string optionalName1, string optionalName2, string optionalName3, string optionalName4,
string? photoPath5, string? photoPath6, string? photoPath7, string? photoPath8,
string? optionalName1, string? optionalName2, string? optionalName3, string? optionalName4,
string firstSampler, string fullName, string levelName
)
: 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("");
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().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": "station" },
{
"data": null,
"render": () => `