patbef-Simulator/Simulator/Controllers/HomeController.cs

375 lines
13 KiB
C#
Raw Permalink Normal View History

2024-01-29 16:28:09 +01:00
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using Simulator.Models;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
namespace Simulator.Controllers
{
public class HomeController : Controller
{
private static SimulatorDbContext db = new SimulatorDbContext("Server=localhost;Database=simulator;Uid=simulator;Pwd=W5Cok8HAy1XoSa63qaRiM5BA8i1E6I;");
private static string _PrivateDirectory = @"C:\pba\private";
public ActionResult Index()
{
if (this.Session == null || this.Session["user"] == null)
{
return RedirectToAction("Index", "Login");
}
Args args = new Args();
args.Patienten = db.GetPatienten();
args.Befunde = db.GetBefunde();
return View(args);
}
[HttpPost]
public ActionResult createPatient(Patient patient)
{
if (this.Session == null || this.Session["user"] == null)
{
return RedirectToAction("Index", "Login");
}
object response = new { status = "", message = "" };
if (patient == null || string.IsNullOrEmpty(patient.Name))
{
response = new { status = "error", message = "Bitte geben Sie den Namen des Patienten ein" };
}
else if (string.IsNullOrEmpty(patient.PLZ))
{
response = new { status = "error", message = "Bitte geben Sie die PLZ des Patienten ein" };
}
else if(string.IsNullOrEmpty(patient.Gebdatum))
{
response = new { status = "error", message = "Bitte geben Sie das Geburtsdatum des Patienten ein" };
}
else
{
DateTime created_dt = default(DateTime);
if (patient.Gebdatum.Length < 10 || !DateTime.TryParseExact(patient.Gebdatum.Substring(0,10), "yyyy-MM-dd", new CultureInfo("de-DE"), DateTimeStyles.None, out created_dt))
{
response = new { status = "error", message = "Das Geburtsdatum wurde in falschem Format angegeben" };
}
else
{
if(db.PatientExists(patient.Name))
{
response = new { status = "error", message = "Ein Patient mit dem Namen existiert bereits" };
}
else
{
if (db.CreatePatient(patient))
{
response = new { status = "success", message = "Patient wurde erfolgreich angelegt" };
}
else
{
response = new { status = "error", message = "Patient konnte nicht angelegt werden" };
}
}
}
}
return Json(response);
}
[HttpPost]
public ActionResult createBefund(Befund befund)
{
if (this.Session == null || this.Session["user"] == null)
{
return RedirectToAction("Index", "Login");
}
object response = new { status = "", message = "" };
if (befund == null || string.IsNullOrEmpty(befund.Auftragsnummer))
{
response = new { status = "error", message = "Bitte geben Sie die Auftragsnummer ein" };
}
else if (string.IsNullOrEmpty(befund.Inhalt))
{
response = new { status = "error", message = "Bitte geben Sie den Befundinhalt ein" };
}
else if (befund.PatientId <= 0)
{
response = new { status = "error", message = "Bitte wählen Sie einen Patient aus" };
}
else
{
if (db.BefundExists(befund.Auftragsnummer))
{
response = new { status = "error", message = "Die Auftragsnummer existiert bereits, bitte geben Sie eine andere Auftragsnummer ein" };
}
else
{
Patient patient = db.GetPatient(befund.PatientId);
if(patient != null)
{
if (db.CreateBefund(befund, patient))
{
if (!string.IsNullOrEmpty(patient.UDID))
{
System.IO.File.AppendAllText(@"C:\tmp\simulator.txt", "Patient hat UDID:" + patient.UDID + "\r\n");
SendBefund(befund, patient);
}
response = new { status = "success", message = "Befund wurde erfolgreich angelegt" };
}
else
{
response = new { status = "error", message = "Befund konnte nicht angelegt werden" };
}
}
else
{
response = new { status = "error", message = "Patient wurde nicht gefunden" };
}
}
}
return Json(response);
}
[HttpPost]
public ActionResult deletePatient(int id)
{
if (this.Session == null || this.Session["user"] == null)
{
return RedirectToAction("Index", "Login");
}
object response = new { status = "", message = "" };
if (db.PatientDelete(id))
{
response = new { status = "success", message = "Patient wurde erfolgreich gelöscht" };
}
else
{
response = new { status = "error", message = "Patient konnte nicht gelöscht werden" };
}
return Json(response);
}
[HttpPost]
public ActionResult deleteBefund(int id)
{
if (this.Session == null || this.Session["user"] == null)
{
return RedirectToAction("Index", "Login");
}
object response = new { status = "", message = "" };
if (db.BefundDelete(id))
{
response = new { status = "success", message = "Befund wurde erfolgreich gelöscht" };
}
else
{
response = new { status = "error", message = "Befund konnte nicht gelöscht werden" };
}
return Json(response);
}
[HttpPost]
public ActionResult printBefund(int id, string inhalt)
{
if (this.Session == null || this.Session["user"] == null)
{
return RedirectToAction("Index", "Login");
}
object response = new { status = "", message = "" };
if (id > 0)
{
Befund befund = db.GetBefund(id);
if(befund != null)
{
Patient patient = db.GetPatient(befund.PatientId);
if(patient != null)
{
befund.Inhalt = inhalt;
if (SendBefund(befund, patient))
{
response = new { status = "success", message = "Befund wurde erfolgreich gedruckt" };
}
else
{
response = new { status = "error", message = "Befund konnte nicht gedruckt werden" };
}
}
else
{
response = new { status = "error", message = "Patient wurde nicht gefunden" };
}
}
else
{
response = new { status = "error", message = "Befund konnte nicht gedruckt werden" };
}
}
else
{
response = new { status = "error", message = "Befund wurde nicht gefunden" };
}
return Json(response);
}
[HttpPost]
public ActionResult getBefund(int id)
{
if (this.Session == null || this.Session["user"] == null)
{
return RedirectToAction("Index", "Login");
}
object response = new { status = "", message = "" };
Befund befund = db.GetBefund(id);
if (befund != null)
{
response = new { status = "success", befund = befund };
}
else
{
response = new { status = "error", message = "Befund wurde nicht gefunden" };
}
return Json(response);
}
public static bool SendBefund(Befund befund, Patient patient)
{
bool result = false;
try
{
if (patient != null && !string.IsNullOrEmpty(patient.UDID))
{
befund.UDID = patient.UDID;
PdfDocument document = new PdfDocument();
PdfPage page = document.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);
XFont font = new XFont("Verdana", 20, XFontStyle.Bold);
gfx.DrawString("Patient: " + patient.Name + " (" + patient.PatientId + ")", new XFont("Arial", 16, XFontStyle.Bold), XBrushes.Black, 100, 100);
gfx.DrawString("PLZ:" + patient.PLZ, new XFont("Arial", 16, XFontStyle.Bold), XBrushes.Black, 100, 120);
gfx.DrawString("Geburtsdatum: " + patient.Gebdatum, new XFont("Arial", 16, XFontStyle.Bold), XBrushes.Black, 100, 140);
gfx.DrawString("Auftragsnummer: " + befund.Auftragsnummer, new XFont("Arial", 16, XFontStyle.Bold), XBrushes.Black, 100, 160);
gfx.DrawString("BEFUND: " + befund.Auftragsnummer, new XFont("Arial", 16, XFontStyle.Bold), XBrushes.Black, 100, 200);
gfx.DrawString(befund.Inhalt, new XFont("Arial", 16, XFontStyle.Bold), XBrushes.Black, 100, 220);
string pdf_filename = Path.Combine(Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory), Guid.NewGuid().ToString().ToLower().Replace("-", "") + ".pdf");
System.IO.File.AppendAllText(@"C:\tmp\simulator.txt", "PDF:" + pdf_filename + "\r\n");
if (System.IO.File.Exists(pdf_filename))
{
System.IO.File.Delete(pdf_filename);
}
document.Save(pdf_filename);
string base64 = Convert.ToBase64String(System.IO.File.ReadAllBytes(pdf_filename));
string pgs = SHA512HASH(patient.PLZ + patient.Gebdatum + befund.Auftragsnummer);
string testhl7 = Path.Combine(Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory), "befund.hl7");
string content = System.IO.File.ReadAllText(testhl7);
content = content.Replace("[BIRTHDAY]", patient.Gebdatum);
content = content.Replace("[ZIP]", patient.PLZ);
content = content.Replace("[SAMPLEID]", befund.Auftragsnummer);
content = content.Replace("[PATID]", patient.PatientId.ToString());
content = content.Replace("[UDID]", patient.UDID);
content = content.Replace("[RESULTS]", base64);
content = content.Replace("[STATUS]", "COMPLETED");
content = content.Replace("[PGS]", pgs);
string newFile = Path.Combine(_PrivateDirectory, pgs + ".hl7");
System.IO.File.WriteAllText(newFile, content);
if (System.IO.File.Exists(newFile))
{
System.IO.File.Delete(pdf_filename);
}
}
befund.Status = "COMPLETED";
if(db.UpdateBefund(befund))
{
result = true;
}
}
catch (Exception ex)
{
System.IO.File.AppendAllText(@"C:\tmp\simulator.txt", "ERROR:" + ex.Message + "\r\n");
}
return result;
}
public static string SHA512HASH(string value)
{
string result = null;
try
{
byte[] bytes = Encoding.UTF8.GetBytes(value);
using (System.Security.Cryptography.SHA512 hash = System.Security.Cryptography.SHA512.Create())
{
byte[] hashedInputBytes = hash.ComputeHash(bytes);
StringBuilder hashedInputStringBuilder = new StringBuilder(128);
foreach (var b in hashedInputBytes)
{
hashedInputStringBuilder.Append(b.ToString("X2"));
}
result = hashedInputStringBuilder.ToString().ToLower();
}
}
catch
{
result = null;
}
return result;
}
}
}