PSTW_CentralizeSystem/Areas/MMS/Controllers/MarineController.cs

157 lines
5.9 KiB
C#

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PSTW_CentralSystem.DBContext;
using PSTW_CentralSystem.Models;
using PSTW_CentralSystem.Areas.MMS;
using System.Linq;
using QuestPDF.Fluent;
using PSTW_CentralSystem.Areas.MMS.Models.PDFGenerator;
using System.Globalization;
using System.IO;
namespace PSTW_CentralSystem.Areas.MMS.Controllers
{
[Area("MMS")]
//[Authorize(Policy = "RoleModulePolicy")]
public class MarineController(MMSSystemContext context) : Controller
{
private readonly MMSSystemContext _context = context;
private const string PhotoBasePath = @"\\192.168.12.42\images\marine\manual_tarball";
public IActionResult Index()
{
return View();
}
public IActionResult TarBallForm()
{
var marineTarballs = _context.MarineTarballs
.Where(t => t.StationID != "1")
.Select(t => new
{
t.Id,
Date = t.DateSample.ToString("yyyy/MM/dd"),
Station = t.StationID
})
.ToList();
// Debugging
foreach (var item in marineTarballs)
{
Console.WriteLine($"Date: {item.Date}, Station: {item.Station}");
}
return View(marineTarballs);
}
public IActionResult GenerateReport(int id)
{
return GeneratePdfResponse(id, true);
}
public IActionResult ViewPDF(int id)
{
return GeneratePdfResponse(id, false);
}
private IActionResult GeneratePdfResponse(int id, bool forceDownload)
{
Console.WriteLine($"Requested ID in {(forceDownload ? "GenerateReport" : "ViewPDF")}: {id}");
try
{
var tarballData = (from marine in _context.MarineTarballs
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,
marine.TimeSample,
marine.ClassifyID,
TarBallYes = marine.ClassifyID != "NO",
TarBallNo = marine.ClassifyID == "NO",
IsSand = marine.ClassifyID == "SD",
IsNonSandy = marine.ClassifyID == "NS",
IsCoquina = marine.ClassifyID == "CO"
}).FirstOrDefault();
if (tarballData == null)
{
return NotFound("The specified record was not found.");
}
// Generate photo paths
var photoBaseName = $"{tarballData.StationID}_{tarballData.DateSample:yyyyMMdd}";
var photoPath1 = GetValidPhotoPath(photoBaseName + "_1.jpg");
var photoPath2 = GetValidPhotoPath(photoBaseName + "_2.jpg");
var photoPath3 = GetValidPhotoPath(photoBaseName + "_3.jpg");
var photoPath4 = GetValidPhotoPath(photoBaseName + "_4.jpg");
var pdfDocument = new TarBallPDF(
tarballData.StateName,
tarballData.StationID,
tarballData.LocationName,
tarballData.Longitude,
tarballData.Latitude,
tarballData.DateSample,
tarballData.TimeSample,
tarballData.ClassifyID,
tarballData.TarBallYes,
tarballData.TarBallNo,
tarballData.IsSand,
tarballData.IsNonSandy,
tarballData.IsCoquina,
photoPath1,
photoPath2,
photoPath3,
photoPath4
);
var pdf = pdfDocument.GeneratePdf();
Console.WriteLine("PDF generation completed.");
if (forceDownload)
{
var fileName = $"TbReport_{tarballData.StationID}_{tarballData.DateSample:yyyyMMdd}.pdf";
return File(pdf, "application/pdf", fileName);
}
return File(pdf, "application/pdf");
}
catch (Exception ex)
{
Console.WriteLine($"Error in {(forceDownload ? "GenerateReport" : "ViewPDF")}: {ex.Message}");
return StatusCode(StatusCodes.Status500InternalServerError,
$"An error occurred while {(forceDownload ? "generating" : "viewing")} the PDF. {ex.Message}");
}
}
private string GetValidPhotoPath(string photoName)
{
var fullPath = Path.Combine(PhotoBasePath, photoName);
return System.IO.File.Exists(fullPath) ? fullPath : null;
}
private string? FindPhoto(string folderPath, string searchPattern)
{
try
{
var files = Directory.GetFiles(folderPath, searchPattern);
return files.Length > 0 ? files[0] : null;
}
catch
{
return null;
}
}
}
}