generate location, longitude & latitude, and fixed format of date/time generated

This commit is contained in:
misya 2025-04-21 16:13:49 +08:00
parent 7f4d39d2dd
commit c7b398374e
5 changed files with 100 additions and 53 deletions

View File

@ -7,6 +7,7 @@ using PSTW_CentralSystem.Areas.MMS;
using System.Linq;
using QuestPDF.Fluent;
using PSTW_CentralSystem.Areas.MMS.Models.PDFGenerator;
using System.Globalization;
namespace PSTW_CentralSystem.Areas.MMS.Controllers
{
@ -21,13 +22,15 @@ namespace PSTW_CentralSystem.Areas.MMS.Controllers
return View(); // This will look for Index.cshtml in Areas/MMS/Views/Marine
}
//what to require from database into front-end (here is logic for calling, etc.)
public IActionResult TarBallForm()
{
var marineTarballs = _context.MarineTarballs
.Where(t=>t.StationID != "1") //Exclude test data with station named "1"
.Select(t => new
{
t.Id, // Include Id property
Date = t.DateSample,
Date = t.DateSample.ToString("yyyy/MM/dd"), // Format DateSample as needed
Station = t.StationID
})
.ToList();
@ -42,24 +45,29 @@ namespace PSTW_CentralSystem.Areas.MMS.Controllers
return View(marineTarballs);
}
//what to do for generating report
public IActionResult GenerateReport(int id)
{
Console.WriteLine($"Requested ID in GenerateReport: {id}"); // Log the ID
Console.WriteLine($"Requested ID in GenerateReport: {id}");
try
{
// Retrieve the specific record based on the id, including the related StateID
var tarballData = (from marine in _context.MarineTarballs
join station in _context.TarballStations
join station in _context.MarineStations
on marine.StationID equals station.StationID
join state in _context.States
on station.StateID equals state.StateID
where marine.Id == id
select new
{
state.StateName,
marine.StationID,
station.LocationName,
marine.Longitude,
marine.Latitude,
marine.DateSample,
state.StateName // Get the full state name
marine.TimeSample
}).FirstOrDefault();
if (tarballData == null)
@ -67,47 +75,53 @@ namespace PSTW_CentralSystem.Areas.MMS.Controllers
return NotFound("The specified record was not found.");
}
Console.WriteLine($"Found Record: StationID = {tarballData.StationID}, StateName = {tarballData.StateName}");
Console.WriteLine($"DateSample: {tarballData.DateSample}, TimeSample: {tarballData.TimeSample}");
// Generate the PDF
var pdfdocument = new TarBallPDF(tarballData.StationID, tarballData.StateName); // Pass StateID to the PDF generator
var pdf = pdfdocument.GeneratePdf();
var pdfDocument = new TarBallPDF(
tarballData.StateName,
tarballData.StationID,
tarballData.LocationName,
tarballData.Longitude,
tarballData.Latitude,
tarballData.DateSample, // Pass DateSample directly (raw value)
tarballData.TimeSample // Pass TimeSample directly (raw value)
);
var pdf = pdfDocument.GeneratePdf();
Console.WriteLine("PDF generation completed.");
// Construct the filename
var formattedDate = tarballData.DateSample.ToString("ddMMyyyy");
var formattedDate = tarballData.DateSample.ToString("yyyyMMdd");
var fileName = $"TbReport_{tarballData.StationID}_{formattedDate}.pdf";
// Return the file
return File(pdf, "application/pdf", fileName);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return StatusCode(StatusCodes.Status500InternalServerError, "An error occurred while generating the PDF. " + ex.Message);
Console.WriteLine($"Error in GenerateReport: {ex.Message}");
return StatusCode(StatusCodes.Status500InternalServerError, "An error occurred while generating the PDF. Generate PDF error. " + ex.Message);
}
}
public IActionResult ViewPDF(int id)
{
Console.WriteLine("testing"); //TESTING DEBUGGING
Console.WriteLine($"Requested ID in ViewReport: {id}"); // Log the ID
Console.WriteLine($"Requested ID in ViewReport: {id}");
try
{
// Retrieve the specific record based on the id, including the related StateID
var tarballData = (from marine in _context.MarineTarballs
join station in _context.TarballStations
on marine.StationID equals station.StationID into StationGroup
from station in StationGroup.DefaultIfEmpty() //Handle missing
join station in _context.MarineStations
on marine.StationID equals station.StationID
join state in _context.States
on station.StateID equals state.StateID
where marine.Id == id
select new
{
state.StateName,
marine.StationID,
station.LocationName,
marine.Longitude,
marine.Latitude,
marine.DateSample,
StationExists = station != null,
state.StateName // Get the full state name
marine.TimeSample
}).FirstOrDefault();
if (tarballData == null)
@ -115,22 +129,27 @@ namespace PSTW_CentralSystem.Areas.MMS.Controllers
return NotFound("The specified record was not found.");
}
Console.WriteLine($"Found Record: StationID = {tarballData.StationID}, StateName = {tarballData.StateName}");
Console.WriteLine($"DateSample: {tarballData.DateSample}, TimeSample: {tarballData.TimeSample}");
// Generate the PDF - Make sure parameter order matches the constructor
var document = new TarBallPDF(
tarballData.StateName,
tarballData.StationID,
tarballData.LocationName,
tarballData.Longitude,
tarballData.Latitude,
tarballData.DateSample,
tarballData.TimeSample
);
// Generate the PDF
var document = new TarBallPDF(tarballData.StationID, tarballData.StateName); // Pass StateID to the PDF generator
var pdf = document.GeneratePdf();
// Construct the filename
var formattedDate = tarballData.DateSample.ToString("ddMMyyyy");
var fileName = $"TbReport_{tarballData.StationID}_{formattedDate}.pdf";
// Return the file
// Return the file without forcing download
return File(pdf, "application/pdf");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine($"Error in ViewPDF: {ex.Message}");
return StatusCode(StatusCodes.Status500InternalServerError, "An error occurred while viewing the PDF. " + ex.Message);
}
}

View File

@ -1,20 +1,21 @@
using QuestPDF.Fluent;
using QuestPDF.Infrastructure;
using QuestPDF.Helpers;
using Google.Protobuf.WellKnownTypes;
using PSTW_CentralSystem.Models;
namespace PSTW_CentralSystem.Areas.MMS.Models.PDFGenerator
{
public class TarBallPDF : IDocument
public class TarBallPDF(string stationID, string stateName, string locationName, string longitude, string latitude, DateTime dateSample, TimeSpan timeSample) : IDocument
{
private readonly string _stationId;
private readonly string _stateName;
private readonly string _stationId = stationID;
private readonly string _stateName = stateName;
private readonly string _locationName = locationName;
private readonly string _longitude = longitude;
private readonly string _latitude = latitude;
private readonly DateTime _dateSample = dateSample;
private readonly TimeSpan _timeSample = timeSample;
// Constructor
public TarBallPDF(string stationId, string stateName)
{
_stationId = stationId;
_stateName = stateName;
}
// Metadata for the PDF document
public DocumentMetadata GetMetadata() => new()
@ -117,17 +118,18 @@ namespace PSTW_CentralSystem.Areas.MMS.Models.PDFGenerator
table.Cell()
.Background(Colors.Grey.Lighten2).Element(CellStyle).Text("LOCATION")
.Bold();
table.Cell().Element(CellStyle).Text("");
table.Cell().Element(CellStyle).Text(_locationName);
table.Cell()
.Background(Colors.Grey.Lighten2).Element(CellStyle).Text("TARBALL SURVEY LOCATION LONGITUDE & LATITUDE")
.Bold();
table.Cell().Element(CellStyle).Text("");
table.Cell().Element(CellStyle).Text($"{_longitude}, {_latitude}");
// Display DateSample and TimeSample together in the table
table.Cell()
.Background(Colors.Grey.Lighten2).Element(CellStyle).Text("DATE / TIME")
.Bold();
table.Cell().Element(CellStyle).Text("");
table.Cell().Element(CellStyle).Text($"{_dateSample:yyyy-MM-dd} {_timeSample:hh\\:mm\\:ss}");
});
column.Spacing(3);

View File

@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using PSTW_CentralSystem.Models; // Add this to use the MarineTarball class
namespace PSTW_CentralSystem.DBContext
@ -11,9 +12,9 @@ namespace PSTW_CentralSystem.DBContext
// DbSet for tbl_marine_tarball
public DbSet<MarineTarball> MarineTarballs { get; set; }
public DbSet<TarballStation> TarballStations { get; set; }
public DbSet<MarineStation> MarineStations { get; set; }
public DbSet<State> States { get; set; }
public DbSet<MarineClassify> ClassifyID { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
@ -41,16 +42,22 @@ namespace PSTW_CentralSystem.DBContext
entity.Property(e => e.Timestamp).HasColumnName("timestamp");
//Configure relationship with TarballStation
entity.HasOne(m => m.TarballStation)
entity.HasOne(m => m.MarineStation)
.WithMany()
.HasForeignKey(m => m.StationID)
.HasPrincipalKey(t => t.StationID);
//Configure relationship with TarballStation
entity.HasOne(m => m.MarineClassify)
.WithMany()
.HasForeignKey(m => m.ClassifyID)
.HasPrincipalKey(t => t.ClassifyID);
});
// Map TarballStation to tbl_tarball_station
modelBuilder.Entity<TarballStation>().ToTable("tbl_tarball_station");
modelBuilder.Entity<MarineStation>().ToTable("tbl_marine_station");
modelBuilder.Entity<TarballStation>(entity =>
modelBuilder.Entity<MarineStation>(entity =>
{
entity.HasKey(e => e.Id); // Primary key
entity.Property(e => e.Id).HasColumnName("id");
@ -79,6 +86,16 @@ namespace PSTW_CentralSystem.DBContext
entity.Property(e => e.StateName).HasColumnName("stateName").HasMaxLength(50);
});
modelBuilder.Entity<MarineClassify>().ToTable("tbl_classify");
modelBuilder.Entity<MarineClassify>(entity =>
{
entity.HasKey(e => e.Id); // Primary key
entity.Property(e => e.Id).HasColumnName("id");
entity.Property(e => e.ClassifyID).HasColumnName("classifyID").HasMaxLength(2);
entity.Property(e => e.ClassifyName).HasColumnName("classifyName").HasMaxLength(50);
});
}
}
}

View File

@ -12,17 +12,20 @@ namespace PSTW_CentralSystem.Models
public TimeSpan TimeSample { get; set; } // Maps to 'timeSample'
public required string StationID { get; set; } // Maps to 'stationID'
public required string ClassifyID { get; set; } // Maps to 'classifyID'
public double Latitude { get; set; } // Maps to 'latitude'
public double Longitude { get; set; } // Maps to 'longitude'
public required string Latitude { get; set; } // Maps to 'latitude'
public required string Longitude { get; set; } // Maps to 'longitude'
public double GetLatitude { get; set; } // Maps to 'getLatitude'
public double GetLongitude { get; set; } // Maps to 'getLongitude'
public DateTime Timestamp { get; set; } // Maps to 'timestamp'
[ForeignKey("StationID")]
public required TarballStation TarballStation { get; set; }
public required MarineStation MarineStation { get; set; }
[ForeignKey("ClassifyID")]
public required MarineClassify MarineClassify { get; set; }
}
public class TarballStation
public class MarineStation
{
public int Id { get; set; } // Maps to 'id'
public required string StationID { get; set; } // Maps to 'stationID'
@ -44,5 +47,11 @@ namespace PSTW_CentralSystem.Models
public required string StateName { get; set; } // Maps to 'stateName'
}
public class MarineClassify
{
public int Id { get; set; } // Maps to 'id'
public required string ClassifyID{ get; set; } // Maps to classification ID
public required string ClassifyName { get; set; } // Maps to classification name
}
}

View File

@ -6,7 +6,7 @@
"CentralConnnection": "Server=219.92.7.60;Port=3307;uid=installer;password='pstw_mysql_installer';database=pstw_cs;", //DB_dev Public connection
//"InventoryConnection": "Server=219.92.7.60;Port=3307;uid=installer;password='pstw_mysql_installer';database=pstw_cs_inventory;" //DB_dev connection
//"DefaultConnection": "Server=219.92.7.60;Port=3307;uid=intern;password='intern_mysql_acct';database=web_interface;"//DB_dev connection
"MMSDatabase": "Server=192.168.12.42;Port=3306;Uid=mmsuser;password=mms@pstw_mysql_root;database=db_mms;"
"MMSDatabase": "Server=192.168.12.42;Port=3306;Uid=mmsuser;password=mms@pstw_mysql_root;database=db_mms;ConvertZeroDateTime=True;"
},
"Logging": {
"LogLevel": {